Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b0e37c0d authored by Dmitry Torokhov's avatar Dmitry Torokhov Committed by David S. Miller
Browse files

net-sysfs: make sure objects belong to container's owner



When creating various objects in /sys/class/net/... make sure that they
belong to container's owner instead of global root (if they belong to a
container/namespace).

Co-Developed-by: default avatarTyler Hicks <tyhicks@canonical.com>
Signed-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: default avatarTyler Hicks <tyhicks@canonical.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 3033fced
Loading
Loading
Loading
Loading
+46 −1
Original line number Original line Diff line number Diff line
@@ -656,6 +656,24 @@ static const struct attribute_group wireless_group = {
#define net_class_groups	NULL
#define net_class_groups	NULL
#endif /* CONFIG_SYSFS */
#endif /* CONFIG_SYSFS */


static void net_ns_get_ownership(const struct net *net,
				 kuid_t *uid, kgid_t *gid)
{
	if (net) {
		kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
		kgid_t ns_root_gid = make_kgid(net->user_ns, 0);

		if (uid_valid(ns_root_uid))
			*uid = ns_root_uid;

		if (gid_valid(ns_root_gid))
			*gid = ns_root_gid;
	} else {
		*uid = GLOBAL_ROOT_UID;
		*gid = GLOBAL_ROOT_GID;
	}
}

#ifdef CONFIG_SYSFS
#ifdef CONFIG_SYSFS
#define to_rx_queue_attr(_attr) \
#define to_rx_queue_attr(_attr) \
	container_of(_attr, struct rx_queue_attribute, attr)
	container_of(_attr, struct rx_queue_attribute, attr)
@@ -905,11 +923,20 @@ static const void *rx_queue_namespace(struct kobject *kobj)
	return ns;
	return ns;
}
}


static void rx_queue_get_ownership(struct kobject *kobj,
				   kuid_t *uid, kgid_t *gid)
{
	const struct net *net = rx_queue_namespace(kobj);

	net_ns_get_ownership(net, uid, gid);
}

static struct kobj_type rx_queue_ktype __ro_after_init = {
static struct kobj_type rx_queue_ktype __ro_after_init = {
	.sysfs_ops = &rx_queue_sysfs_ops,
	.sysfs_ops = &rx_queue_sysfs_ops,
	.release = rx_queue_release,
	.release = rx_queue_release,
	.default_attrs = rx_queue_default_attrs,
	.default_attrs = rx_queue_default_attrs,
	.namespace = rx_queue_namespace
	.namespace = rx_queue_namespace,
	.get_ownership = rx_queue_get_ownership,
};
};


static int rx_queue_add_kobject(struct net_device *dev, int index)
static int rx_queue_add_kobject(struct net_device *dev, int index)
@@ -1431,11 +1458,20 @@ static const void *netdev_queue_namespace(struct kobject *kobj)
	return ns;
	return ns;
}
}


static void netdev_queue_get_ownership(struct kobject *kobj,
				       kuid_t *uid, kgid_t *gid)
{
	const struct net *net = netdev_queue_namespace(kobj);

	net_ns_get_ownership(net, uid, gid);
}

static struct kobj_type netdev_queue_ktype __ro_after_init = {
static struct kobj_type netdev_queue_ktype __ro_after_init = {
	.sysfs_ops = &netdev_queue_sysfs_ops,
	.sysfs_ops = &netdev_queue_sysfs_ops,
	.release = netdev_queue_release,
	.release = netdev_queue_release,
	.default_attrs = netdev_queue_default_attrs,
	.default_attrs = netdev_queue_default_attrs,
	.namespace = netdev_queue_namespace,
	.namespace = netdev_queue_namespace,
	.get_ownership = netdev_queue_get_ownership,
};
};


static int netdev_queue_add_kobject(struct net_device *dev, int index)
static int netdev_queue_add_kobject(struct net_device *dev, int index)
@@ -1625,6 +1661,14 @@ static const void *net_namespace(struct device *d)
	return dev_net(dev);
	return dev_net(dev);
}
}


static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid)
{
	struct net_device *dev = to_net_dev(d);
	const struct net *net = dev_net(dev);

	net_ns_get_ownership(net, uid, gid);
}

static struct class net_class __ro_after_init = {
static struct class net_class __ro_after_init = {
	.name = "net",
	.name = "net",
	.dev_release = netdev_release,
	.dev_release = netdev_release,
@@ -1632,6 +1676,7 @@ static struct class net_class __ro_after_init = {
	.dev_uevent = netdev_uevent,
	.dev_uevent = netdev_uevent,
	.ns_type = &net_ns_type_operations,
	.ns_type = &net_ns_type_operations,
	.namespace = net_namespace,
	.namespace = net_namespace,
	.get_ownership = net_get_ownership,
};
};


#ifdef CONFIG_OF_NET
#ifdef CONFIG_OF_NET