Skip to content

Commit

Permalink
Merge pull request #1396 from micahsnyder/CLAM-2710-ldb-load-assert
Browse files Browse the repository at this point in the history
Fix a possible crash when loading a malformed logical signature
  • Loading branch information
micahsnyder authored Jan 10, 2025
2 parents d6d25c3 + 89711e1 commit de3be9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libclamav_rust/src/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub unsafe extern "C" fn _evidence_add_indicator(
indicator_type: IndicatorType,
err: *mut *mut FFIError,
) -> bool {
let name_str = validate_str_param!(name);
let name_str = validate_str_param!(name, err = err);

let mut evidence = ManuallyDrop::new(Box::from_raw(evidence as *mut Evidence));

Expand Down
28 changes: 28 additions & 0 deletions libclamav_rust/src/ffi_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ mod tests {
/// let blah = validate_str_param!(blah);
/// # }
/// ```
/// ```edition2018
/// use util::validate_str_param;
///
/// # pub extern "C" fn _my_c_interface(blah: *const c_char) -> sys::cl_error_t {
/// let blah = validate_str_param!(blah, err = err);
/// # }
/// ```
#[macro_export]
macro_rules! validate_str_param {
($ptr:ident) => {
Expand All @@ -305,4 +312,25 @@ macro_rules! validate_str_param {
}
}
};

($ptr:ident, err=$err:ident) => {
if $err.is_null() {
warn!("{} is NULL", stringify!($err));
return false;
} else if $ptr.is_null() {
warn!("{} is NULL", stringify!($ptr));
return false;
} else {
#[allow(unused_unsafe)]
match unsafe { CStr::from_ptr($ptr) }.to_str() {
Err(e) => {
warn!("{} is not valid unicode: {}", stringify!($ptr), e);

*$err = Box::into_raw(Box::new(e.into()));
return false;
}
Ok(s) => s,
}
}
};
}
2 changes: 1 addition & 1 deletion libclamav_rust/src/fuzzy_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub unsafe extern "C" fn _fuzzy_hash_load_subsignature(
subsig_id: u32,
err: *mut *mut FFIError,
) -> bool {
let hexsig = validate_str_param!(hexsig);
let hexsig = validate_str_param!(hexsig, err=err);

let mut hashmap = ManuallyDrop::new(Box::from_raw(fuzzy_hashmap as *mut FuzzyHashMap));

Expand Down

0 comments on commit de3be9e

Please sign in to comment.