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

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

Issue 11783104: Linux sandbox: allow 32GB address space size (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Limit the brk() heap. Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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 <fcntl.h> 5 #include <fcntl.h>
6 #include <sys/resource.h> 6 #include <sys/resource.h>
7 #include <sys/stat.h> 7 #include <sys/stat.h>
8 #include <sys/time.h> 8 #include <sys/time.h>
9 #include <sys/types.h> 9 #include <sys/types.h>
10 10
11 #include <limits>
12
11 #include "base/command_line.h" 13 #include "base/command_line.h"
12 #include "base/file_util.h" 14 #include "base/file_util.h"
13 #include "base/logging.h" 15 #include "base/logging.h"
14 #include "base/memory/singleton.h" 16 #include "base/memory/singleton.h"
15 #include "base/posix/eintr_wrapper.h" 17 #include "base/posix/eintr_wrapper.h"
16 #include "base/time.h" 18 #include "base/time.h"
17 #include "content/common/sandbox_linux.h" 19 #include "content/common/sandbox_linux.h"
18 #include "content/common/seccomp_sandbox.h" 20 #include "content/common/seccomp_sandbox.h"
19 #include "content/common/sandbox_seccomp_bpf_linux.h" 21 #include "content/common/sandbox_seccomp_bpf_linux.h"
20 #include "content/public/common/content_switches.h" 22 #include "content/public/common/content_switches.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 // supported. 61 // supported.
60 bool ShouldEnableSeccompLegacy(const std::string& process_type) { 62 bool ShouldEnableSeccompLegacy(const std::string& process_type) {
61 if (IsSeccompLegacyDesired() && 63 if (IsSeccompLegacyDesired() &&
62 process_type == switches::kRendererProcess) { 64 process_type == switches::kRendererProcess) {
63 return true; 65 return true;
64 } else { 66 } else {
65 return false; 67 return false;
66 } 68 }
67 } 69 }
68 70
71 bool AddResourceLimit(int resource, rlim_t limit) {
72 struct rlimit old_rlimit;
73 if (getrlimit(resource, &old_rlimit))
74 return false;
75 // Make sure we don't raise the existing limit.
76 const struct rlimit new_rlimit = {
77 std::min(old_rlimit.rlim_cur, limit),
78 std::min(old_rlimit.rlim_max, limit)
79 };
80 int rc = setrlimit(resource, &new_rlimit);
81 return rc == 0;
82 }
83
69 } // namespace 84 } // namespace
70 85
71 namespace content { 86 namespace content {
72 87
73 LinuxSandbox::LinuxSandbox() 88 LinuxSandbox::LinuxSandbox()
74 : proc_fd_(-1), 89 : proc_fd_(-1),
75 seccomp_bpf_started_(false), 90 seccomp_bpf_started_(false),
76 pre_initialized_(false), 91 pre_initialized_(false),
77 seccomp_legacy_supported_(false), 92 seccomp_legacy_supported_(false),
78 seccomp_bpf_supported_(false), 93 seccomp_bpf_supported_(false),
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 return seccomp_legacy_supported_; 265 return seccomp_legacy_supported_;
251 } 266 }
252 267
253 bool LinuxSandbox::seccomp_bpf_supported() const { 268 bool LinuxSandbox::seccomp_bpf_supported() const {
254 CHECK(pre_initialized_); 269 CHECK(pre_initialized_);
255 return seccomp_bpf_supported_; 270 return seccomp_bpf_supported_;
256 } 271 }
257 272
258 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) { 273 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) {
259 (void) process_type; 274 (void) process_type;
260 #if defined(__x86_64__) && !defined(ADDRESS_SANITIZER) 275 #if !defined(ADDRESS_SANITIZER)
261 CommandLine* command_line = CommandLine::ForCurrentProcess(); 276 CommandLine* command_line = CommandLine::ForCurrentProcess();
262 if (command_line->HasSwitch(switches::kNoSandbox)) { 277 if (command_line->HasSwitch(switches::kNoSandbox)) {
263 return false; 278 return false;
264 } 279 }
265 // Limit the address space to 4GB. 280 #if defined(__x86_64__)
Chris Evans 2013/01/15 09:07:56 I wonder if this can be done more generically so t
jln (very slow on Chromium) 2013/01/15 19:44:19 I hesitated a bit when I originally wrote this, bu
266 const rlim_t kNewAddressSpaceMaxSize = 0x100000000L; 281 // On 64 bits, limit the address space to 16GB. This is in the hope of making
267 struct rlimit old_address_space_limit; 282 // some kernel exploits more complex and less reliable. This limit has to be
268 if (getrlimit(RLIMIT_AS, &old_address_space_limit)) 283 // very high because V8 and possibly others will reserve memory ranges and
269 return false; 284 // rely on on-demand paging for allocation. Unfortunately, even
270 // Make sure we don't raise the existing limit. 285 // MADV_DONTNEED ranges count towards RLIMIT_AS so this is not an option.
Chris Evans 2013/01/15 09:07:56 Also, we believe this is a good defence to a know
jln (very slow on Chromium) 2013/01/15 19:44:19 This should probably be captured in the bug. I add
271 const struct rlimit new_address_space_limit = { 286 const rlim_t kNewAddressSpaceMaxSize = 1L << 34;
272 std::min(old_address_space_limit.rlim_cur, kNewAddressSpaceMaxSize), 287 #else
273 std::min(old_address_space_limit.rlim_max, kNewAddressSpaceMaxSize) 288 // On 32 bits, enforce the 4GB limit. On a 64 bits kernel, this could
274 }; 289 // prevent far calling to 64 bits and abuse the memory allocator to exploit
275 int rc = setrlimit(RLIMIT_AS, &new_address_space_limit); 290 // a kernel vulnerability.
276 return (rc == 0); 291 const rlim_t kNewAddressSpaceMaxSize = std::numeric_limits<uint32_t>::max();
292 #endif // defined(__x86_64__)
293 // On all platforms, add a limit to the brk() heap that would prevent
294 // allocations that can't be index by an int.
295 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max();
296
297 bool limited_as = AddResourceLimit(RLIMIT_AS, kNewAddressSpaceMaxSize);
298 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize);
Chris Evans 2013/01/15 09:07:56 I wonder if this causes a subtle interaction? Real
jln (very slow on Chromium) 2013/01/15 19:44:19 In release mode we try the brk allocator first, th
299 return limited_as && limited_data;
277 #else 300 #else
278 return false; 301 return false;
279 #endif // __x86_64__ && !defined(ADDRESS_SANITIZER) 302 #endif // !defined(ADDRESS_SANITIZER)
280 } 303 }
281 304
282 } // namespace content 305 } // namespace content
283 306
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698