1 /* Copyright (c) 2015-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "core/or/or.h"
5 #include "lib/process/setuid.h"
7 #ifdef HAVE_SYS_CAPABILITY_H
8 #include <sys/capability.h>
14 #define TEST_BUILT_WITH_CAPS 0
15 #define TEST_HAVE_CAPS 1
16 #define TEST_ROOT_CAN_BIND_LOW 2
18 #define TEST_SETUID_KEEPCAPS 4
19 #define TEST_SETUID_STRICT 5
25 { "built-with-caps", TEST_BUILT_WITH_CAPS
},
26 { "have-caps", TEST_HAVE_CAPS
},
27 { "root-bind-low", TEST_ROOT_CAN_BIND_LOW
},
28 { "setuid", TEST_SETUID
},
29 { "setuid-keepcaps", TEST_SETUID_KEEPCAPS
},
30 { "setuid-strict", TEST_SETUID_STRICT
},
36 /* Returns the first port that we think we can bind to without special
37 * permissions. Usually this function returns 1024. */
39 unprivileged_port_range_start(void)
41 uint16_t result
= 1024;
43 #if defined(__linux__)
46 content
= read_file_to_str(
47 "/proc/sys/net/ipv4/ip_unprivileged_port_start",
51 if (content
!= NULL
) {
55 tmp_result
= (uint16_t)tor_parse_long(content
, 10, 0, 65535, &ok
, NULL
);
61 "Unable to convert ip_unprivileged_port_start to integer: %s\n",
67 #endif /* defined(__linux__) */
72 #define PORT_TEST_RANGE_START 600
73 #define PORT_TEST_RANGE_END 1024
75 /* 0 on no, 1 on yes, -1 on failure. */
77 check_can_bind_low_ports(void)
80 struct sockaddr_in sin
;
81 memset(&sin
, 0, sizeof(sin
));
82 sin
.sin_family
= AF_INET
;
84 for (port
= PORT_TEST_RANGE_START
; port
< PORT_TEST_RANGE_END
; ++port
) {
85 sin
.sin_port
= htons(port
);
86 tor_socket_t fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
87 if (! SOCKET_OK(fd
)) {
93 if (setsockopt(fd
, SOL_SOCKET
,SO_REUSEADDR
, (void*)&one
,
94 (socklen_t
)sizeof(one
))) {
96 tor_close_socket_simple(fd
);
100 int res
= bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
));
101 tor_close_socket_simple(fd
);
104 /* bind was successful */
106 } else if (errno
== EACCES
|| errno
== EPERM
) {
107 /* Got a permission-denied error. */
109 } else if (errno
== EADDRINUSE
) {
110 /* Huh; somebody is using that port. */
118 #endif /* !defined(_WIN32) */
121 main(int argc
, char **argv
)
128 fprintf(stderr
, "This test is not supported on your OS.\n");
130 #else /* !defined(_WIN32) */
131 const char *username
;
132 const char *testname
;
134 fprintf(stderr
, "I want 2 arguments: a username and a command.\n");
138 fprintf(stderr
, "This test only works when it's run as root.\n");
145 for (i
= 0; which_test
[i
].name
; ++i
) {
146 if (!strcmp(which_test
[i
].name
, testname
)) {
147 test_id
= which_test
[i
].test_id
;
152 fprintf(stderr
, "Unrecognized test '%s'\n", testname
);
156 #ifdef HAVE_LINUX_CAPABILITIES
157 const int have_cap_support
= 1;
159 const int have_cap_support
= 0;
165 log_severity_list_t sev
;
166 memset(&sev
, 0, sizeof(sev
));
167 set_log_severity_config(LOG_WARN
, LOG_ERR
, &sev
);
168 add_stream_log(&sev
, "", fileno(stderr
));
172 case TEST_BUILT_WITH_CAPS
:
173 /* Succeed if we were built with capability support. */
174 okay
= have_cap_support
;
177 /* Succeed if "capabilities work" == "we were built with capability
179 okay
= have_cap_support
== have_capability_support();
181 case TEST_ROOT_CAN_BIND_LOW
:
182 /* Succeed if root can bind low ports. */
183 okay
= check_can_bind_low_ports() == 1;
186 /* Succeed if we can do a setuid with no capability retention, and doing
187 * so makes us lose the ability to bind low ports */
188 case TEST_SETUID_KEEPCAPS
:
189 /* Succeed if we can do a setuid with capability retention, and doing so
190 * does not make us lose the ability to bind low ports */
192 const int keepcaps
= (test_id
== TEST_SETUID_KEEPCAPS
);
193 okay
= switch_id(username
, keepcaps
? SWITCH_ID_KEEP_BINDLOW
: 0) == 0;
196 /* Only run this check if there are ports we may not be able to bind
198 const uint16_t min_port
= unprivileged_port_range_start();
200 if (min_port
>= PORT_TEST_RANGE_START
&&
201 min_port
< PORT_TEST_RANGE_END
) {
202 okay
= check_can_bind_low_ports() == keepcaps
;
205 "Skipping check for whether we can bind to any "
206 "privileged ports as the user system seems to "
207 "allow us to bind to ports even without any "
208 "capabilities set.\n");
213 case TEST_SETUID_STRICT
:
214 /* Succeed if, after a setuid, we cannot setuid back, and we cannot
215 * re-grab any capabilities. */
216 okay
= switch_id(username
, SWITCH_ID_KEEP_BINDLOW
) == 0;
218 /* We'd better not be able to setuid back! */
219 if (setuid(0) == 0 || errno
!= EPERM
) {
223 #ifdef HAVE_LINUX_CAPABILITIES
225 cap_t caps
= cap_get_proc();
226 const cap_value_t caplist
[] = {
229 cap_set_flag(caps
, CAP_PERMITTED
, 1, caplist
, CAP_SET
);
230 if (cap_set_proc(caps
) == 0 || errno
!= EPERM
) {
235 #endif /* defined(HAVE_LINUX_CAPABILITIES) */
238 fprintf(stderr
, "Unsupported test '%s'\n", testname
);
244 fprintf(stderr
, "Test %s failed!\n", testname
);
247 return (okay
? 0 : 1);
248 #endif /* defined(_WIN32) */