All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Danilo Krummrich" <dakr@kernel.org>
Cc: "Alexandre Courbot" <acourbot@nvidia.com>,
	ojeda@kernel.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v3 11/13] rust: drm: Wrap ioctl dispatch in RegistrationGuard
Date: Sat, 20 Jun 2026 01:13:46 +0000	[thread overview]
Message-ID: <20260620011346.A47D01F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260620005431.1562115-12-dakr@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Type inference hole in declare_drm_ioctls! allows safe handlers to trigger UB by inferring a Registered context before registration is verified.
- [High] Unbounded lifetimes for `data` and `file` in declare_drm_ioctls! allow safe ioctl handlers to stash references, causing use-after-free.
--

commit 28720068d91e18a10855651aab56d3a9e81f5d62
Author: Danilo Krummrich <dakr@kernel.org>

rust: drm: Wrap ioctl dispatch in RegistrationGuard

This commit wraps every ioctl handler inside a drm_dev_enter/exit critical
section using RegistrationGuard. It returns ENODEV if the device is unplugged
and anchors the driver type with a never-called closure to assist compiler
type inference.

> diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs
> index cf328101dde4e..c6a03be018e6d 100644
> --- a/rust/kernel/drm/ioctl.rs
> +++ b/rust/kernel/drm/ioctl.rs
[ ... ]
> @@ -135,6 +138,16 @@ macro_rules! declare_drm_ioctls {
>                              // dev/file match the current driver these ioctls are being declared
>                              // for, and it's not clear how to enforce this within the type system.
>                              let dev = $crate::drm::device::Device::from_raw(raw_dev);
> +
> +                            // Type-inference anchor: the closure is never called but ties `dev`'s
> +                            // type to `$func`'s first parameter, which the compiler cannot infer
> +                            // through method resolution and associated-type projections alone.
> +                            #[allow(unreachable_code)]
> +                            let _ = || $func(dev, unreachable!(), unreachable!());
> +
> +                            let Some(_guard) = dev.registration_guard() else {
> +                                return $crate::error::code::ENODEV.to_errno();
> +                            };

[Severity: High]
This is a pre-existing issue, but does this type inference anchor create a
scenario where safe driver code can trigger undefined behavior? 

If a driver author writes a safe ioctl handler that expects a registered
context, such as &Device<T, Registered>, the type-inference anchor will tie
the type of dev to that first parameter. This causes Device::from_raw() to be
evaluated with C = Registered.

Since Device::from_raw() is called before dev.registration_guard() can
verify the device is actually bound, doesn't this create a Registered
reference for an unplugged device? Would this violate the type invariant
and safety contract of from_raw() before the guard is even checked?

[Severity: High]
This is also a pre-existing issue, but are the lifetimes of the data and file
arguments passed to the user's $func properly constrained?

While the guard bounds the lifetime of the device access, the data and file
parameters instantiated from raw pointers later in this macro don't appear to
have explicit lifetime bounds. If $func demands a 'static lifetime for these
arguments, will the compiler incorrectly infer them as 'static?

If so, could a safe ioctl handler stash these references in a global variable
or async context, causing a use-after-free once the ioctl callback returns and
the C API invalidates the pointers?

>                              // SAFETY: The ioctl argument has size `_IOC_SIZE(cmd)`, which we
>                              // asserted above matches the size of this type, and all bit patterns of
>                              // UAPI structs must be valid.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260620005431.1562115-1-dakr@kernel.org?part=11

  reply	other threads:[~2026-06-20  1:13 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-20  0:51 [PATCH v3 00/13] rust: drm: Higher-Ranked Lifetime private data Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 01/13] rust: drm: rename Uninit DeviceContext to Normal Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 02/13] rust: drm: Add Driver::ParentDevice associated type Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 03/13] rust: drm: change default DeviceContext to Normal Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 04/13] rust: drm: restrict AlwaysRefCounted to Normal Device context Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 05/13] rust: drm: restrict AlwaysRefCounted to Normal GEM Object context Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 06/13] rust: drm: split Deref for Device context typestates Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 07/13] rust: drm: return ParentDevice from Device AsRef Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 08/13] rust: drm: add AsRef<ParentDevice<Bound>> for Device<Registered> Danilo Krummrich
2026-06-20  0:51 ` [PATCH v3 09/13] rust: drm: Add RegistrationGuard for drm_dev_enter/exit critical sections Danilo Krummrich
2026-06-20  1:12   ` sashiko-bot
2026-06-20  0:51 ` [PATCH v3 10/13] rust: drm: Add RegistrationData to drm::Driver Danilo Krummrich
2026-06-20  1:10   ` sashiko-bot
2026-06-20  0:51 ` [PATCH v3 11/13] rust: drm: Wrap ioctl dispatch in RegistrationGuard Danilo Krummrich
2026-06-20  1:13   ` sashiko-bot [this message]
2026-06-20  0:51 ` [PATCH v3 12/13] rust: drm: Pass registration data to ioctl handlers Danilo Krummrich
2026-06-20  1:16   ` sashiko-bot
2026-06-20  0:51 ` [PATCH v3 13/13] drm: nova: Use drm::Device<Registered> to access the parent bus device Danilo Krummrich
2026-06-20  1:17   ` sashiko-bot
2026-06-20 16:52 ` [PATCH v3 00/13] rust: drm: Higher-Ranked Lifetime private data Danilo Krummrich

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=20260620011346.A47D01F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=ojeda@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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.