From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796063411; b=JAqL+ihvfW4lbARUuSQaj0bPrRG681r6q9sqfHlNCkzZsSAZb2jWJlLvMp6q7CLYLKRr n5NC3MvwjcslPVA69LOZGDqSU4FRXff3y91PVduXZJA4QFggt1m62NnSm6MRbTqPGbJok mgiZ5DhFuUfqYXjQ/hPXQIiZJXHUJkN0Gsj6bnqvpgrcwYyR3wk5Kk5zYjdmOR1OkrWEz xWc/o5oFyes4a1VexNfomFArMHJQzY+h7NdYp9kULOXy/9OnjYj1X46pgFm2SI4nh6Jt2 GOCxbwadG1/bag+GHRMgNlB11SooLcXWjNgzVnV9Ewo+TEEzXMsHftPf4KBigJPe1Sg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796063411; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=VQk79rwWYqS/8NfeI4mFgaYZDR/epb4hPa3mm1Pzq0w=; b=SpAJiG5cjiGFsJD9bgBEFMGzZwCje+WLgxY38t64bBDEJaLSUTXl1F7uJzemNZhhq/H4 rmBIP8gWW2fjea7PxG+YFIcrYnPrL9VHg2nQsWIYLrNpPkoPXs6u05qzA7X54MVmappJh 6DnT8QxPS/YtmkrsykYBynwmLFvtcpmBxKihLzZ5pW9lBdzEXO+GLhrDzwvwtlCtFVHtL s0YGShYHoIkAjclbIb1LYh5WPLvs9em95E+V4IBP2omWV8PqCSBQviRB23yrD+Flwz0Ll hqYGW7anGyLLar63o67DU7tbL/Q3VAECh7Mph6hHKENHnlWQyWrxipVD4XKOjJqfkVg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=uci.edu; spf=pass smtp.mailfrom=uci.edu; dkim=pass header.d=uci.edu header.i=@uci.edu header.a=rsa-sha256 header.s=google header.b=JnH7rMVY; arc=none smtp.remote-ip=74.125.82.44 Received: from mail-dl1-f44.google.com (mail-dl1-f44.google.com [74.125.82.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD3A0606A329 for ; Mon, 30 Nov 2026 18:30:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CD3A0606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uci.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=uci.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=uci.edu header.i=@uci.edu header.a=rsa-sha256 header.s=google header.b=JnH7rMVY Received: by mail-dl1-f44.google.com with SMTP id a92af1059eb24-1b5bbedcdbeso2902c88.0 for ; Mon, 30 Nov 2026 10:30:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uci.edu; s=google; t=1796063410; x=1796668210; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=VQk79rwWYqS/8NfeI4mFgaYZDR/epb4hPa3mm1Pzq0w=; b=JnH7rMVY0tFthcMtvO484yADyzveNIVq1+0+vuYU+FYYM8kWrAI4k/Z+xRY+IQobt0 xwvprCA69+WqcWjJ2fB1704acGWX8HASkz7c+gRJ8rbaQwyzM3/zYtrmucK16UqNlO0V pltSylal9fAVlQlQdt4j2pU59t+c2G1OcK45IYcrwaoipeyXauJzd0/40jVvh6FMGL7J 0x8gd/FXCAN3iNl0kNY7ME1e4WSGWcjZbIylaDKTEG4CTyIMozscTmnoMUzioRAemymU 2oWEFQiuaquLgjpDQGXxv9gGUj51LP7waXSL4wj5fHbfyInqlqYT1+CiTUJWpBnekI6u nNag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796063410; x=1796668210; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=VQk79rwWYqS/8NfeI4mFgaYZDR/epb4hPa3mm1Pzq0w=; b=k7/bgGDU1R+EIDa+Zm7nmJPeloVZYt5GqltqmY45b9ZpaFijiQ1DLNy6dgX0igZ927 h4oWyplJpwZd6Orl/YiCnIN+mw5wkHis230+2iI88BntQa2kmygrSnayCzozI6sCA+5d 93OTEH6eFJh6QCNLh+BCn1NoO9UQSSKAxH2Jw4AfnsZZQZY4cqki5hZQ5jMDsrtiYHWF H8OeC2Ne9gOdMVWUshIrWyww9YfM2dvt7Q/yyv9I83qJxNnQZy38U+F9fGFBcwZbkLUw QdTmd1c3LrMag18lYv5S/EDQQTMsaNQxiVpXBTFLBDOyB7SyUdJfEwZnTZbMULXdWzwa TmPQ== X-Forwarded-Encrypted: i=1; AL+hErEJzdn4I7Q9QP4QEp4FVhawhE2qTUDlTIvPbjAVwkRGMuaMkUB8s1vqUV/tncvOVNbAVF8i0uRf9QEJ0fGZ/A==@vger.kernel.org X-Gm-Message-State: APuU4Z/Sb+gFynX5K6rMJJzmMV8m8pP1fJ9IZH51koafZLLLmNJ5uQ+t m1IsU3Q80MchjTE0frIRFJV/4l0s+2LLsRo+i4mAf//5w9xzbEsis1lEPOo8lN5zyY8= X-Gm-Gg: AZFdTZdJiKWiG2VHw7X6krFqA0YGCzfKwd9jVpxFqrwTJ92RgxxrW0IKNOOQMKf1D/Q Kv2aAt1CMatcgE8Is2QVfBFcE4mHDqhB/SaWJLPePeLGa4AnQTioaIqsROU3Oitvl9So9Q0GcoB BQIY7sIk587zQlJYSLlnwVGKCfNlssGZzGRzO2hZSWhR1cuKa8b2pbkbcmcM8m3v1MOSqXWytPm LcYPsSTNQHW2MDaGIm3qXXwcnBsNaIDKReYutodQLmRigfjodIKKwMoms3drzUCPx7ihe4DRWmT H1S2f+tXWbfq6t8bMHx9TWXYO5yT2mHb3/2ZDauhJFl0ocXc7PynYBe+YIWZmqjQs9Vv7CdUzKU ZK8IGnn899IRqmAxMH5bHK/sFx0vPEH0Ax8w46xcD1JVtQVIZ/bentUX+xx9RxfDfMP0dsZ0exC Ctfg6Q2gt39oVyL2Q61fxuBI96EJz4pXd/YG3t6nG9FRxv/dfKJKigSFGHSdXymY9BRacP8qJQG RVInLdE/yZPfKUpjZcIjeCbcopEQcB2qrCy7OzbLY8v1xXYmLA/W9FRcekvwVCnwPcJCPYvT++3 DCOhT/HaaDlySk7tXYfsy23nE8+bgz5+kjpWqV1+6tt5JEXahASBiOvygt16rNl1ajgcfJdySvl Xn/EpwegcKPbMzQ== X-Received: by 2002:a05:7022:7f14:b0:1b5:39fb:c6ba with SMTP id a92af1059eb24-1b5568fe968mr30760663c88.35.1796063409669; Mon, 30 Nov 2026 10:30:09 -0800 (PST) Received: from guest1.. (charm.ics.uci.edu. [128.195.4.118]) by smtp.googlemail.com with ESMTPSA id a92af1059eb24-1b55684c85fsm121356992c88.14.2026.11.30.10.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 10:30:08 -0800 (PST) From: Priya Bala Govindasamy To: 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, pgovind2@uci.edu Subject: [PATCH v3] rust: pwm: reject over-aligned driver data in `Chip::new()` Date: Mon, 30 Nov 2026 18:30:07 +0000 Message-Id: <20261130183007.15225-1-pgovind2@uci.edu> X-Mailer: git-send-email 2.34.1 X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit `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 Signed-off-by: Priya Bala Govindasamy --- This issue was detected by FerroLens, an LLM-assisted analysis tool. Changes since v2: - Use `align_of` from kernel prelude. 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/ Link to v2: https://lore.kernel.org/all/20261128032029.9516-1-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..7e18ea4f209a 100644 --- a/rust/kernel/pwm.rs +++ b/rust/kernel/pwm.rs @@ -587,6 +587,11 @@ pub fn new<'a>( data: impl pin_init::PinInit, ) -> Result> { let sizeof_priv = core::mem::size_of::(); + const_assert!( + align_of::() <= 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