Skip to content

Commit 35ca423

Browse files
committed
selftests/landlock: Add hostfs tests
Add tests for the hostfs filesystems to make sure it has a consistent inode management, which is required for Landlock's file hierarchy identification. This adds 5 new tests for layout3_fs with the hostfs variant. Add hostfs to the new (architecture-specific) config.um file. The hostfs filesystem, only available for an User-Mode Linux kernel, is special because we cannot explicitly mount it. The layout3_fs.hostfs variant tests are skipped if the current test directory is not backed by this filesystem. The layout3_fs.hostfs.tag_inode_dir_child and layout3_fs.hostfs.tag_inode_file tests pass thanks to a previous commit fixing hostfs inode management. Without this fix, the deny-by-default policy would apply and all access requests would be denied. Link: https://lore.kernel.org/r/20230612191430.339153-7-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 04f9070 commit 35ca423

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_HOSTFS=y

tools/testing/selftests/landlock/fs_test.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define _GNU_SOURCE
1111
#include <fcntl.h>
1212
#include <linux/landlock.h>
13+
#include <linux/magic.h>
1314
#include <sched.h>
1415
#include <stdio.h>
1516
#include <string.h>
@@ -19,6 +20,7 @@
1920
#include <sys/sendfile.h>
2021
#include <sys/stat.h>
2122
#include <sys/sysmacros.h>
23+
#include <sys/vfs.h>
2224
#include <unistd.h>
2325

2426
#include "common.h"
@@ -135,6 +137,19 @@ static bool supports_filesystem(const char *const filesystem)
135137
return res;
136138
}
137139

140+
static bool cwd_matches_fs(unsigned int fs_magic)
141+
{
142+
struct statfs statfs_buf;
143+
144+
if (!fs_magic)
145+
return true;
146+
147+
if (statfs(".", &statfs_buf))
148+
return true;
149+
150+
return statfs_buf.f_type == fs_magic;
151+
}
152+
138153
static void mkdir_parents(struct __test_metadata *const _metadata,
139154
const char *const path)
140155
{
@@ -4500,6 +4515,7 @@ FIXTURE_VARIANT(layout3_fs)
45004515
{
45014516
const struct mnt_opt mnt;
45024517
const char *const file_path;
4518+
unsigned int cwd_fs_magic;
45034519
};
45044520

45054521
/* clang-format off */
@@ -4538,13 +4554,23 @@ FIXTURE_VARIANT_ADD(layout3_fs, sysfs) {
45384554
.file_path = TMP_DIR "/kernel/notes",
45394555
};
45404556

4557+
FIXTURE_VARIANT_ADD(layout3_fs, hostfs) {
4558+
.mnt = {
4559+
.source = TMP_DIR,
4560+
.flags = MS_BIND,
4561+
},
4562+
.file_path = TMP_DIR "/dir/file",
4563+
.cwd_fs_magic = HOSTFS_SUPER_MAGIC,
4564+
};
4565+
45414566
FIXTURE_SETUP(layout3_fs)
45424567
{
45434568
struct stat statbuf;
45444569
const char *slash;
45454570
size_t dir_len;
45464571

4547-
if (!supports_filesystem(variant->mnt.type)) {
4572+
if (!supports_filesystem(variant->mnt.type) ||
4573+
!cwd_matches_fs(variant->cwd_fs_magic)) {
45484574
self->skip_test = true;
45494575
SKIP(return, "this filesystem is not supported (setup)");
45504576
}

0 commit comments

Comments
 (0)