[Devel] [PATCH RFC hci-8.2 2/2] selftests/ve: check a host task entering a pseudosuper VE reaches its init

Konstantin Khorenko khorenko at virtuozzo.com
Fri Nov 20 00:49:03 MSK 2026


vzctl starts a Container with ve.pseudosuper set: the init it clones
writes START to ve.state, then a helper forked on the host enters the
init's namespaces and opens its /proc/<pid>/ns/mnt. While the VE is
pseudosuper its setup tasks are reachable only from each other, so the
helper must be a setup task too.

Start a VE the same way and check that a host task entering it through
the ve namespace reaches the init, and that a binary exec'd inside the
VE still does not. Before the fix the first test fails with EACCES, the
error vzctl start reports.

https://virtuozzo.atlassian.net/browse/VSTOR-156164

Feature: trace: prohibit tracing sensitive tasks from CT
Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
---
 tools/testing/selftests/ve/.gitignore           |   1 +
 tools/testing/selftests/ve/Makefile             |   1 +
 tools/testing/selftests/ve/ve_setup_task_test.c | 284 ++++++++++++++++++++++++
 3 files changed, 286 insertions(+)

diff --git a/tools/testing/selftests/ve/.gitignore b/tools/testing/selftests/ve/.gitignore
index a468ef980e28b..fbaea9c8ee39d 100644
--- a/tools/testing/selftests/ve/.gitignore
+++ b/tools/testing/selftests/ve/.gitignore
@@ -7,3 +7,4 @@ ve_numa_migrate_test
 ve_aio_ring_test
 ve_numa_syscalls_prohibited_test
 ve_tmpfs_mpol_test
+ve_setup_task_test
diff --git a/tools/testing/selftests/ve/Makefile b/tools/testing/selftests/ve/Makefile
index fa4f858227e5e..baf035219da98 100644
--- a/tools/testing/selftests/ve/Makefile
+++ b/tools/testing/selftests/ve/Makefile
@@ -10,6 +10,7 @@ TEST_GEN_PROGS += ve_numa_migrate_test
 TEST_GEN_PROGS += ve_aio_ring_test
 TEST_GEN_PROGS += ve_numa_syscalls_prohibited_test
 TEST_GEN_PROGS += ve_tmpfs_mpol_test
