Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 // This is used when mapping stat* calls to __xstat* calls. | |
| 20 #define XSTAT_VERSION 3 | |
| 21 | |
| 19 namespace sandbox { | 22 namespace sandbox { |
| 20 | 23 |
| 21 static bool g_override_urandom = false; | 24 static bool g_override_urandom = false; |
| 22 | 25 |
| 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 | 26 // TODO(sergeyu): Currently this code doesn't work properly under ASAN |
| 30 // - it crashes content_unittests. Make sure it works properly and | 27 // - it crashes content_unittests. Make sure it works properly and |
| 31 // enable it here. http://crbug.com/123263 | 28 // enable it here. http://crbug.com/123263 |
| 32 #if !defined(ADDRESS_SANITIZER) | 29 #if !defined(ADDRESS_SANITIZER) |
| 33 | 30 |
| 34 static const char kUrandomDevPath[] = "/dev/urandom"; | 31 static const char kUrandomDevPath[] = "/dev/urandom"; |
| 35 | 32 |
| 36 typedef FILE* (*FopenFunction)(const char* path, const char* mode); | 33 typedef FILE* (*FopenFunction)(const char* path, const char* mode); |
| 37 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); | 34 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
| 38 typedef int (*Xstat64Function)(int version, const char *path, | 35 typedef int (*Xstat64Function)(int version, const char *path, |
| 39 struct stat64 *buf); | 36 struct stat64 *buf); |
| 37 typedef int (*StatFunction)(const char *path, struct stat *buf); | |
| 38 typedef int (*Stat64Function)(const char *path, struct stat64 *buf); | |
| 40 | 39 |
| 41 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | 40 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; |
| 42 static FopenFunction g_libc_fopen; | 41 static FopenFunction g_libc_fopen = NULL; |
| 43 static FopenFunction g_libc_fopen64; | 42 static FopenFunction g_libc_fopen64 = NULL; |
| 44 static XstatFunction g_libc_xstat; | 43 static XstatFunction g_libc_xstat = NULL; |
| 45 static Xstat64Function g_libc_xstat64; | 44 static Xstat64Function g_libc_xstat64 = NULL; |
| 45 static StatFunction g_libc_stat = NULL; | |
| 46 static Stat64Function g_libc_stat64 = NULL; | |
| 46 | 47 |
| 48 void InitLibcUrandomOverrides() { | |
| 49 // Make sure /dev/urandom is open. | |
| 50 base::GetUrandomFD(); | |
| 51 g_override_urandom = true; | |
| 52 } | |
| 53 | |
| 54 // Find the libc's real fopen* and *stat* functions. | |
| 47 static void InitLibcFileIOFunctions() { | 55 static void InitLibcFileIOFunctions() { |
| 56 | |
| 57 // TODO(mostynb): call this once near the start of the init sequence, don't | |
| 58 // call it repeatedly with pthread_once() - ZygoteMain is not early enough. | |
| 59 | |
| 48 g_libc_fopen = reinterpret_cast<FopenFunction>( | 60 g_libc_fopen = reinterpret_cast<FopenFunction>( |
| 49 dlsym(RTLD_NEXT, "fopen")); | 61 dlsym(RTLD_NEXT, "fopen")); |
| 50 g_libc_fopen64 = reinterpret_cast<FopenFunction>( | 62 g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
| 51 dlsym(RTLD_NEXT, "fopen64")); | 63 dlsym(RTLD_NEXT, "fopen64")); |
| 52 | 64 |
| 53 if (!g_libc_fopen) { | 65 if (!g_libc_fopen) { |
| 54 LOG(FATAL) << "Failed to get fopen() from libc."; | 66 LOG(FATAL) << "Failed to get fopen() from libc."; |
| 55 } else if (!g_libc_fopen64) { | 67 } else if (!g_libc_fopen64) { |
| 56 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 68 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| 57 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; | 69 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; |
| 58 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 70 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| 59 g_libc_fopen64 = g_libc_fopen; | 71 g_libc_fopen64 = g_libc_fopen; |
| 60 } | 72 } |
| 61 | 73 |
| 62 #if defined(LIBC_GLIBC) | 74 // Note: we attempt to dlsym both stat and __xstat but only expect one |
| 63 // TODO(sergeyu): This works only on systems with glibc. Fix it to | 75 // of the calls to succeed (and similarly for the *64 versions). This |
| 64 // work properly on other systems if necessary. | 76 // should work with both glibc and uClibc, and hopefully other libc's. |
| 77 // Background: glibc headers inline stat into __xstat which it exports, | |
| 78 // while uClibc exports stat and does not implement __xstat. | |
| 79 | |
| 65 g_libc_xstat = reinterpret_cast<XstatFunction>( | 80 g_libc_xstat = reinterpret_cast<XstatFunction>( |
| 66 dlsym(RTLD_NEXT, "__xstat")); | 81 dlsym(RTLD_NEXT, "__xstat")); |
| 67 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( | 82 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
| 68 dlsym(RTLD_NEXT, "__xstat64")); | 83 dlsym(RTLD_NEXT, "__xstat64")); |
| 84 g_libc_stat = reinterpret_cast<StatFunction>( | |
| 85 dlsym(RTLD_NEXT, "stat")); | |
| 86 g_libc_stat64 = reinterpret_cast<Stat64Function>( | |
| 87 dlsym(RTLD_NEXT, "stat64")); | |
| 69 | 88 |
| 70 if (!g_libc_xstat) { | 89 if (!g_libc_xstat && !g_libc_stat) { |
| 71 LOG(FATAL) << "Failed to get __xstat() from libc."; | 90 LOG(FATAL) << "Neither __xstat nor stat found."; |
| 72 } | 91 } |
| 73 if (!g_libc_xstat64) { | 92 if (!g_libc_xstat64 && !g_libc_stat64) { |
| 74 LOG(WARNING) << "Failed to get __xstat64() from libc."; | 93 LOG(FATAL) << "Neither __xstat64 nor stat64 found."; |
| 75 } | 94 } |
| 76 } | 95 } |
| 77 | 96 |
| 78 // fopen() and fopen64() are intercepted here so that NSS can open | 97 // fopen() and fopen64() are intercepted here so that NSS can open |
| 79 // /dev/urandom to seed its random number generator. NSS is used by | 98 // /dev/urandom to seed its random number generator. NSS is used by |
| 80 // remoting in the sendbox. | 99 // remoting in the sendbox. |
| 81 | 100 |
| 82 // fopen() call may be redirected to fopen64() in stdio.h using | 101 // fopen() call may be redirected to fopen64() in stdio.h using |
| 83 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This | 102 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This |
| 84 // means that we cannot override fopen() directly here. Instead the | 103 // means that we cannot override fopen() directly here. Instead the |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 113 return NULL; | 132 return NULL; |
| 114 } | 133 } |
| 115 return fdopen(fd, mode); | 134 return fdopen(fd, mode); |
| 116 } else { | 135 } else { |
| 117 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 136 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 118 InitLibcFileIOFunctions)); | 137 InitLibcFileIOFunctions)); |
| 119 return g_libc_fopen64(path, mode); | 138 return g_libc_fopen64(path, mode); |
| 120 } | 139 } |
| 121 } | 140 } |
| 122 | 141 |
| 123 // stat() is subject to the same problem as fopen(), so we have to use | 142 // The stat() family of functions are subject to the same problem as |
| 124 // the same trick to override it. | 143 // fopen(), so we have to use the same trick to override them. |
| 144 | |
| 125 __attribute__ ((__visibility__("default"))) | 145 __attribute__ ((__visibility__("default"))) |
| 126 int xstat_override(int version, | 146 int xstat_override(int version, |
| 127 const char *path, | 147 const char *path, |
| 128 struct stat *buf) __asm__ ("__xstat"); | 148 struct stat *buf) __asm__ ("__xstat"); |
| 129 | 149 |
| 130 __attribute__ ((__visibility__("default"))) | 150 __attribute__ ((__visibility__("default"))) |
| 131 int xstat_override(int version, const char *path, struct stat *buf) { | 151 int xstat_override(int version, const char *path, struct stat *buf) { |
| 152 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 153 InitLibcFileIOFunctions)); | |
| 154 | |
| 132 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 155 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 133 int result = __fxstat(version, base::GetUrandomFD(), buf); | 156 int result = fstat(base::GetUrandomFD(), buf); |
|
jln (very slow on Chromium)
2013/06/26 01:57:21
Loosing "version" here, may lead into very subtle
Mostyn Bramley-Moore
2013/06/26 10:30:18
Done (reverted).
| |
| 134 return result; | 157 return result; |
| 158 } | |
| 159 else if (!g_libc_xstat) { | |
| 160 return g_libc_stat(path, buf); | |
| 135 } else { | 161 } else { |
| 136 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 137 InitLibcFileIOFunctions)); | |
| 138 return g_libc_xstat(version, path, buf); | 162 return g_libc_xstat(version, path, buf); |
| 139 } | 163 } |
| 140 } | 164 } |
| 141 | 165 |
| 142 __attribute__ ((__visibility__("default"))) | 166 __attribute__ ((__visibility__("default"))) |
| 143 int xstat64_override(int version, | 167 int xstat64_override(int version, |
| 144 const char *path, | 168 const char *path, |
| 145 struct stat64 *buf) __asm__ ("__xstat64"); | 169 struct stat64 *buf) __asm__ ("__xstat64"); |
| 146 | 170 |
| 147 __attribute__ ((__visibility__("default"))) | 171 __attribute__ ((__visibility__("default"))) |
| 148 int xstat64_override(int version, const char *path, struct stat64 *buf) { | 172 int xstat64_override(int version, const char *path, struct stat64 *buf) { |
| 173 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 174 InitLibcFileIOFunctions)); | |
| 175 | |
| 149 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 176 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 150 int result = __fxstat64(version, base::GetUrandomFD(), buf); | 177 int result = fstat64(base::GetUrandomFD(), buf); |
| 151 return result; | 178 return result; |
| 179 } | |
| 180 else if (!g_libc_xstat64) { | |
| 181 return g_libc_stat64(path, buf); | |
| 152 } else { | 182 } else { |
| 153 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 154 InitLibcFileIOFunctions)); | |
| 155 CHECK(g_libc_xstat64); | |
| 156 return g_libc_xstat64(version, path, buf); | 183 return g_libc_xstat64(version, path, buf); |
| 157 } | 184 } |
| 158 } | 185 } |
| 159 #endif // defined(LIBC_GLIBC) | 186 |
| 187 __attribute__ ((__visibility__("default"))) | |
| 188 int stat_override(const char *path, | |
| 189 struct stat *buf) __asm__ ("stat"); | |
| 190 | |
| 191 __attribute__ ((__visibility__("default"))) | |
| 192 int stat_override(const char *path, struct stat *buf) { | |
| 193 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
| 194 int result = fstat(base::GetUrandomFD(), buf); | |
| 195 return result; | |
| 196 } | |
| 197 | |
| 198 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 199 InitLibcFileIOFunctions)); | |
| 200 | |
| 201 if (!g_libc_stat) { | |
| 202 return g_libc_xstat(XSTAT_VERSION, path, buf); | |
| 203 } else { | |
| 204 return g_libc_stat(path, buf); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 __attribute__ ((__visibility__("default"))) | |
| 209 int stat64_override(const char *path, | |
| 210 struct stat64 *buf) __asm__ ("stat64"); | |
| 211 | |
| 212 __attribute__ ((__visibility__("default"))) | |
| 213 int stat64_override(const char *path, struct stat64 *buf) { | |
| 214 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
| 215 int result = fstat64(base::GetUrandomFD(), buf); | |
| 216 return result; | |
| 217 } | |
| 218 | |
| 219 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 220 InitLibcFileIOFunctions)); | |
| 221 | |
| 222 if (!g_libc_stat64) { | |
| 223 return g_libc_xstat64(XSTAT_VERSION, path, buf); | |
| 224 } else { | |
| 225 return g_libc_stat64(path, buf); | |
| 226 } | |
| 227 } | |
| 160 | 228 |
| 161 #endif // !defined(ADDRESS_SANITIZER) | 229 #endif // !defined(ADDRESS_SANITIZER) |
| 162 | 230 |
| 163 } // namespace content | 231 } // namespace content |
| OLD | NEW |