userns_child_exec: Propagate child exit value
PATCH v3 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
@@ -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
PATCH v3 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.
---
v3 - redirect stderr to stdout
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
@@ -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
@@ -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' 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 -- sh -c 'echo 1000 > /proc/sys/kernel/ns_last_pid' 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
Re: [PATCH selinux-testsuite v3 2/2] Add tests for checkpoint_restore in cap2_userns
Quoted 106 lines · Petr Lautrbach
According to
pid_namespaces(7)CAP_CHECK_RESTOREis 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 hascap2_usernscheckpoint_restoreallowed.Signed-off-by Petr Lautrbach <[email protected]>---
v3 - redirect stderr to stdout
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@@ -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
-4@@ -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' 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 -- sh -c 'echo 1000 > /proc/sys/kernel/ns_last_pid' 2>&1" + ); + ok($result); +} + if ( -e '/proc/sys/kernel/unprivileged_userns_clone' ) { system("echo 0 > /proc/sys/kernel/unprivileged_userns_clone 2> /dev/null"); } --
Quoted 3 lines
2.49.0
Applied with minor tweaks to make the policy buildable on older distributions (e.g. RHEL 7):
https://github.com/SELinuxProject/selinux-testsuite/commit/44b6ba364eddeb191893b1ce080357c8b338cf70
https://github.com/SELinuxProject/selinux-testsuite/commit/f24939eedb09dee75962cbee48460b435f59266b
Signature · 3 lines
Ondrej Mosnacek Senior Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.