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 (!file_util::ReadFromFD(pipe_fds
[0],
338 reinterpret_cast<char*>(&real_pid
),
340 LOG(FATAL
) << "Failed to synchronise with parent zygote process";
343 LOG(FATAL
) << "Invalid pid from parent zygote";
345 #if defined(OS_LINUX)
346 // Sandboxed processes need to send the global, non-namespaced PID when
347 // setting up an IPC channel to their parent.
348 IPC::Channel::SetGlobalPid(real_pid
);
349 // Force the real PID so chrome event data have a PID that corresponds
350 // to system trace event data.
351 base::debug::TraceLog::GetInstance()->SetProcessID(
352 static_cast<int>(real_pid
));
358 // In the parent process.
363 base::ProcessId real_pid
;
364 if (UsingSUIDSandbox()) {
365 uint8_t reply_buf
[512];
367 request
.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE
);
368 request
.WriteUInt64(dummy_inode
);
370 const ssize_t r
= UnixDomainSocket::SendRecvMsg(
371 GetSandboxFD(), reply_buf
, sizeof(reply_buf
), NULL
,
374 LOG(ERROR
) << "Failed to get child process's real PID";
378 Pickle
reply(reinterpret_cast<char*>(reply_buf
), r
);
379 PickleIterator
iter(reply
);
380 if (!reply
.ReadInt(&iter
, &real_pid
))
383 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
384 LOG(ERROR
) << "METHOD_GET_CHILD_WITH_INODE failed";
388 // If no SUID sandbox is involved then no pid translation is
393 // Now set-up this process to be tracked by the Zygote.
394 if (process_info_map_
.find(real_pid
) != process_info_map_
.end()) {
395 LOG(ERROR
) << "Already tracking PID " << real_pid
;
398 process_info_map_
[real_pid
].internal_pid
= pid
;
399 process_info_map_
[real_pid
].started_from_helper
= use_helper
;
402 if (!helper_
->AckChild(pipe_fds
[1], channel_switch
)) {
403 LOG(ERROR
) << "Failed to synchronise with zygote fork helper";
408 HANDLE_EINTR(write(pipe_fds
[1], &real_pid
, sizeof(real_pid
)));
409 if (written
!= sizeof(real_pid
)) {
410 LOG(ERROR
) << "Failed to synchronise with child process";
420 if (waitpid(pid
, NULL
, WNOHANG
) == -1)
421 LOG(ERROR
) << "Failed to wait for process";
425 if (pipe_fds
[0] >= 0)
427 if (pipe_fds
[1] >= 0)
432 base::ProcessId
Zygote::ReadArgsAndFork(const Pickle
& pickle
,
434 std::vector
<int>& fds
,
435 std::string
* uma_name
,
437 int* uma_boundary_value
) {
438 std::vector
<std::string
> args
;
441 base::GlobalDescriptors::Mapping mapping
;
442 std::string process_type
;
443 std::string channel_id
;
444 const std::string channel_id_prefix
= std::string("--")
445 + switches::kProcessChannelID
+ std::string("=");
447 if (!pickle
.ReadString(&iter
, &process_type
))
449 if (!pickle
.ReadInt(&iter
, &argc
))
452 for (int i
= 0; i
< argc
; ++i
) {
454 if (!pickle
.ReadString(&iter
, &arg
))
457 if (arg
.compare(0, channel_id_prefix
.length(), channel_id_prefix
) == 0)
461 if (!pickle
.ReadInt(&iter
, &numfds
))
463 if (numfds
!= static_cast<int>(fds
.size()))
466 for (int i
= 0; i
< numfds
; ++i
) {
467 base::GlobalDescriptors::Key key
;
468 if (!pickle
.ReadUInt32(&iter
, &key
))
470 mapping
.push_back(std::make_pair(key
, fds
[i
]));
473 mapping
.push_back(std::make_pair(
474 static_cast<uint32_t>(kSandboxIPCChannel
), GetSandboxFD()));
476 // Returns twice, once per process.
477 base::ProcessId child_pid
= ForkWithRealPid(process_type
, mapping
, channel_id
,
478 uma_name
, uma_sample
,
481 // This is the child process.
483 close(kZygoteSocketPairFd
); // Our socket from the browser.
484 if (UsingSUIDSandbox())
485 close(kZygoteIdFd
); // Another socket from the browser.
486 base::GlobalDescriptors::GetInstance()->Reset(mapping
);
488 // Reset the process-wide command line to our new command line.
489 CommandLine::Reset();
490 CommandLine::Init(0, NULL
);
491 CommandLine::ForCurrentProcess()->InitFromArgv(args
);
493 // Update the process title. The argv was already cached by the call to
494 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
495 // (we don't have the original argv at this point).
496 SetProcessTitleFromCommandLine(NULL
);
497 } else if (child_pid
< 0) {
498 LOG(ERROR
) << "Zygote could not fork: process_type " << process_type
499 << " numfds " << numfds
<< " child_pid " << child_pid
;
504 bool Zygote::HandleForkRequest(int fd
,
505 const Pickle
& pickle
,
507 std::vector
<int>& fds
) {
508 std::string uma_name
;
510 int uma_boundary_value
;
511 base::ProcessId child_pid
= ReadArgsAndFork(pickle
, iter
, fds
,
512 &uma_name
, &uma_sample
,
513 &uma_boundary_value
);
516 for (std::vector
<int>::const_iterator
517 i
= fds
.begin(); i
!= fds
.end(); ++i
)
519 if (uma_name
.empty()) {
520 // There is no UMA report from this particular fork.
521 // Use the initial UMA report if any, and clear that record for next time.
522 // Note the swap method here is the efficient way to do this, since
523 // we know uma_name is empty.
524 uma_name
.swap(initial_uma_name_
);
525 uma_sample
= initial_uma_sample_
;
526 uma_boundary_value
= initial_uma_boundary_value_
;
528 // Must always send reply, as ZygoteHost blocks while waiting for it.
530 reply_pickle
.WriteInt(child_pid
);
531 reply_pickle
.WriteString(uma_name
);
532 if (!uma_name
.empty()) {
533 reply_pickle
.WriteInt(uma_sample
);
534 reply_pickle
.WriteInt(uma_boundary_value
);
536 if (HANDLE_EINTR(write(fd
, reply_pickle
.data(), reply_pickle
.size())) !=
537 static_cast<ssize_t
> (reply_pickle
.size()))
538 PLOG(ERROR
) << "write";
542 bool Zygote::HandleGetSandboxStatus(int fd
,
543 const Pickle
& pickle
,
544 PickleIterator iter
) {
545 if (HANDLE_EINTR(write(fd
, &sandbox_flags_
, sizeof(sandbox_flags_
))) !=
546 sizeof(sandbox_flags_
)) {
547 PLOG(ERROR
) << "write";
553 } // namespace content