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> | |
Mark Seaborn
2017/02/09 16:44:36
If we're following our rules about minimising depe
Eric Holk
2017/02/15 02:02:44
Done.
| |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 #include <atomic> | |
30 | |
31 #include "src/flags.h" | |
Mark Seaborn
2017/02/09 16:44:35
We shouldn't have a dependency on that, right? It
Eric Holk
2017/02/15 02:02:44
Done.
| |
32 #include "src/trap-handler/trap-handler-internal.h" | |
33 #include "src/trap-handler/trap-handler.h" | |
34 | |
35 #define TRACE(...) | |
Mark Seaborn
2017/02/09 16:44:36
What is V8's policy on leaving debug logging in co
Eric Holk
2017/02/15 02:02:44
I'm not sure about V8's in specific, but the chrom
| |
36 | |
37 namespace v8 { | |
38 namespace internal { | |
39 namespace trap_handler { | |
40 | |
41 MetadataLock::MetadataLock() { | |
42 if (g_thread_in_wasm_code) { | |
43 abort(); | |
44 } | |
45 | |
46 while (spinlock_.test_and_set(std::memory_order::memory_order_acquire)) { | |
47 } | |
48 } | |
49 | |
50 MetadataLock::~MetadataLock() { | |
51 if (g_thread_in_wasm_code) { | |
52 abort(); | |
53 } | |
54 | |
55 spinlock_.clear(std::memory_order::memory_order_release); | |
56 } | |
57 | |
58 namespace { | |
59 | |
60 #if V8_TRAP_HANDLER_SUPPORTED | |
61 class SigUnmaskStack { | |
62 public: | |
63 explicit SigUnmaskStack(sigset_t sigs) { | |
64 pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_); | |
65 } | |
66 | |
67 ~SigUnmaskStack() { | |
68 // TODO(eholk): consider using linux-syscall-support instead of | |
Mark Seaborn
2017/01/27 20:17:30
Nit: This comment should probably go on the first
Eric Holk
2017/02/02 18:43:30
Done.
| |
69 // pthread_sigmask. | |
70 pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); | |
71 } | |
72 | |
73 private: | |
74 sigset_t old_mask_; | |
75 | |
76 SigUnmaskStack(const SigUnmaskStack&) = delete; | |
Mark Seaborn
2017/02/09 16:44:36
You could comment this with: We'd usually use DISA
Eric Holk
2017/02/15 02:02:44
Done.
| |
77 void operator=(const SigUnmaskStack&) = delete; | |
78 }; | |
79 #endif | |
80 } // namespace | |
81 | |
82 bool TryHandleFault(void* source_instruction, void* target_data, | |
83 void** return_address) { | |
84 #if V8_TRAP_HANDLER_SUPPORTED | |
85 // Ensure the faulting thread was actually running Wasm code. | |
86 if (!g_thread_in_wasm_code) { | |
87 TRACE("signal handler: Thread not running WASM code; crashing\n"); | |
88 return false; | |
89 } | |
90 | |
91 // Clear g_thread_in_wasm_code, primarily to protect against nested faults. | |
92 g_thread_in_wasm_code = false; | |
93 | |
94 // Unmask the signal so that if this signal handler crashes, the crash will be | |
95 // handled by the crash reporter. Otherwise, the process might be killed with | |
96 // the crash going unreported. | |
97 sigset_t sigs; | |
98 // Fortunately, sigemptyset and sigaddset are async-signal-safe according to | |
99 // the POSIX standard. | |
100 sigemptyset(&sigs); | |
101 sigaddset(&sigs, SIGSEGV); | |
102 sigaddset(&sigs, SIGBUS); | |
103 SigUnmaskStack unmask(sigs); | |
104 | |
105 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(source_instruction); | |
106 | |
107 TRACE("Handling fault at %p\n", reinterpret_cast<void*>(fault_addr)); | |
108 | |
109 // TODO(eholk): broad code range check | |
110 | |
111 MetadataLock lock_holder; | |
112 | |
113 for (size_t i = 0; i < gNumCodeObjects; ++i) { | |
114 const CodeProtectionInfo* data = gCodeObjects[i]; | |
115 if (data == nullptr) { | |
116 continue; | |
117 } | |
118 const uintptr_t base = reinterpret_cast<uintptr_t>(data->base); | |
119 | |
120 TRACE(" Checking range base=%p, length = %zu\n", | |
121 reinterpret_cast<void*>(base), data->size); | |
122 | |
123 if (fault_addr >= base && fault_addr < base + data->size) { | |
124 // Hurray, we found the code object. Check for protected addresses. | |
125 const ptrdiff_t offset = fault_addr - base; | |
126 | |
127 TRACE("Range found, offset is %td\n", offset); | |
128 | |
129 for (unsigned i = 0; i < data->num_protected_instructions; ++i) { | |
130 TRACE(" Checking instr offset %d, landing offset %d\n", | |
131 data->instructions[i].instr_offset, | |
132 data->instructions[i].landing_offset); | |
133 if (data->instructions[i].instr_offset == offset) { | |
134 // Hurray again, we found the actual instruction. Jump to | |
135 // the landing pad. | |
136 | |
137 *return_address = reinterpret_cast<void*>( | |
138 data->instructions[i].landing_offset + base); | |
139 return true; | |
140 } | |
141 } | |
142 } | |
143 } | |
144 | |
145 // If we get here, it's not a recoverable wasm fault, so we go to the next | |
146 // handler. | |
147 g_thread_in_wasm_code = true; | |
148 return false; | |
149 #else | |
150 abort(); | |
151 #endif | |
152 } | |
153 | |
154 namespace { | |
155 | |
156 #if V8_TRAP_HANDLER_SUPPORTED | |
157 void HandleSignal(int signum, siginfo_t* info, void* context) { | |
158 // Bail out early in case we got called for the wrong kind of signal. | |
159 if (signum != SIGSEGV && signum != SIGBUS) { | |
160 return; | |
161 } | |
162 | |
163 // Make sure the signal was generated by the kernel and not some other source. | |
164 if (info->si_code <= 0 || info->si_code == SI_USER || | |
165 info->si_code == SI_QUEUE || info->si_code == SI_TIMER || | |
166 info->si_code == SI_ASYNCIO || info->si_code == SI_MESGQ) { | |
167 return; | |
168 } | |
169 | |
170 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context); | |
171 | |
172 void* source_instruction = | |
173 reinterpret_cast<void*>(uc->uc_mcontext.gregs[REG_RIP]); | |
174 void* target_data = reinterpret_cast<void*>(info->si_addr); | |
175 | |
176 void* return_address; | |
177 | |
178 if (!TryHandleFault(source_instruction, target_data, &return_address)) { | |
179 abort(); | |
180 } | |
181 | |
182 uc->uc_mcontext.gregs[REG_RIP] = reinterpret_cast<intptr_t>(return_address); | |
183 } | |
184 #endif | |
185 } // namespace | |
186 | |
187 bool RegisterDefaultSignalHandler() { | |
Mark Seaborn
2017/02/09 16:44:36
If we're restricting this file to code that runs i
Eric Holk
2017/02/15 02:02:44
Done.
| |
188 #if V8_TRAP_HANDLER_SUPPORTED | |
189 struct sigaction action; | |
190 struct sigaction old_action; | |
191 action.sa_sigaction = HandleSignal; | |
192 action.sa_flags = SA_SIGINFO; | |
193 sigemptyset(&action.sa_mask); | |
194 if (sigaction(SIGSEGV, &action, &old_action) != 0) { | |
195 return false; | |
196 } | |
197 | |
198 if (sigaction(SIGBUS, &action, nullptr) != 0) { | |
199 // Undo registering for SIGSEGV so that this function has no effect if | |
Mark Seaborn
2017/01/27 20:17:30
The caller doesn't actually check for an error. C
Eric Holk
2017/02/02 18:43:30
I made the caller check.
Done.
| |
200 // registering for either SIGSEGV or SIGBUS fail. | |
201 sigaction(SIGSEGV, &old_action, nullptr); | |
202 return false; | |
203 } | |
204 | |
205 return true; | |
206 #else | |
207 return false; | |
208 #endif | |
209 } | |
210 | |
211 } // namespace trap_handler | |
212 } // namespace internal | |
213 } // namespace v8 | |
OLD | NEW |