* [PATCH v2] rust: pwm: reject over-aligned driver data in `Chip::new()`
@ 2026-11-28 3:20 Priya Bala Govindasamy
2026-11-28 16:03 ` Gary Guo
0 siblings, 1 reply; 2+ messages in thread
From: Priya Bala Govindasamy @ 2026-11-28 3:20 UTC (permalink / raw)
To: m.wilczynski, ojeda, ukleinek, boqun, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, dakr, daniel.almeida, tamird,
acourbot, work, elle, linux-pwm, rust-for-linux, ardalan, zhiyunq,
dzueck, pgovind2
`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,
+ "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) };
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] rust: pwm: reject over-aligned driver data in `Chip::new()`
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
0 siblings, 0 replies; 2+ messages in thread
From: Gary Guo @ 2026-11-28 16:03 UTC (permalink / raw)
To: Priya Bala Govindasamy, m.wilczynski, ojeda, ukleinek, boqun,
gary, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
daniel.almeida, tamird, acourbot, work, elle, linux-pwm,
rust-for-linux, ardalan, zhiyunq, dzueck
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) };
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-28 16:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox