- 
                Notifications
    
You must be signed in to change notification settings  - Fork 29
 
Description
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
metadata::read_vorbis_comment_block()method
Lines 432 to 438 in 2f05385
| let mut vendor_bytes = Vec::with_capacity(vendor_len as usize); | |
| // We can safely set the lenght of the vector here; the uninitialized memory | |
| // is not exposed. If `read_into` succeeds, it will have overwritten all | |
| // bytes. If not, an error is returned and the memory is never exposed. | |
| unsafe { vendor_bytes.set_len(vendor_len as usize); } | |
| try!(input.read_into(&mut vendor_bytes)); | 
Lines 473 to 475 in 2f05385
| let mut comment_bytes = Vec::with_capacity(comment_len as usize); | |
| unsafe { comment_bytes.set_len(comment_len as usize); } | |
| try!(input.read_into(&mut comment_bytes)); | 
metadata::read_application_block()method
Lines 544 to 546 in 2f05385
| let mut data = Vec::with_capacity(length as usize - 4); | |
| unsafe { data.set_len(length as usize - 4); } | |
| try!(input.read_into(&mut data)); | 
Methods metadata::read_vorbis_comment_block() & metadata::read_application_block() create an uninitialized buffer and passes it to user-provided ReadBytes 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
bufis initialized before callingread. Calling read with an uninitializedbuf(of the kind one obtains viaMaybeUninit<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_into(), in order to prevent user-provided Read from accessing old contents of the newly allocated heap memory.
Also, there are two nightly features for handling such cases.
- https://doc.rust-lang.org/std/io/struct.Initializer.html
 - https://rust-lang.github.io/rfcs/2930-read-buf.html
 
Thank you for checking out this issue 👍