Skip to content

Commit 303c20e

Browse files
gnoacksweettea
authored andcommitted
selftests/landlock: Check IOCTL restrictions for named UNIX domain sockets
Suggested-by: Mickaël Salaün <[email protected]> Signed-off-by: Günther Noack <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent 5666601 commit 303c20e

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
#include <sys/mount.h>
2121
#include <sys/prctl.h>
2222
#include <sys/sendfile.h>
23+
#include <sys/socket.h>
2324
#include <sys/stat.h>
2425
#include <sys/sysmacros.h>
26+
#include <sys/un.h>
2527
#include <sys/vfs.h>
2628
#include <unistd.h>
2729

@@ -3978,6 +3980,55 @@ TEST_F_FORK(layout1, named_pipe_ioctl)
39783980
ASSERT_EQ(child_pid, waitpid(child_pid, NULL, 0));
39793981
}
39803982

3983+
/* For named UNIX domain sockets, no IOCTL restrictions apply. */
3984+
TEST_F_FORK(layout1, named_unix_domain_socket_ioctl)
3985+
{
3986+
const char *const path = file1_s1d1;
3987+
int srv_fd, cli_fd, ruleset_fd;
3988+
socklen_t size;
3989+
struct sockaddr_un srv_un, cli_un;
3990+
const struct landlock_ruleset_attr attr = {
3991+
.handled_access_fs = LANDLOCK_ACCESS_FS_IOCTL_DEV,
3992+
};
3993+
3994+
/* Sets up a server */
3995+
srv_un.sun_family = AF_UNIX;
3996+
strncpy(srv_un.sun_path, path, sizeof(srv_un.sun_path));
3997+
3998+
ASSERT_EQ(0, unlink(path));
3999+
ASSERT_LE(0, (srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)));
4000+
4001+
size = offsetof(struct sockaddr_un, sun_path) + strlen(srv_un.sun_path);
4002+
ASSERT_EQ(0, bind(srv_fd, (struct sockaddr *)&srv_un, size));
4003+
ASSERT_EQ(0, listen(srv_fd, 10 /* qlen */));
4004+
4005+
/* Enables Landlock. */
4006+
ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
4007+
ASSERT_LE(0, ruleset_fd);
4008+
enforce_ruleset(_metadata, ruleset_fd);
4009+
ASSERT_EQ(0, close(ruleset_fd));
4010+
4011+
/* Sets up a client connection to it */
4012+
cli_un.sun_family = AF_UNIX;
4013+
4014+
ASSERT_LE(0, (cli_fd = socket(AF_UNIX, SOCK_STREAM, 0)));
4015+
4016+
size = offsetof(struct sockaddr_un, sun_path) + strlen(cli_un.sun_path);
4017+
ASSERT_EQ(0, bind(cli_fd, (struct sockaddr *)&cli_un, size));
4018+
4019+
bzero(&cli_un, sizeof(cli_un));
4020+
cli_un.sun_family = AF_UNIX;
4021+
strncpy(cli_un.sun_path, path, sizeof(cli_un.sun_path));
4022+
size = offsetof(struct sockaddr_un, sun_path) + strlen(cli_un.sun_path);
4023+
4024+
ASSERT_EQ(0, connect(cli_fd, (struct sockaddr *)&cli_un, size));
4025+
4026+
/* FIONREAD and other IOCTLs should not be forbidden. */
4027+
EXPECT_EQ(0, test_fionread_ioctl(cli_fd));
4028+
4029+
ASSERT_EQ(0, close(cli_fd));
4030+
}
4031+
39814032
/* clang-format off */
39824033
FIXTURE(ioctl) {};
39834034

0 commit comments

Comments
 (0)