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

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