1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Ronny Chevalier
6 #include <netinet/in.h>
8 #include <sys/capability.h>
10 #include <sys/socket.h>
14 #include "alloc-util.h"
15 #include "capability-util.h"
19 #include "parse-util.h"
22 static uid_t test_uid
= -1;
23 static gid_t test_gid
= -1;
25 /* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */
26 static uint64_t test_flags
= 1ULL << CAP_DAC_OVERRIDE
;
28 /* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */
29 static void test_last_cap_file(void) {
30 _cleanup_free_
char *content
= NULL
;
31 unsigned long val
= 0;
34 r
= read_one_line_file("/proc/sys/kernel/cap_last_cap", &content
);
37 r
= safe_atolu(content
, &val
);
40 assert_se(val
== cap_last_cap());
43 /* verify cap_last_cap() against syscall probing */
44 static void test_last_cap_probe(void) {
45 unsigned long p
= (unsigned long)CAP_LAST_CAP
;
47 if (prctl(PR_CAPBSET_READ
, p
) < 0) {
48 for (p
--; p
> 0; p
--)
49 if (prctl(PR_CAPBSET_READ
, p
) >= 0)
53 if (prctl(PR_CAPBSET_READ
, p
+1) < 0)
58 assert_se(p
== cap_last_cap());
61 static void fork_test(void (*test_func
)(void)) {
72 assert_se(waitpid(pid
, &status
, 0) > 0);
73 assert_se(WIFEXITED(status
) && WEXITSTATUS(status
) == 0);
77 static void show_capabilities(void) {
81 caps
= cap_get_proc();
84 text
= cap_to_text(caps
, NULL
);
87 log_info("Capabilities:%s", text
);
92 static int setup_tests(bool *run_ambient
) {
93 struct passwd
*nobody
;
96 nobody
= getpwnam(NOBODY_USER_NAME
);
98 log_error_errno(errno
, "Could not find nobody user: %m");
99 return -EXIT_TEST_SKIP
;
101 test_uid
= nobody
->pw_uid
;
102 test_gid
= nobody
->pw_gid
;
104 *run_ambient
= false;
106 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_CLEAR_ALL
, 0, 0, 0);
108 /* There's support for PR_CAP_AMBIENT if the prctl() call
109 * succeeded or error code was something else than EINVAL. The
110 * EINVAL check should be good enough to rule out false
113 if (r
>= 0 || errno
!= EINVAL
)
119 static void test_drop_privileges_keep_net_raw(void) {
122 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
123 assert_se(sock
>= 0);
126 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
| (1ULL << CAP_NET_RAW
)) >= 0);
127 assert_se(getuid() == test_uid
);
128 assert_se(getgid() == test_gid
);
131 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
132 assert_se(sock
>= 0);
136 static void test_drop_privileges_dontkeep_net_raw(void) {
139 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
140 assert_se(sock
>= 0);
143 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
) >= 0);
144 assert_se(getuid() == test_uid
);
145 assert_se(getgid() == test_gid
);
148 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
152 static void test_drop_privileges_fail(void) {
153 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
) >= 0);
154 assert_se(getuid() == test_uid
);
155 assert_se(getgid() == test_gid
);
157 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
) < 0);
158 assert_se(drop_privileges(0, 0, test_flags
) < 0);
161 static void test_drop_privileges(void) {
162 fork_test(test_drop_privileges_keep_net_raw
);
163 fork_test(test_drop_privileges_dontkeep_net_raw
);
164 fork_test(test_drop_privileges_fail
);
167 static void test_have_effective_cap(void) {
168 assert_se(have_effective_cap(CAP_KILL
));
169 assert_se(have_effective_cap(CAP_CHOWN
));
171 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
| (1ULL << CAP_KILL
)) >= 0);
172 assert_se(getuid() == test_uid
);
173 assert_se(getgid() == test_gid
);
175 assert_se(have_effective_cap(CAP_KILL
));
176 assert_se(!have_effective_cap(CAP_CHOWN
));
179 static void test_update_inherited_set(void) {
184 caps
= cap_get_proc();
186 assert_se(!cap_get_flag(caps
, CAP_CHOWN
, CAP_INHERITABLE
, &fv
));
187 assert(fv
== CAP_CLEAR
);
189 set
= (UINT64_C(1) << CAP_CHOWN
);
191 assert_se(!capability_update_inherited_set(caps
, set
));
192 assert_se(!cap_get_flag(caps
, CAP_CHOWN
, CAP_INHERITABLE
, &fv
));
193 assert(fv
== CAP_SET
);
198 static void test_set_ambient_caps(void) {
203 caps
= cap_get_proc();
205 assert_se(!cap_get_flag(caps
, CAP_CHOWN
, CAP_INHERITABLE
, &fv
));
206 assert(fv
== CAP_CLEAR
);
209 assert_se(prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_IS_SET
, CAP_CHOWN
, 0, 0) == 0);
211 set
= (UINT64_C(1) << CAP_CHOWN
);
213 assert_se(!capability_ambient_set_apply(set
, true));
215 caps
= cap_get_proc();
216 assert_se(!cap_get_flag(caps
, CAP_CHOWN
, CAP_INHERITABLE
, &fv
));
217 assert(fv
== CAP_SET
);
220 assert_se(prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_IS_SET
, CAP_CHOWN
, 0, 0) == 1);
223 int main(int argc
, char *argv
[]) {
227 test_last_cap_file();
228 test_last_cap_probe();
230 log_parse_environment();
233 log_info("have ambient caps: %s", yes_no(ambient_capabilities_supported()));
236 return EXIT_TEST_SKIP
;
238 r
= setup_tests(&run_ambient
);
244 test_drop_privileges();
245 test_update_inherited_set();
247 fork_test(test_have_effective_cap
);
250 fork_test(test_set_ambient_caps
);