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

Custom Read on uninitialized buffer may cause undefined behavior #2

Open
JOE1994 opened this issue Mar 2, 2021 · 0 comments
Open

Comments

@JOE1994
Copy link

JOE1994 commented Mar 2, 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

Methods parser::Parser::<R>::read_resref, parser::Parser::<R>::read_bytes, raw::Gff::read and macro read_into creates an uninitialized buffer and passes it to user-provided Read implementation. This is unsound, because it allows safe Rust code to exhibit an undefined behavior (read from uninitialized memory).

serde-gff/src/parser/mod.rs

Lines 248 to 257 in f5b0d10

pub fn read_resref(&mut self, index: ResRefIndex) -> Result<ResRef> {
self.seek(index)?;
let size = self.reader.read_u8()? as usize;
let mut bytes = Vec::with_capacity(size);
unsafe { bytes.set_len(size); }
self.reader.read_exact(&mut bytes)?;
Ok(ResRef(bytes))
}

serde-gff/src/parser/mod.rs

Lines 335 to 343 in f5b0d10

#[inline]
fn read_bytes(&mut self) -> Result<Vec<u8>> {
let size = self.read_u32()? as usize;
let mut bytes = Vec::with_capacity(size);
unsafe { bytes.set_len(size); }
self.reader.read_exact(&mut bytes)?;
Ok(bytes)
}

serde-gff/src/raw.rs

Lines 315 to 337 in f5b0d10

pub fn read<R: Read + Seek>(reader: &mut R) -> Result<Gff> {
let header = Header::read(reader)?;
let structs = read_exact!(reader, header.structs, Struct);
let fields = read_exact!(reader, header.fields , Field);
reader.seek(SeekFrom::Start(header.labels.offset as u64))?;
let mut labels = Vec::with_capacity(header.labels.count as usize);
for _ in 0..header.labels.count {
let mut label = [0u8; 16];
reader.read_exact(&mut label)?;
labels.push(label.into());
}
reader.seek(SeekFrom::Start(header.field_data.offset as u64))?;
let mut field_data = Vec::with_capacity(header.field_data.count as usize);
unsafe { field_data.set_len(header.field_data.count as usize); }
reader.read_exact(&mut field_data[..])?;
let field_indices = read_into!(reader, header.field_indices);
let list_indices = read_into!(reader, header.list_indices);
Ok(Gff { header, structs, fields, labels, field_data, field_indices, list_indices })
}

serde-gff/src/raw.rs

Lines 290 to 298 in f5b0d10

macro_rules! read_into {
($reader:expr, $section:expr) => ({
$reader.seek(SeekFrom::Start($section.offset as u64))?;
let mut vec = Vec::with_capacity($section.count as usize);
unsafe { vec.set_len(($section.count / 4) as usize); }
$reader.read_u32_into::<LE>(&mut vec[..])?;
vec
});
}

In case a user-provided Read reads from the given buffer, uninitialized buffer can make safe Rust code to cause memory safety errors by miscompilation. Uninitialized values are lowered to LLVM as llvm::UndefValue which may take different random values for each read. Propagation of UndefValue can quickly cause safe Rust code to exhibit undefined behavior.

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.

How to fix the issue?

The Naive & safe way to fix the issue is to always zero-initialize a buffer before lending it to a user-provided Read implementation. Note that this approach will add runtime performance overhead of zero-initializing the buffer.

As of March 2021, there is not yet an ideal fix that works with no performance overhead. Below are links to relevant discussions & suggestions for the fix.

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

No branches or pull requests

1 participant