libafl_frida 0.9.0

Frida backend library for LibAFL
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
use core::fmt::{self, Debug, Formatter};

#[cfg(target_arch = "aarch64")]
use capstone::{
    arch::{self, BuildsCapstone},
    Capstone,
};
#[cfg(all(target_arch = "x86_64", unix))]
use capstone::{
    arch::{self, BuildsCapstone},
    Capstone,
};
#[cfg(unix)]
use frida_gum::instruction_writer::InstructionWriter;
#[cfg(unix)]
use frida_gum::CpuContext;
use frida_gum::{stalker::Transformer, Gum, Module, ModuleDetails, ModuleMap, PageProtection};
use libafl::{
    bolts::{cli::FuzzerOptions, tuples::MatchFirstType},
    inputs::{HasTargetBytes, Input},
    Error,
};
#[cfg(unix)]
use libafl_targets::drcov::DrCovBasicBlock;
#[cfg(unix)]
use nix::sys::mman::{mmap, MapFlags, ProtFlags};
use rangemap::RangeMap;

#[cfg(all(feature = "cmplog", target_arch = "aarch64"))]
use crate::cmplog_rt::CmpLogRuntime;
use crate::coverage_rt::CoverageRuntime;
#[cfg(unix)]
use crate::{asan::asan_rt::AsanRuntime, drcov_rt::DrCovRuntime};

#[cfg(any(target_vendor = "apple"))]
const ANONYMOUS_FLAG: MapFlags = MapFlags::MAP_ANON;
#[cfg(not(any(target_vendor = "apple", target_os = "windows")))]
const ANONYMOUS_FLAG: MapFlags = MapFlags::MAP_ANONYMOUS;

/// The Runtime trait
pub trait FridaRuntime: 'static + Debug {
    /// Initialization
    fn init(
        &mut self,
        gum: &Gum,
        ranges: &RangeMap<usize, (u16, String)>,
        modules_to_instrument: &[&str],
    );

    /// Method called before execution
    fn pre_exec<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error>;

    /// Method called after execution
    fn post_exec<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error>;
}

/// The tuple for Frida Runtime
pub trait FridaRuntimeTuple: MatchFirstType + Debug {
    /// Initialization
    fn init_all(
        &mut self,
        gum: &Gum,
        ranges: &RangeMap<usize, (u16, String)>,
        modules_to_instrument: &[&str],
    );

    /// Method called before execution
    fn pre_exec_all<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error>;

    /// Method called after execution
    fn post_exec_all<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error>;
}

impl FridaRuntimeTuple for () {
    fn init_all(
        &mut self,
        _gum: &Gum,
        _ranges: &RangeMap<usize, (u16, String)>,
        _modules_to_instrument: &[&str],
    ) {
    }
    fn pre_exec_all<I: Input + HasTargetBytes>(&mut self, _input: &I) -> Result<(), Error> {
        Ok(())
    }
    fn post_exec_all<I: Input + HasTargetBytes>(&mut self, _input: &I) -> Result<(), Error> {
        Ok(())
    }
}

impl<Head, Tail> FridaRuntimeTuple for (Head, Tail)
where
    Head: FridaRuntime,
    Tail: FridaRuntimeTuple,
{
    fn init_all(
        &mut self,
        gum: &Gum,
        ranges: &RangeMap<usize, (u16, String)>,
        modules_to_instrument: &[&str],
    ) {
        self.0.init(gum, ranges, modules_to_instrument);
        self.1.init_all(gum, ranges, modules_to_instrument);
    }

    fn pre_exec_all<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error> {
        self.0.pre_exec(input)?;
        self.1.pre_exec_all(input)
    }

    fn post_exec_all<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error> {
        self.0.post_exec(input)?;
        self.1.post_exec_all(input)
    }
}

/// An helper that feeds `FridaInProcessExecutor` with edge-coverage instrumentation
pub struct FridaInstrumentationHelper<'a, RT> {
    #[cfg(unix)]
    capstone: Capstone,
    ranges: RangeMap<usize, (u16, String)>,
    module_map: ModuleMap,
    options: &'a FuzzerOptions,
    transformer: Option<Transformer<'a>>,
    runtimes: RT,
}

impl<RT> Debug for FridaInstrumentationHelper<'_, RT> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut dbg_me = f.debug_struct("FridaInstrumentationHelper");
        dbg_me
            .field("ranges", &self.ranges)
            .field("module_map", &"<ModuleMap>")
            .field("options", &self.options);
        dbg_me.finish()
    }
}

/// Helper function to get the size of a module's CODE section from frida
#[must_use]
pub fn get_module_size(module_name: &str) -> usize {
    let mut code_size = 0;
    let code_size_ref = &mut code_size;
    Module::enumerate_ranges(module_name, PageProtection::ReadExecute, move |details| {
        *code_size_ref = details.memory_range().size();
        true
    });

    code_size
}

