-
Notifications
You must be signed in to change notification settings - Fork 17
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
Unsound Implementation of get_mut in Orderbook Allows Undefined Behavior #3
Comments
same problem for ninjabook/src/fixed_orderbook.rs Line 303 in 7f3b7d3
|
here is my PoC:
Result:
Considering this is a Unsoundness problem and published to crates.io I suggest report it to RustSec. |
Hello, thank you for noticing. However, that orderbook's only use is to run benchmarks, as stated in the file
Just like the Feel free to code a nicer version that can beat benchmarks against In the meantime i will close this Issue, thanks again. |
Description
The get_mut function in the Orderbook implementation uses unsafe code to call Buffer::get_unchecked_mut, bypassing bounds checking. This function is unsound because it allows access to an arbitrary index without ensuring that the index is within bounds. If the caller provides an out-of-bounds index, the program will invoke undefined behavior (UB).
ninjabook/src/fixed_orderbook.rs
Line 307 in 7f3b7d3
Problems:
Unsafe Access Without Validation:
The function uses unsafe to call get_unchecked_mut without verifying that index is within bounds. This shifts the responsibility of ensuring safety to the caller, violating Rust's safety guarantees.
No Documentation or Safety Contract:
There is no documentation specifying the requirements for the index parameter. The caller is not informed that the index must be valid, leading to potential misuse.
Potential Undefined Behavior:
If index is out of bounds, accessing the buffer through get_unchecked_mut will cause undefined behavior. This could lead to memory corruption, crashes, or other unpredictable outcomes.
Suggestion
unsafe
and comment a doc about safety precondition.assert
in the function body.Additional Context
Rust's unsafe blocks are a powerful tool but must be used with extreme care. By bypassing bounds checks in get_mut, the current implementation sacrifices safety for performance.
The text was updated successfully, but these errors were encountered: