rust: configfs: fix data offset calculation for subsystem callbacks
Subsystems (2)
Andreas Hindborg
maintainerPATCH 1/1 rust: configfs: fix data offset calculation for subsystem callbacks
get_group_data() chooses between Group::<Parent>::container_of() and Subsystem::<Parent>::container_of() based on whether this represents the root group of a configfs subsystem. It detects this by checking (*this).cg_subsys.is_null(), but link_group() in fs/configfs/dir.c unconditionally sets cg_subsys for every config_group attached anywhere in a registered subsystem, including the subsystem's own su_group. The only config_group with a NULL cg_subsys is the configfs root, on which userspace cannot trigger callbacks. The check is therefore always false at runtime, and the Subsystem branch is dead. Subsystem-level callbacks reach the Group branch and may read data at the wrong offset.
Thus change the is_root check to correctly identify whether a group is a root group by comparing this to subsys.su_group.
--- rust/kernel/configfs.rs | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-)
rust/kernel/configfs.rs +26 -16
@@ -297,26 +297,36 @@ unsafe fn container_of(group: *const bindings::config_group) -> *const Self {/// /// `this` must be a valid pointer. /// -/// If `this` does not represent the root group of a configfs subsystem, -/// `this` must be a pointer to a `bindings::config_group` embedded in a -/// `Group<Parent>`. +/// If `this` is the `su_group` field of a `bindings::configfs_subsystem`, that +/// `configfs_subsystem` must be embedded in a `Subsystem<Parent>`. /// -/// Otherwise, `this` must be a pointer to a `bindings::config_group` that -/// is embedded in a `bindings::configfs_subsystem` that is embedded in a -/// `Subsystem<Parent>`. +/// Otherwise, `this` must be a pointer to a `bindings::config_group` embedded +/// in a `Group<Parent>`. unsafe fn get_group_data <'a,Parent >(this: *mut bindings::config_group ) -> &'aParent {// SAFETY: `this` is a valid pointer. -let is_root =unsafe { (*this).cg_subsys.is_null () }; - -if !is_root { -// SAFETY: By C API contact,`this` was returned from a call to -// `make_group`. The pointer is known to be embedded within a -// `Group<Parent>`. -unsafe { &(*Group ::<Parent >::container_of (this)).data } - }else { -// SAFETY: By C API contract, `this` is a pointer to the -// `bindings::config_group` field within a `Subsystem<Parent>`. +let subsys =unsafe { (*this).cg_subsys }; +// `link_group()` in `fs/configfs/dir.c` assigns `cg_subsys` for every +// `config_group` attached anywhere in a registered subsystem, including +// the subsystem's own `su_group` (which gets a pointer to itself). The +// only `config_group` with a NULL `cg_subsys` is the configfs root, and +// userspace cannot trigger callbacks on it. The group is therefore the +// subsystem's `su_group` iff it equals `&cg_subsys->su_group`. +// +// SAFETY: For every `config_group` the configfs core dispatches a +// callback on, `cg_subsys` was set by `link_group()` at registration time +// and points to a valid `configfs_subsystem` that outlives the callback. +let is_root = !subsys.is_null () + && core::ptr::eq (this.cast_const (),unsafe { &raw const (*subsys).su_group }); + +if is_root { +// SAFETY: By the above, `this` is the `su_group` field of a +// `configfs_subsystem` that, by function safety requirements, is +// embedded in a `Subsystem<Parent>`. unsafe { &(*Subsystem ::container_of (this)).data } + }else { +// SAFETY: By function safety requirements, `this` is a +// `config_group` embedded in a `Group<Parent>`. +unsafe { &(*Group ::<Parent >::container_of (this)).data } } }
---
base-commit: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32
change-id: 20260608-configfs-fix-offset-6b3117158901
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
Quoted 2 lines · Andreas Hindborg
Fixes 446cafc295bf ("rust: configfs: introduce rust support for configfs")
Same here: should this have Cc: stable given the hash? i.e. 6.18.y+
Cheers,
Miguel
Quoted 11 lines · Andreas Hindborg
get_group_data()chooses betweenGroup::<Parent>::container_of()andSubsystem::<Parent>::container_of()based on whetherthisrepresents the root group of a configfs subsystem. It detects this by checking(*this).cg_subsys.is_null(), butlink_group()infs/configfs/dir.cunconditionally setscg_subsysfor everyconfig_groupattached anywhere in a registered subsystem, including the subsystem's ownsu_group. The onlyconfig_groupwith a NULLcg_subsysis the configfs root, on which userspace cannot trigger callbacks. The check is therefore always false at runtime, and theSubsystembranch is dead. Subsystem-level callbacks reach theGroupbranch and may readdataat the wrong offset.
Quoted 1 line
[...]
Applied, thanks!
[1/1] rust: configfs: fix data offset calculation for subsystem callbacks
commit: e84c9b64df615e658e47561b835c1364dc48f0c0
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>