Linux PWM subsystem development
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Priya Bala Govindasamy" <pgovind2@uci.edu>,
	<m.wilczynski@samsung.com>, <ojeda@kernel.org>,
	<ukleinek@kernel.org>, <boqun@kernel.org>, <gary@garyguo.net>,
	<bjorn3_gh@protonmail.com>, <lossin@kernel.org>,
	<a.hindborg@kernel.org>, <aliceryhl@google.com>,
	<tmgross@umich.edu>, <dakr@kernel.org>,
	<daniel.almeida@collabora.com>, <tamird@kernel.org>,
	<acourbot@nvidia.com>, <work@onurozkan.dev>,
	<elle@weathered-steel.dev>, <linux-pwm@vger.kernel.org>,
	<rust-for-linux@vger.kernel.org>, <ardalan@uci.edu>,
	<zhiyunq@cs.ucr.edu>, <dzueck@uci.edu>
Subject: Re: [PATCH 1/1] rust: pwm: reject over-aligned driver data in `Chip::new()`
Date: Wed, 25 Nov 2026 16:19:30 +0000	[thread overview]
Message-ID: <DN4FFKMFI488.187QX5RI55L8S@garyguo.net> (raw)
In-Reply-To: <caddec5478a0d39a73152247a60bc21b3c609cc7.1794600927.git.pgovind2@uci.edu>

On Fri Nov 13, 2026 at 8:25 PM GMT, Priya Bala Govindasamy wrote:
> `Chip::new()` does not check the alignment of the driver data type `T`.
> This allows a Rust kernel module to choose an arbitrary alignment for `T`.
>
> `pwmchip_alloc()` reserves `size_of::<T>()` bytes for the driver's
> private data. `pwmchip_priv()` aligns this storage to `PWMCHIP_ALIGN`,
> which is defined as `ARCH_DMA_MINALIGN`. But, `PwmOps` does not restrict
> the alignment of `T`, so a caller may provide a `T` whose required
> alignment is greater than `ARCH_DMA_MINALIGN`.

The commit message is overly verbose slop, please simplify it. Just state the
issue (`pwmchip_alloc` aligns to `PWMCHIP_ALIGN` only) and the fix (reject
drvdata with higher alignment with const_assert).

>
> This can result in `drvdata_ptr` pointing to misaligned storage for `T`
> in the underlying C allocation.
> If `drvdata_ptr` is misaligned for `T`, that breaks the safety
> requirements of `pin_init::raw_try_init()` which requires a valid,
> aligned pointer as an argument. This is undefined behavior.
>
> Additionally, `Chip::drvdata()` can be called using the `Chip` produced by
> `Chip::new()`. It creates a reference to `T` from the misaligned storage
> which is also undefined behavior.

These all follows from underalignments and there's no need to do this
explicitly.

>
> Reject over-aligned types at compile time by asserting that T's alignment
> does not exceed ARCH_DMA_MINALIGN.
>
> This issue was detected by FerroLens, an LLM-assisted analysis tool, and
> was then manually analyzed and reproduced.

This should be a note and not commit message. Move it below the ---.

Best,
Gary

> Fixes: d8046cd50879 ("rust: pwm: Add complete abstraction layer")
> Reported-by: Dylan Zueck <dzueck@uci.edu>
> Signed-off-by: Priya Bala Govindasamy <pgovind2@uci.edu>
> ---
>  rust/bindgen_parameters         | 1 +
>  rust/bindings/bindings_helper.h | 1 +
>  rust/kernel/pwm.rs              | 5 +++++
>  3 files changed, 7 insertions(+)
>
> diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters
> index 5bab3cf06336..59d0a6518032 100644
> --- a/rust/bindgen_parameters
> +++ b/rust/bindgen_parameters
> @@ -49,6 +49,7 @@
>  # We use const helpers to aid bindgen, to avoid conflicts when constants are
>  # recognized, block generation of the non-helper constants.
>  --blocklist-item ARCH_SLAB_MINALIGN
> +--blocklist-item ARCH_DMA_MINALIGN
>  --blocklist-item ARCH_KMALLOC_MINALIGN
>  --blocklist-item VM_MERGEABLE
>  --blocklist-item VM_READ
> diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
> index 3791af0aadc7..4ffd714f9229 100644
> --- a/rust/bindings/bindings_helper.h
> +++ b/rust/bindings/bindings_helper.h
> @@ -113,6 +113,7 @@
>  
>  /* `bindgen` gets confused at certain things. */
>  const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
> +const size_t RUST_CONST_HELPER_ARCH_DMA_MINALIGN = ARCH_DMA_MINALIGN;
>  const size_t RUST_CONST_HELPER_ARCH_KMALLOC_MINALIGN = ARCH_KMALLOC_MINALIGN;
>  const size_t RUST_CONST_HELPER_PAGE_SIZE = PAGE_SIZE;
>  const size_t RUST_CONST_HELPER_GENLMSG_DEFAULT_SIZE = GENLMSG_DEFAULT_SIZE;
> diff --git a/rust/kernel/pwm.rs b/rust/kernel/pwm.rs
> index 8aa47304bec3..7823bb56d17a 100644
> --- a/rust/kernel/pwm.rs
> +++ b/rust/kernel/pwm.rs
> @@ -587,6 +587,11 @@ pub fn new<'a>(
>          data: impl pin_init::PinInit<T, Error>,
>      ) -> Result<UnregisteredChip<'a, T>> {
>          let sizeof_priv = core::mem::size_of::<T>();
> +        const_assert!(
> +            core::mem::align_of::<T>() <= bindings::ARCH_DMA_MINALIGN,
> +            "PWM driver data is over-aligned"
> +        );
> +
>          // SAFETY: `pwmchip_alloc` allocates memory for the C struct and our private data.
>          let c_chip_ptr_raw =
>              unsafe { bindings::pwmchip_alloc(parent_dev.as_raw(), num_channels, sizeof_priv) };



  parent reply	other threads:[~2026-11-25 16:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 20:25 [PATCH 0/1] rust: pwm: reject over-aligned driver data in `Chip::new()` Priya Bala Govindasamy
2026-11-13 20:25 ` [PATCH 1/1] " Priya Bala Govindasamy
2026-11-25 15:42   ` Michal Wilczynski
2026-11-25 16:19   ` Gary Guo [this message]
2026-11-25 17:32     ` Uwe Kleine-König
2026-11-25 17:45       ` Gary Guo
2026-11-25 23:15         ` Uwe Kleine-König

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=DN4FFKMFI488.187QX5RI55L8S@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=ardalan@uci.edu \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dzueck@uci.edu \
    --cc=elle@weathered-steel.dev \
    --cc=linux-pwm@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=m.wilczynski@samsung.com \
    --cc=ojeda@kernel.org \
    --cc=pgovind2@uci.edu \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=ukleinek@kernel.org \
    --cc=work@onurozkan.dev \
    --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