All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2 v4 0/3] ve: Container start on the 26.3 kernel with libvzctl before 10.0.144
@ 2026-11-20 16:34 Konstantin Khorenko
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 1/3] ve: trust every host task of a pseudosuper VE until it execs Konstantin Khorenko
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Konstantin Khorenko @ 2026-11-20 16:34 UTC (permalink / raw)
  To: Mirian Shilakadze, Pavel Tikhomirov; +Cc: devel

The VSTOR-151265 series (26.3) hides the setup tasks of a VE from every
task of it but each other while the VE is pseudosuper, and gives the
pseudosuper relaxations to the setup tasks alone. A host task is a
setup task only if it entered while the VE was VE_STATE_STARTING; one
entering a running VE is an entered task, hidden from the container,
with the mount limit as its only relaxation.

libvzctl before 10.0.144 starts a Container like this: pseudosuper=1,
clone the init, the init writes START (the VE is RUNNING from here on),
then a helper joins the init's namespaces one by one through setns(),
opens /proc/<init>/ns/mnt, makes the ploop device nodes and mounts the
second disk inside the container. The helper is an entered task, so the
first step is refused:

  Failed to open /proc/<pid>/ns/mnt: Permission denied
  Failed to configure disk
  Cancel init execution

Every Container start fails with libvzctl before 10.0.144, as in the
hci-8.1.0 builds before 2026-10-22 (10.0.134 in hci-8.1.0-135). libvzctl
10.0.144 (vzctl commit 8bf40747, VSTOR-151174) opens every namespace fd
before it joins the first, while the helper is still a ve0 task, and the
start works. The current hci-8.1.0 builds ship 10.0.159 and hci-8.2
ships 10.1.3 together with the 26.3 kernel, which is why the kernel CI
starts Containers on 26.3.

Patch 1 trusts every task that came from the host and has not exec'd
while the VE is pseudosuper, whichever side of START it entered on.

Patch 2 takes the ve0 case out of ve_current_is_pseudosuper(): asked
about ve0 it answers whether current is a ve0 task, so a caller that
passes ve0 in for a container task grants nothing. The one caller that
does so is the mount reservation, where ve0's INT_MAX limit grants the
mount as before.

Patch 3 is the selftest for the old start sequence: it starts a real VE
(pseudosuper, clone3 into new pid, cgroup, net and mnt namespaces,
START) and checks that a host task joining afterwards reaches the
init's namespaces and an exec'd binary does not.

Verified on a test VM with the Containers there:

  +----------------------------+-------------------+-----------------+
  | kernel                     | libvzctl 10.0.134 | libvzctl 10.1.3 |
  +----------------------------+-------------------+-----------------+
  | 26.3 as is                 | no CT starts      | all start       |
  | 26.3 + patch 1             | all start         | all start       |
  | 26.3 + patches 1 and 2     | all start         | all start       |
  | RHEL 10.3 rebase as is     | no CT starts      | not run         |
  +----------------------------+-------------------+-----------------+

Patch 3 fails on 26.3 as is and passes with patches 1 and 2.

Changes in v4:
- New patch 2 takes the ve0 case out of ve_current_is_pseudosuper():
  asked about ve0 it answers whether current is a ve0 task (Pasha).
  Patch 1 unchanged, Mirian's Reviewed-by kept; the selftest is now
  patch 3.
- Patch 3: the loop holding the init until the test releases it is a
  regular braced loop, no hanging ";" (Pasha).
- Patch 3: the stop poll sleeps its 10 ms in full, nanosleep with the
  remaining time after a signal, so an interrupted sleep no longer
  shortens the wait for ve.state to leave RUNNING (Pasha).
- Link to v3: http://lore.virtuozzo.com/20261120-b4-prep-khorenko-vstor-156164-ve-setup-task-pseudosuper-v3-0-fb0214ce4f45@virtuozzo.com

Changes in v3:
- Rebased onto the current 26.x tip: patch 2 opens the host cgroup2
  mount with open_cg2_fd() as the other ve tests do since 26182b5f9943
  (Mirian).
- Patch 2: the init dying before it reports and ve.state not RUNNING
  after START set errno, so the setup diagnostic no longer shows a
  stale one; the cgroup2 fd is closed when the ve controller cannot be
  enabled (Sashiko).
