Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/sigsegv on module import #69

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion yari-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn main() -> Result<()> {
match matches.subcommand() {
Some(("dump", sub_matches)) => {
let module = Module::from_str(sub_matches.get_one::<String>("MODULE").unwrap())?;
context.dump_module(module);
context.dump_module(module)?;
}
_ => {
// Start interactive shell
Expand Down
13 changes: 0 additions & 13 deletions yari-sys/README.md

This file was deleted.

18 changes: 13 additions & 5 deletions yari-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,9 @@ impl Context {
}

/// Import and initialize `module`.
fn import_module(&mut self, module: Module) {
fn import_module(&mut self, module: Module) -> Result<(), YariError> {
if self.modules.contains_key(&module) {
return;
return Ok(());
}

debug!("Importing module {:?}", module);
Expand All @@ -749,8 +749,14 @@ impl Context {
}
.cast();

if new_module.is_null() {
return Err(YariError::SymbolNotFound(module.to_string()));
}

self.modules.insert(module, new_module);
self.init_objects_cache(new_module);

Ok(())
}

pub fn get_object(&self, path: &str) -> Option<&*mut YR_OBJECT> {
Expand Down Expand Up @@ -1148,7 +1154,7 @@ impl Context {
// Import module used in expression
let expr_module = expr.get_module();
if let Some(module) = expr_module {
self.import_module(module);
self.import_module(module)?;
}

let rule_ctx = self.get_rule_context(rule_name)?;
Expand Down Expand Up @@ -1329,14 +1335,16 @@ impl Context {
) as *mut YR_MATCHES;
}

pub fn dump_module(&mut self, module: Module) {
self.import_module(module);
pub fn dump_module(&mut self, module: Module) -> Result<(), YariError> {
self.import_module(module)?;
match self.modules.get(&module) {
Some(module) => {
self.visit_structure(module.cast::<YR_OBJECT>(), 0);
}
None => error!("Module '{}' not found", module),
}

Ok(())
}

fn visit_structure(&self, structure_ptr: *const YR_OBJECT, depth: usize) {
Expand Down
8 changes: 4 additions & 4 deletions yari-sys/tests/tests_dump_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_function_dump_plain() {
let mut context = common::context();

for module in MODULES {
context.dump_module(*module);
context.dump_module(*module).unwrap();
}
}

Expand All @@ -16,7 +16,7 @@ fn test_function_dump_cuckoo() {
let mut context = common::context_with_cuckoo();

for module in MODULES {
context.dump_module(*module);
context.dump_module(*module).unwrap();
}
}

Expand All @@ -25,7 +25,7 @@ fn test_function_dump_pe() {
let mut context = common::context_with_pe_sample_and_rule();

for module in MODULES {
context.dump_module(*module);
context.dump_module(*module).unwrap();
}
}

Expand All @@ -34,6 +34,6 @@ fn test_function_dump_elf() {
let mut context = common::context_with_elf_sample();

for module in MODULES {
context.dump_module(*module);
context.dump_module(*module).unwrap();
}
}