From: Christian Brauner <hidden> Date: 2020-02-24 17:21:28
Hey everyone,
This is v4 with more documentation and other fixes that Greg requested.
This is v3 with explicit uid and gid parameters added to functions that
change sysfs object ownership as Greg requested.
(I've tagged this with net-next since it's triggered by a bug for
network device files but it also touches driver core aspects so it's
not clear-cut. I can of course split this series into separate
patchsets.)
We have been struggling with a bug surrounding the ownership of network
device sysfs files when moving network devices between network
namespaces owned by different user namespaces reported by multiple
users.
Currently, when moving network devices between network namespaces the
ownership of the corresponding sysfs entries is not changed. This leads
to problems when tools try to operate on the corresponding sysfs files.
I also causes a bug when creating a network device in a network
namespaces owned by a user namespace and moving that network device back
to the host network namespaces. Because when a network device is created
in a network namespaces it will be owned by the root user of the user
namespace and all its associated sysfs files will also be owned by the
root user of the corresponding user namespace.
If such a network device has to be moved back to the host network
namespace the permissions will still be set to the root user of the
owning user namespaces of the originating network namespace. This means
unprivileged users can e.g. re-trigger uevents for such incorrectly
owned devices on the host or in other network namespaces. They can also
modify the settings of the device itself through sysfs when they
wouldn't be able to do the same through netlink. Both of these things
are unwanted.
For example, quite a few workloads will create network devices in the
host network namespace. Other tools will then proceed to move such
devices between network namespaces owner by other user namespaces. While
the ownership of the device itself is updated in
net/core/net-sysfs.c:dev_change_net_namespace() the corresponding sysfs
entry for the device is not. Below you'll find that moving a network
device (here a veth device) from a network namespace into another
network namespaces owned by a different user namespace with a different
id mapping. As you can see the permissions are wrong even though it is
owned by the userns root user after it has been moved and can be
interacted with through netlink:
drwxr-xr-x 5 nobody nobody 0 Jan 25 18:08 .
drwxr-xr-x 9 nobody nobody 0 Jan 25 18:08 ..
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 addr_assign_type
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 addr_len
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 address
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 broadcast
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier_changes
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier_down_count
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier_up_count
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 dev_id
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 dev_port
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 dormant
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 duplex
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 flags
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 gro_flush_timeout
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 ifalias
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 ifindex
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 iflink
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 link_mode
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 mtu
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 name_assign_type
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 netdev_group
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 operstate
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 phys_port_id
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 phys_port_name
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 phys_switch_id
drwxr-xr-x 2 nobody nobody 0 Jan 25 18:09 power
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 proto_down
drwxr-xr-x 4 nobody nobody 0 Jan 25 18:09 queues
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 speed
drwxr-xr-x 2 nobody nobody 0 Jan 25 18:09 statistics
lrwxrwxrwx 1 nobody nobody 0 Jan 25 18:08 subsystem -> ../../../../class/net
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 tx_queue_len
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 type
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:08 uevent
Constrast this with creating a device of the same type in the network
namespace directly. In this case the device's sysfs permissions will be
correctly updated.
(Please also note, that in a lot of workloads this strategy of creating
the network device directly in the network device to workaround this
issue can not be used. Either because the network device is dedicated
after it has been created or because it used by a process that is
heavily sandboxed and couldn't create network devices itself.):
drwxr-xr-x 5 root root 0 Jan 25 18:12 .
drwxr-xr-x 9 nobody nobody 0 Jan 25 18:08 ..
-r--r--r-- 1 root root 4096 Jan 25 18:12 addr_assign_type
-r--r--r-- 1 root root 4096 Jan 25 18:12 addr_len
-r--r--r-- 1 root root 4096 Jan 25 18:12 address
-r--r--r-- 1 root root 4096 Jan 25 18:12 broadcast
-rw-r--r-- 1 root root 4096 Jan 25 18:12 carrier
-r--r--r-- 1 root root 4096 Jan 25 18:12 carrier_changes
-r--r--r-- 1 root root 4096 Jan 25 18:12 carrier_down_count
-r--r--r-- 1 root root 4096 Jan 25 18:12 carrier_up_count
-r--r--r-- 1 root root 4096 Jan 25 18:12 dev_id
-r--r--r-- 1 root root 4096 Jan 25 18:12 dev_port
-r--r--r-- 1 root root 4096 Jan 25 18:12 dormant
-r--r--r-- 1 root root 4096 Jan 25 18:12 duplex
-rw-r--r-- 1 root root 4096 Jan 25 18:12 flags
-rw-r--r-- 1 root root 4096 Jan 25 18:12 gro_flush_timeout
-rw-r--r-- 1 root root 4096 Jan 25 18:12 ifalias
-r--r--r-- 1 root root 4096 Jan 25 18:12 ifindex
-r--r--r-- 1 root root 4096 Jan 25 18:12 iflink
-r--r--r-- 1 root root 4096 Jan 25 18:12 link_mode
-rw-r--r-- 1 root root 4096 Jan 25 18:12 mtu
-r--r--r-- 1 root root 4096 Jan 25 18:12 name_assign_type
-rw-r--r-- 1 root root 4096 Jan 25 18:12 netdev_group
-r--r--r-- 1 root root 4096 Jan 25 18:12 operstate
-r--r--r-- 1 root root 4096 Jan 25 18:12 phys_port_id
-r--r--r-- 1 root root 4096 Jan 25 18:12 phys_port_name
-r--r--r-- 1 root root 4096 Jan 25 18:12 phys_switch_id
drwxr-xr-x 2 root root 0 Jan 25 18:12 power
-rw-r--r-- 1 root root 4096 Jan 25 18:12 proto_down
drwxr-xr-x 4 root root 0 Jan 25 18:12 queues
-r--r--r-- 1 root root 4096 Jan 25 18:12 speed
drwxr-xr-x 2 root root 0 Jan 25 18:12 statistics
lrwxrwxrwx 1 nobody nobody 0 Jan 25 18:12 subsystem -> ../../../../class/net
-rw-r--r-- 1 root root 4096 Jan 25 18:12 tx_queue_len
-r--r--r-- 1 root root 4096 Jan 25 18:12 type
-rw-r--r-- 1 root root 4096 Jan 25 18:12 uevent
Now, when creating a network device in a network namespace owned by a
user namespace and moving it to the host the permissions will be set to
the id that the user namespace root user has been mapped to on the host
leading to all sorts of permission issues mentioned above:
458752
drwxr-xr-x 5 458752 458752 0 Jan 25 18:12 .
drwxr-xr-x 9 root root 0 Jan 25 18:08 ..
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 addr_assign_type
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 addr_len
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 address
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 broadcast
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier_changes
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier_down_count
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier_up_count
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 dev_id
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 dev_port
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 dormant
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 duplex
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 flags
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 gro_flush_timeout
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 ifalias
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 ifindex
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 iflink
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 link_mode
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 mtu
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 name_assign_type
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 netdev_group
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 operstate
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 phys_port_id
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 phys_port_name
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 phys_switch_id
drwxr-xr-x 2 458752 458752 0 Jan 25 18:12 power
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 proto_down
drwxr-xr-x 4 458752 458752 0 Jan 25 18:12 queues
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 speed
drwxr-xr-x 2 458752 458752 0 Jan 25 18:12 statistics
lrwxrwxrwx 1 root root 0 Jan 25 18:12 subsystem -> ../../../../class/net
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 tx_queue_len
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 type
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 uevent
Fix this by changing the basic sysfs files associated with network
devices when moving them between network namespaces. To this end we add
some infrastructure to sysfs.
The patchset takes care to only do this when the owning user namespaces
changes and the kids differ. So there's only a performance overhead,
when the owning user namespace of the network namespace is different
__and__ the kid mappings for the root user are different for the two
user namespaces:
Assume we have a netdev eth0 which we create in netns1 owned by userns1.
userns1 has an id mapping of 0 100000 100000. Now we move eth0 into
netns2 which is owned by userns2 which also defines an id mapping of 0
100000 100000. In this case sysfs doesn't need updating. The patch will
handle this case and not do any needless work. Now assume eth0 is moved
into netns3 which is owned by userns3 which defines an id mapping of 0
123456 65536. In this case the root user in each namespace corresponds
to different kid and sysfs needs updating.
Thanks!
Christian
Christian Brauner (9):
sysfs: add sysfs_file_change_owner_by_name()
sysfs: add sysfs_link_change_owner()
sysfs: add sysfs_group{s}_change_owner()
sysfs: add sysfs_change_owner()
device: add device_change_owner()
drivers/base/power: add dpm_sysfs_change_owner()
net-sysfs: add netdev_change_owner()
net-sysfs: add queue_change_owner()
net: fix sysfs permssions when device changes network namespace
drivers/base/core.c | 120 ++++++++++++++++++++++++++++++
drivers/base/power/power.h | 3 +
drivers/base/power/sysfs.c | 61 ++++++++++++++-
fs/sysfs/file.c | 148 +++++++++++++++++++++++++++++++++++++
fs/sysfs/group.c | 117 +++++++++++++++++++++++++++++
include/linux/device.h | 1 +
include/linux/sysfs.h | 46 ++++++++++++
net/core/dev.c | 9 ++-
net/core/net-sysfs.c | 133 +++++++++++++++++++++++++++++++++
net/core/net-sysfs.h | 2 +
10 files changed, 638 insertions(+), 2 deletions(-)
base-commit: bb6d3fb354c5ee8d6bde2d576eb7220ea09862b9
--
2.25.1
From: Christian Brauner <hidden> Date: 2020-02-24 17:21:32
Add a function to change the owner of the queue entries for a network device
when it is moved between network namespaces.
Currently, when moving network devices between network namespaces the
ownership of the corresponding queue sysfs entries are not changed. This leads
to problems when tools try to operate on the corresponding sysfs files. Fix
this.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- kbuild test robot [off-list ref] via sparse:
- Make net_rx_queue_change_owner() static since it's not exported.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
unchanged
---
net/core/net-sysfs.c | 106 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-24 17:21:35
Add a helper to change the owner of a device's sysfs entries. This
needs to happen when the ownership of a device is changed, e.g. when
moving network devices between network namespaces.
This function will be used to correctly account for ownership changes,
e.g. when moving network devices between network namespaces.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
unchanged
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
- Greg Kroah-Hartman [off-list ref]:
- Add more documentation.
---
drivers/base/core.c | 116 +++++++++++++++++++++++++++++++++++++++++
include/linux/device.h | 1 +
2 files changed, 117 insertions(+)
@@ -3458,6 +3458,122 @@ int device_move(struct device *dev, struct device *new_parent,}EXPORT_SYMBOL_GPL(device_move);+staticintdevice_attrs_change_owner(structdevice*dev,kuid_tkuid,+kgid_tkgid)+{+structkobject*kobj=&dev->kobj;+structclass*class=dev->class;+conststructdevice_type*type=dev->type;+interror;++if(class){+/*+*Changethedevicegroupsofthedeviceclassfor@devto+*@kuid/@kgid.+*/+error=sysfs_groups_change_owner(kobj,class->dev_groups,kuid,+kgid);+if(error)+returnerror;+}++if(type){+/*+*Changethedevicegroupsofthedevicetypefor@devto+*@kuid/@kgid.+*/+error=sysfs_groups_change_owner(kobj,type->groups,kuid,+kgid);+if(error)+returnerror;+}++/* Change the device groups of @dev to @kuid/@kgid. */+error=sysfs_groups_change_owner(kobj,dev->groups,kuid,kgid);+if(error)+returnerror;++if(device_supports_offline(dev)&&!dev->offline_disabled){+/* Change online device attributes of @dev to @kuid/@kgid. */+error=sysfs_file_change_owner(kobj,dev_attr_online.attr.name,+kuid,kgid);+if(error)+returnerror;+}++return0;+}++/**+*device_change_owner-changetheownerofanexistingdevice.+*@dev:device.+*@kuid:newowner'skuid+*@kgid:newowner'skgid+*+*Thischangestheownerof@devanditscorrespondingsysfsentriesto+*@kuid/@kgid.Thisfunctioncloselymirrorshow@devwasaddedviadriver+*core.+*+*Returns0onsuccessorerrorcodeonfailure.+*/+intdevice_change_owner(structdevice*dev,kuid_tkuid,kgid_tkgid)+{+interror;+structkobject*kobj=&dev->kobj;++dev=get_device(dev);+if(!dev)+return-EINVAL;++/*+*Changethekobjectandthedefaultattributesandgroupsofthe+*ktypeassociatedwithitto@kuid/@kgid.+*/+error=sysfs_change_owner(kobj,kuid,kgid);+if(error)+gotoout;++/*+*Changetheueventfilefor@devtothenewowner.Theueventfile+*wascreatedinaseparatestepwhen@devgotaddedandwemirror+*thatstephere.+*/+error=sysfs_file_change_owner(kobj,dev_attr_uevent.attr.name,kuid,+kgid);+if(error)+gotoout;++/*+*Changethedevicegroups,thedevicegroupsassociatedwiththe+*deviceclass,andthegroupsassociatedwiththedevicetypeof@dev+*to@kuid/@kgid.+*/+error=device_attrs_change_owner(dev,kuid,kgid);+if(error)+gotoout;++#ifdef CONFIG_BLOCK+if(sysfs_deprecated&&dev->class==&block_class)+gotoout;+#endif++/*+*Changetheownerofthesymlinklocatedintheclassdirectoryof+*thedeviceclassassociatedwith@devwhichpointstotheactual+*directoryentryfor@devto@kuid/@kgid.Thisensuresthatthe+*symlinkshowsthesamepermissionsasitstarget.+*/+error=sysfs_link_change_owner(&dev->class->p->subsys.kobj,&dev->kobj,+dev_name(dev),kuid,kgid);+if(error)+gotoout;++out:+put_device(dev);+returnerror;+}+EXPORT_SYMBOL_GPL(device_change_owner);+/***device_shutdown-call->shutdown()oneachdevicetoshutdown.*/
From: Christian Brauner <hidden> Date: 2020-02-24 17:21:39
Add a helper to change the owner of a device's power entries. This
needs to happen when the ownership of a device is changed, e.g. when
moving network devices between network namespaces.
This function will be used to correctly account for ownership changes,
e.g. when moving network devices between network namespaces.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- "Rafael J. Wysocki" [off-list ref]:
- Fold if (dev->power.wakeup && dev->power.wakeup->dev) check into
if (device_can_wakeup(dev)) check since the former can never be true if
the latter is false.
- Christian Brauner [off-list ref]:
- Place (dev->power.wakeup && dev->power.wakeup->dev) check under
CONFIG_PM_SLEEP ifdefine since it will wakeup_source will only be available
when this config option is set.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
- "Rafael J. Wysocki" [off-list ref]:
- Remove in-function #ifdef in favor of separate helper that is a nop
whenver !CONFIG_PM_SLEEP.
---
drivers/base/core.c | 4 +++
drivers/base/power/power.h | 3 ++
drivers/base/power/sysfs.c | 61 +++++++++++++++++++++++++++++++++++++-
3 files changed, 67 insertions(+), 1 deletion(-)
From: Christian Brauner <hidden> Date: 2020-02-24 17:21:43
Now that we moved all the helpers in place and make use netdev_change_owner()
to fixup the permissions when moving network devices between network
namespaces.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
unchanged
/* v3 */
unchanged
/* v4 */
unchanged
---
net/core/dev.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
@@ -10031,7 +10032,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char/* Get out if there is nothing todo */err=0;-if(net_eq(dev_net(dev),net))+if(net_eq(net_old,net))gotoout;/* Pick the destination device name, and ensure
@@ -10107,6 +10108,12 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const charerr=device_rename(&dev->dev,dev->name);WARN_ON(err);+/* Adapt owner in case owning user namespace of target network+*namespaceisdifferentfromtheoriginalone.+*/+err=netdev_change_owner(dev,net_old,net);+WARN_ON(err);+/* Add the device back in the hashes */list_netdevice(dev);
From: Christian Brauner <hidden> Date: 2020-02-24 17:21:47
Add a function to change the owner of a network device when it is moved
between network namespaces.
Currently, when moving network devices between network namespaces the
ownership of the corresponding sysfs entries is not changed. This leads
to problems when tools try to operate on the corresponding sysfs files.
This leads to a bug whereby a network device that is created in a
network namespaces owned by a user namespace will have its corresponding
sysfs entry owned by the root user of the corresponding user namespace.
If such a network device has to be moved back to the host network
namespace the permissions will still be set to the user namespaces. This
means unprivileged users can e.g. trigger uevents for such incorrectly
owned devices. They can also modify the settings of the device itself.
Both of these things are unwanted.
For example, workloads will create network devices in the host network
namespace. Other tools will then proceed to move such devices between
network namespaces owner by other user namespaces. While the ownership
of the device itself is updated in
net/core/net-sysfs.c:dev_change_net_namespace() the corresponding sysfs
entry for the device is not:
drwxr-xr-x 5 nobody nobody 0 Jan 25 18:08 .
drwxr-xr-x 9 nobody nobody 0 Jan 25 18:08 ..
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 addr_assign_type
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 addr_len
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 address
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 broadcast
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier_changes
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier_down_count
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 carrier_up_count
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 dev_id
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 dev_port
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 dormant
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 duplex
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 flags
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 gro_flush_timeout
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 ifalias
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 ifindex
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 iflink
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 link_mode
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 mtu
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 name_assign_type
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 netdev_group
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 operstate
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 phys_port_id
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 phys_port_name
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 phys_switch_id
drwxr-xr-x 2 nobody nobody 0 Jan 25 18:09 power
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 proto_down
drwxr-xr-x 4 nobody nobody 0 Jan 25 18:09 queues
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 speed
drwxr-xr-x 2 nobody nobody 0 Jan 25 18:09 statistics
lrwxrwxrwx 1 nobody nobody 0 Jan 25 18:08 subsystem -> ../../../../class/net
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:09 tx_queue_len
-r--r--r-- 1 nobody nobody 4096 Jan 25 18:09 type
-rw-r--r-- 1 nobody nobody 4096 Jan 25 18:08 uevent
However, if a device is created directly in the network namespace then
the device's sysfs permissions will be correctly updated:
drwxr-xr-x 5 root root 0 Jan 25 18:12 .
drwxr-xr-x 9 nobody nobody 0 Jan 25 18:08 ..
-r--r--r-- 1 root root 4096 Jan 25 18:12 addr_assign_type
-r--r--r-- 1 root root 4096 Jan 25 18:12 addr_len
-r--r--r-- 1 root root 4096 Jan 25 18:12 address
-r--r--r-- 1 root root 4096 Jan 25 18:12 broadcast
-rw-r--r-- 1 root root 4096 Jan 25 18:12 carrier
-r--r--r-- 1 root root 4096 Jan 25 18:12 carrier_changes
-r--r--r-- 1 root root 4096 Jan 25 18:12 carrier_down_count
-r--r--r-- 1 root root 4096 Jan 25 18:12 carrier_up_count
-r--r--r-- 1 root root 4096 Jan 25 18:12 dev_id
-r--r--r-- 1 root root 4096 Jan 25 18:12 dev_port
-r--r--r-- 1 root root 4096 Jan 25 18:12 dormant
-r--r--r-- 1 root root 4096 Jan 25 18:12 duplex
-rw-r--r-- 1 root root 4096 Jan 25 18:12 flags
-rw-r--r-- 1 root root 4096 Jan 25 18:12 gro_flush_timeout
-rw-r--r-- 1 root root 4096 Jan 25 18:12 ifalias
-r--r--r-- 1 root root 4096 Jan 25 18:12 ifindex
-r--r--r-- 1 root root 4096 Jan 25 18:12 iflink
-r--r--r-- 1 root root 4096 Jan 25 18:12 link_mode
-rw-r--r-- 1 root root 4096 Jan 25 18:12 mtu
-r--r--r-- 1 root root 4096 Jan 25 18:12 name_assign_type
-rw-r--r-- 1 root root 4096 Jan 25 18:12 netdev_group
-r--r--r-- 1 root root 4096 Jan 25 18:12 operstate
-r--r--r-- 1 root root 4096 Jan 25 18:12 phys_port_id
-r--r--r-- 1 root root 4096 Jan 25 18:12 phys_port_name
-r--r--r-- 1 root root 4096 Jan 25 18:12 phys_switch_id
drwxr-xr-x 2 root root 0 Jan 25 18:12 power
-rw-r--r-- 1 root root 4096 Jan 25 18:12 proto_down
drwxr-xr-x 4 root root 0 Jan 25 18:12 queues
-r--r--r-- 1 root root 4096 Jan 25 18:12 speed
drwxr-xr-x 2 root root 0 Jan 25 18:12 statistics
lrwxrwxrwx 1 nobody nobody 0 Jan 25 18:12 subsystem -> ../../../../class/net
-rw-r--r-- 1 root root 4096 Jan 25 18:12 tx_queue_len
-r--r--r-- 1 root root 4096 Jan 25 18:12 type
-rw-r--r-- 1 root root 4096 Jan 25 18:12 uevent
Now, when creating a network device in a network namespace owned by a
user namespace and moving it to the host the permissions will be set to
the id that the user namespace root user has been mapped to on the host
leading to all sorts of permission issues:
458752
drwxr-xr-x 5 458752 458752 0 Jan 25 18:12 .
drwxr-xr-x 9 root root 0 Jan 25 18:08 ..
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 addr_assign_type
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 addr_len
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 address
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 broadcast
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier_changes
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier_down_count
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 carrier_up_count
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 dev_id
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 dev_port
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 dormant
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 duplex
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 flags
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 gro_flush_timeout
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 ifalias
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 ifindex
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 iflink
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 link_mode
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 mtu
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 name_assign_type
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 netdev_group
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 operstate
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 phys_port_id
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 phys_port_name
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 phys_switch_id
drwxr-xr-x 2 458752 458752 0 Jan 25 18:12 power
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 proto_down
drwxr-xr-x 4 458752 458752 0 Jan 25 18:12 queues
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 speed
drwxr-xr-x 2 458752 458752 0 Jan 25 18:12 statistics
lrwxrwxrwx 1 root root 0 Jan 25 18:12 subsystem -> ../../../../class/net
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 tx_queue_len
-r--r--r-- 1 458752 458752 4096 Jan 25 18:12 type
-rw-r--r-- 1 458752 458752 4096 Jan 25 18:12 uevent
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
unchanged
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
unchanged
---
net/core/net-sysfs.c | 27 +++++++++++++++++++++++++++
net/core/net-sysfs.h | 2 ++
2 files changed, 29 insertions(+)
@@ -1767,6 +1767,33 @@ int netdev_register_kobject(struct net_device *ndev)returnerror;}+/* Change owner for sysfs entries when moving network devices across network+*namespacesownedbydifferentusernamespaces.+*/+intnetdev_change_owner(structnet_device*ndev,conststructnet*net_old,+conststructnet*net_new)+{+structdevice*dev=&ndev->dev;+kuid_told_uid,new_uid;+kgid_told_gid,new_gid;+interror;++net_ns_get_ownership(net_old,&old_uid,&old_gid);+net_ns_get_ownership(net_new,&new_uid,&new_gid);++/* The network namespace was changed but the owning user namespace is+*identicalsothere'snoneedtochangetheownerofsysfsentries.+*/+if(uid_eq(old_uid,new_uid)&&gid_eq(old_gid,new_gid))+return0;++error=device_change_owner(dev,new_uid,new_gid);+if(error)+returnerror;++return0;+}+intnetdev_class_create_file_ns(conststructclass_attribute*class_attr,constvoid*ns){
From: Christian Brauner <hidden> Date: 2020-02-24 17:22:00
Add a helper to change the owner of a sysfs link.
This function will be used to correctly account for kobject ownership
changes, e.g. when moving network devices between network namespaces.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- Greg Kroah-Hartman [off-list ref]:
- Add comment how ownership of sysfs object is changed.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
- Christian Brauner [off-list ref]:
- Add more documentation.
---
fs/sysfs/file.c | 41 +++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 10 ++++++++++
2 files changed, 51 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-24 17:22:04
Add helpers to change the owner of a sysfs files.
This function will be used to correctly account for kobject ownership
changes, e.g. when moving network devices between network namespaces.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- Greg Kroah-Hartman [off-list ref]:
- Better naming for sysfs_file_change_owner() to reflect the fact that it
can be used to change the owner of the kobject itself by passing NULL as
argument.
- Christian Brauner [off-list ref]:
- Split sysfs_file_change_owner() into two helpers sysfs_change_owner() and
sysfs_change_owner_by_name(). The former changes the owner of the kobject
itself, the latter the owner of the kobject looked up via the name
argument.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
- Greg Kroah-Hartman [off-list ref]:
- Remove the second helper which changes the ownership of the kobject itself
and do it in-place instead later on in the series. A separate helper is
not needed for that.
- Christian Brauner [off-list ref]:
- Add more documentation.
---
fs/sysfs/file.c | 47 +++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 10 +++++++++
2 files changed, 57 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-24 17:22:21
Add a helper to change the owner of sysfs objects.
This function will be used to correctly account for kobject ownership
changes, e.g. when moving network devices between network namespaces.
This mirrors how a kobject is added through driver core which in its guts is
done via kobject_add_internal() which in summary creates the main directory via
create_dir(), populates that directory with the groups associated with the
ktype of the kobject (if any) and populates the directory with the basic
attributes associated with the ktype of the kobject (if any). These are the
basic steps that are associated with adding a kobject in sysfs.
Any additional properties are added by the specific subsystem itself (not by
driver core) after it has registered the device. So for the example of network
devices, a network device will e.g. register a queue subdirectory under the
basic sysfs directory for the network device and than further subdirectories
within that queues subdirectory. But that is all specific to network devices
and they call the corresponding sysfs functions to do that directly when they
create those queue objects. So anything that a subsystem adds outside of what
driver core does must also be changed by it (That's already true for removal of
files it created outside of driver core.) and it's the same for ownership
changes.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- Greg Kroah-Hartman [off-list ref]:
- Add comment how ownership of sysfs object is changed.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
- Greg Kroah-Hartman [off-list ref]:
- Change the ownership of the kobject itself directly in
sysfs_change_owner() and do not rely on separate function to do that.
- Add more documentation.
---
fs/sysfs/file.c | 60 +++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 6 +++++
2 files changed, 66 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-24 17:22:30
Add helpers to change the owner of sysfs groups.
This function will be used to correctly account for kobject ownership
changes, e.g. when moving network devices between network namespaces.
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- Greg Kroah-Hartman [off-list ref]:
- Add comment how ownership of sysfs object is changed.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
- Christian Brauner [off-list ref]:
- Collapse groups ownership helper patches into a single patch.
/* v4 */
unchanged
---
fs/sysfs/group.c | 117 ++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 20 ++++++++
2 files changed, 137 insertions(+)
From: "Rafael J. Wysocki" <rafael@kernel.org> Date: 2020-02-24 17:56:17
On Mon, Feb 24, 2020 at 6:21 PM Christian Brauner
[off-list ref] wrote:
quoted hunk
Add a helper to change the owner of a device's power entries. This
needs to happen when the ownership of a device is changed, e.g. when
moving network devices between network namespaces.
This function will be used to correctly account for ownership changes,
e.g. when moving network devices between network namespaces.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- "Rafael J. Wysocki" [off-list ref]:
- Fold if (dev->power.wakeup && dev->power.wakeup->dev) check into
if (device_can_wakeup(dev)) check since the former can never be true if
the latter is false.
- Christian Brauner [off-list ref]:
- Place (dev->power.wakeup && dev->power.wakeup->dev) check under
CONFIG_PM_SLEEP ifdefine since it will wakeup_source will only be available
when this config option is set.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
- "Rafael J. Wysocki" [off-list ref]:
- Remove in-function #ifdef in favor of separate helper that is a nop
whenver !CONFIG_PM_SLEEP.
---
drivers/base/core.c | 4 +++
drivers/base/power/power.h | 3 ++
drivers/base/power/sysfs.c | 61 +++++++++++++++++++++++++++++++++++++-
3 files changed, 67 insertions(+), 1 deletion(-)
From: Christian Brauner <hidden> Date: 2020-02-24 18:00:27
On Mon, Feb 24, 2020 at 06:56:03PM +0100, Rafael J. Wysocki wrote:
On Mon, Feb 24, 2020 at 6:21 PM Christian Brauner
[off-list ref] wrote:
quoted
Add a helper to change the owner of a device's power entries. This
needs to happen when the ownership of a device is changed, e.g. when
moving network devices between network namespaces.
This function will be used to correctly account for ownership changes,
e.g. when moving network devices between network namespaces.
Signed-off-by: Christian Brauner <redacted>
---
/* v2 */
- "Rafael J. Wysocki" [off-list ref]:
- Fold if (dev->power.wakeup && dev->power.wakeup->dev) check into
if (device_can_wakeup(dev)) check since the former can never be true if
the latter is false.
- Christian Brauner [off-list ref]:
- Place (dev->power.wakeup && dev->power.wakeup->dev) check under
CONFIG_PM_SLEEP ifdefine since it will wakeup_source will only be available
when this config option is set.
/* v3 */
- Greg Kroah-Hartman [off-list ref]:
- Add explicit uid/gid parameters.
/* v4 */
- "Rafael J. Wysocki" [off-list ref]:
- Remove in-function #ifdef in favor of separate helper that is a nop
whenver !CONFIG_PM_SLEEP.
---
drivers/base/core.c | 4 +++
drivers/base/power/power.h | 3 ++
drivers/base/power/sysfs.c | 61 +++++++++++++++++++++++++++++++++++++-
3 files changed, 67 insertions(+), 1 deletion(-)