| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sandbox/linux/services/libc_urandom_override.h" | 5 #include "sandbox/linux/services/libc_urandom_override.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 #include <stdio.h> | 9 #include <stdio.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 16 | 16 |
| 17 // Note: this file is used by the zygote and nacl_helper. | 17 // Note: this file is used by the zygote and nacl_helper. |
| 18 | 18 |
| 19 #if !defined(HAVE_XSTAT) && defined(LIBC_GLIBC) |
| 20 #define HAVE_XSTAT 1 |
| 21 #endif |
| 22 |
| 19 namespace sandbox { | 23 namespace sandbox { |
| 20 | 24 |
| 21 static bool g_override_urandom = false; | 25 static bool g_override_urandom = false; |
| 22 | 26 |
| 23 void InitLibcUrandomOverrides() { | |
| 24 // Make sure /dev/urandom is open. | |
| 25 base::GetUrandomFD(); | |
| 26 g_override_urandom = true; | |
| 27 } | |
| 28 | |
| 29 // TODO(sergeyu): Currently this code doesn't work properly under ASAN | 27 // TODO(sergeyu): Currently this code doesn't work properly under ASAN |
| 30 // - it crashes content_unittests. Make sure it works properly and | 28 // - it crashes content_unittests. Make sure it works properly and |
| 31 // enable it here. http://crbug.com/123263 | 29 // enable it here. http://crbug.com/123263 |
| 32 #if !defined(ADDRESS_SANITIZER) | 30 #if !defined(ADDRESS_SANITIZER) |
| 33 | 31 |
| 34 static const char kUrandomDevPath[] = "/dev/urandom"; | 32 static const char kUrandomDevPath[] = "/dev/urandom"; |
| 35 | 33 |
| 36 typedef FILE* (*FopenFunction)(const char* path, const char* mode); | 34 typedef FILE* (*FopenFunction)(const char* path, const char* mode); |
| 35 |
| 36 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; |
| 37 static FopenFunction g_libc_fopen = NULL; |
| 38 static FopenFunction g_libc_fopen64 = NULL; |
| 39 |
| 40 #if HAVE_XSTAT |
| 37 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); | 41 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
| 38 typedef int (*Xstat64Function)(int version, const char *path, | 42 typedef int (*Xstat64Function)(int version, const char *path, |
| 39 struct stat64 *buf); | 43 struct stat64 *buf); |
| 40 | 44 |
| 41 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | 45 static XstatFunction g_libc_xstat = NULL; |
| 42 static FopenFunction g_libc_fopen; | 46 static Xstat64Function g_libc_xstat64 = NULL; |
| 43 static FopenFunction g_libc_fopen64; | 47 #else |
| 44 static XstatFunction g_libc_xstat; | 48 typedef int (*StatFunction)(const char *path, struct stat *buf); |
| 45 static Xstat64Function g_libc_xstat64; | 49 typedef int (*Stat64Function)(const char *path, struct stat64 *buf); |
| 46 | 50 |
| 51 static StatFunction g_libc_stat = NULL; |
| 52 static Stat64Function g_libc_stat64 = NULL; |
| 53 #endif // HAVE_XSTAT |
| 54 |
| 55 // Find the libc's real fopen* and *stat* functions. This should only be |
| 56 // called once, and should be guarded by g_libc_file_io_funcs_guard. |
| 47 static void InitLibcFileIOFunctions() { | 57 static void InitLibcFileIOFunctions() { |
| 48 g_libc_fopen = reinterpret_cast<FopenFunction>( | 58 g_libc_fopen = reinterpret_cast<FopenFunction>( |
| 49 dlsym(RTLD_NEXT, "fopen")); | 59 dlsym(RTLD_NEXT, "fopen")); |
| 50 g_libc_fopen64 = reinterpret_cast<FopenFunction>( | 60 g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
| 51 dlsym(RTLD_NEXT, "fopen64")); | 61 dlsym(RTLD_NEXT, "fopen64")); |
| 52 | 62 |
| 53 if (!g_libc_fopen) { | 63 if (!g_libc_fopen) { |
| 54 LOG(FATAL) << "Failed to get fopen() from libc."; | 64 LOG(FATAL) << "Failed to get fopen() from libc."; |
| 55 } else if (!g_libc_fopen64) { | 65 } else if (!g_libc_fopen64) { |
| 56 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 66 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| 57 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; | 67 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; |
| 58 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 68 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| 59 g_libc_fopen64 = g_libc_fopen; | 69 g_libc_fopen64 = g_libc_fopen; |
| 60 } | 70 } |
| 61 | 71 |
| 62 #if defined(LIBC_GLIBC) | 72 #if HAVE_XSTAT |
| 63 // TODO(sergeyu): This works only on systems with glibc. Fix it to | |
| 64 // work properly on other systems if necessary. | |
| 65 g_libc_xstat = reinterpret_cast<XstatFunction>( | 73 g_libc_xstat = reinterpret_cast<XstatFunction>( |
| 66 dlsym(RTLD_NEXT, "__xstat")); | 74 dlsym(RTLD_NEXT, "__xstat")); |
| 67 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( | 75 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
| 68 dlsym(RTLD_NEXT, "__xstat64")); | 76 dlsym(RTLD_NEXT, "__xstat64")); |
| 69 | 77 |
| 70 if (!g_libc_xstat) { | 78 if (!g_libc_xstat) { |
| 71 LOG(FATAL) << "Failed to get __xstat() from libc."; | 79 LOG(FATAL) << "Failed to get __xstat() from libc."; |
| 72 } | 80 } |
| 73 if (!g_libc_xstat64) { | 81 if (!g_libc_xstat64) { |
| 74 LOG(WARNING) << "Failed to get __xstat64() from libc."; | 82 LOG(FATAL) << "Failed to get __xstat64() from libc."; |
| 75 } | 83 } |
| 84 #else |
| 85 g_libc_stat = reinterpret_cast<StatFunction>( |
| 86 dlsym(RTLD_NEXT, "stat")); |
| 87 g_libc_stat64 = reinterpret_cast<Stat64Function>( |
| 88 dlsym(RTLD_NEXT, "stat64")); |
| 89 |
| 90 if (!g_libc_stat) { |
| 91 LOG(FATAL) << "Failed to get stat() from libc."; |
| 92 } |
| 93 if (!g_libc_stat64) { |
| 94 LOG(FATAL) << "Failed to get stat64() from libc."; |
| 95 } |
| 96 #endif // HAVE_XSTAT |
| 97 } |
| 98 |
| 99 void InitLibcUrandomOverrides() { |
| 100 // Make sure /dev/urandom is open. |
| 101 base::GetUrandomFD(); |
| 102 g_override_urandom = true; |
| 103 |
| 104 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 105 InitLibcFileIOFunctions)); |
| 76 } | 106 } |
| 77 | 107 |
| 78 // fopen() and fopen64() are intercepted here so that NSS can open | 108 // fopen() and fopen64() are intercepted here so that NSS can open |
| 79 // /dev/urandom to seed its random number generator. NSS is used by | 109 // /dev/urandom to seed its random number generator. NSS is used by |
| 80 // remoting in the sendbox. | 110 // remoting in the sendbox. |
| 81 | 111 |
| 82 // fopen() call may be redirected to fopen64() in stdio.h using | 112 // fopen() call may be redirected to fopen64() in stdio.h using |
| 83 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This | 113 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This |
| 84 // means that we cannot override fopen() directly here. Instead the | 114 // means that we cannot override fopen() directly here. Instead the |
| 85 // the code below defines fopen_override() function with asm name | 115 // the code below defines fopen_override() function with asm name |
| (...skipping 27 matching lines...) Expand all Loading... |
| 113 return NULL; | 143 return NULL; |
| 114 } | 144 } |
| 115 return fdopen(fd, mode); | 145 return fdopen(fd, mode); |
| 116 } else { | 146 } else { |
| 117 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 147 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 118 InitLibcFileIOFunctions)); | 148 InitLibcFileIOFunctions)); |
| 119 return g_libc_fopen64(path, mode); | 149 return g_libc_fopen64(path, mode); |
| 120 } | 150 } |
| 121 } | 151 } |
| 122 | 152 |
| 123 // stat() is subject to the same problem as fopen(), so we have to use | 153 // The stat() family of functions are subject to the same problem as |
| 124 // the same trick to override it. | 154 // fopen(), so we have to use the same trick to override them. |
| 155 |
| 156 #if HAVE_XSTAT |
| 157 |
| 125 __attribute__ ((__visibility__("default"))) | 158 __attribute__ ((__visibility__("default"))) |
| 126 int xstat_override(int version, | 159 int xstat_override(int version, |
| 127 const char *path, | 160 const char *path, |
| 128 struct stat *buf) __asm__ ("__xstat"); | 161 struct stat *buf) __asm__ ("__xstat"); |
| 129 | 162 |
| 130 __attribute__ ((__visibility__("default"))) | 163 __attribute__ ((__visibility__("default"))) |
| 131 int xstat_override(int version, const char *path, struct stat *buf) { | 164 int xstat_override(int version, const char *path, struct stat *buf) { |
| 132 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 165 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 133 int result = __fxstat(version, base::GetUrandomFD(), buf); | 166 int result = __fxstat(version, base::GetUrandomFD(), buf); |
| 134 return result; | 167 return result; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 145 struct stat64 *buf) __asm__ ("__xstat64"); | 178 struct stat64 *buf) __asm__ ("__xstat64"); |
| 146 | 179 |
| 147 __attribute__ ((__visibility__("default"))) | 180 __attribute__ ((__visibility__("default"))) |
| 148 int xstat64_override(int version, const char *path, struct stat64 *buf) { | 181 int xstat64_override(int version, const char *path, struct stat64 *buf) { |
| 149 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 182 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 150 int result = __fxstat64(version, base::GetUrandomFD(), buf); | 183 int result = __fxstat64(version, base::GetUrandomFD(), buf); |
| 151 return result; | 184 return result; |
| 152 } else { | 185 } else { |
| 153 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 186 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 154 InitLibcFileIOFunctions)); | 187 InitLibcFileIOFunctions)); |
| 155 CHECK(g_libc_xstat64); | |
| 156 return g_libc_xstat64(version, path, buf); | 188 return g_libc_xstat64(version, path, buf); |
| 157 } | 189 } |
| 158 } | 190 } |
| 159 #endif // defined(LIBC_GLIBC) | 191 |
| 192 #else |
| 193 |
| 194 __attribute__ ((__visibility__("default"))) |
| 195 int stat_override(const char *path, |
| 196 struct stat *buf) __asm__ ("stat"); |
| 197 |
| 198 __attribute__ ((__visibility__("default"))) |
| 199 int stat_override(const char *path, struct stat *buf) { |
| 200 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 201 int result = fstat(base::GetUrandomFD(), buf); |
| 202 return result; |
| 203 } else { |
| 204 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 205 InitLibcFileIOFunctions)); |
| 206 return g_libc_stat(path, buf); |
| 207 } |
| 208 } |
| 209 |
| 210 __attribute__ ((__visibility__("default"))) |
| 211 int stat64_override(const char *path, |
| 212 struct stat64 *buf) __asm__ ("stat64"); |
| 213 |
| 214 __attribute__ ((__visibility__("default"))) |
| 215 int stat64_override(const char *path, struct stat64 *buf) { |
| 216 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 217 int result = fstat64(base::GetUrandomFD(), buf); |
| 218 return result; |
| 219 } else { |
| 220 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 221 InitLibcFileIOFunctions)); |
| 222 return g_libc_stat64(path, buf); |
| 223 } |
| 224 } |
| 225 |
| 226 #endif // HAVE_XSTAT |
| 160 | 227 |
| 161 #endif // !defined(ADDRESS_SANITIZER) | 228 #endif // !defined(ADDRESS_SANITIZER) |
| 162 | 229 |
| 163 } // namespace content | 230 } // namespace content |
| OLD | NEW |