Rust for Linux List
 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 v2] rust: pwm: reject over-aligned driver data in `Chip::new()`
Date: Sat, 28 Nov 2026 16:03:16 +0000	[thread overview]
Message-ID: <DN6YYRN9LCNA.2B6TA03Z7L33G@garyguo.net> (raw)
In-Reply-To: <20261128032029.9516-1-pgovind2@uci.edu>

On Sat Nov 28, 2026 at 3:20 AM GMT, Priya Bala Govindasamy wrote:
> `PwmOps` does not restrict the alignment of driver data type `T`. In
> `Chip::new()`, the call to `pwmchip_alloc()` allocates memory for `T` and
> aligns it to `PWMCHIP_ALIGN` only. A `T` requiring greater alignment
> can receive a misaligned pointer.
>
> This is a soundness issue and it is not triggered on any of the in-tree
> users.
>
> Fix this by rejecting over-aligned types in `Chip::new()` by asserting
> that T's alignment does not exceed PWMCHIP_ALIGN.
>
> Fixes: d8046cd50879 ("rust: pwm: Add complete abstraction layer")
> Reported-by: Dylan Zueck <dzueck@uci.edu>
> Signed-off-by: Priya Bala Govindasamy <pgovind2@uci.edu>
> ---
> This issue was detected by FerroLens, an LLM-assisted analysis tool.
>
> Changes since v1:
>  - Moved the definition of PWMCHIP_ALIGN to include/linux/pwm.h and removed it from drivers/pwm/core.c.
>  - Simplified commit message.
>
> Link to v1: https://lore.kernel.org/all/caddec5478a0d39a73152247a60bc21b3c609cc7.1794600927.git.pgovind2@uci.edu/
>
>  drivers/pwm/core.c              | 2 --
>  include/linux/pwm.h             | 5 ++++-
>  rust/bindgen_parameters         | 1 +
>  rust/bindings/bindings_helper.h | 3 +++
>  rust/kernel/pwm.rs              | 5 +++++
>  5 files changed, 13 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> index 4c6d716e5fde..520e5b788958 100644
> --- a/drivers/pwm/core.c
> +++ b/drivers/pwm/core.c
> @@ -1594,8 +1594,6 @@ static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
>  	}
>  }
>  
> -#define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
> -
>  static void *pwmchip_priv(struct pwm_chip *chip)
>  {
>  	return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
> diff --git a/include/linux/pwm.h b/include/linux/pwm.h
> index b11ae91723f8..efd14a777b44 100644
> --- a/include/linux/pwm.h
> +++ b/include/linux/pwm.h
> @@ -490,8 +490,11 @@ void pwmchip_remove(struct pwm_chip *chip);
>  
>  /*
>   * For FFI wrapper use only:
> - * The Rust PWM abstraction needs this to properly free the pwm_chip.
> + * The Rust PWM abstraction uses PWMCHIP_ALIGN to check the alignment
> + * requirement of the pwm_chip's driver data.
> + * pwmchip_release is needed to properly free the pwm_chip.
>   */
> +#define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
>  void pwmchip_release(struct device *dev);
>  
>  int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
> diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters
> index 5bab3cf06336..8e70ef2e34fb 100644
> --- a/rust/bindgen_parameters
> +++ b/rust/bindgen_parameters
> @@ -50,6 +50,7 @@
>  # recognized, block generation of the non-helper constants.
>  --blocklist-item ARCH_SLAB_MINALIGN
>  --blocklist-item ARCH_KMALLOC_MINALIGN
> +--blocklist-item PWMCHIP_ALIGN
>  --blocklist-item VM_MERGEABLE
>  --blocklist-item VM_READ
>  --blocklist-item VM_WRITE
> diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
> index 3791af0aadc7..2af0af4f6858 100644
> --- a/rust/bindings/bindings_helper.h
> +++ b/rust/bindings/bindings_helper.h
> @@ -114,6 +114,9 @@
>  /* `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_KMALLOC_MINALIGN = ARCH_KMALLOC_MINALIGN;
> +#if IS_REACHABLE(CONFIG_PWM)
> +const size_t RUST_CONST_HELPER_PWMCHIP_ALIGN = PWMCHIP_ALIGN;
> +#endif
>  const size_t RUST_CONST_HELPER_PAGE_SIZE = PAGE_SIZE;
>  const size_t RUST_CONST_HELPER_GENLMSG_DEFAULT_SIZE = GENLMSG_DEFAULT_SIZE;
>  const gfp_t RUST_CONST_HELPER_GFP_ATOMIC = GFP_ATOMIC;
> diff --git a/rust/kernel/pwm.rs b/rust/kernel/pwm.rs
> index 8aa47304bec3..7fd93cc644bb 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::PWMCHIP_ALIGN,

align_of is in the kernel prelude.

Best,
Gary

> +            "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) };



      reply	other threads:[~2026-11-28 16:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28  3:20 [PATCH v2] rust: pwm: reject over-aligned driver data in `Chip::new()` Priya Bala Govindasamy
2026-11-28 16:03 ` Gary Guo [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=DN6YYRN9LCNA.2B6TA03Z7L33G@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