/src/yara/libyara/exception.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2015. The YARA Authors. All Rights Reserved. |
3 | | |
4 | | Redistribution and use in source and binary forms, with or without modification, |
5 | | are permitted provided that the following conditions are met: |
6 | | |
7 | | 1. Redistributions of source code must retain the above copyright notice, this |
8 | | list of conditions and the following disclaimer. |
9 | | |
10 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | this list of conditions and the following disclaimer in the documentation and/or |
12 | | other materials provided with the distribution. |
13 | | |
14 | | 3. Neither the name of the copyright holder nor the names of its contributors |
15 | | may be used to endorse or promote products derived from this software without |
16 | | specific prior written permission. |
17 | | |
18 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
22 | | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
25 | | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #ifndef YR_EXCEPTION_H |
31 | | #define YR_EXCEPTION_H |
32 | | |
33 | | #include <assert.h> |
34 | | #include <yara/globals.h> |
35 | | |
36 | | typedef struct { |
37 | | void* memfault_from; |
38 | | void* memfault_to; |
39 | | void* jump_back; |
40 | | } jumpinfo; |
41 | | |
42 | | |
43 | | #if _WIN32 || __CYGWIN__ |
44 | | |
45 | | #include <windows.h> |
46 | | |
47 | | // If compiling with Microsoft's compiler use structured exception handling. |
48 | | |
49 | | #ifdef _MSC_VER |
50 | | |
51 | | #include <excpt.h> |
52 | | |
53 | | static LONG CALLBACK exception_handler(PEXCEPTION_POINTERS ExceptionInfo) |
54 | | { |
55 | | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) |
56 | | { |
57 | | case EXCEPTION_IN_PAGE_ERROR: |
58 | | case EXCEPTION_ACCESS_VIOLATION: |
59 | | return EXCEPTION_EXECUTE_HANDLER; |
60 | | } |
61 | | |
62 | | return EXCEPTION_CONTINUE_SEARCH; |
63 | | } |
64 | | |
65 | | #define YR_TRYCATCH(_do_, _try_clause_, _catch_clause_) \ |
66 | | do \ |
67 | | { \ |
68 | | if (_do_) \ |
69 | | { \ |
70 | | __try \ |
71 | | { \ |
72 | | _try_clause_ \ |
73 | | } \ |
74 | | __except (exception_handler(GetExceptionInformation())) \ |
75 | | { \ |
76 | | _catch_clause_ \ |
77 | | } \ |
78 | | } \ |
79 | | else \ |
80 | | { \ |
81 | | _try_clause_ \ |
82 | | } \ |
83 | | } while (0) |
84 | | |
85 | | #else |
86 | | |
87 | | // If not compiling with Microsoft's compiler use vectored exception handling. |
88 | | |
89 | | #include <setjmp.h> |
90 | | |
91 | | static LONG CALLBACK exception_handler(PEXCEPTION_POINTERS ExceptionInfo) |
92 | | { |
93 | | jumpinfo* jump_info; |
94 | | |
95 | | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) |
96 | | { |
97 | | case EXCEPTION_IN_PAGE_ERROR: |
98 | | jump_info = |
99 | | (jumpinfo*) yr_thread_storage_get_value(&yr_trycatch_trampoline_tls); |
100 | | |
101 | | if (jump_info != NULL) |
102 | | { |
103 | | void* fault_address = (void*) ExceptionInfo->ExceptionRecord->ExceptionInformation[1]; |
104 | | if (jump_info->memfault_from <= fault_address && jump_info->memfault_to > fault_address) |
105 | | { |
106 | | longjmp(*(jmp_buf*)jump_info->jump_back, 1); |
107 | | } |
108 | | } |
109 | | } |
110 | | |
111 | | return EXCEPTION_CONTINUE_SEARCH; |
112 | | } |
113 | | |
114 | | #define YR_TRYCATCH(_do_, _try_clause_, _catch_clause_) \ |
115 | | do \ |
116 | | { \ |
117 | | if (_do_) \ |
118 | | { \ |
119 | | jumpinfo jump_info; \ |
120 | | jump_info.memfault_from = 0; \ |
121 | | jump_info.memfault_to = 0; \ |
122 | | jmp_buf jb; \ |
123 | | jump_info.jump_back = (void*) &jb; \ |
124 | | /* Store pointer to sigjmp_buf in TLS */ \ |
125 | | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, &jump_info); \ |
126 | | HANDLE exh = AddVectoredExceptionHandler(1, exception_handler); \ |
127 | | if (setjmp(jb) == 0) \ |
128 | | { \ |
129 | | _try_clause_ \ |
130 | | } \ |
131 | | else \ |
132 | | { \ |
133 | | _catch_clause_ \ |
134 | | } \ |
135 | | RemoveVectoredExceptionHandler(exh); \ |
136 | | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, NULL); \ |
137 | | } \ |
138 | | else \ |
139 | | { \ |
140 | | _try_clause_ \ |
141 | | } \ |
142 | | } while (0) |
143 | | |
144 | | #endif |
145 | | |
146 | | #else |
147 | | |
148 | | #if defined(__APPLE__) || defined(__linux__) || defined(_AIX) |
149 | 0 | #define CATCH_SIGSEGV 0 |
150 | 0 | #define CATCH_SIGBUS 1 |
151 | | #elif defined(BSD) |
152 | | // According to #551, older BSD versions use SIGSEGV for invalid mmap access. |
153 | | // Newer versions, however, use SIGBUS (tested with FreeBSD 13.2 / OpenBSD 7.4). |
154 | | // To be compatible with both, catch SIGBUS and SIGSEGV. |
155 | | #define CATCH_SIGSEGV 1 |
156 | | #define CATCH_SIGBUS 1 |
157 | | #else // For unknown systems, play it safe by catching both |
158 | | #define CATCH_SIGSEGV 1 |
159 | | #define CATCH_SIGBUS 1 |
160 | | #endif |
161 | | |
162 | | #include <pthread.h> |
163 | | #include <setjmp.h> |
164 | | #include <signal.h> |
165 | | #include <yara/globals.h> |
166 | | |
167 | | static void exception_handler(int sig, siginfo_t * info, void *context) |
168 | 0 | { |
169 | 0 | if (sig != SIGBUS && sig != SIGSEGV) |
170 | 0 | { |
171 | 0 | return; |
172 | 0 | } |
173 | 0 | jumpinfo* jump_info = (jumpinfo*) yr_thread_storage_get_value(&yr_trycatch_trampoline_tls); |
174 | |
|
175 | 0 | if (jump_info != NULL) |
176 | 0 | { |
177 | 0 | void* fault_address = (void*) info->si_addr; |
178 | 0 | if (jump_info->memfault_from <= fault_address && jump_info->memfault_to > fault_address) |
179 | 0 | { |
180 | 0 | siglongjmp(*(sigjmp_buf*)jump_info->jump_back, 1); |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | // If we're here, the signal we received didn't originate from YARA. |
185 | | // In this case, we want to invoke the original signal handler, which may handle the signal. |
186 | | |
187 | | // Lock the exception handler mutex to prevent simultaneous write access while we read the old signal handler |
188 | 0 | pthread_mutex_lock(&exception_handler_mutex); |
189 | 0 | struct sigaction old_handler; |
190 | 0 | if (sig == SIGBUS) |
191 | 0 | old_handler = old_sigbus_exception_handler; |
192 | 0 | else |
193 | 0 | old_handler = old_sigsegv_exception_handler; |
194 | 0 | pthread_mutex_unlock(&exception_handler_mutex); |
195 | |
|
196 | 0 | if (old_handler.sa_flags & SA_SIGINFO) |
197 | 0 | { |
198 | 0 | old_handler.sa_sigaction(sig, info, context); |
199 | 0 | } |
200 | 0 | else if (old_handler.sa_handler == SIG_DFL) |
201 | 0 | { |
202 | | // Old handler is the default action. To do this, set the signal handler back to default and raise the signal. |
203 | | // This is fairly volatile - since this is not an atomic operation, signals from other threads might also |
204 | | // cause the default action while we're doing this. However, the default action will typically cause a |
205 | | // process termination anyway. |
206 | 0 | pthread_mutex_lock(&exception_handler_mutex); |
207 | 0 | struct sigaction current_handler; |
208 | 0 | sigaction(sig, &old_handler, ¤t_handler); |
209 | 0 | raise(sig); |
210 | 0 | sigaction(sig, ¤t_handler, NULL); |
211 | 0 | pthread_mutex_unlock(&exception_handler_mutex); |
212 | 0 | } |
213 | 0 | else if (old_handler.sa_handler == SIG_IGN) |
214 | 0 | { |
215 | | // SIG_IGN wants us to ignore the signal |
216 | 0 | return; |
217 | 0 | } |
218 | 0 | else |
219 | 0 | { |
220 | 0 | old_handler.sa_handler(sig); |
221 | 0 | } |
222 | 0 | } Unexecuted instantiation: scanner.c:exception_handler Unexecuted instantiation: elf.c:exception_handler |
223 | | |
224 | | // Keep debug output indentation level consistent |
225 | | #if 0 == YR_DEBUG_VERBOSITY |
226 | 0 | #define YR_DEBUG_INDENT_INITIAL 0 |
227 | 0 | #define YR_DEBUG_INDENT_SET(x) ; |
228 | | #else |
229 | | extern YR_TLS int yr_debug_indent; |
230 | | // Ugly, but unfortunately cannot use ifdef macros inside a macro |
231 | | #define YR_DEBUG_INDENT_INITIAL yr_debug_indent |
232 | | #define YR_DEBUG_INDENT_SET(x) yr_debug_indent = (x); |
233 | | |
234 | | #endif |
235 | | |
236 | | typedef struct sigaction sa; |
237 | | |
238 | | #define YR_TRYCATCH(_do_, _try_clause_, _catch_clause_) \ |
239 | 8.54k | do \ |
240 | 8.54k | { \ |
241 | 8.54k | if (_do_) \ |
242 | 8.54k | { \ |
243 | 0 | pthread_mutex_lock(&exception_handler_mutex); \ |
244 | 0 | if (exception_handler_usecount == 0) \ |
245 | 0 | { \ |
246 | 0 | struct sigaction act; \ |
247 | 0 | /* Set exception handler for SIGSEGV / SIGBUS */ \ |
248 | 0 | act.sa_sigaction = exception_handler; \ |
249 | 0 | act.sa_flags = SA_SIGINFO | SA_ONSTACK; \ |
250 | 0 | sigfillset(&act.sa_mask); \ |
251 | 0 | if (CATCH_SIGBUS) \ |
252 | 0 | sigaction(SIGBUS, &act, &old_sigbus_exception_handler); \ |
253 | 0 | if (CATCH_SIGSEGV) \ |
254 | 0 | sigaction(SIGSEGV, &act, &old_sigsegv_exception_handler); \ |
255 | 0 | } \ |
256 | 0 | exception_handler_usecount++; \ |
257 | 0 | pthread_mutex_unlock(&exception_handler_mutex); \ |
258 | 0 | /* Save the current debug indentation level before the jump. */ \ |
259 | 0 | int yr_debug_indent_before_jump = YR_DEBUG_INDENT_INITIAL; \ |
260 | 0 | jumpinfo ji; \ |
261 | 0 | ji.memfault_from = 0; \ |
262 | 0 | ji.memfault_to = 0; \ |
263 | 0 | sigjmp_buf jb; \ |
264 | 0 | ji.jump_back = (void*) &jb; \ |
265 | 0 | /* Store pointer to jumpinfo in TLS */ \ |
266 | 0 | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, &ji); \ |
267 | 0 | if (sigsetjmp(jb, 1) == 0) \ |
268 | 0 | { \ |
269 | 0 | _try_clause_ \ |
270 | 0 | } \ |
271 | 0 | else \ |
272 | 0 | { \ |
273 | 0 | /* Restore debug output indentation in case of failure */ \ |
274 | 0 | YR_DEBUG_INDENT_SET(yr_debug_indent_before_jump); \ |
275 | 0 | \ |
276 | 0 | _catch_clause_ \ |
277 | 0 | } \ |
278 | 0 | pthread_mutex_lock(&exception_handler_mutex); \ |
279 | 0 | exception_handler_usecount--; \ |
280 | 0 | if (exception_handler_usecount == 0) \ |
281 | 0 | { \ |
282 | 0 | /* Stop capturing relevant signals */ \ |
283 | 0 | if (CATCH_SIGBUS) \ |
284 | 0 | sigaction(SIGBUS, &old_sigbus_exception_handler, NULL); \ |
285 | 0 | if (CATCH_SIGSEGV) \ |
286 | 0 | sigaction(SIGSEGV, &old_sigsegv_exception_handler, NULL); \ |
287 | 0 | } \ |
288 | 0 | pthread_mutex_unlock(&exception_handler_mutex); \ |
289 | 0 | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, NULL); \ |
290 | 0 | } \ |
291 | 8.54k | else \ |
292 | 8.54k | { \ |
293 | 47.0k | _try_clause_ \ |
294 | 8.54k | } \ |
295 | 8.54k | } while (0) |
296 | | |
297 | | #endif |
298 | | |
299 | | #endif |