All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Alice Ryhl <aliceryhl@google.com>,
	Daniel Almeida <daniel.almeida@collabora.com>,
	Danilo Krummrich <dakr@kernel.org>
Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, Bilal Hashim <AeeSec@126.com>
Subject: [PATCH v2] drm/tyr: fw: harden Cursor bounds checks
Date: Sat, 28 Nov 2026 10:01:08 +0800	[thread overview]
Message-ID: <20261128020108.834-1-AeeSec@126.com> (raw)
In-Reply-To: <20261127171532.2097157-1-aliceryhl@google.com>

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(())
     }
 }


      reply	other threads:[~2026-11-28  2:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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   ` Bilal Hashim [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128020108.834-1-AeeSec@126.com \
    --to=aeesec@126.com \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.