- Patch 1: Mirian's Reviewed-by, no other change.
- Link to v2: http://lore.virtuozzo.com/20261120-b4-prep-khorenko-vstor-156164-ve-setup-task-pseudosuper-v2-0-c85e827c1dba@virtuozzo.com

Changes in v2:
- Patch 1 replaced. The RFC made a host task entering a pseudosuper VE
  a setup task whatever ve.state says, and such a helper, if it stays
  without exec, becomes an ordinary visible task once pseudosuper is
  cleared. Now, Pasha's variant: ve_current_is_pseudosuper() accepts
  the entered mark too and ve_current_is_host_task() goes. An entered
  task stays hidden after the clear, as before.
- Mirian's one-line alternative, relaxing only the ptrace check, would
  do for the start: the disk setup that follows it runs as an entered
  task with libvzctl 10.1.3 as well, and passes. Not taken because old
  libvzctl runs more in-process helpers inside the VE after START
  (devices, bind mounts, GPUs) that had every relaxation before 26.3,
  and one predicate is easier to reason about than two.
- Patch 1 verified with the old hci-8.1.0 userspace (libvzctl 10.0.134,
  ploop 10.0.47) and with 10.1.3 on the 26.3 kernel: all Containers start.
- No RFC: the two options were discussed, this is the one to apply.
- Patch 2: message and file header reworded for the new rule, the fds
  leaked on the setup failure paths closed (Sashiko).
- Link to v1: http://lore.virtuozzo.com/20261119-b4-prep-khorenko-vstor-156164-ve-setup-task-pseudosuper-v1-0-06fa0b8994a8@virtuozzo.com

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

To: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
To: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>

Assisted-by: LLM
Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
Konstantin Khorenko (3):
      ve: trust every host task of a pseudosuper VE until it execs
      ve: give the pseudosuper relaxations of ve0 to its own tasks only
      selftests/ve: check a host task entering a pseudosuper VE reaches its init

 fs/namespace.c                                  |   4 +-
 include/linux/mm_types.h                        |   4 +-
 include/linux/ve.h                              |   2 -
 kernel/ptrace.c                                 |   2 +-
 kernel/ve/ve.c                                  |  44 +---
 security/device_cgroup.c                        |   2 +-
 tools/testing/selftests/ve/.gitignore           |   1 +
 tools/testing/selftests/ve/Makefile             |   1 +
 tools/testing/selftests/ve/ve_setup_task_test.c | 318 ++++++++++++++++++++++++
 9 files changed, 339 insertions(+), 39 deletions(-)
---
base-commit: fa6cf94f94fa67a00a2ba1cab07ab3a7b93c9593
change-id: 20261119-b4-prep-khorenko-vstor-156164-ve-setup-task-pseudosuper-e11df3a8c1ba

Best regards,
--  
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Devel] [PATCH hci-8.2 v4 1/3] ve: trust every host task of a pseudosuper VE until it execs
  2026-11-20 16:34 [Devel] [PATCH hci-8.2 v4 0/3] ve: Container start on the 26.3 kernel with libvzctl before 10.0.144 Konstantin Khorenko
