Skip to content

Commit f83d51a

Browse files
gnoackl0kod
authored andcommitted
selftests/landlock: Check IOCTL restrictions for named UNIX domain sockets
The LANDLOCK_ACCESS_FS_IOCTL_DEV right should have no effect on the use of named UNIX domain sockets. Suggested-by: Mickaël Salaün <mic@digikod.net> Signed-off-by: Günther Noack <gnoack@google.com> Link: https://lore.kernel.org/r/20240419161122.2023765-7-gnoack@google.com [mic: Add missing stddef.h for offsetof()] Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 56ffd37 commit f83d51a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
#include <linux/landlock.h>
1515
#include <linux/magic.h>
1616
#include <sched.h>
17+
#include <stddef.h>
1718
#include <stdio.h>
1819
#include <string.h>
1920
#include <sys/capability.h>
2021
#include <sys/ioctl.h>
2122
#include <sys/mount.h>
2223
#include <sys/prctl.h>
2324
#include <sys/sendfile.h>
25+
#include <sys/socket.h>
2426
#include <sys/stat.h>
2527
#include <sys/sysmacros.h>
28+
#include <sys/un.h>
2629
#include <sys/vfs.h>
2730
#include <unistd.h>
2831

@@ -3985,6 +3988,56 @@ TEST_F_FORK(layout1, named_pipe_ioctl)
39853988
ASSERT_EQ(child_pid, waitpid(child_pid, NULL, 0));
39863989
}
39873990

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+
39884041
/* clang-format off */
39894042
FIXTURE(ioctl) {};
39904043

0 commit comments

Comments
 (0)