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

Missing Divide-by-Zero Check #268

Closed
JonathanWoollett-Light opened this issue Nov 20, 2023 · 0 comments · Fixed by #258
Closed

Missing Divide-by-Zero Check #268

JonathanWoollett-Light opened this issue Nov 20, 2023 · 0 comments · Fixed by #258
Assignees

Comments

@JonathanWoollett-Light
Copy link
Contributor

From @y-x41

While reviewing the vm-memory Rust crate, it was noticed that the function AtomicBitmap::new() does not properly check the page_size parameter for being zero. As this parameter is being used as a divisor within the function, a value of zero leads to a Divide-by-zero CPU fault resulting in an application panic. The below listing shows the affected code snippet.

While the function is solely used within the rust-vmm ecosystem with valid page_size values unequal to zero, the function is public and might be used to construct a bitmap by other 3rd-party-crates using vm-memory.

#[allow(clippy::len_without_is_empty)]
impl AtomicBitmap {
    /// Create a new bitmap of `byte_size`, with one bit per page. This is effectively
    /// rounded up, and we get a new vector of the next multiple of 64 bigger than `bit_size`.
    pub fn new(byte_size: usize, page_size: usize) -> Self {
        let mut num_pages = byte_size / page_size;
        if byte_size % page_size > 0 {
            num_pages += 1;
        }

        // Adding one entry element more just in case `num_pages` is not a multiple of `64`.
        let map_size = num_pages / 64 + 1;
        let map: Vec<AtomicU64> = (0..map_size).map(|_| AtomicU64::new(0)).collect();

        AtomicBitmap {
            map,
            size: num_pages,
            page_size,
        }
    }
    // [...]
}

X41 advises performing thorough input parameter validation to prevent any possibility of encountering a Divide-by-zero situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant