Skip to content

Unsoundness of safe reciprocal_mg10 #550

@shinmao

Description

@shinmao

Hello, we are PhD researchers of Rust's safety. We find there are potential undefined behaviors in the safe API reciprocal_mg10, which is also visible to external crate user so require to add some safety check.

OOB in safe reciprocal_mg10

debug_assert!(d >= (1 << 63));
let d = Wrapping(d);
let d0 = d & ONE;
let d9 = d >> 55;
let d40 = ONE + (d >> 24);
let d63 = (d + ONE) >> 1;
// let v0 = Wrapping(TABLE[(d9.0 - 256) as usize] as u64);
let v0 = Wrapping(*unsafe { TABLE.get_unchecked((d9.0 - 256) as usize) } as u64);

Even though line 79 has added the check to guarantee d > 0, debug_assert! will be optimized and removed in the release mode. Therefore, potential attackers could still pass 0 as d to the function to trigger out-of-bound access at line 87. Please consider using assert! rather than debug_assert!.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions