Chromium Code Reviews| 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..67a2f0550e34fe0decb093ed7081a8726f4f2ef7 100644 |
| --- a/sandbox/linux/services/libc_urandom_override.cc |
| +++ b/sandbox/linux/services/libc_urandom_override.cc |
| @@ -16,6 +16,10 @@ |
| // Note: this file is used by the zygote and nacl_helper. |
| +#if defined(LIBC_GLIBC) |
| +#define HAVE_XSTAT |
| +#endif |
| + |
| namespace sandbox { |
| static bool g_override_urandom = false; |
| @@ -34,15 +38,23 @@ void InitLibcUrandomOverrides() { |
| static const char kUrandomDevPath[] = "/dev/urandom"; |
| 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); |
| static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; |
| static FopenFunction g_libc_fopen; |
| static FopenFunction g_libc_fopen64; |
| +static StatFunction g_libc_stat; |
| +static Stat64Function g_libc_stat64; |
| + |
| +#if defined(HAVE_XSTAT) |
| +typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
| +typedef int (*Xstat64Function)(int version, const char *path, |
| + struct stat64 *buf); |
| + |
| static XstatFunction g_libc_xstat; |
| static Xstat64Function g_libc_xstat64; |
| +#endif // defined(HAVE_XSTAT) |
| static void InitLibcFileIOFunctions() { |
| g_libc_fopen = reinterpret_cast<FopenFunction>( |
| @@ -59,9 +71,24 @@ 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. |
| + // TODO(sergeyu, mostynb): Hopefully the stat/xstat overrides can work |
|
Sergey Ulanov
2013/06/14 20:13:47
nit: This TODO seems to be too vague to be actiona
Mostyn Bramley-Moore
2013/06/14 20:38:23
Done.
|
| + // on glibc, uClibc and others. It would be good to verify that we |
| + // don't need to override any other stat family functions in the case |
| + // where xstat is not available. |
| + |
| + g_libc_stat = reinterpret_cast<StatFunction>( |
| + dlsym(RTLD_NEXT, "stat")); |
| + g_libc_stat64 = reinterpret_cast<Stat64Function>( |
| + dlsym(RTLD_NEXT, "stat64")); |
| + |
| + if (!g_libc_stat) { |
| + LOG(FATAL) << "Failed to get stat() from libc."; |
| + } |
| + if (!g_libc_stat64) { |
| + LOG(WARNING) << "Failed to get stat64() from libc."; |
|
jln (very slow on Chromium)
2013/06/14 19:43:16
It looks like the rationale for doing a LOG(WARNIN
Mostyn Bramley-Moore
2013/06/14 20:38:23
I made this change in patchset 2, but now I think
|
| + } |
| + |
| +#if defined(HAVE_XSTAT) |
| g_libc_xstat = reinterpret_cast<XstatFunction>( |
| dlsym(RTLD_NEXT, "__xstat")); |
| g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
| @@ -73,6 +100,7 @@ static void InitLibcFileIOFunctions() { |
| if (!g_libc_xstat64) { |
| LOG(WARNING) << "Failed to get __xstat64() from libc."; |
| } |
| +#endif // defined(HAVE_XSTAT) |
| } |
| // fopen() and fopen64() are intercepted here so that NSS can open |
| @@ -120,8 +148,48 @@ 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 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; |
| + } else { |
| + CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| + InitLibcFileIOFunctions)); |
| + 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; |
| + } else { |
| + CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| + InitLibcFileIOFunctions)); |
| + CHECK(g_libc_stat64); |
| + return g_libc_stat64(path, buf); |
| + } |
| +} |
| + |
| +#if defined(HAVE_XSTAT) |
| +// glibc implements the stat() family by using __xstat() variants, |
| +// so this should be sufficient to catch all such calls. We leave |
| +// the stat overrides outside this ifdef in the hope of making this |
| +// work for other libc's. |
| + |
| __attribute__ ((__visibility__("default"))) |
| int xstat_override(int version, |
| const char *path, |
| @@ -156,7 +224,7 @@ int xstat64_override(int version, const char *path, struct stat64 *buf) { |
| return g_libc_xstat64(version, path, buf); |
| } |
| } |
| -#endif // defined(LIBC_GLIBC) |
| +#endif // defined(HAVE_XSTAT) |
| #endif // !defined(ADDRESS_SANITIZER) |