Skip to contentSkip to conversation

userns_child_exec: Propagate child exit value

PATCH Superseded Petr Lautrbach 1 participant 2 messages Jun 5, 2025 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 userns_child_exec: Propagate child exit value

Sometimes it's useful to know how a child ended.

---
 tests/cap_userns/userns_child_exec.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/cap_userns/userns_child_exec.c +3 -3
index cdbf1205a717..2ee831a3ba7e 100644
@@ -198,7 +198,7 @@ static char child_stack[STACK_SIZE];    /* Space for child's stack */
 int
 main(int argc, char *argv[])
 {
-	int flags, opt, map_zero, test_clone = 0;
+	int flags, opt, map_zero, test_clone = 0, wstatus;
 	pid_t child_pid;
 	struct child_args args;
 	char *uid_map, *gid_map;
@@ -332,11 +332,11 @@ main(int argc, char *argv[])
 
 	close(args.pipe_fd[1]);
 
-	if (waitpid(child_pid, NULL, 0) == -1)      /* Wait for child */
+	if (waitpid(child_pid, &wstatus, 0) == -1)      /* Wait for child */
 		errExit("waitpid");
 
 	if (verbose)
 		printf("%s: terminating\n", argv[0]);
 
-	exit(EXIT_SUCCESS);
+	exit(WEXITSTATUS(wstatus));
 }
Signature · 1 line
2.49.0
#1 lore raw

PATCH 2/2 Add tests for checkpoint_restore in cap2_userns

According to pid_namespaces(7) CAP_CHECK_RESTORE is required to write to /proc/sys/kernel/ns_last_pid
Check whether a process in user and pid namespace is able to write there when it has cap2_userns checkpoint_restore allowed.

---
 policy/test_cap_userns.te | 15 +++++++++++++++
 tests/cap_userns/test     | 16 +++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)
policy/test_cap_userns.te +15 -0
index 6f444871bcf0..402675b2eb7e 100644
@@ -4,6 +4,7 @@
 #
 
 attribute capusernsdomain;
+attribute cap2usernsdomain;
 
 # Domain for process that is allowed non-init userns capabilities
 type test_cap_userns_t;
@@ -22,3 +23,17 @@ typeattribute test_no_cap_userns_t capusernsdomain;
 allow_userns_create(capusernsdomain)
 # linux >= v5.12 needs setfcap to map UID 0
 allow capusernsdomain self:capability setfcap;
+
+# Domain for process that is allowed to use cap_checkpoint_restore
+type test_cap2_userns_t;
+testsuite_domain_type(test_cap2_userns_t)
+typeattribute test_cap2_userns_t cap2usernsdomain;
+allow test_cap2_userns_t self:cap2_userns checkpoint_restore;
+
+# Domain for process that is not to use cap_checkpoint_restore
+type test_no_cap2_userns_t;
+testsuite_domain_type(test_no_cap2_userns_t)
+typeattribute test_no_cap2_userns_t cap2usernsdomain;
+
+# Rules common to both domains.
+kernel_rw_kernel_ns_lastpid_sysctl(cap2usernsdomain)
tests/cap_userns/test +15 -1
index 917da00e4805..9cee33df360c 100755
@@ -11,7 +11,7 @@ BEGIN {
             "echo 1 > /proc/sys/kernel/unprivileged_userns_clone 2> /dev/null");
     }
     if ( system("$basedir/userns_child_exec -t -U > /dev/null 2>&1") == 0 ) {
-        plan tests => 2;
+        plan tests => 4;
     }
     else {
         plan skip_all => "CLONE_NEWUSER not supported";
@@ -32,6 +32,20 @@ $result = system(
 );
 ok($result);
 
+# Verify that test_cap2_userns_t can use cap_checkpoint_restore
+
+$result = system(
+"$basedir/userns_child_exec -p -U -M '0 0 1' -G '0 0 1' -- runcon -t test_cap2_userns_t -- sysctl -w kernel.ns_last_pid=1000 2>&1"
+);
+ok( $result eq 0 );
+
+# Verify that test_no_cap_userns_t cannot use cap_checkpoint_restore
+
+$result = system(
+"$basedir/userns_child_exec -p -U -M '0 0 1' -G '0 0 1' -- runcon -t test_no_cap2_userns_t -- sysctl -w kernel.ns_last_pid=1000 2>&1"
+);
+ok($result);
+
 if ( -e '/proc/sys/kernel/unprivileged_userns_clone' ) {
     system("echo 0 > /proc/sys/kernel/unprivileged_userns_clone 2> /dev/null");
 }
Signature · 1 line
2.49.0