rust: pwm: reject over-aligned driver data in `Chip::new()`
Subsystems (3)
PATCH v2 1/1 rust: pwm: reject over-aligned driver data in `Chip::new()`
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.
---
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(-)
drivers/pwm/core.c +0 -2
@@ -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 );
include/linux/pwm.h +4 -1
@@ -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_MINALIGNvoid pwmchip_release (struct device *dev);int __devm_pwmchip_add (struct device *dev,struct pwm_chip *chip,struct module *owner);
rust/bindgen_parameters +1 -0
@@ -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
rust/bindings/bindings_helper.h +3 -0
@@ -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 ;
rust/kernel/pwm.rs +5 -0
@@ -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) };
Signature · 1 line
2.34.1
Gary Guo
reviewerQuoted 91 lines · Priya Bala Govindasamy
PwmOpsdoes not restrict the alignment of driver data typeT. InChip::new(), the call topwmchip_alloc()allocates memory forTand aligns it toPWMCHIP_ALIGNonly. ATrequiring 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 exceedPWMCHIP_ALIGN.Fixes d8046cd50879 ("rust: pwm: Add complete abstraction layer")Reported-by Dylan Zueck <[email protected]>Signed-off-by Priya Bala Govindasamy <[email protected]>---
This issue was detected by FerroLens, an LLM-assisted analysis tool.Changes since v1:
- Moved the definition ofPWMCHIP_ALIGNtoinclude/linux/pwm.hand removed it fromdrivers/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(-)drivers/pwm/core.c +0
-2@@ -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 );include/linux/pwm.h +4
-1@@ -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_MINALIGNvoid pwmchip_release (struct device *dev);int __devm_pwmchip_add (struct device *dev,struct pwm_chip *chip,struct module *owner);rust/bindgen_parameters +1
-0@@ -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_WRITErust/bindings/bindings_helper.h +3
-0@@ -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 ;rust/kernel/pwm.rs +0
-0
rust/kernel/pwm.rs pub fn new<'a>( · line 591
Quoted 5 lines
rust/kernel/pwm.rs +2
-0@@ -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
Quoted 6 lines
rust/kernel/pwm.rs +3
-0+"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) };