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