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

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