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.
5 #include "content/zygote/zygote_linux.h"
9 #include <sys/socket.h>
10 #include <sys/types.h>
13 #include "base/command_line.h"
14 #include "base/debug/trace_event.h"
15 #include "base/file_util.h"
16 #include "base/linux_util.h"
17 #include "base/logging.h"
18 #include "base/pickle.h"
19 #include "base/posix/eintr_wrapper.h"
20 #include "base/posix/global_descriptors.h"
21 #include "base/posix/unix_domain_socket_linux.h"
22 #include "base/process/kill.h"
23 #include "content/common/child_process_sandbox_support_impl_linux.h"
24 #include "content/common/sandbox_linux.h"
25 #include "content/common/set_process_title.h"
26 #include "content/common/zygote_commands_linux.h"
27 #include "content/public/common/content_descriptors.h"
28 #include "content/public/common/result_codes.h"
29 #include "content/public/common/sandbox_linux.h"
30 #include "content/public/common/zygote_fork_delegate_linux.h"
31 #include "ipc/ipc_channel.h"
32 #include "ipc/ipc_switches.h"
34 // See http://code.google.com/p/chromium/wiki/LinuxZygote
40 // NOP function. See below where this handler is installed.
41 void SIGCHLDHandler(int signal
) {
44 int LookUpFd(const base::GlobalDescriptors::Mapping
& fd_mapping
, uint32_t key
) {
45 for (size_t index
= 0; index
< fd_mapping
.size(); ++index
) {
46 if (fd_mapping
[index
].first
== key
)
47 return fd_mapping
[index
].second
;
54 Zygote::Zygote(int sandbox_flags
,
55 ZygoteForkDelegate
* helper
)
56 : sandbox_flags_(sandbox_flags
),
58 initial_uma_sample_(0),
59 initial_uma_boundary_value_(0) {
61 helper_
->InitialUMA(&initial_uma_name_
,
63 &initial_uma_boundary_value_
);
70 bool Zygote::ProcessRequests() {
71 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
73 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
74 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
76 // We need to accept SIGCHLD, even though our handler is a no-op because
77 // otherwise we cannot wait on children. (According to POSIX 2001.)
78 struct sigaction action
;
79 memset(&action
, 0, sizeof(action
));
80 action
.sa_handler
= &SIGCHLDHandler
;
81 CHECK(sigaction(SIGCHLD
, &action
, NULL
) == 0);
83 if (UsingSUIDSandbox()) {
84 // Let the ZygoteHost know we are ready to go.
85 // The receiving code is in content/browser/zygote_host_linux.cc.
86 std::vector
<int> empty
;
87 bool r
= UnixDomainSocket::SendMsg(kZygoteSocketPairFd
,
89 sizeof(kZygoteHelloMessage
), empty
);
90 #if defined(OS_CHROMEOS)
91 LOG_IF(WARNING
, !r
) << "Sending zygote magic failed";
92 // Exit normally on chromeos because session manager may send SIGTERM
93 // right after the process starts and it may fail to send zygote magic
94 // number to browser process.
96 _exit(RESULT_CODE_NORMAL_EXIT
);
98 CHECK(r
) << "Sending zygote magic failed";
103 // This function call can return multiple times, once per fork().
104 if (HandleRequestFromBrowser(kZygoteSocketPairFd
))
109 bool Zygote::GetProcessInfo(base::ProcessHandle pid
,
110 ZygoteProcessInfo
* process_info
) {
111 DCHECK(process_info
);
112 const ZygoteProcessMap::const_iterator it
= process_info_map_
.find(pid
);
113 if (it
== process_info_map_
.end()) {
116 *process_info
= it
->second
;
120 bool Zygote::UsingSUIDSandbox() const {
121 return sandbox_flags_
& kSandboxLinuxSUID
;
124 bool Zygote::HandleRequestFromBrowser(int fd
) {
125 std::vector
<int> fds
;
126 char buf
[kZygoteMaxMessageLength
];
127 const ssize_t len
= UnixDomainSocket::RecvMsg(fd
, buf
, sizeof(buf
), &fds
);
129 if (len
== 0 || (len
== -1 && errno
== ECONNRESET
)) {
130 // EOF from the browser. We should die.
136 PLOG(ERROR
) << "Error reading message from browser";
140 Pickle
pickle(buf
, len
);
141 PickleIterator
iter(pickle
);
144 if (pickle
.ReadInt(&iter
, &kind
)) {
146 case kZygoteCommandFork
:
147 // This function call can return multiple times, once per fork().
148 return HandleForkRequest(fd
, pickle
, iter
, fds
);
150 case kZygoteCommandReap
:
153 HandleReapRequest(fd
, pickle
, iter
);
155 case kZygoteCommandGetTerminationStatus
:
158 HandleGetTerminationStatus(fd
, pickle
, iter
);
160 case kZygoteCommandGetSandboxStatus
:
161 HandleGetSandboxStatus(fd
, pickle
, iter
);
169 LOG(WARNING
) << "Error parsing message from browser";
170 for (std::vector
<int>::const_iterator
171 i
= fds
.begin(); i
!= fds
.end(); ++i
)
176 // TODO(jln): remove callers to this broken API. See crbug.com/274855.
177 void Zygote::HandleReapRequest(int fd
,
178 const Pickle
& pickle
,
179 PickleIterator iter
) {
180 base::ProcessId child
;
182 if (!pickle
.ReadInt(&iter
, &child
)) {
183 LOG(WARNING
) << "Error parsing reap request from browser";
187 ZygoteProcessInfo child_info
;
188 if (!GetProcessInfo(child
, &child_info
)) {
189 LOG(ERROR
) << "Child not found!";
194 if (!child_info
.started_from_helper
) {
195 // TODO(jln): this old code is completely broken. See crbug.com/274855.
196 base::EnsureProcessTerminated(child_info
.internal_pid
);
198 // For processes from the helper, send a GetTerminationStatus request
199 // with known_dead set to true.
200 // This is not perfect, as the process may be killed instantly, but is
201 // better than ignoring the request.
202 base::TerminationStatus status
;
204 bool got_termination_status
=
205 GetTerminationStatus(child
, true /* known_dead */, &status
, &exit_code
);
206 DCHECK(got_termination_status
);
208 process_info_map_
.erase(child
);
211 bool Zygote::GetTerminationStatus(base::ProcessHandle real_pid
,
213 base::TerminationStatus
* status
,
216 ZygoteProcessInfo child_info
;
217 if (!GetProcessInfo(real_pid
, &child_info
)) {
218 LOG(ERROR
) << "Zygote::GetTerminationStatus for unknown PID "
223 // We know about |real_pid|.
224 const base::ProcessHandle child
= child_info
.internal_pid
;
225 if (child_info
.started_from_helper
) {
226 // Let the helper handle the request.
228 if (!helper_
->GetTerminationStatus(child
, known_dead
, status
, exit_code
)) {
232 // Handle the request directly.
234 *status
= base::GetKnownDeadTerminationStatus(child
, exit_code
);
236 // We don't know if the process is dying, so get its status but don't
238 *status
= base::GetTerminationStatus(child
, exit_code
);
241 // Successfully got a status for |real_pid|.
242 if (*status
!= base::TERMINATION_STATUS_STILL_RUNNING
) {
243 // Time to forget about this process.
244 process_info_map_
.erase(real_pid
);
249 void Zygote::HandleGetTerminationStatus(int fd
,
250 const Pickle
& pickle
,
251 PickleIterator iter
) {
253 base::ProcessHandle child_requested
;
255 if (!pickle
.ReadBool(&iter
, &known_dead
) ||
256 !pickle
.ReadInt(&iter
, &child_requested
)) {
257 LOG(WARNING
) << "Error parsing GetTerminationStatus request "
262 base::TerminationStatus status
;
265 bool got_termination_status
=
266 GetTerminationStatus(child_requested
, known_dead
, &status
, &exit_code
);
267 if (!got_termination_status
) {
268 // Assume that if we can't find the child in the sandbox, then
269 // it terminated normally.
271 status
= base::TERMINATION_STATUS_NORMAL_TERMINATION
;
272 exit_code
= RESULT_CODE_NORMAL_EXIT
;
276 write_pickle
.WriteInt(static_cast<int>(status
));
277 write_pickle
.WriteInt(exit_code
);
279 HANDLE_EINTR(write(fd
, write_pickle
.data(), write_pickle
.size()));
280 if (written
!= static_cast<ssize_t
>(write_pickle
.size()))
281 PLOG(ERROR
) << "write";
284 int Zygote::ForkWithRealPid(const std::string
& process_type
,
285 const base::GlobalDescriptors::Mapping
& fd_mapping
,
286 const std::string
& channel_switch
,
287 std::string
* uma_name
,
289 int* uma_boundary_value
) {
290 const bool use_helper
= (helper_
&& helper_
->CanHelp(process_type
,
293 uma_boundary_value
));
296 int pipe_fds
[2] = { -1, -1 };
297 base::ProcessId pid
= 0;
299 dummy_fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
301 LOG(ERROR
) << "Failed to create dummy FD";
304 if (!base::FileDescriptorGetInode(&dummy_inode
, dummy_fd
)) {
305 LOG(ERROR
) << "Failed to get inode for dummy FD";
308 if (pipe(pipe_fds
) != 0) {
309 LOG(ERROR
) << "Failed to create pipe";
314 std::vector
<int> fds
;
315 int ipc_channel_fd
= LookUpFd(fd_mapping
, kPrimaryIPCChannel
);
316 if (ipc_channel_fd
< 0) {
317 DLOG(ERROR
) << "Failed to find kPrimaryIPCChannel in FD mapping";
320 fds
.push_back(ipc_channel_fd
); // kBrowserFDIndex
321 fds
.push_back(dummy_fd
); // kDummyFDIndex
322 fds
.push_back(pipe_fds
[0]); // kParentFDIndex
323 pid
= helper_
->Fork(fds
);
329 } else if (pid
== 0) {
330 // In the child process.
332 base::ProcessId real_pid
;
333 // Wait until the parent process has discovered our PID. We
334 // should not fork any child processes (which the seccomp
335 // sandbox does) until then, because that can interfere with the
336 // parent's discovery of our PID.
337 if (!base::ReadFromFD(pipe_fds
[0], reinterpret_cast<char*>(&real_pid
),
339 LOG(FATAL
) << "Failed to synchronise with parent zygote process";
342 LOG(FATAL
) << "Invalid pid from parent zygote";
344 #if defined(OS_LINUX)
345 // Sandboxed processes need to send the global, non-namespaced PID when
346 // setting up an IPC channel to their parent.
347 IPC::Channel::SetGlobalPid(real_pid
);
348 // Force the real PID so chrome event data have a PID that corresponds
349 // to system trace event data.
350 base::debug::TraceLog::GetInstance()->SetProcessID(
351 static_cast<int>(real_pid
));
357 // In the parent process.
362 base::ProcessId real_pid
;
363 if (UsingSUIDSandbox()) {
364 uint8_t reply_buf
[512];
366 request
.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE
);
367 request
.WriteUInt64(dummy_inode
);
369 const ssize_t r
= UnixDomainSocket::SendRecvMsg(
370 GetSandboxFD(), reply_buf
, sizeof(reply_buf
), NULL
,
373 LOG(ERROR
) << "Failed to get child process's real PID";
377 Pickle
reply(reinterpret_cast<char*>(reply_buf
), r
);
378 PickleIterator
iter(reply
);
379 if (!reply
.ReadInt(&iter
, &real_pid
))
382 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
383 LOG(ERROR
) << "METHOD_GET_CHILD_WITH_INODE failed";
387 // If no SUID sandbox is involved then no pid translation is
392 // Now set-up this process to be tracked by the Zygote.
393 if (process_info_map_
.find(real_pid
) != process_info_map_
.end()) {
394 LOG(ERROR
) << "Already tracking PID " << real_pid
;
397 process_info_map_
[real_pid
].internal_pid
= pid
;
398 process_info_map_
[real_pid
].started_from_helper
= use_helper
;
401 if (!helper_
->AckChild(pipe_fds
[1], channel_switch
)) {
402 LOG(ERROR
) << "Failed to synchronise with zygote fork helper";
407 HANDLE_EINTR(write(pipe_fds
[1], &real_pid
, sizeof(real_pid
)));
408 if (written
!= sizeof(real_pid
)) {
409 LOG(ERROR
) << "Failed to synchronise with child process";
419 if (waitpid(pid
, NULL
, WNOHANG
) == -1)
420 LOG(ERROR
) << "Failed to wait for process";
424 if (pipe_fds
[0] >= 0)
426 if (pipe_fds
[1] >= 0)
431 base::ProcessId
Zygote::ReadArgsAndFork(const Pickle
& pickle
,
433 std::vector
<int>& fds
,
434 std::string
* uma_name
,
436 int* uma_boundary_value
) {
437 std::vector
<std::string
> args
;
440 base::GlobalDescriptors::Mapping mapping
;
441 std::string process_type
;
442 std::string channel_id
;
443 const std::string channel_id_prefix
= std::string("--")
444 + switches::kProcessChannelID
+ std::string("=");
446 if (!pickle
.ReadString(&iter
, &process_type
))
448 if (!pickle
.ReadInt(&iter
, &argc
))
451 for (int i
= 0; i
< argc
; ++i
) {
453 if (!pickle
.ReadString(&iter
, &arg
))
456 if (arg
.compare(0, channel_id_prefix
.length(), channel_id_prefix
) == 0)
460 if (!pickle
.ReadInt(&iter
, &numfds
))
462 if (numfds
!= static_cast<int>(fds
.size()))
465 for (int i
= 0; i
< numfds
; ++i
) {
466 base::GlobalDescriptors::Key key
;
467 if (!pickle
.ReadUInt32(&iter
, &key
))
469 mapping
.push_back(std::make_pair(key
, fds
[i
]));
472 mapping
.push_back(std::make_pair(
473 static_cast<uint32_t>(kSandboxIPCChannel
), GetSandboxFD()));
475 // Returns twice, once per process.
476 base::ProcessId child_pid
= ForkWithRealPid(process_type
, mapping
, channel_id
,
477 uma_name
, uma_sample
,
480 // This is the child process.
482 close(kZygoteSocketPairFd
); // Our socket from the browser.
483 if (UsingSUIDSandbox())
484 close(kZygoteIdFd
); // Another socket from the browser.
485 base::GlobalDescriptors::GetInstance()->Reset(mapping
);
487 // Reset the process-wide command line to our new command line.
488 CommandLine::Reset();
489 CommandLine::Init(0, NULL
);
490 CommandLine::ForCurrentProcess()->InitFromArgv(args
);
492 // Update the process title. The argv was already cached by the call to
493 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
494 // (we don't have the original argv at this point).
495 SetProcessTitleFromCommandLine(NULL
);
496 } else if (child_pid
< 0) {
497 LOG(ERROR
) << "Zygote could not fork: process_type " << process_type
498 << " numfds " << numfds
<< " child_pid " << child_pid
;
503 bool Zygote::HandleForkRequest(int fd
,
504 const Pickle
& pickle
,
506 std::vector
<int>& fds
) {
507 std::string uma_name
;
509 int uma_boundary_value
;
510 base::ProcessId child_pid
= ReadArgsAndFork(pickle
, iter
, fds
,
511 &uma_name
, &uma_sample
,
512 &uma_boundary_value
);
515 for (std::vector
<int>::const_iterator
516 i
= fds
.begin(); i
!= fds
.end(); ++i
)
518 if (uma_name
.empty()) {
519 // There is no UMA report from this particular fork.
520 // Use the initial UMA report if any, and clear that record for next time.
521 // Note the swap method here is the efficient way to do this, since
522 // we know uma_name is empty.
523 uma_name
.swap(initial_uma_name_
);
524 uma_sample
= initial_uma_sample_
;
525 uma_boundary_value
= initial_uma_boundary_value_
;
527 // Must always send reply, as ZygoteHost blocks while waiting for it.
529 reply_pickle
.WriteInt(child_pid
);
530 reply_pickle
.WriteString(uma_name
);
531 if (!uma_name
.empty()) {
532 reply_pickle
.WriteInt(uma_sample
);
533 reply_pickle
.WriteInt(uma_boundary_value
);
535 if (HANDLE_EINTR(write(fd
, reply_pickle
.data(), reply_pickle
.size())) !=
536 static_cast<ssize_t
> (reply_pickle
.size()))
537 PLOG(ERROR
) << "write";
541 bool Zygote::HandleGetSandboxStatus(int fd
,
542 const Pickle
& pickle
,
543 PickleIterator iter
) {
544 if (HANDLE_EINTR(write(fd
, &sandbox_flags_
, sizeof(sandbox_flags_
))) !=
545 sizeof(sandbox_flags_
)) {
546 PLOG(ERROR
) << "write";
552 } // namespace content