| 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/trap.h" | 5 #include "sandbox/linux/seccomp-bpf/trap.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <signal.h> | 8 #include <signal.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <sys/syscall.h> | 10 #include <sys/syscall.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <limits> | 13 #include <limits> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" | 17 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" |
| 18 #include "sandbox/linux/seccomp-bpf/die.h" | 18 #include "sandbox/linux/seccomp-bpf/die.h" |
| 19 #include "sandbox/linux/seccomp-bpf/syscall.h" | 19 #include "sandbox/linux/seccomp-bpf/syscall.h" |
| 20 #include "sandbox/linux/services/syscall_wrappers.h" |
| 20 #include "sandbox/linux/system_headers/linux_seccomp.h" | 21 #include "sandbox/linux/system_headers/linux_seccomp.h" |
| 21 | 22 #include "sandbox/linux/system_headers/linux_signal.h" |
| 22 // Android's signal.h doesn't define ucontext etc. | |
| 23 #if defined(OS_ANDROID) | |
| 24 #include "sandbox/linux/system_headers/android_ucontext.h" | |
| 25 #endif | |
| 26 | 23 |
| 27 namespace { | 24 namespace { |
| 28 | 25 |
| 29 struct arch_sigsys { | 26 struct arch_sigsys { |
| 30 void* ip; | 27 void* ip; |
| 31 int nr; | 28 int nr; |
| 32 unsigned int arch; | 29 unsigned int arch; |
| 33 }; | 30 }; |
| 34 | 31 |
| 35 const int kCapacityIncrement = 20; | 32 const int kCapacityIncrement = 20; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 46 // a lot of complexity. Instead, we co-opt one bit in the signal mask. | 43 // a lot of complexity. Instead, we co-opt one bit in the signal mask. |
| 47 // If BUS is blocked, we assume that we have been called recursively. | 44 // If BUS is blocked, we assume that we have been called recursively. |
| 48 // There is a possibility for collision with other code that needs to do | 45 // There is a possibility for collision with other code that needs to do |
| 49 // this, but in practice the risks are low. | 46 // this, but in practice the risks are low. |
| 50 // If SIGBUS turns out to be a problem, we could instead co-opt one of the | 47 // If SIGBUS turns out to be a problem, we could instead co-opt one of the |
| 51 // realtime signals. There are plenty of them. Unfortunately, there is no | 48 // realtime signals. There are plenty of them. Unfortunately, there is no |
| 52 // way to mark a signal as allocated. So, the potential for collision is | 49 // way to mark a signal as allocated. So, the potential for collision is |
| 53 // possibly even worse. | 50 // possibly even worse. |
| 54 bool GetIsInSigHandler(const ucontext_t* ctx) { | 51 bool GetIsInSigHandler(const ucontext_t* ctx) { |
| 55 // Note: on Android, sigismember does not take a pointer to const. | 52 // Note: on Android, sigismember does not take a pointer to const. |
| 56 return sigismember(const_cast<sigset_t*>(&ctx->uc_sigmask), SIGBUS); | 53 return sigismember(const_cast<sigset_t*>(&ctx->uc_sigmask), LINUX_SIGBUS); |
| 57 } | 54 } |
| 58 | 55 |
| 59 void SetIsInSigHandler() { | 56 void SetIsInSigHandler() { |
| 60 sigset_t mask; | 57 sigset_t mask; |
| 61 if (sigemptyset(&mask) || sigaddset(&mask, SIGBUS) || | 58 if (sigemptyset(&mask) || sigaddset(&mask, LINUX_SIGBUS) || |
| 62 sigprocmask(SIG_BLOCK, &mask, NULL)) { | 59 sandbox::sys_sigprocmask(LINUX_SIG_BLOCK, &mask, NULL)) { |
| 63 SANDBOX_DIE("Failed to block SIGBUS"); | 60 SANDBOX_DIE("Failed to block SIGBUS"); |
| 64 } | 61 } |
| 65 } | 62 } |
| 66 | 63 |
| 67 bool IsDefaultSignalAction(const struct sigaction& sa) { | 64 bool IsDefaultSignalAction(const struct sigaction& sa) { |
| 68 if (sa.sa_flags & SA_SIGINFO || sa.sa_handler != SIG_DFL) { | 65 if (sa.sa_flags & SA_SIGINFO || sa.sa_handler != SIG_DFL) { |
| 69 return false; | 66 return false; |
| 70 } | 67 } |
| 71 return true; | 68 return true; |
| 72 } | 69 } |
| 73 | 70 |
| 74 } // namespace | 71 } // namespace |
| 75 | 72 |
| 76 namespace sandbox { | 73 namespace sandbox { |
| 77 | 74 |
| 78 Trap::Trap() | 75 Trap::Trap() |
| 79 : trap_array_(NULL), | 76 : trap_array_(NULL), |
| 80 trap_array_size_(0), | 77 trap_array_size_(0), |
| 81 trap_array_capacity_(0), | 78 trap_array_capacity_(0), |
| 82 has_unsafe_traps_(false) { | 79 has_unsafe_traps_(false) { |
| 83 // Set new SIGSYS handler | 80 // Set new SIGSYS handler |
| 84 struct sigaction sa = {}; | 81 struct sigaction sa = {}; |
| 85 sa.sa_sigaction = SigSysAction; | 82 // In some toolchain, sa_sigaction is not declared in struct sigaction. |
| 86 sa.sa_flags = SA_SIGINFO | SA_NODEFER; | 83 // So, here cast the pointer to the sa_handler's type. This works because |
| 87 struct sigaction old_sa; | 84 // |sa_handler| and |sa_sigaction| shares the same memory. |
| 88 if (sigaction(SIGSYS, &sa, &old_sa) < 0) { | 85 sa.sa_handler = reinterpret_cast<void (*)(int)>(SigSysAction); |
| 86 sa.sa_flags = LINUX_SA_SIGINFO | LINUX_SA_NODEFER; |
| 87 struct sigaction old_sa = {}; |
| 88 if (sys_sigaction(LINUX_SIGSYS, &sa, &old_sa) < 0) { |
| 89 SANDBOX_DIE("Failed to configure SIGSYS handler"); | 89 SANDBOX_DIE("Failed to configure SIGSYS handler"); |
| 90 } | 90 } |
| 91 | 91 |
| 92 if (!IsDefaultSignalAction(old_sa)) { | 92 if (!IsDefaultSignalAction(old_sa)) { |
| 93 static const char kExistingSIGSYSMsg[] = | 93 static const char kExistingSIGSYSMsg[] = |
| 94 "Existing signal handler when trying to install SIGSYS. SIGSYS needs " | 94 "Existing signal handler when trying to install SIGSYS. SIGSYS needs " |
| 95 "to be reserved for seccomp-bpf."; | 95 "to be reserved for seccomp-bpf."; |
| 96 DLOG(FATAL) << kExistingSIGSYSMsg; | 96 DLOG(FATAL) << kExistingSIGSYSMsg; |
| 97 LOG(ERROR) << kExistingSIGSYSMsg; | 97 LOG(ERROR) << kExistingSIGSYSMsg; |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Unmask SIGSYS | 100 // Unmask SIGSYS |
| 101 sigset_t mask; | 101 sigset_t mask; |
| 102 if (sigemptyset(&mask) || sigaddset(&mask, SIGSYS) || | 102 if (sigemptyset(&mask) || sigaddset(&mask, LINUX_SIGSYS) || |
| 103 sigprocmask(SIG_UNBLOCK, &mask, NULL)) { | 103 sys_sigprocmask(LINUX_SIG_UNBLOCK, &mask, NULL)) { |
| 104 SANDBOX_DIE("Failed to configure SIGSYS handler"); | 104 SANDBOX_DIE("Failed to configure SIGSYS handler"); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 bpf_dsl::TrapRegistry* Trap::Registry() { | 108 bpf_dsl::TrapRegistry* Trap::Registry() { |
| 109 // Note: This class is not thread safe. It is the caller's responsibility | 109 // Note: This class is not thread safe. It is the caller's responsibility |
| 110 // to avoid race conditions. Normally, this is a non-issue as the sandbox | 110 // to avoid race conditions. Normally, this is a non-issue as the sandbox |
| 111 // can only be initialized if there are no other threads present. | 111 // can only be initialized if there are no other threads present. |
| 112 // Also, this is not a normal singleton. Once created, the global trap | 112 // Also, this is not a normal singleton. Once created, the global trap |
| 113 // object must never be destroyed again. | 113 // object must never be destroyed again. |
| 114 if (!global_trap_) { | 114 if (!global_trap_) { |
| 115 global_trap_ = new Trap(); | 115 global_trap_ = new Trap(); |
| 116 if (!global_trap_) { | 116 if (!global_trap_) { |
| 117 SANDBOX_DIE("Failed to allocate global trap handler"); | 117 SANDBOX_DIE("Failed to allocate global trap handler"); |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 return global_trap_; | 120 return global_trap_; |
| 121 } | 121 } |
| 122 | 122 |
| 123 void Trap::SigSysAction(int nr, siginfo_t* info, void* void_context) { | 123 void Trap::SigSysAction(int nr, LinuxSigInfo* info, void* void_context) { |
| 124 if (!global_trap_) { | 124 if (!global_trap_) { |
| 125 RAW_SANDBOX_DIE( | 125 RAW_SANDBOX_DIE( |
| 126 "This can't happen. Found no global singleton instance " | 126 "This can't happen. Found no global singleton instance " |
| 127 "for Trap() handling."); | 127 "for Trap() handling."); |
| 128 } | 128 } |
| 129 global_trap_->SigSys(nr, info, void_context); | 129 global_trap_->SigSys(nr, info, void_context); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void Trap::SigSys(int nr, siginfo_t* info, void* void_context) { | 132 void Trap::SigSys(int nr, LinuxSigInfo* info, void* void_context) { |
| 133 // Signal handlers should always preserve "errno". Otherwise, we could | 133 // Signal handlers should always preserve "errno". Otherwise, we could |
| 134 // trigger really subtle bugs. | 134 // trigger really subtle bugs. |
| 135 const int old_errno = errno; | 135 const int old_errno = errno; |
| 136 | 136 |
| 137 // Various sanity checks to make sure we actually received a signal | 137 // Various sanity checks to make sure we actually received a signal |
| 138 // triggered by a BPF filter. If something else triggered SIGSYS | 138 // triggered by a BPF filter. If something else triggered SIGSYS |
| 139 // (e.g. kill()), there is really nothing we can do with this signal. | 139 // (e.g. kill()), there is really nothing we can do with this signal. |
| 140 if (nr != SIGSYS || info->si_code != SYS_SECCOMP || !void_context || | 140 if (nr != LINUX_SIGSYS || info->si_code != SYS_SECCOMP || !void_context || |
| 141 info->si_errno <= 0 || | 141 info->si_errno <= 0 || |
| 142 static_cast<size_t>(info->si_errno) > trap_array_size_) { | 142 static_cast<size_t>(info->si_errno) > trap_array_size_) { |
| 143 // ATI drivers seem to send SIGSYS, so this cannot be FATAL. | 143 // ATI drivers seem to send SIGSYS, so this cannot be FATAL. |
| 144 // See crbug.com/178166. | 144 // See crbug.com/178166. |
| 145 // TODO(jln): add a DCHECK or move back to FATAL. | 145 // TODO(jln): add a DCHECK or move back to FATAL. |
| 146 RAW_LOG(ERROR, "Unexpected SIGSYS received."); | 146 RAW_LOG(ERROR, "Unexpected SIGSYS received."); |
| 147 errno = old_errno; | 147 errno = old_errno; |
| 148 return; | 148 return; |
| 149 } | 149 } |
| 150 | 150 |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 "CHROME_SANDBOX_DEBUGGING is turned on first"); | 372 "CHROME_SANDBOX_DEBUGGING is turned on first"); |
| 373 } | 373 } |
| 374 } | 374 } |
| 375 // Returns the, possibly updated, value of has_unsafe_traps_. | 375 // Returns the, possibly updated, value of has_unsafe_traps_. |
| 376 return has_unsafe_traps_; | 376 return has_unsafe_traps_; |
| 377 } | 377 } |
| 378 | 378 |
| 379 Trap* Trap::global_trap_; | 379 Trap* Trap::global_trap_; |
| 380 | 380 |
| 381 } // namespace sandbox | 381 } // namespace sandbox |
| OLD | NEW |