OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/browser/zygote_host/zygote_host_impl_linux.h" | 5 #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
6 | 6 |
| 7 #include <errno.h> |
| 8 #include <string.h> |
| 9 #include <sys/socket.h> |
| 10 #include <sys/stat.h> |
| 11 #include <sys/types.h> |
| 12 #include <unistd.h> |
| 13 |
| 14 #include "base/base_switches.h" |
| 15 #include "base/command_line.h" |
| 16 #include "base/environment.h" |
7 #include "base/files/file_enumerator.h" | 17 #include "base/files/file_enumerator.h" |
8 #include "base/process/kill.h" | 18 #include "base/files/file_util.h" |
| 19 #include "base/files/scoped_file.h" |
| 20 #include "base/linux_util.h" |
| 21 #include "base/logging.h" |
| 22 #include "base/macros.h" |
| 23 #include "base/memory/linked_ptr.h" |
| 24 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/metrics/histogram.h" |
| 26 #include "base/metrics/sparse_histogram.h" |
| 27 #include "base/path_service.h" |
| 28 #include "base/posix/eintr_wrapper.h" |
| 29 #include "base/posix/unix_domain_socket_linux.h" |
| 30 #include "base/process/launch.h" |
9 #include "base/process/memory.h" | 31 #include "base/process/memory.h" |
| 32 #include "base/process/process_handle.h" |
10 #include "base/strings/string_number_conversions.h" | 33 #include "base/strings/string_number_conversions.h" |
| 34 #include "base/strings/string_util.h" |
| 35 #include "base/strings/utf_string_conversions.h" |
| 36 #include "base/time/time.h" |
| 37 #include "build/build_config.h" |
| 38 #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
| 39 #include "content/common/child_process_sandbox_support_impl_linux.h" |
| 40 #include "content/common/zygote_commands_linux.h" |
11 #include "content/public/browser/content_browser_client.h" | 41 #include "content/public/browser/content_browser_client.h" |
| 42 #include "content/public/common/content_switches.h" |
| 43 #include "content/public/common/result_codes.h" |
| 44 #include "sandbox/linux/services/credentials.h" |
| 45 #include "sandbox/linux/services/namespace_sandbox.h" |
| 46 #include "sandbox/linux/services/namespace_utils.h" |
| 47 #include "sandbox/linux/suid/client/setuid_sandbox_host.h" |
12 #include "sandbox/linux/suid/common/sandbox.h" | 48 #include "sandbox/linux/suid/common/sandbox.h" |
| 49 #include "ui/base/ui_base_switches.h" |
| 50 #include "ui/gfx/switches.h" |
13 | 51 |
14 #if defined(USE_TCMALLOC) | 52 #if defined(USE_TCMALLOC) |
15 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" | 53 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
16 #endif | 54 #endif |
17 | 55 |
18 namespace content { | 56 namespace content { |
19 | 57 |
| 58 namespace { |
| 59 |
| 60 // Receive a fixed message on fd and return the sender's PID. |
| 61 // Returns true if the message received matches the expected message. |
| 62 bool ReceiveFixedMessage(int fd, |
| 63 const char* expect_msg, |
| 64 size_t expect_len, |
| 65 base::ProcessId* sender_pid) { |
| 66 char buf[expect_len + 1]; |
| 67 std::vector<base::ScopedFD> fds_vec; |
| 68 |
| 69 const ssize_t len = base::UnixDomainSocket::RecvMsgWithPid( |
| 70 fd, buf, sizeof(buf), &fds_vec, sender_pid); |
| 71 if (static_cast<size_t>(len) != expect_len) |
| 72 return false; |
| 73 if (memcmp(buf, expect_msg, expect_len) != 0) |
| 74 return false; |
| 75 if (!fds_vec.empty()) |
| 76 return false; |
| 77 return true; |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
20 // static | 82 // static |
21 ZygoteHost* ZygoteHost::GetInstance() { | 83 ZygoteHost* ZygoteHost::GetInstance() { |
22 return ZygoteHostImpl::GetInstance(); | 84 return ZygoteHostImpl::GetInstance(); |
23 } | 85 } |
24 | 86 |
25 ZygoteHostImpl::ZygoteHostImpl() | 87 ZygoteHostImpl::ZygoteHostImpl() |
26 : use_suid_sandbox_for_adj_oom_score_(false), | 88 : control_fd_(-1), |
| 89 control_lock_(), |
| 90 pid_(-1), |
| 91 init_(false), |
| 92 use_suid_sandbox_for_adj_oom_score_(false), |
27 sandbox_binary_(), | 93 sandbox_binary_(), |
28 zygote_pids_lock_(), | 94 have_read_sandbox_status_word_(false), |
29 zygote_pids_() {} | 95 sandbox_status_(0), |
| 96 child_tracking_lock_(), |
| 97 list_of_running_zygote_children_(), |
| 98 should_teardown_after_last_child_exits_(false) {} |
30 | 99 |
31 ZygoteHostImpl::~ZygoteHostImpl() {} | 100 ZygoteHostImpl::~ZygoteHostImpl() { TearDown(); } |
32 | 101 |
33 // static | 102 // static |
34 ZygoteHostImpl* ZygoteHostImpl::GetInstance() { | 103 ZygoteHostImpl* ZygoteHostImpl::GetInstance() { |
35 return base::Singleton<ZygoteHostImpl>::get(); | 104 return base::Singleton<ZygoteHostImpl>::get(); |
36 } | 105 } |
37 | 106 |
38 void ZygoteHostImpl::Init(const std::string& sandbox_cmd) { | 107 void ZygoteHostImpl::Init(const std::string& sandbox_cmd) { |
39 sandbox_binary_ = sandbox_cmd; | 108 DCHECK(!init_); |
40 } | 109 init_ = true; |
41 | 110 |
42 void ZygoteHostImpl::AddZygotePid(pid_t pid) { | 111 base::FilePath chrome_path; |
43 base::AutoLock lock(zygote_pids_lock_); | 112 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); |
44 zygote_pids_.insert(pid); | 113 base::CommandLine cmd_line(chrome_path); |
45 } | 114 |
46 | 115 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kZygoteProcess); |
47 bool ZygoteHostImpl::IsZygotePid(pid_t pid) { | 116 |
48 base::AutoLock lock(zygote_pids_lock_); | 117 int fds[2]; |
49 return zygote_pids_.find(pid) != zygote_pids_.end(); | 118 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); |
50 } | 119 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(fds[0])); |
51 | 120 base::FileHandleMappingVector fds_to_map; |
52 const std::string& ZygoteHostImpl::SandboxCommand() const { | 121 fds_to_map.push_back(std::make_pair(fds[1], kZygoteSocketPairFd)); |
53 return sandbox_binary_; | 122 |
54 } | 123 base::LaunchOptions options; |
55 | 124 const base::CommandLine& browser_command_line = |
56 void ZygoteHostImpl::SetRendererSandboxStatus(int status) { | 125 *base::CommandLine::ForCurrentProcess(); |
57 renderer_sandbox_status_ = status; | 126 if (browser_command_line.HasSwitch(switches::kZygoteCmdPrefix)) { |
58 } | 127 cmd_line.PrependWrapper( |
59 | 128 browser_command_line.GetSwitchValueNative(switches::kZygoteCmdPrefix)); |
60 int ZygoteHostImpl::GetRendererSandboxStatus() const { | 129 } |
61 return renderer_sandbox_status_; | 130 // Append any switches from the browser process that need to be forwarded on |
62 } | 131 // to the zygote/renderers. |
| 132 // Should this list be obtained from browser_render_process_host.cc? |
| 133 static const char* kForwardSwitches[] = { |
| 134 switches::kAllowSandboxDebugging, |
| 135 switches::kDisableSeccompFilterSandbox, |
| 136 switches::kEnableHeapProfiling, |
| 137 switches::kEnableLogging, // Support, e.g., --enable-logging=stderr. |
| 138 // Zygote process needs to know what resources to have loaded when it |
| 139 // becomes a renderer process. |
| 140 switches::kForceDeviceScaleFactor, |
| 141 switches::kLoggingLevel, |
| 142 switches::kNoSandbox, |
| 143 switches::kPpapiInProcess, |
| 144 switches::kRegisterPepperPlugins, |
| 145 switches::kV, |
| 146 switches::kVModule, |
| 147 }; |
| 148 cmd_line.CopySwitchesFrom(browser_command_line, kForwardSwitches, |
| 149 arraysize(kForwardSwitches)); |
| 150 |
| 151 GetContentClient()->browser()->AppendExtraCommandLineSwitches(&cmd_line, -1); |
| 152 |
| 153 sandbox_binary_ = sandbox_cmd.c_str(); |
| 154 |
| 155 const bool using_namespace_sandbox = ShouldUseNamespaceSandbox(); |
| 156 // A non empty sandbox_cmd means we want a SUID sandbox. |
| 157 const bool using_suid_sandbox = |
| 158 !sandbox_cmd.empty() && !using_namespace_sandbox; |
| 159 |
| 160 // Use the SUID sandbox for adjusting OOM scores when we are using the setuid |
| 161 // or namespace sandbox. This is needed beacuse the processes are |
| 162 // non-dumpable, so /proc/pid/oom_score_adj can only be written by root. |
| 163 use_suid_sandbox_for_adj_oom_score_ = |
| 164 !sandbox_binary_.empty() && using_suid_sandbox; |
| 165 |
| 166 // Start up the sandbox host process and get the file descriptor for the |
| 167 // renderers to talk to it. |
| 168 const int sfd = RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); |
| 169 fds_to_map.push_back(std::make_pair(sfd, GetSandboxFD())); |
| 170 |
| 171 base::ScopedFD dummy_fd; |
| 172 if (using_suid_sandbox) { |
| 173 scoped_ptr<sandbox::SetuidSandboxHost> sandbox_host( |
| 174 sandbox::SetuidSandboxHost::Create()); |
| 175 sandbox_host->PrependWrapper(&cmd_line); |
| 176 sandbox_host->SetupLaunchOptions(&options, &fds_to_map, &dummy_fd); |
| 177 sandbox_host->SetupLaunchEnvironment(); |
| 178 } |
| 179 |
| 180 options.fds_to_remap = &fds_to_map; |
| 181 base::Process process = |
| 182 using_namespace_sandbox |
| 183 ? sandbox::NamespaceSandbox::LaunchProcess(cmd_line, options) |
| 184 : base::LaunchProcess(cmd_line, options); |
| 185 CHECK(process.IsValid()) << "Failed to launch zygote process"; |
| 186 |
| 187 dummy_fd.reset(); |
| 188 |
| 189 if (using_suid_sandbox || using_namespace_sandbox) { |
| 190 // The SUID sandbox will execute the zygote in a new PID namespace, and |
| 191 // the main zygote process will then fork from there. Watch now our |
| 192 // elaborate dance to find and validate the zygote's PID. |
| 193 |
| 194 // First we receive a message from the zygote boot process. |
| 195 base::ProcessId boot_pid; |
| 196 CHECK(ReceiveFixedMessage( |
| 197 fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid)); |
| 198 |
| 199 // Within the PID namespace, the zygote boot process thinks it's PID 1, |
| 200 // but its real PID can never be 1. This gives us a reliable test that |
| 201 // the kernel is translating the sender's PID to our namespace. |
| 202 CHECK_GT(boot_pid, 1) |
| 203 << "Received invalid process ID for zygote; kernel might be too old? " |
| 204 "See crbug.com/357670 or try using --" |
| 205 << switches::kDisableSetuidSandbox << " to workaround."; |
| 206 |
| 207 // Now receive the message that the zygote's ready to go, along with the |
| 208 // main zygote process's ID. |
| 209 CHECK(ReceiveFixedMessage( |
| 210 fds[0], kZygoteHelloMessage, sizeof(kZygoteHelloMessage), &pid_)); |
| 211 CHECK_GT(pid_, 1); |
| 212 |
| 213 if (process.Pid() != pid_) { |
| 214 // Reap the sandbox. |
| 215 base::EnsureProcessGetsReaped(process.Pid()); |
| 216 } |
| 217 } else { |
| 218 // Not using the SUID sandbox. |
| 219 // Note that ~base::Process() will reset the internal value, but there's no |
| 220 // real "handle" on POSIX so that is safe. |
| 221 pid_ = process.Pid(); |
| 222 } |
| 223 |
| 224 close(fds[1]); |
| 225 control_fd_ = fds[0]; |
| 226 |
| 227 base::Pickle pickle; |
| 228 pickle.WriteInt(kZygoteCommandGetSandboxStatus); |
| 229 if (!SendMessage(pickle, NULL)) |
| 230 LOG(FATAL) << "Cannot communicate with zygote"; |
| 231 // We don't wait for the reply. We'll read it in ReadReply. |
| 232 } |
| 233 |
| 234 void ZygoteHostImpl::TearDownAfterLastChild() { |
| 235 bool do_teardown = false; |
| 236 { |
| 237 base::AutoLock lock(child_tracking_lock_); |
| 238 should_teardown_after_last_child_exits_ = true; |
| 239 do_teardown = list_of_running_zygote_children_.empty(); |
| 240 } |
| 241 if (do_teardown) { |
| 242 TearDown(); |
| 243 } |
| 244 } |
| 245 |
| 246 // Note: this is also called from the destructor. |
| 247 void ZygoteHostImpl::TearDown() { |
| 248 base::AutoLock lock(control_lock_); |
| 249 if (control_fd_ > -1) { |
| 250 // Closing the IPC channel will act as a notification to exit |
| 251 // to the Zygote. |
| 252 if (IGNORE_EINTR(close(control_fd_))) { |
| 253 PLOG(ERROR) << "Could not close Zygote control channel."; |
| 254 NOTREACHED(); |
| 255 } |
| 256 control_fd_ = -1; |
| 257 } |
| 258 } |
| 259 |
| 260 void ZygoteHostImpl::ZygoteChildBorn(pid_t process) { |
| 261 base::AutoLock lock(child_tracking_lock_); |
| 262 bool new_element_inserted = |
| 263 list_of_running_zygote_children_.insert(process).second; |
| 264 DCHECK(new_element_inserted); |
| 265 } |
| 266 |
| 267 void ZygoteHostImpl::ZygoteChildDied(pid_t process) { |
| 268 bool do_teardown = false; |
| 269 { |
| 270 base::AutoLock lock(child_tracking_lock_); |
| 271 size_t num_erased = list_of_running_zygote_children_.erase(process); |
| 272 DCHECK_EQ(1U, num_erased); |
| 273 do_teardown = should_teardown_after_last_child_exits_ && |
| 274 list_of_running_zygote_children_.empty(); |
| 275 } |
| 276 if (do_teardown) { |
| 277 TearDown(); |
| 278 } |
| 279 } |
| 280 |
| 281 bool ZygoteHostImpl::SendMessage(const base::Pickle& data, |
| 282 const std::vector<int>* fds) { |
| 283 DCHECK_NE(-1, control_fd_); |
| 284 CHECK(data.size() <= kZygoteMaxMessageLength) |
| 285 << "Trying to send too-large message to zygote (sending " << data.size() |
| 286 << " bytes, max is " << kZygoteMaxMessageLength << ")"; |
| 287 CHECK(!fds || fds->size() <= base::UnixDomainSocket::kMaxFileDescriptors) |
| 288 << "Trying to send message with too many file descriptors to zygote " |
| 289 << "(sending " << fds->size() << ", max is " |
| 290 << base::UnixDomainSocket::kMaxFileDescriptors << ")"; |
| 291 |
| 292 return base::UnixDomainSocket::SendMsg(control_fd_, |
| 293 data.data(), data.size(), |
| 294 fds ? *fds : std::vector<int>()); |
| 295 } |
| 296 |
| 297 ssize_t ZygoteHostImpl::ReadReply(void* buf, size_t buf_len) { |
| 298 DCHECK_NE(-1, control_fd_); |
| 299 // At startup we send a kZygoteCommandGetSandboxStatus request to the zygote, |
| 300 // but don't wait for the reply. Thus, the first time that we read from the |
| 301 // zygote, we get the reply to that request. |
| 302 if (!have_read_sandbox_status_word_) { |
| 303 if (HANDLE_EINTR(read(control_fd_, &sandbox_status_, |
| 304 sizeof(sandbox_status_))) != |
| 305 sizeof(sandbox_status_)) { |
| 306 return -1; |
| 307 } |
| 308 |
| 309 have_read_sandbox_status_word_ = true; |
| 310 UMA_HISTOGRAM_SPARSE_SLOWLY("Linux.SandboxStatus", sandbox_status_); |
| 311 } |
| 312 |
| 313 return HANDLE_EINTR(read(control_fd_, buf, buf_len)); |
| 314 } |
| 315 |
| 316 pid_t ZygoteHostImpl::ForkRequest(const std::vector<std::string>& argv, |
| 317 scoped_ptr<FileDescriptorInfo> mapping, |
| 318 const std::string& process_type) { |
| 319 DCHECK(init_); |
| 320 base::Pickle pickle; |
| 321 |
| 322 int raw_socks[2]; |
| 323 PCHECK(0 == socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks)); |
| 324 base::ScopedFD my_sock(raw_socks[0]); |
| 325 base::ScopedFD peer_sock(raw_socks[1]); |
| 326 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(my_sock.get())); |
| 327 |
| 328 pickle.WriteInt(kZygoteCommandFork); |
| 329 pickle.WriteString(process_type); |
| 330 pickle.WriteInt(argv.size()); |
| 331 for (std::vector<std::string>::const_iterator |
| 332 i = argv.begin(); i != argv.end(); ++i) |
| 333 pickle.WriteString(*i); |
| 334 |
| 335 // Fork requests contain one file descriptor for the PID oracle, and one |
| 336 // more for each file descriptor mapping for the child process. |
| 337 const size_t num_fds_to_send = 1 + mapping->GetMappingSize(); |
| 338 pickle.WriteInt(num_fds_to_send); |
| 339 |
| 340 std::vector<int> fds; |
| 341 |
| 342 // First FD to send is peer_sock. |
| 343 // TODO(morrita): Ideally, this should be part of the mapping so that |
| 344 // FileDescriptorInfo can manages its lifetime. |
| 345 fds.push_back(peer_sock.get()); |
| 346 |
| 347 // The rest come from mapping. |
| 348 for (size_t i = 0; i < mapping->GetMappingSize(); ++i) { |
| 349 pickle.WriteUInt32(mapping->GetIDAt(i)); |
| 350 fds.push_back(mapping->GetFDAt(i)); |
| 351 } |
| 352 |
| 353 // Sanity check that we've populated |fds| correctly. |
| 354 DCHECK_EQ(num_fds_to_send, fds.size()); |
| 355 |
| 356 pid_t pid; |
| 357 { |
| 358 base::AutoLock lock(control_lock_); |
| 359 if (!SendMessage(pickle, &fds)) |
| 360 return base::kNullProcessHandle; |
| 361 mapping.reset(); |
| 362 peer_sock.reset(); |
| 363 |
| 364 { |
| 365 char buf[sizeof(kZygoteChildPingMessage) + 1]; |
| 366 std::vector<base::ScopedFD> recv_fds; |
| 367 base::ProcessId real_pid; |
| 368 |
| 369 ssize_t n = base::UnixDomainSocket::RecvMsgWithPid( |
| 370 my_sock.get(), buf, sizeof(buf), &recv_fds, &real_pid); |
| 371 if (n != sizeof(kZygoteChildPingMessage) || |
| 372 0 != memcmp(buf, |
| 373 kZygoteChildPingMessage, |
| 374 sizeof(kZygoteChildPingMessage))) { |
| 375 // Zygote children should still be trustworthy when they're supposed to |
| 376 // ping us, so something's broken if we don't receive a valid ping. |
| 377 LOG(ERROR) << "Did not receive ping from zygote child"; |
| 378 NOTREACHED(); |
| 379 real_pid = -1; |
| 380 } |
| 381 my_sock.reset(); |
| 382 |
| 383 // Always send PID back to zygote. |
| 384 base::Pickle pid_pickle; |
| 385 pid_pickle.WriteInt(kZygoteCommandForkRealPID); |
| 386 pid_pickle.WriteInt(real_pid); |
| 387 if (!SendMessage(pid_pickle, NULL)) |
| 388 return base::kNullProcessHandle; |
| 389 } |
| 390 |
| 391 // Read the reply, which pickles the PID and an optional UMA enumeration. |
| 392 static const unsigned kMaxReplyLength = 2048; |
| 393 char buf[kMaxReplyLength]; |
| 394 const ssize_t len = ReadReply(buf, sizeof(buf)); |
| 395 |
| 396 base::Pickle reply_pickle(buf, len); |
| 397 base::PickleIterator iter(reply_pickle); |
| 398 if (len <= 0 || !iter.ReadInt(&pid)) |
| 399 return base::kNullProcessHandle; |
| 400 |
| 401 // If there is a nonempty UMA name string, then there is a UMA |
| 402 // enumeration to record. |
| 403 std::string uma_name; |
| 404 int uma_sample; |
| 405 int uma_boundary_value; |
| 406 if (iter.ReadString(&uma_name) && |
| 407 !uma_name.empty() && |
| 408 iter.ReadInt(&uma_sample) && |
| 409 iter.ReadInt(&uma_boundary_value)) { |
| 410 // We cannot use the UMA_HISTOGRAM_ENUMERATION macro here, |
| 411 // because that's only for when the name is the same every time. |
| 412 // Here we're using whatever name we got from the other side. |
| 413 // But since it's likely that the same one will be used repeatedly |
| 414 // (even though it's not guaranteed), we cache it here. |
| 415 static base::HistogramBase* uma_histogram; |
| 416 if (!uma_histogram || uma_histogram->histogram_name() != uma_name) { |
| 417 uma_histogram = base::LinearHistogram::FactoryGet( |
| 418 uma_name, 1, |
| 419 uma_boundary_value, |
| 420 uma_boundary_value + 1, |
| 421 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 422 } |
| 423 uma_histogram->Add(uma_sample); |
| 424 } |
| 425 |
| 426 if (pid <= 0) |
| 427 return base::kNullProcessHandle; |
| 428 } |
63 | 429 |
64 #if !defined(OS_OPENBSD) | 430 #if !defined(OS_OPENBSD) |
| 431 // This is just a starting score for a renderer or extension (the |
| 432 // only types of processes that will be started this way). It will |
| 433 // get adjusted as time goes on. (This is the same value as |
| 434 // chrome::kLowestRendererOomScore in chrome/chrome_constants.h, but |
| 435 // that's not something we can include here.) |
| 436 const int kLowestRendererOomScore = 300; |
| 437 AdjustRendererOOMScore(pid, kLowestRendererOomScore); |
| 438 #endif |
| 439 |
| 440 ZygoteChildBorn(pid); |
| 441 return pid; |
| 442 } |
| 443 |
| 444 #if !defined(OS_OPENBSD) |
65 void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid, | 445 void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid, |
66 int score) { | 446 int score) { |
67 // 1) You can't change the oom_score_adj of a non-dumpable process | 447 // 1) You can't change the oom_score_adj of a non-dumpable process |
68 // (EPERM) unless you're root. Because of this, we can't set the | 448 // (EPERM) unless you're root. Because of this, we can't set the |
69 // oom_adj from the browser process. | 449 // oom_adj from the browser process. |
70 // | 450 // |
71 // 2) We can't set the oom_score_adj before entering the sandbox | 451 // 2) We can't set the oom_score_adj before entering the sandbox |
72 // because the zygote is in the sandbox and the zygote is as | 452 // because the zygote is in the sandbox and the zygote is as |
73 // critical as the browser process. Its oom_adj value shouldn't | 453 // critical as the browser process. Its oom_adj value shouldn't |
74 // be changed. | 454 // be changed. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 base::LaunchProcess(adj_oom_score_cmdline, options); | 507 base::LaunchProcess(adj_oom_score_cmdline, options); |
128 if (sandbox_helper_process.IsValid()) | 508 if (sandbox_helper_process.IsValid()) |
129 base::EnsureProcessGetsReaped(sandbox_helper_process.Pid()); | 509 base::EnsureProcessGetsReaped(sandbox_helper_process.Pid()); |
130 } else if (!use_suid_sandbox_for_adj_oom_score_) { | 510 } else if (!use_suid_sandbox_for_adj_oom_score_) { |
131 if (!base::AdjustOOMScore(pid, score)) | 511 if (!base::AdjustOOMScore(pid, score)) |
132 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; | 512 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; |
133 } | 513 } |
134 } | 514 } |
135 #endif | 515 #endif |
136 | 516 |
| 517 void ZygoteHostImpl::EnsureProcessTerminated(pid_t process) { |
| 518 DCHECK(init_); |
| 519 base::Pickle pickle; |
| 520 |
| 521 pickle.WriteInt(kZygoteCommandReap); |
| 522 pickle.WriteInt(process); |
| 523 if (!SendMessage(pickle, NULL)) |
| 524 LOG(ERROR) << "Failed to send Reap message to zygote"; |
| 525 ZygoteChildDied(process); |
| 526 } |
| 527 |
| 528 base::TerminationStatus ZygoteHostImpl::GetTerminationStatus( |
| 529 base::ProcessHandle handle, |
| 530 bool known_dead, |
| 531 int* exit_code) { |
| 532 DCHECK(init_); |
| 533 base::Pickle pickle; |
| 534 pickle.WriteInt(kZygoteCommandGetTerminationStatus); |
| 535 pickle.WriteBool(known_dead); |
| 536 pickle.WriteInt(handle); |
| 537 |
| 538 static const unsigned kMaxMessageLength = 128; |
| 539 char buf[kMaxMessageLength]; |
| 540 ssize_t len; |
| 541 { |
| 542 base::AutoLock lock(control_lock_); |
| 543 if (!SendMessage(pickle, NULL)) |
| 544 LOG(ERROR) << "Failed to send GetTerminationStatus message to zygote"; |
| 545 len = ReadReply(buf, sizeof(buf)); |
| 546 } |
| 547 |
| 548 // Set this now to handle the error cases. |
| 549 if (exit_code) |
| 550 *exit_code = RESULT_CODE_NORMAL_EXIT; |
| 551 int status = base::TERMINATION_STATUS_NORMAL_TERMINATION; |
| 552 |
| 553 if (len == -1) { |
| 554 LOG(WARNING) << "Error reading message from zygote: " << errno; |
| 555 } else if (len == 0) { |
| 556 LOG(WARNING) << "Socket closed prematurely."; |
| 557 } else { |
| 558 base::Pickle read_pickle(buf, len); |
| 559 int tmp_status, tmp_exit_code; |
| 560 base::PickleIterator iter(read_pickle); |
| 561 if (!iter.ReadInt(&tmp_status) || !iter.ReadInt(&tmp_exit_code)) { |
| 562 LOG(WARNING) |
| 563 << "Error parsing GetTerminationStatus response from zygote."; |
| 564 } else { |
| 565 if (exit_code) |
| 566 *exit_code = tmp_exit_code; |
| 567 status = tmp_status; |
| 568 } |
| 569 } |
| 570 |
| 571 if (status != base::TERMINATION_STATUS_STILL_RUNNING) { |
| 572 ZygoteChildDied(handle); |
| 573 } |
| 574 return static_cast<base::TerminationStatus>(status); |
| 575 } |
| 576 |
| 577 pid_t ZygoteHostImpl::GetPid() const { |
| 578 return pid_; |
| 579 } |
| 580 |
| 581 int ZygoteHostImpl::GetSandboxStatus() const { |
| 582 if (have_read_sandbox_status_word_) |
| 583 return sandbox_status_; |
| 584 return 0; |
| 585 } |
| 586 |
| 587 bool ZygoteHostImpl::ShouldUseNamespaceSandbox() { |
| 588 const base::CommandLine& command_line = |
| 589 *base::CommandLine::ForCurrentProcess(); |
| 590 if (command_line.HasSwitch(switches::kNoSandbox)) { |
| 591 return false; |
| 592 } |
| 593 |
| 594 if (command_line.HasSwitch(switches::kDisableNamespaceSandbox)) { |
| 595 return false; |
| 596 } |
| 597 |
| 598 if (!sandbox::Credentials::CanCreateProcessInNewUserNS()) { |
| 599 return false; |
| 600 } |
| 601 |
| 602 return true; |
| 603 } |
| 604 |
137 } // namespace content | 605 } // namespace content |
OLD | NEW |