Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: content/common/sandbox_linux/sandbox_linux.cc

Issue 919203002: Linux Sandbox: add resource limits to NaCl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Correct typo. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/nacl/loader/sandbox_linux/nacl_sandbox_linux.cc ('k') | sandbox/linux/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <dirent.h> 5 #include <dirent.h>
6 #include <fcntl.h> 6 #include <fcntl.h>
7 #include <sys/resource.h> 7 #include <sys/resource.h>
8 #include <sys/stat.h> 8 #include <sys/stat.h>
9 #include <sys/time.h> 9 #include <sys/time.h>
10 #include <sys/types.h> 10 #include <sys/types.h>
(...skipping 18 matching lines...) Expand all
29 #include "base/time/time.h" 29 #include "base/time/time.h"
30 #include "build/build_config.h" 30 #include "build/build_config.h"
31 #include "content/common/sandbox_linux/sandbox_debug_handling_linux.h" 31 #include "content/common/sandbox_linux/sandbox_debug_handling_linux.h"
32 #include "content/common/sandbox_linux/sandbox_linux.h" 32 #include "content/common/sandbox_linux/sandbox_linux.h"
33 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h" 33 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h"
34 #include "content/public/common/content_switches.h" 34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/sandbox_linux.h" 35 #include "content/public/common/sandbox_linux.h"
36 #include "sandbox/linux/services/credentials.h" 36 #include "sandbox/linux/services/credentials.h"
37 #include "sandbox/linux/services/namespace_sandbox.h" 37 #include "sandbox/linux/services/namespace_sandbox.h"
38 #include "sandbox/linux/services/proc_util.h" 38 #include "sandbox/linux/services/proc_util.h"
39 #include "sandbox/linux/services/resource_limits.h"
39 #include "sandbox/linux/services/thread_helpers.h" 40 #include "sandbox/linux/services/thread_helpers.h"
40 #include "sandbox/linux/services/yama.h" 41 #include "sandbox/linux/services/yama.h"
41 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" 42 #include "sandbox/linux/suid/client/setuid_sandbox_client.h"
42 43
43 #if defined(ANY_OF_AMTLU_SANITIZER) 44 #if defined(ANY_OF_AMTLU_SANITIZER)
44 #include <sanitizer/common_interface_defs.h> 45 #include <sanitizer/common_interface_defs.h>
45 #endif 46 #endif
46 47
47 using sandbox::Yama; 48 using sandbox::Yama;
48 49
(...skipping 11 matching lines...) Expand all
60 const base::CommandLine& command_line = 61 const base::CommandLine& command_line =
61 *base::CommandLine::ForCurrentProcess(); 62 *base::CommandLine::ForCurrentProcess();
62 const std::string process_type = 63 const std::string process_type =
63 command_line.GetSwitchValueASCII(switches::kProcessType); 64 command_line.GetSwitchValueASCII(switches::kProcessType);
64 const std::string activated_sandbox = 65 const std::string activated_sandbox =
65 "Activated " + sandbox_name + " sandbox for process type: " + 66 "Activated " + sandbox_name + " sandbox for process type: " +
66 process_type + "."; 67 process_type + ".";
67 VLOG(1) << activated_sandbox; 68 VLOG(1) << activated_sandbox;
68 } 69 }
69 70
70 bool AddResourceLimit(int resource, rlim_t limit) {
71 struct rlimit old_rlimit;
72 if (getrlimit(resource, &old_rlimit))
73 return false;
74 // Make sure we don't raise the existing limit.
75 const struct rlimit new_rlimit = {
76 std::min(old_rlimit.rlim_cur, limit),
77 std::min(old_rlimit.rlim_max, limit)
78 };
79 int rc = setrlimit(resource, &new_rlimit);
80 return rc == 0;
81 }
82
83 bool IsRunningTSAN() { 71 bool IsRunningTSAN() {
84 #if defined(THREAD_SANITIZER) 72 #if defined(THREAD_SANITIZER)
85 return true; 73 return true;
86 #else 74 #else
87 return false; 75 return false;
88 #endif 76 #endif
89 } 77 }
90 78
91 // Try to open /proc/self/task/ with the help of |proc_fd|. |proc_fd| can be 79 // Try to open /proc/self/task/ with the help of |proc_fd|. |proc_fd| can be
92 // -1. Will return -1 on error and set errno like open(2). 80 // -1. Will return -1 on error and set errno like open(2).
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 if (process_type == switches::kRendererProcess || 401 if (process_type == switches::kRendererProcess ||
414 process_type == switches::kGpuProcess) { 402 process_type == switches::kGpuProcess) {
415 address_space_limit = 1L << 34; 403 address_space_limit = 1L << 34;
416 } 404 }
417 #endif // defined(__LP64__) 405 #endif // defined(__LP64__)
418 406
419 // On all platforms, add a limit to the brk() heap that would prevent 407 // On all platforms, add a limit to the brk() heap that would prevent
420 // allocations that can't be index by an int. 408 // allocations that can't be index by an int.
421 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max(); 409 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max();
422 410
423 bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit); 411 bool limited_as =
424 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize); 412 sandbox::ResourceLimits::Lower(RLIMIT_AS, address_space_limit);
413 bool limited_data =
414 sandbox::ResourceLimits::Lower(RLIMIT_DATA, kNewDataSegmentMaxSize);
425 415
426 // Cache the resource limit before turning on the sandbox. 416 // Cache the resource limit before turning on the sandbox.
427 base::SysInfo::AmountOfVirtualMemory(); 417 base::SysInfo::AmountOfVirtualMemory();
428 418
429 return limited_as && limited_data; 419 return limited_as && limited_data;
430 #else 420 #else
431 // Silence the compiler warning about unused function. This doesn't actually
432 // call AddResourceLimit().
433 ignore_result(AddResourceLimit);
434 base::SysInfo::AmountOfVirtualMemory(); 421 base::SysInfo::AmountOfVirtualMemory();
435 return false; 422 return false;
436 #endif // !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && 423 #endif // !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) &&
437 // !defined(THREAD_SANITIZER) 424 // !defined(THREAD_SANITIZER)
438 } 425 }
439 426
440 bool LinuxSandbox::HasOpenDirectories() const { 427 bool LinuxSandbox::HasOpenDirectories() const {
441 return sandbox::ProcUtil::HasOpenDirectory(proc_fd_); 428 return sandbox::ProcUtil::HasOpenDirectory(proc_fd_);
442 } 429 }
443 430
(...skipping 21 matching lines...) Expand all
465 452
466 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const { 453 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const {
467 DCHECK(thread); 454 DCHECK(thread);
468 base::ScopedFD proc_self_task(OpenProcTaskFd(proc_fd_)); 455 base::ScopedFD proc_self_task(OpenProcTaskFd(proc_fd_));
469 PCHECK(proc_self_task.is_valid()); 456 PCHECK(proc_self_task.is_valid());
470 CHECK(sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task.get(), 457 CHECK(sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task.get(),
471 thread)); 458 thread));
472 } 459 }
473 460
474 } // namespace content 461 } // namespace content
OLDNEW
« no previous file with comments | « components/nacl/loader/sandbox_linux/nacl_sandbox_linux.cc ('k') | sandbox/linux/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698