mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Karl Mehltretter <kmehltretter@gmail.com>
To: Shuai Liu <847807867@qq.com>
Cc: Karl Mehltretter <kmehltretter@gmail.com>,
	linux-spi@vger.kernel.org, rust-for-linux@vger.kernel.org,
	Mark Brown <broonie@kernel.org>, Miguel Ojeda <ojeda@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 2/3] rust: spi: add SPI driver abstractions
Date: Tue, 27 Oct 2026 04:36:32 +0100	[thread overview]
Message-ID: <20261027033632.77840-1-kmehltretter@gmail.com> (raw)
In-Reply-To: <tencent_66EE264CDD33A4355946E42D9ACA7019C409@qq.com>

> +    pub fn write_then_read(&self, write_buf: &[u8], read_buf: &mut [u8]) -> Result {
> +        let n_tx = u32::try_from(write_buf.len())?;
> +        let n_rx = u32::try_from(read_buf.len())?;

Could we check n_tx.checked_add(n_rx) here? spi_write_then_read() adds
these as unsigned int when sizing its bounce buffer. The individual
conversions don't prevent the sum from wrapping.

On a 64-bit kernel, VVec can provide two disjoint 2 GiB slices. It uses
vmalloc, so the kmalloc limit doesn't bound the inputs. With a bound
device and a successful 4 GiB allocation:

    use kernel::alloc::{flags::GFP_KERNEL, VVec};

    let mut storage = VVec::<u8>::zeroed(1usize << 32, GFP_KERNEL)?;
    let (tx, rx) = storage.split_at_mut(1usize << 31);
    dev.write_then_read(tx, rx)?;

Both lengths fit in u32, but their sum wraps to zero. C then selects
or allocates only SPI_BUFSIZ bytes and attempts to copy 2 GiB into it.
The allocation size has already wrapped before kmalloc sees it.

The C helper is intended for small transfers, but this safe wrapper
accepts arbitrary slices. Bound doesn't constrain their lengths.

Found during source review with AI assistance. I haven't compiled or
run this example, and I don't know of an in-tree caller passing such
large buffers.

Thanks,
Karl

  reply	other threads:[~2026-10-27  3:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261027030352.615926-1-847807867@qq.com>
2026-10-27  3:03 ` [PATCH v5 1/3] rust: helpers: add SPI helpers Shuai Liu
2026-10-27  3:03 ` [PATCH v5 2/3] rust: spi: add SPI driver abstractions Shuai Liu
2026-10-27  3:36   ` Karl Mehltretter [this message]
2026-10-27  3:03 ` [PATCH v5 3/3] samples: rust: add SPI driver sample Shuai Liu

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=20261027033632.77840-1-kmehltretter@gmail.com \
    --to=kmehltretter@gmail.com \
    --cc=847807867@qq.com \
    --cc=broonie@kernel.org \
    --cc=dakr@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=ojeda@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®