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 | |
37 namespace v8 { | |
38 namespace internal { | |
39 namespace trap_handler { | |
40 | |
41 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.
| |
42 | |
43 extern size_t gNumCodeObjects; | |
44 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
| |
45 | |
46 MetadataLock::MetadataLock() { | |
47 if (g_thread_in_wasm_code) { | |
48 abort(); | |
49 } | |
50 | |
51 while (spinlock_.test_and_set(std::memory_order::memory_order_acquire)) { | |
52 } | |
53 } | |
54 | |
55 MetadataLock::~MetadataLock() { | |
56 if (g_thread_in_wasm_code) { | |
57 abort(); | |
58 } | |
59 | |
60 spinlock_.clear(std::memory_order::memory_order_release); | |
61 } | |
62 | |
63 namespace { | |
64 | |
65 #if V8_TRAP_HANDLER_SUPPORTED | |
66 class SigUnmaskStack { | |
67 public: | |
68 explicit SigUnmaskStack(sigset_t sigs) { | |
69 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.
| |
70 } | |
71 | |
72 ~SigUnmaskStack() { pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr); } | |
73 | |
74 private: | |
75 sigset_t old_mask_; | |
76 | |
77 SigUnmaskStack(const SigUnmaskStack &) = delete; | |
78 void operator=(const SigUnmaskStack &) = delete; | |
79 }; | |
80 #endif | |
81 } // namespace | |
82 | |
83 bool TryHandleFault(void *source_instruction, void *target_data, | |
84 void **return_address) { | |
85 #if V8_TRAP_HANDLER_SUPPORTED | |
86 // Ensure the faulting thread was actually running Wasm code. | |
87 if (!g_thread_in_wasm_code) { | |
88 TRACE("signal handler: Thread not running WASM code; crashing\n"); | |
89 return false; | |
90 } | |
91 | |
92 // Clear g_thread_in_wasm_code, primarily to protect against nested faults. | |
93 g_thread_in_wasm_code = false; | |
94 | |
95 // 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.
| |
96 sigset_t sigs; | |
97 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.
| |
98 sigaddset(&sigs, SIGSEGV); | |
99 sigaddset(&sigs, SIGBUS); | |
100 SigUnmaskStack unmask(sigs); | |
101 | |
102 intptr_t fault_addr = reinterpret_cast<intptr_t>(source_instruction); | |
103 | |
104 TRACE("Handling fault at %p\n", reinterpret_cast<void *>(fault_addr)); | |
105 | |
106 // TODO(eholk): broad code range check | |
107 | |
108 MetadataLock lock_holder; | |
109 | |
110 for (size_t i = 0; i < gNumCodeObjects; ++i) { | |
111 const CodeProtectionInfo *data = gCodeObjects[i]; | |
112 if (data == nullptr) { | |
113 continue; | |
114 } | |
115 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
| |
116 | |
117 TRACE(" Checking range base=%p, length = %zu\n", | |
118 reinterpret_cast<void *>(base), data->size); | |
119 | |
120 if (fault_addr >= base && | |
121 fault_addr < base + static_cast<intptr_t>(data->size)) { | |
122 // Hurray, we found the code object. Check for protected addresses. | |
123 const ptrdiff_t offset = fault_addr - base; | |
124 | |
125 TRACE("Range found, offset is %td\n", offset); | |
126 | |
127 for (unsigned i = 0; i < data->num_protected_instructions; ++i) { | |
128 TRACE(" Checking instr offset %d, landing offset %d\n", | |
129 data->instructions[i].instr_offset, | |
130 data->instructions[i].landing_offset); | |
131 if (data->instructions[i].instr_offset == offset) { | |
132 // Hurray again, we found the actual instruction. Jump to | |
133 // the landing pad. | |
134 | |
135 *return_address = reinterpret_cast<void *>( | |
136 data->instructions[i].landing_offset + base); | |
137 return true; | |
138 } | |
139 } | |
140 } | |
141 } | |
142 | |
143 // 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.
| |
144 return false; | |
145 #else | |
146 abort(); | |
147 #endif | |
148 } | |
149 | |
150 namespace { | |
151 | |
152 #if V8_TRAP_HANDLER_SUPPORTED | |
153 void HandleSignal(int signum, siginfo_t *info, void *context) { | |
154 // Bail out early in case we got called for the wrong kind of signal. | |
155 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
| |
156 return; | |
157 } | |
158 | |
159 // Make sure the signal was generated by the kernel and not some other source. | |
160 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
| |
161 info->si_code == SI_QUEUE || info->si_code == SI_TIMER || | |
162 info->si_code == SI_ASYNCIO || info->si_code == SI_MESGQ) { | |
163 return; | |
164 } | |
165 | |
166 ucontext_t *uc = reinterpret_cast<ucontext_t *>(context); | |
167 | |
168 void *source_instruction = | |
169 reinterpret_cast<void *>(uc->uc_mcontext.gregs[REG_RIP]); | |
170 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
| |
171 | |
172 void *return_address; | |
173 | |
174 if (!TryHandleFault(source_instruction, target_data, &return_address)) { | |
175 abort(); | |
176 } | |
177 | |
178 uc->uc_mcontext.gregs[REG_RIP] = reinterpret_cast<intptr_t>(return_address); | |
179 } | |
180 #endif | |
181 } // namespace | |
182 | |
183 bool RegisterDefaultSignalHandler() { | |
184 #if V8_TRAP_HANDLER_SUPPORTED | |
185 struct sigaction action; | |
186 action.sa_sigaction = HandleSignal; | |
187 action.sa_flags = SA_SIGINFO; | |
188 sigemptyset(&action.sa_mask); | |
189 return sigaction(SIGSEGV, &action, nullptr) == 0; | |
190 #else | |
191 return false; | |
192 #endif | |
193 } | |
194 | |
195 } // namespace trap_handler | |
196 } // namespace internal | |
197 } // namespace v8 | |
OLD | NEW |