libafl 0.15.4

Slot your own fuzzers together and extend their features using Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/// In-Process crash handling for `Windows`
pub mod windows_asan_handler {
    use alloc::string::String;
    use core::sync::atomic::{Ordering, compiler_fence};

    use libafl_bolts::os::SIGNAL_RECURSION_EXIT;
    use windows::Win32::System::Threading::{
        CRITICAL_SECTION, EnterCriticalSection, ExitProcess, LeaveCriticalSection,
    };

    use crate::{
        events::{EventFirer, EventRestarter},
        executors::{
            Executor, ExitKind, HasObservers, hooks::inprocess::GLOBAL_STATE,
            inprocess::run_observers_and_save_state,
        },
        feedbacks::Feedback,
        fuzzer::HasObjective,
        inputs::Input,
        observers::ObserversTuple,
        state::{HasCurrentTestcase, HasExecutions, HasSolutions},
    };

    /// # Safety
    /// ASAN deatch handler
    pub unsafe extern "C" fn asan_death_handler<E, EM, I, OF, S, Z>()
    where
        E: Executor<EM, I, S, Z> + HasObservers,
        E::Observers: ObserversTuple<I, S>,
        EM: EventFirer<I, S> + EventRestarter<S>,
        I: Input + Clone,
        OF: Feedback<EM, I, E::Observers, S>,
        S: HasExecutions + HasSolutions<I> + HasCurrentTestcase<I>,
        Z: HasObjective<Objective = OF>,
    {
        unsafe {
            let data = &raw mut GLOBAL_STATE;
            let (max_depth_reached, _signal_depth) = (*data).signal_handler_enter();

            if max_depth_reached {
                log::error!(
                    "We crashed inside a asan death handler, but this should never happen!"
                );
                ExitProcess(SIGNAL_RECURSION_EXIT as u32);
            }

            // Have we set a timer_before?
            if (*data).ptp_timer.is_some() {
                /*
                    We want to prevent the timeout handler being run while the main thread is executing the crash handler
                    Timeout handler runs if it has access to the critical section or data.in_target == 0
                    Writing 0 to the data.in_target makes the timeout handler makes the timeout handler invalid.
                */
                compiler_fence(Ordering::SeqCst);
                EnterCriticalSection((*data).critical as *mut CRITICAL_SECTION);
                compiler_fence(Ordering::SeqCst);
                (*data).in_target = 0;
                compiler_fence(Ordering::SeqCst);
                LeaveCriticalSection((*data).critical as *mut CRITICAL_SECTION);
                compiler_fence(Ordering::SeqCst);
            }

            log::error!("ASAN detected crash!");
            if (*data).current_input_ptr.is_null() {
                {
                    log::error!("Double crash\n");
                    log::error!(
                        "ASAN detected crash but we're not in the target... Bug in the fuzzer? Exiting.",
                    );
                }
                #[cfg(feature = "std")]
                {
                    log::error!("Type QUIT to restart the child");
                    let mut line = String::new();
                    while line.trim() != "QUIT" {
                        let _ = std::io::stdin().read_line(&mut line);
                    }
                }

                // TODO tell the parent to not restart
            } else {
                let executor = (*data).executor_mut::<E>();
                // reset timer
                if (*data).ptp_timer.is_some() {
                    (*data).ptp_timer = None;
                }

                let state = (*data).state_mut::<S>();
                let fuzzer = (*data).fuzzer_mut::<Z>();
                let event_mgr = (*data).event_mgr_mut::<EM>();

                log::error!("Child crashed!");

                // Make sure we don't crash in the crash handler forever.
                let input = (*data).take_current_input::<I>();

                run_observers_and_save_state::<E, EM, I, OF, S, Z>(
                    executor,
                    state,
                    input,
                    fuzzer,
                    event_mgr,
                    ExitKind::Crash,
                );
            }
            // Don't need to exit, Asan will exit for us
            // ExitProcess(1);
        }
    }
}

/// The module to take care of windows crash or timeouts
pub mod windows_exception_handler {
    #[cfg(feature = "std")]
    use alloc::boxed::Box;
    use alloc::{string::String, vec::Vec};
    use core::{
        ffi::c_void,
        mem::transmute,
        ptr,
        sync::atomic::{Ordering, compiler_fence},
    };
    #[cfg(feature = "std")]
    use std::io::Write;
    #[cfg(feature = "std")]
    use std::panic;

