Skip to contentSkip to conversation

user_ns: Add support for unprivileged remount

PATCH In review Zhao Hongjiang 3 participants 8 messages Sep 13, 2012 – Jan 21, 2013 7 messages not in the archive VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 user_ns: Add support for unprivileged remount

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

---
 fs/namespace.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/namespace.c +1 -1
index af238d0..72be266 100644
@@ -1748,7 +1748,7 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
 	struct super_block *sb = path->mnt->mnt_sb;
 	struct mount *mnt = real_mount(path->mnt);
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
 
 	if (!check_mnt(mnt))

-- 1.7.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 5 lines · Zhao Hongjiang

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

Remount in general make filesystem configuration changes not mount level changes.

In general remount is not safe for unprivielged users.

Do you have a use case where you need to remount a filesystem?

Eric

A message that is not in the archive (sent off-list or not yet received): 87fw6lfwc3.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#4 lore raw
Quoted 8 lines · Eric W. Biederman
Quoted 5 lines · Zhao Hongjiang

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

Quoted 6 lines

Remount in general make filesystem configuration changes not mount level changes.

In general remount is not safe for unprivielged users.

Do you have a use case where you need to remount a filesystem?

As we can do a umount+mount,I don't see why remount operation is not allowed. Shouldn't we add checks in remount path in the specific filesystem to ensure safety instead when we enable unprivilleged mount?

Quoted 3 lines

Eric

_______________________________________________
Containers mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 12 lines · Zhao Hongjiang
Quoted 11 lines · Eric W. Biederman
Quoted 5 lines · Zhao Hongjiang

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

Remount in general make filesystem configuration changes not mount level changes.

Quoted 7 lines
Quoted 3 lines

In general remount is not safe for unprivielged users.

Do you have a use case where you need to remount a filesystem?

As we can do a umount+mount,I don't see why remount operation is not allowed. Shouldn't we add checks in remount path in the specific filesystem to ensure safety instead when we enable unprivilleged mount?

But the thing is remount != mount+umount. Remount is change lowlevel filesystem options.

The basic danger is if someone in the primary user namespace mounted a filesystem, and then we cloned that filesystem.

umounting filesystems is ok. There reference count will drop or they will just unmount if the ref count goes to zero.

However mount -o remount -r /home could very easily remount everyone's home directory in all mount namespaces read-only by making the filesystem itself readonly.

That danger applies even to some extent even if the options are safe for us to perform at the filesystem level.

Now that doesn't mean remount is a hopeless operation. What it does mean is that we need to be very carefully with enabling remounting of a filesystem.

Eric

_______________________________________________
Containers mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 877grxdk48.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#8 lore raw
Quoted 35 lines · Eric W. Biederman
Quoted 12 lines · Zhao Hongjiang
Quoted 11 lines · Eric W. Biederman
Quoted 5 lines · Zhao Hongjiang

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

Remount in general make filesystem configuration changes not mount level changes.

Quoted 7 lines
Quoted 3 lines

In general remount is not safe for unprivielged users.

Do you have a use case where you need to remount a filesystem?

As we can do a umount+mount,I don't see why remount operation is not allowed. Shouldn't we add checks in remount path in the specific filesystem to ensure safety instead when we enable unprivilleged mount?

But the thing is remount != mount+umount. Remount is change lowlevel filesystem options.

The basic danger is if someone in the primary user namespace mounted a filesystem, and then we cloned that filesystem.

umounting filesystems is ok. There reference count will drop or they will just unmount if the ref count goes to zero.

However mount -o remount -r /home could very easily remount everyone's home directory in all mount namespaces read-only by making the filesystem itself readonly.

Quoted 7 lines

That danger applies even to some extent even if the options are safe for us to perform at the filesystem level.

Now that doesn't mean remount is a hopeless operation. What it does mean is that we need to be very carefully with enabling remounting of a filesystem.

Hi Eric

what's you idea about the patch below.
Maybe it better to add a new fs_flags FS_USERNS_REMOUNT? It's not a good experience that remount is disabled in container.

Thanks!

From 8c5a01c007d72c748018665d3bd27cd2bde52c0f Mon Sep 17 00:00:00 2001
From: Gao feng <[email protected]>
Date: Thu, 17 Jan 2013 14:41:00 +0800
Subject: [PATCH] userns: allow remount filesystem in un-init userns

The proc and sysfs filesystem already enable userns support, remounting these filesystems in un-init userns do no harm to the host.

