From 7a521850cfce3f0b879210a34907380642c3b220 Mon Sep 17 00:00:00 2001 From: Josh Ferrell Date: Thu, 6 Jun 2024 21:48:58 -0400 Subject: Fix DWARF parser reporting as valid for any binary with a build id --- rust/examples/dwarf/dwarf_import/src/helpers.rs | 44 +++++++++++++++++-------- rust/examples/dwarf/dwarf_import/src/lib.rs | 6 ++-- rust/examples/dwarf/shared/src/lib.rs | 4 +-- 3 files changed, 35 insertions(+), 19 deletions(-) (limited to 'rust/examples') diff --git a/rust/examples/dwarf/dwarf_import/src/helpers.rs b/rust/examples/dwarf/dwarf_import/src/helpers.rs index 8c19f4ad..babe9713 100644 --- a/rust/examples/dwarf/dwarf_import/src/helpers.rs +++ b/rust/examples/dwarf/dwarf_import/src/helpers.rs @@ -432,17 +432,22 @@ pub(crate) fn download_debug_info(view: &BinaryView) -> Result, } -pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result>, String> { +pub(crate) fn find_local_debug_file(view: &BinaryView) -> Option { let settings = Settings::new(""); let debug_info_paths = settings.get_string_list("analysis.debugInfo.debugDirectories", Some(view), None); + if debug_info_paths.is_empty() { - return Ok(None) + return None } + let build_id = match get_build_id(view) { + Ok(x) => x, + Err(_) => return None, + }; + for debug_info_path in debug_info_paths.into_iter() { if let Ok(path) = PathBuf::from_str(&debug_info_path.to_string()) { - let build_id = get_build_id(view)?; let elf_path = path .join(&build_id[..2]) .join(&build_id[2..]) @@ -452,7 +457,6 @@ pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result Result Result>, String> { + if let Some(debug_file_path) = find_local_debug_file(view) { + Ok( + binaryninja::load_with_options( + debug_file_path, + false, + Some("{\"analysis.debugInfo.internal\": false}") + ) + ) + } + else { + Ok(None) + } } diff --git a/rust/examples/dwarf/dwarf_import/src/lib.rs b/rust/examples/dwarf/dwarf_import/src/lib.rs index 1f4fce4d..71367bfc 100644 --- a/rust/examples/dwarf/dwarf_import/src/lib.rs +++ b/rust/examples/dwarf/dwarf_import/src/lib.rs @@ -271,8 +271,8 @@ struct DWARFParser; impl CustomDebugInfoParser for DWARFParser { fn is_valid(&self, view: &BinaryView) -> bool { dwarfreader::is_valid(view) || - dwarfreader::can_use_build_id(view) || - dwarfreader::can_use_debuginfod(view) + dwarfreader::can_use_debuginfod(view) || + (dwarfreader::has_build_id_section(view) && helpers::find_local_debug_file(view).is_some()) } fn parse_info( @@ -283,7 +283,7 @@ impl CustomDebugInfoParser for DWARFParser { progress: Box Result<(), ()>>, ) -> bool { let external_file = if !dwarfreader::is_valid(bv) { - if dwarfreader::can_use_build_id(bv) { + if dwarfreader::has_build_id_section(bv) { if let Ok(Some(debug_view)) = helpers::load_debug_info_for_build_id(bv) { Some(debug_view) } diff --git a/rust/examples/dwarf/shared/src/lib.rs b/rust/examples/dwarf/shared/src/lib.rs index e3d1315d..febbe3ed 100644 --- a/rust/examples/dwarf/shared/src/lib.rs +++ b/rust/examples/dwarf/shared/src/lib.rs @@ -54,12 +54,12 @@ pub fn is_raw_dwo_dwarf(view: &BinaryView) -> bool { } pub fn can_use_debuginfod(view: &BinaryView) -> bool { - can_use_build_id(view) && + has_build_id_section(view) && Settings::new("") .get_bool("network.enableDebuginfod", Some(view), None) } -pub fn can_use_build_id(view: &BinaryView) -> bool { +pub fn has_build_id_section(view: &BinaryView) -> bool { if let Ok(raw_view) = view.raw_view() { return raw_view.section_by_name(".note.gnu.build-id").is_ok() } -- cgit v1.3.1