    use libafl_bolts::os::{
        SIGNAL_RECURSION_EXIT,
        windows_exceptions::{
            CRASH_EXCEPTIONS, EXCEPTION_HANDLERS_SIZE, EXCEPTION_POINTERS, ExceptionCode,
            ExceptionHandler,
        },
    };
    use windows::Win32::System::Threading::{
        CRITICAL_SECTION, EnterCriticalSection, ExitProcess, LeaveCriticalSection,
    };

    use crate::{
        events::{EventFirer, EventRestarter},
        executors::{
            Executor, ExitKind, HasObservers,
            hooks::inprocess::{GLOBAL_STATE, HasTimeout, InProcessExecutorHandlerData},
            inprocess::{HasInProcessHooks, run_observers_and_save_state},
        },
        feedbacks::Feedback,
        fuzzer::HasObjective,
        inputs::Input,
        observers::ObserversTuple,
        state::{HasCurrentTestcase, HasExecutions, HasSolutions},
    };

    pub(crate) type HandlerFuncPtr =
        unsafe fn(*mut EXCEPTION_POINTERS, *mut InProcessExecutorHandlerData);

    /*pub unsafe fn nop_handler(
        _code: ExceptionCode,
        _exception_pointers: *mut EXCEPTION_POINTERS,
        _data: &mut InProcessExecutorHandlerData,
    ) {
    }*/

    impl ExceptionHandler for InProcessExecutorHandlerData {
        /// # Safety
        /// Will dereference `EXCEPTION_POINTERS` and access `GLOBAL_STATE`.
        unsafe fn handle(
            &mut self,
            _code: ExceptionCode,
            exception_pointers: *mut EXCEPTION_POINTERS,
        ) {
            unsafe {
                let data = &raw mut GLOBAL_STATE;
                let (max_depth_reached, _signal_depth) = (*data).signal_handler_enter();

                if max_depth_reached {
                    log::error!("We crashed inside a crash handler, but this should never happen!");
                    ExitProcess(SIGNAL_RECURSION_EXIT as u32);
                }

                if !(*data).crash_handler.is_null() {
                    let func: HandlerFuncPtr = transmute((*data).crash_handler);
                    (func)(exception_pointers, data);
                }
                (*data).signal_handler_exit();
            }
        }

        fn exceptions(&self) -> Vec<ExceptionCode> {
            let crash_list = CRASH_EXCEPTIONS.to_vec();
            assert!(crash_list.len() < EXCEPTION_HANDLERS_SIZE - 1);
            crash_list
        }
    }

    /// invokes the `post_exec` hook on all observer in case of panic
    ///
    /// # Safety
    /// Well, exception handling is not safe
    #[cfg(feature = "std")]
    pub fn setup_panic_hook<E, EM, I, OF, S, Z>()
    where
        E: Executor<EM, I, S, Z> + HasObservers,
        E::Observers: ObserversTuple<I, S>,
        EM: EventFirer<I, S> + EventRestarter<S>,
        I: Input + Clone,
        OF: Feedback<EM, I, E::Observers, S>,
        S: HasExecutions + HasSolutions<I> + HasCurrentTestcase<I>,
        Z: HasObjective<Objective = OF>,
    {
        let old_hook = panic::take_hook();
        panic::set_hook(Box::new(move |panic_info| unsafe {
            let data = &raw mut GLOBAL_STATE;
            let (max_depth_reached, _signal_depth) = (*data).signal_handler_enter();

            if max_depth_reached {
                log::error!("We crashed inside a crash handler, but this should never happen!");
                ExitProcess(SIGNAL_RECURSION_EXIT as u32);
            }

            // Have we set a timer_before?
            if (*data).ptp_timer.is_some() {
                /*
                    We want to prevent the timeout handler being run while the main thread is executing the crash handler
                    Timeout handler runs if it has access to the critical section or data.in_target == 0
                    Writing 0 to the data.in_target makes the timeout handler makes the timeout handler invalid.
                */
                compiler_fence(Ordering::SeqCst);
                EnterCriticalSection((*data).critical as *mut CRITICAL_SECTION);
                compiler_fence(Ordering::SeqCst);
                (*data).in_target = 0;
                compiler_fence(Ordering::SeqCst);
                LeaveCriticalSection((*data).critical as *mut CRITICAL_SECTION);
                compiler_fence(Ordering::SeqCst);
            }

            if (*data).is_valid() {
                // We are fuzzing!
                let executor = (*data).executor_mut::<E>();
                let state = (*data).state_mut::<S>();
                let fuzzer = (*data).fuzzer_mut::<Z>();
                let event_mgr = (*data).event_mgr_mut::<EM>();

                let input = (*data).take_current_input::<I>();

                run_observers_and_save_state::<E, EM, I, OF, S, Z>(
                    executor,
                    state,
                    input,
                    fuzzer,
                    event_mgr,
                    ExitKind::Crash,
                );

                ExitProcess(1);
            }
            old_hook(panic_info);
            (*data).signal_handler_exit();
        }));
    }

