Re: [PATCH] drm/tyr: fw: fix integer overflow and inverted range check in Cursor

From: Alice Ryhl

Date: Fri Nov 27 2026 - 12:15:41 EST


On Fri, Nov 27, 2026 at 08:41:07PM +0800, Bilal Hashim wrote:
> In drivers/gpu/drm/tyr/fw/parser.rs, the firmware binary parser `Cursor`
> has two bounds-checking bugs that can panic the kernel on malformed
> firmware images:

Note that neither of these can actually trigger a panic on malformed firmware
images today. `Cursor` is private to `parser.rs`, and its callers already
guarantee these conditions:

- `view()` is only called in `parse_entry()`, which already checks
`entry_size >= size_of::<EntryHeader>()` and computes
`entry_end = entry_start.checked_add(entry_size)?`, so
`self.cursor.pos() <= entry_end` always holds.
- `read()` is only called with `nbytes` in `{1, 2, 4}`, and `advance()` is
only called with `section_hdr_size <= 248` (since `EntryHeader::size()` is
an 8-bit field). Because `self.pos <= self.data.len() <= isize::MAX as
usize`, `self.pos + nbytes` cannot overflow `usize` even on 32-bit systems.

Hardening `Cursor` so its bounds checks are self-contained is still nice to
have, so please reword the commit message to describe this as defensive
hardening rather than fixing a reachable kernel panic (and no `Fixes:` tag
is needed).

> @@ -144,7 +144,7 @@ impl<'a> Cursor<'a> {
> ///
> /// Returns an error if the advance would exceed the data bounds.
> fn advance(&mut self, nbytes: usize) -> Result {
> - if self.pos + nbytes > self.data.len() {
> + let Some(end) = self.pos.checked_add(nbytes).filter(|&e| e <= self.data.len()) else {

This line fails `rustfmt --check` (please run `make LLVM=1 rustfmt`).

Alice