Get foreground tab on Android
[chromium-blink-merge.git] / content / common / sandbox_linux.cc
blob6589682f49db0fb7d64f34550f43d134d03d3e83
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 <dirent.h>
6 #include <fcntl.h>
7 #include <sys/resource.h>
8 #include <sys/stat.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
12 #include <limits>
14 #include "base/bind.h"
15 #include "base/callback_helpers.h"
16 #include "base/command_line.h"
17 #include "base/logging.h"
18 #include "base/memory/singleton.h"
19 #include "base/posix/eintr_wrapper.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/time/time.h"
22 #include "content/common/sandbox_linux.h"
23 #include "content/common/sandbox_seccomp_bpf_linux.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/sandbox_linux.h"
26 #include "sandbox/linux/suid/client/setuid_sandbox_client.h"
28 namespace {
30 void LogSandboxStarted(const std::string& sandbox_name) {
31 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
32 const std::string process_type =
33 command_line.GetSwitchValueASCII(switches::kProcessType);
34 const std::string activated_sandbox =
35 "Activated " + sandbox_name + " sandbox for process type: " +
36 process_type + ".";
37 #if defined(OS_CHROMEOS)
38 LOG(WARNING) << activated_sandbox;
39 #else
40 VLOG(1) << activated_sandbox;
41 #endif
44 bool AddResourceLimit(int resource, rlim_t limit) {
45 struct rlimit old_rlimit;
46 if (getrlimit(resource, &old_rlimit))
47 return false;
48 // Make sure we don't raise the existing limit.
49 const struct rlimit new_rlimit = {
50 std::min(old_rlimit.rlim_cur, limit),
51 std::min(old_rlimit.rlim_max, limit)
53 int rc = setrlimit(resource, &new_rlimit);
54 return rc == 0;
57 bool IsRunningTSAN() {
58 #if defined(THREAD_SANITIZER)
59 return true;
60 #else
61 return false;
62 #endif
65 struct DIRDeleter {
66 void operator()(DIR* d) {
67 PCHECK(closedir(d) == 0);
71 // |proc_fd| should be a file descriptor to /proc (useful if the process
72 // is sandboxed) or -1.
73 // If |proc_fd| is -1, this function will return false if /proc/self/fd
74 // cannot be opened.
75 bool CurrentProcessHasOpenDirectories(int proc_fd) {
76 int proc_self_fd = -1;
77 if (proc_fd >= 0) {
78 proc_self_fd = openat(proc_fd, "self/fd", O_DIRECTORY | O_RDONLY);
79 } else {
80 proc_self_fd = openat(AT_FDCWD, "/proc/self/fd", O_DIRECTORY | O_RDONLY);
81 if (proc_self_fd < 0) {
82 // If not available, guess false.
83 // TODO(mostynb@opera.com): add a CHECK_EQ(ENOENT, errno); Figure out what
84 // other situations are here. http://crbug.com/314985
85 return false;
88 CHECK_GE(proc_self_fd, 0);
90 // Ownership of proc_self_fd is transferred here, it must not be closed
91 // or modified afterwards except via dir.
92 scoped_ptr<DIR, DIRDeleter> dir(fdopendir(proc_self_fd));
93 CHECK(dir);
95 struct dirent e;
96 struct dirent* de;
97 while (!readdir_r(dir.get(), &e, &de) && de) {
98 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) {
99 continue;
102 int fd_num;
103 CHECK(base::StringToInt(e.d_name, &fd_num));
104 if (fd_num == proc_fd || fd_num == proc_self_fd) {
105 continue;
108 struct stat s;
109 // It's OK to use proc_self_fd here, fstatat won't modify it.
110 CHECK(fstatat(proc_self_fd, e.d_name, &s, 0) == 0);
111 if (S_ISDIR(s.st_mode)) {
112 return true;
116 // No open unmanaged directories found.
117 return false;
120 } // namespace
122 namespace content {
124 LinuxSandbox::LinuxSandbox()
125 : proc_fd_(-1),
126 seccomp_bpf_started_(false),
127 pre_initialized_(false),
128 seccomp_bpf_supported_(false),
129 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) {
130 if (setuid_sandbox_client_ == NULL) {
131 LOG(FATAL) << "Failed to instantiate the setuid sandbox client.";
135 LinuxSandbox::~LinuxSandbox() {
138 LinuxSandbox* LinuxSandbox::GetInstance() {
139 LinuxSandbox* instance = Singleton<LinuxSandbox>::get();
140 CHECK(instance);
141 return instance;
144 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX)
145 // ASan API call to notify the tool the sandbox is going to be turned on.
146 extern "C" void __sanitizer_sandbox_on_notify(void *reserved);
147 #endif
149 void LinuxSandbox::PreinitializeSandbox() {
150 CHECK(!pre_initialized_);
151 seccomp_bpf_supported_ = false;
152 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX)
153 // ASan needs to open some resources before the sandbox is enabled.
154 // This should not fork, not launch threads, not open a directory.
155 __sanitizer_sandbox_on_notify(/*reserved*/NULL);
156 #endif
158 #if !defined(NDEBUG)
159 // Open proc_fd_ only in Debug mode so that forgetting to close it doesn't
160 // produce a sandbox escape in Release mode.
161 proc_fd_ = open("/proc", O_DIRECTORY | O_RDONLY);
162 CHECK_GE(proc_fd_, 0);
163 #endif // !defined(NDEBUG)
164 // We "pre-warm" the code that detects supports for seccomp BPF.
165 if (SandboxSeccompBpf::IsSeccompBpfDesired()) {
166 if (!SandboxSeccompBpf::SupportsSandbox()) {
167 VLOG(1) << "Lacking support for seccomp-bpf sandbox.";
168 } else {
169 seccomp_bpf_supported_ = true;
172 pre_initialized_ = true;
175 bool LinuxSandbox::InitializeSandbox() {
176 bool seccomp_bpf_started = false;
177 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
178 // We need to make absolutely sure that our sandbox is "sealed" before
179 // InitializeSandbox does exit.
180 base::ScopedClosureRunner sandbox_sealer(
181 base::Bind(&LinuxSandbox::SealSandbox, base::Unretained(linux_sandbox)));
182 const std::string process_type =
183 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
184 switches::kProcessType);
186 // No matter what, it's always an error to call InitializeSandbox() after
187 // threads have been created.
188 if (!linux_sandbox->IsSingleThreaded()) {
189 std::string error_message = "InitializeSandbox() called with multiple "
190 "threads in process " + process_type;
191 // TSAN starts a helper thread. So we don't start the sandbox and don't
192 // even report an error about it.
193 if (IsRunningTSAN())
194 return false;
195 // The GPU process is allowed to call InitializeSandbox() with threads for
196 // now, because it loads third party libraries.
197 if (process_type != switches::kGpuProcess)
198 CHECK(false) << error_message;
199 LOG(ERROR) << error_message;
200 return false;
203 DCHECK(!linux_sandbox->HasOpenDirectories()) <<
204 "InitializeSandbox() called after unexpected directories have been " <<
205 "opened. This breaks the security of the setuid sandbox.";
207 // Attempt to limit the future size of the address space of the process.
208 linux_sandbox->LimitAddressSpace(process_type);
210 // First, try to enable seccomp-bpf.
211 seccomp_bpf_started = linux_sandbox->StartSeccompBpf(process_type);
213 return seccomp_bpf_started;
216 int LinuxSandbox::GetStatus() const {
217 CHECK(pre_initialized_);
218 int sandbox_flags = 0;
219 if (setuid_sandbox_client_->IsSandboxed()) {
220 sandbox_flags |= kSandboxLinuxSUID;
221 if (setuid_sandbox_client_->IsInNewPIDNamespace())
222 sandbox_flags |= kSandboxLinuxPIDNS;
223 if (setuid_sandbox_client_->IsInNewNETNamespace())
224 sandbox_flags |= kSandboxLinuxNetNS;
227 if (seccomp_bpf_supported() &&
228 SandboxSeccompBpf::ShouldEnableSeccompBpf(switches::kRendererProcess)) {
229 // We report whether the sandbox will be activated when renderers go
230 // through sandbox initialization.
231 sandbox_flags |= kSandboxLinuxSeccompBpf;
234 return sandbox_flags;
237 // Threads are counted via /proc/self/task. This is a little hairy because of
238 // PID namespaces and existing sandboxes, so "self" must really be used instead
239 // of using the pid.
240 bool LinuxSandbox::IsSingleThreaded() const {
241 struct stat task_stat;
242 int fstat_ret;
243 if (proc_fd_ >= 0) {
244 // If a handle to /proc is available, use it. This allows to bypass file
245 // system restrictions.
246 fstat_ret = fstatat(proc_fd_, "self/task/", &task_stat, 0);
247 } else {
248 // Otherwise, make an attempt to access the file system directly.
249 fstat_ret = fstatat(AT_FDCWD, "/proc/self/task/", &task_stat, 0);
251 // In Debug mode, it's mandatory to be able to count threads to catch bugs.
252 #if !defined(NDEBUG)
253 // Using DCHECK here would be incorrect. DCHECK can be enabled in non
254 // official release mode.
255 CHECK_EQ(0, fstat_ret) << "Could not count threads, the sandbox was not "
256 << "pre-initialized properly.";
257 #endif // !defined(NDEBUG)
258 if (fstat_ret) {
259 // Pretend to be monothreaded if it can't be determined (for instance the
260 // setuid sandbox is already engaged but no proc_fd_ is available).
261 return true;
264 // At least "..", "." and the current thread should be present.
265 CHECK_LE(3UL, task_stat.st_nlink);
266 // Counting threads via /proc/self/task could be racy. For the purpose of
267 // determining if the current proces is monothreaded it works: if at any
268 // time it becomes monothreaded, it'll stay so.
269 return task_stat.st_nlink == 3;
272 bool LinuxSandbox::seccomp_bpf_started() const {
273 return seccomp_bpf_started_;
276 sandbox::SetuidSandboxClient*
277 LinuxSandbox::setuid_sandbox_client() const {
278 return setuid_sandbox_client_.get();
281 // For seccomp-bpf, we use the SandboxSeccompBpf class.
282 bool LinuxSandbox::StartSeccompBpf(const std::string& process_type) {
283 CHECK(!seccomp_bpf_started_);
284 if (!pre_initialized_)
285 PreinitializeSandbox();
286 if (seccomp_bpf_supported())
287 seccomp_bpf_started_ = SandboxSeccompBpf::StartSandbox(process_type);
289 if (seccomp_bpf_started_)
290 LogSandboxStarted("seccomp-bpf");
292 return seccomp_bpf_started_;
295 bool LinuxSandbox::seccomp_bpf_supported() const {
296 CHECK(pre_initialized_);
297 return seccomp_bpf_supported_;
300 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) {
301 (void) process_type;
302 #if !defined(ADDRESS_SANITIZER)
303 CommandLine* command_line = CommandLine::ForCurrentProcess();
304 if (command_line->HasSwitch(switches::kNoSandbox)) {
305 return false;
308 // Limit the address space to 4GB.
309 // This is in the hope of making some kernel exploits more complex and less
310 // reliable. It also limits sprays a little on 64-bit.
311 rlim_t address_space_limit = std::numeric_limits<uint32_t>::max();
312 #if defined(__LP64__)
313 // On 64 bits, V8 and possibly others will reserve massive memory ranges and
314 // rely on on-demand paging for allocation. Unfortunately, even
315 // MADV_DONTNEED ranges count towards RLIMIT_AS so this is not an option.
316 // See crbug.com/169327 for a discussion.
317 // On the GPU process, irrespective of V8, we can exhaust a 4GB address space
318 // under normal usage, see crbug.com/271119
319 // For now, increase limit to 16GB for renderer and worker and gpu processes
320 // to accomodate.
321 if (process_type == switches::kRendererProcess ||
322 process_type == switches::kWorkerProcess ||
323 process_type == switches::kGpuProcess) {
324 address_space_limit = 1L << 34;
326 #endif // defined(__LP64__)
328 // On all platforms, add a limit to the brk() heap that would prevent
329 // allocations that can't be index by an int.
330 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max();
332 bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit);
333 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize);
334 return limited_as && limited_data;
335 #else
336 return false;
337 #endif // !defined(ADDRESS_SANITIZER)
340 bool LinuxSandbox::HasOpenDirectories() {
341 return CurrentProcessHasOpenDirectories(proc_fd_);
344 void LinuxSandbox::SealSandbox() {
345 if (proc_fd_ >= 0) {
346 int ret = HANDLE_EINTR(close(proc_fd_));
347 CHECK_EQ(0, ret);
348 proc_fd_ = -1;
352 } // namespace content