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=1796382399; b=aFTMTKceq2s7CIe4+QM7XbShZgQWFvXsNkkJDLBqEKVO5BS1nMbEoUhdcvx5qgNAINkd tQsZY0bJ32lX2J5lXJofVf+6jan3Al0JC20o0Zjn+mZE75Ufj3lU6SEHn5JbY0SLB2b0h 3xFEiSg6vsDIYFSMFm1v3SYAonx4X0eD3vICXOD+x4S44nWFY8qHEK376/0Rn3wZVHQlD dBvkUvWjG6pip6A++zM2bIvq7Pq8EFTRn53G0d1neoI2c2Kv9GP3pRHVs/67LWgOKN/up xVmvzjzGVARob8yWFlBxUMRfUkBnP14SeejjOJ35Ea1lz5VduHVcH6bT5P6w/cxxtCQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382399; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=19m/dW7fb6cfgQSqHbITxkXUtcSKm7uTkN3KcbKVLKM=; b=bNTC65B86ZCyB1GsY6RDIblM5BIi1BRGCSeEBp49525VZPBQm5EWH6epFWaYvCAgKEXm TeS7Xr90tj97MipINlHTUanfgdkF5ZZ8xXwka7cbS0Noe0VAD9F7B2lKw4T/4PiFhpmRv 5aI5ZgoG8TAalyiiLjidgvWSAIvkNsZJM57tt4FmqBP+un42+SHF3sjtL84wEgswTCOyn tmyuE2faJWQ0Uesj7zqC6Jby9Fmm9OnL2Bd043mUjSfLAKXGKokamRGPEvRPyqxIS2sZj dP5GERDVj4GYh/5dbFwJjATFMXiN06uG3KWNSVX2pKpkzYBS6LMVbW/hiZ23KB8Ac9w== 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="nYxpq/K/"; 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 774A7606A322; Fri, 4 Dec 2026 11:06:39 +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=nYxpq/K/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 430251F0092C; Fri, 4 Dec 2026 11:06:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796382399; bh=19m/dW7fb6cfgQSqHbITxkXUtcSKm7uTkN3KcbKVLKM=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=nYxpq/K/W1qSiVsDvuJMvEF4sXyfSSU69fGB1QwSY5qk4vUzmU7DgJHNLT23Jf1zh H5EPPv6jVr1+l3uDJ36pNCXvBX0R0BeriZGAFWM8UzpHxxeFABs8YVbiDXTNoLL9bV /9g+hTuOOo3eXBAus+4ZqHRpjfYOfGPX4nPyZ/TceGCaHbDvF2rfoAyJS8YLLJPV8T AMXgkps8jXY2r3TrHSL4GTj8tNp5L39+ZJlUBjVD/3cxgxn793m2aSotNOyTXDLoOL X8aR9NFcVqTmiRIXAcTYKKFUZXY/399Nren5IOn1DcqEol8BobdvfqEj/+4JwTzqYi 8Jyz3DLChM0iw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v6 6/9] rust: kernel: Add greybus abstractions Reply-To: sashiko-reviews@lists.linux.dev To: "Ayush Singh" , "Jason Kridner" , robertcnelson@gmail.com, "Johan Hovold" , "Alex Elder" , "Greg Kroah-Hartman" , "Miguel Ojeda" , "Boqun Feng" , "Gary Guo" , =?utf-8?b?Qmo=?= =?utf-8?b?w7ZybiBSb3kgQmFyb24=?= , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Daniel Almeida" , "Tamir Duberstein" , "Alexandre Courbot" , =?utf-8?b?T251ciDDlnprYW4=?= , "Eric Biggers" , "Ard Biesheuvel" , "Ayush Singh" , "Rob Herring" , "Krzysztof Kozlowski" , "Conor Dooley" , "Lorenzo Stoakes" , "Vlastimil Babka" , "Liam R. Howlett" , "Uladzislau Rezki" , "Markus Probst" Cc: linux-crypto@vger.kernel.org, devicetree@vger.kernel.org In-Reply-To: <20261204-gb-uart-transport-v6-6-668b333ae71d@beagleboard.org> References: <20261204-gb-uart-transport-v6-0-668b333ae71d@beagleboard.org> <20261204-gb-uart-transport-v6-6-668b333ae71d@beagleboard.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 11:06:35 +0000 X-Mailing-List: devicetree@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 9 potential issues to consider: - [High] greybus: double-drop in gb_operation_request_send() on send failure - [High] rust: greybus: data_rcvd() exposes gb_connection_recv() size underflow - [Medium] rust: greybus: UAF of HdDriverVTable in gb_hd_release() after module unload - [Medium] rust: greybus: Message::sent() holds references across potential free - [Low] rust: alloc: missing inline attribute on Flags::new() - [Low] rust: greybus: missing calling-context docs on HdDriver::message_send() - [Low] rust: greybus: hd::Device missing DeviceContext bound and SAFETY comment - [Low] rust: greybus: doc mismatches in Message::payload() and data_rcvd() - [Low] rust: greybus: HdDriver::message_cancel() lacks &Self and default no-op impl -- commit 0aa91303eac5e1c6fd892a4ecdeb192bf9f8230f Author: Ayush Singh rust: kernel: Add greybus abstractions This commit adds Rust abstractions for the Greybus core, covering host device registration and driver callbacks, message/operation/connection wrappers, and SVC protocol wire-format definitions. > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > index 6cd29ca35244d..e38bfe6e85728 100644 > --- a/rust/kernel/alloc.rs > +++ b/rust/kernel/alloc.rs > @@ -41,6 +41,12 @@ > pub struct Flags(u32); > =20 > impl Flags { > + /// Creates [`Flags`] from a raw `gfp_t`. > + #[cfg_attr(not(CONFIG_RUST_GREYBUS_ABSTRACTIONS), expect(dead_code))] > + pub(crate) fn new(v: bindings::gfp_t) -> Self { > + Self(v) > + } > + > /// Get the raw representation of this flag. > pub(crate) fn as_raw(self) -> u32 { > self.0 [Severity: Low] Should Flags::new() be marked #[inline] to match the convention for small wrappers in the kernel crate? > diff --git a/rust/kernel/greybus/hd.rs b/rust/kernel/greybus/hd.rs > new file mode 100644 > index 0000000000000..24913c054f6a5 > --- /dev/null > +++ b/rust/kernel/greybus/hd.rs > @@ -0,0 +1,391 @@ [ ... ] > + /// Transmits `msg` to `dest_cport_id`. > + /// > + /// The message is only borrowed for the duration of this call: copy= what is needed out of it, > + /// do not stash the reference. Returning `Ok(())` reports the messa= ge as sent to the core. > + /// > + /// `hd` is the host device the message is sent on. It is only guara= nteed to be alive for the > + /// duration of this call. > + fn message_send( > + data: &Self, > + hd: &Device, > + dest_cport_id: u16, > + msg: &Message, > + gfp_mask: Flags, > + ) -> Result; [Severity: Low] Should HdDriver::message_send() document its calling context and sleeping constraints? gb_operation_request_send() is exported and forwards its caller-supplied gfp_t directly to message_send(), so callers can invoke it from atomic context with GFP_ATOMIC where sleeping or taking a mutex is forbidden. Unlike message_cancel(), which documents process context, message_send() leaves this unspecified. > + /// Aborts the transmission of a message previously handed to [`HdDr= iver::message_send`]. > + /// > + /// Always called in process context. > + fn message_cancel(msg: &Message); [Severity: Low] Should HdDriver::message_cancel() take data: &Self and hd: &Device, and provide a default empty implementation? Unlike C drivers, which can follow message->operation->connection->hd to reach driver private data, the Rust wrappers are opaque so drivers cannot access per-device state here. gb_message_cancel() already dereferences message->operation->connection->hd before invoking the callback, so HdDriverVTable::message_cancel() can borrow hd and data without changing the C API. Also, because HdDriverVTable::message_send() synchronously calls msg.sent(hd, 0) on Ok(()), synchronous transports have nothing to cancel and currently have to define empty stubs. [ ... ] > + match T::message_send(&data, hd_normal, dest_cport_id, msg, Flag= s::new(gfp_mask)) { > + Ok(()) =3D> { > + // SAFETY: `msg` is still valid since `sent` is not call= ed anywhere else. > + unsafe { msg.sent(hd, 0) }; > + 0 > + } > + Err(e) =3D> e.to_errno(), > + } [Severity: High] Can returning Err(e) from HdDriver::message_send() trigger a use-after-free and refcount underflow in gb_operation_request_send()? gb_operation_request_send() acquires two active counts and two references, arms operation->timer, and then calls gb_message_send(). If the timer fires or another thread calls gb_operation_cancel() while T::message_send() is running, gb_operation_work() is queued and takes ownership of one active count and one reference. When T::message_send() then returns an error, the err_put_active path calls timer_delete_sync() (which does not flush operation->work) and unconditionally drops both active counts and both references: CPU0 CPU1 gb_operation_request_send() __gb_operation_get_active(..., 2) add_timer(&operation->timer) gb_message_send() T::message_send() gb_operation_timeout() gb_operation_result_set(..., -ETIMEDOUT) queue_work(..., &operation->work) return Err(e) err_put_active: timer_delete_sync() gb_operation_put_active() x2 gb_operation_put() x2 gb_operation_work() operation->callback(...) <-- use-after-= free gb_operation_put_active() <-- underflow gb_operation_put() That drops three active counts and three references for the two acquired, freeing operation while gb_operation_work() is still queued or running and underflowing operation->active to -1. [ ... ] > +#[repr(transparent)] > +pub struct Device(Opaque, PhantomData); > + > +// SAFETY: `gb_host_device` is reference counted through its embedded `s= truct device`, which may be > +// used from any thread. > +unsafe impl Send for Device {} > + > +// SAFETY: `gb_host_device` has its own internal locking, so it is safe = to share references to it > +// across threads. > +unsafe impl Sync for Device {} > + > +kernel::impl_device_context_deref!(unsafe { Device }); [Severity: Low] Should Device (and impl Device) carry a Ctx: device::DeviceContext bound like other bus device wrappers, and should impl_device_context_deref!(unsafe { Device }) have a // SAFETY: comment explaining that Device is #[repr(transparent)] with layout independent of Ctx? Also, in HdDriverVTable::message_send(), Device::::from_raw(hd.as_raw()) is an unnecessary unsafe cast because hd already deref-coerces to &Device via impl_device_context_deref!. [ ... ] > + /// Hands a message received on `hd_cport_id` to the Greybus core. > + #[inline] > + pub fn data_rcvd(&self, cport_id: u16, msg: &[u8]) { > + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a= pointer to a valid > + // `struct gb_host_device`. `msg` is valid for reads of ``msg.le= n()` bytes for the duration > + // of the call, and the core only reads through the pointer =E2= =80=94 it copies the payload into > + // the operation before returning =E2=80=94 so handing it a `*mu= t` derived from a shared reference > + // is sound. > + // > + // TODO: The C signature of this function should be changed to t= ake const pointer for msg. > + unsafe { > + bindings::greybus_data_rcvd(self.as_raw(), cport_id, msg.as_= ptr().cast_mut(), msg.len()) > + } > + } > + > + /// Hands `msg`, received from CPort `cport` of interface `intf_id`,= to the Greybus core. > + /// > + /// Fails with `EINVAL` if no connection is bound to that pair. > + #[inline] > + pub fn data_rcvd_by_intf(&self, intf_id: u8, cport: u16, msg: &[u8])= -> Result { > + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a= pointer to a valid > + // `struct gb_host_device`. `msg` is valid for reads of `msg.len= ()` bytes for the duration > + // of the call, and the core only reads through the pointer =E2= =80=94 it copies the payload into > + // the operation before returning =E2=80=94 so handing it a `*mu= t` derived from a shared reference > + // is sound. > + // > + // TODO: The C signature of this function should be changed to t= ake const pointer for msg. > + to_result(unsafe { > + bindings::greybus_data_rcvd_by_intf( > + self.as_raw(), > + intf_id, > + cport, > + msg.as_ptr().cast_mut(), > + msg.len(), > + ) > + }) > + } [Severity: High] Can passing a message with header.size < sizeof(gb_operation_msg_hdr) to Device::data_rcvd() or Device::data_rcvd_by_intf() cause a slab out-of-bounds write or ZERO_SIZE_PTR dereference? Both safe methods forward arbitrary &[u8] slices to gb_connection_recv(), which checks size >=3D sizeof(header) and size >=3D msg_size, but never che= cks msg_size >=3D sizeof(header). For a slice of at least 8 bytes with header.size in 0..7, gb_operation_create_incoming() underflows request_size =3D size - sizeof(struct gb_operation_msg_hdr), and gb_operation_message_alloc() adds sizeof(*header) back so message_size wraps to 0..7 and bypasses hd->buffer_size_max. kzalloc() then returns ZERO_SIZE_PTR (for 0) or a 1..7-byte buffer, and gb_operation_message_init() writes the 8-byte header out of bounds or faults on ZERO_SIZE_PTR. On the response path with GB_OPERATION_FLAG_SHORT_RESPON= SE, gb_connection_recv_response() similarly underflows message->payload_size. Rejecting msg_size < sizeof(header) in gb_connection_recv() (or validating msg in the Rust wrappers) would close both the C underflow and the Rust soundness hole. [ ... ] > + // SAFETY: `parent` is a valid device, and the driver table is `= 'static`. The core only > + // ever reads through the driver pointer, so casting away `const= ` is fine. > + let hd =3D from_err_ptr(unsafe { > + bindings::gb_hd_create( > + core::ptr::from_ref(HdDriverVTable::::build()).cast_m= ut(), > + parent.as_raw(), > + buffer_size_max, > + num_cports, > + ) > + })?; [ ... ] > + fn drop<'a>(&'a mut self) { > + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a= pointer to a valid > + // `struct gb_host_device`. > + unsafe { bindings::gb_hd_del(self.as_raw()) } > + > + { > + // SAFETY: `hd` is a valid pointer to `gb_host_device`. > + let hd_dev =3D unsafe { Device::>::= from_raw(self.as_raw()) }; > + // SAFETY: The driver data was set to a `T` in `Registration= ::new()` and has not been > + // taken since, and this is the only place that takes it. > + drop(unsafe { hd_dev.as_ref().drvdata_obtain::() }); > + } > + > + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a= pointer to a valid > + // `struct gb_host_device`. > + unsafe { bindings::gb_hd_put(self.as_raw()) }; > + } [Severity: Medium] Can gb_hd_release() dereference HdDriverVTable::::DRIVER after a loadable host driver module is unloaded? Registration::new() passes &HdDriverVTable::::DRIVER, which lives in the driver module's .rodata, to gb_hd_create() without pinning the module. Because hd::Device implements AlwaysRefCounted and gb_hd_add() exposes hd->dev in sysfs, an external reference can keep hd->dev alive past Registration::drop() and module unload. When the final reference is later dropped, gb_hd_release() -> gb_svc_put() -> gb_svc_release() -> gb_connection_destroy(svc->connection) -> gb_hd_cport_release() reads hd->driver->cport_release from unmapped module memory. Pinning the module for the lifetime of gb_host_device or repointing hd->driver to a kernel-crate-resident table before gb_hd_put() would avoid the use-after-free. > diff --git a/rust/kernel/greybus/mod.rs b/rust/kernel/greybus/mod.rs > new file mode 100644 > index 0000000000000..908356af08e67 > --- /dev/null > +++ b/rust/kernel/greybus/mod.rs > @@ -0,0 +1,200 @@ [ ... ] > + /// # Safety > + /// > + /// `Message` is not valid after this call. > + #[inline] > + pub(crate) unsafe fn sent(&self, hd: &hd::Device, status: = i32) { > + // SAFETY: By the type invariant the message is valid and still = owned by the transport, > + // and its connection and host device are alive for as long as i= t is. > + unsafe { > + bindings::greybus_message_sent(hd.as_raw(), self.0.get(), st= atus); > + } > + } [Severity: Medium] Does Message::sent() hold &self and hd: &hd::Device across a call that can free both objects? greybus_message_sent() calls gb_operation_put_active(operation) followed by gb_operation_put(operation). Dropping operation->active to zero can wake a concurrent Registration::drop() -> gb_hd_del() -> gb_hd_put() -> kfree(hd), and gb_operation_put() can synchronously destroy operation and free message before greybus_message_sent() returns. Ending the borrows in HdDriverVTable::message_send() and passing raw *mut pointers to bindings::greybus_message_sent() avoids holding references across deallocation. [ ... ] > + /// Interprets the message payload as a `T`. > + /// > + /// Returns `None` if the payload is too short or misaligned for `T`. > + #[inline] > + pub fn payload(&self) -> Option<&T> { > + T::from_bytes(self.payload_bytes()) > + } [Severity: Low] This isn't a bug, but the doc comment on Message::payload() says it returns None if the payload is too short or misaligned for T, whereas FromBytes::from_bytes() requires an exact length match and also returns None when the payload is longer than size_of::(). Separately, in rust/kernel/greybus/hd.rs, the doc comment on Device::data_rcvd() refers to hd_cport_id instead of the parameter name cport_id. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204-gb-uart-tr= ansport-v6-0-668b333ae71d@beagleboard.org?part=3D6