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