From: Christian Brauner <hidden> Date: 2020-02-18 16:31:09
Hey everyone,
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 | 84 +++++++++++++++++++++
drivers/base/power/power.h | 3 +
drivers/base/power/sysfs.c | 42 +++++++++++
fs/sysfs/file.c | 146 +++++++++++++++++++++++++++++++++++++
fs/sysfs/group.c | 117 +++++++++++++++++++++++++++++
include/linux/device.h | 1 +
include/linux/sysfs.h | 53 ++++++++++++++
net/core/dev.c | 9 ++-
net/core/net-sysfs.c | 133 +++++++++++++++++++++++++++++++++
net/core/net-sysfs.h | 2 +
10 files changed, 589 insertions(+), 1 deletion(-)
base-commit: bb6d3fb354c5ee8d6bde2d576eb7220ea09862b9
--
2.25.0
From: Christian Brauner <hidden> Date: 2020-02-18 16:30:09
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.
---
fs/sysfs/file.c | 67 +++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 17 +++++++++++
2 files changed, 84 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-18 16:30:12
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.
---
fs/sysfs/file.c | 40 ++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 10 ++++++++++
2 files changed, 50 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-18 16:30:18
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.
---
net/core/net-sysfs.c | 106 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-18 16:30:23
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.
---
drivers/base/core.c | 80 ++++++++++++++++++++++++++++++++++++++++++
include/linux/device.h | 1 +
2 files changed, 81 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-18 16:30:26
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
---
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-18 16:30:34
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.
---
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-18 16:30:43
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.
---
drivers/base/core.c | 4 ++++
drivers/base/power/power.h | 3 +++
drivers/base/power/sysfs.c | 42 ++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-18 16:30:47
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.
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.
---
fs/sysfs/file.c | 39 +++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 6 ++++++
2 files changed, 45 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-18 16:31:05
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.
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.
---
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-20 10:02:18
On Tue, Feb 18, 2020 at 5:30 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.
---
drivers/base/core.c | 4 ++++
drivers/base/power/power.h | 3 +++
drivers/base/power/sysfs.c | 42 ++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
@@ -684,6 +684,48 @@ int dpm_sysfs_add(struct device *dev)returnrc;}+intdpm_sysfs_change_owner(structdevice*dev,kuid_tkuid,kgid_tkgid)+{+intrc;++if(device_pm_not_required(dev))+return0;++rc=sysfs_group_change_owner(&dev->kobj,&pm_attr_group,kuid,kgid);+if(rc)+returnrc;++if(pm_runtime_callbacks_present(dev)){+rc=sysfs_group_change_owner(+&dev->kobj,&pm_runtime_attr_group,kuid,kgid);+if(rc)+returnrc;+}+if(device_can_wakeup(dev)){+rc=sysfs_group_change_owner(&dev->kobj,&pm_wakeup_attr_group,+kuid,kgid);+if(rc)+returnrc;++#ifdef CONFIG_PM_SLEEP+if(dev->power.wakeup&&dev->power.wakeup->dev){+rc=device_change_owner(dev->power.wakeup->dev,kuid,+kgid);+if(rc)+returnrc;+}+#endif
First off, I don't particularly like #ifdefs in function bodies. In
particular, there is a CONFIG_PM_SLEEP block in this file already and
you could define a new function in there to carry out the above
operations, and provide an empty stub of it for the "unset" case.
Failing to do so is somewhat on the "rushing things in" side in my
view.
Second, the #ifdef should cover the entire if (device_can_wakeup(dev))
{} block, because wakeup_sysfs_add() is only called if
device_can_wakeup(dev) returns 'true' for the device in question (and
arguably you could have checked that easily enough).
From: Christian Brauner <hidden> Date: 2020-02-20 10:21:17
On Thu, Feb 20, 2020 at 11:02:04AM +0100, Rafael J. Wysocki wrote:
On Tue, Feb 18, 2020 at 5:30 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.
---
drivers/base/core.c | 4 ++++
drivers/base/power/power.h | 3 +++
drivers/base/power/sysfs.c | 42 ++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
@@ -684,6 +684,48 @@ int dpm_sysfs_add(struct device *dev)returnrc;}+intdpm_sysfs_change_owner(structdevice*dev,kuid_tkuid,kgid_tkgid)+{+intrc;++if(device_pm_not_required(dev))+return0;++rc=sysfs_group_change_owner(&dev->kobj,&pm_attr_group,kuid,kgid);+if(rc)+returnrc;++if(pm_runtime_callbacks_present(dev)){+rc=sysfs_group_change_owner(+&dev->kobj,&pm_runtime_attr_group,kuid,kgid);+if(rc)+returnrc;+}+if(device_can_wakeup(dev)){+rc=sysfs_group_change_owner(&dev->kobj,&pm_wakeup_attr_group,+kuid,kgid);+if(rc)+returnrc;++#ifdef CONFIG_PM_SLEEP+if(dev->power.wakeup&&dev->power.wakeup->dev){+rc=device_change_owner(dev->power.wakeup->dev,kuid,+kgid);+if(rc)+returnrc;+}+#endif
First off, I don't particularly like #ifdefs in function bodies. In
particular, there is a CONFIG_PM_SLEEP block in this file already and
you could define a new function in there to carry out the above
operations, and provide an empty stub of it for the "unset" case.
Failing to do so is somewhat on the "rushing things in" side in my
view.
How ifdefines are used is highly dependent on the subsystem; networking
ofen uses in-place ifdefines in some parts and not in others. That has
nothing to do with rushing things. I'm happy to change it to your
preferences. Thanks for pointing out your expectations. But please don't
assume bad intentions on my part because I'm not meeting them right
away. It often is the case that adding a helper that is called in one
place is not well-received.
Second, the #ifdef should cover the entire if (device_can_wakeup(dev))
{} block, because wakeup_sysfs_add() is only called if
device_can_wakeup(dev) returns 'true' for the device in question (and
arguably you could have checked that easily enough).
I've looked at the header definitions for device_can_wakeup() and with
and without CONFIG_PM_SLEEP it is defined as:
static inline bool device_can_wakeup(struct device *dev)
{
return dev->power.can_wakeup;
}
which to me looks like it would neet to be called in all cases.
I'll rework this to you preferences.
Thanks!
Christian
From: "Rafael J. Wysocki" <rafael@kernel.org> Date: 2020-02-20 10:30:47
On Thu, Feb 20, 2020 at 11:21 AM Christian Brauner
[off-list ref] wrote:
On Thu, Feb 20, 2020 at 11:02:04AM +0100, Rafael J. Wysocki wrote:
quoted
On Tue, Feb 18, 2020 at 5:30 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.
---
drivers/base/core.c | 4 ++++
drivers/base/power/power.h | 3 +++
drivers/base/power/sysfs.c | 42 ++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
@@ -684,6 +684,48 @@ int dpm_sysfs_add(struct device *dev)returnrc;}+intdpm_sysfs_change_owner(structdevice*dev,kuid_tkuid,kgid_tkgid)+{+intrc;++if(device_pm_not_required(dev))+return0;++rc=sysfs_group_change_owner(&dev->kobj,&pm_attr_group,kuid,kgid);+if(rc)+returnrc;++if(pm_runtime_callbacks_present(dev)){+rc=sysfs_group_change_owner(+&dev->kobj,&pm_runtime_attr_group,kuid,kgid);+if(rc)+returnrc;+}+if(device_can_wakeup(dev)){+rc=sysfs_group_change_owner(&dev->kobj,&pm_wakeup_attr_group,+kuid,kgid);+if(rc)+returnrc;++#ifdef CONFIG_PM_SLEEP+if(dev->power.wakeup&&dev->power.wakeup->dev){+rc=device_change_owner(dev->power.wakeup->dev,kuid,+kgid);+if(rc)+returnrc;+}+#endif
First off, I don't particularly like #ifdefs in function bodies. In
particular, there is a CONFIG_PM_SLEEP block in this file already and
you could define a new function in there to carry out the above
operations, and provide an empty stub of it for the "unset" case.
Failing to do so is somewhat on the "rushing things in" side in my
view.
How ifdefines are used is highly dependent on the subsystem; networking
ofen uses in-place ifdefines in some parts and not in others. That has
nothing to do with rushing things. I'm happy to change it to your
preferences.
Thanks!
Thanks for pointing out your expectations. But please don't
assume bad intentions on my part because I'm not meeting them right
away. It often is the case that adding a helper that is called in one
place is not well-received.
From: Christian Brauner <hidden> Date: 2020-02-20 10:35:35
On Thu, Feb 20, 2020 at 11:30:32AM +0100, Rafael J. Wysocki wrote:
On Thu, Feb 20, 2020 at 11:21 AM Christian Brauner
[off-list ref] wrote:
quoted
On Thu, Feb 20, 2020 at 11:02:04AM +0100, Rafael J. Wysocki wrote:
quoted
On Tue, Feb 18, 2020 at 5:30 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.
---
drivers/base/core.c | 4 ++++
drivers/base/power/power.h | 3 +++
drivers/base/power/sysfs.c | 42 ++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
@@ -684,6 +684,48 @@ int dpm_sysfs_add(struct device *dev)returnrc;}+intdpm_sysfs_change_owner(structdevice*dev,kuid_tkuid,kgid_tkgid)+{+intrc;++if(device_pm_not_required(dev))+return0;++rc=sysfs_group_change_owner(&dev->kobj,&pm_attr_group,kuid,kgid);+if(rc)+returnrc;++if(pm_runtime_callbacks_present(dev)){+rc=sysfs_group_change_owner(+&dev->kobj,&pm_runtime_attr_group,kuid,kgid);+if(rc)+returnrc;+}+if(device_can_wakeup(dev)){+rc=sysfs_group_change_owner(&dev->kobj,&pm_wakeup_attr_group,+kuid,kgid);+if(rc)+returnrc;++#ifdef CONFIG_PM_SLEEP+if(dev->power.wakeup&&dev->power.wakeup->dev){+rc=device_change_owner(dev->power.wakeup->dev,kuid,+kgid);+if(rc)+returnrc;+}+#endif
First off, I don't particularly like #ifdefs in function bodies. In
particular, there is a CONFIG_PM_SLEEP block in this file already and
you could define a new function in there to carry out the above
operations, and provide an empty stub of it for the "unset" case.
Failing to do so is somewhat on the "rushing things in" side in my
view.
How ifdefines are used is highly dependent on the subsystem; networking
ofen uses in-place ifdefines in some parts and not in others. That has
nothing to do with rushing things. I'm happy to change it to your
preferences.
Thanks!
quoted
Thanks for pointing out your expectations. But please don't
assume bad intentions on my part because I'm not meeting them right
away. It often is the case that adding a helper that is called in one
place is not well-received.
Fair enough, sorry for being harsh.
Np, I didn't read it as such. I was really just worried you thought
that I was trying to rush things in. It's Thursday anyway, usually about
the time where we're all grumpy because we can't wait until we made it
to Friday. :)
Christian
On Tue, Feb 18, 2020 at 05:29:35PM +0100, Christian Brauner wrote:
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>
On Tue, Feb 18, 2020 at 05:29:35PM +0100, Christian Brauner wrote:
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.
Looks much better, thanks for doing these changes. I'll review the
whole series now...
greg k-h
On Tue, Feb 18, 2020 at 05:29:36PM +0100, Christian Brauner wrote:
quoted hunk
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.
---
fs/sysfs/file.c | 40 ++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 10 ++++++++++
2 files changed, 50 insertions(+)
On Tue, Feb 18, 2020 at 05:29:37PM +0100, Christian Brauner wrote:
quoted hunk
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.
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.
---
fs/sysfs/group.c | 117 ++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 20 ++++++++
2 files changed, 137 insertions(+)
@@ -457,3 +458,119 @@ int __compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,returnPTR_ERR_OR_ZERO(link);}EXPORT_SYMBOL_GPL(__compat_only_sysfs_link_entry_to_kobj);++staticintsysfs_group_attrs_change_owner(structkernfs_node*grp_kn,+conststructattribute_group*grp,+structiattr*newattrs)+{+structkernfs_node*kn;+interror;++if(grp->attrs){+structattribute*const*attr;++for(attr=grp->attrs;*attr;attr++){+kn=kernfs_find_and_get(grp_kn,(*attr)->name);+if(!kn)+return-ENOENT;++error=kernfs_setattr(kn,newattrs);+kernfs_put(kn);+if(error)+returnerror;+}+}++if(grp->bin_attrs){+structbin_attribute*const*bin_attr;++for(bin_attr=grp->bin_attrs;*bin_attr;bin_attr++){+kn=kernfs_find_and_get(grp_kn,(*bin_attr)->attr.name);+if(!kn)+return-ENOENT;++error=kernfs_setattr(kn,newattrs);+kernfs_put(kn);+if(error)+returnerror;+}+}++return0;+}++/**+*sysfs_group_change_owner-changeownerofanattributegroup.+*@kobj:Thekobjectcontainingthegroup.+*@grp:Theattributegroup.+*@kuid:newowner'skuid+*@kgid:newowner'skgid+*+*Returns0onsuccessorerrorcodeonfailure.
This is fine to document, just funny it's the only one documented about
the return value so far in this series.
Anyway, looks good to me:
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Oops, wait, what "file" are you changing here? You aren't changing the
kobject's attributes, but rather a file in the kobject's directory,
right? But kobj->sd is the directory of the kobject itself, so why
isn't this function just the same thing as sysfs_change_owner()?
Why would you call this function at all?
confused,
greg k-h
On Tue, Feb 18, 2020 at 05:29:38PM +0100, Christian Brauner wrote:
quoted hunk
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.
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.
---
fs/sysfs/file.c | 39 +++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 6 ++++++
2 files changed, 45 insertions(+)
Then what are you changing here?
I think the kerneldoc needs a lot more explaination as to what is going
on in this function and why you would call it, and not some of the other
functions you are adding.
thanks,
greg k-h
On Tue, Feb 18, 2020 at 05:29:39PM +0100, Christian Brauner wrote:
quoted hunk
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.
---
drivers/base/core.c | 80 ++++++++++++++++++++++++++++++++++++++++++
include/linux/device.h | 1 +
2 files changed, 81 insertions(+)
On Wed, Feb 19, 2020 at 04:24:16PM -0800, David Miller wrote:
From: Christian Brauner <redacted>
Date: Tue, 18 Feb 2020 17:29:34 +0100
quoted
This is v3 with explicit uid and gid parameters added to functions that
change sysfs object ownership as Greg requested.
Greg, please review.
Give me a chance :)
It's looking better, still needs a little bit of work before I'm happy
with the driver core and sysfs bits, see my review comments so far.
thanks,
greg k-h
From: Christian Brauner <hidden> Date: 2020-02-20 19:38:43
On Thu, Feb 20, 2020 at 12:15:50PM +0100, Greg Kroah-Hartman wrote:
On Tue, Feb 18, 2020 at 05:29:37PM +0100, Christian Brauner wrote:
quoted
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.
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.
---
fs/sysfs/group.c | 117 ++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 20 ++++++++
2 files changed, 137 insertions(+)
@@ -457,3 +458,119 @@ int __compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,returnPTR_ERR_OR_ZERO(link);}EXPORT_SYMBOL_GPL(__compat_only_sysfs_link_entry_to_kobj);++staticintsysfs_group_attrs_change_owner(structkernfs_node*grp_kn,+conststructattribute_group*grp,+structiattr*newattrs)+{+structkernfs_node*kn;+interror;++if(grp->attrs){+structattribute*const*attr;++for(attr=grp->attrs;*attr;attr++){+kn=kernfs_find_and_get(grp_kn,(*attr)->name);+if(!kn)+return-ENOENT;++error=kernfs_setattr(kn,newattrs);+kernfs_put(kn);+if(error)+returnerror;+}+}++if(grp->bin_attrs){+structbin_attribute*const*bin_attr;++for(bin_attr=grp->bin_attrs;*bin_attr;bin_attr++){+kn=kernfs_find_and_get(grp_kn,(*bin_attr)->attr.name);+if(!kn)+return-ENOENT;++error=kernfs_setattr(kn,newattrs);+kernfs_put(kn);+if(error)+returnerror;+}+}++return0;+}++/**+*sysfs_group_change_owner-changeownerofanattributegroup.+*@kobj:Thekobjectcontainingthegroup.+*@grp:Theattributegroup.+*@kuid:newowner'skuid+*@kgid:newowner'skgid+*+*Returns0onsuccessorerrorcodeonfailure.
This is fine to document, just funny it's the only one documented about
the return value so far in this series.
I stuck to the documentation style common to the file. Most of the
functions in fs/syfs/file.c did not mention return codes
sysfs_remove_bin_file(), sysfs_create_bin_file(),
sysfs_remove_file_from_group() etc. But I'll document all in this series
with return codes now.
Thanks!
Christian
From: Christian Brauner <hidden> Date: 2020-02-20 19:51:54
On Thu, Feb 20, 2020 at 12:14:43PM +0100, Greg Kroah-Hartman wrote:
On Tue, Feb 18, 2020 at 05:29:36PM +0100, Christian Brauner wrote:
quoted
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.
---
fs/sysfs/file.c | 40 ++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 10 ++++++++++
2 files changed, 50 insertions(+)
From: Christian Brauner <hidden> Date: 2020-02-20 20:14:02
On Thu, Feb 20, 2020 at 12:23:14PM +0100, Greg Kroah-Hartman wrote:
On Tue, Feb 18, 2020 at 05:29:38PM +0100, Christian Brauner wrote:
quoted
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.
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.
---
fs/sysfs/file.c | 39 +++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 6 ++++++
2 files changed, 45 insertions(+)
And here you change all of the files of the kobject.
This changes the default attributes associated with that ktype (if any)
and mirrors how a kobject is registered in sysfs.
But what about files that have a subdir? Does that also happen here?
Maybe that was all to brief on my end, sorry:
So all of this mirrors how a kobject is added through driver core which
in its guts is done via kobject_add_internal() which in summary does:
- create the main directory via create_dir()
- populate the directory with the groups associated with that ktype (if any)
- populate the directory with the basic attributes associated with that
ktype (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 them (That's already true for removal
of files it created outside of driver core.) and it's the same for
ownership changes. :)
I'll document that.
So this changes the default groups associated with the ktype for that
kobject and again mirrors how a kobject is registered in sysfs.
I think the kerneldoc needs a lot more explaination as to what is going
on in this function and why you would call it, and not some of the other
functions you are adding.
Oops, wait, what "file" are you changing here? You aren't changing the
kobject's attributes, but rather a file in the kobject's directory,
right? But kobj->sd is the directory of the kobject itself, so why
isn't this function just the same thing as sysfs_change_owner()?
I've moved it directly into sysfs_change_owner(), removed the function,
and renamed "_by_name()" back to sysfs_file_change_owner() which is
easier to parse and makes more sense.
From: Christian Brauner <hidden> Date: 2020-02-24 13:18:24
On Thu, Feb 20, 2020 at 12:25:13PM +0100, Greg Kroah-Hartman wrote:
On Tue, Feb 18, 2020 at 05:29:39PM +0100, Christian Brauner wrote:
quoted
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.
---
drivers/base/core.c | 80 ++++++++++++++++++++++++++++++++++++++++++
include/linux/device.h | 1 +
2 files changed, 81 insertions(+)
So, this changed the ownership of the class link for the device to match
the directory entry for that device, so e.g. given a network device
symlink (or any other type) that points to the actual directory entry
for that device:
/sys/class/net/my-dev -> ../../devices/virtual/net/my-dev
it makes my-dev show the same permissions as the directory my-dev has.
If we don't do this this will look weird, because the symlink will show
different permissions than the target it is pointoing to.
Again, more documentation please as to exactly what is being changed in
this function is needed.