| 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 "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 5 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 6 | 6 |
| 7 // Some headers on Android are missing cdefs: crbug.com/172337. | 7 // Some headers on Android are missing cdefs: crbug.com/172337. |
| 8 // (We can't use OS_ANDROID here since build_config.h is not included). | 8 // (We can't use OS_ANDROID here since build_config.h is not included). |
| 9 #if defined(ANDROID) | 9 #if defined(ANDROID) |
| 10 #include <sys/cdefs.h> | 10 #include <sys/cdefs.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <errno.h> | 13 #include <errno.h> |
| 14 #include <linux/filter.h> | |
| 15 #include <sys/prctl.h> | 14 #include <sys/prctl.h> |
| 16 #include <sys/types.h> | 15 #include <sys/types.h> |
| 17 #include <unistd.h> | 16 #include <unistd.h> |
| 18 | 17 |
| 19 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 20 #include "base/files/scoped_file.h" | 19 #include "base/files/scoped_file.h" |
| 21 #include "base/logging.h" | 20 #include "base/logging.h" |
| 22 #include "base/macros.h" | 21 #include "base/macros.h" |
| 23 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
| 24 #include "base/posix/eintr_wrapper.h" | 23 #include "base/posix/eintr_wrapper.h" |
| 25 #include "base/third_party/valgrind/valgrind.h" | 24 #include "base/third_party/valgrind/valgrind.h" |
| 26 #include "sandbox/linux/bpf_dsl/codegen.h" | 25 #include "sandbox/linux/bpf_dsl/codegen.h" |
| 27 #include "sandbox/linux/bpf_dsl/policy.h" | 26 #include "sandbox/linux/bpf_dsl/policy.h" |
| 28 #include "sandbox/linux/bpf_dsl/policy_compiler.h" | 27 #include "sandbox/linux/bpf_dsl/policy_compiler.h" |
| 29 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" | 28 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" |
| 30 #include "sandbox/linux/bpf_dsl/syscall_set.h" | 29 #include "sandbox/linux/bpf_dsl/syscall_set.h" |
| 31 #include "sandbox/linux/seccomp-bpf/die.h" | 30 #include "sandbox/linux/seccomp-bpf/die.h" |
| 32 #include "sandbox/linux/seccomp-bpf/syscall.h" | 31 #include "sandbox/linux/seccomp-bpf/syscall.h" |
| 33 #include "sandbox/linux/seccomp-bpf/trap.h" | 32 #include "sandbox/linux/seccomp-bpf/trap.h" |
| 34 #include "sandbox/linux/services/proc_util.h" | 33 #include "sandbox/linux/services/proc_util.h" |
| 35 #include "sandbox/linux/services/syscall_wrappers.h" | 34 #include "sandbox/linux/services/syscall_wrappers.h" |
| 36 #include "sandbox/linux/services/thread_helpers.h" | 35 #include "sandbox/linux/services/thread_helpers.h" |
| 36 #include "sandbox/linux/system_headers/linux_filter.h" |
| 37 #include "sandbox/linux/system_headers/linux_seccomp.h" | 37 #include "sandbox/linux/system_headers/linux_seccomp.h" |
| 38 #include "sandbox/linux/system_headers/linux_syscalls.h" | 38 #include "sandbox/linux/system_headers/linux_syscalls.h" |
| 39 | 39 |
| 40 namespace sandbox { | 40 namespace sandbox { |
| 41 | 41 |
| 42 namespace { | 42 namespace { |
| 43 | 43 |
| 44 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } | 44 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } |
| 45 | 45 |
| 46 bool IsSingleThreaded(int proc_fd) { | 46 bool IsSingleThreaded(int proc_fd) { |
| 47 return ThreadHelpers::IsSingleThreaded(proc_fd); | 47 return ThreadHelpers::IsSingleThreaded(proc_fd); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Check if the kernel supports seccomp-filter (a.k.a. seccomp mode 2) via | 50 // Check if the kernel supports seccomp-filter (a.k.a. seccomp mode 2) via |
| 51 // prctl(). | 51 // prctl(). |
| 52 bool KernelSupportsSeccompBPF() { | 52 bool KernelSupportsSeccompBPF() { |
| 53 errno = 0; | 53 errno = 0; |
| 54 const int rv = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, nullptr); | 54 const int rv = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, nullptr); |
| 55 | 55 |
| 56 if (rv == -1 && EFAULT == errno) { | 56 if (rv == -1 && EFAULT == errno) { |
| 57 return true; | 57 return true; |
| 58 } | 58 } |
| 59 return false; | 59 return false; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // LG introduced a buggy syscall, sys_set_media_ext, with the same number as |
| 63 // seccomp. Return true if the current kernel has this buggy syscall. |
| 64 // |
| 65 // We want this to work with upcoming versions of seccomp, so we pass bogus |
| 66 // flags that are unlikely to ever be used by the kernel. A normal kernel would |
| 67 // return -EINVAL, but a buggy LG kernel would return 1. |
| 68 bool KernelHasLGBug() { |
| 69 #if defined(OS_ANDROID) |
| 70 // sys_set_media will see this as NULL, which should be a safe (non-crashing) |
| 71 // way to invoke it. A genuine seccomp syscall will see it as |
| 72 // SECCOMP_SET_MODE_STRICT. |
| 73 const unsigned int operation = 0; |
| 74 // Chosen by fair dice roll. Guaranteed to be random. |
| 75 const unsigned int flags = 0xf7a46a5c; |
| 76 const int rv = sys_seccomp(operation, flags, nullptr); |
| 77 // A genuine kernel would return -EINVAL (which would set rv to -1 and errno |
| 78 // to EINVAL), or at the very least return some kind of error (which would |
| 79 // set rv to -1). Any other behavior indicates that whatever code received |
| 80 // our syscall was not the real seccomp. |
| 81 if (rv != -1) { |
| 82 return true; |
| 83 } |
| 84 #endif // defined(OS_ANDROID) |
| 85 |
| 86 return false; |
| 87 } |
| 88 |
| 62 // Check if the kernel supports seccomp-filter via the seccomp system call | 89 // Check if the kernel supports seccomp-filter via the seccomp system call |
| 63 // and the TSYNC feature to enable seccomp on all threads. | 90 // and the TSYNC feature to enable seccomp on all threads. |
| 64 bool KernelSupportsSeccompTsync() { | 91 bool KernelSupportsSeccompTsync() { |
| 92 if (KernelHasLGBug()) { |
| 93 return false; |
| 94 } |
| 95 |
| 65 errno = 0; | 96 errno = 0; |
| 66 const int rv = | 97 const int rv = |
| 67 sys_seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, nullptr); | 98 sys_seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, nullptr); |
| 68 | 99 |
| 69 if (rv == -1 && errno == EFAULT) { | 100 if (rv == -1 && errno == EFAULT) { |
| 70 return true; | 101 return true; |
| 71 } else { | 102 } else { |
| 72 // TODO(jln): turn these into DCHECK after 417888 is considered fixed. | 103 // TODO(jln): turn these into DCHECK after 417888 is considered fixed. |
| 73 CHECK_EQ(-1, rv); | 104 CHECK_EQ(-1, rv); |
| 74 CHECK(ENOSYS == errno || EINVAL == errno); | 105 CHECK(ENOSYS == errno || EINVAL == errno); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 } else { | 270 } else { |
| 240 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { | 271 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { |
| 241 SANDBOX_DIE("Kernel refuses to turn on BPF filters"); | 272 SANDBOX_DIE("Kernel refuses to turn on BPF filters"); |
| 242 } | 273 } |
| 243 } | 274 } |
| 244 | 275 |
| 245 sandbox_has_started_ = true; | 276 sandbox_has_started_ = true; |
| 246 } | 277 } |
| 247 | 278 |
| 248 } // namespace sandbox | 279 } // namespace sandbox |
| OLD | NEW |