Skip to content

Commit 3b23012

Browse files
wfdewithrmalmain
andauthored
Exclude ASAN DSO address ranges in QEMU AsanModule (#3180)
Co-authored-by: Romain Malmain <[email protected]>
1 parent 1620bd7 commit 3b23012

1 file changed

Lines changed: 52 additions & 12 deletions

File tree

  • libafl_qemu/src/modules/usermode

libafl_qemu/src/modules/usermode/asan.rs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414
use hashbrown::{HashMap, HashSet};
1515
use libafl::{executors::ExitKind, observers::ObserversTuple};
1616
use libafl_bolts::os::unix_signals::Signal;
17-
use libafl_qemu_sys::GuestAddr;
17+
use libafl_qemu_sys::{GuestAddr, MapInfo};
1818
use libc::{
1919
MAP_ANON, MAP_FAILED, MAP_FIXED, MAP_NORESERVE, MAP_PRIVATE, PROT_READ, PROT_WRITE, c_void,
2020
};
@@ -61,6 +61,8 @@ pub struct AsanModule {
6161
empty: bool,
6262
rt: Pin<Box<AsanGiovese>>,
6363
filter: StdAddressFilter,
64+
asan_lib: Option<String>,
65+
asan_mappings: Option<Vec<MapInfo>>,
6466
}
6567

6668
pub struct AsanGiovese {
@@ -408,6 +410,8 @@ impl AsanModule {
408410
empty: true,
409411
rt,
410412
filter,
413+
asan_lib: None,
414+
asan_mappings: None,
411415
}
412416
}
413417

@@ -981,7 +985,7 @@ where
981985

982986
// Let the use skip preloading the ASAN DSO. Maybe they want to use
983987
// their own implementation.
984-
if env::var_os("SKIP_ASAN_LD_PRELOAD").is_none() {
988+
let asan_lib = if env::var_os("SKIP_ASAN_LD_PRELOAD").is_none() {
985989
let current = env::current_exe().unwrap();
986990
let asan_lib = fs::canonicalize(current)
987991
.unwrap()
@@ -1034,13 +1038,18 @@ where
10341038
args.insert(1, "LD_PRELOAD=".to_string() + &asan_lib);
10351039
args.insert(1, "-E".into());
10361040
}
1037-
}
1041+
Some(asan_lib)
1042+
} else {
1043+
None
1044+
};
10381045

10391046
unsafe {
10401047
AsanGiovese::init(&mut self.rt, emulator_modules.hooks().qemu_hooks());
10411048
}
10421049

10431050
*qemu_params = QemuParams::Cli(args);
1051+
1052+
self.asan_lib = asan_lib;
10441053
}
10451054

10461055
fn post_qemu_init<ET>(&mut self, _qemu: Qemu, emulator_modules: &mut EmulatorModules<ET, I, S>)
@@ -1056,12 +1065,23 @@ where
10561065

10571066
fn first_exec<ET>(
10581067
&mut self,
1059-
_qemu: Qemu,
1068+
qemu: Qemu,
10601069
emulator_modules: &mut EmulatorModules<ET, I, S>,
10611070
_state: &mut S,
10621071
) where
10631072
ET: EmulatorModuleTuple<I, S>,
10641073
{
1074+
if let Some(asan_lib) = &self.asan_lib {
1075+
let asan_mappings = qemu
1076+
.mappings()
1077+
.filter(|m| match m.path() {
1078+
Some(p) => p == asan_lib,
1079+
None => false,
1080+
})
1081+
.collect::<Vec<MapInfo>>();
1082+
self.asan_mappings = Some(asan_mappings);
1083+
}
1084+
10651085
emulator_modules.reads(
10661086
Hook::Function(gen_readwrite_asan::<ET, I, S>),
10671087
Hook::Function(trace_read_asan::<ET, I, S, 1>),
@@ -1173,11 +1193,21 @@ where
11731193
S: Unpin,
11741194
{
11751195
let h = emulator_modules.get_mut::<AsanModule>().unwrap();
1176-
if h.must_instrument(pc) {
1177-
Some(pc.into())
1178-
} else {
1179-
None
1196+
if !h.must_instrument(pc) {
1197+
return None;
1198+
}
1199+
1200+
// Don't sanitize the sanitizer!
1201+
if let Some(asan_mappings) = &h.asan_mappings {
1202+
if asan_mappings
1203+
.iter()
1204+
.any(|m| m.start() <= pc && pc < m.end())
1205+
{
1206+
return None;
1207+
}
11801208
}
1209+
1210+
Some(pc.into())
11811211
}
11821212

11831213
pub fn trace_read_asan<ET, I, S, const N: usize>(
@@ -1260,11 +1290,21 @@ where
12601290
S: Unpin,
12611291
{
12621292
let h = emulator_modules.get_mut::<AsanModule>().unwrap();
1263-
if h.must_instrument(pc) {
1264-
Some(pc.into())
1265-
} else {
1266-
Some(0)
1293+
if !h.must_instrument(pc) {
1294+
return Some(0);
12671295
}
1296+
1297+
// Don't sanitize the sanitizer!
1298+
if let Some(asan_mappings) = &h.asan_mappings {
1299+
if asan_mappings
1300+
.iter()
1301+
.any(|m| m.start() <= pc && pc < m.end())
1302+
{
1303+
return Some(0);
1304+
}
1305+
}
1306+
1307+
Some(pc.into())
12681308
}
12691309

12701310
pub fn trace_write_asan_snapshot<ET, I, S, const N: usize>(

0 commit comments

Comments
 (0)