Index: sandbox/linux/services/libc_urandom_override.cc |
diff --git a/sandbox/linux/services/libc_urandom_override.cc b/sandbox/linux/services/libc_urandom_override.cc |
index 8af42ebbcf1f26e13a2e6a26bee2501da4ef00e7..7ebf34f7b37f1a48267be077367ac3e9f2e3124e 100644 |
--- a/sandbox/linux/services/libc_urandom_override.cc |
+++ b/sandbox/linux/services/libc_urandom_override.cc |
@@ -1,4 +1,4 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
@@ -16,16 +16,13 @@ |
// Note: this file is used by the zygote and nacl_helper. |
+// This is used when mapping stat* calls to __xstat* calls. |
+#define XSTAT_VERSION 3 |
+ |
namespace sandbox { |
static bool g_override_urandom = false; |
-void InitLibcUrandomOverrides() { |
- // Make sure /dev/urandom is open. |
- base::GetUrandomFD(); |
- g_override_urandom = true; |
-} |
- |
// 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 |
@@ -37,14 +34,33 @@ 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); |
+typedef int (*StatFunction)(const char *path, struct stat *buf); |
+typedef int (*Stat64Function)(const char *path, struct stat64 *buf); |
+typedef int (*FxstatFunction)(int version, int fd, struct stat *buf); |
+typedef int (*Fxstat64Function)(int version, int fd, struct stat64 *buf); |
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 FopenFunction g_libc_fopen = NULL; |
+static FopenFunction g_libc_fopen64 = NULL; |
+static XstatFunction g_libc_xstat = NULL; |
+static Xstat64Function g_libc_xstat64 = NULL; |
+static StatFunction g_libc_stat = NULL; |
+static Stat64Function g_libc_stat64 = NULL; |
+static FxstatFunction g_libc_fxstat = NULL; |
+static Fxstat64Function g_libc_fxstat64 = NULL; |
+ |
+void InitLibcUrandomOverrides() { |
+ // Make sure /dev/urandom is open. |
jln (very slow on Chromium)
2013/06/25 23:00:03
Calling InitLibcFileIOFunctions() here would guara
Mostyn Bramley-Moore
2013/06/26 10:30:18
Done.
|
+ base::GetUrandomFD(); |
+ g_override_urandom = true; |
+} |
static void InitLibcFileIOFunctions() { |
+ // find the libc's real fopen* and *stat* functions |
jln (very slow on Chromium)
2013/06/25 23:00:03
Nit: comment above the function, start with a capi
Mostyn Bramley-Moore
2013/06/26 00:09:57
Done.
|
+ |
+ // TODO(mostynb): call this once near the start of the init sequence, don't |
+ // call it repeatedly with pthread_once() - ZygoteMain is not early enough. |
jln (very slow on Chromium)
2013/06/25 23:00:03
In practice it'll only be called once, right?
If
Mostyn Bramley-Moore
2013/06/25 23:40:41
This function is only called once, what I was tryi
jln (very slow on Chromium)
2013/06/26 01:57:21
Yeah, I'm not worried about mutiple calls to pthre
Mostyn Bramley-Moore
2013/06/26 10:30:18
Done in patchset 11.
|
+ |
g_libc_fopen = reinterpret_cast<FopenFunction>( |
dlsym(RTLD_NEXT, "fopen")); |
g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
@@ -59,20 +75,34 @@ static void InitLibcFileIOFunctions() { |
g_libc_fopen64 = g_libc_fopen; |
} |
-#if defined(LIBC_GLIBC) |
- // TODO(sergeyu): This works only on systems with glibc. Fix it to |
- // work properly on other systems if necessary. |
+ // Note: we attempt to dlsym both stat and __xstat but only expect one |
+ // of the calls to succeed (and similarly for the *64 versions). This |
+ // should work with both glibc and uClibc, and hopefully other libc's. |
+ // Background: glibc headers inline stat into __xstat which it exports, |
+ // while uClibc exports stat and does not implement __xstat. |
+ |
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."; |
+ g_libc_stat = reinterpret_cast<StatFunction>( |
+ dlsym(RTLD_NEXT, "stat")); |
+ g_libc_stat64 = reinterpret_cast<Stat64Function>( |
+ dlsym(RTLD_NEXT, "stat64")); |
+ g_libc_fxstat = reinterpret_cast<FxstatFunction>( |
+ dlsym(RTLD_NEXT, "__fxstat")); |
+ g_libc_fxstat64 = reinterpret_cast<Fxstat64Function>( |
+ dlsym(RTLD_NEXT, "__fxstat64")); |
+ |
+ if (!g_libc_xstat && !g_libc_stat) { |
+ LOG(FATAL) << "Neither __xstat nor stat found."; |
} |
- if (!g_libc_xstat64) { |
- LOG(WARNING) << "Failed to get __xstat64() from libc."; |
+ if (!g_libc_xstat64 && !g_libc_stat64) { |
+ LOG(FATAL) << "Neither __xstat64 nor stat64 found."; |
} |
+ |
+ // Note: it's OK if __fxstat and __fxstat64 aren't found, |
+ // we can map them to fstat and fstat64. |
jln (very slow on Chromium)
2013/06/25 23:00:03
This makes everything way too complicated.
In wha
Mostyn Bramley-Moore
2013/06/25 23:40:41
In practice, probably none. As mentioned in my co
Mostyn Bramley-Moore
2013/06/26 10:30:18
Done (removed the fstat* dlsym's now).
|
} |
// fopen() and fopen64() are intercepted here so that NSS can open |
@@ -120,8 +150,9 @@ FILE* fopen64(const char* path, const char* mode) { |
} |
} |
-// stat() is subject to the same problem as fopen(), so we have to use |
-// the same trick to override it. |
+// The stat() family of functions are subject to the same problem as |
+// fopen(), so we have to use the same trick to override them. |
+ |
__attribute__ ((__visibility__("default"))) |
int xstat_override(int version, |
const char *path, |
@@ -129,12 +160,21 @@ int xstat_override(int version, |
__attribute__ ((__visibility__("default"))) |
int xstat_override(int version, const char *path, struct stat *buf) { |
+ CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
+ InitLibcFileIOFunctions)); |
+ |
if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
- int result = __fxstat(version, base::GetUrandomFD(), buf); |
- return result; |
+ if (g_libc_fxstat) { |
+ int result = g_libc_fxstat(version, base::GetUrandomFD(), buf); |
+ return result; |
+ } else { |
+ int result = fstat(base::GetUrandomFD(), buf); |
+ return result; |
jln (very slow on Chromium)
2013/06/25 23:00:03
See my above comment, the logic becomes way too co
Mostyn Bramley-Moore
2013/06/25 23:40:41
It should be safe to simply use fstat directly her
Mostyn Bramley-Moore
2013/06/26 10:30:18
Done (reverted).
|
+ } |
+ } |
+ else if (!g_libc_xstat) { |
+ return g_libc_stat(path, buf); |
} else { |
- CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
- InitLibcFileIOFunctions)); |
return g_libc_xstat(version, path, buf); |
} |
} |
@@ -146,17 +186,66 @@ int xstat64_override(int version, |
__attribute__ ((__visibility__("default"))) |
int xstat64_override(int version, const char *path, struct stat64 *buf) { |
+ CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
+ InitLibcFileIOFunctions)); |
+ |
if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
- int result = __fxstat64(version, base::GetUrandomFD(), buf); |
- return result; |
+ if (g_libc_fxstat64) { |
+ int result = g_libc_fxstat64(version, base::GetUrandomFD(), buf); |
+ return result; |
+ } else { |
+ int result = fstat64(base::GetUrandomFD(), buf); |
+ return result; |
+ } |
+ } |
+ else if (!g_libc_xstat64) { |
+ return g_libc_stat64(path, buf); |
} 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 // defined(LIBC_GLIBC) |
+ |
+__attribute__ ((__visibility__("default"))) |
+int stat_override(const char *path, |
+ struct stat *buf) __asm__ ("stat"); |
+ |
+__attribute__ ((__visibility__("default"))) |
+int stat_override(const char *path, struct stat *buf) { |
+ if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
+ int result = fstat(base::GetUrandomFD(), buf); |
+ return result; |
+ } |
+ |
+ CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
+ InitLibcFileIOFunctions)); |
+ |
+ if (!g_libc_stat) { |
+ return g_libc_xstat(XSTAT_VERSION, path, buf); |
+ } else { |
+ return g_libc_stat(path, buf); |
+ } |
+} |
+ |
+__attribute__ ((__visibility__("default"))) |
+int stat64_override(const char *path, |
+ struct stat64 *buf) __asm__ ("stat64"); |
+ |
+__attribute__ ((__visibility__("default"))) |
+int stat64_override(const char *path, struct stat64 *buf) { |
+ if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
+ int result = fstat64(base::GetUrandomFD(), buf); |
+ return result; |
+ } |
+ |
+ CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
+ InitLibcFileIOFunctions)); |
+ |
+ if (!g_libc_stat64) { |
+ return g_libc_xstat64(XSTAT_VERSION, path, buf); |
+ } else { |
+ return g_libc_stat64(path, buf); |
+ } |
+} |
#endif // !defined(ADDRESS_SANITIZER) |