OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // PLEASE READ BEFORE CHANGING THIS FILE! | |
6 // | |
7 // This file implements the out of bounds signal handler for | |
8 // WebAssembly. Signal handlers are notoriously difficult to get | |
9 // right, and getting it wrong can lead to security | |
10 // vulnerabilities. In order to minimize this risk, here are some | |
11 // rules to follow. | |
12 // | |
13 // 1. Do not introduce any new external dependencies. This file needs | |
14 // to be self contained so it is easy to audit everything that a | |
15 // signal handler might do. | |
16 // | |
17 // 2. Any changes must be reviewed by someone from the crash reporting | |
18 // or security team. | |
19 | |
20 // This file contains all of the code that actually runs in a signal handler | |
21 // context. | |
22 | |
23 #include <signal.h> | |
24 #include <stddef.h> | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 #include <atomic> | |
30 | |
31 #include "src/flags.h" | |
32 #include "src/trap-handler/trap-handler-internal.h" | |
33 #include "src/trap-handler/trap-handler.h" | |
34 | |
35 #define TRACE(...) | |
36 // #define TRACE(...) fprintf(stderr, __VA_ARGS__) | |
titzer
2017/01/09 09:26:56
Can you use the PrintF function here?
Eric Holk
2017/01/10 23:10:48
We'd have to include more v8 headers, which increa
| |
37 | |
38 namespace v8 { | |
39 namespace internal { | |
40 namespace trap_handler { | |
41 | |
42 extern __thread bool g_thread_in_wasm_code; | |
43 | |
44 extern size_t gNumCodeObjects; | |
45 extern CodeObjectData **gCodeObjects; | |
46 | |
47 MetadataLock::MetadataLock() { | |
48 if (g_thread_in_wasm_code) { | |
49 abort(); | |
50 } | |
51 | |
52 while (spinlock_.test_and_set(std::memory_order::memory_order_acquire)) { | |
53 } | |
54 } | |
55 | |
56 MetadataLock::~MetadataLock() { | |
57 if (g_thread_in_wasm_code) { | |
58 abort(); | |
59 } | |
60 | |
61 spinlock_.clear(std::memory_order::memory_order_release); | |
62 } | |
63 | |
64 namespace { | |
65 | |
66 class SigUnmaskStack { | |
67 public: | |
68 explicit SigUnmaskStack(int signum) { | |
69 sigset_t sigs; | |
70 sigemptyset(&sigs); | |
71 sigaddset(&sigs, signum); | |
72 pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_); | |
73 } | |
74 | |
75 ~SigUnmaskStack() { pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); } | |
76 | |
77 private: | |
78 sigset_t old_mask_; | |
79 | |
80 SigUnmaskStack(const SigUnmaskStack&) = delete; | |
81 void operator=(const SigUnmaskStack&) = delete; | |
82 }; | |
83 } // namespace | |
84 | |
85 bool MaybeHandleFault(int signum, void *siginfo, void *context) { | |
86 // Bail out early in case we got called for the wrong kind of signal. | |
87 if (signum != SIGSEGV && signum != SIGBUS) { | |
88 return false; | |
89 } | |
90 | |
91 // Make sure the signal was generated by the kernel and not some other source. | |
92 siginfo_t *info = reinterpret_cast<siginfo_t *>(siginfo); | |
93 if (info->si_code <= 0 || info->si_code == SI_USER || | |
94 info->si_code == SI_QUEUE || info->si_code == SI_TIMER || | |
95 info->si_code == SI_ASYNCIO || info->si_code == SI_MESGQ) { | |
96 return false; | |
97 } | |
98 | |
99 // Ensure the faulting thread was actually running Wasm code. | |
100 if (!g_thread_in_wasm_code) { | |
101 TRACE("signal handler: Thread not running WASM code; crashing\n"); | |
102 return false; | |
103 } | |
104 | |
105 // This isn't Wasm code anymore. At this point we were either crash or throw | |
titzer
2017/01/09 09:26:56
Not sure I understand this comment. Does this prot
Eric Holk
2017/01/10 23:10:48
Yes. I changed the comment to reflect that.
I gue
| |
106 // an exception, so we will not be running Wasm code again and there is no | |
107 // need to set the flag again later. | |
108 g_thread_in_wasm_code = false; | |
109 | |
110 // Unmask the signal in case we fault in the signal hander. | |
111 SigUnmaskStack unmask(signum); | |
112 | |
113 ucontext_t *uc = reinterpret_cast<ucontext_t *>(context); | |
114 | |
115 // TODO(eholk): broad code range check | |
116 intptr_t fault_addr = uc->uc_mcontext.gregs[REG_RIP]; | |
117 | |
118 TRACE("Handling fault at %p\n", reinterpret_cast<void *>(fault_addr)); | |
119 | |
120 MetadataLock lock_holder; | |
121 | |
122 for (size_t i = 0; i < gNumCodeObjects; ++i) { | |
123 const CodeObjectData *data = gCodeObjects[i]; | |
124 if (data == nullptr) { | |
125 continue; | |
126 } | |
127 const intptr_t base = reinterpret_cast<intptr_t>(data->base); | |
128 | |
129 TRACE(" Checking range base=%p, length = %zu\n", | |
130 reinterpret_cast<void *>(base), data->size); | |
131 | |
132 if (fault_addr >= base && | |
133 fault_addr < base + static_cast<intptr_t>(data->size)) { | |
134 // Hurray, we found the code object. Check for protected addresses. | |
135 const ptrdiff_t offset = fault_addr - base; | |
136 | |
137 TRACE("Range found, offset is %td\n", offset); | |
138 | |
139 for (unsigned i = 0; i < data->num_protected_instructions; ++i) { | |
140 TRACE(" Checking instr offset %d, landing offset %d\n", | |
141 data->instructions[i].instr_offset, | |
142 data->instructions[i].landing_offset); | |
143 if (data->instructions[i].instr_offset == offset) { | |
144 // Hurray again, we found the actual instruction. Jump to | |
145 // the landing pad. | |
146 | |
147 uc->uc_mcontext.gregs[REG_RIP] = | |
148 data->instructions[i].landing_offset + base; | |
149 return true; | |
150 } | |
151 } | |
152 } | |
153 } | |
154 | |
155 // If we get here, it's not a wasm fault, so we go to the next handler. | |
156 return false; | |
157 } | |
158 } // namespace trap_handler | |
159 } // namespace internal | |
160 } // namespace v8 | |
OLD | NEW |