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(HAVE_XSTAT) && defined(LIBC_GLIBC) |
| 20 #define HAVE_XSTAT 1 |
| 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 |
| 27 // TODO(sergeyu): Currently InitLibcUrandomOverrides() doesn't work properly |
| 28 // under ASAN - it crashes content_unittests. Make sure it works properly and |
| 29 // enable it here. http://crbug.com/123263 |
| 30 #if !defined(ADDRESS_SANITIZER) |
| 31 static void InitLibcFileIOFunctions(); |
| 32 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; |
| 33 #endif |
| 34 |
23 void InitLibcUrandomOverrides() { | 35 void InitLibcUrandomOverrides() { |
24 // Make sure /dev/urandom is open. | 36 // Make sure /dev/urandom is open. |
25 base::GetUrandomFD(); | 37 base::GetUrandomFD(); |
26 g_override_urandom = true; | 38 g_override_urandom = true; |
| 39 |
| 40 #if !defined(ADDRESS_SANITIZER) |
| 41 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 42 InitLibcFileIOFunctions)); |
| 43 #endif |
27 } | 44 } |
28 | 45 |
29 // TODO(sergeyu): Currently this code doesn't work properly under ASAN | |
30 // - it crashes content_unittests. Make sure it works properly and | |
31 // enable it here. http://crbug.com/123263 | |
32 #if !defined(ADDRESS_SANITIZER) | 46 #if !defined(ADDRESS_SANITIZER) |
33 | 47 |
34 static const char kUrandomDevPath[] = "/dev/urandom"; | 48 static const char kUrandomDevPath[] = "/dev/urandom"; |
35 | 49 |
36 typedef FILE* (*FopenFunction)(const char* path, const char* mode); | 50 typedef FILE* (*FopenFunction)(const char* path, const char* mode); |
| 51 |
| 52 static FopenFunction g_libc_fopen = NULL; |
| 53 static FopenFunction g_libc_fopen64 = NULL; |
| 54 |
| 55 #if HAVE_XSTAT |
37 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); | 56 typedef int (*XstatFunction)(int version, const char *path, struct stat *buf); |
38 typedef int (*Xstat64Function)(int version, const char *path, | 57 typedef int (*Xstat64Function)(int version, const char *path, |
39 struct stat64 *buf); | 58 struct stat64 *buf); |
40 | 59 |
41 static pthread_once_t g_libc_file_io_funcs_guard = PTHREAD_ONCE_INIT; | 60 static XstatFunction g_libc_xstat = NULL; |
42 static FopenFunction g_libc_fopen; | 61 static Xstat64Function g_libc_xstat64 = NULL; |
43 static FopenFunction g_libc_fopen64; | 62 #else |
44 static XstatFunction g_libc_xstat; | 63 typedef int (*StatFunction)(const char *path, struct stat *buf); |
45 static Xstat64Function g_libc_xstat64; | 64 typedef int (*Stat64Function)(const char *path, struct stat64 *buf); |
46 | 65 |
| 66 static StatFunction g_libc_stat = NULL; |
| 67 static Stat64Function g_libc_stat64 = NULL; |
| 68 #endif // HAVE_XSTAT |
| 69 |
| 70 // Find the libc's real fopen* and *stat* functions. This should only be |
| 71 // called once, and should be guarded by g_libc_file_io_funcs_guard. |
47 static void InitLibcFileIOFunctions() { | 72 static void InitLibcFileIOFunctions() { |
48 g_libc_fopen = reinterpret_cast<FopenFunction>( | 73 g_libc_fopen = reinterpret_cast<FopenFunction>( |
49 dlsym(RTLD_NEXT, "fopen")); | 74 dlsym(RTLD_NEXT, "fopen")); |
50 g_libc_fopen64 = reinterpret_cast<FopenFunction>( | 75 g_libc_fopen64 = reinterpret_cast<FopenFunction>( |
51 dlsym(RTLD_NEXT, "fopen64")); | 76 dlsym(RTLD_NEXT, "fopen64")); |
52 | 77 |
53 if (!g_libc_fopen) { | 78 if (!g_libc_fopen) { |
54 LOG(FATAL) << "Failed to get fopen() from libc."; | 79 LOG(FATAL) << "Failed to get fopen() from libc."; |
55 } else if (!g_libc_fopen64) { | 80 } else if (!g_libc_fopen64) { |
56 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 81 #if !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
57 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; | 82 LOG(WARNING) << "Failed to get fopen64() from libc. Using fopen() instead."; |
58 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) | 83 #endif // !defined(OS_OPENBSD) && !defined(OS_FREEBSD) |
59 g_libc_fopen64 = g_libc_fopen; | 84 g_libc_fopen64 = g_libc_fopen; |
60 } | 85 } |
61 | 86 |
62 #if defined(LIBC_GLIBC) | 87 #if HAVE_XSTAT |
63 // TODO(sergeyu): This works only on systems with glibc. Fix it to | |
64 // work properly on other systems if necessary. | |
65 g_libc_xstat = reinterpret_cast<XstatFunction>( | 88 g_libc_xstat = reinterpret_cast<XstatFunction>( |
66 dlsym(RTLD_NEXT, "__xstat")); | 89 dlsym(RTLD_NEXT, "__xstat")); |
67 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( | 90 g_libc_xstat64 = reinterpret_cast<Xstat64Function>( |
68 dlsym(RTLD_NEXT, "__xstat64")); | 91 dlsym(RTLD_NEXT, "__xstat64")); |
69 | 92 |
70 if (!g_libc_xstat) { | 93 if (!g_libc_xstat) { |
71 LOG(FATAL) << "Failed to get __xstat() from libc."; | 94 LOG(FATAL) << "Failed to get __xstat() from libc."; |
72 } | 95 } |
73 if (!g_libc_xstat64) { | 96 if (!g_libc_xstat64) { |
74 LOG(WARNING) << "Failed to get __xstat64() from libc."; | 97 LOG(FATAL) << "Failed to get __xstat64() from libc."; |
75 } | 98 } |
| 99 #else |
| 100 g_libc_stat = reinterpret_cast<StatFunction>( |
| 101 dlsym(RTLD_NEXT, "stat")); |
| 102 g_libc_stat64 = reinterpret_cast<Stat64Function>( |
| 103 dlsym(RTLD_NEXT, "stat64")); |
| 104 |
| 105 if (!g_libc_stat) { |
| 106 LOG(FATAL) << "Failed to get stat() from libc."; |
| 107 } |
| 108 if (!g_libc_stat64) { |
| 109 LOG(FATAL) << "Failed to get stat64() from libc."; |
| 110 } |
| 111 #endif // HAVE_XSTAT |
76 } | 112 } |
77 | 113 |
78 // fopen() and fopen64() are intercepted here so that NSS can open | 114 // fopen() and fopen64() are intercepted here so that NSS can open |
79 // /dev/urandom to seed its random number generator. NSS is used by | 115 // /dev/urandom to seed its random number generator. NSS is used by |
80 // remoting in the sendbox. | 116 // remoting in the sendbox. |
81 | 117 |
82 // fopen() call may be redirected to fopen64() in stdio.h using | 118 // fopen() call may be redirected to fopen64() in stdio.h using |
83 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This | 119 // __REDIRECT(), which sets asm name for fopen() to "fopen64". This |
84 // means that we cannot override fopen() directly here. Instead the | 120 // means that we cannot override fopen() directly here. Instead the |
85 // the code below defines fopen_override() function with asm name | 121 // the code below defines fopen_override() function with asm name |
(...skipping 27 matching lines...) Expand all Loading... |
113 return NULL; | 149 return NULL; |
114 } | 150 } |
115 return fdopen(fd, mode); | 151 return fdopen(fd, mode); |
116 } else { | 152 } else { |
117 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 153 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
118 InitLibcFileIOFunctions)); | 154 InitLibcFileIOFunctions)); |
119 return g_libc_fopen64(path, mode); | 155 return g_libc_fopen64(path, mode); |
120 } | 156 } |
121 } | 157 } |
122 | 158 |
123 // stat() is subject to the same problem as fopen(), so we have to use | 159 // The stat() family of functions are subject to the same problem as |
124 // the same trick to override it. | 160 // fopen(), so we have to use the same trick to override them. |
| 161 |
| 162 #if HAVE_XSTAT |
| 163 |
125 __attribute__ ((__visibility__("default"))) | 164 __attribute__ ((__visibility__("default"))) |
126 int xstat_override(int version, | 165 int xstat_override(int version, |
127 const char *path, | 166 const char *path, |
128 struct stat *buf) __asm__ ("__xstat"); | 167 struct stat *buf) __asm__ ("__xstat"); |
129 | 168 |
130 __attribute__ ((__visibility__("default"))) | 169 __attribute__ ((__visibility__("default"))) |
131 int xstat_override(int version, const char *path, struct stat *buf) { | 170 int xstat_override(int version, const char *path, struct stat *buf) { |
132 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 171 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
133 int result = __fxstat(version, base::GetUrandomFD(), buf); | 172 int result = __fxstat(version, base::GetUrandomFD(), buf); |
134 return result; | 173 return result; |
(...skipping 10 matching lines...) Expand all Loading... |
145 struct stat64 *buf) __asm__ ("__xstat64"); | 184 struct stat64 *buf) __asm__ ("__xstat64"); |
146 | 185 |
147 __attribute__ ((__visibility__("default"))) | 186 __attribute__ ((__visibility__("default"))) |
148 int xstat64_override(int version, const char *path, struct stat64 *buf) { | 187 int xstat64_override(int version, const char *path, struct stat64 *buf) { |
149 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { | 188 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
150 int result = __fxstat64(version, base::GetUrandomFD(), buf); | 189 int result = __fxstat64(version, base::GetUrandomFD(), buf); |
151 return result; | 190 return result; |
152 } else { | 191 } else { |
153 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, | 192 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
154 InitLibcFileIOFunctions)); | 193 InitLibcFileIOFunctions)); |
155 CHECK(g_libc_xstat64); | |
156 return g_libc_xstat64(version, path, buf); | 194 return g_libc_xstat64(version, path, buf); |
157 } | 195 } |
158 } | 196 } |
159 #endif // defined(LIBC_GLIBC) | 197 |
| 198 #else |
| 199 |
| 200 __attribute__ ((__visibility__("default"))) |
| 201 int stat_override(const char *path, |
| 202 struct stat *buf) __asm__ ("stat"); |
| 203 |
| 204 __attribute__ ((__visibility__("default"))) |
| 205 int stat_override(const char *path, struct stat *buf) { |
| 206 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 207 int result = fstat(base::GetUrandomFD(), buf); |
| 208 return result; |
| 209 } else { |
| 210 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 211 InitLibcFileIOFunctions)); |
| 212 return g_libc_stat(path, buf); |
| 213 } |
| 214 } |
| 215 |
| 216 __attribute__ ((__visibility__("default"))) |
| 217 int stat64_override(const char *path, |
| 218 struct stat64 *buf) __asm__ ("stat64"); |
| 219 |
| 220 __attribute__ ((__visibility__("default"))) |
| 221 int stat64_override(const char *path, struct stat64 *buf) { |
| 222 if (g_override_urandom && strcmp(path, kUrandomDevPath) == 0) { |
| 223 int result = fstat64(base::GetUrandomFD(), buf); |
| 224 return result; |
| 225 } else { |
| 226 CHECK_EQ(0, pthread_once(&g_libc_file_io_funcs_guard, |
| 227 InitLibcFileIOFunctions)); |
| 228 return g_libc_stat64(path, buf); |
| 229 } |
| 230 } |
| 231 |
| 232 #endif // HAVE_XSTAT |
160 | 233 |
161 #endif // !defined(ADDRESS_SANITIZER) | 234 #endif // !defined(ADDRESS_SANITIZER) |
162 | 235 |
163 } // namespace content | 236 } // namespace content |
OLD | NEW |