Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: chrome/nacl/nacl_helper_linux.cc

Issue 16881004: Move chrome/nacl to components/nacl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // A mini-zygote specifically for Native Client.
6
7 #include "chrome/common/nacl_helper_linux.h"
8
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <link.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/socket.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17
18 #include <string>
19 #include <vector>
20
21 #include "base/at_exit.h"
22 #include "base/command_line.h"
23 #include "base/json/string_escape.h"
24 #include "base/logging.h"
25 #include "base/message_loop.h"
26 #include "base/posix/eintr_wrapper.h"
27 #include "base/posix/global_descriptors.h"
28 #include "base/posix/unix_domain_socket_linux.h"
29 #include "base/rand_util.h"
30 #include "chrome/nacl/nacl_listener.h"
31 #include "crypto/nss_util.h"
32 #include "ipc/ipc_descriptors.h"
33 #include "ipc/ipc_switches.h"
34 #include "sandbox/linux/services/libc_urandom_override.h"
35
36 namespace {
37
38 // The child must mimic the behavior of zygote_main_linux.cc on the child
39 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from
40 // if (!child) {
41 // Note: this code doesn't attempt to support the SECCOMP sandbox.
42 void BecomeNaClLoader(const std::vector<int>& child_fds,
43 size_t prereserved_sandbox_size) {
44 VLOG(1) << "NaCl loader: setting up IPC descriptor";
45 // don't need zygote FD any more
46 if (HANDLE_EINTR(close(kNaClZygoteDescriptor)) != 0)
47 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed.";
48 base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel,
49 child_fds[kNaClBrowserFDIndex]);
50
51 base::MessageLoopForIO main_message_loop;
52 NaClListener listener;
53 listener.set_prereserved_sandbox_size(prereserved_sandbox_size);
54 listener.Listen();
55 _exit(0);
56 }
57
58 // Some of this code was lifted from
59 // content/browser/zygote_main_linux.cc:ForkWithRealPid()
60 void HandleForkRequest(const std::vector<int>& child_fds,
61 size_t prereserved_sandbox_size) {
62 VLOG(1) << "nacl_helper: forking";
63 pid_t childpid = fork();
64 if (childpid < 0) {
65 perror("fork");
66 LOG(ERROR) << "*** HandleForkRequest failed\n";
67 // fall through to parent case below
68 } else if (childpid == 0) { // In the child process.
69 bool validack = false;
70 const size_t kMaxReadSize = 1024;
71 char buffer[kMaxReadSize];
72 // Wait until the parent process has discovered our PID. We
73 // should not fork any child processes (which the seccomp
74 // sandbox does) until then, because that can interfere with the
75 // parent's discovery of our PID.
76 const int nread = HANDLE_EINTR(read(child_fds[kNaClParentFDIndex], buffer,
77 kMaxReadSize));
78 const std::string switch_prefix = std::string("--") +
79 switches::kProcessChannelID + std::string("=");
80 const size_t len = switch_prefix.length();
81
82 if (nread < 0) {
83 perror("read");
84 LOG(ERROR) << "read returned " << nread;
85 } else if (nread > static_cast<int>(len)) {
86 if (switch_prefix.compare(0, len, buffer, 0, len) == 0) {
87 VLOG(1) << "NaCl loader is synchronised with Chrome zygote";
88 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
89 switches::kProcessChannelID,
90 std::string(&buffer[len], nread - len));
91 validack = true;
92 }
93 }
94 if (HANDLE_EINTR(close(child_fds[kNaClDummyFDIndex])) != 0)
95 LOG(ERROR) << "close(child_fds[kNaClDummyFDIndex]) failed";
96 if (HANDLE_EINTR(close(child_fds[kNaClParentFDIndex])) != 0)
97 LOG(ERROR) << "close(child_fds[kNaClParentFDIndex]) failed";
98 if (validack) {
99 BecomeNaClLoader(child_fds, prereserved_sandbox_size);
100 } else {
101 LOG(ERROR) << "Failed to synch with zygote";
102 }
103 // NOTREACHED
104 return;
105 }
106 // I am the parent.
107 // First, close the dummy_fd so the sandbox won't find me when
108 // looking for the child's pid in /proc. Also close other fds.
109 for (size_t i = 0; i < child_fds.size(); i++) {
110 if (HANDLE_EINTR(close(child_fds[i])) != 0)
111 LOG(ERROR) << "close(child_fds[i]) failed";
112 }
113 VLOG(1) << "nacl_helper: childpid is " << childpid;
114 // Now tell childpid to the Chrome zygote.
115 if (HANDLE_EINTR(send(kNaClZygoteDescriptor,
116 &childpid, sizeof(childpid), MSG_EOR))
117 != sizeof(childpid)) {
118 LOG(ERROR) << "*** send() to zygote failed";
119 }
120 }
121
122 // This is a poor man's check on whether we are sandboxed.
123 bool IsSandboxed() {
124 int proc_fd = open("/proc/self/exe", O_RDONLY);
125 if (proc_fd >= 0) {
126 HANDLE_EINTR(close(proc_fd));
127 return false;
128 }
129 return true;
130 }
131
132 } // namespace
133
134 static const char kNaClHelperReservedAtZero[] = "reserved_at_zero";
135 static const char kNaClHelperRDebug[] = "r_debug";
136
137 // Since we were started by nacl_helper_bootstrap rather than in the
138 // usual way, the debugger cannot figure out where our executable
139 // or the dynamic linker or the shared libraries are in memory,
140 // so it won't find any symbols. But we can fake it out to find us.
141 //
142 // The zygote passes --r_debug=0xXXXXXXXXXXXXXXXX.
143 // nacl_helper_bootstrap replaces the Xs with the address of its _r_debug
144 // structure. The debugger will look for that symbol by name to
145 // discover the addresses of key dynamic linker data structures.
146 // Since all it knows about is the original main executable, which
147 // is the bootstrap program, it finds the symbol defined there. The
148 // dynamic linker's structure is somewhere else, but it is filled in
149 // after initialization. The parts that really matter to the
150 // debugger never change. So we just copy the contents of the
151 // dynamic linker's structure into the address provided by the option.
152 // Hereafter, if someone attaches a debugger (or examines a core dump),
153 // the debugger will find all the symbols in the normal way.
154 static void CheckRDebug(char* argv0) {
155 std::string r_debug_switch_value =
156 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kNaClHelperRDebug);
157 if (!r_debug_switch_value.empty()) {
158 char* endp;
159 uintptr_t r_debug_addr = strtoul(r_debug_switch_value.c_str(), &endp, 0);
160 if (r_debug_addr != 0 && *endp == '\0') {
161 r_debug* bootstrap_r_debug = reinterpret_cast<r_debug*>(r_debug_addr);
162 *bootstrap_r_debug = _r_debug;
163
164 // Since the main executable (the bootstrap program) does not
165 // have a dynamic section, the debugger will not skip the
166 // first element of the link_map list as it usually would for
167 // an executable or PIE that was loaded normally. But the
168 // dynamic linker has set l_name for the PIE to "" as is
169 // normal for the main executable. So the debugger doesn't
170 // know which file it is. Fill in the actual file name, which
171 // came in as our argv[0].
172 link_map* l = _r_debug.r_map;
173 if (l->l_name[0] == '\0')
174 l->l_name = argv0;
175 }
176 }
177 }
178
179 // The zygote passes --reserved_at_zero=0xXXXXXXXXXXXXXXXX.
180 // nacl_helper_bootstrap replaces the Xs with the amount of prereserved
181 // sandbox memory.
182 //
183 // CheckReservedAtZero parses the value of the argument reserved_at_zero
184 // and returns the amount of prereserved sandbox memory.
185 static size_t CheckReservedAtZero() {
186 size_t prereserved_sandbox_size = 0;
187 std::string reserved_at_zero_switch_value =
188 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
189 kNaClHelperReservedAtZero);
190 if (!reserved_at_zero_switch_value.empty()) {
191 char* endp;
192 prereserved_sandbox_size =
193 strtoul(reserved_at_zero_switch_value.c_str(), &endp, 0);
194 if (*endp != '\0')
195 LOG(ERROR) << "Could not parse reserved_at_zero argument value of "
196 << reserved_at_zero_switch_value;
197 }
198 return prereserved_sandbox_size;
199 }
200
201 #if defined(ADDRESS_SANITIZER)
202 // Do not install the SIGSEGV handler in ASan. This should make the NaCl
203 // platform qualification test pass.
204 static const char kAsanDefaultOptionsNaCl[] = "handle_segv=0";
205
206 // Override the default ASan options for the NaCl helper.
207 // __asan_default_options should not be instrumented, because it is called
208 // before ASan is initialized.
209 extern "C"
210 __attribute__((no_address_safety_analysis))
211 const char* __asan_default_options() {
212 return kAsanDefaultOptionsNaCl;
213 }
214 #endif
215
216 int main(int argc, char* argv[]) {
217 CommandLine::Init(argc, argv);
218 base::AtExitManager exit_manager;
219 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised
220 // Allows NSS to fopen() /dev/urandom.
221 sandbox::InitLibcUrandomOverrides();
222 #if defined(USE_NSS)
223 // Configure NSS for use inside the NaCl process.
224 // The fork check has not caused problems for NaCl, but this appears to be
225 // best practice (see other places LoadNSSLibraries is called.)
226 crypto::DisableNSSForkCheck();
227 // Without this line on Linux, HMAC::Init will instantiate a singleton that
228 // in turn attempts to open a file. Disabling this behavior avoids a ~70 ms
229 // stall the first time HMAC is used.
230 crypto::ForceNSSNoDBInit();
231 // Load shared libraries before sandbox is raised.
232 // NSS is needed to perform hashing for validation caching.
233 crypto::LoadNSSLibraries();
234 #endif
235 std::vector<int> empty; // for SendMsg() calls
236 size_t prereserved_sandbox_size = CheckReservedAtZero();
237
238 CheckRDebug(argv[0]);
239
240 // Check that IsSandboxed() works. We should not be sandboxed at this point.
241 CHECK(!IsSandboxed()) << "Unexpectedly sandboxed!";
242
243 // Send the zygote a message to let it know we are ready to help
244 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor,
245 kNaClHelperStartupAck,
246 sizeof(kNaClHelperStartupAck), empty)) {
247 LOG(ERROR) << "*** send() to zygote failed";
248 }
249
250 while (true) {
251 int badpid = -1;
252 std::vector<int> fds;
253 static const unsigned kMaxMessageLength = 2048;
254 char buf[kMaxMessageLength];
255 const ssize_t msglen = UnixDomainSocket::RecvMsg(kNaClZygoteDescriptor,
256 &buf, sizeof(buf), &fds);
257 // If the Zygote has started handling requests, we should be sandboxed via
258 // the setuid sandbox.
259 if (!IsSandboxed()) {
260 LOG(ERROR) << "NaCl helper process running without a sandbox!\n"
261 << "Most likely you need to configure your SUID sandbox "
262 << "correctly";
263 }
264 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) {
265 // EOF from the browser. Goodbye!
266 _exit(0);
267 } else if (msglen < 0) {
268 LOG(ERROR) << "nacl_helper: receive from zygote failed, errno = "
269 << errno;
270 } else if (msglen == sizeof(kNaClForkRequest) - 1 &&
271 memcmp(buf, kNaClForkRequest, msglen) == 0) {
272 if (kNaClParentFDIndex + 1 == fds.size()) {
273 HandleForkRequest(fds, prereserved_sandbox_size);
274 continue; // fork succeeded. Note: child does not return
275 } else {
276 LOG(ERROR) << "nacl_helper: unexpected number of fds, got "
277 << fds.size();
278 }
279 } else {
280 LOG(ERROR) << "nacl_helper unrecognized request: "
281 << base::GetDoubleQuotedJson(std::string(buf, buf + msglen));
282 _exit(-1);
283 }
284 // if fork fails, send PID=-1 to zygote
285 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid,
286 sizeof(badpid), empty)) {
287 LOG(ERROR) << "*** send() to zygote failed";
288 }
289 }
290 CHECK(false); // This routine must not return
291 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698