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/common/seccomp_sandbox.h" | |
6 #include "content/public/common/sandbox_init.h" | |
7 | |
8 #if defined(__i386__) || defined(__x86_64__) | |
9 | |
10 // This is an assert for GYP | |
11 #if !defined(OS_LINUX) | |
12 #error "Linux specific file compiled on non Linux OS!" | |
13 #endif | |
14 | |
15 #include <asm/unistd.h> | 5 #include <asm/unistd.h> |
16 #include <dlfcn.h> | 6 #include <dlfcn.h> |
17 #include <errno.h> | 7 #include <errno.h> |
18 #include <fcntl.h> | 8 #include <fcntl.h> |
19 #include <linux/audit.h> | 9 #include <linux/audit.h> |
20 #include <linux/filter.h> | 10 #include <linux/filter.h> |
21 #include <signal.h> | 11 #include <signal.h> |
22 #include <string.h> | 12 #include <string.h> |
23 #include <sys/prctl.h> | 13 #include <sys/prctl.h> |
24 #include <sys/stat.h> | 14 #include <sys/stat.h> |
25 #include <sys/types.h> | 15 #include <sys/types.h> |
26 #include <ucontext.h> | 16 #include <ucontext.h> |
27 #include <unistd.h> | 17 #include <unistd.h> |
28 | 18 |
29 #include <vector> | 19 #include <vector> |
30 | 20 |
31 #include "base/command_line.h" | 21 #include "base/command_line.h" |
32 #include "base/file_util.h" | |
33 #include "base/logging.h" | 22 #include "base/logging.h" |
34 #include "base/time.h" | |
35 #include "content/common/sandbox_linux.h" | 23 #include "content/common/sandbox_linux.h" |
| 24 #include "content/common/sandbox_seccomp_bpf_linux.h" |
36 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
| 26 |
| 27 // These are the only architectures supported for now. |
| 28 #if defined(__i386__) || defined(__x86_64__) |
| 29 #define SECCOMP_BPF_SANDBOX |
| 30 #endif |
| 31 |
| 32 #if defined(SECCOMP_BPF_SANDBOX) |
37 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 33 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
38 | 34 |
39 // These are fairly new and not defined in all headers yet. | 35 // These are fairly new and not defined in all headers yet. |
40 #if defined(__x86_64__) | 36 #if defined(__x86_64__) |
41 | 37 |
42 #ifndef __NR_process_vm_readv | 38 #ifndef __NR_process_vm_readv |
43 #define __NR_process_vm_readv 310 | 39 #define __NR_process_vm_readv 310 |
44 #endif | 40 #endif |
45 | 41 |
46 #ifndef __NR_process_vm_writev | 42 #ifndef __NR_process_vm_writev |
47 #define __NR_process_vm_writev 311 | 43 #define __NR_process_vm_writev 311 |
48 #endif | 44 #endif |
49 | 45 |
50 #elif defined(__i386__) | 46 #elif defined(__i386__) |
51 | 47 |
52 #ifndef __NR_process_vm_readv | 48 #ifndef __NR_process_vm_readv |
53 #define __NR_process_vm_readv 347 | 49 #define __NR_process_vm_readv 347 |
54 #endif | 50 #endif |
55 | 51 |
56 #ifndef __NR_process_vm_writev | 52 #ifndef __NR_process_vm_writev |
57 #define __NR_process_vm_writev 348 | 53 #define __NR_process_vm_writev 348 |
58 #endif | 54 #endif |
59 | 55 |
60 #endif | 56 #endif |
61 | 57 |
62 namespace { | 58 namespace { |
63 | 59 |
64 bool IsSingleThreaded() { | |
65 // Possibly racy, but it's ok because this is more of a debug check to catch | |
66 // new threaded situations arising during development. | |
67 int num_threads = | |
68 file_util::CountFilesCreatedAfter(FilePath("/proc/self/task"), | |
69 base::Time::UnixEpoch()); | |
70 | |
71 // We pass the test if we don't know ( == 0), because the setuid sandbox | |
72 // will prevent /proc access in some contexts. | |
73 return num_threads == 1 || num_threads == 0; | |
74 } | |
75 | |
76 inline bool IsChromeOS() { | 60 inline bool IsChromeOS() { |
77 #if defined(OS_CHROMEOS) | 61 #if defined(OS_CHROMEOS) |
78 return true; | 62 return true; |
79 #else | 63 #else |
80 return false; | 64 return false; |
81 #endif | 65 #endif |
82 } | 66 } |
83 | 67 |
84 void LogSandboxStarted(const std::string& sandbox_name, | 68 void LogSandboxStarted(const std::string& sandbox_name, |
85 const std::string& process_type) { | 69 const std::string& process_type) { |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 "/usr/lib64/va/drivers/i965_drv_video.so"; | 420 "/usr/lib64/va/drivers/i965_drv_video.so"; |
437 dlopen(kI965DrvVideoPath_64, RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE); | 421 dlopen(kI965DrvVideoPath_64, RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE); |
438 } | 422 } |
439 } | 423 } |
440 #endif | 424 #endif |
441 } | 425 } |
442 | 426 |
443 // Is the sandbox fully disabled for this process? | 427 // Is the sandbox fully disabled for this process? |
444 bool ShouldDisableBpfSandbox(const CommandLine& command_line, | 428 bool ShouldDisableBpfSandbox(const CommandLine& command_line, |
445 const std::string& process_type) { | 429 const std::string& process_type) { |
446 if (command_line.HasSwitch(switches::kNoSandbox) || | |
447 command_line.HasSwitch(switches::kDisableSeccompFilterSandbox)) { | |
448 return true; | |
449 } | |
450 | |
451 if (process_type == switches::kGpuProcess) { | 430 if (process_type == switches::kGpuProcess) { |
452 // The GPU sandbox is disabled by default in ChromeOS, enabled by default on | 431 // The GPU sandbox is disabled by default in ChromeOS, enabled by default on |
453 // generic Linux. | 432 // generic Linux. |
454 // TODO(jorgelo): when we feel comfortable, make this a policy decision | 433 // TODO(jorgelo): when we feel comfortable, make this a policy decision |
455 // instead. (i.e. move this to GetProcessSyscallPolicy) and return an | 434 // instead. (i.e. move this to GetProcessSyscallPolicy) and return an |
456 // AllowAllPolicy for lack of "--enable-gpu-sandbox". | 435 // AllowAllPolicy for lack of "--enable-gpu-sandbox". |
457 bool should_disable; | 436 bool should_disable; |
458 if (IsChromeOS()) { | 437 if (IsChromeOS()) { |
459 should_disable = true; | 438 should_disable = true; |
460 } else { | 439 } else { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 // This will be our default if we need one. | 472 // This will be our default if we need one. |
494 return AllowAllPolicy; | 473 return AllowAllPolicy; |
495 #else | 474 #else |
496 // On IA32, we only have a small blacklist at the moment. | 475 // On IA32, we only have a small blacklist at the moment. |
497 (void) process_type; | 476 (void) process_type; |
498 return BlacklistPtracePolicy; | 477 return BlacklistPtracePolicy; |
499 #endif // __x86_64__ | 478 #endif // __x86_64__ |
500 } | 479 } |
501 | 480 |
502 // Initialize the seccomp-bpf sandbox. | 481 // Initialize the seccomp-bpf sandbox. |
503 bool InitializeBpfSandbox_x86(const CommandLine& command_line, | 482 bool StartBpfSandbox_x86(const CommandLine& command_line, |
504 const std::string& process_type) { | 483 const std::string& process_type) { |
505 if (ShouldDisableBpfSandbox(command_line, process_type)) | |
506 return false; | |
507 | |
508 // No matter what, InitializeSandbox() should always be called before threads | |
509 // are started. | |
510 // Note: IsSingleThreaded() will be true if /proc is not accessible! | |
511 if (!IsSingleThreaded()) { | |
512 std::string error_message = "InitializeSandbox() called with multiple " | |
513 "threads in process " + process_type; | |
514 // TODO(jln): change this into a CHECK() once we are more comfortable it | |
515 // does not trigger. | |
516 // On non-DEBUG build, we still log an error | |
517 LOG(ERROR) << error_message; | |
518 return false; | |
519 } | |
520 | |
521 // TODO(jln): find a way for the Zygote processes under the setuid sandbox to | |
522 // have a /proc fd and pass it here. | |
523 // Passing -1 as the /proc fd since we have no special way to have it for | |
524 // now. | |
525 if (playground2::Sandbox::supportsSeccompSandbox(-1) != | |
526 playground2::Sandbox::STATUS_AVAILABLE) { | |
527 return false; | |
528 } | |
529 | |
530 playground2::Sandbox::EvaluateSyscall SyscallPolicy = | 484 playground2::Sandbox::EvaluateSyscall SyscallPolicy = |
531 GetProcessSyscallPolicy(command_line, process_type); | 485 GetProcessSyscallPolicy(command_line, process_type); |
532 | 486 |
533 // Warms up resources needed by the policy we're about to enable. | 487 // Warms up resources needed by the policy we're about to enable. |
534 WarmupPolicy(SyscallPolicy); | 488 WarmupPolicy(SyscallPolicy); |
535 | 489 |
536 playground2::Sandbox::setSandboxPolicy(SyscallPolicy, NULL); | 490 playground2::Sandbox::setSandboxPolicy(SyscallPolicy, NULL); |
537 playground2::Sandbox::startSandbox(); | 491 playground2::Sandbox::startSandbox(); |
538 | 492 |
539 return true; | 493 return true; |
540 } | 494 } |
541 | 495 |
542 } // anonymous namespace | 496 } // namespace |
543 | 497 |
544 #endif // defined(__i386__) || defined(__x86_64__) | 498 #endif // SECCOMP_BPF_SANDBOX |
545 | 499 |
546 namespace content { | 500 namespace content { |
547 | 501 |
548 void InitializeSandbox() { | 502 // Is seccomp BPF globally enabled? |
549 #if defined(__i386__) || defined(__x86_64__) | 503 bool SandboxSeccompBpf::IsSeccompBpfDesired() { |
550 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 504 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
551 const std::string process_type = | 505 if (!command_line.HasSwitch(switches::kNoSandbox) && |
552 command_line.GetSwitchValueASCII(switches::kProcessType); | 506 !command_line.HasSwitch(switches::kDisableSeccompFilterSandbox)) { |
553 bool seccomp_legacy_started = false; | 507 return true; |
554 bool seccomp_bpf_started = false; | 508 } else { |
| 509 return false; |
| 510 } |
| 511 } |
555 | 512 |
556 // First, try to enable seccomp-legacy. | 513 bool SandboxSeccompBpf::SupportsSandbox() { |
557 seccomp_legacy_started = | 514 #if defined(SECCOMP_BPF_SANDBOX) |
558 LinuxSandbox::GetInstance()->StartSeccompLegacy(process_type); | 515 // TODO(jln): pass the saved proc_fd_ from the LinuxSandbox singleton |
559 if (seccomp_legacy_started) | 516 // here. |
560 LogSandboxStarted("seccomp-legacy", process_type); | 517 if (playground2::Sandbox::supportsSeccompSandbox(-1) == |
| 518 playground2::Sandbox::STATUS_AVAILABLE) { |
| 519 return true; |
| 520 } |
| 521 #endif |
| 522 return false; |
| 523 } |
561 | 524 |
562 // Then, try to enable seccomp-bpf. | 525 bool SandboxSeccompBpf::StartSandbox(const std::string& process_type) { |
563 // If seccomp-legacy is enabled, seccomp-bpf initialization will crash | 526 #if defined(SECCOMP_BPF_SANDBOX) |
564 // instead of failing gracefully. | 527 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
565 // TODO(markus): fix this (crbug.com/139872). | 528 |
566 if (!seccomp_legacy_started) { | 529 if (IsSeccompBpfDesired() && // Global switches policy. |
567 seccomp_bpf_started = | 530 // Process-specific policy. |
568 InitializeBpfSandbox_x86(command_line, process_type); | 531 !ShouldDisableBpfSandbox(command_line, process_type) && |
| 532 SupportsSandbox()) { |
| 533 return StartBpfSandbox_x86(command_line, process_type); |
569 } | 534 } |
570 if (seccomp_bpf_started) | |
571 LogSandboxStarted("seccomp-bpf", process_type); | |
572 #endif | 535 #endif |
| 536 return false; |
573 } | 537 } |
574 | 538 |
575 } // namespace content | 539 } // namespace content |
OLD | NEW |