2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/in.h>
22 #include <sys/capability.h>
23 #include <sys/socket.h>
27 #include "capability.h"
32 static uid_t test_uid
= -1;
33 static gid_t test_gid
= -1;
35 /* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */
36 static uint64_t test_flags
= 1ULL << CAP_DAC_OVERRIDE
;
38 static void fork_test(void (*test_func
)(void)) {
49 assert_se(waitpid(pid
, &status
, 0) > 0);
50 assert_se(WIFEXITED(status
) && WEXITSTATUS(status
) == 0);
54 static void show_capabilities(void) {
58 caps
= cap_get_proc();
61 text
= cap_to_text(caps
, NULL
);
64 log_info("Capabilities:%s", text
);
69 static int setup_tests(void) {
70 struct passwd
*nobody
;
72 nobody
= getpwnam("nobody");
74 log_error_errno(errno
, "Could not find nobody user: %m");
75 return -EXIT_TEST_SKIP
;
77 test_uid
= nobody
->pw_uid
;
78 test_gid
= nobody
->pw_gid
;
83 static void test_drop_privileges_keep_net_raw(void) {
86 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
90 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
| (1ULL << CAP_NET_RAW
)) >= 0);
91 assert_se(getuid() == test_uid
);
92 assert_se(getgid() == test_gid
);
95 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
100 static void test_drop_privileges_dontkeep_net_raw(void) {
103 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
104 assert_se(sock
>= 0);
107 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
) >= 0);
108 assert_se(getuid() == test_uid
);
109 assert_se(getgid() == test_gid
);
112 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
116 static void test_drop_privileges_fail(void) {
117 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
) >= 0);
118 assert_se(getuid() == test_uid
);
119 assert_se(getgid() == test_gid
);
121 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
) < 0);
122 assert_se(drop_privileges(0, 0, test_flags
) < 0);
125 static void test_drop_privileges(void) {
126 fork_test(test_drop_privileges_keep_net_raw
);
127 fork_test(test_drop_privileges_dontkeep_net_raw
);
128 fork_test(test_drop_privileges_fail
);
131 static void test_have_effective_cap(void) {
132 assert_se(have_effective_cap(CAP_KILL
));
133 assert_se(have_effective_cap(CAP_CHOWN
));
135 assert_se(drop_privileges(test_uid
, test_gid
, test_flags
| (1ULL << CAP_KILL
)) >= 0);
136 assert_se(getuid() == test_uid
);
137 assert_se(getgid() == test_gid
);
139 assert_se(have_effective_cap(CAP_KILL
));
140 assert_se(!have_effective_cap(CAP_CHOWN
));
143 int main(int argc
, char *argv
[]) {
146 log_parse_environment();
150 return EXIT_TEST_SKIP
;
158 test_drop_privileges();
159 fork_test(test_have_effective_cap
);