public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
	"Danilo Krummrich" <dakr@kernel.org>
Cc: "Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH 0/4] rust: add pointer projection infrastructure and convert DMA
Date: Sun, 15 Feb 2026 15:39:57 +0100	[thread overview]
Message-ID: <DGFM36CRX0U7.2RPN7REFTGFTW@kernel.org> (raw)
In-Reply-To: <CANiq72kt-it2y8iAR6uA-EuJ8pVygwvOV70MSd5+vYwq1Tp9Bg@mail.gmail.com>

On Sun Feb 15, 2026 at 1:47 AM CET, Miguel Ojeda wrote:
> On Sat, Feb 14, 2026 at 11:34 AM Danilo Krummrich <dakr@kernel.org> wrote:
>>
>> Miguel: If you don't mind I would like to take this one through the driver-core
>> tree as well, as it is not only the base for this fix, but also the base for the
>> upcoming I/O infrastructure work.
>
> Is there some code that is currently "actually" buggy, i.e. not just a
> soundness bug, but actually triggering UB or similar?

I investigated this a bit in the beginning and I don't think this can be
triggered by us at the moment.

Here is a comprehensive explanation (maybe Gary can pick this up for the
commit message?):

The unsoundness comes from the fact that in the current version of the
`dma_read!` macro does not prevent the field access from "going through
a deref". Here is an example of an expression experiencing UB:

    dma_read!(dma[0].derefable.field)

Given `dma: &CoherentAllocation<Derefable>` and

    struct Derefable {
        inner: KBox<Struct>,
    }

    impl Deref for Derefable {
        type Target = Struct;

        fn deref(&self) -> &Struct {
            self.inner.deref()
        }
    }

    struct Struct {
        field: Field,
    }

The `dma_read!` creates a raw pointer to the dma allocation at the
provided offset. It then offsets that pointer according to the macro
input:  `&raw const (*raw_ptr).derefable.field`. This is the step that
produces UB:
- the Rust compiler finds no field named `field` on the `Derefable`
  type. It thus checks if it implements `Deref`, which it does, so it
  proceeds to try the target type.
- The target type is `Struct`, which does have a field named `field`, so
  it desugars the raw pointer offset to this:

    &raw const Deref::deref(&(*raw_ptr).derefable).field

This thus creates a *reference* into the dma-coherent memory, which
AFAIK is UB. It then uses that to call the `Deref::deref` function,
which reads from dma without volatile and then dereferences the read
value again.

Now there is a lint called `dangerous_implicit_autorefs` [1], which
exists to warn specifically on these implicit reference conversions. It
was introduced as error-by-default in Rust 1.89, so as long as we test
with latest stable, we should catch these.

An additional "layer of defense" is that `CoherentAllocation` requires
the generic to implement the `FromBytes` trait, which is incompatible
with "being a pointer" (this impl is missing from the example code
above).

I discussed a related question on the rust-lang zulip in which we also
talked about this unsoundness. The recommendation from the Rust
developers was to **not** rely on a lint for soundness [2]. There are
several ways to disable lints and it's not guaranteed that the lint will
catch everything.

[1]: https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#dangerous-implicit-autorefs
[2]: https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/dangerous_implicit_autorefs.20and.20Box/near/572196666

Cheers,
Benno

      parent reply	other threads:[~2026-02-15 14:40 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-14  5:33 [PATCH 0/4] rust: add pointer projection infrastructure and convert DMA Gary Guo
2026-02-14  5:33 ` [PATCH 1/4] rust: add projection infrastructure Gary Guo
2026-02-14  9:53   ` Benno Lossin
2026-02-14 10:36     ` Gary Guo
2026-02-14 14:48       ` Benno Lossin
2026-02-14 10:27   ` Danilo Krummrich
2026-02-22  0:57   ` Benno Lossin
2026-02-22 10:52     ` Gary Guo
2026-02-14  5:33 ` [PATCH 2/4] rust: dma: generalize `dma_{read,write}` macro Gary Guo
2026-02-14 10:04   ` Benno Lossin
2026-02-14 10:46     ` Gary Guo
2026-02-14 14:53       ` Benno Lossin
2026-02-14  5:33 ` [PATCH 3/4] gpu: nova-core: convert to use new `dma_write!` syntax Gary Guo
2026-02-14 10:06   ` Benno Lossin
2026-02-14  5:33 ` [PATCH 4/4] rust: dma: remove old dma_{read,write} macro compatibility syntax Gary Guo
2026-02-14 10:05   ` Benno Lossin
2026-02-14 10:33 ` [PATCH 0/4] rust: add pointer projection infrastructure and convert DMA Danilo Krummrich
2026-02-14 10:50   ` Gary Guo
2026-02-14 11:00     ` Danilo Krummrich
2026-02-15  0:47   ` Miguel Ojeda
2026-02-15 11:06     ` Danilo Krummrich
2026-02-15 12:06       ` Miguel Ojeda
2026-02-15 12:56         ` Danilo Krummrich
2026-02-15 15:16           ` Miguel Ojeda
2026-02-15 17:02             ` Danilo Krummrich
2026-02-18 10:49               ` Miguel Ojeda
2026-02-18 18:25                 ` Danilo Krummrich
2026-02-15 14:39     ` Benno Lossin [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=DGFM36CRX0U7.2RPN7REFTGFTW@kernel.org \
    --to=lossin@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.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