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..860d7ec5df2f7ae229b5770cbed4e28b2c3ce254 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" |
@@ -632,11 +634,16 @@ 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); |
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_fopen_funcs_guard = PTHREAD_ONCE_INIT; |
+static FopenFunction g_libc_fopen; |
+static FopenFunction g_libc_fopen64; |
+ |
static void InitLibcLocaltimeFunctions() { |
g_libc_localtime = reinterpret_cast<LocaltimeFunction>( |
dlsym(RTLD_NEXT, "localtime")); |
@@ -686,6 +693,50 @@ struct tm* localtime_r(const time_t* timep, struct tm* result) { |
} |
} |
+static void InitLibcFopenFunctions() { |
+ g_libc_fopen = reinterpret_cast<FopenFunction>( |
+ dlsym(RTLD_NEXT, "fopen")); |
+ g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
+ dlsym(RTLD_NEXT, "fopen64")); |
+ |
+ if (!g_libc_fopen || !g_libc_fopen64) { |
+ LOG(ERROR) << "Failed to get fopen() from glibc."; |
+ } |
+} |
+ |
+__attribute__ ((__visibility__("default"))) |
+FILE* fopen(const char* path, const char* mode) __asm__ ("fopen"); |
+FILE* fopen(const char* path, const char* mode) { |
+ if (g_am_zygote_or_renderer && strcmp(path, "/dev/urandom") == 0) { |
Ben Chan
2012/04/10 02:17:15
static const char kDevUrandomFile[] = "/dev/urando
Sergey Ulanov
2012/04/10 03:02:53
How would that be better than strcmp()? It would a
Ben Chan
2012/04/10 03:24:41
strcmp should be fine. But I think it's still good
Sergey Ulanov
2012/04/10 06:03:38
Done.
|
+ int fd = dup(GetUrandomFD()); |
+ if (fd < 0) { |
+ LOG(ERROR) << "dup() failed."; |
+ return NULL; |
+ } |
+ return fdopen(fd, mode); |
+ } else { |
+ CHECK_EQ(0, pthread_once(&g_libc_fopen_funcs_guard, |
+ InitLibcFopenFunctions)); |
+ 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, "/dev/urandom") == 0) { |
+ int fd = dup(GetUrandomFD()); |
+ if (fd < 0) { |
+ PLOG(ERROR) << "dup() failed."; |
+ return NULL; |
+ } |
+ return fdopen(fd, mode); |
Ben Chan
2012/04/10 02:17:15
indentation
Sergey Ulanov
2012/04/10 03:02:53
Done.
|
+ } else { |
+ CHECK_EQ(0, pthread_once(&g_libc_fopen_funcs_guard, |
+ InitLibcFopenFunctions)); |
+ return g_libc_fopen64(path, mode); |
+ } |
+} |
+ |
#endif // !CHROMIUM_SELINUX |
// This function triggers the static and lazy construction of objects that need |