Landlock support for UML
Subsystems (5)
Mickaël Salaün
maintainerv2 cover Landlock support for UML
Hi,
Commit cb2c7d1a1776 ("landlock: Support filesystem access-control") introduced a new ARCH_EPHEMERAL_INODES configuration, only enabled for User-Mode Linux. The reason was that UML's hostfs managed inodes in an ephemeral way: from the kernel point of view, the same inode struct could be created several times while being used by user space because the kernel didn't hold references to inodes. Because Landlock (and probably other subsystems) ties properties (i.e. access rights) to inode objects, it wasn't possible to create rules that match inodes and then allow specific accesses.
This patch series fixes the way UML manages inodes according to the underlying filesystem. They are now properly handles as for other filesystems, which enables to support Landlock (and probably other features).
Changes since v1:
https://lore.kernel.org/r/[email protected]
- Remove Cc stable@ (suggested by Richard).
- Add Acked-by: Richard Weinberger to the first patch.
- Split the test patch into two patches: one for the common pseudo-filesystems, and another patch dedicated to hostfs.
- Remove CONFIG_SECURITY_PATH because it is useless for merge_config.sh
- Move CONFIG_HOSTFS to a new config.um file.
- Fix commit message spelling and test warnings.
- Improve prepare_layout_opt() with remove_path() call to avoid cascading errors when some tested filesystems are not supported.
- Remove cgroup-v1 tests because this filesystem cannot really be mounted several times.
- Add test coverage with and without kernel debug code, according to GCC 12 and GCC 13.
Regards,
Mickaël Salaün (6):
hostfs: Fix ephemeral inodes selftests/landlock: Don't create useless file layouts selftests/landlock: Add supports_filesystem() helper selftests/landlock: Make mounts configurable selftests/landlock: Add tests for pseudo filesystems selftests/landlock: Add hostfs tests
arch/Kconfig | 7 - arch/um/Kconfig | 1 - fs/hostfs/hostfs.h | 1 + fs/hostfs/hostfs_kern.c | 213 ++++++------ fs/hostfs/hostfs_user.c | 1 + security/landlock/Kconfig | 2 +- tools/testing/selftests/landlock/config | 9 +- tools/testing/selftests/landlock/config.um | 1 + tools/testing/selftests/landlock/fs_test.c | 387 +++++++++++++++++++-- 9 files changed, 478 insertions(+), 144 deletions(-) create mode 100644 tools/testing/selftests/landlock/config.um
base-commit: 858fd168a95c5b9669aac8db6c14a9aeab446375
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerPATCH v2 1/6 hostfs: Fix ephemeral inodes
hostfs creates a new inode for each opened or created file, which created useless inode allocations and forbade identifying a host file with a kernel inode.
Fix this uncommon filesystem behavior by tying kernel inodes to host file's inode and device IDs. Even if the host filesystem inodes may be recycled, this cannot happen while a file referencing it is opened, which is the case with hostfs. It should be noted that hostfs inode IDs may not be unique for the same hostfs superblock because multiple host's (backed) superblocks may be used.
Delete inodes when dropping them to force backed host's file descriptors closing.
This enables to entirely remove ARCH_EPHEMERAL_INODES, and then makes Landlock fully supported by UML. This is very useful for testing changes.
These changes also factor out and simplify some helpers thanks to the new hostfs_inode_update() and the hostfs_iget() revamp: read_name(), hostfs_create(), hostfs_lookup(), hostfs_mknod(), and hostfs_fill_sb_common().
A following commit with new Landlock tests check this new hostfs inode consistency.
--- arch/Kconfig | 7 -- arch/um/Kconfig | 1 - fs/hostfs/hostfs.h | 1 + fs/hostfs/hostfs_kern.c | 213 +++++++++++++++++++------------------- fs/hostfs/hostfs_user.c | 1 + security/landlock/Kconfig | 2 +- 6 files changed, 109 insertions(+), 116 deletions(-)
arch/Kconfig +0 -7
@@ -1188,13 +1188,6 @@ config COMPAT_32BIT_TIME config ARCH_NO_PREEMPT bool -config ARCH_EPHEMERAL_INODES - def_bool n - help - An arch should select this symbol if it doesn't keep track of inode - instances on its own, but instead relies on something else (e.g. the - host kernel for an UML kernel). - config ARCH_SUPPORTS_RT bool
arch/um/Kconfig +0 -1
@@ -5,7 +5,6 @@ menu "UML-specific options" config UML bool default y - select ARCH_EPHEMERAL_INODES select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_KCOV
fs/hostfs/hostfs.h +1 -0
@@ -65,6 +65,7 @@ struct hostfs_stat {unsigned long long blocks;unsigned int maj;unsigned int min; +dev_t dev; };extern int stat_file (const char *path,struct hostfs_stat *p,int fd);
fs/hostfs/hostfs_kern.c +106 -107
332 lines not on this page · raw message
fs/hostfs/hostfs_user.c +1 -0
@@ -36,6 +36,7 @@ static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p) p->blocks = buf->st_blocks; p->maj =os_major (buf->st_rdev); p->min =os_minor (buf->st_rdev); + p->dev = buf->st_dev; }int stat_file (const char *path,struct hostfs_stat *p,int fd)
security/landlock/Kconfig +1 -1
@@ -2,7 +2,7 @@ config SECURITY_LANDLOCK bool "Landlock support" - depends on SECURITY && !ARCH_EPHEMERAL_INODES + depends on SECURITY select SECURITY_PATH help Landlock is a sandboxing mechanism that enables processes to restrict
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerPATCH v2 2/6 selftests/landlock: Don't create useless file layouts
In reply to Mickaël SalaünAdd and use a layout0 test fixture to not populate the tmpfs filesystem if it is not required for tests: unknown_access_rights, proc_nsfs, unpriv and max_layers.
This doesn't change these tests but it speeds up their setup and makes them less prone to error. This prepare the ground for a next commit.
--- tools/testing/selftests/landlock/fs_test.c | 26 +++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-)
tools/testing/selftests/landlock/fs_test.c +20 -6
@@ -231,6 +231,20 @@ static void cleanup_layout(struct __test_metadata *const _metadata)EXPECT_EQ (0 ,remove_path (TMP_DIR )); } +/* clang-format off */ +FIXTURE (layout0) {}; +/* clang-format on */ + +FIXTURE_SETUP (layout0) +{ +prepare_layout (_metadata); +} + +FIXTURE_TEARDOWN (layout0) +{ +cleanup_layout (_metadata); +} +static void create_layout1 (struct __test_metadata *const _metadata) {create_file (_metadata, file1_s1d1);
@@ -510,7 +524,7 @@ TEST_F_FORK(layout1, file_and_dir_access_rights)ASSERT_EQ (0 ,close (ruleset_fd)); } -TEST_F_FORK(layout1, unknown_access_rights) +TEST_F_FORK(layout0, unknown_access_rights) {__u64 access_mask;
@@ -608,7 +622,7 @@ static void enforce_ruleset(struct __test_metadata *const _metadata, } } -TEST_F_FORK(layout1, proc_nsfs) +TEST_F_FORK(layout0, proc_nsfs) {const struct rule rules[] = { {
@@ -657,11 +671,11 @@ TEST_F_FORK(layout1, proc_nsfs)ASSERT_EQ (0 ,close (path_beneath.parent_fd)); } -TEST_F_FORK(layout1, unpriv) +TEST_F_FORK(layout0, unpriv) {const struct rule rules[] = { { - .path = dir_s1d2, + .path =TMP_DIR , .access =ACCESS_RO , }, {},
@@ -1301,12 +1315,12 @@ TEST_F_FORK(layout1, inherit_superset)ASSERT_EQ (0 ,test_open (file1_s1d3,O_RDONLY )); } -TEST_F_FORK(layout1, max_layers) +TEST_F_FORK(layout0, max_layers) {int i, err;const struct rule rules[] = { { - .path = dir_s1d2, + .path =TMP_DIR , .access =ACCESS_RO , }, {},
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerPATCH v2 3/6 selftests/landlock: Add supports_filesystem() helper
In reply to Mickaël SalaünReplace supports_overlayfs() with supports_filesystem() to be able to check several filesystems. This will be useful in a following commit.
Only check for overlay filesystem once in the setup step, and then rely on self->skip_test.
--- tools/testing/selftests/landlock/fs_test.c | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-)
tools/testing/selftests/landlock/fs_test.c +23 -13
@@ -107,8 +107,10 @@ static bool fgrep(FILE *const inf, const char *const str)return false; } -static bool supports_overlayfs (void ) +static bool supports_filesystem (const char *const filesystem) { +char str[32 ]; +int len;bool res;FILE *const inf =fopen ("/proc/filesystems" ,"r" );
@@ -119,7 +121,12 @@ static bool supports_overlayfs(void)if (!inf)return true; - res =fgrep (inf,"nodev\toverlay\n" ); + len =snprintf (str,sizeof (str),"nodev\t%s\n" , filesystem); +if (len >=sizeof (str)) +/* Ignores too-long filesystem names. */ +return true; + + res =fgrep (inf, str);fclose (inf);return res; }
@@ -4044,14 +4051,17 @@ static const char (*merge_sub_files[])[] = {* └── work */ -/* clang-format off */ -FIXTURE (layout2_overlay) {}; -/* clang-format on */ +FIXTURE (layout2_overlay) +{ +bool skip_test; +};FIXTURE_SETUP (layout2_overlay) { -if (!supports_overlayfs ()) -SKIP (return,"overlayfs is not supported" ); +if (!supports_filesystem ("overlay" )) { + self->skip_test = true; +SKIP (return,"overlayfs is not supported (setup)" ); + }prepare_layout (_metadata);
@@ -4089,8 +4099,8 @@ FIXTURE_SETUP(layout2_overlay)FIXTURE_TEARDOWN (layout2_overlay) { -if (!supports_overlayfs ()) -SKIP (return,"overlayfs is not supported" ); +if (self->skip_test) +SKIP (return,"overlayfs is not supported (teardown)" );EXPECT_EQ (0 ,remove_path (lower_do1_fl3));EXPECT_EQ (0 ,remove_path (lower_dl1_fl2));
@@ -4123,8 +4133,8 @@ FIXTURE_TEARDOWN(layout2_overlay) TEST_F_FORK(layout2_overlay, no_restriction) { -if (!supports_overlayfs ()) -SKIP (return,"overlayfs is not supported" ); +if (self->skip_test) +SKIP (return,"overlayfs is not supported (test)" );ASSERT_EQ (0 ,test_open (lower_fl1,O_RDONLY ));ASSERT_EQ (0 ,test_open (lower_dl1,O_RDONLY ));
@@ -4289,8 +4299,8 @@ TEST_F_FORK(layout2_overlay, same_content_different_file)size_t i;const char *path_entry; -if (!supports_overlayfs ()) -SKIP (return,"overlayfs is not supported" ); +if (self->skip_test) +SKIP (return,"overlayfs is not supported (test)" );/* Sets rules on base directories (i.e. outside overlay scope). */ ruleset_fd =create_ruleset (_metadata,ACCESS_RW , layer1_base);
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerPATCH v2 4/6 selftests/landlock: Make mounts configurable
In reply to Mickaël SalaünAdd a new struct mnt_opt to define a mount point with the mount_opt() helper. This doesn't change tests but prepare for the next commit.
--- tools/testing/selftests/landlock/fs_test.c | 45 +++++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-)
tools/testing/selftests/landlock/fs_test.c +40 -5
@@ -213,7 +213,26 @@ static int remove_path(const char *const path)return err; } -static void prepare_layout (struct __test_metadata *const _metadata) +struct mnt_opt { +const char *const source; +const char *const type; +const unsigned long flags; +const char *const data; +}; + +const struct mnt_opt mnt_tmp = { + .type ="tmpfs" , + .data ="size=4m,mode=700" , +}; + +static int mount_opt (const struct mnt_opt *const mnt,const char *const target) +{ +return mount (mnt->source ?: mnt->type, target, mnt->type, mnt->flags, + mnt->data); +} + +static void prepare_layout_opt (struct __test_metadata *const _metadata, +const struct mnt_opt *const mnt) {disable_caps (_metadata);umask (0077 );
@@ -225,11 +244,27 @@ static void prepare_layout(struct __test_metadata *const _metadata)*/ set_cap (_metadata,CAP_SYS_ADMIN );ASSERT_EQ (0 ,unshare (CLONE_NEWNS )); -ASSERT_EQ (0 ,mount ("tmp" ,TMP_DIR ,"tmpfs" ,0 ,"size=4m,mode=700" )); + ASSERT_EQ(0, mount_opt(mnt, TMP_DIR)) + { +TH_LOG ("Failed to mount the %s filesystem: %s" , mnt->type, +strerror (errno)); +/* +* FIXTURE_TEARDOWN() is not called when FIXTURE_SETUP() +* failed, so we need to explicitly do a minimal cleanup to +* avoid cascading errors with other tests that don't depend on +* the same filesystem. +*/ +remove_path (TMP_DIR ); + }ASSERT_EQ (0 ,mount (NULL ,TMP_DIR ,NULL ,MS_PRIVATE |MS_REC ,NULL ));clear_cap (_metadata,CAP_SYS_ADMIN ); } +static void prepare_layout (struct __test_metadata *const _metadata) +{ +prepare_layout_opt (_metadata, &mnt_tmp); +} +static void cleanup_layout (struct __test_metadata *const _metadata) {set_cap (_metadata,CAP_SYS_ADMIN );
@@ -269,7 +304,7 @@ static void create_layout1(struct __test_metadata *const _metadata)create_file (_metadata, file1_s3d1);create_directory (_metadata, dir_s3d2);set_cap (_metadata,CAP_SYS_ADMIN ); -ASSERT_EQ (0 ,mount ("tmp" , dir_s3d2,"tmpfs" ,0 ,"size=4m,mode=700" )); +ASSERT_EQ (0 ,mount_opt (&mnt_tmp, dir_s3d2));clear_cap (_metadata,CAP_SYS_ADMIN );ASSERT_EQ (0 ,mkdir (dir_s3d3,0700 ));
@@ -4068,7 +4103,7 @@ FIXTURE_SETUP(layout2_overlay)create_directory (_metadata,LOWER_BASE );set_cap (_metadata,CAP_SYS_ADMIN );/* Creates tmpfs mount points to get deterministic overlayfs. */ -ASSERT_EQ (0 ,mount ("tmp" ,LOWER_BASE ,"tmpfs" ,0 ,"size=4m,mode=700" )); +ASSERT_EQ (0 ,mount_opt (&mnt_tmp,LOWER_BASE ));clear_cap (_metadata,CAP_SYS_ADMIN );create_file (_metadata, lower_fl1);create_file (_metadata, lower_dl1_fl2);
@@ -4078,7 +4113,7 @@ FIXTURE_SETUP(layout2_overlay)create_directory (_metadata,UPPER_BASE );set_cap (_metadata,CAP_SYS_ADMIN ); -ASSERT_EQ (0 ,mount ("tmp" ,UPPER_BASE ,"tmpfs" ,0 ,"size=4m,mode=700" )); +ASSERT_EQ (0 ,mount_opt (&mnt_tmp,UPPER_BASE ));clear_cap (_metadata,CAP_SYS_ADMIN );create_file (_metadata, upper_fu1);create_file (_metadata, upper_du1_fu2);
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerPATCH v2 5/6 selftests/landlock: Add tests for pseudo filesystems
In reply to Mickaël SalaünAdd generic and read-only tests for 6 pseudo filesystems to make sure they have a consistent inode management, which is required for Landlock's file hierarchy identification:
- tmpfs
- ramfs
- cgroup2
- proc
- sysfs
Update related kernel configuration to support these new filesystems, remove useless CONFIG_SECURITY_PATH, and sort all entries. If these filesystems are not supported by the kernel running tests, the related tests are skipped.
Expanding variants, this adds 25 new tests for layout3_fs:
- tag_inode_dir_parent
- tag_inode_dir_mnt
- tag_inode_dir_child
- tag_inode_dir_file
- release_inodes
Test coverage for security/landlock with kernel debug code:
- 94.7% of 835 lines according to gcc/gcov-12
- 93.0% of 852 lines according to gcc/gcov-13
Test coverage for security/landlock without kernel debug code:
- 95.5% of 624 lines according to gcc/gcov-12
- 93.1% of 641 lines according to gcc/gcov-13
--- tools/testing/selftests/landlock/config | 9 +- tools/testing/selftests/landlock/fs_test.c | 254 ++++++++++++++++++++- 2 files changed, 259 insertions(+), 4 deletions(-)
tools/testing/selftests/landlock/config +6 -3
@@ -1,7 +1,10 @@ +CONFIG_CGROUPS=y +CONFIG_CGROUP_SCHED=y CONFIG_OVERLAY_FS=y -CONFIG_SECURITY_LANDLOCK=y -CONFIG_SECURITY_PATH=y +CONFIG_PROC_FS=y CONFIG_SECURITY=y +CONFIG_SECURITY_LANDLOCK=y CONFIG_SHMEM=y -CONFIG_TMPFS_XATTR=y +CONFIG_SYSFS=y CONFIG_TMPFS=y +CONFIG_TMPFS_XATTR=y
tools/testing/selftests/landlock/fs_test.c +253 -1
281 lines not on this page · raw message
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerPATCH v2 6/6 selftests/landlock: Add hostfs tests
In reply to Mickaël SalaünAdd 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.
--- tools/testing/selftests/landlock/config.um | 1 + tools/testing/selftests/landlock/fs_test.c | 28 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/landlock/config.um
tools/testing/selftests/landlock/config.um new file +1 -0
@@ -0,0 +1 @@ +CONFIG_HOSTFS=y
tools/testing/selftests/landlock/fs_test.c +27 -1
@@ -10,6 +10,7 @@#define _GNU_SOURCE#include <fcntl.h> #include <linux/landlock.h> +#include <linux/magic.h> #include <sched.h> #include <stdio.h> #include <string.h>
@@ -19,6 +20,7 @@#include <sys/sendfile.h> #include <sys/stat.h> #include <sys/sysmacros.h> +#include <sys/vfs.h> #include <unistd.h> #include "common.h"
@@ -135,6 +137,19 @@ static bool supports_filesystem(const char *const filesystem)return res; } +static bool cwd_matches_fs (unsigned int fs_magic) +{ +struct statfs statfs_buf; + +if (!fs_magic) +return true; + +if (statfs ("." , &statfs_buf)) +return true; + +return statfs_buf.f_type == fs_magic; +} +static void mkdir_parents (struct __test_metadata *const _metadata,const char *const path) {
@@ -4500,6 +4515,7 @@ FIXTURE_VARIANT(layout3_fs) {const struct mnt_opt mnt;const char *const file_path; +unsigned int cwd_fs_magic; };/* clang-format off */
@@ -4538,13 +4554,23 @@ FIXTURE_VARIANT_ADD(layout3_fs, sysfs) { .file_path = TMP_DIR "/kernel/notes", }; +FIXTURE_VARIANT_ADD(layout3_fs, hostfs) { + .mnt = { + .source = TMP_DIR, + .flags = MS_BIND, + }, + .file_path = TMP_DIR "/dir/file", + .cwd_fs_magic = HOSTFS_SUPER_MAGIC, +}; +FIXTURE_SETUP (layout3_fs) {struct stat statbuf;const char *slash;size_t dir_len; -if (!supports_filesystem (variant->mnt.type)) { +if (!supports_filesystem (variant->mnt.type) || + !cwd_matches_fs (variant->cwd_fs_magic)) { self->skip_test = true;SKIP (return,"this filesystem is not supported (setup)" ); }
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerI've push this series into -next. Thanks Richard!
Quoted 57 lines · Mickaël Salaün
Hi,
Commit
cb2c7d1a1776("landlock: Support filesystem access-control") introduced a newARCH_EPHEMERAL_INODESconfiguration, only enabled for User-Mode Linux. The reason was that UML's hostfs managed inodes in an ephemeral way: from the kernel point of view, the same inode struct could be created several times while being used by user space because the kernel didn't hold references to inodes. Because Landlock (and probably other subsystems) ties properties (i.e. access rights) to inode objects, it wasn't possible to create rules that match inodes and then allow specific accesses.This patch series fixes the way UML manages inodes according to the underlying filesystem. They are now properly handles as for other filesystems, which enables to support Landlock (and probably other features).
Changes since v1:
https://lore.kernel.org/r/[email protected]
- Remove Cc stable@ (suggested by Richard).
- Add Acked-by: Richard Weinberger to the first patch.
- Split the test patch into two patches: one for the common pseudo-filesystems, and another patch dedicated to hostfs.
- RemoveCONFIG_SECURITY_PATHbecause it is useless formerge_config.sh
- MoveCONFIG_HOSTFSto a new config.um file.
- Fix commit message spelling and test warnings.
- Improveprepare_layout_opt()withremove_path()call to avoid cascading errors when some tested filesystems are not supported.
- Remove cgroup-v1 tests because this filesystem cannot really be mounted several times.
- Add test coverage with and without kernel debug code, according to GCC 12 and GCC 13.Regards,
Mickaël Salaün (6):
hostfs: Fix ephemeral inodes selftests/landlock: Don't create useless file layouts selftests/landlock: Addsupports_filesystem()helper selftests/landlock: Make mounts configurable selftests/landlock: Add tests for pseudo filesystems selftests/landlock: Add hostfs testsarch/Kconfig | 7 - arch/um/Kconfig | 1 - fs/hostfs/hostfs.h | 1 + fs/hostfs/hostfs_kern.c | 213 ++++++------ fs/hostfs/hostfs_user.c | 1 + security/landlock/Kconfig | 2 +- tools/testing/selftests/landlock/config | 9 +- tools/testing/selftests/landlock/config.um | 1 + tools/testing/selftests/landlock/fs_test.c | 387 +++++++++++++++++++-- 9 files changed, 478 insertions(+), 144 deletions(-)create mode 100644
tools/testing/selftests/landlock/config.umbase-commit:
858fd168a95c5b9669aac8db6c14a9aeab446375