@ 2026-11-20 16:34 ` Konstantin Khorenko
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 2/3] ve: give the pseudosuper relaxations of ve0 to its own tasks only Konstantin Khorenko
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 3/3] selftests/ve: check a host task entering a pseudosuper VE reaches its init Konstantin Khorenko
  2 siblings, 0 replies; 6+ messages in thread
From: Konstantin Khorenko @ 2026-11-20 16:34 UTC (permalink / raw)
  To: Mirian Shilakadze, Pavel Tikhomirov; +Cc: devel

Since "ve: hide the tasks that set up a starting VE from the rest of
it" a host task entering a VE is marked VD_VE_SETUP_TASK only while the
VE is VE_STATE_STARTING, and VD_VE_ENTER_TASK afterwards. While the VE
is pseudosuper only the setup tasks may reach each other and use the
pseudosuper relaxations; an entered task only skips the mount limit.

libvzctl before 10.0.144 writes START to ve.state, which ends in
VE_STATE_RUNNING, before it configures the disk: a helper then joins
the init task's namespaces one by one through setns(), opens
/proc/<init>/ns/mnt, makes the ploop device nodes and mounts the second
disk inside the container. It joined a running VE, so it is an entered
task, and the init task, a setup task, is hidden from it.

Every container start with such a libvzctl fails:

  Failed to open /proc/<pid>/ns/mnt: Permission denied
  Failed to configure disk
  Cancel init execution

libvzctl 10.0.144 and newer open the namespaces before joining any and
are not affected.

Both kinds of task come from the host and have not exec'd. Before the
series every task of a pseudosuper VE had the relaxations, and libvzctl
runs its in-process helpers for disks, devices, bind mounts and GPUs
inside the VE after START, so trust them alike: ve_current_is_pseudosuper()
accepts either mark while the VE is pseudosuper, and
ve_current_is_host_task() goes. An entered task stays hidden from the
container after pseudosuper is cleared, as before, and a binary the host
tools exec still drops its mark.

Fixes: a7d797d177e7 ("ve: hide the tasks that set up a starting VE from the rest of it")
https://virtuozzo.atlassian.net/browse/VSTOR-156164

Feature: trace: prohibit tracing sensitive tasks from CT
Reviewed-by: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
 fs/namespace.c           |  4 ++--
 include/linux/mm_types.h |  4 ++--
 include/linux/ve.h       |  2 --
 kernel/ptrace.c          |  2 +-
 kernel/ve/ve.c           | 43 ++++++++++++-------------------------------
 security/device_cgroup.c |  2 +-
 6 files changed, 18 insertions(+), 39 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index 0eae165b1299e..db0100203dc29 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -3226,7 +3226,7 @@ int ve_devmnt_process(struct ve_struct *ve, dev_t dev, void **data_pp, int remou
 		if (devmnt->dev == dev) {
 			err = ve_devmnt_check(data, devmnt->allowed_options);
 			/*
-			 * Setup tasks of a pseudosuper VE, ie the restore
+			 * Host tasks of a pseudosuper VE, ie the restore
 			 * procedure, skip the allowed options filtering, since
 			 * restore mode is special.
 			 */
@@ -3367,7 +3367,7 @@ int ve_devmnt_verify(struct ve_struct *ve, dev_t dev, char *opts, bool new_mount
 
 static inline int ve_try_reserve_mount(struct ve_struct *ve)
 {
-	int ret = ve_current_is_host_task(ve) || ve_is_super(get_exec_env());
+	int ret = ve_current_is_pseudosuper(ve) || ve_is_super(get_exec_env());
 	/*
 	 * Ignore limits in ve0 and for the host's tasks of a pseudosuper VE,
 	 * but still count.
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 53cb5ff32a233..388b256ed4af8 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -1116,8 +1116,8 @@ extern struct mm_struct init_mm;
 #define VD_LICDATA_ACCESS	2/* tasks accessed containers license data,
 				  * no ptrace and no coredump allowed */
 #define VD_VE_SETUP_TASK	3/* tasks the host sent into a starting VE,
-				  * reachable only from each other while it
-				  * is pseudosuper, VD_PTRACE_COREDUMP after */
+				  * reachable only from the host's tasks while
+				  * it is pseudosuper, VD_PTRACE_COREDUMP after */
 
 /* Pointer magic because the dynamic array size confuses some compilers. */
 static inline void mm_init_cpumask(struct mm_struct *mm)
diff --git a/include/linux/ve.h b/include/linux/ve.h
index f83d2229d36de..d29339112f250 100644
--- a/include/linux/ve.h
+++ b/include/linux/ve.h
@@ -284,7 +284,6 @@ extern bool is_ve_init_net(const struct net *net);
 void ve_setup_task(struct task_struct *p, struct ve_struct *old_ve,
 		   struct ve_struct *ve);
 bool ve_current_is_pseudosuper(struct ve_struct *ve);
-bool ve_current_is_host_task(struct ve_struct *ve);
 int ve_vps_dumpable(struct mm_struct *mm, struct ve_struct *ve);
 
 void ve_set_rpc_kill_fn(void (*fn)(struct net *, bool));
@@ -419,7 +418,6 @@ static inline int vz_security_protocol_check(struct net *net, int protocol) { re
 static inline void ve_setup_task(struct task_struct *p, struct ve_struct *old_ve,
 				 struct ve_struct *ve) { }
 static inline bool ve_current_is_pseudosuper(struct ve_struct *ve) { return true; }
-static inline bool ve_current_is_host_task(struct ve_struct *ve) { return true; }
 #define ve_vps_dumpable(mm, ve)	((mm)->vps_dumpable)
 
 static inline void ve_set_rpc_kill_fn(void (*fn)(struct net *, bool)) { }
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index ff71c3e37bc14..c2b27e455c8ee 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -285,7 +285,7 @@ static bool task_still_dumpable(struct task_struct *task, unsigned int mode)
 	 * have its vps_dumpable confirmed, so deny it. This closes the window
 	 * where a dying CT task could be inspected (e.g. an fd stolen via
 	 * pidfd_getfd()) after exit_mm() cleared ->mm. While its VE is
-	 * pseudosuper, a setup task is only inspectable by another one.
+	 * pseudosuper, a setup task is only inspectable by a host task.
 	 */
 	if (!ve_is_super(get_exec_env())) {
 		struct ve_struct *ve = task_ve(task);
diff --git a/kernel/ve/ve.c b/kernel/ve/ve.c
index 076892a750cfc..0ad145b38b54b 100644
--- a/kernel/ve/ve.c
+++ b/kernel/ve/ve.c
@@ -1022,7 +1022,7 @@ static u64 ve_pseudosuper_read(struct cgroup_subsys_state *css, struct cftype *c
  * is allowed in a sake of container restore for example.
  *
  * To set this value up one has to be a real node's owner.
- * Inside the VE only its setup tasks may drop it, see
+ * Inside the VE only its host tasks may drop it, see
  * cgroup_file_write().
  */
 static int ve_pseudosuper_write(struct cgroup_subsys_state *css, struct cftype *cft, u64 val)
@@ -1780,44 +1780,25 @@ bool ve_check_trusted_exec(struct file *file, struct filename *name)
 	return false;
 }
 
-/**
- * ve_current_has_mark - is current a task of pseudosuper @ve with @mark
- * @ve: the VE
- * @mark: the vps_dumpable value
- */
-static bool ve_current_has_mark(struct ve_struct *ve, unsigned int mark)
-{
-	struct mm_struct *mm = current->mm;
-
-	return ve->is_pseudosuper && task_ve(current) == ve &&
-	       mm && mm->vps_dumpable == mark;
-}
-
 /**
  * ve_current_is_pseudosuper - may current use the pseudosuper relaxations of @ve
  * @ve: the VE that grants them
  *
- * Always true for ve0. Otherwise only a VD_VE_SETUP_TASK of @ve may, and only
- * until pseudosuper is cleared. Exec clears VD_VE_SETUP_TASK, so the container
- * binaries that the host tools run may not.
+ * Always true for ve0. Otherwise only a host task of @ve may, one the host
+ * sent in while @ve was starting (VD_VE_SETUP_TASK) or after that
+ * (VD_VE_ENTER_TASK), and only until pseudosuper is cleared. Exec clears
+ * both marks, so the container binaries that the host tools run may not.
  */
 bool ve_current_is_pseudosuper(struct ve_struct *ve)
 {
-	return ve_is_super(ve) || ve_current_has_mark(ve, VD_VE_SETUP_TASK);
-}
+	struct mm_struct *mm = current->mm;
 
-/**
- * ve_current_is_host_task - is current a host task of pseudosuper @ve
- * @ve: the VE
- *
- * Always true for ve0. Otherwise true for a setup task of @ve and for a host
- * task that joined @ve after it started, such as the CRIU helper that mounts
- * proc for a nested pid namespace, until pseudosuper is cleared.
- */
-bool ve_current_is_host_task(struct ve_struct *ve)
-{
-	return ve_current_is_pseudosuper(ve) ||
-	       ve_current_has_mark(ve, VD_VE_ENTER_TASK);
+	if (ve_is_super(ve))
+		return true;
+
+	return ve->is_pseudosuper && task_ve(current) == ve && mm &&
+	       (mm->vps_dumpable == VD_VE_SETUP_TASK ||
+		mm->vps_dumpable == VD_VE_ENTER_TASK);
 }
 
 /**
diff --git a/security/device_cgroup.c b/security/device_cgroup.c
index b0ef5f9380d1f..013fa00f15d8a 100644
--- a/security/device_cgroup.c
+++ b/security/device_cgroup.c
@@ -924,7 +924,7 @@ static int devcgroup_legacy_check_permission(short type, u32 major, u32 minor,
 #ifdef CONFIG_VE
 	/*
 	 * When restoring container allow everything to its
-	 * setup tasks in pseudosuper state. We need this for
+	 * host tasks in pseudosuper state. We need this for
 	 * early mounting of second ploop device. Still, don't
 	 * change behaviour on the ve0.
 	 */

-- 
2.43.0

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Devel] [PATCH hci-8.2 v4 2/3] ve: give the pseudosuper relaxations of ve0 to its own tasks only
  2026-11-20 16:34 [Devel] [PATCH hci-8.2 v4 0/3] ve: Container start on the 26.3 kernel with libvzctl before 10.0.144 Konstantin Khorenko
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 1/3] ve: trust every host task of a pseudosuper VE until it execs Konstantin Khorenko
@ 2026-11-20 16:34 ` Konstantin Khorenko
  2026-11-23  9:19   ` Pavel Tikhomirov
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 3/3] selftests/ve: check a host task entering a pseudosuper VE reaches its init Konstantin Khorenko
  2 siblings, 1 reply; 6+ messages in thread
From: Konstantin Khorenko @ 2026-11-20 16:34 UTC (permalink / raw)
  To: Mirian Shilakadze, Pavel Tikhomirov; +Cc: devel

ve_current_is_pseudosuper() answers yes whenever the VE it is asked
about is ve0, whoever asks. The line keeps the behaviour of ve0 being
statically pseudosuper, but it reads as a grant to any task: a
container task would get the relaxations of the host from a caller
that passes it ve0.

Let the ve0 branch ask whether current is a ve0 task instead. The
relaxations of a VE belong to the tasks the host sent into it, and for
ve0 those are its own tasks, no mark involved.

No caller but one sees a different answer:

- devcgroup_legacy_check_permission(), netlink_setsockopt() for
  NETLINK_REPAIR, cgroup_file_write(), ve_hide_cgroups(),
  prctl_set_task_ct_fields(), ve_devmnt_verify() and
  ve_devmnt_process() pass the caller's own VE behind a !ve_is_super()
  check;
- ve_features_write(), ve_failcount_write(),
  ve_pid_max_write_running_u64() and ve_aio_max_nr_write() pass the css
  of a CFTYPE_NOT_ON_ROOT file, which ve0 does not have;
- task_still_dumpable() asks only about a VD_VE_SETUP_TASK of a VE
  other than ve0, ve_vps_dumpable() reports ve0's as
  VD_PTRACE_COREDUMP.

ve_try_reserve_mount() does pass ve0 for a VE task: a host helper that
joined the VE cgroup but still sits in the host's mount namespace owns
its bind mounts through ve0. Its reservation now goes through ve0's
mnt_avail_nr like any counted one; the counter starts at INT_MAX and
has no cgroup file to lower it, so the mount is granted as before.

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

Feature: trace: prohibit tracing sensitive tasks from CT
Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
 kernel/ve/ve.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/kernel/ve/ve.c b/kernel/ve/ve.c
index 0ad145b38b54b..7bbc1106ba389 100644
--- a/kernel/ve/ve.c
+++ b/kernel/ve/ve.c
@@ -1784,17 +1784,18 @@ bool ve_check_trusted_exec(struct file *file, struct filename *name)
  * ve_current_is_pseudosuper - may current use the pseudosuper relaxations of @ve
  * @ve: the VE that grants them
  *
- * Always true for ve0. Otherwise only a host task of @ve may, one the host
- * sent in while @ve was starting (VD_VE_SETUP_TASK) or after that
- * (VD_VE_ENTER_TASK), and only until pseudosuper is cleared. Exec clears
- * both marks, so the container binaries that the host tools run may not.
+ * The relaxations of ve0 belong to its own tasks. Those of a container
+ * belong to a host task of @ve, one the host sent in while @ve was starting
+ * (VD_VE_SETUP_TASK) or after that (VD_VE_ENTER_TASK), and only until
+ * pseudosuper is cleared. Exec clears both marks, so the container binaries
+ * that the host tools run may not.
  */
 bool ve_current_is_pseudosuper(struct ve_struct *ve)
 {
 	struct mm_struct *mm = current->mm;
 
 	if (ve_is_super(ve))
-		return true;
+		return ve_is_super(get_exec_env());
 
 	return ve->is_pseudosuper && task_ve(current) == ve && mm &&
 	       (mm->vps_dumpable == VD_VE_SETUP_TASK ||

-- 
2.43.0

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Devel] [PATCH hci-8.2 v4 3/3] selftests/ve: check a host task entering a pseudosuper VE reaches its init
  2026-11-20 16:34 [Devel] [PATCH hci-8.2 v4 0/3] ve: Container start on the 26.3 kernel with libvzctl before 10.0.144 Konstantin Khorenko
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 1/3] ve: trust every host task of a pseudosuper VE until it execs Konstantin Khorenko
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 2/3] ve: give the pseudosuper relaxations of ve0 to its own tasks only Konstantin Khorenko
@ 2026-11-20 16:34 ` Konstantin Khorenko
  2 siblings, 0 replies; 6+ messages in thread
