|
14 | 14 | #include <linux/landlock.h>
|
15 | 15 | #include <linux/magic.h>
|
16 | 16 | #include <sched.h>
|
| 17 | +#include <stddef.h> |
17 | 18 | #include <stdio.h>
|
18 | 19 | #include <string.h>
|
19 | 20 | #include <sys/capability.h>
|
20 | 21 | #include <sys/ioctl.h>
|
21 | 22 | #include <sys/mount.h>
|
22 | 23 | #include <sys/prctl.h>
|
23 | 24 | #include <sys/sendfile.h>
|
| 25 | +#include <sys/socket.h> |
24 | 26 | #include <sys/stat.h>
|
25 | 27 | #include <sys/sysmacros.h>
|
| 28 | +#include <sys/un.h> |
26 | 29 | #include <sys/vfs.h>
|
27 | 30 | #include <unistd.h>
|
28 | 31 |
|
@@ -3985,6 +3988,56 @@ TEST_F_FORK(layout1, named_pipe_ioctl)
|
3985 | 3988 | ASSERT_EQ(child_pid, waitpid(child_pid, NULL, 0));
|
3986 | 3989 | }
|
3987 | 3990 |
|
| 3991 | +/* For named UNIX domain sockets, no IOCTL restrictions apply. */ |
| 3992 | +TEST_F_FORK(layout1, named_unix_domain_socket_ioctl) |
| 3993 | +{ |
| 3994 | + const char *const path = file1_s1d1; |
| 3995 | + int srv_fd, cli_fd, ruleset_fd; |
| 3996 | + socklen_t size; |
| 3997 | + struct sockaddr_un srv_un, cli_un; |
| 3998 | + const struct landlock_ruleset_attr attr = { |
| 3999 | + .handled_access_fs = LANDLOCK_ACCESS_FS_IOCTL_DEV, |
| 4000 | + }; |
| 4001 | + |
| 4002 | + /* Sets up a server */ |
| 4003 | + srv_un.sun_family = AF_UNIX; |
| 4004 | + strncpy(srv_un.sun_path, path, sizeof(srv_un.sun_path)); |
| 4005 | + |
| 4006 | + ASSERT_EQ(0, unlink(path)); |
| 4007 | + srv_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 4008 | + ASSERT_LE(0, srv_fd); |
| 4009 | + |
| 4010 | + size = offsetof(struct sockaddr_un, sun_path) + strlen(srv_un.sun_path); |
| 4011 | + ASSERT_EQ(0, bind(srv_fd, (struct sockaddr *)&srv_un, size)); |
| 4012 | + ASSERT_EQ(0, listen(srv_fd, 10 /* qlen */)); |
| 4013 | + |
| 4014 | + /* Enables Landlock. */ |
| 4015 | + ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); |
| 4016 | + ASSERT_LE(0, ruleset_fd); |
| 4017 | + enforce_ruleset(_metadata, ruleset_fd); |
| 4018 | + ASSERT_EQ(0, close(ruleset_fd)); |
| 4019 | + |
| 4020 | + /* Sets up a client connection to it */ |
| 4021 | + cli_un.sun_family = AF_UNIX; |
| 4022 | + cli_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 4023 | + ASSERT_LE(0, cli_fd); |
| 4024 | + |
| 4025 | + size = offsetof(struct sockaddr_un, sun_path) + strlen(cli_un.sun_path); |
| 4026 | + ASSERT_EQ(0, bind(cli_fd, (struct sockaddr *)&cli_un, size)); |
| 4027 | + |
| 4028 | + bzero(&cli_un, sizeof(cli_un)); |
| 4029 | + cli_un.sun_family = AF_UNIX; |
| 4030 | + strncpy(cli_un.sun_path, path, sizeof(cli_un.sun_path)); |
| 4031 | + size = offsetof(struct sockaddr_un, sun_path) + strlen(cli_un.sun_path); |
| 4032 | + |
| 4033 | + ASSERT_EQ(0, connect(cli_fd, (struct sockaddr *)&cli_un, size)); |
| 4034 | + |
| 4035 | + /* FIONREAD and other IOCTLs should not be forbidden. */ |
| 4036 | + EXPECT_EQ(0, test_fionread_ioctl(cli_fd)); |
| 4037 | + |
| 4038 | + ASSERT_EQ(0, close(cli_fd)); |
| 4039 | +} |
| 4040 | + |
3988 | 4041 | /* clang-format off */
|
3989 | 4042 | FIXTURE(ioctl) {};
|
3990 | 4043 |
|
|
0 commit comments