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. |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
You can, but don't need to update this anymore.
Mostyn Bramley-Moore
2013/06/27 00:26:10
I will revert this if you prefer?
| |
| 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 | |
| 23 #if HAVE_XSTAT | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
#if defined(HAVE_XSTAT)
Mostyn Bramley-Moore
2013/06/27 00:26:10
I would prefer to use the value of HAVE_XSTAT inst
| |
| 24 // This is used when mapping stat* calls to __xstat* calls. | |
| 25 #define XSTAT_VERSION 3 | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
We really shouldn't hardcode this.
This would lea
Mostyn Bramley-Moore
2013/06/27 00:26:10
This is only used when we are overriding stat/stat
| |
| 26 #endif | |
| 27 | |
| 19 namespace sandbox { | 28 namespace sandbox { |
| 20 | 29 |
| 21 static bool g_override_urandom = false; | 30 static bool g_override_urandom = false; |
| 22 | 31 |
| 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 | 32 // TODO(sergeyu): Currently this code doesn't work properly under ASAN |
| 30 // - it crashes content_unittests. Make sure it works properly and | 33 // - it crashes content_unittests. Make sure it works properly and |
| 31 // enable it here. http://crbug.com/123263 | 34 // enable it here. http://crbug.com/123263 |
| 32 #if !defined(ADDRESS_SANITIZER) | 35 #if !defined(ADDRESS_SANITIZER) |
| 33 | 36 |
| 34 static const char kUrandomDevPath[] = "/dev/urandom"; | 37 static const char kUrandomDevPath[] = "/dev/urandom"; |
| 35 | 38 |
| 36 typedef FILE* (*FopenFunction)(const char* path, const char* mode); | 39 typedef FILE* (*FopenFunction)(const char* path, const char* mode); |
| 40 typedef int (*StatFunction)(const char *path, struct stat *buf); | |
| 41 typedef int (*Stat64Function)(const char *path, struct stat64 *buf); | |
| 42 | |
| 43 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | |
| 44 static FopenFunction g_libc_fopen = NULL; | |
| 45 static FopenFunction g_libc_fopen64 = NULL; | |
| 46 static StatFunction g_libc_stat = NULL; | |
| 47 static Stat64Function g_libc_stat64 = NULL; | |
| 48 | |
| 49 #if HAVE_XSTAT | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
#if defined(HAVE_XSTAT)
| |
| 37 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); | 50 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
| 38 typedef int (*Xstat64Function)(int version, const char *path, | 51 typedef int (*Xstat64Function)(int version, const char *path, |
| 39 struct stat64 *buf); | 52 struct stat64 *buf); |
| 40 | 53 |
| 41 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | 54 static XstatFunction g_libc_xstat = NULL; |
| 42 static FopenFunction g_libc_fopen; | 55 static Xstat64Function g_libc_xstat64 = NULL; |
| 43 static FopenFunction g_libc_fopen64; | 56 #endif // HAVE_XSTAT |
| 44 static XstatFunction g_libc_xstat; | |
| 45 static Xstat64Function g_libc_xstat64; | |
| 46 | 57 |
| 58 void InitLibcUrandomOverrides() { | |
| 59 // Make sure /dev/urandom is open. | |
| 60 base::GetUrandomFD(); | |
| 61 g_override_urandom = true; | |
| 62 | |
| 63 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 64 InitLibcFileIOFunctions()); | |
| 65 } | |
| 66 | |
| 67 // Find the libc's real fopen* and *stat* functions. This should only be | |
| 68 // called once, and should be guarded by g_libc_file_io_funcs_guard. | |
| 47 static void InitLibcFileIOFunctions() { | 69 static void InitLibcFileIOFunctions() { |
| 48 g_libc_fopen = reinterpret_cast<FopenFunction>( | 70 g_libc_fopen = reinterpret_cast<FopenFunction>( |
| 49 dlsym(RTLD_NEXT, "fopen")); | 71 dlsym(RTLD_NEXT, "fopen")); |
| 50 g_libc_fopen64 = reinterpret_cast<FopenFunction>( | 72 g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
| 51 dlsym(RTLD_NEXT, "fopen64")); | 73 dlsym(RTLD_NEXT, "fopen64")); |
| 52 | 74 |
| 53 if (!g_libc_fopen) { | 75 if (!g_libc_fopen) { |
| 54 LOG(FATAL) << "Failed to get fopen() from libc."; | 76 LOG(FATAL) << "Failed to get fopen() from libc."; |
| 55 } else if (!g_libc_fopen64) { | 77 } else if (!g_libc_fopen64) { |
| 56 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 78 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| 57 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; | 79 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; |
| 58 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 80 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| 59 g_libc_fopen64 = g_libc_fopen; | 81 g_libc_fopen64 = g_libc_fopen; |
| 60 } | 82 } |
| 61 | 83 |
| 62 #if defined(LIBC_GLIBC) | 84 g_libc_stat = reinterpret_cast<StatFunction>( |
| 63 // TODO(sergeyu): This works only on systems with glibc. Fix it to | 85 dlsym(RTLD_NEXT, "stat")); |
| 64 // work properly on other systems if necessary. | 86 g_libc_stat64 = reinterpret_cast<Stat64Function>( |
| 87 dlsym(RTLD_NEXT, "stat64")); | |
| 88 | |
| 89 #if HAVE_XSTAT | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
#if defined(HAVE_XSTAT)
| |
| 65 g_libc_xstat = reinterpret_cast<XstatFunction>( | 90 g_libc_xstat = reinterpret_cast<XstatFunction>( |
| 66 dlsym(RTLD_NEXT, "__xstat")); | 91 dlsym(RTLD_NEXT, "__xstat")); |
| 67 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( | 92 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
| 68 dlsym(RTLD_NEXT, "__xstat64")); | 93 dlsym(RTLD_NEXT, "__xstat64")); |
| 69 | 94 |
| 70 if (!g_libc_xstat) { | 95 if (!g_libc_xstat) { |
| 71 LOG(FATAL) << "Failed to get __xstat() from libc."; | 96 LOG(FATAL) << "Failed to get __xstat() from libc."; |
| 72 } | 97 } |
| 73 if (!g_libc_xstat64) { | 98 if (!g_libc_xstat64) { |
| 74 LOG(WARNING) << "Failed to get __xstat64() from libc."; | 99 LOG(FATAL) << "Failed to get __xstat64() from libc."; |
| 75 } | 100 } |
| 101 #else | |
| 102 if (!g_libc_stat) { | |
| 103 LOG(FATAL) << "Failed to get stat() from libc."; | |
| 104 } | |
| 105 if (!g_libc_stat64) { | |
| 106 LOG(FATAL) << "Failed to get stat64() from libc."; | |
| 107 } | |
| 108 #endif // HAVE_XSTAT | |
| 76 } | 109 } |
| 77 | 110 |
| 78 // fopen() and fopen64() are intercepted here so that NSS can open | 111 // fopen() and fopen64() are intercepted here so that NSS can open |
| 79 // /dev/urandom to seed its random number generator. NSS is used by | 112 // /dev/urandom to seed its random number generator. NSS is used by |
| 80 // remoting in the sendbox. | 113 // remoting in the sendbox. |
| 81 | 114 |
| 82 // fopen() call may be redirected to fopen64() in stdio.h using | 115 // fopen() call may be redirected to fopen64() in stdio.h using |
| 83 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This | 116 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This |
| 84 // means that we cannot override fopen() directly here. Instead the | 117 // means that we cannot override fopen() directly here. Instead the |
| 85 // the code below defines fopen_override() function with asm name | 118 // the code below defines fopen_override() function with asm name |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 113 return NULL; | 146 return NULL; |
| 114 } | 147 } |
| 115 return fdopen(fd, mode); | 148 return fdopen(fd, mode); |
| 116 } else { | 149 } else { |
| 117 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 150 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 118 InitLibcFileIOFunctions)); | 151 InitLibcFileIOFunctions)); |
| 119 return g_libc_fopen64(path, mode); | 152 return g_libc_fopen64(path, mode); |
| 120 } | 153 } |
| 121 } | 154 } |
| 122 | 155 |
| 123 // stat() is subject to the same problem as fopen(), so we have to use | 156 // The stat() family of functions are subject to the same problem as |
| 124 // the same trick to override it. | 157 // fopen(), so we have to use the same trick to override them. |
| 158 | |
| 159 #if HAVE_XSTAT | |
| 160 | |
| 125 __attribute__ ((__visibility__("default"))) | 161 __attribute__ ((__visibility__("default"))) |
| 126 int xstat_override(int version, | 162 int xstat_override(int version, |
| 127 const char *path, | 163 const char *path, |
| 128 struct stat *buf) __asm__ ("__xstat"); | 164 struct stat *buf) __asm__ ("__xstat"); |
| 129 | 165 |
| 130 __attribute__ ((__visibility__("default"))) | 166 __attribute__ ((__visibility__("default"))) |
| 131 int xstat_override(int version, const char *path, struct stat *buf) { | 167 int xstat_override(int version, const char *path, struct stat *buf) { |
| 132 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 168 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 133 int result = __fxstat(version, base::GetUrandomFD(), buf); | 169 int result = __fxstat(version, base::GetUrandomFD(), buf); |
| 134 return result; | 170 return result; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 145 struct stat64 *buf) __asm__ ("__xstat64"); | 181 struct stat64 *buf) __asm__ ("__xstat64"); |
| 146 | 182 |
| 147 __attribute__ ((__visibility__("default"))) | 183 __attribute__ ((__visibility__("default"))) |
| 148 int xstat64_override(int version, const char *path, struct stat64 *buf) { | 184 int xstat64_override(int version, const char *path, struct stat64 *buf) { |
| 149 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 185 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 150 int result = __fxstat64(version, base::GetUrandomFD(), buf); | 186 int result = __fxstat64(version, base::GetUrandomFD(), buf); |
| 151 return result; | 187 return result; |
| 152 } else { | 188 } else { |
| 153 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 189 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 154 InitLibcFileIOFunctions)); | 190 InitLibcFileIOFunctions)); |
| 155 CHECK(g_libc_xstat64); | |
| 156 return g_libc_xstat64(version, path, buf); | 191 return g_libc_xstat64(version, path, buf); |
| 157 } | 192 } |
| 158 } | 193 } |
| 159 #endif // defined(LIBC_GLIBC) | 194 |
| 195 #endif // HAVE_XSTAT | |
| 196 | |
| 197 __attribute__ ((__visibility__("default"))) | |
| 198 int stat_override(const char *path, | |
| 199 struct stat *buf) __asm__ ("stat"); | |
| 200 | |
| 201 __attribute__ ((__visibility__("default"))) | |
| 202 int stat_override(const char *path, struct stat *buf) { | |
| 203 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
| 204 int result = fstat(base::GetUrandomFD(), buf); | |
| 205 return result; | |
| 206 } | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
do you want to use the } else { construct as above
| |
| 207 | |
| 208 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 209 InitLibcFileIOFunctions)); | |
| 210 | |
| 211 #if HAVE_XSTAT | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
#if defined(HAVE_XSTAT)
| |
| 212 if (!g_libc_stat) { | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
So, if I'm not mistaken, since this stat is inline
Mostyn Bramley-Moore
2013/06/27 00:26:10
Correct- this whole function should never be calle
| |
| 213 return g_libc_xstat(XSTAT_VERSION, path, buf); | |
| 214 } | |
| 215 #endif // HAVE_XSTAT | |
| 216 | |
| 217 return g_libc_stat(path, buf); | |
| 218 } | |
| 219 | |
| 220 __attribute__ ((__visibility__("default"))) | |
| 221 int stat64_override(const char *path, | |
| 222 struct stat64 *buf) __asm__ ("stat64"); | |
| 223 | |
| 224 __attribute__ ((__visibility__("default"))) | |
| 225 int stat64_override(const char *path, struct stat64 *buf) { | |
| 226 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
| 227 int result = fstat64(base::GetUrandomFD(), buf); | |
| 228 return result; | |
| 229 } | |
| 230 | |
| 231 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
| 232 InitLibcFileIOFunctions)); | |
| 233 | |
| 234 #if HAVE_XSTAT | |
| 235 if (!g_libc_stat64) { | |
| 236 return g_libc_xstat64(XSTAT_VERSION, path, buf); | |
|
jln (very slow on Chromium)
2013/06/26 23:49:47
Same remark here of course.
| |
| 237 } | |
| 238 #endif // HAVE_XSTAT | |
| 239 | |
| 240 return g_libc_stat64(path, buf); | |
| 241 } | |
| 160 | 242 |
| 161 #endif // !defined(ADDRESS_SANITIZER) | 243 #endif // !defined(ADDRESS_SANITIZER) |
| 162 | 244 |
| 163 } // namespace content | 245 } // namespace content |
| OLD | NEW |