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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading on uninitialized buffer may cause UB #3

Closed
JOE1994 opened this issue Jan 6, 2021 · 1 comment 路 Fixed by #4
Closed

Reading on uninitialized buffer may cause UB #3

JOE1994 opened this issue Jan 6, 2021 · 1 comment 路 Fixed by #4

Comments

@JOE1994
Copy link
Contributor

JOE1994 commented Jan 6, 2021

Hello 馃 ,
we (Rust group @sslab-gatech) found a memory-safety/soundness issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.

Issue Description

let mut body_bytes = Vec::with_capacity(length);
unsafe {
body_bytes.set_len(length);
}
proceed_if_filled!(cursor.read(&mut body_bytes), length);

let mut tracing_bytes = Vec::with_capacity(UUID_LEN);
unsafe {
tracing_bytes.set_len(UUID_LEN);
}
body_cursor.read_exact(&mut tracing_bytes)?;

We found two cases where an uninitialized buffer is created (body_bytes & tracing_bytes) and passed to user-provided Read implementation. This is unsound, because it allows safe Rust code to exhibit an undefined behavior (read from uninitialized memory).

This part from the Read trait documentation explains the issue:

It is your responsibility to make sure that buf is initialized before calling read. Calling read with an uninitialized buf (of the kind one obtains via MaybeUninit<T>) is not safe, and can lead to undefined behavior.

Suggested Fix

It is safe to zero-initialize the newly allocated u8 buffer before read(), in order to prevent user-provided Read from accessing old contents of the newly allocated heap memory.

Thank you for checking out this issue 馃憤

@AlexPikalov
Copy link
Owner

Thank you @JOE1994

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.

2 participants