Rust for Linux List
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: Yuan Tan <ytan089@ucr.edu>
Cc: ojeda@kernel.org, boqun@kernel.org,
	rust-for-linux@vger.kernel.org, zhiyunq@cs.ucr.edu,
	ardalan@uci.edu, pgovind2@uci.edu, dzueck@uci.edu,
	stable@vger.kernel.org
Subject: Re: [PATCH] rust: firmware: return empty slice for zero-size firmware
Date: Fri,  5 Jun 2026 10:10:42 +0300	[thread overview]
Message-ID: <20260605071104.135675-1-work@onurozkan.dev> (raw)
In-Reply-To: <20260605041134.38290-1-ytan089@ucr.edu>

On Thu, 04 Jun 2026 21:11:34 -0700
Yuan Tan <ytan089@ucr.edu> wrote:

> Firmware::data() builds a Rust slice with core::slice::from_raw_parts().
> Unlike many C APIs, from_raw_parts() requires its pointer argument to be
> non-NULL even when the length is zero.
> 
> The firmware loader can represent an empty firmware image with size == 0


I haven't checked in detail yet but "empty firmware image with size == 0"
sounds like an invalid image. Can such an image actually make it all the way
to Firmware::data()? I would be surprised if the loader accepted it.

Thanks,
Onur

> and data == NULL. Passing that pointer to from_raw_parts() would be
> undefined behavior.
> 
> Return an empty slice before constructing the raw slice. For non-zero
> firmware sizes, the existing firmware API guarantee that data has size
> bytes also means that the pointer is non-NULL.
> 
> Fixes: de6582833db0 ("rust: add firmware abstractions")
> Cc: stable@vger.kernel.org
> Reported-by: Priya Bala Govindasamy <pgovind2@uci.edu>
> Reported-by: Dylan Zueck <dzueck@uci.edu>
> Signed-off-by: Yuan Tan <ytan089@ucr.edu>
> ---
>  rust/kernel/firmware.rs | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs
> index 71168d8004e2..5e22a574a91e 100644
> --- a/rust/kernel/firmware.rs
> +++ b/rust/kernel/firmware.rs
> @@ -106,10 +106,17 @@ pub fn size(&self) -> usize {
>  
>      /// Returns the requested firmware as `&[u8]`.
>      pub fn data(&self) -> &[u8] {
> +        let size = self.size();
> +
> +        if size == 0 {
> +            return &[];
> +        }
> +
>          // SAFETY: `self.as_raw()` is valid by the type invariant. Additionally,
>          // `bindings::firmware` guarantees, if successfully requested, that
>          // `bindings::firmware::data` has a size of `bindings::firmware::size` bytes.
> -        unsafe { core::slice::from_raw_parts((*self.as_raw()).data, self.size()) }
> +        // For non-zero `size`, this also means `bindings::firmware::data` is not NULL.
> +        unsafe { core::slice::from_raw_parts((*self.as_raw()).data, size) }
>      }
>  }
>  
> -- 
> 2.43.2
> 

  reply	other threads:[~2026-06-05  7:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-05  4:11 [PATCH] rust: firmware: return empty slice for zero-size firmware Yuan Tan
2026-06-05  7:10 ` Onur Özkan [this message]
2026-06-05  8:13   ` Gary Guo
2026-06-05  9:16     ` Onur Özkan
2026-06-05  9:28       ` Gary Guo
2026-06-05 11:15         ` Miguel Ojeda
2026-06-05 13:11           ` Onur Özkan

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=20260605071104.135675-1-work@onurozkan.dev \
    --to=work@onurozkan.dev \
    --cc=ardalan@uci.edu \
    --cc=boqun@kernel.org \
    --cc=dzueck@uci.edu \
    --cc=ojeda@kernel.org \
    --cc=pgovind2@uci.edu \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=ytan089@ucr.edu \
    --cc=zhiyunq@cs.ucr.edu \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox