1 // Copyright 2013 The Chromium Authors
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 "components/nacl/loader/nacl_helper_linux.h"
8
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <link.h>
12 #include <signal.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/socket.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "base/at_exit.h"
26 #include "base/base_switches.h"
27 #include "base/command_line.h"
28 #include "base/environment.h"
29 #include "base/files/scoped_file.h"
30 #include "base/json/json_reader.h"
31 #include "base/logging.h"
32 #include "base/message_loop/message_pump_type.h"
33 #include "base/metrics/field_trial.h"
34 #include "base/metrics/histogram_shared_memory.h"
35 #include "base/numerics/safe_conversions.h"
36 #include "base/pickle.h"
37 #include "base/posix/eintr_wrapper.h"
38 #include "base/posix/global_descriptors.h"
39 #include "base/posix/unix_domain_socket.h"
40 #include "base/process/kill.h"
41 #include "base/process/process_handle.h"
42 #include "base/rand_util.h"
43 #include "base/task/single_thread_task_executor.h"
44 #include "build/build_config.h"
45 #include "components/nacl/common/nacl_switches.h"
46 #include "components/nacl/loader/nacl_listener.h"
47 #include "components/nacl/loader/sandbox_linux/nacl_sandbox_linux.h"
48 #include "content/public/common/content_descriptors.h"
49 #include "content/public/common/zygote/send_zygote_child_ping_linux.h"
50 #include "content/public/common/zygote/zygote_fork_delegate_linux.h"
51 #include "mojo/core/embedder/embedder.h"
52 #include "sandbox/linux/services/credentials.h"
53 #include "sandbox/linux/services/namespace_sandbox.h"
54 #include "third_party/cros_system_api/switches/chrome_switches.h"
55
56 namespace {
57
58 struct NaClLoaderSystemInfo {
59 size_t prereserved_sandbox_size;
60 long number_of_cores;
61 };
62
63 #if BUILDFLAG(IS_CHROMEOS)
GetCommandLineFeatureFlagChoice(const base::CommandLine * command_line,std::string feature_flag)64 std::string GetCommandLineFeatureFlagChoice(
65 const base::CommandLine* command_line,
66 std::string feature_flag) {
67 std::string encoded =
68 command_line->GetSwitchValueNative(chromeos::switches::kFeatureFlags);
69 if (encoded.empty()) {
70 return "";
71 }
72
73 auto flags_list = base::JSONReader::Read(encoded);
74 if (!flags_list) {
75 LOG(WARNING) << "Failed to parse feature flags configuration";
76 return "";
77 }
78
79 for (const auto& flag : flags_list.value().GetList()) {
80 if (!flag.is_string())
81 continue;
82 std::string flag_string = flag.GetString();
83 if (flag_string.rfind(feature_flag) != std::string::npos)
84 // For option x, this has the form "feature-flag-name@x". Return "x".
85 return flag_string.substr(feature_flag.size() + 1);
86 }
87 return "";
88 }
89
AddVerboseLoggingInNaclSwitch(base::CommandLine * command_line)90 void AddVerboseLoggingInNaclSwitch(base::CommandLine* command_line) {
91 if (command_line->HasSwitch(switches::kVerboseLoggingInNacl))
92 // Flag is already present, nothing to do here.
93 return;
94
95 std::string option = GetCommandLineFeatureFlagChoice(
96 command_line, switches::kVerboseLoggingInNacl);
97
98 // This needs to be kept in sync with the order of choices for
99 // kVerboseLoggingInNacl in chrome/browser/about_flags.cc
100 if (option == "")
101 return;
102 if (option == "1")
103 return command_line->AppendSwitchASCII(
104 switches::kVerboseLoggingInNacl,
105 switches::kVerboseLoggingInNaclChoiceLow);
106 if (option == "2")
107 return command_line->AppendSwitchASCII(
108 switches::kVerboseLoggingInNacl,
109 switches::kVerboseLoggingInNaclChoiceMedium);
110 if (option == "3")
111 return command_line->AppendSwitchASCII(
112 switches::kVerboseLoggingInNacl,
113 switches::kVerboseLoggingInNaclChoiceHigh);
114 if (option == "4")
115 return command_line->AppendSwitchASCII(
116 switches::kVerboseLoggingInNacl,
117 switches::kVerboseLoggingInNaclChoiceHighest);
118 if (option == "5")
119 return command_line->AppendSwitchASCII(
120 switches::kVerboseLoggingInNacl,
121 switches::kVerboseLoggingInNaclChoiceDisabled);
122 }
123 #endif // BUILDFLAG(IS_CHROMEOS)
124
125 // The child must mimic the behavior of zygote_main_linux.cc on the child
126 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from
127 // if (!child) {
BecomeNaClLoader(base::ScopedFD browser_fd,const NaClLoaderSystemInfo & system_info,nacl::NaClSandbox * nacl_sandbox,const std::vector<std::string> & args)128 void BecomeNaClLoader(base::ScopedFD browser_fd,
129 const NaClLoaderSystemInfo& system_info,
130 nacl::NaClSandbox* nacl_sandbox,
131 const std::vector<std::string>& args) {
132 DCHECK(nacl_sandbox);
133 VLOG(1) << "NaCl loader: setting up IPC descriptor";
134 // Close or shutdown IPC channels that we don't need anymore.
135 PCHECK(0 == IGNORE_EINTR(close(kNaClZygoteDescriptor)));
136
137 // Append any passed switches to the forked loader's command line. This is
138 // necessary to get e.g. any field trial handle and feature overrides from
139 // whomever initiated the this fork request.
140 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
141 command_line.AppendArguments(base::CommandLine::FromArgvWithoutProgram(args),
142 /*include_program=*/false);
143 if (command_line.HasSwitch(switches::kVerboseLoggingInNacl)) {
144 base::Environment::Create()->SetVar(
145 "NACLVERBOSITY",
146 command_line.GetSwitchValueASCII(switches::kVerboseLoggingInNacl));
147 }
148
149 // Always ignore SIGPIPE, for consistency with other Chrome processes and
150 // because some IPC code, such as sync_socket_posix.cc, requires this.
151 // We do this before seccomp-bpf is initialized.
152 PCHECK(signal(SIGPIPE, SIG_IGN) != SIG_ERR);
153
154 base::HistogramSharedMemory::InitFromLaunchParameters(command_line);
155
156 base::FieldTrialList field_trial_list;
157 base::FieldTrialList::CreateTrialsInChildProcess(command_line);
158 auto feature_list = std::make_unique<base::FeatureList>();
159 base::FieldTrialList::ApplyFeatureOverridesInChildProcess(feature_list.get());
160 base::FeatureList::SetInstance(std::move(feature_list));
161
162 // Finish layer-1 sandbox initialization and initialize the layer-2 sandbox.
163 CHECK(!nacl_sandbox->HasOpenDirectory());
164 nacl_sandbox->InitializeLayerTwoSandbox();
165 nacl_sandbox->SealLayerOneSandbox();
166 nacl_sandbox->CheckSandboxingStateWithPolicy();
167
168 base::GlobalDescriptors::GetInstance()->Set(kMojoIPCChannel,
169 browser_fd.release());
170
171 // The Mojo EDK must be initialized before using IPC.
172 mojo::core::InitFeatures();
173 mojo::core::Init();
174
175 base::SingleThreadTaskExecutor io_task_executor(base::MessagePumpType::IO);
176 NaClListener listener;
177 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size);
178 listener.set_number_of_cores(system_info.number_of_cores);
179 listener.Listen();
180
181 _exit(0);
182 }
183
184 // Start the NaCl loader in a child created by the NaCl loader Zygote.
ChildNaClLoaderInit(std::vector<base::ScopedFD> child_fds,const NaClLoaderSystemInfo & system_info,nacl::NaClSandbox * nacl_sandbox,const std::string & channel_id,const std::vector<std::string> & args)185 void ChildNaClLoaderInit(std::vector<base::ScopedFD> child_fds,
186 const NaClLoaderSystemInfo& system_info,
187 nacl::NaClSandbox* nacl_sandbox,
188 const std::string& channel_id,
189 const std::vector<std::string>& args) {
190 DCHECK(child_fds.size() >
191 std::max(content::ZygoteForkDelegate::kPIDOracleFDIndex,
192 content::ZygoteForkDelegate::kBrowserFDIndex));
193
194 // Ping the PID oracle socket.
195 CHECK(content::SendZygoteChildPing(
196 child_fds[content::ZygoteForkDelegate::kPIDOracleFDIndex].get()));
197
198 // Stash the field trial descriptor in GlobalDescriptors so FieldTrialList
199 // can be initialized later. See BecomeNaClLoader().
200 base::GlobalDescriptors::GetInstance()->Set(
201 kFieldTrialDescriptor,
202 child_fds[content::ZygoteForkDelegate::kFieldTrialFDIndex].release());
203
204 // Stash the histogram descriptor in GlobalDescriptors so the histogram
205 // allocator can be initialized later. See BecomeNaClLoader().
206 // TODO(crbug.com/1028263): Always update mapping once metrics shared memory
207 // region is always passed on startup.
208 if (child_fds.size() > content::ZygoteForkDelegate::kHistogramFDIndex &&
209 child_fds[content::ZygoteForkDelegate::kHistogramFDIndex].is_valid()) {
210 base::GlobalDescriptors::GetInstance()->Set(
211 kHistogramSharedMemoryDescriptor,
212 child_fds[content::ZygoteForkDelegate::kHistogramFDIndex].release());
213 }
214
215 // Save the browser socket and close the rest.
216 base::ScopedFD browser_fd(
217 std::move(child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]));
218 child_fds.clear();
219
220 BecomeNaClLoader(std::move(browser_fd), system_info, nacl_sandbox, args);
221 _exit(1);
222 }
223
224 // Handle a fork request from the Zygote.
225 // Some of this code was lifted from
226 // content/browser/zygote_main_linux.cc:ForkWithRealPid()
HandleForkRequest(std::vector<base::ScopedFD> child_fds,const NaClLoaderSystemInfo & system_info,nacl::NaClSandbox * nacl_sandbox,base::PickleIterator * input_iter,base::Pickle * output_pickle)227 bool HandleForkRequest(std::vector<base::ScopedFD> child_fds,
228 const NaClLoaderSystemInfo& system_info,
229 nacl::NaClSandbox* nacl_sandbox,
230 base::PickleIterator* input_iter,
231 base::Pickle* output_pickle) {
232 std::string channel_id;
233 if (!input_iter->ReadString(&channel_id)) {
234 LOG(ERROR) << "Could not read channel_id string";
235 return false;
236 }
237
238 // Read the args passed by the launcher and prepare to forward them to our own
239 // forked child.
240 int argc;
241 if (!input_iter->ReadInt(&argc) || argc < 0) {
242 LOG(ERROR) << "nacl_helper: Invalid argument list";
243 return false;
244 }
245 std::vector<std::string> args(static_cast<size_t>(argc));
246 for (std::string& arg : args) {
247 if (!input_iter->ReadString(&arg)) {
248 LOG(ERROR) << "nacl_helper: Invalid argument list";
249 return false;
250 }
251 }
252
253 // |child_fds| should contain either kNumPassedFDs or kNumPassedFDs-1 file
254 // descriptors.. The actual size of |child_fds| depends on whether or not the
255 // metrics shared memory region is being passed on startup.
256 // TODO(crbug.com/1028263): Expect a fixed size once passing the metrics
257 // shared memory region on startup has been launched.
258 if (child_fds.size() != content::ZygoteForkDelegate::kNumPassedFDs &&
259 child_fds.size() != content::ZygoteForkDelegate::kNumPassedFDs - 1) {
260 LOG(ERROR) << "nacl_helper: unexpected number of fds, got "
261 << child_fds.size();
262 return false;
263 }
264
265 VLOG(1) << "nacl_helper: forking";
266 pid_t child_pid;
267 if (sandbox::NamespaceSandbox::InNewUserNamespace()) {
268 child_pid = sandbox::NamespaceSandbox::ForkInNewPidNamespace(
269 /*drop_capabilities_in_child=*/true);
270 } else {
271 child_pid = sandbox::Credentials::ForkAndDropCapabilitiesInChild();
272 }
273
274 if (child_pid < 0) {
275 PLOG(ERROR) << "*** fork() failed.";
276 }
277
278 if (child_pid == 0) {
279 ChildNaClLoaderInit(std::move(child_fds), system_info, nacl_sandbox,
280 channel_id, args);
281 NOTREACHED();
282 }
283
284 // I am the parent.
285 // First, close the dummy_fd so the sandbox won't find me when
286 // looking for the child's pid in /proc. Also close other fds.
287 child_fds.clear();
288 VLOG(1) << "nacl_helper: child_pid is " << child_pid;
289
290 // Now send child_pid (eventually -1 if fork failed) to the Chrome Zygote.
291 output_pickle->WriteInt(child_pid);
292 return true;
293 }
294
HandleGetTerminationStatusRequest(base::PickleIterator * input_iter,base::Pickle * output_pickle)295 bool HandleGetTerminationStatusRequest(base::PickleIterator* input_iter,
296 base::Pickle* output_pickle) {
297 pid_t child_to_wait;
298 if (!input_iter->ReadInt(&child_to_wait)) {
299 LOG(ERROR) << "Could not read pid to wait for";
300 return false;
301 }
302
303 bool known_dead;
304 if (!input_iter->ReadBool(&known_dead)) {
305 LOG(ERROR) << "Could not read known_dead status";
306 return false;
307 }
308 // TODO(jln): With NaCl, known_dead seems to never be set to true (unless
309 // called from the Zygote's kZygoteCommandReap command). This means that we
310 // will sometimes detect the process as still running when it's not. Fix
311 // this!
312
313 int exit_code;
314 base::TerminationStatus status;
315 if (known_dead)
316 status = base::GetKnownDeadTerminationStatus(child_to_wait, &exit_code);
317 else
318 status = base::GetTerminationStatus(child_to_wait, &exit_code);
319 output_pickle->WriteInt(static_cast<int>(status));
320 output_pickle->WriteInt(exit_code);
321 return true;
322 }
323
324 // Honor a command |command_type|. Eventual command parameters are
325 // available in |input_iter| and eventual file descriptors attached to
326 // the command are in |attached_fds|.
327 // Reply to the command on |reply_fds|.
HonorRequestAndReply(int reply_fd,int command_type,std::vector<base::ScopedFD> attached_fds,const NaClLoaderSystemInfo & system_info,nacl::NaClSandbox * nacl_sandbox,base::PickleIterator * input_iter)328 bool HonorRequestAndReply(int reply_fd,
329 int command_type,
330 std::vector<base::ScopedFD> attached_fds,
331 const NaClLoaderSystemInfo& system_info,
332 nacl::NaClSandbox* nacl_sandbox,
333 base::PickleIterator* input_iter) {
334 base::Pickle write_pickle;
335 bool have_to_reply = false;
336 // Commands must write anything to send back to |write_pickle|.
337 switch (command_type) {
338 case nacl::kNaClForkRequest:
339 have_to_reply =
340 HandleForkRequest(std::move(attached_fds), system_info, nacl_sandbox,
341 input_iter, &write_pickle);
342 break;
343 case nacl::kNaClGetTerminationStatusRequest:
344 have_to_reply =
345 HandleGetTerminationStatusRequest(input_iter, &write_pickle);
346 break;
347 default:
348 LOG(ERROR) << "Unsupported command from Zygote";
349 return false;
350 }
351 if (!have_to_reply)
352 return false;
353 const std::vector<int> empty; // We never send file descriptors back.
354 if (!base::UnixDomainSocket::SendMsg(reply_fd, write_pickle.data(),
355 write_pickle.size(), empty)) {
356 LOG(ERROR) << "*** send() to zygote failed";
357 return false;
358 }
359 return true;
360 }
361
362 // Read a request from the Zygote from |zygote_ipc_fd| and handle it.
363 // Die on EOF from |zygote_ipc_fd|.
HandleZygoteRequest(int zygote_ipc_fd,const NaClLoaderSystemInfo & system_info,nacl::NaClSandbox * nacl_sandbox)364 bool HandleZygoteRequest(int zygote_ipc_fd,
365 const NaClLoaderSystemInfo& system_info,
366 nacl::NaClSandbox* nacl_sandbox) {
367 std::vector<base::ScopedFD> fds;
368 uint8_t buf[kNaClMaxIPCMessageLength];
369 const ssize_t msglen = base::UnixDomainSocket::RecvMsg(zygote_ipc_fd,
370 &buf, sizeof(buf), &fds);
371 // If the Zygote has started handling requests, we should be sandboxed via
372 // the setuid sandbox.
373 if (!nacl_sandbox->layer_one_enabled()) {
374 LOG(ERROR) << "NaCl helper process running without a sandbox!\n"
375 << "Most likely you need to configure your SUID sandbox "
376 << "correctly";
377 }
378 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) {
379 // EOF from the browser. Goodbye!
380 _exit(0);
381 }
382 if (msglen < 0) {
383 PLOG(ERROR) << "nacl_helper: receive from zygote failed";
384 return false;
385 }
386
387 base::Pickle read_pickle = base::Pickle::WithUnownedBuffer(
388 base::span(buf, base::checked_cast<size_t>(msglen)));
389 base::PickleIterator read_iter(read_pickle);
390 int command_type;
391 if (!read_iter.ReadInt(&command_type)) {
392 LOG(ERROR) << "Unable to read command from Zygote";
393 return false;
394 }
395 return HonorRequestAndReply(zygote_ipc_fd, command_type, std::move(fds),
396 system_info, nacl_sandbox, &read_iter);
397 }
398
399 static const char kNaClHelperReservedAtZero[] = "reserved_at_zero";
400 static const char kNaClHelperRDebug[] = "r_debug";
401
402 // Since we were started by nacl_helper_bootstrap rather than in the
403 // usual way, the debugger cannot figure out where our executable
404 // or the dynamic linker or the shared libraries are in memory,
405 // so it won't find any symbols. But we can fake it out to find us.
406 //
407 // The zygote passes --r_debug=0xXXXXXXXXXXXXXXXX.
408 // nacl_helper_bootstrap replaces the Xs with the address of its _r_debug
409 // structure. The debugger will look for that symbol by name to
410 // discover the addresses of key dynamic linker data structures.
411 // Since all it knows about is the original main executable, which
412 // is the bootstrap program, it finds the symbol defined there. The
413 // dynamic linker's structure is somewhere else, but it is filled in
414 // after initialization. The parts that really matter to the
415 // debugger never change. So we just copy the contents of the
416 // dynamic linker's structure into the address provided by the option.
417 // Hereafter, if someone attaches a debugger (or examines a core dump),
418 // the debugger will find all the symbols in the normal way.
CheckRDebug(char * argv0)419 static void CheckRDebug(char* argv0) {
420 std::string r_debug_switch_value =
421 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
422 kNaClHelperRDebug);
423 if (!r_debug_switch_value.empty()) {
424 char* endp;
425 uintptr_t r_debug_addr = strtoul(r_debug_switch_value.c_str(), &endp, 0);
426 if (r_debug_addr != 0 && *endp == '\0') {
427 r_debug* bootstrap_r_debug = reinterpret_cast<r_debug*>(r_debug_addr);
428 *bootstrap_r_debug = _r_debug;
429
430 // Since the main executable (the bootstrap program) does not
431 // have a dynamic section, the debugger will not skip the
432 // first element of the link_map list as it usually would for
433 // an executable or PIE that was loaded normally. But the
434 // dynamic linker has set l_name for the PIE to "" as is
435 // normal for the main executable. So the debugger doesn't
436 // know which file it is. Fill in the actual file name, which
437 // came in as our argv[0].
438 link_map* l = _r_debug.r_map;
439 if (l->l_name[0] == '\0')
440 l->l_name = argv0;
441 }
442 }
443 }
444
445 // The zygote passes --reserved_at_zero=0xXXXXXXXXXXXXXXXX.
446 // nacl_helper_bootstrap replaces the Xs with the amount of prereserved
447 // sandbox memory.
448 //
449 // CheckReservedAtZero parses the value of the argument reserved_at_zero
450 // and returns the amount of prereserved sandbox memory.
CheckReservedAtZero()451 static size_t CheckReservedAtZero() {
452 size_t prereserved_sandbox_size = 0;
453 std::string reserved_at_zero_switch_value =
454 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
455 kNaClHelperReservedAtZero);
456 if (!reserved_at_zero_switch_value.empty()) {
457 char* endp;
458 prereserved_sandbox_size =
459 strtoul(reserved_at_zero_switch_value.c_str(), &endp, 0);
460 if (*endp != '\0')
461 LOG(ERROR) << "Could not parse reserved_at_zero argument value of "
462 << reserved_at_zero_switch_value;
463 }
464 return prereserved_sandbox_size;
465 }
466
467 } // namespace
468
469 #if defined(ADDRESS_SANITIZER)
470 // Do not install the SIGSEGV handler in ASan. This should make the NaCl
471 // platform qualification test pass.
472 // detect_odr_violation=0: http://crbug.com/376306
473 extern const char* kAsanDefaultOptionsNaCl;
474 const char* kAsanDefaultOptionsNaCl = "handle_segv=0:detect_odr_violation=0";
475 #endif
476
main(int argc,char * argv[])477 int main(int argc, char* argv[]) {
478 base::CommandLine::Init(argc, argv);
479 base::AtExitManager exit_manager;
480 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised
481
482 const NaClLoaderSystemInfo system_info = {CheckReservedAtZero(),
483 sysconf(_SC_NPROCESSORS_ONLN)};
484
485 CheckRDebug(argv[0]);
486
487 #if BUILDFLAG(IS_CHROMEOS)
488 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
489 AddVerboseLoggingInNaclSwitch(command_line);
490 if (command_line->HasSwitch(switches::kVerboseLoggingInNacl)) {
491 if (!freopen("/tmp/nacl.log", "a", stderr))
492 LOG(WARNING) << "Could not open /tmp/nacl.log";
493 }
494 #endif
495
496 std::unique_ptr<nacl::NaClSandbox> nacl_sandbox(new nacl::NaClSandbox);
497 // Make sure that the early initialization did not start any spurious
498 // threads.
499 #if !defined(THREAD_SANITIZER)
500 CHECK(nacl_sandbox->IsSingleThreaded());
501 #endif
502
503 const bool is_init_process = 1 == getpid();
504 nacl_sandbox->InitializeLayerOneSandbox();
505 CHECK_EQ(is_init_process, nacl_sandbox->layer_one_enabled());
506
507 const std::vector<int> empty;
508 // Send the zygote a message to let it know we are ready to help
509 if (!base::UnixDomainSocket::SendMsg(kNaClZygoteDescriptor,
510 kNaClHelperStartupAck,
511 sizeof(kNaClHelperStartupAck), empty)) {
512 LOG(ERROR) << "*** send() to zygote failed";
513 }
514
515 // Now handle requests from the Zygote.
516 while (true) {
517 bool request_handled = HandleZygoteRequest(
518 kNaClZygoteDescriptor, system_info, nacl_sandbox.get());
519 // Do not turn this into a CHECK() without thinking about robustness
520 // against malicious IPC requests.
521 DCHECK(request_handled);
522 }
523 NOTREACHED();
524 }
525