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 3/3] selftests/ve: test locking a mount from the host
Date: Mon, 16 Nov 2026 10:12:54 +0400	[thread overview]
Message-ID: <20261116061256.54472-4-mirian.shilakadze@virtuozzo.com> (raw)
In-Reply-To: <20261116061256.54472-1-mirian.shilakadze@virtuozzo.com>

Cover the MOUNT_ATTR_VE_LOCK_* protection locks. A forked task that owns
its user and mount namespace moves in a clone the host locked, the way
vzctl hands a container its bind mounts, and must not be able to clear
a locked protection or its lock, or lock a mount of its own. A bind it
makes keeps the locks. On the host, check that each lock bit locks its
own protection and no other, that a lock stands only where its
protection is set, that a protection lock cannot be dropped, that locks
are refused with AT_RECURSIVE, and that statmount() reports the locks.

Feature: ve: ve generic structures
https://virtuozzo.atlassian.net/browse/VSTOR-155305
Signed-off-by: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
---
 tools/testing/selftests/ve/.gitignore         |   1 +
 tools/testing/selftests/ve/Makefile           |   1 +
 .../testing/selftests/ve/ve_mount_lock_test.c | 274 ++++++++++++++++++
 3 files changed, 276 insertions(+)
 create mode 100644 tools/testing/selftests/ve/ve_mount_lock_test.c

diff --git a/tools/testing/selftests/ve/.gitignore b/tools/testing/selftests/ve/.gitignore
index 3d1179bd2c5f..9b26fb05e342 100644
--- a/tools/testing/selftests/ve/.gitignore
+++ b/tools/testing/selftests/ve/.gitignore
@@ -4,3 +4,4 @@ ve_mount_accounting_test
 ve_cpuid_override_test
 ve_vtty_stress
 ve_numa_migrate_test
+ve_mount_lock_test
diff --git a/tools/testing/selftests/ve/Makefile b/tools/testing/selftests/ve/Makefile
index e96320a18384..57d688381f93 100644
--- a/tools/testing/selftests/ve/Makefile
+++ b/tools/testing/selftests/ve/Makefile
@@ -7,6 +7,7 @@ TEST_GEN_PROGS += ve_perms_test
 TEST_GEN_PROGS += ve_mount_accounting_test
 TEST_GEN_PROGS += ve_vtty_stress
 TEST_GEN_PROGS += ve_numa_migrate_test
+TEST_GEN_PROGS += ve_mount_lock_test
 
 # CPUID faulting is x86-64 only. ARCH says "x86" for 32-bit too and lib.mk
 # picks the clang target by its emptiness, so decide by the compiler.
