A container can strip ro, nosuid, nodev or noexec from a bind mount the
host gave it with "mount -o remount,rw,bind" and then write host data.
Nothing raises the MNT_LOCK_* bits that forbid this on such a mount, and
userspace cannot set them.

Give each of them a mount_setattr() attribute, MOUNT_ATTR_VE_LOCK_RDONLY,
_NOSUID, _NODEV and _NOEXEC, with MOUNT_ATTR_VE_LOCK for all four. A
lock stands only where its protection is set and is never dropped,
attr_clr refuses it. Only CAP_SYS_ADMIN in the initial user namespace
may lock, on one mount at a time.

Locks can now be raised on an attached mount, so remount tests them
again under the lock that guards the change.

Feature: ve: ve generic structures
https://virtuozzo.atlassian.net/browse/VSTOR-155305
Signed-off-by: Mirian Shilakadze <[email protected]>
---
 fs/namespace.c             | 58 ++++++++++++++++++++++++++++++++++----
 include/uapi/linux/mount.h | 15 ++++++++++
 2 files changed, 67 insertions(+), 6 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index 75cabf7763ef..fa3c1851b7da 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -100,6 +100,7 @@ struct mount_kattr {
        unsigned int propagation;
        unsigned int lookup_flags;
        bool recurse;
+       bool lock_flags;
        struct user_namespace *mnt_userns;
        struct mnt_idmap *mnt_idmap;
 };
