Skip to contentSkip to conversation

userns_child_exec: Propagate child exit value

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

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 v2 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.

---

v2 changes:

- do not run test on old kernels - CAP_CHECKPOINT_RESTORE check was introduced in 5.9
- use direct write instead of sysctl - old versions would not return correct error - https://gitlab.com/procps-ng/procps/-/commit/cd3a440b562e6efc713ce1524678dd068a85bf91

 policy/test_cap_userns.te | 15 +++++++++++++++
 tests/cap_userns/test     | 36 +++++++++++++++++++++++++++++++++---
 2 files changed, 48 insertions(+), 3 deletions(-)
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 +33 -3
index 917da00e4805..98b91a7845f9 100755
@@ -6,15 +6,28 @@ BEGIN {
     $basedir = $0;
     $basedir =~ s|(.*)/[^/]*|$1|;
 
+    $test_checkpoint = 0;
+
     if ( -e '/proc/sys/kernel/unprivileged_userns_clone' ) {
         system(
             "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;
+    if ( system("$basedir/userns_child_exec -t -U > /dev/null 2>&1") != 0 ) {
+        plan skip_all => "CLONE_NEWUSER not supported";
     }
     else {
-        plan skip_all => "CLONE_NEWUSER not supported";
+        $test_count = 2;
+
+        # CAP_CHECKPOINT_RESTORE is supported since 5.9
+        $kvercur = `uname -r`;
+        chomp($kvercur);
+
+        if ( `$basedir/../kvercmp $kvercur 5.9` > 0 ) {
+            $test_checkpoint = 1;
+            $test_count += 2;
+        }
+
+        plan tests => $test_count;
     }
 }
 
@@ -32,6 +45,23 @@ $result = system(
 );
 ok($result);
 
+if ($test_checkpoint) {
+
+    # 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 -- sh -c 'echo 1000 > /proc/sys/kernel/ns_last_pid'"
+    );
+    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 -- sh -c 'echo 1000 > /proc/sys/kernel/ns_last_pid'"
+    );
+    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