Skip to content

Commit 7954a1d

Browse files
gnoackl0kod
authored andcommitted
selftests/landlock: Test ioctl(2) and ftruncate(2) with open(O_PATH)
ioctl(2) and ftruncate(2) operations on files opened with O_PATH should always return EBADF, independent of the LANDLOCK_ACCESS_FS_TRUNCATE and LANDLOCK_ACCESS_FS_IOCTL_DEV access rights in that file hierarchy. 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 dd6d32a commit 7954a1d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,46 @@ static int test_fionread_ioctl(int fd)
39023902
return 0;
39033903
}
39043904

3905+
TEST_F_FORK(layout1, o_path_ftruncate_and_ioctl)
3906+
{
3907+
const struct landlock_ruleset_attr attr = {
3908+
.handled_access_fs = ACCESS_ALL,
3909+
};
3910+
int ruleset_fd, fd;
3911+
3912+
/*
3913+
* Checks that for files opened with O_PATH, both ioctl(2) and
3914+
* ftruncate(2) yield EBADF, as it is documented in open(2) for the
3915+
* O_PATH flag.
3916+
*/
3917+
fd = open(dir_s1d1, O_PATH | O_CLOEXEC);
3918+
ASSERT_LE(0, fd);
3919+
3920+
EXPECT_EQ(EBADF, test_ftruncate(fd));
3921+
EXPECT_EQ(EBADF, test_fs_ioc_getflags_ioctl(fd));
3922+
3923+
ASSERT_EQ(0, close(fd));
3924+
3925+
/* Enables Landlock. */
3926+
ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
3927+
ASSERT_LE(0, ruleset_fd);
3928+
enforce_ruleset(_metadata, ruleset_fd);
3929+
ASSERT_EQ(0, close(ruleset_fd));
3930+
3931+
/*
3932+
* Checks that after enabling Landlock,
3933+
* - the file can still be opened with O_PATH
3934+
* - both ioctl and truncate still yield EBADF (not EACCES).
3935+
*/
3936+
fd = open(dir_s1d1, O_PATH | O_CLOEXEC);
3937+
ASSERT_LE(0, fd);
3938+
3939+
EXPECT_EQ(EBADF, test_ftruncate(fd));
3940+
EXPECT_EQ(EBADF, test_fs_ioc_getflags_ioctl(fd));
3941+
3942+
ASSERT_EQ(0, close(fd));
3943+
}
3944+
39053945
/* clang-format off */
39063946
FIXTURE(ioctl) {};
39073947

0 commit comments

Comments
 (0)