+TEST_GEN_PROGS += ve_setup_task_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_setup_task_test.c b/tools/testing/selftests/ve/ve_setup_task_test.c
new file mode 100644
index 0000000000000..e9f8851e91724
--- /dev/null
+++ b/tools/testing/selftests/ve/ve_setup_task_test.c
@@ -0,0 +1,284 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ve_setup_task selftest
+ *
+ * vzctl starts a Container with ve.pseudosuper set: the init it clones
+ * writes START to ve.state, and only then a helper forked on the host
+ * enters the init's namespaces and opens its /proc/<pid>/ns/mnt. While
+ * the VE is pseudosuper its setup tasks are reachable only from each
+ * other, so the helper must be a setup task too, or the start fails
+ * with EACCES. A container binary, one exec'd inside the VE, must still
+ * not reach the init.
+ *
+ * The tests start a VE the way vzctl does and check that:
+ *
+ *   - a host task entering the running pseudosuper VE reaches the init
+ *   - a binary exec'd inside the VE does not
+ */
+#define _GNU_SOURCE
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/sched.h>
+#include <sys/prctl.h>
+#include <sys/random.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+
+#include "../kselftest_harness.h"
+#include "ve_selftest.h"
+
+/* Re-exec of the test binary: open the mnt namespace of the given pid. */
+#define PROBE_ARG		"--open-mnt-ns"
+
+FIXTURE(ve_setup_task)
+{
+	int cgv2_fd;
+	int ctid;
+	pid_t init_pid;
+	int init_release;		/* its read end holds the init */
+	struct namespace_ctx ns_ctx;	/* the VE namespace of the init */
+	char exe[PATH_MAX];		/* this binary, for the probe exec */
+};
+
+/* 0 when /proc/@pid/ns/mnt opens, else errno. */
+static int open_mnt_ns(pid_t pid)
+{
+	char path[64];
+	int fd;
+
+	snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return errno;
+	close(fd);
+	return 0;
+}
+
+static int ve_write(FIXTURE_DATA(ve_setup_task) * self, const char *file,
+		    const char *val)
+{
+	char path[64];
+
+	snprintf(path, sizeof(path), "%d/%s", self->ctid, file);
+	return write_file_at(self->cgv2_fd, path, val);
+}
+
+static int ve_read(FIXTURE_DATA(ve_setup_task) * self, const char *file,
+		   char *buf, size_t len)
+{
+	char path[64];
+
+	snprintf(path, sizeof(path), "%d/%s", self->ctid, file);
+	return read_file_at(self->cgv2_fd, path, buf, len);
+}
+
+/*
+ * The init of the VE: start it, report the result and stay until the
+ * test closes the release pipe.
+ */
+static void run_init(FIXTURE_DATA(ve_setup_task) * self, int status[2],
+		     int release[2])
+{
+	char c;
+
+	prctl(PR_SET_PDEATHSIG, SIGKILL);
+	close(status[0]);
+	close(release[1]);
+	c = ve_write(self, "ve.state", "START") ? errno : 0;
+	if (write(status[1], &c, 1) != 1)
+		_exit(1);
+	close(status[1]);
+	while (read(release[0], &c, 1) < 0 && errno == EINTR)
+		;
+	_exit(0);
+}
+
+/* Wait for the VE to stop after the init is released, then remove it. */
+static void stop_ve(FIXTURE_DATA(ve_setup_task) * self)
+{
+	char state[32], path[64];
+	int i;
+
+	if (self->init_pid > 0) {
+		close(self->init_release);
+		waitpid(self->init_pid, NULL, 0);
+	}
+	for (i = 0; i < 500; i++) {
+		if (ve_read(self, "ve.state", state, sizeof(state)) < 0 ||
+		    !strncmp(state, "STOPPED", 7) ||
+		    !strncmp(state, "STARTING", 8))
+			break;
+		usleep(10000);
+	}
+	ve_write(self, "ve.pseudosuper", "0");
+	/* The start makes a vz.slice child under the VE root */
+	snprintf(path, sizeof(path), "%d/vz.slice", self->ctid);
+	unlinkat(self->cgv2_fd, path, AT_REMOVEDIR);
+	destroy_ve(self->cgv2_fd, self->ctid);
+}
+
+/*
+ * Start the VE the way vzctl does: pseudosuper first, then an init cloned
+ * from the host writes START. The start needs the init in its own pid,
+ * cgroup and net namespaces. Returns -1 with the failed step in @what.
+ */
+static int start_ve(FIXTURE_DATA(ve_setup_task) * self, const char **what)
+{
+	struct clone_args cargs = {
+		.flags = CLONE_NEWVE | CLONE_NEWPID | CLONE_NEWCGROUP |
+			 CLONE_NEWNS | CLONE_NEWNET,
+		.exit_signal = SIGCHLD,
+	};
+	char state[32], path[64], val[32];
+	int status[2], release[2];
+	unsigned int veid;
+	char c;
+
+	*what = "ve cgroup";
+	self->ctid = make_ve(self->cgv2_fd, CTID_MIN);
+	if (self->ctid < 0)
+		return -1;
+
+	/* A random high veid, as a container gets, so it clashes with none */
+	*what = "ve.veid";
+	if (getrandom(&veid, sizeof(veid), 0) != sizeof(veid))
+		return -1;
+	veid = (veid >> 2) | (1u << 30);
+	snprintf(val, sizeof(val), "%u", veid);
+	if (ve_write(self, "ve.veid", val) < 0)
+		return -1;
+	*what = "ve.pseudosuper";
+	if (ve_write(self, "ve.pseudosuper", "1") < 0)
+		return -1;
+
+	*what = "pipe";
+	if (pipe(status) < 0 || pipe(release) < 0)
+		return -1;
+
+	/* The init: a host task cloned into the VE while it starts */
+	*what = "clone3";
+	if (enter_cgroup(self->cgv2_fd, self->ctid) < 0)
+		return -1;
+	self->init_pid = syscall(__NR_clone3, &cargs, sizeof(cargs));
+	if (self->init_pid == 0)
+		run_init(self, status, release);
+	close(status[1]);
+	close(release[0]);
+	self->init_release = release[1];
+	if (enter_cgroup(self->cgv2_fd, 0) < 0 || self->init_pid < 0)
+		return -1;
+
+	*what = "ve.state START";
+	if (read(status[0], &c, 1) != 1)
+		return -1;
+	close(status[0]);
+	if (c) {
+		errno = c;
+		return -1;
+	}
+	*what = "ve.state is not RUNNING";
+	if (ve_read(self, "ve.state", state, sizeof(state)) <= 0 ||
+	    strncmp(state, "RUNNING", 7))
+		return -1;
+
+	*what = "/proc/<init>/ns/ve";
+	snprintf(path, sizeof(path), "/proc/%d/ns/ve", self->init_pid);
+	self->ns_ctx.ve_fd = open(path, O_RDONLY);
+	if (self->ns_ctx.ve_fd < 0)
+		return -1;
+	self->ns_ctx.ns_save_mask = CLONE_NEWVE;
+	return 0;
+}
+
+FIXTURE_SETUP(ve_setup_task)
+{
+	const char *what;
+	ssize_t len;
+	int ret, err;
+
+	self->ctid = -1;
+	self->init_pid = -1;
+	self->ns_ctx.ns_save_mask = 0;
+
+	len = readlink("/proc/self/exe", self->exe, sizeof(self->exe) - 1);
+	ASSERT_GT(len, 0);
+	self->exe[len] = '\0';
+
+	self->cgv2_fd = mount_cg2_fd();
+	if (self->cgv2_fd < 0)
+		SKIP(return, "cannot mount cgroup2: %s", strerror(errno));
+	if (write_file_at(self->cgv2_fd, "cgroup.subtree_control",
+			  VE_CONTROLLERS) < 0)
+		SKIP(return, "cannot enable the ve controller: %s",
+		     strerror(errno));
+
+	/* The harness skips the teardown when the setup fails */
+	ret = start_ve(self, &what);
+	err = errno;
+	if (ret)
+		stop_ve(self);
+	ASSERT_EQ(ret, 0) TH_LOG("%s: %s", what, strerror(err));
+}
+
+FIXTURE_TEARDOWN(ve_setup_task)
+{
+	if (self->ctid < 0)
+		return;
+	close_namespace_fds(&self->ns_ctx);
+	stop_ve(self);
+	close(self->cgv2_fd);
+}
+
+static int open_init_mnt_ns(void *arg)
+{
+	int err = open_mnt_ns(*(pid_t *)arg);
+
+	if (err)
+		fprintf(stderr, "%s: /proc/<init>/ns/mnt: %s\n", __func__,
+			strerror(err));
+	return err ? -1 : 0;
+}
+
+TEST_F(ve_setup_task, host_task_reaches_init)
+{
+	EXPECT_EQ(run_in_ve(self->cgv2_fd, self->ctid, &self->ns_ctx,
+			    CLONE_NEWVE, open_init_mnt_ns, &self->init_pid), 0);
+}
+
+TEST_F(ve_setup_task, exec_in_ve_does_not_reach_init)
+{
+	char pid[16];
+	int status;
+	pid_t p;
+
+	snprintf(pid, sizeof(pid), "%d", self->init_pid);
+	p = fork();
+	ASSERT_GE(p, 0);
+	if (p == 0) {
+		if (enter_cgroup(self->cgv2_fd, self->ctid) < 0 ||
+		    enter_namespaces(&self->ns_ctx, CLONE_NEWVE) < 0)
+			_exit(254);
+		execl(self->exe, self->exe, PROBE_ARG, pid, NULL);
+		fprintf(stderr, "execl(%s): %s\n", self->exe, strerror(errno));
+		_exit(254);
+	}
+	ASSERT_EQ(waitpid(p, &status, 0), p);
+	ASSERT_TRUE(WIFEXITED(status));
+	ASSERT_NE(WEXITSTATUS(status), 254);
+	EXPECT_EQ(WEXITSTATUS(status), EACCES)
+		TH_LOG("open of the init's mnt namespace: %s",
+		       strerror(WEXITSTATUS(status)));
+}
+
+int main(int argc, char **argv)
+{
+	if (argc == 3 && !strcmp(argv[1], PROBE_ARG))
+		return open_mnt_ns(atoi(argv[2]));
+	return test_harness_run(argc, argv);
+}

-- 
2.43.0



More information about the Devel mailing list