From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335356; b=Snqij/sIQXwSTBzZ13enIrPys8/s/LtDXxRAiRLgBfY+ABXaM8crV3ZBMQoDK20EhE3S 4cKvewX6fO0xzUVKjQxtRFXd2YKqqsvIFsdNzJKhF2RtME+9z+rS7pMbWR9af6ZrBQPXE 5pHytOc1GzmFsXFC0DY7DdqqZbXvoPmF9Zq6GKp17UpV9t+QohDhx60hqAdwUn3gUvn5v E+a4hwbyUco+JgPe/Ev+cyjIZKGnqprqhayuifChpb+8tn/WJv70VZbfgoNu2MjkVyNfZ uPZCTJQcK2cZF8Q/3QzlHLY3e5bqSFzLnN+BXQI3gZEkivE60BD+NGeNy7+Wtc73Q4A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335356; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=/XC2aZBgrnmZNwNMqmLRqK/Yn3ku7BmPwdprhBKwyE4=; b=GBn4HyAah9VFAUXTVjpkip03FPieySW57vtiS53E3JY6coLyHOftcwbjnWNyTxRdv3g5 uvgKOxptxVgwG1R2stnc+NGfAVclpnyMGqTu2etRdyVZzGWn9us3DbsEtkzAMdxF+9woy eErxnTTFmP6qHDYTTW2Rb63vq0HiproQY6iAOxK3OgXUWnfM0/+T4L2RVwRy2FdTduiP2 YLOriNka38xN+B+GqAOiAodpJepEoasxbE2TtXYhv8DSoAB7lhQZE0//0X7QQlX/PBCuq FXgJ6O1KgPpMvjOgTYxSfgaCkCsIoeQjrQS1w14O/xM3qn+6uyVLDRKOrqrCCYML9rQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZxI16uNL; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8DC6B606A32C; Thu, 3 Dec 2026 22:02:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZxI16uNL Received: by smtp.kernel.org (Postfix) with ESMTPSA id D04CC1F0092B; Thu, 3 Dec 2026 22:02:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796335356; bh=/XC2aZBgrnmZNwNMqmLRqK/Yn3ku7BmPwdprhBKwyE4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=ZxI16uNLj6+93Rak2bKVqYIspJDB9T+udpxjPQKerXy85I+FrBiCZPxXTcGRC+wRg uRqzN0P8uTv5N2UWo2G1UWXUww1ufijomHHwAW5E6ipM4Rt2xfxFx9++JZh6XEPhVW 6fsBq7l+u/idU0uh4bBjOsM8u601Vd8wrsjpkNEPEfpSbi1nLjWI87Kl3NoYteGWHE YiGlW8eeIKbg5JWB+mZXesSOO8sGsfOKqTap889dPyMaCOjtTqJxYLkUXumZFY4wff I7gSPrztfYzllNTRbxbXawEshLiw3M+8IizK5T+UgvBfzp+qDvCPpW+O1FGPMhLIvY vYsdQXgXV+f6A== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v29 1/4] rust: leds: Add basic led classdev abstractions Reply-To: sashiko-reviews@lists.linux.dev To: "Markus Probst" Cc: linux-leds@vger.kernel.org, linux-pci@vger.kernel.org, lee@kernel.org, ojeda@kernel.org, gary@garyguo.net In-Reply-To: <20261203-rust_leds-v29-1-dc8a8ccb96d7@posteo.de> References: <20261203-rust_leds-v29-0-dc8a8ccb96d7@posteo.de> <20261203-rust_leds-v29-1-dc8a8ccb96d7@posteo.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 22:02:35 +0000 X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] rust: leds: missing kCFI cfi_encoding for enum led_brightness in callbacks - [High] rust: leds: double-put of software_node fwnode in PinnedDrop::drop() - [Medium] rust: leds: safe DeviceBuilder::build() allows UAF via core::mem::forget() - [Medium] rust: leds: aliased mutable references in Adapter::blink_set_callback() - [Low] rust: leds: T::MAX_BRIGHTNESS checks mismatch runtime max_brightness overrides - [Low] rust: leds: DeviceBuilder typestate allows invalid states and drops errors - [Low] rust: leds: missing inline on Color::try_from() and Device::deref() -- commit 3198ee5b6a5df02b501695f833e966524250cb17 Author: Markus Probst rust: leds: Add basic led classdev abstractions This commit adds basic Rust abstractions for LED class devices, including the LedOps trait, DeviceBuilder, and the Device wrapper around struct led_classdev. > diff --git a/rust/kernel/led.rs b/rust/kernel/led.rs > new file mode 100644 > index 0000000000000..e21b81e863cbe > --- /dev/null > +++ b/rust/kernel/led.rs [ ... ] > +impl DeviceBuilder<'static, S> { > + /// Creates a new [`DeviceBuilder`]. > + #[inline] > + #[expect( > + clippy::new_without_default, > + reason =3D "no need and derive is prevented by S" > + )] > + pub fn new() -> Self { > + Self { > + fwnode: None, > + name: None, > + devicename: None, > + devname_mandatory: false, > + initial_brightness: 0, > + color: Color::default(), > + _p: PhantomData, > + } > + } > +} > + > +impl<'init> DeviceBuilder<'init, Unnamed> { > + /// Sets the firmware node. > + #[inline] > + pub fn fwnode(self, fwnode: Option>) -> Self { > + Self { fwnode, ..self } > + } [Severity: Low] Can DeviceBuilder::::new() without calling name() or DeviceBuilder::::new() with fwnode set to None ever succeed? Because DeviceBuilder::build() always sets default_label to NULL, led_compose_name() returns -EINVAL when both fwnode and default_label are NULL; taking the required name or ARef in the constructors would enforce this at compile time. Also, hardcoding 'static on DeviceBuilder::new() instead of 'init prevents passing a non-'static &'init CStr to name() or devicename() on a local builder variable. In addition, Adapter::brightness_set_callback() silently discards errors returned by LedOps::brightness_set() when BLOCKING is false, which is not documented on LedOps. [ ... ] > +impl TryFrom for Color { > + type Error =3D zerocopy::ValidityError; > + > + fn try_from(value: u32) -> core::result::Result { > + try_transmute!(value) > + } > +} [Severity: Low] This isn't a bug, but Color::try_from() here and Device::deref() in rust/kernel/led/normal.rs are single-expression kernel crate methods missing #[inline]. In addition, struct Device in rust/kernel/led/normal.rs lacks an # Invariants doc section (and matching // INVARIANT: comments in DeviceBuilder::build()) for the registration and validity invariants relied on by PinnedDrop::drop(). > diff --git a/rust/kernel/led/normal.rs b/rust/kernel/led/normal.rs > new file mode 100644 > index 0000000000000..a22c29cfd2628 > --- /dev/null > +++ b/rust/kernel/led/normal.rs [ ... ] > +impl<'init, S: DeviceBuilderState> DeviceBuilder<'init, S> { > + /// Registers a new [`Device`]. > + pub fn build<'bound: 'init, T: LedOps + 'bound>( > + self, > + parent: &'bound device::Device, > + ops: impl PinInit + 'init, > + ) -> impl PinInit, Error> + 'init { [Severity: Medium] Can a safe caller trigger a use-after-free or NULL pointer dereference by leaking the returned Device<'bound, T> with core::mem::forget()? DeviceBuilder::build() is a safe function that registers led_classdev directly with led_classdev_register_ext() without devres cleanup, relying on PinnedDrop::drop() to call led_classdev_unregister(). Leaking a heap-pinned Device<'bound, T> via core::mem::forget() is safe in Rust and leaves the classdev registered after 'bound ends or parent unbinds, so subsequent C callbacks dereference dangling 'bound references in ops and led_module_get() dereferences a NULL parent->driver->owner. Marking build() unsafe with a no-forget contract (like MiscDeviceRegistration::register()) or tying unregistration to devres would close this hole. > + const_assert!(T::MAX_BRIGHTNESS <=3D i32::MAX.unsigned_abs() || = !T::HAS_BRIGHTNESS_GET); [Severity: Low] Does this compile-time check match the runtime led_cdev->max_brightness after led_classdev_register_ext() runs? led_classdev_register_ext() overwrites led_cdev->max_brightness from the fwnode "max-brightness" property without clamping to T::MAX_BRIGHTNESS or i32::MAX, and replaces 0 with LED_FULL (255). As a result, brightness_set() can receive values above T::MAX_BRIGHTNESS, a firmware value above i32::MAX causes brightness_get_callback() to fail with -EINVAL, and debug_assert!(*val <=3D T::MAX_BRIGHTNESS) in brightness_get_callback() can panic under CONFIG_RUST_DEBUG_ASSERTIONS=3Dy when returning a value valid f= or (*led_cdev).max_brightness. [ ... ] > + unsafe extern "C" fn brightness_set_callback( > + led_cdev: *mut bindings::led_classdev, > + brightness: u32, > + ) { [ ... ] > + unsafe extern "C" fn brightness_set_blocking_callback( > + led_cdev: *mut bindings::led_classdev, > + brightness: u32, > + ) -> i32 { [ ... ] > + unsafe extern "C" fn brightness_get_callback(led_cdev: *mut bindings= ::led_classdev) -> u32 { [Severity: High] Will these callbacks trigger a kCFI panic on CONFIG_CFI_CLANG=3Dy kernels? In include/linux/leds.h, struct led_classdev declares brightness_set, brightness_set_blocking, and brightness_get with enum led_brightness, which Clang encodes by enum tag name rather than as u32. Because led_brightness is not declared in rust/bindgen_parameters with --newtype-enum and #[cfi_encoding=3D"14led_brightness"] and these callbacks use raw u32, every indirect call from the C LED core (including led_update_brightness() during registration or sysfs reads) will fail the kCFI check and trap. [ ... ] > + unsafe extern "C" fn blink_set_callback( > + led_cdev: *mut bindings::led_classdev, > + delay_on: *mut usize, > + delay_off: *mut usize, > + ) -> i32 { > + from_result(|| { > + // SAFETY: The function's contract guarantees that `led_cdev= ` is a valid pointer to a > + // `led_classdev` embedded within a `led::Device`. > + let classdev =3D unsafe { Device::::from_raw(led_cdev) }; > + > + classdev.blink_set( > + // SAFETY: The function's contract guarantees that `dela= y_on` points to a `usize` > + // and is exclusive for the period of this function. > + unsafe { &mut *delay_on }, > + // SAFETY: The function's contract guarantees that `dela= y_off` points to a `usize` > + // and is exclusive for the period of this function. > + unsafe { &mut *delay_off }, > + )?; > + Ok(0) > + }) > + } [Severity: Medium] Are delay_on and delay_off actually exclusive when blink_set_callback() is called? In drivers/leds/trigger/ledtrig-timer.c, led_delay_on_store() calls led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off) without holding any per-LED lock, and led_blink_setup() invokes led_cdev->blink_set() directly. Because kernfs_fop_write_iter() only serializes per open file description, two concurrent writes to delay_on on separate file descriptors reach blink_set_callback() together and construct aliasing &mut usize references to led_cdev->blink_delay_off, causing Rust aliasing and data-race undefined behavior. [ ... ] > +#[pinned_drop] > +impl<'bound, T: 'bound> PinnedDrop for Device<'bound, T> { > + fn drop(self: Pin<&mut Self>) { > + let raw =3D self.classdev.get(); > + // SAFETY: The existence of `self` guarantees that `self.classde= v.get()` is a pointer to a > + // valid `led_classdev`. > + let dev: &device::Device =3D unsafe { device::Device::from_raw((= *raw).dev) }; > + > + let _fwnode =3D dev > + .fwnode() > + // SAFETY: the reference count of `fwnode` has previously be= en > + // incremented in `led::DeviceBuilder::build`. > + .map(|fwnode| unsafe { ARef::from_raw(NonNull::from(fwnode))= }); > + > + // SAFETY: The existence of `self` guarantees that `self.classde= v` has previously been > + // successfully registered with `led_classdev_register_ext`. > + unsafe { bindings::led_classdev_unregister(raw) }; > + } > +} [Severity: High] Does this double-put a software_node fwnode and cause a use-after-free on unregister? In drivers/leds/led-class.c, led_classdev_register_ext() calls device_create_with_groups() before device_set_node(led_cdev->dev, init_data->fwnode), so software_node_notify() runs while dev->fwnode is still NULL and does not call swnode_get(). DeviceBuilder::build() therefore leaves only the single forgotten ARef reference on swnode. When Device is dropped, led_classdev_unregister() calls device_unregister() -> device_del() -> software_node_notify_remove(), which sees the populated dev->fwnode and calls swnode_put(). Dropping _fwnode at the end of PinnedDrop::drop() then calls fwnode_handle_put() -> swnode_put() a second time, underflowing the refcount and freeing the still-registered software node via software_node_release(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203-rust_leds-= v29-0-dc8a8ccb96d7@posteo.de?part=3D1