Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+28 -1
+1
tools/testing/selftests/landlock/config.um
··· 1 + CONFIG_HOSTFS=y
+27 -1
tools/testing/selftests/landlock/fs_test.c
··· 10 10 #define _GNU_SOURCE 11 11 #include <fcntl.h> 12 12 #include <linux/landlock.h> 13 + #include <linux/magic.h> 13 14 #include <sched.h> 14 15 #include <stdio.h> 15 16 #include <string.h> ··· 20 19 #include <sys/sendfile.h> 21 20 #include <sys/stat.h> 22 21 #include <sys/sysmacros.h> 22 + #include <sys/vfs.h> 23 23 #include <unistd.h> 24 24 25 25 #include "common.h" ··· 135 133 res = fgrep(inf, str); 136 134 fclose(inf); 137 135 return res; 136 + } 137 + 138 + static bool cwd_matches_fs(unsigned int fs_magic) 139 + { 140 + struct statfs statfs_buf; 141 + 142 + if (!fs_magic) 143 + return true; 144 + 145 + if (statfs(".", &statfs_buf)) 146 + return true; 147 + 148 + return statfs_buf.f_type == fs_magic; 138 149 } 139 150 140 151 static void mkdir_parents(struct __test_metadata *const _metadata, ··· 4515 4500 { 4516 4501 const struct mnt_opt mnt; 4517 4502 const char *const file_path; 4503 + unsigned int cwd_fs_magic; 4518 4504 }; 4519 4505 4520 4506 /* clang-format off */ ··· 4554 4538 .file_path = TMP_DIR "/kernel/notes", 4555 4539 }; 4556 4540 4541 + FIXTURE_VARIANT_ADD(layout3_fs, hostfs) { 4542 + .mnt = { 4543 + .source = TMP_DIR, 4544 + .flags = MS_BIND, 4545 + }, 4546 + .file_path = TMP_DIR "/dir/file", 4547 + .cwd_fs_magic = HOSTFS_SUPER_MAGIC, 4548 + }; 4549 + 4557 4550 FIXTURE_SETUP(layout3_fs) 4558 4551 { 4559 4552 struct stat statbuf; 4560 4553 const char *slash; 4561 4554 size_t dir_len; 4562 4555 4563 - if (!supports_filesystem(variant->mnt.type)) { 4556 + if (!supports_filesystem(variant->mnt.type) || 4557 + !cwd_matches_fs(variant->cwd_fs_magic)) { 4564 4558 self->skip_test = true; 4565 4559 SKIP(return, "this filesystem is not supported (setup)"); 4566 4560 }