Blame SOURCES/0001-Use-getentropy-if-arc4random_buf-is-not-available.patch

1640c2
From 8044880840bcde6f15a078e267cf163072ac1878 Mon Sep 17 00:00:00 2001
1640c2
From: Benjamin Tissoires <benjamin.tissoires@gmail.com>
1640c2
Date: Tue, 4 Apr 2017 19:12:53 +0200
1640c2
Subject: [PATCH libICE 1/2] Use getentropy() if arc4random_buf() is not
1640c2
 available
1640c2
1640c2
This allows to fix CVE-2017-2626 on Linux platforms without pulling in
1640c2
libbsd.
1640c2
The libc getentropy() is available since glibc 2.25 but also on OpenBSD.
1640c2
For Linux, we need at least a v3.17 kernel. If the recommended
1640c2
arc4random_buf() function is not available, emulate it by first trying
1640c2
to use getentropy() on a supported glibc and kernel. If the call fails,
1640c2
fall back to the current (partly vulnerable) code.
1640c2
1640c2
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
1640c2
Reviewed-by: Mark Kettenis <kettenis@openbsd.org>
1640c2
Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
1640c2
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
1640c2
---
1640c2
 configure.ac  |  2 +-
1640c2
 src/iceauth.c | 65 ++++++++++++++++++++++++++++++++++++++++++-----------------
1640c2
 2 files changed, 47 insertions(+), 20 deletions(-)
1640c2
1640c2
diff --git a/configure.ac b/configure.ac
1640c2
index 458882a..c971ab6 100644
1640c2
--- a/configure.ac
1640c2
+++ b/configure.ac
1640c2
@@ -38,7 +38,7 @@ AC_DEFINE(ICE_t, 1, [Xtrans transport type])
1640c2
 
1640c2
 # Checks for library functions.
1640c2
 AC_CHECK_LIB([bsd], [arc4random_buf])
1640c2
-AC_CHECK_FUNCS([asprintf arc4random_buf])
1640c2
+AC_CHECK_FUNCS([asprintf arc4random_buf getentropy])
1640c2
 
1640c2
 # Allow checking code with lint, sparse, etc.
1640c2
 XORG_WITH_LINT
1640c2
diff --git a/src/iceauth.c b/src/iceauth.c
1640c2
index ef66626..9b77eac 100644
1640c2
--- a/src/iceauth.c
1640c2
+++ b/src/iceauth.c
1640c2
@@ -42,31 +42,19 @@ Author: Ralph Mor, X Consortium
1640c2
 
1640c2
 static int was_called_state;
1640c2
 
1640c2
-/*
1640c2
- * MIT-MAGIC-COOKIE-1 is a sample authentication method implemented by
1640c2
- * the SI.  It is not part of standard ICElib.
1640c2
- */
1640c2
+#ifndef HAVE_ARC4RANDOM_BUF
1640c2
 
1640c2
-
1640c2
-char *
1640c2
-IceGenerateMagicCookie (
1640c2
+static void
1640c2
+emulate_getrandom_buf (
1640c2
+	char *auth,
1640c2
 	int len
1640c2
 )
1640c2
 {
1640c2
-    char    *auth;
1640c2
-#ifndef HAVE_ARC4RANDOM_BUF
1640c2
     long    ldata[2];
1640c2
     int	    seed;
1640c2
     int	    value;
1640c2
     int	    i;
1640c2
-#endif
1640c2
 
1640c2
-    if ((auth = malloc (len + 1)) == NULL)
1640c2
-	return (NULL);
1640c2
-
1640c2
-#ifdef HAVE_ARC4RANDOM_BUF
1640c2
-    arc4random_buf(auth, len);
1640c2
-#else
1640c2
 #ifdef ITIMER_REAL
1640c2
     {
1640c2
 	struct timeval  now;
1640c2
@@ -74,13 +62,13 @@ IceGenerateMagicCookie (
1640c2
 	ldata[0] = now.tv_sec;
1640c2
 	ldata[1] = now.tv_usec;
1640c2
     }
1640c2
-#else
1640c2
+#else /* ITIMER_REAL */
1640c2
     {
1640c2
 	long    time ();
1640c2
 	ldata[0] = time ((long *) 0);
1640c2
 	ldata[1] = getpid ();
1640c2
     }
1640c2
-#endif
1640c2
+#endif /* ITIMER_REAL */
1640c2
     seed = (ldata[0]) + (ldata[1] << 16);
1640c2
     srand (seed);
1640c2
     for (i = 0; i < len; i++)
1640c2
@@ -88,7 +76,46 @@ IceGenerateMagicCookie (
1640c2
 	value = rand ();
1640c2
 	auth[i] = value & 0xff;
1640c2
     }
1640c2
-#endif
1640c2
+}
1640c2
+
1640c2
+static void
1640c2
+arc4random_buf (
1640c2
+	char *auth,
1640c2
+	int len
1640c2
+)
1640c2
+{
1640c2
+    int	    ret;
1640c2
+
1640c2
+#if HAVE_GETENTROPY
1640c2
+    /* weak emulation of arc4random through the entropy libc */
1640c2
+    ret = getentropy (auth, len);
1640c2
+    if (ret == 0)
1640c2
+	return;
1640c2
+#endif /* HAVE_GETENTROPY */
1640c2
+
1640c2
+    emulate_getrandom_buf (auth, len);
1640c2
+}
1640c2
+
1640c2
+#endif /* !defined(HAVE_ARC4RANDOM_BUF) */
1640c2
+
1640c2
+/*
1640c2
+ * MIT-MAGIC-COOKIE-1 is a sample authentication method implemented by
1640c2
+ * the SI.  It is not part of standard ICElib.
1640c2
+ */
1640c2
+
1640c2
+
1640c2
+char *
1640c2
+IceGenerateMagicCookie (
1640c2
+	int len
1640c2
+)
1640c2
+{
1640c2
+    char    *auth;
1640c2
+
1640c2
+    if ((auth = malloc (len + 1)) == NULL)
1640c2
+	return (NULL);
1640c2
+
1640c2
+    arc4random_buf (auth, len);
1640c2
+
1640c2
     auth[len] = '\0';
1640c2
     return (auth);
1640c2
 }
1640c2
-- 
1640c2
2.9.3
1640c2