---
 fs/namespace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/namespace.c +4 -1
index 55605c5..b9d83fb 100644
@@ -1748,7 +1748,10 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
 	struct super_block *sb = path->mnt->mnt_sb;
 	struct mount *mnt = real_mount(path->mnt);
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (sb->s_type->fs_flags & FS_USERNS_MOUNT) {
+		if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
+			return -EPERM;
+	} else if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
 	if (!check_mnt(mnt))
Signature · 1 line
1.7.11.7

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw
Quoted 43 lines · Gao feng
Quoted 35 lines · Eric W. Biederman
Quoted 12 lines · Zhao Hongjiang
Quoted 11 lines · Eric W. Biederman
Quoted 5 lines · Zhao Hongjiang

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

Remount in general make filesystem configuration changes not mount level changes.

Quoted 7 lines
Quoted 3 lines

In general remount is not safe for unprivielged users.

Do you have a use case where you need to remount a filesystem?

As we can do a umount+mount,I don't see why remount operation is not allowed. Shouldn't we add checks in remount path in the specific filesystem to ensure safety instead when we enable unprivilleged mount?

But the thing is remount != mount+umount. Remount is change lowlevel filesystem options.

The basic danger is if someone in the primary user namespace mounted a filesystem, and then we cloned that filesystem.

umounting filesystems is ok. There reference count will drop or they will just unmount if the ref count goes to zero.

However mount -o remount -r /home could very easily remount everyone's home directory in all mount namespaces read-only by making the filesystem itself readonly.

Quoted 7 lines

That danger applies even to some extent even if the options are safe for us to perform at the filesystem level.

Now that doesn't mean remount is a hopeless operation. What it does mean is that we need to be very carefully with enabling remounting of a filesystem.

Quoted 5 lines

Hi Eric

what's you idea about the patch below.
Maybe it better to add a new fs_flags FS_USERNS_REMOUNT? It's not a good experience that remount is disabled in container.

I think you totally did not read and understand what I said above, and your patch description is wrong.

Remounting proc is not safe, making the patch below is broken. Read proc and read my comments above.

Thinking that this might be an issue that can be fixed with fs_flags is thinking about this wrong. Thinking a remount in any way would be isolated to a single mount namespaces is wrong. When implementing remount you have to think about everyone who has access to that filesystem, and is is legitimate for you to affect all of them.

What are people doing with remounting? Why do we want it? As a practical matter, assume that it is impossible to resue the current filesystem level implementation of remount.

As I said above I don't even think mount -o remount -r /some/filesystem is safe. Think what would happen if you run the shell script below with /tmp being a separate tmpfs mount point and /proc being procfs.

---
#!/bin/sh
export IFIFO=/tmp/pidns-test-$$-in
export OFIFO=/tmp/pidns-test-$$-out
rm -f $IFIFO $OFIFO
mkfifo $IFIFO
mkfifo $OFIFO
unshare --user -- /bin/bash -s <<'EOF' &
echo waiting-for-uid-and-gid-maps > $OFIFO
read LINE < $IFIFO
exec unshare --mount -- /bin/bash -s <<'EOF2'

mount -n -o remount -ro /tmp
mount -n -o remount /proc hidpide=2

EOF2
EOF
child=$!
read LINE < $OFIFO
uid=$(id --user)
gid=$(id --group)
echo "0 $uid 1" > /proc/$child/uid_map
echo "0 $gid 1" > /proc/$child/gid_map
echo uid-and-gid-maps > $IFIFO
wait $child
---

Quoted 22 lines

Thanks!

From 8c5a01c007d72c748018665d3bd27cd2bde52c0f Mon Sep 17 00:00:00 2001
From: Gao feng <[email protected]>
Date: Thu, 17 Jan 2013 14:41:00 +0800
Subject: [PATCH] userns: allow remount filesystem in un-init userns

The proc and sysfs filesystem already enable userns support, remounting these filesystems in un-init userns do no harm to the host.

---
 fs/namespace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/namespace.c +0 -0
index 55605c5..b9d83fb 100644
@@ -1748,7 +1748,10 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
 	struct super_block *sb = path->mnt->mnt_sb;

