OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sandbox/linux/services/libc_urandom_override.h" | 5 #include "sandbox/linux/services/libc_urandom_override.h" |
6 | 6 |
7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
8 #include <pthread.h> | 8 #include <pthread.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
16 | 16 |
17 // Note: this file is used by the zygote and nacl_helper. | 17 // Note: this file is used by the zygote and nacl_helper. |
18 | 18 |
19 #if defined(LIBC_GLIBC) | |
20 #define HAVE_XSTAT | |
21 #endif | |
22 | |
19 namespace sandbox { | 23 namespace sandbox { |
20 | 24 |
21 static bool g_override_urandom = false; | 25 static bool g_override_urandom = false; |
22 | 26 |
23 void InitLibcUrandomOverrides() { | 27 void InitLibcUrandomOverrides() { |
24 // Make sure /dev/urandom is open. | 28 // Make sure /dev/urandom is open. |
25 base::GetUrandomFD(); | 29 base::GetUrandomFD(); |
26 g_override_urandom = true; | 30 g_override_urandom = true; |
27 } | 31 } |
28 | 32 |
29 // TODO(sergeyu): Currently this code doesn't work properly under ASAN | 33 // TODO(sergeyu): Currently this code doesn't work properly under ASAN |
30 // - it crashes content_unittests. Make sure it works properly and | 34 // - it crashes content_unittests. Make sure it works properly and |
31 // enable it here. http://crbug.com/123263 | 35 // enable it here. http://crbug.com/123263 |
32 #if !defined(ADDRESS_SANITIZER) | 36 #if !defined(ADDRESS_SANITIZER) |
33 | 37 |
34 static const char kUrandomDevPath[] = "/dev/urandom"; | 38 static const char kUrandomDevPath[] = "/dev/urandom"; |
35 | 39 |
36 typedef FILE* (*FopenFunction)(const char* path, const char* mode); | 40 typedef FILE* (*FopenFunction)(const char* path, const char* mode); |
41 typedef int (*StatFunction)(const char *path, struct stat *buf); | |
42 typedef int (*Stat64Function)(const char *path, struct stat64 *buf); | |
43 | |
44 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | |
45 static FopenFunction g_libc_fopen; | |
46 static FopenFunction g_libc_fopen64; | |
47 static StatFunction g_libc_stat; | |
48 static Stat64Function g_libc_stat64; | |
49 | |
50 #if defined(HAVE_XSTAT) | |
37 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); | 51 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
38 typedef int (*Xstat64Function)(int version, const char *path, | 52 typedef int (*Xstat64Function)(int version, const char *path, |
39 struct stat64 *buf); | 53 struct stat64 *buf); |
40 | 54 |
41 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | |
42 static FopenFunction g_libc_fopen; | |
43 static FopenFunction g_libc_fopen64; | |
44 static XstatFunction g_libc_xstat; | 55 static XstatFunction g_libc_xstat; |
45 static Xstat64Function g_libc_xstat64; | 56 static Xstat64Function g_libc_xstat64; |
57 #endif // defined(HAVE_XSTAT) | |
46 | 58 |
47 static void InitLibcFileIOFunctions() { | 59 static void InitLibcFileIOFunctions() { |
48 g_libc_fopen = reinterpret_cast<FopenFunction>( | 60 g_libc_fopen = reinterpret_cast<FopenFunction>( |
49 dlsym(RTLD_NEXT, "fopen")); | 61 dlsym(RTLD_NEXT, "fopen")); |
50 g_libc_fopen64 = reinterpret_cast<FopenFunction>( | 62 g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
51 dlsym(RTLD_NEXT, "fopen64")); | 63 dlsym(RTLD_NEXT, "fopen64")); |
52 | 64 |
53 if (!g_libc_fopen) { | 65 if (!g_libc_fopen) { |
54 LOG(FATAL) << "Failed to get fopen() from libc."; | 66 LOG(FATAL) << "Failed to get fopen() from libc."; |
55 } else if (!g_libc_fopen64) { | 67 } else if (!g_libc_fopen64) { |
56 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 68 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
57 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; | 69 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; |
58 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 70 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
59 g_libc_fopen64 = g_libc_fopen; | 71 g_libc_fopen64 = g_libc_fopen; |
60 } | 72 } |
61 | 73 |
62 #if defined(LIBC_GLIBC) | 74 // 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.
| |
63 // TODO(sergeyu): This works only on systems with glibc. Fix it to | 75 // on glibc, uClibc and others. It would be good to verify that we |
64 // work properly on other systems if necessary. | 76 // don't need to override any other stat family functions in the case |
77 // where xstat is not available. | |
78 | |
79 g_libc_stat = reinterpret_cast<StatFunction>( | |
80 dlsym(RTLD_NEXT, "stat")); | |
81 g_libc_stat64 = reinterpret_cast<Stat64Function>( | |
82 dlsym(RTLD_NEXT, "stat64")); | |
83 | |
84 if (!g_libc_stat) { | |
85 LOG(FATAL) << "Failed to get stat() from libc."; | |
86 } | |
87 if (!g_libc_stat64) { | |
88 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
| |
89 } | |
90 | |
91 #if defined(HAVE_XSTAT) | |
65 g_libc_xstat = reinterpret_cast<XstatFunction>( | 92 g_libc_xstat = reinterpret_cast<XstatFunction>( |
66 dlsym(RTLD_NEXT, "__xstat")); | 93 dlsym(RTLD_NEXT, "__xstat")); |
67 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( | 94 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
68 dlsym(RTLD_NEXT, "__xstat64")); | 95 dlsym(RTLD_NEXT, "__xstat64")); |
69 | 96 |
70 if (!g_libc_xstat) { | 97 if (!g_libc_xstat) { |
71 LOG(FATAL) << "Failed to get __xstat() from libc."; | 98 LOG(FATAL) << "Failed to get __xstat() from libc."; |
72 } | 99 } |
73 if (!g_libc_xstat64) { | 100 if (!g_libc_xstat64) { |
74 LOG(WARNING) << "Failed to get __xstat64() from libc."; | 101 LOG(WARNING) << "Failed to get __xstat64() from libc."; |
75 } | 102 } |
103 #endif // defined(HAVE_XSTAT) | |
76 } | 104 } |
77 | 105 |
78 // fopen() and fopen64() are intercepted here so that NSS can open | 106 // fopen() and fopen64() are intercepted here so that NSS can open |
79 // /dev/urandom to seed its random number generator. NSS is used by | 107 // /dev/urandom to seed its random number generator. NSS is used by |
80 // remoting in the sendbox. | 108 // remoting in the sendbox. |
81 | 109 |
82 // fopen() call may be redirected to fopen64() in stdio.h using | 110 // fopen() call may be redirected to fopen64() in stdio.h using |
83 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This | 111 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This |
84 // means that we cannot override fopen() directly here. Instead the | 112 // means that we cannot override fopen() directly here. Instead the |
85 // the code below defines fopen_override() function with asm name | 113 // the code below defines fopen_override() function with asm name |
(...skipping 27 matching lines...) Expand all Loading... | |
113 return NULL; | 141 return NULL; |
114 } | 142 } |
115 return fdopen(fd, mode); | 143 return fdopen(fd, mode); |
116 } else { | 144 } else { |
117 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 145 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
118 InitLibcFileIOFunctions)); | 146 InitLibcFileIOFunctions)); |
119 return g_libc_fopen64(path, mode); | 147 return g_libc_fopen64(path, mode); |
120 } | 148 } |
121 } | 149 } |
122 | 150 |
123 // stat() is subject to the same problem as fopen(), so we have to use | 151 // The stat() family of functions are subject to the same problem as |
124 // the same trick to override it. | 152 // fopen(), so we have to use the same trick to override them. |
153 | |
154 __attribute__ ((__visibility__("default"))) | |
155 int stat_override(const char *path, | |
156 struct stat *buf) __asm__ ("stat"); | |
157 | |
158 __attribute__ ((__visibility__("default"))) | |
159 int stat_override(const char *path, struct stat *buf) { | |
160 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
161 int result = fstat(base::GetUrandomFD(), buf); | |
162 return result; | |
163 } else { | |
164 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
165 InitLibcFileIOFunctions)); | |
166 return g_libc_stat(path, buf); | |
167 } | |
168 } | |
169 | |
170 __attribute__ ((__visibility__("default"))) | |
171 int stat64_override(const char *path, | |
172 struct stat64 *buf) __asm__ ("stat64"); | |
173 | |
174 __attribute__ ((__visibility__("default"))) | |
175 int stat64_override(const char *path, struct stat64 *buf) { | |
176 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
177 int result = fstat64(base::GetUrandomFD(), buf); | |
178 return result; | |
179 } else { | |
180 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
181 InitLibcFileIOFunctions)); | |
182 CHECK(g_libc_stat64); | |
183 return g_libc_stat64(path, buf); | |
184 } | |
185 } | |
186 | |
187 #if defined(HAVE_XSTAT) | |
188 // glibc implements the stat() family by using __xstat() variants, | |
189 // so this should be sufficient to catch all such calls. We leave | |
190 // the stat overrides outside this ifdef in the hope of making this | |
191 // work for other libc's. | |
192 | |
125 __attribute__ ((__visibility__("default"))) | 193 __attribute__ ((__visibility__("default"))) |
126 int xstat_override(int version, | 194 int xstat_override(int version, |
127 const char *path, | 195 const char *path, |
128 struct stat *buf) __asm__ ("__xstat"); | 196 struct stat *buf) __asm__ ("__xstat"); |
129 | 197 |
130 __attribute__ ((__visibility__("default"))) | 198 __attribute__ ((__visibility__("default"))) |
131 int xstat_override(int version, const char *path, struct stat *buf) { | 199 int xstat_override(int version, const char *path, struct stat *buf) { |
132 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 200 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
133 int result = __fxstat(version, base::GetUrandomFD(), buf); | 201 int result = __fxstat(version, base::GetUrandomFD(), buf); |
134 return result; | 202 return result; |
(...skipping 14 matching lines...) Expand all Loading... | |
149 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 217 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
150 int result = __fxstat64(version, base::GetUrandomFD(), buf); | 218 int result = __fxstat64(version, base::GetUrandomFD(), buf); |
151 return result; | 219 return result; |
152 } else { | 220 } else { |
153 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 221 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
154 InitLibcFileIOFunctions)); | 222 InitLibcFileIOFunctions)); |
155 CHECK(g_libc_xstat64); | 223 CHECK(g_libc_xstat64); |
156 return g_libc_xstat64(version, path, buf); | 224 return g_libc_xstat64(version, path, buf); |
157 } | 225 } |
158 } | 226 } |
159 #endif // defined(LIBC_GLIBC) | 227 #endif // defined(HAVE_XSTAT) |
160 | 228 |
161 #endif // !defined(ADDRESS_SANITIZER) | 229 #endif // !defined(ADDRESS_SANITIZER) |
162 | 230 |
163 } // namespace content | 231 } // namespace content |
OLD | NEW |