xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/userns/userns01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Huawei Technologies Co., Ltd., 2015
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that if a user ID has no mapping inside the namespace, user ID and
11*49cdfc7eSAndroid Build Coastguard Worker  * group ID will be the value defined in the file /proc/sys/kernel/overflowuid
12*49cdfc7eSAndroid Build Coastguard Worker  * (defaults to 65534) and /proc/sys/kernel/overflowgid (defaults to 65534). A
13*49cdfc7eSAndroid Build Coastguard Worker  * child process has a full set of permitted and effective capabilities, even
14*49cdfc7eSAndroid Build Coastguard Worker  * though the program was run from an unprivileged account.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_LIBCAP
20*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/capability.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #define OVERFLOWUIDPATH "/proc/sys/kernel/overflowuid"
28*49cdfc7eSAndroid Build Coastguard Worker #define OVERFLOWGIDPATH "/proc/sys/kernel/overflowgid"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static long overflowuid;
31*49cdfc7eSAndroid Build Coastguard Worker static long overflowgid;
32*49cdfc7eSAndroid Build Coastguard Worker 
child_fn1(void)33*49cdfc7eSAndroid Build Coastguard Worker static void child_fn1(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker 	int uid, gid;
36*49cdfc7eSAndroid Build Coastguard Worker 	cap_t caps;
37*49cdfc7eSAndroid Build Coastguard Worker 	int i, last_cap;
38*49cdfc7eSAndroid Build Coastguard Worker 	cap_flag_value_t flag_val;
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	uid = geteuid();
41*49cdfc7eSAndroid Build Coastguard Worker 	gid = getegid();
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "USERNS test is running in a new user namespace.");
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(uid, overflowuid);
46*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(gid, overflowgid);
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	caps = cap_get_proc();
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF("/proc/sys/kernel/cap_last_cap", "%d", &last_cap);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i <= last_cap; i++) {
53*49cdfc7eSAndroid Build Coastguard Worker 		cap_get_flag(caps, i, CAP_EFFECTIVE, &flag_val);
54*49cdfc7eSAndroid Build Coastguard Worker 		if (!flag_val)
55*49cdfc7eSAndroid Build Coastguard Worker 			break;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 		cap_get_flag(caps, i, CAP_PERMITTED, &flag_val);
58*49cdfc7eSAndroid Build Coastguard Worker 		if (!flag_val)
59*49cdfc7eSAndroid Build Coastguard Worker 			break;
60*49cdfc7eSAndroid Build Coastguard Worker 	}
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	if (!flag_val)
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "unexpected effective/permitted caps at %d", i);
64*49cdfc7eSAndroid Build Coastguard Worker 	else
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "expected capabilities");
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)68*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF(OVERFLOWUIDPATH, "%ld", &overflowuid);
71*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF(OVERFLOWGIDPATH, "%ld", &overflowgid);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker 
run(void)74*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker 	const struct tst_clone_args args = {
77*49cdfc7eSAndroid Build Coastguard Worker 		.flags = CLONE_NEWUSER,
78*49cdfc7eSAndroid Build Coastguard Worker 		.exit_signal = SIGCHLD,
79*49cdfc7eSAndroid Build Coastguard Worker 	};
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	if (!SAFE_CLONE(&args)) {
82*49cdfc7eSAndroid Build Coastguard Worker 		child_fn1();
83*49cdfc7eSAndroid Build Coastguard Worker 		return;
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
88*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
89*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
90*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
91*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
92*49cdfc7eSAndroid Build Coastguard Worker 	.caps = (struct tst_cap []) {
93*49cdfc7eSAndroid Build Coastguard Worker 		TST_CAP(TST_CAP_DROP, CAP_NET_RAW),
94*49cdfc7eSAndroid Build Coastguard Worker 		{}
95*49cdfc7eSAndroid Build Coastguard Worker 	},
96*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
97*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS",
98*49cdfc7eSAndroid Build Coastguard Worker 		NULL,
99*49cdfc7eSAndroid Build Coastguard Worker 	},
100*49cdfc7eSAndroid Build Coastguard Worker };
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker #else
103*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("System is missing libcap");
104*49cdfc7eSAndroid Build Coastguard Worker #endif
105