diff --git a/tools/testing/selftests/ve/ve_mount_lock_test.c b/tools/testing/selftests/ve/ve_mount_lock_test.c
new file mode 100644
index 000000000000..b113aebe617d
--- /dev/null
+++ b/tools/testing/selftests/ve/ve_mount_lock_test.c
@@ -0,0 +1,274 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ve_mount_lock selftests
+ *
+ * The host locks a mount with the MOUNT_ATTR_VE_LOCK_* attributes before it
+ * hands the mount over. A task that owns its user and mount namespace, as a
+ * container does, moves the mount in and must not be able to undo the locks.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <linux/mount.h>
+
+#include "../kselftest_harness.h"
+#include "ve_selftest.h"
+
+#ifndef MOUNT_ATTR_VE_LOCK_RDONLY
+#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)
+#endif
+
+#define SRC	"/tmp/src"
+#define DST	"/tmp/dst"
+#define DST2	"/tmp/dst2"
+
+#define PROTECTIONS (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | \
+		     MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC)
+
+static int setattr(int dfd, const char *path, unsigned int flags,
+		   __u64 set, __u64 clr)
+{
+	struct mount_attr attr = { .attr_set = set, .attr_clr = clr };
+
+	return syscall(__NR_mount_setattr, dfd, path, flags, &attr,
+		       sizeof(attr));
+}
+
+static int setattr_path(const char *path, __u64 set, __u64 clr)
+{
+	return setattr(AT_FDCWD, path, 0, set, clr);
+}
+
+static int move(int fd, const char *dst)
+{
+	return syscall(__NR_move_mount, fd, "", AT_FDCWD, dst,
+		       MOVE_MOUNT_F_EMPTY_PATH);
+}
+
+/* A detached clone of @path carrying @set, as the host hands a mount over. */
+static int handover(const char *path, __u64 set)
+{
+	int fd = syscall(__NR_open_tree, AT_FDCWD, path, OPEN_TREE_CLONE);
+
+	if (fd >= 0 && setattr(fd, "", AT_EMPTY_PATH, set, 0)) {
+		close(fd);
+		return -1;
+	}
+	return fd;
+}
+
+static int mnt_attr(const char *path, __u64 *attr)
+{
+	struct mnt_id_req req = {
+		.size = MNT_ID_REQ_SIZE_VER0,
+		.param = STATMOUNT_MNT_BASIC,
+	};
+	struct statmount sm;
+	struct statx sx;
+
+	if (statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx))
+		return -1;
+	req.mnt_id = sx.stx_mnt_id;
+	if (syscall(__NR_statmount, &req, &sm, sizeof(sm), 0))
+		return -1;
+	*attr = sm.mnt_attr;
+	return 0;
+}
+
+/* Run @fn as container root once handover(SRC, @set) is moved onto DST. */
+static void in_ct(struct __test_metadata *_metadata,
+		  void (*fn)(struct __test_metadata *), __u64 set)
+{
+	int status;
+	pid_t pid;
+
+	pid = fork();
+	ASSERT_LE(0, pid);
+	if (pid == 0) {
+		int fd = handover(SRC, set);
+
+		if (fd < 0 || unshare(CLONE_NEWUSER | CLONE_NEWNS) ||
+		    write_file_at(AT_FDCWD, "/proc/self/setgroups", "deny") ||
+		    write_file_at(AT_FDCWD, "/proc/self/uid_map", "0 0 1") ||
+		    write_file_at(AT_FDCWD, "/proc/self/gid_map", "0 0 1") ||
+		    move(fd, DST)) {
+			perror("container setup");
+			_exit(1);
+		}
+		fn(_metadata);
+		_exit(_metadata->exit_code);
+	}
+	ASSERT_EQ(pid, waitpid(pid, &status, 0));
+	ASSERT_EQ(0, status);
+}
+
+FIXTURE(lock) {};
+
+FIXTURE_SETUP(lock)
+{
+	ASSERT_EQ(0, unshare(CLONE_NEWNS));
+	ASSERT_EQ(0, mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL));
+	ASSERT_EQ(0, mount("tmpfs", "/tmp", "tmpfs", 0, NULL));
+	ASSERT_EQ(0, mkdir(SRC, 0755));
+	ASSERT_EQ(0, mkdir(DST, 0755));
+	ASSERT_EQ(0, mkdir(DST2, 0755));
+}
+
+FIXTURE_TEARDOWN(lock)
+{
+}
+
+static void relax_unlocked(struct __test_metadata *_metadata)
+{
+	__u64 attr;
+
+	ASSERT_EQ(0, mount(NULL, DST, NULL, MS_REMOUNT | MS_BIND, NULL));
+	ASSERT_EQ(0, mnt_attr(DST, &attr));
+	EXPECT_EQ(0ULL, attr & MOUNT_ATTR_RDONLY);
+}
+
+/* Without a lock the container takes a read-only mount writable. */
+TEST_F(lock, unlocked_mount_is_relaxed)
+{
+	in_ct(_metadata, relax_unlocked, MOUNT_ATTR_RDONLY);
+}
+
+static void relax_locked(struct __test_metadata *_metadata)
+{
+	static const struct { unsigned long ms; __u64 attr; } prot[] = {
+		{ MS_RDONLY, MOUNT_ATTR_RDONLY },
+		{ MS_NOSUID, MOUNT_ATTR_NOSUID },
+		{ MS_NODEV, MOUNT_ATTR_NODEV },
+		{ MS_NOEXEC, MOUNT_ATTR_NOEXEC },
+	};
+	unsigned long all = MS_RDONLY | MS_NOSUID | MS_NODEV | MS_NOEXEC;
+	unsigned int i;
+	__u64 attr;
+
+	for (i = 0; i < ARRAY_SIZE(prot); i++) {
+		ASSERT_EQ(-1, mount(NULL, DST, NULL, MS_REMOUNT | MS_BIND |
+				    (all & ~prot[i].ms), NULL));
+		ASSERT_EQ(EPERM, errno);
+		ASSERT_EQ(-1, setattr_path(DST, 0, prot[i].attr));
+		ASSERT_EQ(EPERM, errno);
+	}
+	ASSERT_EQ(-1, setattr_path(DST, 0, MOUNT_ATTR_VE_LOCK_RDONLY));
+	ASSERT_EQ(EPERM, errno);
+	ASSERT_EQ(0, mnt_attr(DST, &attr));
+	EXPECT_EQ(PROTECTIONS, attr & PROTECTIONS);
+	EXPECT_EQ(MOUNT_ATTR_VE_LOCK, attr & MOUNT_ATTR_VE_LOCK);
+	EXPECT_EQ(0, setattr_path(DST, MOUNT_ATTR_NOSYMFOLLOW, 0));
+}
+
+/* No locked protection or lock can be cleared, other attributes can be set. */
+TEST_F(lock, locked_protections_hold)
+{
+	in_ct(_metadata, relax_locked, PROTECTIONS | MOUNT_ATTR_VE_LOCK);
+}
+
+static void lock_own(struct __test_metadata *_metadata)
+{
+	ASSERT_EQ(0, mount("tmpfs", DST2, "tmpfs", MS_RDONLY, NULL));
+	ASSERT_EQ(-1, setattr_path(DST2, MOUNT_ATTR_VE_LOCK, 0));
+	ASSERT_EQ(EPERM, errno);
+}
+
+/* The container cannot lock its own mounts. */
+TEST_F(lock, container_cannot_lock)
+{
+	in_ct(_metadata, lock_own, MOUNT_ATTR_RDONLY | MOUNT_ATTR_VE_LOCK);
+}
+
+static void bind_locked(struct __test_metadata *_metadata)
+{
+	ASSERT_EQ(0, mount(DST, DST2, NULL, MS_BIND, NULL));
+	ASSERT_EQ(-1, mount(NULL, DST2, NULL, MS_REMOUNT | MS_BIND, NULL));
+	ASSERT_EQ(EPERM, errno);
+}
+
+/* A bind keeps the protection locks. */
+TEST_F(lock, bind_keeps_protection_locks)
+{
+	in_ct(_metadata, bind_locked, MOUNT_ATTR_RDONLY | MOUNT_ATTR_VE_LOCK);
+}
+
+/* A protection lock stands only where its protection is set. */
+TEST_F(lock, lock_follows_protection)
+{
+	int fd = handover(SRC, MOUNT_ATTR_RDONLY);
+	__u64 attr;
+
+	ASSERT_GE(fd, 0);
+	ASSERT_EQ(0, move(fd, DST));
+	ASSERT_EQ(0, setattr_path(DST, MOUNT_ATTR_VE_LOCK, 0));
+	ASSERT_EQ(0, mnt_attr(DST, &attr));
+	EXPECT_EQ(MOUNT_ATTR_VE_LOCK_RDONLY, attr & MOUNT_ATTR_VE_LOCK);
+
+	ASSERT_EQ(0, setattr_path(DST, MOUNT_ATTR_NOSUID, 0));
+	EXPECT_EQ(0, setattr_path(DST, 0, MOUNT_ATTR_NOSUID));
+}
+
+/* Each lock bit locks its own protection and no other. */
+TEST_F(lock, one_lock_per_protection)
+{
+	static const struct { __u64 prot; __u64 lock; } pair[] = {
+		{ MOUNT_ATTR_RDONLY, MOUNT_ATTR_VE_LOCK_RDONLY },
+		{ MOUNT_ATTR_NOSUID, MOUNT_ATTR_VE_LOCK_NOSUID },
+		{ MOUNT_ATTR_NODEV, MOUNT_ATTR_VE_LOCK_NODEV },
+		{ MOUNT_ATTR_NOEXEC, MOUNT_ATTR_VE_LOCK_NOEXEC },
+	};
+	unsigned int i;
+	__u64 attr;
+
+	for (i = 0; i < ARRAY_SIZE(pair); i++) {
+		__u64 other = pair[(i + 1) % ARRAY_SIZE(pair)].prot;
+		int fd = handover(SRC, PROTECTIONS | pair[i].lock);
+
+		ASSERT_GE(fd, 0);
+		ASSERT_EQ(0, move(fd, DST));
+		close(fd);
+		ASSERT_EQ(0, mnt_attr(DST, &attr));
+		EXPECT_EQ(pair[i].lock, attr & MOUNT_ATTR_VE_LOCK);
+		ASSERT_EQ(-1, setattr_path(DST, 0, pair[i].prot));
+		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(0, setattr_path(DST, 0, other));
+		ASSERT_EQ(0, umount2(DST, 0));
+	}
+}
+
+/* Not even the host drops a protection lock or clears what it protects. */
+TEST_F(lock, host_cannot_unlock_protection)
+{
+	int fd = handover(SRC, MOUNT_ATTR_RDONLY | MOUNT_ATTR_VE_LOCK);
+
+	ASSERT_GE(fd, 0);
+	ASSERT_EQ(0, move(fd, DST));
+	ASSERT_EQ(-1, setattr_path(DST, 0, MOUNT_ATTR_VE_LOCK_RDONLY));
+	ASSERT_EQ(EINVAL, errno);
+	ASSERT_EQ(-1, setattr_path(DST, 0, MOUNT_ATTR_RDONLY));
+	ASSERT_EQ(EPERM, errno);
+}
+
+/* Locks change on the one mount named, never recursively. */
+TEST_F(lock, not_recursive)
+{
+	ASSERT_EQ(0, mount(SRC, DST, NULL, MS_BIND, NULL));
+	ASSERT_EQ(-1, setattr(AT_FDCWD, DST, AT_RECURSIVE,
+			      MOUNT_ATTR_VE_LOCK, 0));
+	EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_HARNESS_MAIN
-- 
2.43.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  parent 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 ` [Devel] [PATCH vz10 v2 1/3] fs: let the protection flags of a mount be locked from userspace Mirian Shilakadze
2026-11-16  6:24   ` 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 ` Mirian Shilakadze [this message]
2026-11-16  6:38   ` [Devel] [PATCH vz10 v2 3/3] selftests/ve: test locking a mount from the host 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-4-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.