| 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/compiler_specific.h" |
| 15 #include "base/logging.h" | 16 #include "base/logging.h" |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 17 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" | 18 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" |
| 18 #include "sandbox/linux/seccomp-bpf/die.h" | 19 #include "sandbox/linux/seccomp-bpf/die.h" |
| 19 #include "sandbox/linux/seccomp-bpf/syscall.h" | 20 #include "sandbox/linux/seccomp-bpf/syscall.h" |
| 20 #include "sandbox/linux/services/syscall_wrappers.h" | 21 #include "sandbox/linux/services/syscall_wrappers.h" |
| 21 #include "sandbox/linux/system_headers/linux_seccomp.h" | 22 #include "sandbox/linux/system_headers/linux_seccomp.h" |
| 22 #include "sandbox/linux/system_headers/linux_signal.h" | 23 #include "sandbox/linux/system_headers/linux_signal.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 if (!global_trap_) { | 115 if (!global_trap_) { |
| 115 global_trap_ = new Trap(); | 116 global_trap_ = new Trap(); |
| 116 if (!global_trap_) { | 117 if (!global_trap_) { |
| 117 SANDBOX_DIE("Failed to allocate global trap handler"); | 118 SANDBOX_DIE("Failed to allocate global trap handler"); |
| 118 } | 119 } |
| 119 } | 120 } |
| 120 return global_trap_; | 121 return global_trap_; |
| 121 } | 122 } |
| 122 | 123 |
| 123 void Trap::SigSysAction(int nr, LinuxSigInfo* info, void* void_context) { | 124 void Trap::SigSysAction(int nr, LinuxSigInfo* info, void* void_context) { |
| 125 if (info) { |
| 126 MSAN_UNPOISON(info, sizeof(*info)); |
| 127 } |
| 128 |
| 129 // Obtain the signal context. This, most notably, gives us access to |
| 130 // all CPU registers at the time of the signal. |
| 131 ucontext_t* ctx = reinterpret_cast<ucontext_t*>(void_context); |
| 132 if (ctx) { |
| 133 MSAN_UNPOISON(ctx, sizeof(*ctx)); |
| 134 } |
| 135 |
| 124 if (!global_trap_) { | 136 if (!global_trap_) { |
| 125 RAW_SANDBOX_DIE( | 137 RAW_SANDBOX_DIE( |
| 126 "This can't happen. Found no global singleton instance " | 138 "This can't happen. Found no global singleton instance " |
| 127 "for Trap() handling."); | 139 "for Trap() handling."); |
| 128 } | 140 } |
| 129 global_trap_->SigSys(nr, info, void_context); | 141 global_trap_->SigSys(nr, info, ctx); |
| 130 } | 142 } |
| 131 | 143 |
| 132 void Trap::SigSys(int nr, LinuxSigInfo* info, void* void_context) { | 144 void Trap::SigSys(int nr, LinuxSigInfo* info, ucontext_t* ctx) { |
| 133 // Signal handlers should always preserve "errno". Otherwise, we could | 145 // Signal handlers should always preserve "errno". Otherwise, we could |
| 134 // trigger really subtle bugs. | 146 // trigger really subtle bugs. |
| 135 const int old_errno = errno; | 147 const int old_errno = errno; |
| 136 | 148 |
| 137 // Various sanity checks to make sure we actually received a signal | 149 // Various sanity checks to make sure we actually received a signal |
| 138 // triggered by a BPF filter. If something else triggered SIGSYS | 150 // triggered by a BPF filter. If something else triggered SIGSYS |
| 139 // (e.g. kill()), there is really nothing we can do with this signal. | 151 // (e.g. kill()), there is really nothing we can do with this signal. |
| 140 if (nr != LINUX_SIGSYS || info->si_code != SYS_SECCOMP || !void_context || | 152 if (nr != LINUX_SIGSYS || info->si_code != SYS_SECCOMP || !ctx || |
| 141 info->si_errno <= 0 || | 153 info->si_errno <= 0 || |
| 142 static_cast<size_t>(info->si_errno) > trap_array_size_) { | 154 static_cast<size_t>(info->si_errno) > trap_array_size_) { |
| 143 // ATI drivers seem to send SIGSYS, so this cannot be FATAL. | 155 // ATI drivers seem to send SIGSYS, so this cannot be FATAL. |
| 144 // See crbug.com/178166. | 156 // See crbug.com/178166. |
| 145 // TODO(jln): add a DCHECK or move back to FATAL. | 157 // TODO(jln): add a DCHECK or move back to FATAL. |
| 146 RAW_LOG(ERROR, "Unexpected SIGSYS received."); | 158 RAW_LOG(ERROR, "Unexpected SIGSYS received."); |
| 147 errno = old_errno; | 159 errno = old_errno; |
| 148 return; | 160 return; |
| 149 } | 161 } |
| 150 | 162 |
| 151 // Obtain the signal context. This, most notably, gives us access to | |
| 152 // all CPU registers at the time of the signal. | |
| 153 ucontext_t* ctx = reinterpret_cast<ucontext_t*>(void_context); | |
| 154 | 163 |
| 155 // Obtain the siginfo information that is specific to SIGSYS. Unfortunately, | 164 // Obtain the siginfo information that is specific to SIGSYS. Unfortunately, |
| 156 // most versions of glibc don't include this information in siginfo_t. So, | 165 // most versions of glibc don't include this information in siginfo_t. So, |
| 157 // we need to explicitly copy it into a arch_sigsys structure. | 166 // we need to explicitly copy it into a arch_sigsys structure. |
| 158 struct arch_sigsys sigsys; | 167 struct arch_sigsys sigsys; |
| 159 memcpy(&sigsys, &info->_sifields, sizeof(sigsys)); | 168 memcpy(&sigsys, &info->_sifields, sizeof(sigsys)); |
| 160 | 169 |
| 161 #if defined(__mips__) | 170 #if defined(__mips__) |
| 162 // When indirect syscall (syscall(__NR_foo, ...)) is made on Mips, the | 171 // When indirect syscall (syscall(__NR_foo, ...)) is made on Mips, the |
| 163 // number in register SECCOMP_SYSCALL(ctx) is always __NR_syscall and the | 172 // number in register SECCOMP_SYSCALL(ctx) is always __NR_syscall and the |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 "CHROME_SANDBOX_DEBUGGING is turned on first"); | 381 "CHROME_SANDBOX_DEBUGGING is turned on first"); |
| 373 } | 382 } |
| 374 } | 383 } |
| 375 // Returns the, possibly updated, value of has_unsafe_traps_. | 384 // Returns the, possibly updated, value of has_unsafe_traps_. |
| 376 return has_unsafe_traps_; | 385 return has_unsafe_traps_; |
| 377 } | 386 } |
| 378 | 387 |
| 379 Trap* Trap::global_trap_; | 388 Trap* Trap::global_trap_; |
| 380 | 389 |
| 381 } // namespace sandbox | 390 } // namespace sandbox |
| OLD | NEW |