| 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 "base/allocator/allocator_extension.h" | 7 #include "base/allocator/allocator_extension.h" |
| 8 #include "base/command_line.h" |
| 8 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 9 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
| 10 #include "base/process/memory.h" | 11 #include "base/process/memory.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "content/public/browser/content_browser_client.h" | 13 #include "content/public/browser/content_browser_client.h" |
| 14 #include "content/public/common/content_switches.h" |
| 15 #include "sandbox/linux/services/credentials.h" |
| 13 #include "sandbox/linux/suid/common/sandbox.h" | 16 #include "sandbox/linux/suid/common/sandbox.h" |
| 14 | 17 |
| 15 namespace content { | 18 namespace content { |
| 16 | 19 |
| 17 // static | 20 // static |
| 18 ZygoteHost* ZygoteHost::GetInstance() { | 21 ZygoteHost* ZygoteHost::GetInstance() { |
| 19 return ZygoteHostImpl::GetInstance(); | 22 return ZygoteHostImpl::GetInstance(); |
| 20 } | 23 } |
| 21 | 24 |
| 22 ZygoteHostImpl::ZygoteHostImpl() | 25 ZygoteHostImpl::ZygoteHostImpl() |
| 23 : use_suid_sandbox_for_adj_oom_score_(false), | 26 : should_use_namespace_sandbox_(true), |
| 27 use_suid_sandbox_for_adj_oom_score_(false), |
| 24 sandbox_binary_(), | 28 sandbox_binary_(), |
| 25 zygote_pids_lock_(), | 29 zygote_pids_lock_(), |
| 26 zygote_pids_() {} | 30 zygote_pids_() {} |
| 27 | 31 |
| 28 ZygoteHostImpl::~ZygoteHostImpl() {} | 32 ZygoteHostImpl::~ZygoteHostImpl() {} |
| 29 | 33 |
| 30 // static | 34 // static |
| 31 ZygoteHostImpl* ZygoteHostImpl::GetInstance() { | 35 ZygoteHostImpl* ZygoteHostImpl::GetInstance() { |
| 32 return base::Singleton<ZygoteHostImpl>::get(); | 36 return base::Singleton<ZygoteHostImpl>::get(); |
| 33 } | 37 } |
| 34 | 38 |
| 35 void ZygoteHostImpl::Init(const std::string& sandbox_cmd) { | 39 void ZygoteHostImpl::Init(const std::string& sandbox_cmd) { |
| 36 sandbox_binary_ = sandbox_cmd; | 40 sandbox_binary_ = sandbox_cmd; |
| 41 |
| 42 const base::CommandLine& command_line = |
| 43 *base::CommandLine::ForCurrentProcess(); |
| 44 if (command_line.HasSwitch(switches::kNoSandbox) || |
| 45 command_line.HasSwitch(switches::kDisableNamespaceSandbox) || |
| 46 !sandbox::Credentials::CanCreateProcessInNewUserNS()) { |
| 47 should_use_namespace_sandbox_ = false; |
| 48 } |
| 49 |
| 50 const bool using_namespace_sandbox = ShouldUseNamespaceSandbox(); |
| 51 // A non empty sandbox_cmd means we want a SUID sandbox. |
| 52 const bool using_suid_sandbox = |
| 53 sandbox_binary_.empty() && !using_namespace_sandbox; |
| 54 |
| 55 // Use the SUID sandbox for adjusting OOM scores when we are using the setuid |
| 56 // sandbox. This is needed beacuse the processes are non-dumpable, so |
| 57 // /proc/pid/oom_score_adj can only be written by root. |
| 58 use_suid_sandbox_for_adj_oom_score_ = using_suid_sandbox; |
| 59 |
| 60 #if defined(OS_CHROMEOS) |
| 61 // Chrome OS has a kernel patch that restricts oom_score_adj. See |
| 62 // crbug.com/576409 for details. |
| 63 if (!sandbox_binary_.empty()) { |
| 64 use_suid_sandbox_for_adj_oom_score_ = true; |
| 65 } |
| 66 #endif |
| 37 } | 67 } |
| 38 | 68 |
| 39 void ZygoteHostImpl::AddZygotePid(pid_t pid) { | 69 void ZygoteHostImpl::AddZygotePid(pid_t pid) { |
| 40 base::AutoLock lock(zygote_pids_lock_); | 70 base::AutoLock lock(zygote_pids_lock_); |
| 41 zygote_pids_.insert(pid); | 71 zygote_pids_.insert(pid); |
| 42 } | 72 } |
| 43 | 73 |
| 44 bool ZygoteHostImpl::IsZygotePid(pid_t pid) { | 74 bool ZygoteHostImpl::IsZygotePid(pid_t pid) { |
| 45 base::AutoLock lock(zygote_pids_lock_); | 75 base::AutoLock lock(zygote_pids_lock_); |
| 46 return zygote_pids_.find(pid) != zygote_pids_.end(); | 76 return zygote_pids_.find(pid) != zygote_pids_.end(); |
| 47 } | 77 } |
| 48 | 78 |
| 49 const std::string& ZygoteHostImpl::SandboxCommand() const { | 79 const std::string& ZygoteHostImpl::SandboxCommand() const { |
| 50 return sandbox_binary_; | 80 return sandbox_binary_; |
| 51 } | 81 } |
| 52 | 82 |
| 53 void ZygoteHostImpl::SetRendererSandboxStatus(int status) { | 83 void ZygoteHostImpl::SetRendererSandboxStatus(int status) { |
| 54 renderer_sandbox_status_ = status; | 84 renderer_sandbox_status_ = status; |
| 55 } | 85 } |
| 56 | 86 |
| 57 int ZygoteHostImpl::GetRendererSandboxStatus() const { | 87 int ZygoteHostImpl::GetRendererSandboxStatus() const { |
| 58 return renderer_sandbox_status_; | 88 return renderer_sandbox_status_; |
| 59 } | 89 } |
| 60 | 90 |
| 91 bool ZygoteHostImpl::ShouldUseNamespaceSandbox() { |
| 92 return should_use_namespace_sandbox_; |
| 93 } |
| 94 |
| 61 #if !defined(OS_OPENBSD) | 95 #if !defined(OS_OPENBSD) |
| 62 void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid, | 96 void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid, |
| 63 int score) { | 97 int score) { |
| 64 // 1) You can't change the oom_score_adj of a non-dumpable process | 98 // 1) You can't change the oom_score_adj of a non-dumpable process |
| 65 // (EPERM) unless you're root. Because of this, we can't set the | 99 // (EPERM) unless you're root. Because of this, we can't set the |
| 66 // oom_adj from the browser process. | 100 // oom_adj from the browser process. |
| 67 // | 101 // |
| 68 // 2) We can't set the oom_score_adj before entering the sandbox | 102 // 2) We can't set the oom_score_adj before entering the sandbox |
| 69 // because the zygote is in the sandbox and the zygote is as | 103 // because the zygote is in the sandbox and the zygote is as |
| 70 // critical as the browser process. Its oom_adj value shouldn't | 104 // critical as the browser process. Its oom_adj value shouldn't |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 if (sandbox_helper_process.IsValid()) | 158 if (sandbox_helper_process.IsValid()) |
| 125 base::EnsureProcessGetsReaped(sandbox_helper_process.Pid()); | 159 base::EnsureProcessGetsReaped(sandbox_helper_process.Pid()); |
| 126 } else if (!use_suid_sandbox_for_adj_oom_score_) { | 160 } else if (!use_suid_sandbox_for_adj_oom_score_) { |
| 127 if (!base::AdjustOOMScore(pid, score)) | 161 if (!base::AdjustOOMScore(pid, score)) |
| 128 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; | 162 PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid; |
| 129 } | 163 } |
| 130 } | 164 } |
| 131 #endif | 165 #endif |
| 132 | 166 |
| 133 } // namespace content | 167 } // namespace content |
| OLD | NEW |