xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/prctl/prctl07.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) 2019 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[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  * Test the PR_CAP_AMBIENT of prctl(2).
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Reads or changes the ambient capability set of the calling thread,
13*49cdfc7eSAndroid Build Coastguard Worker  * according to the value of arg2, which must be one of the following:
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * - PR_CAP_AMBIENT_RAISE: The capability specified in arg3 is added to the
16*49cdfc7eSAndroid Build Coastguard Worker  *   ambient set. The specified capability must already be present in both pE
17*49cdfc7eSAndroid Build Coastguard Worker  *   and pI. If we set SECBIT_NO_CAP_AMBIENT_RAISE bit, raise option will be
18*49cdfc7eSAndroid Build Coastguard Worker  *   rejected and return EPERM. We also raise a CAP twice.
19*49cdfc7eSAndroid Build Coastguard Worker  *
20*49cdfc7eSAndroid Build Coastguard Worker  * - PR_CAP_AMBIENT_LOWER: The capability specified in arg3 is removed from the
21*49cdfc7eSAndroid Build Coastguard Worker  *   ambient set. Even though this cap is not in set, it also should return 0.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * - PR_CAP_AMBIENT_IS_SET: Returns 1 if the capability in arg3 is in the
24*49cdfc7eSAndroid Build Coastguard Worker  *   ambient set and 0 if it is not.
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * - PR_CAP_AMBIENT_CLEAR_ALL:  All capabilities will be removed from the
27*49cdfc7eSAndroid Build Coastguard Worker  *   ambient set. This operation requires setting arg3 to zero.
28*49cdfc7eSAndroid Build Coastguard Worker  */
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/prctl.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
33*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_CAPABILITY_H
34*49cdfc7eSAndroid Build Coastguard Worker # include <sys/capability.h>
35*49cdfc7eSAndroid Build Coastguard Worker #endif
36*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/prctl.h"
38*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/securebits.h"
39*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #define PROC_STATUS "/proc/self/status"
42*49cdfc7eSAndroid Build Coastguard Worker #define ZERO_STRING "0000000000000000"
43*49cdfc7eSAndroid Build Coastguard Worker /*CAP_NET_BIND_SERVICE stored in the CapAmb field of PROC_STATUS*/
44*49cdfc7eSAndroid Build Coastguard Worker #define CAP_STRING  "0000000000000400"
45*49cdfc7eSAndroid Build Coastguard Worker 
check_cap_raise(unsigned int cap,char * message,int fail_flag)46*49cdfc7eSAndroid Build Coastguard Worker static inline void check_cap_raise(unsigned int cap, char *message, int fail_flag)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0));
49*49cdfc7eSAndroid Build Coastguard Worker 	switch (fail_flag) {
50*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
51*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 0)
52*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "PR_CAP_AMBIENT_RAISE %s succeeded", message);
53*49cdfc7eSAndroid Build Coastguard Worker 	else
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "PR_CAP_AMBIENT_RAISE %s failed unexpectedly",
55*49cdfc7eSAndroid Build Coastguard Worker 			message);
56*49cdfc7eSAndroid Build Coastguard Worker 	break;
57*49cdfc7eSAndroid Build Coastguard Worker 	case 1:
58*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 0)
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL,
60*49cdfc7eSAndroid Build Coastguard Worker 			"PR_CAP_AMBIENT_RAISE succeeded unexpectedly %s",
61*49cdfc7eSAndroid Build Coastguard Worker 			message);
62*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_ERR == EPERM)
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS,
64*49cdfc7eSAndroid Build Coastguard Worker 			"PR_CAP_AMBIENT_RAISE failed with EPERM %s", message);
65*49cdfc7eSAndroid Build Coastguard Worker 	else
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
67*49cdfc7eSAndroid Build Coastguard Worker 			"PR_CAP_AMBIENT_RAISE failed %s", message);
68*49cdfc7eSAndroid Build Coastguard Worker 	break;
69*49cdfc7eSAndroid Build Coastguard Worker 	}
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker 
check_cap_is_set(unsigned int cap,char * message,int val)72*49cdfc7eSAndroid Build Coastguard Worker static inline void check_cap_is_set(unsigned int cap, char *message, int val)
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, cap, 0, 0));
75*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 1)
76*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(val ? TPASS : TFAIL,
77*49cdfc7eSAndroid Build Coastguard Worker 			"PR_CAP_AMBIENT_IS_SET %s in AmbientCap", message);
78*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_RET == 0)
79*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(val ? TFAIL : TPASS,
80*49cdfc7eSAndroid Build Coastguard Worker 			"PR_CAP_AMBIENT_IS_SET %s not in AmbientCap", message);
81*49cdfc7eSAndroid Build Coastguard Worker 	else
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "PR_CAP_AMBIENT_IS_SET failed");
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker 
check_cap_lower(unsigned int cap,char * message)85*49cdfc7eSAndroid Build Coastguard Worker static inline void check_cap_lower(unsigned int cap, char *message)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, cap, 0, 0));
88*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
90*49cdfc7eSAndroid Build Coastguard Worker 			"PR_CAP_AMBIENT_LOWER %s failed", message);
91*49cdfc7eSAndroid Build Coastguard Worker 	else
92*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "PR_CAP_AMBIENT_LOWER %s succeeded", message);
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker 
verify_prctl(void)95*49cdfc7eSAndroid Build Coastguard Worker static void verify_prctl(void)
96*49cdfc7eSAndroid Build Coastguard Worker {
97*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_LIBCAP
98*49cdfc7eSAndroid Build Coastguard Worker 	cap_t caps = cap_init();
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	cap_value_t caplist[3] = {CAP_NET_RAW, CAP_NET_BIND_SERVICE, CAP_SETPCAP};
101*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int numcaps = 3;
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_flag(caps, CAP_EFFECTIVE, numcaps, caplist, CAP_SET);
104*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_flag(caps, CAP_INHERITABLE, numcaps, caplist, CAP_SET);
105*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_flag(caps, CAP_PERMITTED, numcaps, caplist, CAP_SET);
106*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_proc(caps);
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "At the beginning");
109*49cdfc7eSAndroid Build Coastguard Worker 	TST_ASSERT_FILE_STR(PROC_STATUS, "CapAmb", ZERO_STRING);
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	cap_clear_flag(caps, CAP_INHERITABLE);
112*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_proc(caps);
113*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_raise(CAP_NET_BIND_SERVICE, "on non-inheritable cap", 1);
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_flag(caps, CAP_INHERITABLE, numcaps, caplist, CAP_SET);
116*49cdfc7eSAndroid Build Coastguard Worker 	cap_clear_flag(caps, CAP_PERMITTED);
117*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_proc(caps);
118*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_raise(CAP_NET_RAW, "on non-permitted cap", 1);
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_flag(caps, CAP_PERMITTED, numcaps, caplist, CAP_SET);
121*49cdfc7eSAndroid Build Coastguard Worker 	cap_set_proc(caps);
122*49cdfc7eSAndroid Build Coastguard Worker 	prctl(PR_SET_SECUREBITS, SECBIT_NO_CAP_AMBIENT_RAISE);
123*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_raise(CAP_NET_BIND_SERVICE, "because of NO_RAISE_SECBIT set", 1);
124*49cdfc7eSAndroid Build Coastguard Worker 	prctl(PR_SET_SECUREBITS, 0);
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_raise(CAP_NET_BIND_SERVICE, "CAP_NET_BIND_SERVICE", 0);
127*49cdfc7eSAndroid Build Coastguard Worker 	/*Even this cap has been in ambient set, raise succeeds and return 0*/
128*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_raise(CAP_NET_BIND_SERVICE, "CAP_NET_BIND_SERIVCE twice", 0);
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "After PR_CAP_AMBIENT_RAISE");
131*49cdfc7eSAndroid Build Coastguard Worker 	TST_ASSERT_FILE_STR(PROC_STATUS, "CapAmb", CAP_STRING);
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_is_set(CAP_NET_BIND_SERVICE, "CAP_NET_BIND_SERVICE was", 1);
134*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_is_set(CAP_NET_RAW, "CAP_NET_RAW was", 0);
135*49cdfc7eSAndroid Build Coastguard Worker 	/*move a cap what was not in ambient set, it also return 0*/
136*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_lower(CAP_NET_RAW, "CAP_NET_RAW(it wasn't in ambient set)");
137*49cdfc7eSAndroid Build Coastguard Worker 	check_cap_lower(CAP_NET_BIND_SERVICE, "CAP_NET_BIND_SERVICE(it was in ambient set)");
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "After PR_CAP_AMBIENT_LORWER");
140*49cdfc7eSAndroid Build Coastguard Worker 	TST_ASSERT_FILE_STR(PROC_STATUS, "CapAmb", ZERO_STRING);
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 	prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0);
143*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "raise cap for clear");
144*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0));
145*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 0)
146*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "PR_CAP_AMBIENT_CLEAR ALL succeeded");
147*49cdfc7eSAndroid Build Coastguard Worker 	else
148*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "PR_AMBIENT_CLEAR_ALL failed");
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "After PR_CAP_AMBIENT_CLEAR_ALL");
151*49cdfc7eSAndroid Build Coastguard Worker 	TST_ASSERT_FILE_STR(PROC_STATUS, "CapAmb", ZERO_STRING);
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker 	cap_free(caps);
154*49cdfc7eSAndroid Build Coastguard Worker #else
155*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TCONF, "libcap devel files missing during compilation");
156*49cdfc7eSAndroid Build Coastguard Worker #endif
157*49cdfc7eSAndroid Build Coastguard Worker }
158*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)159*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
160*49cdfc7eSAndroid Build Coastguard Worker {
161*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0));
162*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 0) {
163*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "kernel supports PR_CAP_AMBIENT");
164*49cdfc7eSAndroid Build Coastguard Worker 		return;
165*49cdfc7eSAndroid Build Coastguard Worker 	}
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == EINVAL)
168*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "kernel doesn't support PR_CAP_AMBIENT");
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker 	tst_brk(TBROK | TERRNO,
171*49cdfc7eSAndroid Build Coastguard Worker 		"current environment doesn't permit PR_CAP_AMBIENT");
172*49cdfc7eSAndroid Build Coastguard Worker }
173*49cdfc7eSAndroid Build Coastguard Worker 
174*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
175*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
176*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_prctl,
177*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
178*49cdfc7eSAndroid Build Coastguard Worker };
179