Index: src/trap-handler/signal-handler.cc |
diff --git a/src/trap-handler/signal-handler.cc b/src/trap-handler/signal-handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..01f3ab1790e86954b273c658f7d721d66c4464f3 |
--- /dev/null |
+++ b/src/trap-handler/signal-handler.cc |
@@ -0,0 +1,197 @@ |
+// Copyright 2016 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// PLEASE READ BEFORE CHANGING THIS FILE! |
+// |
+// This file implements the out of bounds signal handler for |
+// WebAssembly. Signal handlers are notoriously difficult to get |
+// right, and getting it wrong can lead to security |
+// vulnerabilities. In order to minimize this risk, here are some |
+// rules to follow. |
+// |
+// 1. Do not introduce any new external dependencies. This file needs |
+// to be self contained so it is easy to audit everything that a |
+// signal handler might do. |
+// |
+// 2. Any changes must be reviewed by someone from the crash reporting |
+// or security team. |
+ |
+// This file contains all of the code that actually runs in a signal handler |
+// context. |
+ |
+#include <signal.h> |
+#include <stddef.h> |
+#include <stdio.h> |
+#include <stdlib.h> |
+#include <string.h> |
+ |
+#include <atomic> |
+ |
+#include "src/flags.h" |
+#include "src/trap-handler/trap-handler-internal.h" |
+#include "src/trap-handler/trap-handler.h" |
+ |
+#define TRACE(...) |
+ |
+namespace v8 { |
+namespace internal { |
+namespace trap_handler { |
+ |
+extern THREAD_LOCAL bool g_thread_in_wasm_code; |
Mark Seaborn
2017/01/24 07:14:39
Can you put these extern declarations in a header,
Eric Holk
2017/01/26 01:33:36
Done.
|
+ |
+extern size_t gNumCodeObjects; |
+extern CodeProtectionInfo **gCodeObjects; |
Mark Seaborn
2017/01/24 07:14:39
Nit: Presumably this should follow the Chromium st
Eric Holk
2017/01/26 01:33:36
Done.
I was fighting with clangfmt because it was
|
+ |
+MetadataLock::MetadataLock() { |
+ if (g_thread_in_wasm_code) { |
+ abort(); |
+ } |
+ |
+ while (spinlock_.test_and_set(std::memory_order::memory_order_acquire)) { |
+ } |
+} |
+ |
+MetadataLock::~MetadataLock() { |
+ if (g_thread_in_wasm_code) { |
+ abort(); |
+ } |
+ |
+ spinlock_.clear(std::memory_order::memory_order_release); |
+} |
+ |
+namespace { |
+ |
+#if V8_TRAP_HANDLER_SUPPORTED |
+class SigUnmaskStack { |
+ public: |
+ explicit SigUnmaskStack(sigset_t sigs) { |
+ pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_); |
Mark Seaborn
2017/01/24 07:14:39
Can you add a TODO to consider using linux-syscall
Eric Holk
2017/01/26 01:33:36
Done.
|
+ } |
+ |
+ ~SigUnmaskStack() { pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); } |
+ |
+ private: |
+ sigset_t old_mask_; |
+ |
+ SigUnmaskStack(const SigUnmaskStack &) = delete; |
+ void operator=(const SigUnmaskStack &) = delete; |
+}; |
+#endif |
+} // namespace |
+ |
+bool TryHandleFault(void *source_instruction, void *target_data, |
+ void **return_address) { |
+#if V8_TRAP_HANDLER_SUPPORTED |
+ // Ensure the faulting thread was actually running Wasm code. |
+ if (!g_thread_in_wasm_code) { |
+ TRACE("signal handler: Thread not running WASM code; crashing\n"); |
+ return false; |
+ } |
+ |
+ // Clear g_thread_in_wasm_code, primarily to protect against nested faults. |
+ g_thread_in_wasm_code = false; |
+ |
+ // Unmask the signal in case we fault in the signal hander. |
Mark Seaborn
2017/01/24 07:14:39
This could be more explicit. How about: "This mea
Eric Holk
2017/01/26 01:33:36
Done.
|
+ sigset_t sigs; |
+ sigemptyset(&sigs); |
Mark Seaborn
2017/01/24 07:14:39
Happily, sigemptyset() and sigaddset() are declare
Eric Holk
2017/01/26 01:33:37
Done.
|
+ sigaddset(&sigs, SIGSEGV); |
+ sigaddset(&sigs, SIGBUS); |
+ SigUnmaskStack unmask(sigs); |
+ |
+ intptr_t fault_addr = reinterpret_cast<intptr_t>(source_instruction); |
+ |
+ TRACE("Handling fault at %p\n", reinterpret_cast<void *>(fault_addr)); |
+ |
+ // TODO(eholk): broad code range check |
+ |
+ MetadataLock lock_holder; |
+ |
+ for (size_t i = 0; i < gNumCodeObjects; ++i) { |
+ const CodeProtectionInfo *data = gCodeObjects[i]; |
+ if (data == nullptr) { |
+ continue; |
+ } |
+ const intptr_t base = reinterpret_cast<intptr_t>(data->base); |
Mark Seaborn
2017/01/24 07:14:39
Can you use uintptr_t for this, rather than intptr
Eric Holk
2017/01/26 01:33:36
You're right, there's no reason for these to be si
|
+ |
+ TRACE(" Checking range base=%p, length = %zu\n", |
+ reinterpret_cast<void *>(base), data->size); |
+ |
+ if (fault_addr >= base && |
+ fault_addr < base + static_cast<intptr_t>(data->size)) { |
+ // Hurray, we found the code object. Check for protected addresses. |
+ const ptrdiff_t offset = fault_addr - base; |
+ |
+ TRACE("Range found, offset is %td\n", offset); |
+ |
+ for (unsigned i = 0; i < data->num_protected_instructions; ++i) { |
+ TRACE(" Checking instr offset %d, landing offset %d\n", |
+ data->instructions[i].instr_offset, |
+ data->instructions[i].landing_offset); |
+ if (data->instructions[i].instr_offset == offset) { |
+ // Hurray again, we found the actual instruction. Jump to |
+ // the landing pad. |
+ |
+ *return_address = reinterpret_cast<void *>( |
+ data->instructions[i].landing_offset + base); |
+ return true; |
+ } |
+ } |
+ } |
+ } |
+ |
+ // If we get here, it's not a wasm fault, so we go to the next handler. |
Mark Seaborn
2017/01/24 07:14:39
To be more precise, "If we get here, it's not a re
Eric Holk
2017/01/26 01:33:36
Done.
|
+ return false; |
+#else |
+ abort(); |
+#endif |
+} |
+ |
+namespace { |
+ |
+#if V8_TRAP_HANDLER_SUPPORTED |
+void HandleSignal(int signum, siginfo_t *info, void *context) { |
+ // Bail out early in case we got called for the wrong kind of signal. |
+ if (signum != SIGSEGV && signum != SIGBUS) { |
Mark Seaborn
2017/01/24 07:14:39
RegisterDefaultSignalHandler() only registers for
Eric Holk
2017/01/26 01:33:36
I changed it so that RegisterDefaultSignalHandler
Mark Seaborn
2017/01/27 20:17:30
I'd recommend only doing SIGSEGV, because I don't
Mark Mentovai
2017/01/27 20:34:09
Mark Seaborn wrote:
Mark Seaborn
2017/01/27 20:50:51
Those are the kind of things that it would be good
Eric Holk
2017/02/02 18:43:30
I will go with handling just SIGSEGV until we have
|
+ return; |
+ } |
+ |
+ // Make sure the signal was generated by the kernel and not some other source. |
+ if (info->si_code <= 0 || info->si_code == SI_USER || |
Mark Seaborn
2017/01/24 07:14:39
I think these checks should be done by TryHandleFa
Eric Holk
2017/01/26 01:33:36
Doing it this way is the result of some changes Jo
Mark Seaborn
2017/01/27 20:17:30
In the interests of doing things incrementally, yo
Eric Holk
2017/02/02 18:43:30
Jochen suggested that he was okay with having #ifd
|
+ info->si_code == SI_QUEUE || info->si_code == SI_TIMER || |
+ info->si_code == SI_ASYNCIO || info->si_code == SI_MESGQ) { |
+ return; |
+ } |
+ |
+ ucontext_t *uc = reinterpret_cast<ucontext_t *>(context); |
+ |
+ void *source_instruction = |
+ reinterpret_cast<void *>(uc->uc_mcontext.gregs[REG_RIP]); |
+ void *target_data = reinterpret_cast<void *>(info->si_addr); |
Mark Seaborn
2017/01/24 07:14:39
I notice this isn't used yet, so you could remove
Eric Holk
2017/01/26 01:33:36
Assuming the signature for TryHandleFault stay as
Mark Seaborn
2017/01/27 20:17:30
OK, though I'd prefer to remove this variable if i
Eric Holk
2017/02/02 18:43:30
Since this has moved back into TryHandleSignal, I
|
+ |
+ void *return_address; |
+ |
+ if (!TryHandleFault(source_instruction, target_data, &return_address)) { |
+ abort(); |
+ } |
+ |
+ uc->uc_mcontext.gregs[REG_RIP] = reinterpret_cast<intptr_t>(return_address); |
+} |
+#endif |
+} // namespace |
+ |
+bool RegisterDefaultSignalHandler() { |
+#if V8_TRAP_HANDLER_SUPPORTED |
+ struct sigaction action; |
+ action.sa_sigaction = HandleSignal; |
+ action.sa_flags = SA_SIGINFO; |
+ sigemptyset(&action.sa_mask); |
+ return sigaction(SIGSEGV, &action, nullptr) == 0; |
+#else |
+ return false; |
+#endif |
+} |
+ |
+} // namespace trap_handler |
+} // namespace internal |
+} // namespace v8 |