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 | |
42 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | |
43 static FopenFunction g_libc_fopen; | |
44 static FopenFunction g_libc_fopen64; | |
45 | |
46 #if defined(HAVE_XSTAT) | |
37 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); | 47 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
38 typedef int (*Xstat64Function)(int version, const char *path, | 48 typedef int (*Xstat64Function)(int version, const char *path, |
39 struct stat64 *buf); | 49 struct stat64 *buf); |
40 | 50 |
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; | 51 static XstatFunction g_libc_xstat; |
45 static Xstat64Function g_libc_xstat64; | 52 static Xstat64Function g_libc_xstat64; |
53 #else | |
54 typedef int (*StatFunction)(const char *path, struct stat *buf); | |
55 typedef int (*Stat64Function)(const char *path, struct stat64 *buf); | |
56 | |
57 static StatFunction g_libc_stat; | |
58 static Stat64Function g_libc_stat64; | |
59 #endif // defined(HAVE_XSTAT) | |
46 | 60 |
47 static void InitLibcFileIOFunctions() { | 61 static void InitLibcFileIOFunctions() { |
48 g_libc_fopen = reinterpret_cast<FopenFunction>( | 62 g_libc_fopen = reinterpret_cast<FopenFunction>( |
49 dlsym(RTLD_NEXT, "fopen")); | 63 dlsym(RTLD_NEXT, "fopen")); |
50 g_libc_fopen64 = reinterpret_cast<FopenFunction>( | 64 g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
51 dlsym(RTLD_NEXT, "fopen64")); | 65 dlsym(RTLD_NEXT, "fopen64")); |
52 | 66 |
53 if (!g_libc_fopen) { | 67 if (!g_libc_fopen) { |
54 LOG(FATAL) << "Failed to get fopen() from libc."; | 68 LOG(FATAL) << "Failed to get fopen() from libc."; |
55 } else if (!g_libc_fopen64) { | 69 } else if (!g_libc_fopen64) { |
56 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 70 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
57 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; | 71 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; |
58 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 72 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
59 g_libc_fopen64 = g_libc_fopen; | 73 g_libc_fopen64 = g_libc_fopen; |
60 } | 74 } |
61 | 75 |
62 #if defined(LIBC_GLIBC) | 76 // Hopefully the stat/xstat overrides can work on glibc, uClibc and |
63 // TODO(sergeyu): This works only on systems with glibc. Fix it to | 77 // others. It would be good to verify that we don't need to override |
64 // work properly on other systems if necessary. | 78 // any other stat family functions in the case where xstat is not |
79 // available. | |
80 | |
81 #if defined(HAVE_XSTAT) | |
65 g_libc_xstat = reinterpret_cast<XstatFunction>( | 82 g_libc_xstat = reinterpret_cast<XstatFunction>( |
66 dlsym(RTLD_NEXT, "__xstat")); | 83 dlsym(RTLD_NEXT, "__xstat")); |
67 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( | 84 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
68 dlsym(RTLD_NEXT, "__xstat64")); | 85 dlsym(RTLD_NEXT, "__xstat64")); |
69 | 86 |
70 if (!g_libc_xstat) { | 87 if (!g_libc_xstat) { |
71 LOG(FATAL) << "Failed to get __xstat() from libc."; | 88 LOG(FATAL) << "Failed to get __xstat() from libc."; |
72 } | 89 } |
73 if (!g_libc_xstat64) { | 90 if (!g_libc_xstat64) { |
74 LOG(WARNING) << "Failed to get __xstat64() from libc."; | 91 LOG(FATAL) << "Failed to get __xstat64() from libc."; |
75 } | 92 } |
93 #else | |
94 g_libc_stat = reinterpret_cast<StatFunction>( | |
95 dlsym(RTLD_NEXT, "stat")); | |
96 g_libc_stat64 = reinterpret_cast<Stat64Function>( | |
97 dlsym(RTLD_NEXT, "stat64")); | |
98 | |
99 if (!g_libc_stat) { | |
100 LOG(FATAL) << "Failed to get stat() from libc."; | |
101 } | |
102 if (!g_libc_stat64) { | |
103 LOG(FATAL) << "Failed to get stat64() from libc."; | |
104 } | |
105 #endif // defined(HAVE_XSTAT) | |
76 } | 106 } |
77 | 107 |
78 // fopen() and fopen64() are intercepted here so that NSS can open | 108 // fopen() and fopen64() are intercepted here so that NSS can open |
79 // /dev/urandom to seed its random number generator. NSS is used by | 109 // /dev/urandom to seed its random number generator. NSS is used by |
80 // remoting in the sendbox. | 110 // remoting in the sendbox. |
81 | 111 |
82 // fopen() call may be redirected to fopen64() in stdio.h using | 112 // fopen() call may be redirected to fopen64() in stdio.h using |
83 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This | 113 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This |
84 // means that we cannot override fopen() directly here. Instead the | 114 // means that we cannot override fopen() directly here. Instead the |
85 // the code below defines fopen_override() function with asm name | 115 // the code below defines fopen_override() function with asm name |
(...skipping 27 matching lines...) Expand all Loading... | |
113 return NULL; | 143 return NULL; |
114 } | 144 } |
115 return fdopen(fd, mode); | 145 return fdopen(fd, mode); |
116 } else { | 146 } else { |
117 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 147 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
118 InitLibcFileIOFunctions)); | 148 InitLibcFileIOFunctions)); |
119 return g_libc_fopen64(path, mode); | 149 return g_libc_fopen64(path, mode); |
120 } | 150 } |
121 } | 151 } |
122 | 152 |
123 // stat() is subject to the same problem as fopen(), so we have to use | 153 // The stat() family of functions are subject to the same problem as |
124 // the same trick to override it. | 154 // fopen(), so we have to use the same trick to override them. |
155 | |
156 #if defined(HAVE_XSTAT) | |
157 // glibc implements the stat() family by using __xstat() variants, | |
158 // so this should be sufficient to catch all such calls. | |
159 | |
125 __attribute__ ((__visibility__("default"))) | 160 __attribute__ ((__visibility__("default"))) |
126 int xstat_override(int version, | 161 int xstat_override(int version, |
127 const char *path, | 162 const char *path, |
128 struct stat *buf) __asm__ ("__xstat"); | 163 struct stat *buf) __asm__ ("__xstat"); |
129 | 164 |
130 __attribute__ ((__visibility__("default"))) | 165 __attribute__ ((__visibility__("default"))) |
131 int xstat_override(int version, const char *path, struct stat *buf) { | 166 int xstat_override(int version, const char *path, struct stat *buf) { |
132 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 167 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
133 int result = __fxstat(version, base::GetUrandomFD(), buf); | 168 int result = __fxstat(version, base::GetUrandomFD(), buf); |
134 return result; | 169 return result; |
(...skipping 14 matching lines...) Expand all Loading... | |
149 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 184 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
150 int result = __fxstat64(version, base::GetUrandomFD(), buf); | 185 int result = __fxstat64(version, base::GetUrandomFD(), buf); |
151 return result; | 186 return result; |
152 } else { | 187 } else { |
153 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 188 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
154 InitLibcFileIOFunctions)); | 189 InitLibcFileIOFunctions)); |
155 CHECK(g_libc_xstat64); | 190 CHECK(g_libc_xstat64); |
156 return g_libc_xstat64(version, path, buf); | 191 return g_libc_xstat64(version, path, buf); |
157 } | 192 } |
158 } | 193 } |
159 #endif // defined(LIBC_GLIBC) | 194 #else |
195 __attribute__ ((__visibility__("default"))) | |
196 int stat_override(const char *path, | |
197 struct stat *buf) __asm__ ("stat"); | |
jln (very slow on Chromium)
2013/06/24 21:53:01
Nit: one less space.
Mostyn Bramley-Moore
2013/06/24 22:14:22
Done.
| |
198 | |
199 __attribute__ ((__visibility__("default"))) | |
200 int stat_override(const char *path, struct stat *buf) { | |
201 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
202 int result = fstat(base::GetUrandomFD(), buf); | |
203 return result; | |
204 } else { | |
205 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
206 InitLibcFileIOFunctions)); | |
207 return g_libc_stat(path, buf); | |
208 } | |
209 } | |
210 | |
211 __attribute__ ((__visibility__("default"))) | |
212 int stat64_override(const char *path, | |
213 struct stat64 *buf) __asm__ ("stat64"); | |
jln (very slow on Chromium)
2013/06/24 21:53:01
Nit: one less space.
Mostyn Bramley-Moore
2013/06/24 22:14:22
Done.
| |
214 | |
215 __attribute__ ((__visibility__("default"))) | |
216 int stat64_override(const char *path, struct stat64 *buf) { | |
217 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | |
218 int result = fstat64(base::GetUrandomFD(), buf); | |
219 return result; | |
220 } else { | |
221 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | |
222 InitLibcFileIOFunctions)); | |
223 return g_libc_stat64(path, buf); | |
224 } | |
225 } | |
226 #endif // defined(HAVE_XSTAT) | |
160 | 227 |
161 #endif // !defined(ADDRESS_SANITIZER) | 228 #endif // !defined(ADDRESS_SANITIZER) |
162 | 229 |
163 } // namespace content | 230 } // namespace content |
OLD | NEW |