    /// Timeout handler for windows
    ///
    /// # Safety
    /// Well, exception handling is not safe
    pub unsafe extern "system" fn inproc_timeout_handler<E, EM, I, OF, S, Z>(
        _p0: *mut u8,
        global_state: *mut c_void,
        _p1: *mut u8,
    ) where
        E: Executor<EM, I, S, Z> + HasInProcessHooks<I, S> + HasObservers,
        E::Observers: ObserversTuple<I, S>,
        EM: EventFirer<I, S> + EventRestarter<S>,
        I: Input + Clone,
        OF: Feedback<EM, I, E::Observers, S>,
        S: HasExecutions + HasSolutions<I> + HasCurrentTestcase<I>,
        Z: HasObjective<Objective = OF>,
    {
        let data: &mut InProcessExecutorHandlerData =
            unsafe { &mut *(global_state as *mut InProcessExecutorHandlerData) };
        compiler_fence(Ordering::SeqCst);
        unsafe {
            EnterCriticalSection((data.critical as *mut CRITICAL_SECTION).as_mut().unwrap());
        }
        compiler_fence(Ordering::SeqCst);

        if !data.executor_ptr.is_null()
            && unsafe {
                data.executor_mut::<E>()
                    .inprocess_hooks_mut()
                    .handle_timeout()
            }
        {
            compiler_fence(Ordering::SeqCst);
            unsafe {
                LeaveCriticalSection((data.critical as *mut CRITICAL_SECTION).as_mut().unwrap());
            }
            compiler_fence(Ordering::SeqCst);

            return;
        }

        if data.in_target == 1 {
            let executor = unsafe { data.executor_mut::<E>() };
            let state = unsafe { data.state_mut::<S>() };
            let fuzzer = unsafe { data.fuzzer_mut::<Z>() };
            let event_mgr = unsafe { data.event_mgr_mut::<EM>() };

            if data.current_input_ptr.is_null() {
                log::error!("TIMEOUT or SIGUSR2 happened, but currently not fuzzing. Exiting");
            } else {
                log::error!("Timeout in fuzz run.");

                let input = unsafe { (data.current_input_ptr as *const I).as_ref().unwrap() };
                data.current_input_ptr = ptr::null_mut();

                run_observers_and_save_state::<E, EM, I, OF, S, Z>(
                    executor,
                    state,
                    input,
                    fuzzer,
                    event_mgr,
                    ExitKind::Timeout,
                );

                compiler_fence(Ordering::SeqCst);

                unsafe {
                    ExitProcess(1);
                }
            }
        }
        compiler_fence(Ordering::SeqCst);
        unsafe {
            LeaveCriticalSection((data.critical as *mut CRITICAL_SECTION).as_mut().unwrap());
        }
        compiler_fence(Ordering::SeqCst);
        // log::info!("TIMER INVOKED!");
    }

