All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
To: khorenko@virtuozzo.com, ptikhomirov@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH vz10 v2 1/3] fs: let the protection flags of a mount be locked from userspace
Date: Mon, 16 Nov 2026 10:12:52 +0400	[thread overview]
Message-ID: <20261116061256.54472-2-mirian.shilakadze@virtuozzo.com> (raw)
In-Reply-To: <20261116061256.54472-1-mirian.shilakadze@virtuozzo.com>

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 <mirian.shilakadze@virtuozzo.com>
---
 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
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  reply	other threads:[~2026-11-16  6:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  6:12 [Devel] [PATCH vz10 v2 0/3] fs: let the host lock the protection flags of a mount Mirian Shilakadze
2026-11-16  6:12 ` Mirian Shilakadze [this message]
2026-11-16  6:24   ` [Devel] [PATCH vz10 v2 1/3] fs: let the protection flags of a mount be locked from userspace no-reply
2026-11-16 17:13   ` Konstantin Khorenko
2026-11-16  6:12 ` [Devel] [PATCH vz10 v2 2/3] fs: report the locks of a mount in statmount() Mirian Shilakadze
2026-11-16  6:17   ` no-reply
2026-11-16 17:13   ` Konstantin Khorenko
2026-11-16  6:12 ` [Devel] [PATCH vz10 v2 3/3] selftests/ve: test locking a mount from the host Mirian Shilakadze
2026-11-16  6:38   ` no-reply
2026-11-16 17:13   ` Konstantin Khorenko
2026-11-16 17:13 ` [Devel] [PATCH vz10 v2 0/3] fs: let the host lock the protection flags of a mount Konstantin Khorenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116061256.54472-2-mirian.shilakadze@virtuozzo.com \
    --to=mirian.shilakadze@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=khorenko@virtuozzo.com \
    --cc=ptikhomirov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.