|
Line 0
Link Here
|
|
|
1 |
From 44a643812ce3c07cd38972abfa9dbd163529c192 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Matthias Gerstner <mgerstner@suse.de> |
| 3 |
Date: Thu, 13 Jul 2017 14:58:04 +0200 |
| 4 |
Subject: [PATCH] Use better fallbacks to generate cookies if arc4random_buf(3) |
| 5 |
is unavailable |
| 6 |
References: bsc#1025084 |
| 7 |
|
| 8 |
If arc4random_buf() is not available for generating cookies: |
| 9 |
|
| 10 |
- use getentropy(), if available (which was only recently added to |
| 11 |
glibc) |
| 12 |
- use getrandom() via syscall(), if available (there was no glibc |
| 13 |
wrapper for this syscall for a long time) |
| 14 |
- if all else fails, directly read from /dev/urandom as before, but |
| 15 |
employ O_CLOEXEC, do an OsAbort() in case the random data couldn't be |
| 16 |
read to avoid unsecure situations. Don't know if that's too hard a |
| 17 |
measure but it shouldn't actually occur except on maximum number of |
| 18 |
FDs reached |
| 19 |
|
| 20 |
Reviewed-by: Stefan Dirsch <sndirsch@suse.de> |
| 21 |
--- |
| 22 |
configure.ac | 4 +- |
| 23 |
include/dix-config.h.in | 6 +++ |
| 24 |
os/auth.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++-- |
| 25 |
3 files changed, 141 insertions(+), 7 deletions(-) |
| 26 |
|
| 27 |
Index: xorg-server-1.6.5/configure.ac |
| 28 |
=================================================================== |
| 29 |
--- xorg-server-1.6.5.orig/configure.ac |
| 30 |
+++ xorg-server-1.6.5/configure.ac |
| 31 |
@@ -101,7 +101,7 @@ AM_CONDITIONAL(XSERVER_DTRACE, [test "x$ |
| 32 |
|
| 33 |
AC_HEADER_DIRENT |
| 34 |
AC_HEADER_STDC |
| 35 |
-AC_CHECK_HEADERS([fcntl.h stdlib.h string.h unistd.h]) |
| 36 |
+AC_CHECK_HEADERS([fcntl.h stdlib.h string.h unistd.h sys/syscall.h]) |
| 37 |
|
| 38 |
dnl Checks for typedefs, structures, and compiler characteristics. |
| 39 |
AC_C_CONST |
| 40 |
@@ -186,7 +186,7 @@ AC_CHECK_FUNCS([geteuid getuid link memm |
| 41 |
getisax getzoneid shmctl64 strcasestr ffs]) |
| 42 |
AC_REPLACE_FUNCS([timingsafe_memcmp]) |
| 43 |
AC_CHECK_LIB([bsd], [arc4random_buf]) |
| 44 |
-AC_CHECK_FUNCS([arc4random_buf]) |
| 45 |
+AC_CHECK_FUNCS([arc4random_buf getentropy]) |
| 46 |
AC_FUNC_ALLOCA |
| 47 |
dnl Old HAS_* names used in os/*.c. |
| 48 |
AC_CHECK_FUNC([getdtablesize], |
| 49 |
Index: xorg-server-1.6.5/include/dix-config.h.in |
| 50 |
=================================================================== |
| 51 |
--- xorg-server-1.6.5.orig/include/dix-config.h.in |
| 52 |
+++ xorg-server-1.6.5/include/dix-config.h.in |
| 53 |
@@ -160,6 +160,9 @@ |
| 54 |
/* Define to 1 if you have the `arc4random_buf' function. */ |
| 55 |
#undef HAVE_ARC4RANDOM_BUF |
| 56 |
|
| 57 |
+/* Define to 1 if you have the `getentropy' function. */ |
| 58 |
+#undef HAVE_GETENTROPY |
| 59 |
+ |
| 60 |
/* Define to use libmd SHA1 functions instead of OpenSSL libcrypto */ |
| 61 |
#undef HAVE_SHA1_IN_LIBMD |
| 62 |
|
| 63 |
@@ -213,6 +216,9 @@ |
| 64 |
/* Define to 1 if you have the <sys/vm86.h> header file. */ |
| 65 |
#undef HAVE_SYS_VM86_H |
| 66 |
|
| 67 |
+/* Define to 1 if you have the <sys/syscall.h> header file. */ |
| 68 |
+#undef HAVE_SYS_SYSCALL_H |
| 69 |
+ |
| 70 |
/* Define to 1 if you have the `timingsafe_memcmp' function. */ |
| 71 |
#undef HAVE_TIMINGSAFE_MEMCMP |
| 72 |
|
| 73 |
Index: xorg-server-1.6.5/os/auth.c |
| 74 |
=================================================================== |
| 75 |
--- xorg-server-1.6.5.orig/os/auth.c |
| 76 |
+++ xorg-server-1.6.5/os/auth.c |
| 77 |
@@ -48,6 +48,10 @@ from The Open Group. |
| 78 |
#ifdef HAVE_LIBBSD |
| 79 |
#include <bsd/stdlib.h> /* for arc4random_buf() */ |
| 80 |
#endif |
| 81 |
+#include <errno.h> |
| 82 |
+#ifdef HAVE_SYS_SYSCALL_H |
| 83 |
+#include <syscall.h> |
| 84 |
+#endif |
| 85 |
|
| 86 |
struct protocol { |
| 87 |
unsigned short name_length; |
| 88 |
@@ -316,18 +320,141 @@ GenerateAuthorization( |
| 89 |
return -1; |
| 90 |
} |
| 91 |
|
| 92 |
+#if ! defined(HAVE_ARC4RANDOM_BUF) |
| 93 |
+ |
| 94 |
+// fallback function to get random data directly from /dev/urandom |
| 95 |
+ |
| 96 |
+static int |
| 97 |
+GetUrandom ( char *buffer, size_t length ) |
| 98 |
+{ |
| 99 |
+ int random_fd = -1; |
| 100 |
+ int res = -1; |
| 101 |
+ size_t filled = 0; |
| 102 |
+ |
| 103 |
+ // larger requests are typically rejected by getentropy() / getrandom() |
| 104 |
+ // because they could block or return partially filled buffers |
| 105 |
+ if( length > 256 ) { |
| 106 |
+ errno = EIO; |
| 107 |
+ return -1; |
| 108 |
+ } |
| 109 |
+ |
| 110 |
+ random_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); |
| 111 |
+ |
| 112 |
+ if( random_fd == -1 ) { |
| 113 |
+ return -1; |
| 114 |
+ } |
| 115 |
+ |
| 116 |
+ while( filled < length ) { |
| 117 |
+ res = read(random_fd, (char*)buffer + filled, length - filled); |
| 118 |
+ |
| 119 |
+ if( res == -1 ) { |
| 120 |
+ // shouldn't actually happen acc. to man(4) random, |
| 121 |
+ // but you never know |
| 122 |
+ if( errno == EINTR ) { |
| 123 |
+ continue; |
| 124 |
+ } |
| 125 |
+ |
| 126 |
+ res = errno; |
| 127 |
+ close(random_fd); |
| 128 |
+ errno = res; |
| 129 |
+ return -1; |
| 130 |
+ } |
| 131 |
+ else if( res == 0 ) { |
| 132 |
+ close(random_fd); |
| 133 |
+ // no more bytes available? should not happen |
| 134 |
+ errno = EIO; |
| 135 |
+ return -1; |
| 136 |
+ } |
| 137 |
+ |
| 138 |
+ filled += res; |
| 139 |
+ } |
| 140 |
+ |
| 141 |
+ close(random_fd); |
| 142 |
+ |
| 143 |
+ return 0; |
| 144 |
+} |
| 145 |
+ |
| 146 |
+#endif // ! defined(HAVE_ARC4RANDOM_BUF) |
| 147 |
+ |
| 148 |
+#if !defined(HAVE_GETENTROPY) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_getrandom) |
| 149 |
+# define TRY_GETRANDOM |
| 150 |
+#endif |
| 151 |
+ |
| 152 |
+#ifdef TRY_GETRANDOM |
| 153 |
+ |
| 154 |
+/* |
| 155 |
+ * wrapper for the getrandom() syscall which was for a long time implemented |
| 156 |
+ * in the Linux kernel, but not wrapped in glibc |
| 157 |
+ */ |
| 158 |
+static int |
| 159 |
+GetRandom ( char *buffer, size_t length ) |
| 160 |
+{ |
| 161 |
+ int res; |
| 162 |
+ size_t filled = 0; |
| 163 |
+ |
| 164 |
+ // larger requests are typically rejected by getentropy() / getrandom() |
| 165 |
+ // because they could block or return partially filled buffers |
| 166 |
+ if( length > 256 ) |
| 167 |
+ { |
| 168 |
+ errno = EIO; |
| 169 |
+ return -1; |
| 170 |
+ } |
| 171 |
+ |
| 172 |
+ while( filled < length ) |
| 173 |
+ { |
| 174 |
+ /* |
| 175 |
+ * glibc does not contain a syscall wrapper for this in older |
| 176 |
+ * versions |
| 177 |
+ */ |
| 178 |
+ res = syscall(SYS_getrandom, (char*)buffer + filled, length - filled, 0); |
| 179 |
+ |
| 180 |
+ if( res == -1 ) |
| 181 |
+ { |
| 182 |
+ if( errno == EINTR ) { |
| 183 |
+ continue; |
| 184 |
+ } |
| 185 |
+ |
| 186 |
+ return -1; |
| 187 |
+ } |
| 188 |
+ else if( res == 0 ) |
| 189 |
+ { |
| 190 |
+ // no more bytes available? should not happen |
| 191 |
+ errno = EIO; |
| 192 |
+ return -1; |
| 193 |
+ } |
| 194 |
+ |
| 195 |
+ filled += res; |
| 196 |
+ } |
| 197 |
+ |
| 198 |
+ return 0; |
| 199 |
+} |
| 200 |
+ |
| 201 |
+#endif /* TRY_GETRANDOM */ |
| 202 |
+ |
| 203 |
void |
| 204 |
GenerateRandomData (int len, char *buf) |
| 205 |
{ |
| 206 |
#ifdef HAVE_ARC4RANDOM_BUF |
| 207 |
arc4random_buf(buf, len); |
| 208 |
#else |
| 209 |
- int fd; |
| 210 |
- |
| 211 |
- fd = open("/dev/urandom", O_RDONLY); |
| 212 |
- read(fd, buf, len); |
| 213 |
- close(fd); |
| 214 |
+ int ret = -1; |
| 215 |
+#ifdef HAVE_GETENTROPY |
| 216 |
+ /* use getentropy instead */ |
| 217 |
+ ret = getentropy (buf, len); |
| 218 |
+#elif defined(TRY_GETRANDOM) |
| 219 |
+ /* try getrandom() wrapper */ |
| 220 |
+ ret = GetRandom(buf, len); |
| 221 |
#endif |
| 222 |
+ |
| 223 |
+ if( ret == -1 ) { |
| 224 |
+ // fallback to manual reading of /dev/urandom |
| 225 |
+ ret = GetUrandom(buf, len); |
| 226 |
+ } |
| 227 |
+ if( ret == -1 ) { |
| 228 |
+ // no error return possible, rather abort than have security problems |
| 229 |
+ OsAbort(); |
| 230 |
+ } |
| 231 |
+#endif // HAVE_ARC4RANDOM_BUF |
| 232 |
} |
| 233 |
|
| 234 |
#endif /* XCSECURITY */ |
| 235 |
Index: xorg-server-1.6.5/include/os.h |
| 236 |
=================================================================== |
| 237 |
--- xorg-server-1.6.5.orig/include/os.h |
| 238 |
+++ xorg-server-1.6.5/include/os.h |
| 239 |
@@ -264,6 +264,9 @@ void OsBlockSignals (void); |
| 240 |
|
| 241 |
void OsReleaseSignals (void); |
| 242 |
|
| 243 |
+extern _X_EXPORT void |
| 244 |
+OsAbort(void); |
| 245 |
+ |
| 246 |
#if !defined(WIN32) |
| 247 |
extern int System(char *); |
| 248 |
extern pointer Popen(char *, char *); |
| 249 |
Index: xorg-server-1.6.5/os/utils.c |
| 250 |
=================================================================== |
| 251 |
--- xorg-server-1.6.5.orig/os/utils.c |
| 252 |
+++ xorg-server-1.6.5/os/utils.c |
| 253 |
@@ -1345,6 +1345,26 @@ OsReleaseSignals (void) |
| 254 |
#endif |
| 255 |
} |
| 256 |
|
| 257 |
+/* |
| 258 |
+ * Pending signals may interfere with core dumping. Provide a |
| 259 |
+ * mechanism to block signals when aborting. |
| 260 |
+ */ |
| 261 |
+ |
| 262 |
+void |
| 263 |
+OsAbort(void) |
| 264 |
+{ |
| 265 |
+#ifndef __APPLE__ |
| 266 |
+ OsBlockSignals(); |
| 267 |
+#endif |
| 268 |
+#if !defined(WIN32) || defined(__CYGWIN__) |
| 269 |
+ /* abort() raises SIGABRT, so we have to stop handling that to prevent |
| 270 |
+ * recursion |
| 271 |
+ */ |
| 272 |
+ OsSignal(SIGABRT, SIG_DFL); |
| 273 |
+#endif |
| 274 |
+ abort(); |
| 275 |
+} |
| 276 |
+ |
| 277 |
#if !defined(WIN32) |
| 278 |
/* |
| 279 |
* "safer" versions of system(3), popen(3) and pclose(3) which give up |