diff options
| author | Josh Ferrell <josh@vector35.com> | 2024-06-06 21:48:58 -0400 |
|---|---|---|
| committer | Josh Ferrell <josh@vector35.com> | 2024-06-06 21:48:58 -0400 |
| commit | 7a521850cfce3f0b879210a34907380642c3b220 (patch) | |
| tree | 1579d60fc5e110cd3dfc958fb29923723bb28256 /rust/examples | |
| parent | 783a3cb0f4c2f2943653293937010b3de1aaf900 (diff) | |
Fix DWARF parser reporting as valid for any binary with a build id
Diffstat (limited to 'rust/examples')
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/helpers.rs | 44 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/lib.rs | 6 | ||||
| -rw-r--r-- | rust/examples/dwarf/shared/src/lib.rs | 4 |
3 files changed, 35 insertions, 19 deletions
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<Ref<BinaryView>, } -pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result<Option<Ref<BinaryView>>, String> { +pub(crate) fn find_local_debug_file(view: &BinaryView) -> Option<String> { 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<Option<R .join(&build_id[..2]) .join(format!("{}.debug", &build_id[2..])); - let options = "{\"analysis.debugInfo.internal\": false}"; let final_path = if debug_ext_path.exists() { debug_ext_path } @@ -460,17 +464,29 @@ pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result<Option<R elf_path } else { - // No paths exist - return Ok(None) + // No paths exist in this dir, try the next one + continue; }; - return Ok( - binaryninja::load_with_options( - final_path.to_string_lossy().to_string(), - false, - Some(options) - ) - ); + return final_path + .to_str() + .and_then(|x| Some(x.to_string())); } } - Ok(None) + None +} + + +pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result<Option<Ref<BinaryView>>, 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<dyn Fn(usize, usize) -> 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() } |
