Skip to content

Commit c7fbb82

Browse files
Fernando Fernandez Manceragregkh
authored andcommitted
sysfs: check visibility before changing group attribute ownership
Since commit 0c17270 ("net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)"), __dev_change_net_namespace() can hit WARN_ON() when trying to change owner of a file that isn't visible. See the trace below: WARNING: CPU: 6 PID: 2938 at net/core/dev.c:12410 __dev_change_net_namespace+0xb89/0xc30 CPU: 6 UID: 0 PID: 2938 Comm: incusd Not tainted 6.17.1-1-mainline #1 PREEMPT(full) 4b783b4a638669fb644857f484487d17cb45ed1f Hardware name: Framework Laptop 13 (AMD Ryzen 7040Series)/FRANMDCP07, BIOS 03.07 02/19/2025 RIP: 0010:__dev_change_net_namespace+0xb89/0xc30 [...] Call Trace: <TASK> ? if6_seq_show+0x30/0x50 do_setlink.isra.0+0xc7/0x1270 ? __nla_validate_parse+0x5c/0xcc0 ? security_capable+0x94/0x1a0 rtnl_newlink+0x858/0xc20 ? update_curr+0x8e/0x1c0 ? update_entity_lag+0x71/0x80 ? sched_balance_newidle+0x358/0x450 ? psi_task_switch+0x113/0x2a0 ? __pfx_rtnl_newlink+0x10/0x10 rtnetlink_rcv_msg+0x346/0x3e0 ? sched_clock+0x10/0x30 ? __pfx_rtnetlink_rcv_msg+0x10/0x10 netlink_rcv_skb+0x59/0x110 netlink_unicast+0x285/0x3c0 ? __alloc_skb+0xdb/0x1a0 netlink_sendmsg+0x20d/0x430 ____sys_sendmsg+0x39f/0x3d0 ? import_iovec+0x2f/0x40 ___sys_sendmsg+0x99/0xe0 __sys_sendmsg+0x8a/0xf0 do_syscall_64+0x81/0x970 ? __sys_bind+0xe3/0x110 ? syscall_exit_work+0x143/0x1b0 ? do_syscall_64+0x244/0x970 ? sock_alloc_file+0x63/0xc0 ? syscall_exit_work+0x143/0x1b0 ? do_syscall_64+0x244/0x970 ? alloc_fd+0x12e/0x190 ? put_unused_fd+0x2a/0x70 ? do_sys_openat2+0xa2/0xe0 ? syscall_exit_work+0x143/0x1b0 ? do_syscall_64+0x244/0x970 ? exc_page_fault+0x7e/0x1a0 entry_SYSCALL_64_after_hwframe+0x76/0x7e [...] </TASK> Fix this by checking is_visible() before trying to touch the attribute. Fixes: 303a427 ("sysfs: add sysfs_group{s}_change_owner()") Fixes: 0c17270 ("net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)") Reported-by: Cynthia <cynthia@kosmx.dev> Closes: https://lore.kernel.org/netdev/01070199e22de7f8-28f711ab-d3f1-46d9-b9a0-048ab05eb09b-000000@eu-central-1.amazonses.com/ Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de> Reviewed-by: Jakub Kicinski <kuba@kernel.org> Link: https://lore.kernel.org/r/20251016101456.4087-1-fmancera@suse.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a91c809 commit c7fbb82

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

fs/sysfs/group.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,17 +498,26 @@ int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
498498
}
499499
EXPORT_SYMBOL_GPL(compat_only_sysfs_link_entry_to_kobj);
500500

501-
static int sysfs_group_attrs_change_owner(struct kernfs_node *grp_kn,
501+
static int sysfs_group_attrs_change_owner(struct kobject *kobj,
502+
struct kernfs_node *grp_kn,
502503
const struct attribute_group *grp,
503504
struct iattr *newattrs)
504505
{
505506
struct kernfs_node *kn;
506-
int error;
507+
int error, i;
508+
umode_t mode;
507509

508510
if (grp->attrs) {
509511
struct attribute *const *attr;
510512

511-
for (attr = grp->attrs; *attr; attr++) {
513+
for (i = 0, attr = grp->attrs; *attr; i++, attr++) {
514+
if (grp->is_visible) {
515+
mode = grp->is_visible(kobj, *attr, i);
516+
if (mode & SYSFS_GROUP_INVISIBLE)
517+
break;
518+
if (!mode)
519+
continue;
520+
}
512521
kn = kernfs_find_and_get(grp_kn, (*attr)->name);
513522
if (!kn)
514523
return -ENOENT;
@@ -523,7 +532,14 @@ static int sysfs_group_attrs_change_owner(struct kernfs_node *grp_kn,
523532
if (grp->bin_attrs) {
524533
const struct bin_attribute *const *bin_attr;
525534

526-
for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) {
535+
for (i = 0, bin_attr = grp->bin_attrs; *bin_attr; i++, bin_attr++) {
536+
if (grp->is_bin_visible) {
537+
mode = grp->is_bin_visible(kobj, *bin_attr, i);
538+
if (mode & SYSFS_GROUP_INVISIBLE)
539+
break;
540+
if (!mode)
541+
continue;
542+
}
527543
kn = kernfs_find_and_get(grp_kn, (*bin_attr)->attr.name);
528544
if (!kn)
529545
return -ENOENT;
@@ -573,7 +589,7 @@ int sysfs_group_change_owner(struct kobject *kobj,
573589

574590
error = kernfs_setattr(grp_kn, &newattrs);
575591
if (!error)
576-
error = sysfs_group_attrs_change_owner(grp_kn, grp, &newattrs);
592+
error = sysfs_group_attrs_change_owner(kobj, grp_kn, grp, &newattrs);
577593

578594
kernfs_put(grp_kn);
579595

0 commit comments

Comments
 (0)