From: Konstantin Khorenko @ 2026-11-20 16:34 UTC (permalink / raw)
  To: Mirian Shilakadze, Pavel Tikhomirov; +Cc: devel

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/<pid>/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
Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
 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/<pid>/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 <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.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) {
+		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/<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->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/<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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Devel] [PATCH hci-8.2 v4 2/3] ve: give the pseudosuper relaxations of ve0 to its own tasks only
  2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 2/3] ve: give the pseudosuper relaxations of ve0 to its own tasks only Konstantin Khorenko
@ 2026-11-23  9:19   ` Pavel Tikhomirov
  2026-11-24 10:02     ` Mirian Shilakadze
  0 siblings, 1 reply; 6+ messages in thread
From: Pavel Tikhomirov @ 2026-11-23  9:19 UTC (permalink / raw)
  To: Konstantin Khorenko, Mirian Shilakadze; +Cc: devel



On 11/20/26 17:34, Konstantin Khorenko wrote:
> ve_current_is_pseudosuper() answers yes whenever the VE it is asked
> about is ve0, whoever asks. The line keeps the behaviour of ve0 being
> statically pseudosuper, but it reads as a grant to any task: a
> container task would get the relaxations of the host from a caller
> that passes it ve0.
> 
> Let the ve0 branch ask whether current is a ve0 task instead. The
> relaxations of a VE belong to the tasks the host sent into it, and for
> ve0 those are its own tasks, no mark involved.
> 
> No caller but one sees a different answer:
> 
> - devcgroup_legacy_check_permission(), netlink_setsockopt() for
>   NETLINK_REPAIR, cgroup_file_write(), ve_hide_cgroups(),
>   prctl_set_task_ct_fields(), ve_devmnt_verify() and
>   ve_devmnt_process() pass the caller's own VE behind a !ve_is_super()
>   check;
> - ve_features_write(), ve_failcount_write(),
>   ve_pid_max_write_running_u64() and ve_aio_max_nr_write() pass the css
>   of a CFTYPE_NOT_ON_ROOT file, which ve0 does not have;
> - task_still_dumpable() asks only about a VD_VE_SETUP_TASK of a VE
>   other than ve0, ve_vps_dumpable() reports ve0's as
>   VD_PTRACE_COREDUMP.
> 
> ve_try_reserve_mount() does pass ve0 for a VE task: a host helper that
> joined the VE cgroup but still sits in the host's mount namespace owns
> its bind mounts through ve0. Its reservation now goes through ve0's
> mnt_avail_nr like any counted one; the counter starts at INT_MAX and
> has no cgroup file to lower it, so the mount is granted as before.
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-156164
> 
> Feature: trace: prohibit tracing sensitive tasks from CT
> Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
> ---
>  kernel/ve/ve.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/kernel/ve/ve.c b/kernel/ve/ve.c
> index 0ad145b38b54b..7bbc1106ba389 100644
> --- a/kernel/ve/ve.c
> +++ b/kernel/ve/ve.c
> @@ -1784,17 +1784,18 @@ bool ve_check_trusted_exec(struct file *file, struct filename *name)
>   * ve_current_is_pseudosuper - may current use the pseudosuper relaxations of @ve
>   * @ve: the VE that grants them
>   *
> - * Always true for ve0. Otherwise only a host task of @ve may, one the host
> - * sent in while @ve was starting (VD_VE_SETUP_TASK) or after that
> - * (VD_VE_ENTER_TASK), and only until pseudosuper is cleared. Exec clears
> - * both marks, so the container binaries that the host tools run may not.
> + * The relaxations of ve0 belong to its own tasks. Those of a container
> + * belong to a host task of @ve, one the host sent in while @ve was starting
> + * (VD_VE_SETUP_TASK) or after that (VD_VE_ENTER_TASK), and only until
> + * pseudosuper is cleared. Exec clears both marks, so the container binaries
> + * that the host tools run may not.
>   */
>  bool ve_current_is_pseudosuper(struct ve_struct *ve)
>  {
>  	struct mm_struct *mm = current->mm;
>  
>  	if (ve_is_super(ve))
> -		return true;
> +		return ve_is_super(get_exec_env());
>  
>  	return ve->is_pseudosuper && task_ve(current) == ve && mm &&
>  	       (mm->vps_dumpable == VD_VE_SETUP_TASK ||

I'd rather you simplify:

bool ve_current_is_pseudosuper(struct ve_struct *ve)
{
        struct mm_struct *mm = current->mm;

        if (task_ve(current) != ve)
                return false;

        /* ve0 trusts all of its own tasks */
        if (ve_is_super(get_exec_env()))
                return true;

        return ve->is_pseudosuper && mm &&
               (mm->vps_dumpable == VD_VE_SETUP_TASK ||
                mm->vps_dumpable == VD_VE_ENTER_TASK);
}

So it's absolutely obvious that we check super only against task's own ve.

> 

-- 
Best regards, Pavel Tikhomirov
Senior Software Developer, Virtuozzo.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Devel] [PATCH hci-8.2 v4 2/3] ve: give the pseudosuper relaxations of ve0 to its own tasks only
  2026-11-23  9:19   ` Pavel Tikhomirov
@ 2026-11-24 10:02     ` Mirian Shilakadze
  0 siblings, 0 replies; 6+ messages in thread
From: Mirian Shilakadze @ 2026-11-24 10:02 UTC (permalink / raw)
  To: Pavel Tikhomirov, Konstantin Khorenko; +Cc: devel


On 11/23/26 1:19 PM, Pavel Tikhomirov wrote:
> I'd rather you simplify:
> 
> bool ve_current_is_pseudosuper(struct ve_struct *ve)
> {
>          struct mm_struct *mm = current->mm;
> 
>          if (task_ve(current) != ve)
>                  return false;
> 
>          /* ve0 trusts all of its own tasks */
>          if (ve_is_super(get_exec_env()))
>                  return true;
> 
>          return ve->is_pseudosuper && mm &&
>                 (mm->vps_dumpable == VD_VE_SETUP_TASK ||
>                  mm->vps_dumpable == VD_VE_ENTER_TASK);
> }
> 
> So it's absolutely obvious that we check super only against task's own ve.
Agreed, your version makes it obvious, I'd take it.

-- 
Best regards, Mirian Shilakadze
Linux C Virtualization Developer, Virtuozzo.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-24 10:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 16:34 [Devel] [PATCH hci-8.2 v4 0/3] ve: Container start on the 26.3 kernel with libvzctl before 10.0.144 Konstantin Khorenko
2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 1/3] ve: trust every host task of a pseudosuper VE until it execs Konstantin Khorenko
2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 2/3] ve: give the pseudosuper relaxations of ve0 to its own tasks only Konstantin Khorenko
2026-11-23  9:19   ` Pavel Tikhomirov
2026-11-24 10:02     ` Mirian Shilakadze
2026-11-20 16:34 ` [Devel] [PATCH hci-8.2 v4 3/3] selftests/ve: check a host task entering a pseudosuper VE reaches its init Konstantin Khorenko

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.