#[cfg(target_arch = "aarch64")]
fn pc(context: &CpuContext) -> usize {
    context.pc() as usize
}

#[cfg(all(target_arch = "x86_64", unix))]
fn pc(context: &CpuContext) -> usize {
    context.rip() as usize
}

/// The implementation of the [`FridaInstrumentationHelper`]
impl<'a, RT> FridaInstrumentationHelper<'a, RT>
where
    RT: FridaRuntimeTuple,
{
    /// Constructor function to create a new [`FridaInstrumentationHelper`], given a `module_name`.
    #[allow(clippy::too_many_lines)]
    #[must_use]
    pub fn new(gum: &'a Gum, options: &'a FuzzerOptions, runtimes: RT) -> Self {
        // workaround frida's frida-gum-allocate-near bug:
        #[cfg(unix)]
        unsafe {
            for _ in 0..512 {
                mmap(
                    std::ptr::null_mut(),
                    128 * 1024,
                    ProtFlags::PROT_NONE,
                    ANONYMOUS_FLAG | MapFlags::MAP_PRIVATE | MapFlags::MAP_NORESERVE,
                    -1,
                    0,
                )
                .expect("Failed to map dummy regions for frida workaround");
                mmap(
                    std::ptr::null_mut(),
                    4 * 1024 * 1024,
                    ProtFlags::PROT_NONE,
                    ANONYMOUS_FLAG | MapFlags::MAP_PRIVATE | MapFlags::MAP_NORESERVE,
                    -1,
                    0,
                )
                .expect("Failed to map dummy regions for frida workaround");
            }
        }

        let mut modules_to_instrument = vec![options
            .harness
            .as_ref()
            .unwrap()
            .to_string_lossy()
            .to_string()];
        modules_to_instrument.append(&mut options.libs_to_instrument.clone());
        let modules_to_instrument: Vec<&str> =
            modules_to_instrument.iter().map(AsRef::as_ref).collect();

        let mut helper = Self {
            #[cfg(target_arch = "aarch64")]
            capstone: Capstone::new()
                .arm64()
                .mode(arch::arm64::ArchMode::Arm)
                .detail(true)
                .build()
                .expect("Failed to create Capstone object"),
            #[cfg(all(target_arch = "x86_64", unix))]
            capstone: Capstone::new()
                .x86()
                .mode(arch::x86::ArchMode::Mode64)
                .detail(true)
                .build()
                .expect("Failed to create Capstone object"),
            ranges: RangeMap::new(),
            module_map: ModuleMap::new_from_names(gum, &modules_to_instrument),
            options,
            runtimes,
            transformer: None,
        };

        if options.cmplog || options.asan || !options.disable_coverage {
            for (i, module) in helper.module_map.values().iter().enumerate() {
                let range = module.range();
                let start = range.base_address().0 as usize;
                // println!("start: {:x}", start);
                helper
                    .ranges
                    .insert(start..(start + range.size()), (i as u16, module.path()));
            }
            if !options.dont_instrument.is_empty() {
                for (module_name, offset) in options.dont_instrument.clone() {
                    let module_details = ModuleDetails::with_name(module_name).unwrap();
                    let lib_start = module_details.range().base_address().0 as usize;
                    // println!("removing address: {:#x}", lib_start + offset);
                    helper
                        .ranges
                        .remove((lib_start + offset)..(lib_start + offset + 4));
                }
            }

            // make sure we aren't in the instrumented list, as it would cause recursions
            assert!(
                !helper.ranges.contains_key(&(Self::new as usize)),
                "instrumented libraries must not include the fuzzer"
            );

            helper
                .runtimes
                .init_all(gum, &helper.ranges, &modules_to_instrument);
        }

        let transformer = Transformer::from_callback(gum, |basic_block, output| {
            let mut first = true;
            for instruction in basic_block {
                let instr = instruction.instr();
                #[cfg(unix)]
                let instr_size = instr.bytes().len();
                let address = instr.address();
                //println!("block @ {:x} transformed to {:x}", address, output.writer().pc());

                //println!(
                //"address: {:x} contains: {:?}",
                //address,
                //self.ranges().contains_key(&(address as usize))
                //);

                // println!("Ranges: {:#?}", self.ranges());
                if helper.ranges().contains_key(&(address as usize)) {
                    if first {
                        first = false;
                        //println!("block @ {:x} transformed to {:x}", address, output.writer().pc());
                        if let Some(rt) = helper.runtime_mut::<CoverageRuntime>() {
                            rt.emit_coverage_mapping(address, &output);
                        }

                        #[cfg(unix)]
                        if let Some(rt) = helper.runtime_mut::<DrCovRuntime>() {
                            instruction.put_callout(|context| {
                                let real_address = rt.real_address_for_stalked(pc(&context));
                                //let (range, (id, name)) = helper.ranges.get_key_value(&real_address).unwrap();
                                //println!("{}:0x{:016x}", name, real_address - range.start);
                                rt.drcov_basic_blocks.push(DrCovBasicBlock::new(
                                    real_address,
                                    real_address + instr_size,
                                ));
                            });
                        }
                    }

                    #[cfg(unix)]
                    let res = if let Some(_rt) = helper.runtime::<AsanRuntime>() {
                        AsanRuntime::asan_is_interesting_instruction(
                            &helper.capstone,
                            address,
                            instr,
                        )
                    } else {
                        None
                    };

                    #[cfg(all(target_arch = "x86_64", unix))]
                    if let Some((segment, width, basereg, indexreg, scale, disp)) = res {
                        if let Some(rt) = helper.runtime_mut::<AsanRuntime>() {
                            rt.emit_shadow_check(
                                address, &output, segment, width, basereg, indexreg, scale, disp,
                            );
                        }
                    }

                    #[cfg(target_arch = "aarch64")]
                    if let Some((basereg, indexreg, displacement, width, shift, extender)) = res {
                        if let Some(rt) = helper.runtime_mut::<AsanRuntime>() {
                            rt.emit_shadow_check(
                                address,
                                &output,
                                basereg,
                                indexreg,
                                displacement,
                                width,
                                shift,
                                extender,
                            );
                        }
                    }

                    #[cfg(all(feature = "cmplog", target_arch = "aarch64"))]
                    if let Some(rt) = helper.runtime::<CmpLogRuntime>() {
                        if let Some((op1, op2, special_case)) =
                            CmpLogRuntime::cmplog_is_interesting_instruction(
                                &helper.capstone,
                                address,
                                instr,
                            )
                        {
                            //emit code that saves the relevant data in runtime(passes it to x0, x1)
                            rt.emit_comparison_handling(address, &output, &op1, &op2, special_case);
                        }
                    }

                    #[cfg(unix)]
                    if let Some(rt) = helper.runtime_mut::<AsanRuntime>() {
                        rt.add_stalked_address(
                            output.writer().pc() as usize - instr_size,
                            address as usize,
                        );
                    }

                    #[cfg(unix)]
                    if let Some(rt) = helper.runtime_mut::<DrCovRuntime>() {
                        rt.add_stalked_address(
                            output.writer().pc() as usize - instr_size,
                            address as usize,
                        );
                    }
                }
                instruction.keep();
            }
        });

        helper.transformer = Some(transformer);

        helper
    }

    /// Return the runtime
    pub fn runtime<R>(&self) -> Option<&R>
    where
        R: FridaRuntime,
    {
        self.runtimes.match_first_type::<R>()
    }

    /// Return the mutable runtime
    pub fn runtime_mut<R>(&mut self) -> Option<&mut R>
    where
        R: FridaRuntime,
    {
        self.runtimes.match_first_type_mut::<R>()
    }

    /// Returns ref to the Transformer
    pub fn transformer(&mut self) -> &Transformer<'a> {
        // the Transformer is always initialized on `new`. We can safely unwrap.
        self.transformer.as_ref().unwrap()
    }

    /// Initialize all
    pub fn init(
        &mut self,
        gum: &'a Gum,
        ranges: &RangeMap<usize, (u16, String)>,
        modules_to_instrument: &'a [&str],
    ) {
        self.runtimes.init_all(gum, ranges, modules_to_instrument);
    }

    /// Method called before execution
    pub fn pre_exec<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error> {
        self.runtimes.pre_exec_all(input)
    }

    /// Method called after execution
    pub fn post_exec<I: Input + HasTargetBytes>(&mut self, input: &I) -> Result<(), Error> {
        self.runtimes.post_exec_all(input)
    }

    /// If stalker is enabled
    pub fn stalker_enabled(&self) -> bool {
        self.options.cmplog || self.options.asan || !self.options.disable_coverage
    }

    /// Pointer to coverage map
    pub fn map_mut_ptr(&mut self) -> Option<*mut u8> {
        self.runtime_mut::<CoverageRuntime>()
            .map(CoverageRuntime::map_mut_ptr)
    }

    /// Ranges
    pub fn ranges(&self) -> &RangeMap<usize, (u16, String)> {
        &self.ranges
    }

    /// Mutable ranges
    pub fn ranges_mut(&mut self) -> &mut RangeMap<usize, (u16, String)> {
        &mut self.ranges
    }

    /// Return the ref to options
    #[inline]
    pub fn options(&self) -> &FuzzerOptions {
        self.options
    }
}