OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // A mini-zygote specifically for Native Client. | 5 // A mini-zygote specifically for Native Client. |
6 | 6 |
7 #include "components/nacl/loader/nacl_helper_linux.h" | 7 #include "components/nacl/loader/nacl_helper_linux.h" |
8 | 8 |
9 #include <errno.h> | 9 #include <errno.h> |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
(...skipping 10 matching lines...) Expand all Loading... | |
21 | 21 |
22 #include "base/at_exit.h" | 22 #include "base/at_exit.h" |
23 #include "base/command_line.h" | 23 #include "base/command_line.h" |
24 #include "base/logging.h" | 24 #include "base/logging.h" |
25 #include "base/message_loop/message_loop.h" | 25 #include "base/message_loop/message_loop.h" |
26 #include "base/posix/eintr_wrapper.h" | 26 #include "base/posix/eintr_wrapper.h" |
27 #include "base/posix/global_descriptors.h" | 27 #include "base/posix/global_descriptors.h" |
28 #include "base/posix/unix_domain_socket_linux.h" | 28 #include "base/posix/unix_domain_socket_linux.h" |
29 #include "base/process/kill.h" | 29 #include "base/process/kill.h" |
30 #include "base/rand_util.h" | 30 #include "base/rand_util.h" |
31 #include "components/nacl/common/nacl_switches.h" | |
31 #include "components/nacl/loader/nacl_listener.h" | 32 #include "components/nacl/loader/nacl_listener.h" |
32 #include "components/nacl/loader/nacl_sandbox_linux.h" | 33 #include "components/nacl/loader/nacl_sandbox_linux.h" |
33 #include "content/public/common/zygote_fork_delegate_linux.h" | 34 #include "content/public/common/zygote_fork_delegate_linux.h" |
34 #include "crypto/nss_util.h" | 35 #include "crypto/nss_util.h" |
35 #include "ipc/ipc_descriptors.h" | 36 #include "ipc/ipc_descriptors.h" |
36 #include "ipc/ipc_switches.h" | 37 #include "ipc/ipc_switches.h" |
37 #include "sandbox/linux/services/libc_urandom_override.h" | 38 #include "sandbox/linux/services/libc_urandom_override.h" |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 struct NaClLoaderSystemInfo { | 42 struct NaClLoaderSystemInfo { |
42 size_t prereserved_sandbox_size; | 43 size_t prereserved_sandbox_size; |
43 long number_of_cores; | 44 long number_of_cores; |
44 }; | 45 }; |
45 | 46 |
47 // This is a poor man's check on whether we are sandboxed. | |
48 bool IsSandboxed() { | |
49 int proc_fd = open("/proc/self/exe", O_RDONLY); | |
50 if (proc_fd >= 0) { | |
51 close(proc_fd); | |
52 return false; | |
53 } | |
54 return true; | |
55 } | |
56 | |
57 void InitializeSandbox(bool uses_nonsfi_mode) { | |
58 if (uses_nonsfi_mode) { | |
59 const bool setuid_sandbox_enabled = IsSandboxed(); | |
60 CHECK(setuid_sandbox_enabled) | |
61 << "SUID sandbox is mandatory for non-SFI NaCl"; | |
62 // TODO(hamaji): Add a strict seccomp sandbox for non-SFI NaCl. | |
63 // https://code.google.com/p/chromium/issues/detail?id=359285 | |
64 bool bpf_sandbox_initialized = InitializeBPFSandbox(); | |
65 CHECK(bpf_sandbox_initialized) | |
66 << "Could not initialize NaCl's second " | |
67 << "layer sandbox (seccomp-bpf) for non-SFI mode."; | |
68 } else { | |
69 bool bpf_sandbox_initialized = InitializeBPFSandbox(); | |
70 if (!bpf_sandbox_initialized) { | |
71 LOG(ERROR) << "Could not initialize NaCl's second " | |
72 << "layer sandbox (seccomp-bpf) for SFI mode."; | |
73 } | |
74 } | |
75 } | |
76 | |
46 // The child must mimic the behavior of zygote_main_linux.cc on the child | 77 // The child must mimic the behavior of zygote_main_linux.cc on the child |
47 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from | 78 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from |
48 // if (!child) { | 79 // if (!child) { |
49 void BecomeNaClLoader(const std::vector<int>& child_fds, | 80 void BecomeNaClLoader(const std::vector<int>& child_fds, |
50 const NaClLoaderSystemInfo& system_info, | 81 const NaClLoaderSystemInfo& system_info, |
51 bool uses_nonsfi_mode) { | 82 bool uses_nonsfi_mode, |
83 bool no_sandbox) { | |
52 VLOG(1) << "NaCl loader: setting up IPC descriptor"; | 84 VLOG(1) << "NaCl loader: setting up IPC descriptor"; |
53 // don't need zygote FD any more | 85 // don't need zygote FD any more |
54 if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0) | 86 if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0) |
55 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; | 87 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; |
56 bool sandbox_initialized = InitializeBPFSandbox(); | 88 if (!no_sandbox) |
57 if (!sandbox_initialized) { | 89 InitializeSandbox(uses_nonsfi_mode); |
58 LOG(ERROR) << "Could not initialize NaCl's second " | |
59 << "layer sandbox (seccomp-bpf)."; | |
60 } | |
61 base::GlobalDescriptors::GetInstance()->Set( | 90 base::GlobalDescriptors::GetInstance()->Set( |
62 kPrimaryIPCChannel, | 91 kPrimaryIPCChannel, |
63 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]); | 92 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]); |
64 | 93 |
65 base::MessageLoopForIO main_message_loop; | 94 base::MessageLoopForIO main_message_loop; |
66 NaClListener listener; | 95 NaClListener listener; |
67 listener.set_uses_nonsfi_mode(uses_nonsfi_mode); | 96 listener.set_uses_nonsfi_mode(uses_nonsfi_mode); |
68 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); | 97 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); |
69 listener.set_number_of_cores(system_info.number_of_cores); | 98 listener.set_number_of_cores(system_info.number_of_cores); |
70 listener.Listen(); | 99 listener.Listen(); |
71 _exit(0); | 100 _exit(0); |
72 } | 101 } |
73 | 102 |
74 // Start the NaCl loader in a child created by the NaCl loader Zygote. | 103 // Start the NaCl loader in a child created by the NaCl loader Zygote. |
75 void ChildNaClLoaderInit(const std::vector<int>& child_fds, | 104 void ChildNaClLoaderInit(const std::vector<int>& child_fds, |
76 const NaClLoaderSystemInfo& system_info, | 105 const NaClLoaderSystemInfo& system_info, |
77 bool uses_nonsfi_mode) { | 106 bool uses_nonsfi_mode) { |
107 bool no_sandbox = false; | |
108 if (uses_nonsfi_mode) { | |
109 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
110 no_sandbox = cmd_line->HasSwitch(switches::kNaClDangerousNoSandboxNonSfi); | |
jln (very slow on Chromium)
2014/04/07 23:38:10
Does this really work? How does this get forwarded
hamaji
2014/04/08 04:38:30
No, I forgot to merge two commits. Now the flag sh
| |
111 if (no_sandbox) | |
112 LOG(ERROR) << "DANGEROUS: Running non-SFI NaCl without sandbox!"; | |
113 // We reset command line flags for non-SFI mode not to let flags | |
114 // for SFI mode accidentally changes the behavior of non-SFI mode. | |
115 // Particularly, non-SFI NaCl should disregard | |
116 // --disable-seccomp-filter-sandbox. This flag is considered safe | |
117 // for SFI NaCl as seccomp sandbox is its secondary syscall | |
118 // sandbox. However, non-SFI NaCl should initialize seccomp unless | |
119 // more scary flag (i.e., --nacl-dangerous-no-sandbox-nonsfi) is | |
120 // specified. | |
121 cmd_line->Reset(); | |
122 CHECK(cmd_line->Init(0, NULL)); | |
jln (very slow on Chromium)
2014/04/07 23:38:10
Instead of doing this, simply relax the CHECK() li
hamaji
2014/04/08 04:38:30
Done.
| |
123 } | |
124 | |
78 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; | 125 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; |
79 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; | 126 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; |
80 bool validack = false; | 127 bool validack = false; |
81 const size_t kMaxReadSize = 1024; | 128 const size_t kMaxReadSize = 1024; |
82 char buffer[kMaxReadSize]; | 129 char buffer[kMaxReadSize]; |
83 // Wait until the parent process has discovered our PID. We | 130 // Wait until the parent process has discovered our PID. We |
84 // should not fork any child processes (which the seccomp | 131 // should not fork any child processes (which the seccomp |
85 // sandbox does) until then, because that can interfere with the | 132 // sandbox does) until then, because that can interfere with the |
86 // parent's discovery of our PID. | 133 // parent's discovery of our PID. |
87 const int nread = HANDLE_EINTR(read(parent_fd, buffer, kMaxReadSize)); | 134 const int nread = HANDLE_EINTR(read(parent_fd, buffer, kMaxReadSize)); |
(...skipping 11 matching lines...) Expand all Loading... | |
99 switches::kProcessChannelID, | 146 switches::kProcessChannelID, |
100 std::string(&buffer[len], nread - len)); | 147 std::string(&buffer[len], nread - len)); |
101 validack = true; | 148 validack = true; |
102 } | 149 } |
103 } | 150 } |
104 if (IGNORE_EINTR(close(dummy_fd)) != 0) | 151 if (IGNORE_EINTR(close(dummy_fd)) != 0) |
105 LOG(ERROR) << "close(dummy_fd) failed"; | 152 LOG(ERROR) << "close(dummy_fd) failed"; |
106 if (IGNORE_EINTR(close(parent_fd)) != 0) | 153 if (IGNORE_EINTR(close(parent_fd)) != 0) |
107 LOG(ERROR) << "close(parent_fd) failed"; | 154 LOG(ERROR) << "close(parent_fd) failed"; |
108 if (validack) { | 155 if (validack) { |
109 BecomeNaClLoader(child_fds, system_info, uses_nonsfi_mode); | 156 BecomeNaClLoader(child_fds, system_info, uses_nonsfi_mode, no_sandbox); |
110 } else { | 157 } else { |
111 LOG(ERROR) << "Failed to synch with zygote"; | 158 LOG(ERROR) << "Failed to synch with zygote"; |
112 } | 159 } |
113 _exit(1); | 160 _exit(1); |
114 } | 161 } |
115 | 162 |
116 // Handle a fork request from the Zygote. | 163 // Handle a fork request from the Zygote. |
117 // Some of this code was lifted from | 164 // Some of this code was lifted from |
118 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | 165 // content/browser/zygote_main_linux.cc:ForkWithRealPid() |
119 bool HandleForkRequest(const std::vector<int>& child_fds, | 166 bool HandleForkRequest(const std::vector<int>& child_fds, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 base::TerminationStatus status; | 226 base::TerminationStatus status; |
180 if (known_dead) | 227 if (known_dead) |
181 status = base::GetKnownDeadTerminationStatus(child_to_wait, &exit_code); | 228 status = base::GetKnownDeadTerminationStatus(child_to_wait, &exit_code); |
182 else | 229 else |
183 status = base::GetTerminationStatus(child_to_wait, &exit_code); | 230 status = base::GetTerminationStatus(child_to_wait, &exit_code); |
184 output_pickle->WriteInt(static_cast<int>(status)); | 231 output_pickle->WriteInt(static_cast<int>(status)); |
185 output_pickle->WriteInt(exit_code); | 232 output_pickle->WriteInt(exit_code); |
186 return true; | 233 return true; |
187 } | 234 } |
188 | 235 |
189 // This is a poor man's check on whether we are sandboxed. | |
190 bool IsSandboxed() { | |
191 int proc_fd = open("/proc/self/exe", O_RDONLY); | |
192 if (proc_fd >= 0) { | |
193 close(proc_fd); | |
194 return false; | |
195 } | |
196 return true; | |
197 } | |
198 | |
199 // Honor a command |command_type|. Eventual command parameters are | 236 // Honor a command |command_type|. Eventual command parameters are |
200 // available in |input_iter| and eventual file descriptors attached to | 237 // available in |input_iter| and eventual file descriptors attached to |
201 // the command are in |attached_fds|. | 238 // the command are in |attached_fds|. |
202 // Reply to the command on |reply_fds|. | 239 // Reply to the command on |reply_fds|. |
203 bool HonorRequestAndReply(int reply_fd, | 240 bool HonorRequestAndReply(int reply_fd, |
204 int command_type, | 241 int command_type, |
205 const std::vector<int>& attached_fds, | 242 const std::vector<int>& attached_fds, |
206 const NaClLoaderSystemInfo& system_info, | 243 const NaClLoaderSystemInfo& system_info, |
207 PickleIterator* input_iter) { | 244 PickleIterator* input_iter) { |
208 Pickle write_pickle; | 245 Pickle write_pickle; |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
395 // Now handle requests from the Zygote. | 432 // Now handle requests from the Zygote. |
396 while (true) { | 433 while (true) { |
397 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, | 434 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, |
398 system_info); | 435 system_info); |
399 // Do not turn this into a CHECK() without thinking about robustness | 436 // Do not turn this into a CHECK() without thinking about robustness |
400 // against malicious IPC requests. | 437 // against malicious IPC requests. |
401 DCHECK(request_handled); | 438 DCHECK(request_handled); |
402 } | 439 } |
403 NOTREACHED(); | 440 NOTREACHED(); |
404 } | 441 } |
OLD | NEW |