fs/namespace.c static int do_remount(struct path *path, int flags, int mnt_flags, · line 1757

Quoted 10 lines
fs/namespace.c +4 -1
 	struct mount *mnt = real_mount(path->mnt);
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (sb->s_type->fs_flags & FS_USERNS_MOUNT) {
+		if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
+			return -EPERM;
+	} else if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
 	if (!check_mnt(mnt))

_______________________________________________
Containers mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 87vcawf7jr.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#12 lore raw
Quoted 63 lines · Eric W. Biederman
Quoted 43 lines · Gao feng
Quoted 35 lines · Eric W. Biederman
Quoted 12 lines · Zhao Hongjiang
Quoted 11 lines · Eric W. Biederman
Quoted 5 lines · Zhao Hongjiang

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

Remount in general make filesystem configuration changes not mount level changes.

Quoted 7 lines
Quoted 3 lines

In general remount is not safe for unprivielged users.

Do you have a use case where you need to remount a filesystem?

As we can do a umount+mount,I don't see why remount operation is not allowed. Shouldn't we add checks in remount path in the specific filesystem to ensure safety instead when we enable unprivilleged mount?

But the thing is remount != mount+umount. Remount is change lowlevel filesystem options.

The basic danger is if someone in the primary user namespace mounted a filesystem, and then we cloned that filesystem.

umounting filesystems is ok. There reference count will drop or they will just unmount if the ref count goes to zero.

However mount -o remount -r /home could very easily remount everyone's home directory in all mount namespaces read-only by making the filesystem itself readonly.

Quoted 7 lines

That danger applies even to some extent even if the options are safe for us to perform at the filesystem level.

Now that doesn't mean remount is a hopeless operation. What it does mean is that we need to be very carefully with enabling remounting of a filesystem.

Quoted 5 lines

Hi Eric

what's you idea about the patch below.
Maybe it better to add a new fs_flags FS_USERNS_REMOUNT? It's not a good experience that remount is disabled in container.

I think you totally did not read and understand what I said above, and your patch description is wrong.

Remounting proc is not safe, making the patch below is broken. Read proc and read my comments above.

Thinking that this might be an issue that can be fixed with fs_flags is thinking about this wrong. Thinking a remount in any way would be isolated to a single mount namespaces is wrong. When implementing remount you have to think about everyone who has access to that filesystem, and is is legitimate for you to affect all of them.

Quoted 8 lines

What are people doing with remounting? Why do we want it? As a practical matter, assume that it is impossible to resue the current filesystem level implementation of remount.

As I said above I don't even think mount -o remount -r /some/filesystem is safe. Think what would happen if you run the shell script below with /tmp being a separate tmpfs mount point and /proc being procfs.

Thanks for you explanation,You mean the super-block may be shared within two mntns,And allowing owner of the mntns which maybe unprivileged user to remount this filesystem will affect the other mntns.
I'm right?

Such as proc filesystem,the root user of mntns may umount and then mount it again.So the super block will not be shared with other,In this situation, we can allow this root user to remount the proc filesystem.

I think we may need a flag such as MS_SHARED_NS to point out if this super block is shared by may mntns.if the super block is only owned by one mntns, we can allow the root user of this mntns to remount the filesystem.

Thanks.

Quoted 51 lines

---
#!/bin/sh
export IFIFO=/tmp/pidns-test-$$-in
export OFIFO=/tmp/pidns-test-$$-out
rm -f $IFIFO $OFIFO
mkfifo $IFIFO
mkfifo $OFIFO
unshare --user -- /bin/bash -s <<'EOF' &
echo waiting-for-uid-and-gid-maps > $OFIFO
read LINE < $IFIFO
exec unshare --mount -- /bin/bash -s <<'EOF2'

mount -n -o remount -ro /tmp
mount -n -o remount /proc hidpide=2

EOF2
EOF
child=$!
read LINE < $OFIFO
uid=$(id --user)
gid=$(id --group)
echo "0 $uid 1" > /proc/$child/uid_map
echo "0 $gid 1" > /proc/$child/gid_map
echo uid-and-gid-maps > $IFIFO
wait $child
---

Quoted 24 lines

Thanks!

From 8c5a01c007d72c748018665d3bd27cd2bde52c0f Mon Sep 17 00:00:00 2001
From: Gao feng <[email protected]>
Date: Thu, 17 Jan 2013 14:41:00 +0800
Subject: [PATCH] userns: allow remount filesystem in un-init userns

The proc and sysfs filesystem already enable userns support, remounting these filesystems in un-init userns do no harm to the host.

---
 fs/namespace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/namespace.c +0 -0
index 55605c5..b9d83fb 100644
@@ -1748,7 +1748,10 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
 	struct super_block *sb = path->mnt->mnt_sb;
 	struct mount *mnt = real_mount(path->mnt);
 
Quoted 9 lines

fs/namespace.c static int do_remount(struct path *path, int flags, int mnt_flags, · line 1757

Quoted 8 lines
fs/namespace.c +4 -1
-	if (!capable(CAP_SYS_ADMIN))
+	if (sb->s_type->fs_flags & FS_USERNS_MOUNT) {
+		if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
+			return -EPERM;
+	} else if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
 	if (!check_mnt(mnt))

_______________________________________________
Containers mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw
Quoted 74 lines · Gao feng
Quoted 63 lines · Eric W. Biederman
Quoted 43 lines · Gao feng
Quoted 35 lines · Eric W. Biederman
Quoted 12 lines · Zhao Hongjiang
Quoted 11 lines · Eric W. Biederman
Quoted 5 lines · Zhao Hongjiang

From: Zhao Hongjiang <[email protected]>

Relax the permission checks to allow unprivileged users that have CAP_SYS_ADMIN permissions in the user namespace referred to by the current mount namespace to be allowed to remount filesystems.

Remount in general make filesystem configuration changes not mount level changes.

Quoted 7 lines
Quoted 3 lines

In general remount is not safe for unprivielged users.

Do you have a use case where you need to remount a filesystem?

As we can do a umount+mount,I don't see why remount operation is not allowed. Shouldn't we add checks in remount path in the specific filesystem to ensure safety instead when we enable unprivilleged mount?

But the thing is remount != mount+umount. Remount is change lowlevel filesystem options.

The basic danger is if someone in the primary user namespace mounted a filesystem, and then we cloned that filesystem.

umounting filesystems is ok. There reference count will drop or they will just unmount if the ref count goes to zero.

However mount -o remount -r /home could very easily remount everyone's home directory in all mount namespaces read-only by making the filesystem itself readonly.

Quoted 7 lines

That danger applies even to some extent even if the options are safe for us to perform at the filesystem level.

Now that doesn't mean remount is a hopeless operation. What it does mean is that we need to be very carefully with enabling remounting of a filesystem.

Quoted 5 lines

Hi Eric

what's you idea about the patch below.
Maybe it better to add a new fs_flags FS_USERNS_REMOUNT? It's not a good experience that remount is disabled in container.

I think you totally did not read and understand what I said above, and your patch description is wrong.

Remounting proc is not safe, making the patch below is broken. Read proc and read my comments above.

Thinking that this might be an issue that can be fixed with fs_flags is thinking about this wrong. Thinking a remount in any way would be isolated to a single mount namespaces is wrong. When implementing remount you have to think about everyone who has access to that filesystem, and is is legitimate for you to affect all of them.

Quoted 8 lines

What are people doing with remounting? Why do we want it? As a practical matter, assume that it is impossible to resue the current filesystem level implementation of remount.

As I said above I don't even think mount -o remount -r /some/filesystem is safe. Think what would happen if you run the shell script below with /tmp being a separate tmpfs mount point and /proc being procfs.

Quoted 4 lines

Thanks for you explanation,You mean the super-block may be shared within two mntns,And allowing owner of the mntns which maybe unprivileged user to remount this filesystem will affect the other mntns.
I'm right?

Yes.

Quoted 3 lines

Such as proc filesystem,the root user of mntns may umount and then mount it again.So the super block will not be shared with other,In this situation, we can allow this root user to remount the proc filesystem.

Given that there is exactly one proc mount per pid namespace I don't think your argument exactly holds up.

Quoted 3 lines

I think we may need a flag such as MS_SHARED_NS to point out if this super block is shared by may mntns.if the super block is only owned by one mntns, we can allow the root user of this mntns to remount the filesystem.

mount namespaces can't own anything and with mount propogation and the fact unsharing the mount namespace copies all mounts it is hard to keep any mount in a single mount namespace.

You can think of user namespace ownership. Especially since user namespaces form a tree, so even filesystems that are the most user namespace friendly should have a user namespace that could be considered their owner.

With those caveats I agree that with the appropriate permissions in the user namespace that owns the underlying object remounts should be fine.

Also remounts with MS_BIND specified should be as safe as mounts and unmounts are.

Eric

_______________________________________________
Containers mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/containers