Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(783)

Unified Diff: content/browser/zygote_main_linux.cc

Issue 10031027: Redirect fopen("/dev/urandom") so that NSS can properly seed it's RNG. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698