Skip to content

Commit f24939e

Browse files
bachradsusiWOnder93
authored andcommitted
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. Signed-off-by: Petr Lautrbach <[email protected]> [OM: added ifdefs to make the policy build in more environments] Signed-off-by: Ondrej Mosnacek <[email protected]>
1 parent 44b6ba3 commit f24939e

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

policy/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ ifeq ($(shell grep -q user_namespace $(POLDEV)/include/support/all_perms.spt &&
175175
export M4PARAM += -Duser_namespace_defined
176176
endif
177177

178+
ifeq ($(shell grep -q checkpoint_restore $(POLDEV)/include/support/all_perms.spt && echo true),true)
179+
export M4PARAM += -Dcheckpoint_restore_defined
180+
endif
181+
178182
# conditional xperm dependency: policy >= 34
179183
ifeq ($(shell [ $(POL_VERS) -ge 34 -a $(MAX_KERNEL_POLICY) -ge 34 ] && echo true),true)
180184
TARGETS += test_ioctl_cond_xperms.te

policy/test_cap_userns.te

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55

66
attribute capusernsdomain;
7+
attribute cap2usernsdomain;
78

89
# Domain for process that is allowed non-init userns capabilities
910
type test_cap_userns_t;
@@ -22,3 +23,23 @@ typeattribute test_no_cap_userns_t capusernsdomain;
2223
allow_userns_create(capusernsdomain)
2324
# linux >= v5.12 needs setfcap to map UID 0
2425
allow capusernsdomain self:capability setfcap;
26+
27+
# Domain for process that is allowed to use cap_checkpoint_restore
28+
type test_cap2_userns_t;
29+
testsuite_domain_type(test_cap2_userns_t)
30+
typeattribute test_cap2_userns_t cap2usernsdomain;
31+
ifdef(`checkpoint_restore_defined', `
32+
allow test_cap2_userns_t self:cap2_userns checkpoint_restore;
33+
')
34+
35+
# Domain for process that is not to use cap_checkpoint_restore
36+
type test_no_cap2_userns_t;
37+
testsuite_domain_type(test_no_cap2_userns_t)
38+
typeattribute test_no_cap2_userns_t cap2usernsdomain;
39+
40+
# Rules common to both domains.
41+
ifdef(`kernel_rw_kernel_ns_lastpid_sysctl',`
42+
kernel_rw_kernel_ns_lastpid_sysctl(cap2usernsdomain)
43+
', `
44+
kernel_rw_kernel_sysctl(cap2usernsdomain)
45+
')

tests/cap_userns/test

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@ BEGIN {
66
$basedir = $0;
77
$basedir =~ s|(.*)/[^/]*|$1|;
88

9+
$test_checkpoint = 0;
10+
911
if ( -e '/proc/sys/kernel/unprivileged_userns_clone' ) {
1012
system(
1113
"echo 1 > /proc/sys/kernel/unprivileged_userns_clone 2> /dev/null");
1214
}
13-
if ( system("$basedir/userns_child_exec -t -U > /dev/null 2>&1") == 0 ) {
14-
plan tests => 2;
15+
if ( system("$basedir/userns_child_exec -t -U > /dev/null 2>&1") != 0 ) {
16+
plan skip_all => "CLONE_NEWUSER not supported";
1517
}
1618
else {
17-
plan skip_all => "CLONE_NEWUSER not supported";
19+
$test_count = 2;
20+
21+
# CAP_CHECKPOINT_RESTORE is supported since 5.9
22+
$kvercur = `uname -r`;
23+
chomp($kvercur);
24+
25+
if ( `$basedir/../kvercmp $kvercur 5.9` > 0 ) {
26+
$test_checkpoint = 1;
27+
$test_count += 2;
28+
}
29+
30+
plan tests => $test_count;
1831
}
1932
}
2033

@@ -32,6 +45,23 @@ $result = system(
3245
);
3346
ok($result);
3447

48+
if ($test_checkpoint) {
49+
50+
# Verify that test_cap2_userns_t can use cap_checkpoint_restore
51+
52+
$result = system(
53+
"$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"
54+
);
55+
ok( $result eq 0 );
56+
57+
# Verify that test_no_cap_userns_t cannot use cap_checkpoint_restore
58+
59+
$result = system(
60+
"$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"
61+
);
62+
ok($result);
63+
}
64+
3565
if ( -e '/proc/sys/kernel/unprivileged_userns_clone' ) {
3666
system("echo 0 > /proc/sys/kernel/unprivileged_userns_clone 2> /dev/null");
3767
}

0 commit comments

Comments
 (0)