* [PATCH] drm/tyr: fw: fix integer overflow and inverted range check in Cursor
@ 2026-11-27 12:41 Bilal Hashim
2026-11-27 12:45 ` sashiko-bot
2026-11-27 17:15 ` Alice Ryhl
0 siblings, 2 replies; 4+ messages in thread
From: Bilal Hashim @ 2026-11-27 12:41 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich
Cc: dri-devel, rust-for-linux, linux-kernel, Bilal Hashim
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:
1. `Cursor::view()` checks `range.start < self.pos || range.end >
self.data.len()`, but does not check `range.start > range.end`. An
inverted range passes the check and panics when indexing
`&self.data[range]`.
2. `Cursor::read()` and `Cursor::advance()` compute `self.pos + nbytes`
without overflow checking. On 32-bit systems (or with large `nbytes`),
the addition can wrap around to a value less than `self.data.len()`,
bypassing the bounds check and panicking in `&self.data[start..end]`
or corrupting `self.pos`.
Use `checked_add()` in `read()` and `advance()`, and reject inverted
ranges (`range.start > range.end`) in `view()`.
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -75,7 +75,7 @@ impl<'a> Cursor<'a> {
///
/// This spawns a new cursor, leaving the current cursor unchanged.
fn view(&self, range: Range<usize>) -> Result<Cursor<'_>> {
- if range.start < self.pos || range.end > self.data.len() {
+ if range.start > range.end || range.start < self.pos || range.end > self.data.len() {
dev_err!(
self.dev,
"Invalid cursor range {:?} for data of length {}",
@@ -98,9 +98,9 @@ impl<'a> Cursor<'a> {
/// Returns an error if the read would exceed the data bounds.
fn read(&mut self, nbytes: usize) -> Result<&[u8]> {
let start = self.pos;
- let end = start + nbytes;
+ let end = start.checked_add(nbytes);
- if end > self.data.len() {
+ let Some(end) = end.filter(|&e| e <= self.data.len()) else {
dev_err!(
self.dev,
"Invalid firmware file: read of size {} at position {} is out of bounds",
@@ -108,9 +108,9 @@ impl<'a> Cursor<'a> {
start,
);
return Err(EINVAL);
- }
+ };
- self.pos += nbytes;
+ self.pos = end;
Ok(&self.data[start..end])
}
@@ -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 {
dev_err!(
self.dev,
"Invalid firmware file: advance of size {} at position {} is out of bounds",
@@ -152,8 +152,8 @@ impl<'a> Cursor<'a> {
self.pos,
);
return Err(EINVAL);
- }
- self.pos += nbytes;
+ };
+ self.pos = end;
Ok(())
}
}
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] drm/tyr: fw: fix integer overflow and inverted range check in Cursor
2026-11-27 12:41 [PATCH] drm/tyr: fw: fix integer overflow and inverted range check in Cursor Bilal Hashim
@ 2026-11-27 12:45 ` sashiko-bot
2026-11-27 17:15 ` Alice Ryhl
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-27 12:45 UTC (permalink / raw)
To: Bilal Hashim; +Cc: dri-devel, ojeda, gary
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] drm/tyr: missing Fixes trailer for Cursor bounds and overflow
fix
--
commit b90a1dd0329f798d9b7953db9ea5076afe11d6da
Author: Bilal Hashim <AeeSec@126.com>
> drm/tyr: fw: fix integer overflow and inverted range check in Cursor
>
> 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:
>
> 1. `Cursor::view()` checks `range.start < self.pos || range.end >
> self.data.len()`, but does not check `range.start > range.end`. An
> inverted range passes the check and panics when indexing
> `&self.data[range]`.
> 2. `Cursor::read()` and `Cursor::advance()` compute `self.pos + nbytes`
> without overflow checking. On 32-bit systems (or with large `nbytes`),
> the addition can wrap around to a value less than `self.data.len()`,
> bypassing the bounds check and panicking in `&self.data[start..end]`
> or corrupting `self.pos`.
>
> Use `checked_add()` in `read()` and `advance()`, and reject inverted
> ranges (`range.start > range.end`) in `view()`.
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>
[Severity: Low]
This fixes bounds-checking bugs in Cursor::view(), Cursor::read(), and
Cursor::advance(). Should the commit message include a Fixes tag?
Fixes: aa9ce8201360 ("drm/tyr: add parser for firmware binary")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127124107.1782-1-AeeSec@126.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] drm/tyr: fw: fix integer overflow and inverted range check in Cursor
2026-11-27 12:41 [PATCH] drm/tyr: fw: fix integer overflow and inverted range check in Cursor Bilal Hashim
2026-11-27 12:45 ` sashiko-bot
@ 2026-11-27 17:15 ` Alice Ryhl
2026-11-28 2:01 ` [PATCH v2] drm/tyr: fw: harden Cursor bounds checks Bilal Hashim
1 sibling, 1 reply; 4+ messages in thread
From: Alice Ryhl @ 2026-11-27 17:15 UTC (permalink / raw)
To: Bilal Hashim
Cc: Alice Ryhl, Daniel Almeida, Danilo Krummrich, dri-devel,
rust-for-linux, linux-kernel
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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2] drm/tyr: fw: harden Cursor bounds checks
2026-11-27 17:15 ` Alice Ryhl
@ 2026-11-28 2:01 ` Bilal Hashim
0 siblings, 0 replies; 4+ messages in thread
From: Bilal Hashim @ 2026-11-28 2:01 UTC (permalink / raw)
To: Alice Ryhl, Daniel Almeida, Danilo Krummrich
Cc: dri-devel, rust-for-linux, linux-kernel, Bilal Hashim
In drivers/gpu/drm/tyr/fw/parser.rs, `Cursor` currently relies on its
callers to uphold non-inverted ranges and non-overflowing sizes:
1. `Cursor::view()` checks `range.start < self.pos || range.end >
self.data.len()`, without checking `range.start > range.end`.
2. `Cursor::read()` and `Cursor::advance()` compute `self.pos + nbytes`
without `checked_add()`.
Although current private callers in `parser.rs` already uphold these
preconditions, make `Cursor`'s bounds checks self-contained as defensive
hardening by rejecting inverted ranges in `view()` and using
`checked_add()` in `read()` and `advance()`.
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
Changes in v2:
- Reword commit message to describe this as defensive hardening rather
than a reachable kernel panic (per Alice Ryhl).
- Format `advance()` with `rustfmt`.
---
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -75,7 +75,7 @@
///
/// This spawns a new cursor, leaving the current cursor unchanged.
fn view(&self, range: Range<usize>) -> Result<Cursor<'_>> {
- if range.start < self.pos || range.end > self.data.len() {
+ if range.start > range.end || range.start < self.pos || range.end > self.data.len() {
dev_err!(
self.dev,
"Invalid cursor range {:?} for data of length {}",
@@ -98,9 +98,9 @@
/// Returns an error if the read would exceed the data bounds.
fn read(&mut self, nbytes: usize) -> Result<&[u8]> {
let start = self.pos;
- let end = start + nbytes;
+ let end = start.checked_add(nbytes);
- if end > self.data.len() {
+ let Some(end) = end.filter(|&e| e <= self.data.len()) else {
dev_err!(
self.dev,
"Invalid firmware file: read of size {} at position {} is out of bounds",
@@ -108,9 +108,9 @@
start,
);
return Err(EINVAL);
- }
+ };
- self.pos += nbytes;
+ self.pos = end;
Ok(&self.data[start..end])
}
@@ -144,7 +144,11 @@
///
/// 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 {
dev_err!(
self.dev,
"Invalid firmware file: advance of size {} at position {} is out of bounds",
@@ -152,8 +156,8 @@
self.pos,
);
return Err(EINVAL);
- }
- self.pos += nbytes;
+ };
+ self.pos = end;
Ok(())
}
}
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-28 2:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 12:41 [PATCH] drm/tyr: fw: fix integer overflow and inverted range check in Cursor Bilal Hashim
2026-11-27 12:45 ` sashiko-bot
2026-11-27 17:15 ` Alice Ryhl
2026-11-28 2:01 ` [PATCH v2] drm/tyr: fw: harden Cursor bounds checks Bilal Hashim
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.