Skip to contentSkip to conversation

vfs: allow unprivileged whiteout creation

PATCH In review Miklos Szeredi 4 participants 8 messages Apr 9, 2020 – May 5, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfs: allow unprivileged whiteout creation

From: Miklos Szeredi <[email protected]>

Whiteouts, unlike real device node should not require privileges to create.

The general concern with device nodes is that opening them can have side effects. The kernel already avoids zero major (see Documentation/admin-guide/devices.txt). To be on the safe side the patch explicitly forbids registering a char device with 0/0 number (see cdev_add()).

This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV; i.e. it won't have any side effect.

---
 fs/char_dev.c                 |    3 +++
 fs/namei.c                    |   17 ++++-------------
 include/linux/device_cgroup.h |    3 +++
 3 files changed, 10 insertions(+), 13 deletions(-)
fs/char_dev.c +3 -0
@@ -483,6 +483,9 @@ int cdev_add(struct cdev *p, dev_t dev,
 	p->dev = dev;
 	p->count = count;
 
+	if (WARN_ON(dev == WHITEOUT_DEV))
+		return -EBUSY;
+
 	error = kobj_map(cdev_map, dev, count, NULL,
 			 exact_match, exact_lock, p);
 	if (error)
fs/namei.c +4 -13
@@ -3505,12 +3505,14 @@ EXPORT_SYMBOL(user_path_create);
 
 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
 {
+	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
 	int error = may_create(dir, dentry);
 
 	if (error)
 		return error;
 
-	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+	    !is_whiteout)
 		return -EPERM;
 
 	if (!dir->i_op->mknod)
@@ -4345,9 +4347,6 @@ static int do_renameat2(int olddfd, cons
 	    (flags & RENAME_EXCHANGE))
 		return -EINVAL;
 
-	if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
-		return -EPERM;
-
 	if (flags & RENAME_EXCHANGE)
 		target_flags = 0;
 
@@ -4485,15 +4484,7 @@ SYSCALL_DEFINE2(rename, const char __use
 
 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
 {
-	int error = may_create(dir, dentry);
-	if (error)
-		return error;
-
-	if (!dir->i_op->mknod)
-		return -EPERM;
-
-	return dir->i_op->mknod(dir, dentry,
-				S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
+	return vfs_mknod(dir, dentry, S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
 }
 EXPORT_SYMBOL(vfs_whiteout);
 
include/linux/device_cgroup.h +3 -0
@@ -51,6 +51,9 @@ static inline int devcgroup_inode_mknod(
 	if (!S_ISBLK(mode) && !S_ISCHR(mode))
 		return 0;
 
+	if (S_ISCHR(mode) && dev == WHITEOUT_DEV)
+		return 0;
+
 	if (S_ISBLK(mode))
 		type = DEVCG_DEV_BLOCK;
 	else
maintainer
#1 lore raw
Quoted 15 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Whiteouts, unlike real device node should not require privileges to create.

The general concern with device nodes is that opening them can have side effects. The kernel already avoids zero major (see Documentation/admin-guide/devices.txt). To be on the safe side the patch explicitly forbids registering a char device with 0/0 number (see cdev_add()).

This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV; i.e. it won't have any side effect.

int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) {

fs/namei.c EXPORT_SYMBOL(user_path_create); · line 3516

Quoted 10 lines
fs/namei.c +3 -1
+	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
 	int error = may_create(dir, dentry);
 
 	if (error)
 		return error;
 
-	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+	    !is_whiteout)
 		return -EPERM;

Hmm... That exposes vfs_whiteout() to LSM; are you sure that you won't end up with regressions for overlayfs on sufficiently weird setups?

#2 lore raw
Quoted 22 lines · Al Viro
Quoted 21 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Whiteouts, unlike real device node should not require privileges to create.

The general concern with device nodes is that opening them can have side effects. The kernel already avoids zero major (see Documentation/admin-guide/devices.txt). To be on the safe side the patch explicitly forbids registering a char device with 0/0 number (see cdev_add()).

This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV; i.e. it won't have any side effect.

int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) {

(quoted diff) +1 -0
+	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
 	int error = may_create(dir, dentry);
 
 	if (error)
 		return error;
Quoted 7 lines

Review

Quoted 4 lines
(quoted diff) +2 -1
-	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+	    !is_whiteout)
 		return -EPERM;

Hmm... That exposes vfs_whiteout() to LSM; are you sure that you won't end up with regressions for overlayfs on sufficiently weird setups?

You're right. OTOH, what can we do? We can't fix the weird setups, only the distros/admins can.

Can we just try this, and revert to calling ->mknod directly from overlayfs if it turns out to be a problem that people can't fix easily?

I guess we could add a new ->whiteout security hook as well, but I'm not sure it's worth it. Cc: LMS mailing list; patch re-added for context.

Thanks,
Miklos

---
 fs/char_dev.c                 |    3 +++
 fs/namei.c                    |   17 ++++-------------
 include/linux/device_cgroup.h |    3 +++
 3 files changed, 10 insertions(+), 13 deletions(-)
fs/char_dev.c +3 -0
@@ -483,6 +483,9 @@ int cdev_add(struct cdev *p, dev_t dev,
 	p->dev = dev;
 	p->count = count;
 
+	if (WARN_ON(dev == WHITEOUT_DEV))
+		return -EBUSY;
+
 	error = kobj_map(cdev_map, dev, count, NULL,
 			 exact_match, exact_lock, p);
 	if (error)
fs/namei.c +4 -13
@@ -3505,12 +3505,14 @@ EXPORT_SYMBOL(user_path_create);
 
 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
 {
+	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
 	int error = may_create(dir, dentry);
 
 	if (error)
 		return error;
 
-	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+	    !is_whiteout)
 		return -EPERM;
 
 	if (!dir->i_op->mknod)
@@ -4345,9 +4347,6 @@ static int do_renameat2(int olddfd, cons
 	    (flags & RENAME_EXCHANGE))
 		return -EINVAL;
 
-	if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
-		return -EPERM;
-
 	if (flags & RENAME_EXCHANGE)
 		target_flags = 0;
 
@@ -4485,15 +4484,7 @@ SYSCALL_DEFINE2(rename, const char __use
 
 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
 {
-	int error = may_create(dir, dentry);
-	if (error)
-		return error;
-
-	if (!dir->i_op->mknod)
-		return -EPERM;
-
-	return dir->i_op->mknod(dir, dentry,
-				S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
+	return vfs_mknod(dir, dentry, S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
 }
 EXPORT_SYMBOL(vfs_whiteout);
 
include/linux/device_cgroup.h +3 -0
@@ -51,6 +51,9 @@ static inline int devcgroup_inode_mknod(
 	if (!S_ISBLK(mode) && !S_ISCHR(mode))
 		return 0;
 
+	if (S_ISCHR(mode) && dev == WHITEOUT_DEV)
+		return 0;
+
 	if (S_ISBLK(mode))
 		type = DEVCG_DEV_BLOCK;
 	else
#3 lore raw
Quoted 66 lines · Miklos Szeredi
Quoted 22 lines · Al Viro
Quoted 21 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Whiteouts, unlike real device node should not require privileges to create.

The general concern with device nodes is that opening them can have side effects. The kernel already avoids zero major (see Documentation/admin-guide/devices.txt). To be on the safe side the patch explicitly forbids registering a char device with 0/0 number (see cdev_add()).

This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV; i.e. it won't have any side effect.

int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) {

+   bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
    int error = may_create(dir, dentry);

    if (error)
            return error;
Quoted 7 lines

fs/namei.c EXPORT_SYMBOL(user_path_create); · line 3516

Quoted 4 lines
fs/namei.c +2 -1
-   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+       !is_whiteout)
            return -EPERM;

Hmm... That exposes vfs_whiteout() to LSM; are you sure that you won't end up with regressions for overlayfs on sufficiently weird setups?

You're right. OTOH, what can we do? We can't fix the weird setups, only the distros/admins can.

Can we just try this, and revert to calling ->mknod directly from overlayfs if it turns out to be a problem that people can't fix easily?

I guess we could add a new ->whiteout security hook as well, but I'm not sure it's worth it. Cc: LMS mailing list; patch re-added for context.

Thanks,
Miklos

---
 fs/char_dev.c                 |    3 +++
 fs/namei.c                    |   17 ++++-------------
 include/linux/device_cgroup.h |    3 +++
 3 files changed, 10 insertions(+), 13 deletions(-)
fs/char_dev.c +3 -0
@@ -483,6 +483,9 @@ int cdev_add(struct cdev *p, dev_t dev,
        p->dev = dev;
        p->count = count;
 
+       if (WARN_ON(dev == WHITEOUT_DEV))
+               return -EBUSY;
+
        error = kobj_map(cdev_map, dev, count, NULL,
                         exact_match, exact_lock, p);
        if (error)
fs/namei.c +0 -0
@@ -3505,12 +3505,14 @@ EXPORT_SYMBOL(user_path_create);
 
 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)

fs/namei.c EXPORT_SYMBOL(user_path_create); · line 3515

Quoted 10 lines
fs/namei.c +3 -1
 {
+       bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
        int error = may_create(dir, dentry);
 
        if (error)
                return error;
 
-       if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+       if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+           !is_whiteout)

Sorry for sidetracking, but !capable(CAP_MKNOD) needs to be last in the chain, otherwise you could get a bogus audit report of CAP_MKNOD being denied in case is_whiteout is true.

Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
#4 lore raw
Quoted 15 lines · Ondrej Mosnacek
Quoted 12 lines · Miklos Szeredi
fs/namei.c +1 -0
@@ -3505,12 +3505,14 @@ EXPORT_SYMBOL(user_path_create);
 
 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
 {
+       bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
        int error = may_create(dir, dentry);
 
        if (error)
                return error;
 
Quoted 7 lines

fs/namei.c EXPORT_SYMBOL(user_path_create); · line 3515

Quoted 3 lines
fs/namei.c +2 -1
-       if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+       if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+           !is_whiteout)

Sorry for sidetracking, but !capable(CAP_MKNOD) needs to be last in the chain, otherwise you could get a bogus audit report of CAP_MKNOD being denied in case is_whiteout is true.

Thanks, fixed in the latest revision.

Miklos

#5 lore raw
In reply to Miklos Szeredi
Quoted 32 lines · Miklos Szeredi
Quoted 22 lines · Al Viro
Quoted 21 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Whiteouts, unlike real device node should not require privileges to create.

The general concern with device nodes is that opening them can have side effects. The kernel already avoids zero major (see Documentation/admin-guide/devices.txt). To be on the safe side the patch explicitly forbids registering a char device with 0/0 number (see cdev_add()).

This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV; i.e. it won't have any side effect.

int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) {

+   bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
    int error = may_create(dir, dentry);

    if (error)
            return error;
Quoted 7 lines

fs/namei.c EXPORT_SYMBOL(user_path_create); · line 3516

Quoted 4 lines
fs/namei.c +2 -1
-   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+       !is_whiteout)
            return -EPERM;

Hmm... That exposes vfs_whiteout() to LSM; are you sure that you won't end up with regressions for overlayfs on sufficiently weird setups?

Quoted 8 lines

You're right. OTOH, what can we do? We can't fix the weird setups, only the distros/admins can.

Can we just try this, and revert to calling ->mknod directly from overlayfs if it turns out to be a problem that people can't fix easily?

I guess we could add a new ->whiteout security hook as well, but I'm not sure it's worth it. Cc: LMS mailing list; patch re-added for context.

I feel like I am still missing context but IIUC this change is allowing unprivileged userspace to explicitly call mknod(2) with the whiteout device number and skip all permission checks (except the LSM one). And then you are switching vfs_whiteout() over to using vfs_mknod() internally since it no longer does permission checking and that was why vfs_whiteout() was separate originally to avoid imposing any checks on overlayfs-internal creation of whiteouts?

If that's correct, then it seems problematic since we have no way in the LSM hook to distinguish the two cases (userspace invocation of mknod(2) versus overlayfs-internal operation). Don't know offhand what credential is in effect in the overlayfs case (mounter or current) but regardless Android seems to use current regardless, and that could easily fail.

#6 lore raw
Quoted 51 lines · Stephen Smalley
Quoted 32 lines · Miklos Szeredi
Quoted 22 lines · Al Viro
Quoted 21 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Whiteouts, unlike real device node should not require privileges to create.

The general concern with device nodes is that opening them can have side effects. The kernel already avoids zero major (see Documentation/admin-guide/devices.txt). To be on the safe side the patch explicitly forbids registering a char device with 0/0 number (see cdev_add()).

This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV; i.e. it won't have any side effect.

int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) {

+   bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
    int error = may_create(dir, dentry);

    if (error)
            return error;
Quoted 7 lines

fs/namei.c EXPORT_SYMBOL(user_path_create); · line 3516

Quoted 4 lines
fs/namei.c +2 -1
-   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+       !is_whiteout)
            return -EPERM;

Hmm... That exposes vfs_whiteout() to LSM; are you sure that you won't end up with regressions for overlayfs on sufficiently weird setups?

Quoted 8 lines

You're right. OTOH, what can we do? We can't fix the weird setups, only the distros/admins can.

Can we just try this, and revert to calling ->mknod directly from overlayfs if it turns out to be a problem that people can't fix easily?

I guess we could add a new ->whiteout security hook as well, but I'm not sure it's worth it. Cc: LMS mailing list; patch re-added for context.

I feel like I am still missing context but IIUC this change is allowing unprivileged userspace to explicitly call mknod(2) with the whiteout device number and skip all permission checks (except the LSM one). And then you are switching vfs_whiteout() over to using vfs_mknod() internally since it no longer does permission checking and that was why vfs_whiteout() was separate originally to avoid imposing any checks on overlayfs-internal creation of whiteouts?

Quoted 6 lines

If that's correct, then it seems problematic since we have no way in the LSM hook to distinguish the two cases (userspace invocation of mknod(2) versus overlayfs-internal operation). Don't know offhand what credential is in effect in the overlayfs case (mounter or current) but regardless Android seems to use current regardless, and that could easily fail.

The major point is: whiteouts are *not* device files, not in the real sense, it just happens that whiteouts are represented by the file having a char/0/0 type.

Also the fact that overlayfs invocation is indistinguishable from userspace invocation is very much on purpose. Whiteout creation was the exception before this change, not the rule.

If you consider the above, how should this be handled from an LSM perspective?

Thanks,
Miklos

#7 lore raw
Quoted 65 lines · Miklos Szeredi
Quoted 51 lines · Stephen Smalley
Quoted 32 lines · Miklos Szeredi
Quoted 22 lines · Al Viro
Quoted 21 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Whiteouts, unlike real device node should not require privileges to create.

The general concern with device nodes is that opening them can have side effects. The kernel already avoids zero major (see Documentation/admin-guide/devices.txt). To be on the safe side the patch explicitly forbids registering a char device with 0/0 number (see cdev_add()).

This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV; i.e. it won't have any side effect.

int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) {

+   bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
    int error = may_create(dir, dentry);

    if (error)
            return error;
Quoted 7 lines

fs/namei.c EXPORT_SYMBOL(user_path_create); · line 3516

Quoted 4 lines
fs/namei.c +2 -1
-   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
+   if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD) &&
+       !is_whiteout)
            return -EPERM;

Hmm... That exposes vfs_whiteout() to LSM; are you sure that you won't end up with regressions for overlayfs on sufficiently weird setups?

Quoted 8 lines

You're right. OTOH, what can we do? We can't fix the weird setups, only the distros/admins can.

Can we just try this, and revert to calling ->mknod directly from overlayfs if it turns out to be a problem that people can't fix easily?

I guess we could add a new ->whiteout security hook as well, but I'm not sure it's worth it. Cc: LMS mailing list; patch re-added for context.

I feel like I am still missing context but IIUC this change is allowing unprivileged userspace to explicitly call mknod(2) with the whiteout device number and skip all permission checks (except the LSM one). And then you are switching vfs_whiteout() over to using vfs_mknod() internally since it no longer does permission checking and that was why vfs_whiteout() was separate originally to avoid imposing any checks on overlayfs-internal creation of whiteouts?

Quoted 6 lines

If that's correct, then it seems problematic since we have no way in the LSM hook to distinguish the two cases (userspace invocation of mknod(2) versus overlayfs-internal operation). Don't know offhand what credential is in effect in the overlayfs case (mounter or current) but regardless Android seems to use current regardless, and that could easily fail.

The major point is: whiteouts are *not* device files, not in the real sense, it just happens that whiteouts are represented by the file having a char/0/0 type.

Quoted 5 lines

Also the fact that overlayfs invocation is indistinguishable from userspace invocation is very much on purpose. Whiteout creation was the exception before this change, not the rule.

If you consider the above, how should this be handled from an LSM perspective?

In that case, I guess you can leave the patch as is aside from moving the capable() check last, and we will just need to allow creation of these files to the mounter context for overlayfs-internal usage. It doesn't appear to be safe to skip the hook call altogether for the general case (e.g. userspace mknod(2)).