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> |
11 #include <link.h> | 11 #include <link.h> |
12 #include <signal.h> | 12 #include <signal.h> |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
15 #include <sys/socket.h> | 15 #include <sys/socket.h> |
16 #include <sys/stat.h> | 16 #include <sys/stat.h> |
17 #include <sys/types.h> | 17 #include <sys/types.h> |
18 | 18 |
19 #include <string> | 19 #include <string> |
20 #include <vector> | 20 #include <vector> |
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/files/scoped_file.h" |
24 #include "base/logging.h" | 25 #include "base/logging.h" |
25 #include "base/memory/scoped_ptr.h" | 26 #include "base/memory/scoped_ptr.h" |
| 27 #include "base/memory/scoped_vector.h" |
26 #include "base/message_loop/message_loop.h" | 28 #include "base/message_loop/message_loop.h" |
27 #include "base/posix/eintr_wrapper.h" | 29 #include "base/posix/eintr_wrapper.h" |
28 #include "base/posix/global_descriptors.h" | 30 #include "base/posix/global_descriptors.h" |
29 #include "base/posix/unix_domain_socket_linux.h" | 31 #include "base/posix/unix_domain_socket_linux.h" |
30 #include "base/process/kill.h" | 32 #include "base/process/kill.h" |
31 #include "base/process/process_handle.h" | 33 #include "base/process/process_handle.h" |
32 #include "base/rand_util.h" | 34 #include "base/rand_util.h" |
33 #include "components/nacl/common/nacl_switches.h" | 35 #include "components/nacl/common/nacl_switches.h" |
34 #include "components/nacl/loader/nacl_listener.h" | 36 #include "components/nacl/loader/nacl_listener.h" |
35 #include "components/nacl/loader/nacl_sandbox_linux.h" | 37 #include "components/nacl/loader/nacl_sandbox_linux.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 if (!bpf_sandbox_initialized) { | 111 if (!bpf_sandbox_initialized) { |
110 LOG(ERROR) << "Could not initialize NaCl's second " | 112 LOG(ERROR) << "Could not initialize NaCl's second " |
111 << "layer sandbox (seccomp-bpf) for SFI mode."; | 113 << "layer sandbox (seccomp-bpf) for SFI mode."; |
112 } | 114 } |
113 } | 115 } |
114 } | 116 } |
115 | 117 |
116 // The child must mimic the behavior of zygote_main_linux.cc on the child | 118 // The child must mimic the behavior of zygote_main_linux.cc on the child |
117 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from | 119 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from |
118 // if (!child) { | 120 // if (!child) { |
119 void BecomeNaClLoader(const std::vector<int>& child_fds, | 121 void BecomeNaClLoader(base::ScopedFD browser_fd, |
120 const NaClLoaderSystemInfo& system_info, | 122 const NaClLoaderSystemInfo& system_info, |
121 bool uses_nonsfi_mode) { | 123 bool uses_nonsfi_mode) { |
122 VLOG(1) << "NaCl loader: setting up IPC descriptor"; | 124 VLOG(1) << "NaCl loader: setting up IPC descriptor"; |
123 // don't need zygote FD any more | 125 // don't need zygote FD any more |
124 if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0) | 126 if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0) |
125 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; | 127 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; |
126 InitializeLayerTwoSandbox(uses_nonsfi_mode); | 128 InitializeLayerTwoSandbox(uses_nonsfi_mode); |
127 base::GlobalDescriptors::GetInstance()->Set( | 129 base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel, |
128 kPrimaryIPCChannel, | 130 browser_fd.release()); |
129 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]); | |
130 | 131 |
131 base::MessageLoopForIO main_message_loop; | 132 base::MessageLoopForIO main_message_loop; |
132 NaClListener listener; | 133 NaClListener listener; |
133 listener.set_uses_nonsfi_mode(uses_nonsfi_mode); | 134 listener.set_uses_nonsfi_mode(uses_nonsfi_mode); |
134 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); | 135 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); |
135 listener.set_number_of_cores(system_info.number_of_cores); | 136 listener.set_number_of_cores(system_info.number_of_cores); |
136 listener.Listen(); | 137 listener.Listen(); |
137 _exit(0); | 138 _exit(0); |
138 } | 139 } |
139 | 140 |
140 // Start the NaCl loader in a child created by the NaCl loader Zygote. | 141 // Start the NaCl loader in a child created by the NaCl loader Zygote. |
141 void ChildNaClLoaderInit(const std::vector<int>& child_fds, | 142 void ChildNaClLoaderInit(ScopedVector<base::ScopedFD> child_fds, |
142 const NaClLoaderSystemInfo& system_info, | 143 const NaClLoaderSystemInfo& system_info, |
143 bool uses_nonsfi_mode, | 144 bool uses_nonsfi_mode, |
144 const std::string& channel_id) { | 145 const std::string& channel_id) { |
145 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; | |
146 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; | |
147 | |
148 bool validack = false; | 146 bool validack = false; |
149 base::ProcessId real_pid; | 147 base::ProcessId real_pid; |
150 // Wait until the parent process has discovered our PID. We | 148 // Wait until the parent process has discovered our PID. We |
151 // should not fork any child processes (which the seccomp | 149 // should not fork any child processes (which the seccomp |
152 // sandbox does) until then, because that can interfere with the | 150 // sandbox does) until then, because that can interfere with the |
153 // parent's discovery of our PID. | 151 // parent's discovery of our PID. |
154 const ssize_t nread = | 152 const ssize_t nread = HANDLE_EINTR( |
155 HANDLE_EINTR(read(parent_fd, &real_pid, sizeof(real_pid))); | 153 read(child_fds[content::ZygoteForkDelegate::kParentFDIndex]->get(), |
| 154 &real_pid, |
| 155 sizeof(real_pid))); |
156 if (static_cast<size_t>(nread) == sizeof(real_pid)) { | 156 if (static_cast<size_t>(nread) == sizeof(real_pid)) { |
157 // Make sure the parent didn't accidentally send us our real PID. | 157 // Make sure the parent didn't accidentally send us our real PID. |
158 // We don't want it to be discoverable anywhere in our address space | 158 // We don't want it to be discoverable anywhere in our address space |
159 // when we start running untrusted code. | 159 // when we start running untrusted code. |
160 CHECK(real_pid == 0); | 160 CHECK(real_pid == 0); |
161 | 161 |
162 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 162 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
163 switches::kProcessChannelID, channel_id); | 163 switches::kProcessChannelID, channel_id); |
164 validack = true; | 164 validack = true; |
165 } else { | 165 } else { |
166 if (nread < 0) | 166 if (nread < 0) |
167 perror("read"); | 167 perror("read"); |
168 LOG(ERROR) << "read returned " << nread; | 168 LOG(ERROR) << "read returned " << nread; |
169 } | 169 } |
170 | 170 |
171 if (IGNORE_EINTR(close(dummy_fd)) != 0) | 171 base::ScopedFD browser_fd( |
172 LOG(ERROR) << "close(dummy_fd) failed"; | 172 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]->Pass()); |
173 if (IGNORE_EINTR(close(parent_fd)) != 0) | 173 child_fds.clear(); |
174 LOG(ERROR) << "close(parent_fd) failed"; | 174 |
175 if (validack) { | 175 if (validack) { |
176 BecomeNaClLoader(child_fds, system_info, uses_nonsfi_mode); | 176 BecomeNaClLoader(browser_fd.Pass(), system_info, uses_nonsfi_mode); |
177 } else { | 177 } else { |
178 LOG(ERROR) << "Failed to synch with zygote"; | 178 LOG(ERROR) << "Failed to synch with zygote"; |
179 } | 179 } |
180 _exit(1); | 180 _exit(1); |
181 } | 181 } |
182 | 182 |
183 // Handle a fork request from the Zygote. | 183 // Handle a fork request from the Zygote. |
184 // Some of this code was lifted from | 184 // Some of this code was lifted from |
185 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | 185 // content/browser/zygote_main_linux.cc:ForkWithRealPid() |
186 bool HandleForkRequest(const std::vector<int>& child_fds, | 186 bool HandleForkRequest(ScopedVector<base::ScopedFD> child_fds, |
187 const NaClLoaderSystemInfo& system_info, | 187 const NaClLoaderSystemInfo& system_info, |
188 PickleIterator* input_iter, | 188 PickleIterator* input_iter, |
189 Pickle* output_pickle) { | 189 Pickle* output_pickle) { |
190 bool uses_nonsfi_mode; | 190 bool uses_nonsfi_mode; |
191 if (!input_iter->ReadBool(&uses_nonsfi_mode)) { | 191 if (!input_iter->ReadBool(&uses_nonsfi_mode)) { |
192 LOG(ERROR) << "Could not read uses_nonsfi_mode status"; | 192 LOG(ERROR) << "Could not read uses_nonsfi_mode status"; |
193 return false; | 193 return false; |
194 } | 194 } |
195 | 195 |
196 std::string channel_id; | 196 std::string channel_id; |
197 if (!input_iter->ReadString(&channel_id)) { | 197 if (!input_iter->ReadString(&channel_id)) { |
198 LOG(ERROR) << "Could not read channel_id string"; | 198 LOG(ERROR) << "Could not read channel_id string"; |
199 return false; | 199 return false; |
200 } | 200 } |
201 | 201 |
202 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) { | 202 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) { |
203 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | 203 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " |
204 << child_fds.size(); | 204 << child_fds.size(); |
205 return false; | 205 return false; |
206 } | 206 } |
207 | 207 |
208 VLOG(1) << "nacl_helper: forking"; | 208 VLOG(1) << "nacl_helper: forking"; |
209 pid_t child_pid = fork(); | 209 pid_t child_pid = fork(); |
210 if (child_pid < 0) { | 210 if (child_pid < 0) { |
211 PLOG(ERROR) << "*** fork() failed."; | 211 PLOG(ERROR) << "*** fork() failed."; |
212 } | 212 } |
213 | 213 |
214 if (child_pid == 0) { | 214 if (child_pid == 0) { |
215 ChildNaClLoaderInit(child_fds, system_info, uses_nonsfi_mode, channel_id); | 215 ChildNaClLoaderInit( |
| 216 child_fds.Pass(), system_info, uses_nonsfi_mode, channel_id); |
216 NOTREACHED(); | 217 NOTREACHED(); |
217 } | 218 } |
218 | 219 |
219 // I am the parent. | 220 // I am the parent. |
220 // First, close the dummy_fd so the sandbox won't find me when | 221 // First, close the dummy_fd so the sandbox won't find me when |
221 // looking for the child's pid in /proc. Also close other fds. | 222 // looking for the child's pid in /proc. Also close other fds. |
222 for (size_t i = 0; i < child_fds.size(); i++) { | 223 child_fds.clear(); |
223 if (IGNORE_EINTR(close(child_fds[i])) != 0) | |
224 LOG(ERROR) << "close(child_fds[i]) failed"; | |
225 } | |
226 VLOG(1) << "nacl_helper: child_pid is " << child_pid; | 224 VLOG(1) << "nacl_helper: child_pid is " << child_pid; |
227 | 225 |
228 // Now send child_pid (eventually -1 if fork failed) to the Chrome Zygote. | 226 // Now send child_pid (eventually -1 if fork failed) to the Chrome Zygote. |
229 output_pickle->WriteInt(child_pid); | 227 output_pickle->WriteInt(child_pid); |
230 return true; | 228 return true; |
231 } | 229 } |
232 | 230 |
233 bool HandleGetTerminationStatusRequest(PickleIterator* input_iter, | 231 bool HandleGetTerminationStatusRequest(PickleIterator* input_iter, |
234 Pickle* output_pickle) { | 232 Pickle* output_pickle) { |
235 pid_t child_to_wait; | 233 pid_t child_to_wait; |
(...skipping 22 matching lines...) Expand all Loading... |
258 output_pickle->WriteInt(exit_code); | 256 output_pickle->WriteInt(exit_code); |
259 return true; | 257 return true; |
260 } | 258 } |
261 | 259 |
262 // Honor a command |command_type|. Eventual command parameters are | 260 // Honor a command |command_type|. Eventual command parameters are |
263 // available in |input_iter| and eventual file descriptors attached to | 261 // available in |input_iter| and eventual file descriptors attached to |
264 // the command are in |attached_fds|. | 262 // the command are in |attached_fds|. |
265 // Reply to the command on |reply_fds|. | 263 // Reply to the command on |reply_fds|. |
266 bool HonorRequestAndReply(int reply_fd, | 264 bool HonorRequestAndReply(int reply_fd, |
267 int command_type, | 265 int command_type, |
268 const std::vector<int>& attached_fds, | 266 ScopedVector<base::ScopedFD> attached_fds, |
269 const NaClLoaderSystemInfo& system_info, | 267 const NaClLoaderSystemInfo& system_info, |
270 PickleIterator* input_iter) { | 268 PickleIterator* input_iter) { |
271 Pickle write_pickle; | 269 Pickle write_pickle; |
272 bool have_to_reply = false; | 270 bool have_to_reply = false; |
273 // Commands must write anything to send back to |write_pickle|. | 271 // Commands must write anything to send back to |write_pickle|. |
274 switch (command_type) { | 272 switch (command_type) { |
275 case nacl::kNaClForkRequest: | 273 case nacl::kNaClForkRequest: |
276 have_to_reply = HandleForkRequest(attached_fds, system_info, | 274 have_to_reply = HandleForkRequest( |
277 input_iter, &write_pickle); | 275 attached_fds.Pass(), system_info, input_iter, &write_pickle); |
278 break; | 276 break; |
279 case nacl::kNaClGetTerminationStatusRequest: | 277 case nacl::kNaClGetTerminationStatusRequest: |
280 have_to_reply = | 278 have_to_reply = |
281 HandleGetTerminationStatusRequest(input_iter, &write_pickle); | 279 HandleGetTerminationStatusRequest(input_iter, &write_pickle); |
282 break; | 280 break; |
283 default: | 281 default: |
284 LOG(ERROR) << "Unsupported command from Zygote"; | 282 LOG(ERROR) << "Unsupported command from Zygote"; |
285 return false; | 283 return false; |
286 } | 284 } |
287 if (!have_to_reply) | 285 if (!have_to_reply) |
288 return false; | 286 return false; |
289 const std::vector<int> empty; // We never send file descriptors back. | 287 const std::vector<int> empty; // We never send file descriptors back. |
290 if (!UnixDomainSocket::SendMsg(reply_fd, write_pickle.data(), | 288 if (!UnixDomainSocket::SendMsg(reply_fd, write_pickle.data(), |
291 write_pickle.size(), empty)) { | 289 write_pickle.size(), empty)) { |
292 LOG(ERROR) << "*** send() to zygote failed"; | 290 LOG(ERROR) << "*** send() to zygote failed"; |
293 return false; | 291 return false; |
294 } | 292 } |
295 return true; | 293 return true; |
296 } | 294 } |
297 | 295 |
298 // Read a request from the Zygote from |zygote_ipc_fd| and handle it. | 296 // Read a request from the Zygote from |zygote_ipc_fd| and handle it. |
299 // Die on EOF from |zygote_ipc_fd|. | 297 // Die on EOF from |zygote_ipc_fd|. |
300 bool HandleZygoteRequest(int zygote_ipc_fd, | 298 bool HandleZygoteRequest(int zygote_ipc_fd, |
301 const NaClLoaderSystemInfo& system_info) { | 299 const NaClLoaderSystemInfo& system_info) { |
302 std::vector<int> fds; | 300 ScopedVector<base::ScopedFD> fds; |
303 char buf[kNaClMaxIPCMessageLength]; | 301 char buf[kNaClMaxIPCMessageLength]; |
304 const ssize_t msglen = UnixDomainSocket::RecvMsg(zygote_ipc_fd, | 302 const ssize_t msglen = UnixDomainSocket::RecvMsg(zygote_ipc_fd, |
305 &buf, sizeof(buf), &fds); | 303 &buf, sizeof(buf), &fds); |
306 // If the Zygote has started handling requests, we should be sandboxed via | 304 // If the Zygote has started handling requests, we should be sandboxed via |
307 // the setuid sandbox. | 305 // the setuid sandbox. |
308 if (!IsSandboxed()) { | 306 if (!IsSandboxed()) { |
309 LOG(ERROR) << "NaCl helper process running without a sandbox!\n" | 307 LOG(ERROR) << "NaCl helper process running without a sandbox!\n" |
310 << "Most likely you need to configure your SUID sandbox " | 308 << "Most likely you need to configure your SUID sandbox " |
311 << "correctly"; | 309 << "correctly"; |
312 } | 310 } |
313 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { | 311 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { |
314 // EOF from the browser. Goodbye! | 312 // EOF from the browser. Goodbye! |
315 _exit(0); | 313 _exit(0); |
316 } | 314 } |
317 if (msglen < 0) { | 315 if (msglen < 0) { |
318 PLOG(ERROR) << "nacl_helper: receive from zygote failed"; | 316 PLOG(ERROR) << "nacl_helper: receive from zygote failed"; |
319 return false; | 317 return false; |
320 } | 318 } |
321 | 319 |
322 Pickle read_pickle(buf, msglen); | 320 Pickle read_pickle(buf, msglen); |
323 PickleIterator read_iter(read_pickle); | 321 PickleIterator read_iter(read_pickle); |
324 int command_type; | 322 int command_type; |
325 if (!read_iter.ReadInt(&command_type)) { | 323 if (!read_iter.ReadInt(&command_type)) { |
326 LOG(ERROR) << "Unable to read command from Zygote"; | 324 LOG(ERROR) << "Unable to read command from Zygote"; |
327 return false; | 325 return false; |
328 } | 326 } |
329 return HonorRequestAndReply(zygote_ipc_fd, command_type, fds, system_info, | 327 return HonorRequestAndReply( |
330 &read_iter); | 328 zygote_ipc_fd, command_type, fds.Pass(), system_info, &read_iter); |
331 } | 329 } |
332 | 330 |
333 static const char kNaClHelperReservedAtZero[] = "reserved_at_zero"; | 331 static const char kNaClHelperReservedAtZero[] = "reserved_at_zero"; |
334 static const char kNaClHelperRDebug[] = "r_debug"; | 332 static const char kNaClHelperRDebug[] = "r_debug"; |
335 | 333 |
336 // Since we were started by nacl_helper_bootstrap rather than in the | 334 // Since we were started by nacl_helper_bootstrap rather than in the |
337 // usual way, the debugger cannot figure out where our executable | 335 // usual way, the debugger cannot figure out where our executable |
338 // or the dynamic linker or the shared libraries are in memory, | 336 // or the dynamic linker or the shared libraries are in memory, |
339 // so it won't find any symbols. But we can fake it out to find us. | 337 // so it won't find any symbols. But we can fake it out to find us. |
340 // | 338 // |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 // Now handle requests from the Zygote. | 461 // Now handle requests from the Zygote. |
464 while (true) { | 462 while (true) { |
465 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, | 463 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, |
466 system_info); | 464 system_info); |
467 // Do not turn this into a CHECK() without thinking about robustness | 465 // Do not turn this into a CHECK() without thinking about robustness |
468 // against malicious IPC requests. | 466 // against malicious IPC requests. |
469 DCHECK(request_handled); | 467 DCHECK(request_handled); |
470 } | 468 } |
471 NOTREACHED(); | 469 NOTREACHED(); |
472 } | 470 } |
OLD | NEW |