From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 9DEC580032 for ; Fri, 27 Nov 2026 17:41:13 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6ARHe1YW024334; Fri, 27 Nov 2026 20:40:02 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6ARHe1YW024334 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="RHZi5sjs" Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6ARHdmKT024296 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Fri, 27 Nov 2026 20:39:49 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6ARHdmKT024296 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-c40bbd26f74so659384866b.0 for ; Fri, 27 Nov 2026 09:39:49 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErHO6CUyWxGjqcIPPoYKABqxp99hRuoCeTOsI8j/3EpUE+9TEQRFlXgaV36kB5g3/7BAgBPNyg==@openvz.org X-Gm-Message-State: APuU4Z/tqvvu7SXeU0vPwo1r+nMb7yomNbntU8HxyPOOo774o33in4OH w/GMKR6VJRwMxbwxhLaZ5b5jM8HgiW/LucFoLFW/rxTihVdtLSAp7PyxUvFJGVegQVw0BolFa7l 8Wo0nEXPrNhy2wma9cPc4cLM13+oDiNG0rqb4q8aWe0WVjqeWuqMXrQ== X-Gm-Gg: AZFdTZfsPapHc4OydiORuWmxab1syG5wdrd/M/rxqO5N6bY1x0zmBjYKaMTg4j1pfR1 p/fZvHy23zd4qEUzWoLb8TyhjVgP5BP/tyXwddmY4YeP/UVkHgzNvxArYAPD06olhKI6Z9DkkVZ 99xf0U3fq4p9Un3oaSn/7JN6pkYFZEoNaY021a61WOM8tzRUh7KntOwFUG9gI4XTuA8ZU3uZlUA xxRMXQWDf17+osaZvGIU7zaVqt7ImhGlkYgknKb3nPEjH8JZ1pDAEOMIIBaCHkJ/HFMihQiFxJ4 ndPMYLKGn1MDAkeQat/5Pxui3RnycMu5YyhuUqZxBnZeqFH1TD3NV9onjFH77/6+Z1DYuuOu2Ce OeRRWQCKhnl/V70Rb X-Received: by 2002:a17:907:3d0c:b0:c31:c767:adfb with SMTP id a640c23a62f3a-c513a274fc2mr1964612366b.0.1795801188699; Fri, 27 Nov 2026 09:39:48 -0800 (PST) X-Received: by 2002:a17:907:3d0c:b0:c31:c767:adfb with SMTP id a640c23a62f3a-c513a274fc2mr1964611466b.0.1795801188215; Fri, 27 Nov 2026 09:39:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795801188; cv=none; d=google.com; s=arc-20260327; b=CpGHVd34J/aaCBaA78RVtg4+iCyArKfjfPu570dXODuYRS4FXhyGM7+0ocpQXCs9rf e+uLUwci48nzkaKQNphxbwNduLGTra3KmfB72WP7VP2jqeVX1u7vhVKKU5e+HKy6KrsQ qhhfjtFjIKoaqr6tMSflYQDTAVSEi99blXr4+dvKxJYiXOS74pgacMAO6ui6CZsEhwH4 723GWi9AL3UAd5FU3yGnD7hVig3gXY48oU9HQ+7YQBj0GG5LlcVDWOkK5S8+6XL4996G vSQRjeXTvskyzh7q3DdXYfeNXWN2UU8MDBQzWW/iX6zNEXzCju20/OskVD19r585nLYa FI7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=qm5twUvsg9tXRpQzGtnfaiRFn741BgQ4ktQTR9uiXgo=; fh=xqcbrIOv/j1NC9DwWL7cs9wu5dYsNcrfZRJG6Y55lVA=; b=UW7zjK0XpTCFmr2F5NubHfSXYats1bOHall+GgsLd0CRyZf8b05VbiKFXWjYCYCkMg En5mRLoHPuADAg2mzlRTjP9ODGuwnB5AoBU/acISOg7DUxx0f8ZNYEcxe59tFnAgH/8C n0DJ4M8/h2xyv+hVIw++vmjGfsQMhBRViW2V8QicWuYGXShMFdUL3o+citfRDQClHVaJ 2qSwfHSF10OF5ro81G5jvwSmo3bY7hlZQtS51qGJU2jhQbJOCvNcLl5OdyYP0qFxhzha /3EnyUa7ussazixlpO2Rmv0fEIAqIo8lkBnHcYRR0ZzlgQoX1y+8IjQ0LpctEkRMwQ3z dqtw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=RHZi5sjs; spf=pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=khorenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id a640c23a62f3a-c513ad00a83si1661418566b.9.2026.11.27.09.39.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2026 09:39:48 -0800 (PST) Received-SPF: pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=RHZi5sjs; spf=pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=khorenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Message-Id:Content-Type:MIME-Version:Subject:Date :From; bh=qm5twUvsg9tXRpQzGtnfaiRFn741BgQ4ktQTR9uiXgo=; b=RHZi5sjsPaWI9ZRpoM2 EQZei+4uIoFss5A4JCWt4KOmttd5gOVTsQwVV98pMvy8CudTFcboHCNYoTJIRdSIC1RuwyHxpcMtv ZxT22Ig2rzJB7GSsVHRrO8NtuOnS3RDSY+C/NIB1cub2ZIrF85+ARepEq6yDzTrbOXUGjH3goNhXw /FszpHaOrs8ohBuQvlc7/HmVQwqJe5fssz78csoZeyh2K9O8Ei69q7i+CtbHQoEwALQwV1f9YnpCd y3V9KfwBxd/Pm5Nng0gxVuhZY7vZVF2IpyjcsBJVlme0G6qJ7z8cgu3XOm9w2P2IdewbQw+vB8uK7 Em3jP8Xj8I+ml+g==; Received: from ch-demo-asa.virtuozzo.com ([130.117.225.8] helo=f0.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1xWzv3-00CAsq-0a; Fri, 27 Nov 2026 18:39:43 +0100 From: Konstantin Khorenko Date: Fri, 27 Nov 2026 18:39:45 +0100 MIME-Version: 1.0 Message-Id: <20261127-b4-prep-khorenko-vstor-156164-ve-setup-task-pseudosuper-v5-3-32c4b6342ec2@virtuozzo.com> References: <20261127-b4-prep-khorenko-vstor-156164-ve-setup-task-pseudosuper-v5-0-32c4b6342ec2@virtuozzo.com> In-Reply-To: <20261127-b4-prep-khorenko-vstor-156164-ve-setup-task-pseudosuper-v5-0-32c4b6342ec2@virtuozzo.com> To: Mirian Shilakadze , Pavel Tikhomirov X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11016; i=khorenko@virtuozzo.com; h=from:subject:message-id; bh=uPo8a76mcY6Y3GC9fajWeWrRxFjHBXgqTgmsq6nBR+Y=; b=owEBbQKS/ZANAwAKAVGWCkf5YVwqAcsmYgBrCcBifS2GGt/BVv7bh2wlLoHfgZr5Q5LF3ssEB +ws/m2keKGJAjMEAAEKAB0WIQRWD23IPcXT0GO9CCxRlgpH+WFcKgUCawnAYgAKCRBRlgpH+WFc Kh56D/9sc79fL+QhkXtXK7eVwILSeuuk6M2Bz+IGrGMxKY5YsxOZU/lOmv06qUC12wYwartONRU cXc7ggvHxoKGo89OMppNAGiiQn5skAmXBbvvFjE/8r5d8jq/tjtWnxgm6kAP5qz12GC/PrqFpGF B+B6A1ypPeZh9rcu5+uh9OGV+4OTB52NsBq1OZ3dvyrUPRYAoJQFCHJ1EDweEJVttx1vJ/w4opU J9cSysWP3b+usPbRZLX0YtoomLUv33F/HdDr0HyS/lRw8EBgJcUa/CyfT9IHEVLtf9/HHJfZCvs sImvHo0NmWc9HTt41jmKtrdPWjsadxI8I7xbuhLFJQ+TNmPUE/VApzokWosU0OTo+hHsWBlWN2R FtMuaRYGScjbWvTszhjZ6Ne1xCg3bYLqzHhdHFJVoCLLeq90wAP+u6YnWH48qkALZ4Zc3fPQPDi u0rCtNSrrMdTs9p21uDs5odRjTCBbNBiYmWyMGeNKFfHyZky4KG6yCRzC0xKCBnip7BuvXgcMNF I0gG2cfGfjn49AyHbM8bvmSr0iejeA1sm2L4VCqYsh+J0KM2fIIv07WfwxE+9OrzM+0I0w6YD0F 3trJ7ewBHeIRbk/9bHlDjnLlt1Oz/6H+7cGk0GjE9PxiZVbRkenGVS1KYbgRfHhGt7GENmokOrV DA0km4ADoFvknPw== X-Developer-Key: i=khorenko@virtuozzo.com; a=openpgp; fpr=560F6DC83DC5D3D063BD082C51960A47F9615C2A X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v5 3/3] selftests/ve: check a host task entering a pseudosuper VE reaches its init X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org libvzctl before 10.0.144 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//ns/mnt. While the VE is pseudosuper the init, a setup task, is reachable only from the host's tasks, so the helper must count as one. 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 Reviewed-by: Pavel Tikhomirov Reviewed-by: Mirian Shilakadze Signed-off-by: Konstantin Khorenko --- tools/testing/selftests/ve/.gitignore | 1 + tools/testing/selftests/ve/Makefile | 1 + tools/testing/selftests/ve/ve_setup_task_test.c | 318 ++++++++++++++++++++++++ 3 files changed, 320 insertions(+) diff --git a/tools/testing/selftests/ve/.gitignore b/tools/testing/selftests/ve/.gitignore index 9f0f4832326ef..f53f178190d8a 100644 --- a/tools/testing/selftests/ve/.gitignore +++ b/tools/testing/selftests/ve/.gitignore @@ -9,5 +9,6 @@ ve_numa_syscalls_prohibited_test ve_tmpfs_mpol_test ve_devcg_bpf_test ve_printk_test +ve_setup_task_test test_segf test_trap diff --git a/tools/testing/selftests/ve/Makefile b/tools/testing/selftests/ve/Makefile index 36c068b4bb66c..0064804e289d8 100644 --- a/tools/testing/selftests/ve/Makefile +++ b/tools/testing/selftests/ve/Makefile @@ -12,6 +12,7 @@ TEST_GEN_PROGS += ve_numa_syscalls_prohibited_test TEST_GEN_PROGS += ve_tmpfs_mpol_test TEST_GEN_PROGS += ve_devcg_bpf_test TEST_GEN_PROGS += ve_printk_test +TEST_GEN_PROGS += ve_setup_task_test TEST_GEN_FILES += test_segf test_trap # CPUID faulting is x86-64 only. ARCH says "x86" for 32-bit too and lib.mk 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..66423f9f4a17f --- /dev/null +++ b/tools/testing/selftests/ve/ve_setup_task_test.c @@ -0,0 +1,318 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * ve_setup_task selftest + * + * libvzctl before 10.0.144 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//ns/mnt. While the VE is pseudosuper the init, a setup + * task, is reachable only from the host's tasks, so the helper must + * count as one, 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 that way 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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) { + if (errno != EINTR) + break; + } + _exit(0); +} + +/* Sleep the whole @ms: a signal must not shorten the wait below. */ +static void sleep_ms(long ms) +{ + struct timespec left = { .tv_nsec = ms * 1000000L }; + + while (nanosleep(&left, &left) < 0) { + if (errno != EINTR) + break; + } +} + +/* 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_release >= 0) + close(self->init_release); + if (self->init_pid > 0) + 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; + sleep_ms(10); + } + 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; + int ret; + 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) + return -1; + if (pipe(release) < 0) { + close(status[0]); + close(status[1]); + 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) + 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) { + close(status[0]); + return -1; + } + + *what = "ve.state START"; + ret = read(status[0], &c, 1); + close(status[0]); + if (ret != 1) { + if (ret == 0) + errno = EPIPE; + return -1; + } + if (c) { + errno = c; + return -1; + } + *what = "ve.state is not RUNNING"; + if (ve_read(self, "ve.state", state, sizeof(state)) < 0) + return -1; + if (strncmp(state, "RUNNING", 7)) { + errno = EINVAL; + return -1; + } + + *what = "/proc//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->init_release = -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 = open_cg2_fd(); + if (self->cgv2_fd < 0) + SKIP(return, "cannot open cgroup2: %s", strerror(errno)); + if (write_file_at(self->cgv2_fd, "cgroup.subtree_control", + VE_CONTROLLERS) < 0) { + err = errno; + close(self->cgv2_fd); + SKIP(return, "cannot enable the ve controller: %s", + strerror(err)); + } + + /* The harness skips the teardown when the setup fails */ + ret = start_ve(self, &what); + err = errno; + if (ret) { + stop_ve(self); + close(self->cgv2_fd); + } + 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//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 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel