Skip to content

Commit dd6d32a

Browse files
gnoackl0kod
authored andcommitted
selftests/landlock: Test IOCTL with memfds
Because the LANDLOCK_ACCESS_FS_IOCTL_DEV right is associated with the opened file during open(2), IOCTLs are supposed to work with files which are opened by means other than open(2). 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 3ecf19e commit dd6d32a

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3849,20 +3849,48 @@ TEST_F_FORK(ftruncate, open_and_ftruncate_in_different_processes)
38493849
ASSERT_EQ(0, close(socket_fds[1]));
38503850
}
38513851

3852-
TEST(memfd_ftruncate)
3852+
/* Invokes the FS_IOC_GETFLAGS IOCTL and returns its errno or 0. */
3853+
static int test_fs_ioc_getflags_ioctl(int fd)
38533854
{
3854-
int fd;
3855+
uint32_t flags;
3856+
3857+
if (ioctl(fd, FS_IOC_GETFLAGS, &flags) < 0)
3858+
return errno;
3859+
return 0;
3860+
}
38553861

3856-
fd = memfd_create("name", MFD_CLOEXEC);
3857-
ASSERT_LE(0, fd);
3862+
TEST(memfd_ftruncate_and_ioctl)
3863+
{
3864+
const struct landlock_ruleset_attr attr = {
3865+
.handled_access_fs = ACCESS_ALL,
3866+
};
3867+
int ruleset_fd, fd, i;
38583868

38593869
/*
3860-
* Checks that ftruncate is permitted on file descriptors that are
3861-
* created in ways other than open(2).
3870+
* We exercise the same test both with and without Landlock enabled, to
3871+
* ensure that it behaves the same in both cases.
38623872
*/
3863-
EXPECT_EQ(0, test_ftruncate(fd));
3873+
for (i = 0; i < 2; i++) {
3874+
/* Creates a new memfd. */
3875+
fd = memfd_create("name", MFD_CLOEXEC);
3876+
ASSERT_LE(0, fd);
38643877

3865-
ASSERT_EQ(0, close(fd));
3878+
/*
3879+
* Checks that operations associated with the opened file
3880+
* (ftruncate, ioctl) are permitted on file descriptors that are
3881+
* created in ways other than open(2).
3882+
*/
3883+
EXPECT_EQ(0, test_ftruncate(fd));
3884+
EXPECT_EQ(0, test_fs_ioc_getflags_ioctl(fd));
3885+
3886+
ASSERT_EQ(0, close(fd));
3887+
3888+
/* Enables Landlock. */
3889+
ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
3890+
ASSERT_LE(0, ruleset_fd);
3891+
enforce_ruleset(_metadata, ruleset_fd);
3892+
ASSERT_EQ(0, close(ruleset_fd));
3893+
}
38663894
}
38673895

38683896
static int test_fionread_ioctl(int fd)

0 commit comments

Comments
 (0)