    /// Crash handler for windows
    ///
    /// # Safety
    /// Well, exception handling is not safe
    pub unsafe fn inproc_crash_handler<E, EM, I, OF, S, Z>(
        exception_pointers: *mut EXCEPTION_POINTERS,
        data: &mut InProcessExecutorHandlerData,
    ) where
        E: Executor<EM, I, S, Z> + HasObservers,
        E::Observers: ObserversTuple<I, S>,
        EM: EventFirer<I, S> + EventRestarter<S>,
        I: Input + Clone,
        OF: Feedback<EM, I, E::Observers, S>,
        S: HasExecutions + HasSolutions<I> + HasCurrentTestcase<I>,
        Z: HasObjective<Objective = OF>,
    {
        // Have we set a timer_before?
        if data.ptp_timer.is_some() {
            /*
                We want to prevent the timeout handler being run while the main thread is executing the crash handler
                Timeout handler runs if it has access to the critical section or data.in_target == 0
                Writing 0 to the data.in_target makes the timeout handler makes the timeout handler invalid.
            */
            compiler_fence(Ordering::SeqCst);
            unsafe {
                EnterCriticalSection(data.critical as *mut CRITICAL_SECTION);
            }
            compiler_fence(Ordering::SeqCst);
            data.in_target = 0;
            compiler_fence(Ordering::SeqCst);
            unsafe {
                LeaveCriticalSection(data.critical as *mut CRITICAL_SECTION);
            }
            compiler_fence(Ordering::SeqCst);
        }

        // Is this really crash?
        let mut is_crash = true;
        #[cfg(feature = "std")]
        if let Some(exception_pointers) = unsafe { exception_pointers.as_mut() } {
            let code: ExceptionCode = ExceptionCode::from(unsafe {
                exception_pointers
                    .ExceptionRecord
                    .as_mut()
                    .unwrap()
                    .ExceptionCode
                    .0
            });

            let exception_list = data.exceptions();
            if exception_list.contains(&code) {
                log::error!(
                    "Crashed with {code} at {:?} in thread {:?}",
                    unsafe {
                        exception_pointers
                            .ExceptionRecord
                            .as_mut()
                            .unwrap()
                            .ExceptionAddress
                    },
                    unsafe { winapi::um::processthreadsapi::GetCurrentThreadId() }
                );
            } else {
                // log::trace!("Exception code received, but {code} is not in CRASH_EXCEPTIONS");
                is_crash = false;
            }
        } else {
            log::error!("Crashed without exception (probably due to SIGABRT)");
        }

        if data.current_input_ptr.is_null() {
            {
                log::error!("Double crash\n");
                let crash_addr = unsafe {
                    exception_pointers
                        .as_mut()
                        .unwrap()
                        .ExceptionRecord
                        .as_mut()
                        .unwrap()
                        .ExceptionAddress as usize
                };

                log::error!(
                    "We crashed at addr 0x{crash_addr:x}, but are not in the target... Bug in the fuzzer? Exiting."
                );
            }
            #[cfg(feature = "std")]
            {
                log::error!("Type QUIT to restart the child");
                let mut line = String::new();
                while line.trim() != "QUIT" {
                    let _ = std::io::stdin().read_line(&mut line);
                }
            }

            // TODO tell the parent to not restart
        } else {
            let executor = unsafe { data.executor_mut::<E>() };
            // reset timer
            if data.ptp_timer.is_some() {
                data.ptp_timer = None;
            }

            let state = unsafe { data.state_mut::<S>() };
            let fuzzer = unsafe { data.fuzzer_mut::<Z>() };
            let event_mgr = unsafe { data.event_mgr_mut::<EM>() };

            if is_crash {
                log::error!("Child crashed!");
            } else {
                // log::info!("Exception received!");
            }

            // Make sure we don't crash in the crash handler forever.
            if is_crash {
                log::warn!("Running observers and exiting!");
                // // I want to disable the hooks before doing anything, especially before taking a stack dump
                let input = unsafe { data.take_current_input::<I>() };
                run_observers_and_save_state::<E, EM, I, OF, S, Z>(
                    executor,
                    state,
                    input,
                    fuzzer,
                    event_mgr,
                    ExitKind::Crash,
                );
                {
                    let mut bsod = Vec::new();
                    {
                        let mut writer = std::io::BufWriter::new(&mut bsod);
                        writeln!(writer, "input: {:?}", input.generate_name(None)).unwrap();
                        libafl_bolts::minibsod::generate_minibsod(&mut writer, exception_pointers)
                            .unwrap();
                        writer.flush().unwrap();
                    }
                    log::error!("{}", core::str::from_utf8(&bsod).unwrap());
                }
            } else {
                // This is not worth saving
            }
        }

        if is_crash {
            log::info!("Exiting!");
            unsafe {
                ExitProcess(1);
            }
        }
        // log::info!("Not Exiting!");
    }
}