Chromium Code Reviews| Index: content/browser/zygote_main_linux.cc |
| diff --git a/content/browser/zygote_main_linux.cc b/content/browser/zygote_main_linux.cc |
| index 020d7771ca6ed9cfe8351dcba1098d10b8a47f84..2c5e5fec57396921fc0ed45132580996cbd7df62 100644 |
| --- a/content/browser/zygote_main_linux.cc |
| +++ b/content/browser/zygote_main_linux.cc |
| @@ -7,6 +7,7 @@ |
| #include <dlfcn.h> |
| #include <fcntl.h> |
| #include <pthread.h> |
| +#include <stdio.h> |
| #include <sys/socket.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| @@ -25,6 +26,7 @@ |
| #include "base/pickle.h" |
| #include "base/process_util.h" |
| #include "base/rand_util.h" |
| +#include "base/rand_util_c.h" |
| #include "base/sys_info.h" |
| #include "build/build_config.h" |
| #include "crypto/nss_util.h" |
| @@ -64,6 +66,8 @@ static const int kMagicSandboxIPCDescriptor = 5; |
| static const int kZygoteIdDescriptor = 7; |
| static bool g_suid_sandbox_active = false; |
| +static const char kUrandomDevPath[] = "/dev/urandom"; |
| + |
| #if defined(SECCOMP_SANDBOX) |
| static int g_proc_fd = -1; |
| #endif |
| @@ -632,16 +636,26 @@ static bool g_am_zygote_or_renderer = false; |
| typedef struct tm* (*LocaltimeFunction)(const time_t* timep); |
| typedef struct tm* (*LocaltimeRFunction)(const time_t* timep, |
| struct tm* result); |
| +typedef FILE* (*FopenFunction)(const char* path, const char* mode); |
| +typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
| +typedef int (*Xstat64Function)(int version, const char *path, |
| + struct stat64 *buf); |
| static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT; |
| static LocaltimeFunction g_libc_localtime; |
| static LocaltimeRFunction g_libc_localtime_r; |
| +static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; |
| +static FopenFunction g_libc_fopen; |
| +static FopenFunction g_libc_fopen64; |
| +static XstatFunction g_libc_xstat; |
| +static Xstat64Function g_libc_xstat64; |
| + |
| static void InitLibcLocaltimeFunctions() { |
| g_libc_localtime = reinterpret_cast<LocaltimeFunction>( |
| - dlsym(RTLD_NEXT, "localtime")); |
| + dlsym(RTLD_NEXT, "localtime")); |
| g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>( |
| - dlsym(RTLD_NEXT, "localtime_r")); |
| + dlsym(RTLD_NEXT, "localtime_r")); |
| if (!g_libc_localtime || !g_libc_localtime_r) { |
| // http://code.google.com/p/chromium/issues/detail?id=16800 |
| @@ -686,6 +700,125 @@ struct tm* localtime_r(const time_t* timep, struct tm* result) { |
| } |
| } |
| +// TODO(sergeyu): Currently this code doesn't work properly under ASAN |
| +// - it crashes content_unittests. Make sure it works properly and |
| +// enable it here. http://crbug.com/123263 |
| +#if !defined(ADDRESS_SANITIZER) |
| + |
| +static void InitLibcFileIOFunctions() { |
| + g_libc_fopen = reinterpret_cast<FopenFunction>( |
| + dlsym(RTLD_NEXT, "fopen")); |
| + g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
| + dlsym(RTLD_NEXT, "fopen64")); |
| + |
| + if (!g_libc_fopen) { |
| + LOG(FATAL) << "Failed to get fopen() from libc."; |
| + } else if (!g_libc_fopen64) { |
| +#if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| + LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; |
| +#endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
| + g_libc_fopen64 = g_libc_fopen; |
| + } |
| + |
| + // TODO(sergeyu): This works only on systems with glibc. Fix it to |
| + // work properly on other systems if necessary. |
| + g_libc_xstat = reinterpret_cast<XstatFunction>( |
| + dlsym(RTLD_NEXT, "__xstat")); |
| + g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
| + dlsym(RTLD_NEXT, "__xstat64")); |
| + |
| + if (!g_libc_xstat) { |
| + LOG(FATAL) << "Failed to get __xstat() from libc."; |
| + } |
| + if (!g_libc_xstat64) { |
| + LOG(WARNING) << "Failed to get __xstat64() from libc."; |
| + } |
| +} |
| + |
| +// fopen() and fopen64() are intercepted here so that NSS can open |
| +// /dev/urandom to seed its random number generator. NSS is used by |
| +// remoting in the sendbox. |
|
wtc
2012/04/17 22:02:47
Typo: sendbox => sandbox
|
| + |
| +// fopen() call may be redirected to fopen64() in stdio.h using |
| +// __REDIRECT(), which sets asm name for fopen() to "fopen64". This |
| +// means that we cannot override fopen() directly here. Instead the |
| +// the code below defines fopen_override() function with asm name |
|
wtc
2012/04/17 22:02:47
Typo: remove "the"
I suggest you just move "the"
|
| +// "fopen", so that all references to fopen() will resolve to this |
| +// function. |
| +__attribute__ ((__visibility__("default"))) |
| +FILE* fopen_override(const char* path, const char* mode) __asm__ ("fopen"); |
| + |
| +__attribute__ ((__visibility__("default"))) |
| +FILE* fopen_override(const char* path, const char* mode) { |
| + if (g_am_zygote_or_renderer && strcmp(path, kUrandomDevPath) == 0) { |
| + int fd = HANDLE_EINTR(dup(GetUrandomFD())); |
| + if (fd < 0) { |
| + PLOG(ERROR) << "dup() failed."; |
| + return NULL; |
| + } |
| + return fdopen(fd, mode); |
| + } else { |
| + CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| + InitLibcFileIOFunctions)); |
| + return g_libc_fopen(path, mode); |
| + } |
| +} |
| + |
| +__attribute__ ((__visibility__("default"))) |
| +FILE* fopen64(const char* path, const char* mode) { |
| + if (g_am_zygote_or_renderer && strcmp(path, kUrandomDevPath) == 0) { |
| + int fd = HANDLE_EINTR(dup(GetUrandomFD())); |
| + if (fd < 0) { |
| + PLOG(ERROR) << "dup() failed."; |
| + return NULL; |
| + } |
| + return fdopen(fd, mode); |
| + } else { |
| + CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| + InitLibcFileIOFunctions)); |
| + return g_libc_fopen64(path, mode); |
| + } |
| +} |
| + |
| +// stat() is subject to the same problem as fopen(), so we have to use |
| +// the same trick to override it. |
|
wtc
2012/04/17 22:02:47
You should explain why you need to intercept stat(
|
| +__attribute__ ((__visibility__("default"))) |
| +int xstat_override(int version, |
| + const char *path, |
| + struct stat *buf) __asm__ ("__xstat"); |
| + |
| +__attribute__ ((__visibility__("default"))) |
| +int xstat_override(int version, const char *path, struct stat *buf) { |
| + if (g_am_zygote_or_renderer && strcmp(path, kUrandomDevPath) == 0) { |
| + int result = __fxstat(version, GetUrandomFD(), buf); |
| + return result; |
| + } else { |
| + CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| + InitLibcFileIOFunctions)); |
| + return g_libc_xstat(version, path, buf); |
| + } |
| +} |
| + |
| +__attribute__ ((__visibility__("default"))) |
| +int xstat64_override(int version, |
| + const char *path, |
| + struct stat64 *buf) __asm__ ("__xstat64"); |
| + |
| +__attribute__ ((__visibility__("default"))) |
| +int xstat64_override(int version, const char *path, struct stat64 *buf) { |
| + if (g_am_zygote_or_renderer && strcmp(path, kUrandomDevPath) == 0) { |
| + int result = __fxstat64(version, GetUrandomFD(), buf); |
| + return result; |
| + } else { |
| + CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| + InitLibcFileIOFunctions)); |
| + CHECK(g_libc_xstat64); |
| + return g_libc_xstat64(version, path, buf); |
| + } |
| +} |
| + |
| +#endif // !ADDRESS_SANITIZER |
| + |
| #endif // !CHROMIUM_SELINUX |
| // This function triggers the static and lazy construction of objects that need |