Skip to content

Unsound usages of unsafe implementation from len to u16 #278

@llooFlashooll

Description

@llooFlashooll

Hi, I am scanning the pelite in the latest version with my own static analyzer tool.

Unsafe conversion found at: src/resources/mod.rs#L86

	#[inline]
	fn slice_ws(&self, offset: u32) -> Result<&'a [u16]> {
		let offset = offset as usize;
		// Alignment checking
		if !cfg!(feature = "unsafe_alignment") && offset & 1 != 0 {
			return Err(Error::Misaligned);
		}
		// The name is prefixed by its length in words
		let len = self.section.get(offset..offset + 2).ok_or(Error::Bounds)?;
		let len = unsafe { *(len.as_ptr() as *const u16) } as usize;
		// Extract the name given its length
		let name = self.section.get(offset + 2..offset + 2 + len * 2).ok_or(Error::Bounds)?;
		let name = unsafe { slice::from_raw_parts(name.as_ptr() as *const u16, len) };
		Ok(name)
	}

This unsound implementation would create a misalignment issues if the type size of len.as_ptr() is smaller than the type size of u16.

This would potentially cause undefined behaviors in Rust. If we further manipulate the problematic converted types, it would potentially lead to different consequences such as access out-of-bound. I am reporting this issue for your attention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions