Skip to content

Commit 4e4098a

Browse files
committed
driver core: handle user namespaces properly with the uid/gid devtmpfs change
Now that devtmpfs is caring about uid/gid, we need to use the correct internal types so users who have USER_NS enabled will have things work properly for them. Thanks to Eric for pointing this out, and the patch review. Reported-by: Eric W. Biederman <ebiederm@xmission.com> Cc: Kay Sievers <kay@vrfy.org> Cc: Ming Lei <ming.lei@canonical.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d81c8d1 commit 4e4098a

5 files changed

Lines changed: 20 additions & 20 deletions

File tree

‎block/genhd.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ struct class block_class = {
11121112
};
11131113

11141114
static char *block_devnode(struct device *dev, umode_t *mode,
1115-
uid_t *uid, gid_t *gid)
1115+
kuid_t *uid, kgid_t *gid)
11161116
{
11171117
struct gendisk *disk = dev_to_disk(dev);
11181118

‎drivers/base/core.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
283283
const char *tmp;
284284
const char *name;
285285
umode_t mode = 0;
286-
uid_t uid = 0;
287-
gid_t gid = 0;
286+
kuid_t uid = GLOBAL_ROOT_UID;
287+
kgid_t gid = GLOBAL_ROOT_GID;
288288

289289
add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
290290
add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
@@ -293,10 +293,10 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
293293
add_uevent_var(env, "DEVNAME=%s", name);
294294
if (mode)
295295
add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
296-
if (uid)
297-
add_uevent_var(env, "DEVUID=%u", uid);
298-
if (gid)
299-
add_uevent_var(env, "DEVGID=%u", gid);
296+
if (!uid_eq(uid, GLOBAL_ROOT_UID))
297+
add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
298+
if (!gid_eq(gid, GLOBAL_ROOT_GID))
299+
add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
300300
kfree(tmp);
301301
}
302302
}
@@ -1297,7 +1297,7 @@ static struct device *next_device(struct klist_iter *i)
12971297
* freed by the caller.
12981298
*/
12991299
const char *device_get_devnode(struct device *dev,
1300-
umode_t *mode, uid_t *uid, gid_t *gid,
1300+
umode_t *mode, kuid_t *uid, kgid_t *gid,
13011301
const char **tmp)
13021302
{
13031303
char *s;

‎drivers/base/devtmpfs.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ static struct req {
4242
int err;
4343
const char *name;
4444
umode_t mode; /* 0 => delete */
45-
uid_t uid;
46-
gid_t gid;
45+
kuid_t uid;
46+
kgid_t gid;
4747
struct device *dev;
4848
} *requests;
4949

@@ -88,8 +88,8 @@ int devtmpfs_create_node(struct device *dev)
8888
return 0;
8989

9090
req.mode = 0;
91-
req.uid = 0;
92-
req.gid = 0;
91+
req.uid = GLOBAL_ROOT_UID;
92+
req.gid = GLOBAL_ROOT_GID;
9393
req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
9494
if (!req.name)
9595
return -ENOMEM;
@@ -192,8 +192,8 @@ static int create_path(const char *nodepath)
192192
return err;
193193
}
194194

195-
static int handle_create(const char *nodename, umode_t mode, uid_t uid,
196-
gid_t gid, struct device *dev)
195+
static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
196+
kgid_t gid, struct device *dev)
197197
{
198198
struct dentry *dentry;
199199
struct path path;
@@ -212,8 +212,8 @@ static int handle_create(const char *nodename, umode_t mode, uid_t uid,
212212
struct iattr newattrs;
213213

214214
newattrs.ia_mode = mode;
215-
newattrs.ia_uid = KUIDT_INIT(uid);
216-
newattrs.ia_gid = KGIDT_INIT(gid);
215+
newattrs.ia_uid = uid;
216+
newattrs.ia_gid = gid;
217217
newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
218218
mutex_lock(&dentry->d_inode->i_mutex);
219219
notify_change(dentry, &newattrs);
@@ -364,7 +364,7 @@ int devtmpfs_mount(const char *mntdir)
364364

365365
static DECLARE_COMPLETION(setup_done);
366366

367-
static int handle(const char *name, umode_t mode, uid_t uid, gid_t gid,
367+
static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
368368
struct device *dev)
369369
{
370370
if (mode)

‎drivers/usb/core/usb.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ static const struct dev_pm_ops usb_device_pm_ops = {
318318

319319

320320
static char *usb_devnode(struct device *dev,
321-
umode_t *mode, uid_t *uid, gid_t *gid)
321+
umode_t *mode, kuid_t *uid, kgid_t *gid)
322322
{
323323
struct usb_device *usb_dev;
324324

‎include/linux/device.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ struct device_type {
467467
const struct attribute_group **groups;
468468
int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
469469
char *(*devnode)(struct device *dev, umode_t *mode,
470-
uid_t *uid, gid_t *gid);
470+
kuid_t *uid, kgid_t *gid);
471471
void (*release)(struct device *dev);
472472

473473
const struct dev_pm_ops *pm;
@@ -845,7 +845,7 @@ extern int device_rename(struct device *dev, const char *new_name);
845845
extern int device_move(struct device *dev, struct device *new_parent,
846846
enum dpm_order dpm_order);
847847
extern const char *device_get_devnode(struct device *dev,
848-
umode_t *mode, uid_t *uid, gid_t *gid,
848+
umode_t *mode, kuid_t *uid, kgid_t *gid,
849849
const char **tmp);
850850
extern void *dev_get_drvdata(const struct device *dev);
851851
extern int dev_set_drvdata(struct device *dev, void *data);

0 commit comments

Comments
 (0)