Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

device: add device_change_owner()

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.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Christian Brauner and committed by
David S. Miller
b8f33e5d 2c4f9401

+117
+116
drivers/base/core.c
··· 3458 3458 } 3459 3459 EXPORT_SYMBOL_GPL(device_move); 3460 3460 3461 + static int device_attrs_change_owner(struct device *dev, kuid_t kuid, 3462 + kgid_t kgid) 3463 + { 3464 + struct kobject *kobj = &dev->kobj; 3465 + struct class *class = dev->class; 3466 + const struct device_type *type = dev->type; 3467 + int error; 3468 + 3469 + if (class) { 3470 + /* 3471 + * Change the device groups of the device class for @dev to 3472 + * @kuid/@kgid. 3473 + */ 3474 + error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid, 3475 + kgid); 3476 + if (error) 3477 + return error; 3478 + } 3479 + 3480 + if (type) { 3481 + /* 3482 + * Change the device groups of the device type for @dev to 3483 + * @kuid/@kgid. 3484 + */ 3485 + error = sysfs_groups_change_owner(kobj, type->groups, kuid, 3486 + kgid); 3487 + if (error) 3488 + return error; 3489 + } 3490 + 3491 + /* Change the device groups of @dev to @kuid/@kgid. */ 3492 + error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid); 3493 + if (error) 3494 + return error; 3495 + 3496 + if (device_supports_offline(dev) && !dev->offline_disabled) { 3497 + /* Change online device attributes of @dev to @kuid/@kgid. */ 3498 + error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name, 3499 + kuid, kgid); 3500 + if (error) 3501 + return error; 3502 + } 3503 + 3504 + return 0; 3505 + } 3506 + 3507 + /** 3508 + * device_change_owner - change the owner of an existing device. 3509 + * @dev: device. 3510 + * @kuid: new owner's kuid 3511 + * @kgid: new owner's kgid 3512 + * 3513 + * This changes the owner of @dev and its corresponding sysfs entries to 3514 + * @kuid/@kgid. This function closely mirrors how @dev was added via driver 3515 + * core. 3516 + * 3517 + * Returns 0 on success or error code on failure. 3518 + */ 3519 + int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) 3520 + { 3521 + int error; 3522 + struct kobject *kobj = &dev->kobj; 3523 + 3524 + dev = get_device(dev); 3525 + if (!dev) 3526 + return -EINVAL; 3527 + 3528 + /* 3529 + * Change the kobject and the default attributes and groups of the 3530 + * ktype associated with it to @kuid/@kgid. 3531 + */ 3532 + error = sysfs_change_owner(kobj, kuid, kgid); 3533 + if (error) 3534 + goto out; 3535 + 3536 + /* 3537 + * Change the uevent file for @dev to the new owner. The uevent file 3538 + * was created in a separate step when @dev got added and we mirror 3539 + * that step here. 3540 + */ 3541 + error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid, 3542 + kgid); 3543 + if (error) 3544 + goto out; 3545 + 3546 + /* 3547 + * Change the device groups, the device groups associated with the 3548 + * device class, and the groups associated with the device type of @dev 3549 + * to @kuid/@kgid. 3550 + */ 3551 + error = device_attrs_change_owner(dev, kuid, kgid); 3552 + if (error) 3553 + goto out; 3554 + 3555 + #ifdef CONFIG_BLOCK 3556 + if (sysfs_deprecated && dev->class == &block_class) 3557 + goto out; 3558 + #endif 3559 + 3560 + /* 3561 + * Change the owner of the symlink located in the class directory of 3562 + * the device class associated with @dev which points to the actual 3563 + * directory entry for @dev to @kuid/@kgid. This ensures that the 3564 + * symlink shows the same permissions as its target. 3565 + */ 3566 + error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj, 3567 + dev_name(dev), kuid, kgid); 3568 + if (error) 3569 + goto out; 3570 + 3571 + out: 3572 + put_device(dev); 3573 + return error; 3574 + } 3575 + EXPORT_SYMBOL_GPL(device_change_owner); 3576 + 3461 3577 /** 3462 3578 * device_shutdown - call ->shutdown() on each device to shutdown. 3463 3579 */
+1
include/linux/device.h
··· 817 817 extern int device_rename(struct device *dev, const char *new_name); 818 818 extern int device_move(struct device *dev, struct device *new_parent, 819 819 enum dpm_order dpm_order); 820 + extern int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid); 820 821 extern const char *device_get_devnode(struct device *dev, 821 822 umode_t *mode, kuid_t *uid, kgid_t *gid, 822 823 const char **tmp);