|
3 | 3 | // whether or not they return the expected ECAPMODE.
|
4 | 4 | #include <sys/types.h>
|
5 | 5 | #include <sys/socket.h>
|
| 6 | +#ifdef __FreeBSD__ |
| 7 | +#include <sys/sockio.h> |
| 8 | +#endif |
6 | 9 | #include <sys/stat.h>
|
7 | 10 | #include <sys/mount.h>
|
8 | 11 | #include <sys/mman.h>
|
|
11 | 14 | #include <sys/resource.h>
|
12 | 15 | #include <sys/ptrace.h>
|
13 | 16 | #include <dirent.h>
|
| 17 | +#include <net/if.h> |
14 | 18 | #include <netinet/in.h>
|
15 | 19 | #include <fcntl.h>
|
16 | 20 | #include <sched.h>
|
@@ -203,6 +207,39 @@ FORK_TEST_F(WithFiles, AllowedSocketSyscalls) {
|
203 | 207 | if (fd_pair[1] >= 0) close(fd_pair[1]);
|
204 | 208 | }
|
205 | 209 |
|
| 210 | +FORK_TEST_F(WithFiles, AllowedSocketSyscallsIfRoot) { |
| 211 | + GTEST_SKIP_IF_NOT_ROOT(); |
| 212 | + |
| 213 | + EXPECT_OK(cap_enter()); // Enter capability mode. |
| 214 | + |
| 215 | + // Creation of raw sockets is not permitted in capability mode. |
| 216 | + EXPECT_CAPMODE(socket(AF_INET, SOCK_RAW, 0)); |
| 217 | + EXPECT_CAPMODE(socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)); |
| 218 | + EXPECT_CAPMODE(socket(AF_INET, SOCK_RAW, IPPROTO_TCP)); |
| 219 | + EXPECT_CAPMODE(socket(AF_INET, SOCK_RAW, IPPROTO_UDP)); |
| 220 | + |
| 221 | + EXPECT_CAPMODE(socket(AF_INET6, SOCK_RAW, IPPROTO_ICMP)); |
| 222 | + EXPECT_CAPMODE(socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)); |
| 223 | + EXPECT_CAPMODE(socket(AF_INET6, SOCK_RAW, IPPROTO_TCP)); |
| 224 | + EXPECT_CAPMODE(socket(AF_INET6, SOCK_RAW, IPPROTO_UDP)); |
| 225 | + |
| 226 | + EXPECT_CAPMODE(socket(AF_ROUTE, SOCK_RAW, 0)); |
| 227 | + |
| 228 | + // Interface configuration ioctls are not permitted in capability |
| 229 | + // mode. |
| 230 | +#ifdef __FreeBSD__ |
| 231 | + struct if_clonereq req; |
| 232 | + |
| 233 | + req.ifcr_total = 0; |
| 234 | + req.ifcr_count = 1; |
| 235 | + req.ifcr_buffer = static_cast<char *>(malloc(IFNAMSIZ)); |
| 236 | + |
| 237 | + EXPECT_CAPMODE(ioctl(fd_socket_, SIOCIFGCLONERS, &req)); |
| 238 | + |
| 239 | + free(req.ifcr_buffer); |
| 240 | +#endif |
| 241 | +} |
| 242 | + |
206 | 243 | #ifdef HAVE_SEND_RECV_MMSG
|
207 | 244 | FORK_TEST(Capmode, AllowedMmsgSendRecv) {
|
208 | 245 | int fd_socket = socket(PF_INET, SOCK_DGRAM, 0);
|
|
0 commit comments