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 <[email protected]> --- --- 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(()) } }