@@ -3018,8 +3019,9 @@ SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, 
filename, unsigned, fl
 /*
  * Don't allow locked mount flags to be cleared.
  *
- * No locks need to be held here while testing the various MNT_LOCK
- * flags because those flags can never be cleared once they are set.
+ * mount_setattr() can lock an attached mount, under the mount hash and
+ * s_umount held for read, so test again under the hash or s_umount held
+ * for write before acting on the result.
  */
 static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
 {
@@ -3124,7 +3126,9 @@ static int do_reconfigure_mnt(struct path *path, unsigned 
int mnt_flags)
         */
        down_read(&sb->s_umount);
        lock_mount_hash();
-       ret = change_mount_ro_state(mnt, mnt_flags);
+       ret = -EPERM;
+       if (can_change_locked_flags(mnt, mnt_flags))
+               ret = change_mount_ro_state(mnt, mnt_flags);
        if (ret == 0)
                set_mount_attributes(mnt, mnt_flags);
        unlock_mount_hash();
@@ -3479,7 +3483,8 @@ static int do_remount(struct path *path, int ms_flags, 
int sb_flags,
        if (!err) {
                down_write(&sb->s_umount);
                err = -EPERM;
-               if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
+               if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN) &&
+                   can_change_locked_flags(mnt, mnt_flags)) {
                        err = reconfigure_super(fc);
                        if (!err) {
                                lock_mount_hash();
@@ -4543,7 +4548,10 @@ SYSCALL_DEFINE5(mount, char __user *, dev_name, char 
__user *, dir_name,
         MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME |       \
         MOUNT_ATTR_NOSYMFOLLOW)
 
-#define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
+#define MOUNT_SETATTR_LOCK_FLAGS MOUNT_ATTR_VE_LOCK
+
+#define MOUNT_SETATTR_VALID_FLAGS \
+       (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP | MOUNT_SETATTR_LOCK_FLAGS)
 
 #define MOUNT_SETATTR_PROPAGATION_FLAGS \
        (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
@@ -4564,6 +4572,14 @@ static unsigned int attr_flags_to_mnt_flags(u64 
attr_flags)
                mnt_flags |= MNT_NODIRATIME;
        if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
                mnt_flags |= MNT_NOSYMFOLLOW;
+       if (attr_flags & MOUNT_ATTR_VE_LOCK_RDONLY)
+               mnt_flags |= MNT_LOCK_READONLY;
+       if (attr_flags & MOUNT_ATTR_VE_LOCK_NOSUID)
+               mnt_flags |= MNT_LOCK_NOSUID;
+       if (attr_flags & MOUNT_ATTR_VE_LOCK_NODEV)
+               mnt_flags |= MNT_LOCK_NODEV;
+       if (attr_flags & MOUNT_ATTR_VE_LOCK_NOEXEC)
+               mnt_flags |= MNT_LOCK_NOEXEC;
 
        return mnt_flags;
 }
@@ -4915,6 +4931,15 @@ static unsigned int recalc_flags(struct mount_kattr 
*kattr, struct mount *mnt)
        flags &= ~kattr->attr_clr;
        /* flags to raise */
        flags |= kattr->attr_set;
+       /* a protection lock only stands where the protection is set */
+       if (!(flags & MNT_READONLY))
+               flags &= ~MNT_LOCK_READONLY;
+       if (!(flags & MNT_NOSUID))
+               flags &= ~MNT_LOCK_NOSUID;
+       if (!(flags & MNT_NODEV))
+               flags &= ~MNT_LOCK_NODEV;
+       if (!(flags & MNT_NOEXEC))
+               flags &= ~MNT_LOCK_NOEXEC;
 
        return flags;
 }
@@ -5083,6 +5108,10 @@ static int do_mount_setattr(struct path *path, struct 
mount_kattr *kattr)
                kattr->mnt_idmap = mnt_idmap;
        }
 
+       /* do_remount() tests the locks and changes the flags under s_umount */
+       if (kattr->lock_flags)
+               down_read(&mnt->mnt.mnt_sb->s_umount);
+
        if (kattr->propagation) {
                /*
                 * Only take namespace_lock() if we're actually changing
@@ -5093,7 +5122,7 @@ static int do_mount_setattr(struct path *path, struct 
mount_kattr *kattr)
                        err = invent_group_ids(mnt, kattr->recurse);
                        if (err) {
                                namespace_unlock();
-                               return err;
+                               goto out_lock;
                        }
                }
        }
@@ -5137,6 +5166,10 @@ static int do_mount_setattr(struct path *path, struct 
mount_kattr *kattr)
                namespace_unlock();
        }
 
+out_lock:
+       if (kattr->lock_flags)
+               up_read(&mnt->mnt.mnt_sb->s_umount);
+
        return err;
 }
 
@@ -5230,6 +5263,19 @@ static int build_mount_kattr(const struct mount_attr 
*attr, size_t usize,
        if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
                return -EINVAL;
 
+       /*
+        * Only the host may lock, one mount per call. Like the ones
+        * lock_mnt_tree() raises, a protection lock is never dropped.
+        */
+       kattr->lock_flags = (attr->attr_set | attr->attr_clr) &
+                           MOUNT_SETATTR_LOCK_FLAGS;
+       if (kattr->lock_flags && !capable(CAP_SYS_ADMIN))
+               return -EPERM;
+       if (attr->attr_clr & MOUNT_ATTR_VE_LOCK)
+               return -EINVAL;
+       if (kattr->lock_flags && kattr->recurse)
+               return -EINVAL;
+
        kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
        kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
 
diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h
index 225bc366ffcb..8c6a5e643b9c 100644
--- a/include/uapi/linux/mount.h
+++ b/include/uapi/linux/mount.h
@@ -125,6 +125,21 @@ enum fsconfig_command {
 #define MOUNT_ATTR_IDMAP       0x00100000 /* Idmap mount to @userns_fd in 
struct mount_attr. */
 #define MOUNT_ATTR_NOSYMFOLLOW 0x00200000 /* Do not follow symlinks */
 
+/*
+ * Virtuozzo attributes take bits from 63 down, clear of the upstream ones.
+ *
+ * Lock a protection so that whoever gets the mount cannot clear it. Only the
+ * host locks, one mount per mount_setattr() call. A lock holds only where its
+ * protection is set and is never dropped. A bind copies the locks.
+ */
+#define MOUNT_ATTR_VE_LOCK_RDONLY      0x8000000000000000ULL
+#define MOUNT_ATTR_VE_LOCK_NOSUID      0x4000000000000000ULL
+#define MOUNT_ATTR_VE_LOCK_NODEV       0x2000000000000000ULL
+#define MOUNT_ATTR_VE_LOCK_NOEXEC      0x1000000000000000ULL
+#define MOUNT_ATTR_VE_LOCK \
+       (MOUNT_ATTR_VE_LOCK_RDONLY | MOUNT_ATTR_VE_LOCK_NOSUID | \
+        MOUNT_ATTR_VE_LOCK_NODEV | MOUNT_ATTR_VE_LOCK_NOEXEC)
+
 /*
  * mount_setattr()
  */
-- 
2.43.0

_______________________________________________
Devel mailing list
[email protected]
https://lists.openvz.org/mailman/listinfo/devel

Reply via email to