Skip to contentSkip to conversation

Landlock filesystem caching

PATCH New Mickaël Salaün 1 participant 5 messages Jun 30, 2021

Subsystems (4)
lore
Share & tools
lore

Mickaël Salaün

author maintainer
#0 lore raw

cover Landlock filesystem caching

Hi,

The goal of this patch series is to reduce the performance impact of walking through a lot of files while being landlocked. Indeed, because of the unprivileged nature of Landlock, each file access implies to check access granted to each directory of the path, which slows down open time.

Currently, openat(2) calls spend more than 22% of their time in hook_file_open(). The performance impact for a common worth case scenario is significantly reduced thanks to this patch series, theoretically going from O(n) with n as the depth of a path, to O(1) (cf. benchmarks in the caching patch).

This series adds a new security hook (resolve_path_at) and uses it to implement access caching in Landlock. I'm planning to build on top of that for other improvements (using task's working directory and task's root directory) but that will require other hook changes.

This new hook is also a first step to be able to securely restrict file descriptors used for path resolution (e.g. dirfd in openat2).

Caching may be difficult to get right especially for security checks. I extended the current tests and I'm still working on new ones. If you have test/attack scenarios, please share them. I would really appreciate constructive reviews for these critical changes. This series can be applied on top of v5.13 .

Regards,

Mickaël Salaün (4):
fs,security: Add resolve_path_at() hook
landlock: Add filesystem rule caching selftests/landlock: Work in a temporary directory selftests/landlock: Check all possible intermediate directories

 fs/namei.c                                 |   9 +
 include/linux/lsm_hook_defs.h              |   2 +
 include/linux/lsm_hooks.h                  |   8 +
 include/linux/security.h                   |   9 +
 security/landlock/cache.h                  |  77 +++++++
 security/landlock/cred.c                   |  15 +-
 security/landlock/cred.h                   |  20 +-
 security/landlock/fs.c                     | 224 +++++++++++++++++++--
 security/landlock/fs.h                     |  29 +++
 security/landlock/setup.c                  |   2 +
 security/security.c                        |   6 +
 tools/testing/selftests/landlock/fs_test.c | 205 ++++++++++++++-----
 12 files changed, 544 insertions(+), 62 deletions(-)
 create mode 100644 security/landlock/cache.h

base-commit: 62fb9874f5da54fdb243003b386128037319b219

Signature · 1 line
2.32.0
author maintainer
#1 lore raw

PATCH 1/4 fs,security: Add resolve_path_at() hook

From: Mickaël Salaün <[email protected]>

This new resolve_path_at() security hook enables checking absolute and relative pathname resolutions according to the task's root directory, the task's working directory, or an open file descriptor (e.g. thanks to *at() syscalls).

It is useful to (efficiently) restrict access to file hierarchies (e.g. leveraging openat2()'s resolve flags) according to file descriptors, and to improve path access check time, which is used by Landlock in the following commit.

---
 fs/namei.c                    | 9 +++++++++
 include/linux/lsm_hook_defs.h | 2 ++
 include/linux/lsm_hooks.h     | 8 ++++++++
 include/linux/security.h      | 9 +++++++++
 security/security.c           | 6 ++++++
 5 files changed, 34 insertions(+)
fs/namei.c +9 -0
index 79b0ff9b151e..6efdd0b8e2b1 100644
@@ -2331,6 +2331,9 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 		error = nd_jump_root(nd);
 		if (unlikely(error))
 			return ERR_PTR(error);
+		error = security_resolve_path_at(&nd->path, NULL, flags);
+		if (error)
+			return ERR_PTR(error);
 		return s;
 	}
 
@@ -2350,6 +2353,9 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 			get_fs_pwd(current->fs, &nd->path);
 			nd->inode = nd->path.dentry->d_inode;
 		}
+		error = security_resolve_path_at(&nd->path, NULL, flags);
+		if (error)
+			return ERR_PTR(error);
 	} else {
 		/* Caller must check execute permissions on the starting path component */
 		struct fd f = fdget_raw(nd->dfd);
@@ -2373,7 +2379,10 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 			path_get(&nd->path);
 			nd->inode = nd->path.dentry->d_inode;
 		}
+		error = security_resolve_path_at(&nd->path, f.file, flags);
 		fdput(f);
+		if (error)
+			return ERR_PTR(error);
 	}
 
 	/* For scoped-lookups we need to set the root to the dirfd as well. */
include/linux/lsm_hook_defs.h +2 -0
index 04c01794de83..1db6d53b1dfb 100644
@@ -178,6 +178,8 @@ LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk,
 	 struct fown_struct *fown, int sig)
 LSM_HOOK(int, 0, file_receive, struct file *file)
 LSM_HOOK(int, 0, file_open, struct file *file)
+LSM_HOOK(int, 0, resolve_path_at, const struct path *path_at,
+	 struct file *file_at, int lookup_flags)
 LSM_HOOK(int, 0, task_alloc, struct task_struct *task,
 	 unsigned long clone_flags)
 LSM_HOOK(void, LSM_RET_VOID, task_free, struct task_struct *task)
include/linux/lsm_hooks.h +8 -0
index 5c4c5c0602cb..2ef130e36309 100644
@@ -600,6 +600,14 @@
  *	Save open-time permission checking state for later use upon
  *	file_permission, and recheck access if anything has changed
  *	since inode_permission.
+ * @resolve_path_at:
+ *	Check path resolution from a file descriptor, or the current working
+ *      directory, or the current root directory.
+ *      Can be called in RCU read-side critical section.
+ *      @path_at points to the base path.
+ *      @file_at can point to the file descriptor used to resolve the path, or
+ *      be NULL for AT_FDCWD.
+ *      @lookup_flags contains the lookup options (e.g. LOOKUP_IS_SCOPED).
  *
  * Security hooks for task operations.
  *
include/linux/security.h +9 -0
index 06f7c50ce77f..e3ae93b9189d 100644
@@ -391,6 +391,8 @@ int security_file_send_sigiotask(struct task_struct *tsk,
 				 struct fown_struct *fown, int sig);
 int security_file_receive(struct file *file);
 int security_file_open(struct file *file);
+int security_resolve_path_at(const struct path *path_at, struct file *file_at,
+		int lookup_flags);
 int security_task_alloc(struct task_struct *task, unsigned long clone_flags);
 void security_task_free(struct task_struct *task);
 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp);
@@ -1011,6 +1013,13 @@ static inline int security_file_open(struct file *file)
 	return 0;
 }
 
+static inline int security_resolve_path_at(const struct path *path_at,
+					   struct file *file_at,
+					   int lookup_flags)
+{
+	return 0;
+}
+
 static inline int security_task_alloc(struct task_struct *task,
 				      unsigned long clone_flags)
 {
security/security.c +6 -0
index b38155b2de83..98161f5919ee 100644
@@ -1637,6 +1637,12 @@ int security_file_open(struct file *file)
 	return fsnotify_perm(file, MAY_OPEN);
 }
 
+int security_resolve_path_at(const struct path *path_at, struct file *file_at,
+		int lookup_flags)
+{
+	return call_int_hook(resolve_path_at, 0, path_at, file_at, lookup_flags);
+}
+
 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
 {
 	int rc = lsm_task_alloc(task);
Signature · 1 line
2.32.0
author maintainer
#2 lore raw

PATCH 2/4 landlock: Add filesystem rule caching

In reply to Mickaël Salaün

From: Mickaël Salaün <[email protected]>

Because of the unprivileged nature of Landlock, the main issue with the current implementation is that path access checks require to walk from the leaf file to the (real) root directory for each open-like syscall. To limit this path walk, leverage the new resolve_path_at() hook to reuse cached accesses collected for files at open time, when these files are then used for relative path resolution (e.g. dirfd in openat()-like syscalls).

Indeed, in practice, software walking through the filesystem (e.g. "find" tool) trigger a lot of access checks. However, efficient walk implementations reuse opened directory file descriptors to continue the walk to children directories. Being able to cache such access checks leads to significant performance improvements.

This cache mechanism is implemented thanks to new Landlock security blobs attached to tasks and files. Each landlocked task keeps a (weak) reference to the last file used to open a path. If the referenced base path matches a directory in the current path walk and if the access request was already granted to the base path, then the path walk stops early and access is granted.

Each file opened by a landlocked task gets a cache that will be valid as long as the backing struct file exists. This cache also references the domain for which the cached accesses are valid.

Running the "find" tool on a directory with Linux source files (version 5.13, which contains a depth of less than 10 nested source directories) is used as a metric because it is a real-life worse case that requires to walk through a lot of files in different directories. To highlight the measured performance impact, we put these source files in directories of different depth: 1, 10 and 20. Here are the times taken by Landlock for an open syscall after 10,000 iterations with the mainline kernel and the caching changes:

Depth of 1: /0/
- v5.13: 28%
- this patch: 22%

Depth of 10: /0/1/.../9/
- v5.13: 43%
- this patch: 25%

Depth of 20: /0/1/.../19/
- v5.13: 54%
- this patch: 30%

The performance impact for such worth case is significantly reduced:
theoretically going from O(n) with n as the depth of a path, to O(1), but as we can see there is still an increased penalty with the depth. This may be explained with the relative ".." resolutions that don't benefit from this caching. As a side note, the getdents64 syscall (not impacted by Landlock) takes 18 times longer than the openat syscall in is this scenario.

---
 security/landlock/cache.h |  77 +++++++++++++
 security/landlock/cred.c  |  15 ++-
 security/landlock/cred.h  |  20 +++-
 security/landlock/fs.c    | 224 +++++++++++++++++++++++++++++++++++---
 security/landlock/fs.h    |  29 +++++
 security/landlock/setup.c |   2 +
 6 files changed, 350 insertions(+), 17 deletions(-)
 create mode 100644 security/landlock/cache.h
security/landlock/cache.h new file +77 -0
new file mode 100644
index 000000000000..7ceaa3b15671
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - Generic cache management
+ *
+ * Copyright © 2021 Microsoft Corporation
+ */
+
+#ifndef _SECURITY_LANDLOCK_CACHE_H
+#define _SECURITY_LANDLOCK_CACHE_H
+
+#include <linux/compiler.h>
+#include <linux/refcount.h>
+#include <linux/types.h>
+
+#include "ruleset.h"
+
+/**
+ * struct landlock_cache - Generic access cache for an object
+ *
+ * Store cached access rights for a Landlock object (i.e. tied to specific
+ * domain).  Allowed accesses are set once (e.g. at file opening) and never
+ * change after that.  As a side effect, this means that such cache created
+ * before a domain transition will not get an up to date allowed accesses.
+ * This implies to always check a cached domain against the current domain
+ * thanks to landlock_cache_is_valid().
+ *
+ * This struct is part of a typed cache (e.g. &struct landlock_fs_cache.core)
+ * that identifies the tied object.
+ */
+struct landlock_cache {
+	/**
+	 * @dangling_domain: If not NULL, points to the domain for which
+	 * @allowed_accesses is valid.  This brings two constraints:
+	 * - @dangling_domain must only be read with READ_ONCE() and written
+	 *   with WRITE_ONCE() (except at initialization).
+	 * - @dangling_domain can only be safely dereferenced by the cache
+	 *   owner (e.g. with landlock_disable_cache() when the underlying file
+	 *   is being closed).
+	 */
+	void *dangling_domain;
+	/**
+	 * @usage: This counter is used to keep a cache alive while it can
+	 * still be checked against.
+	 */
+	refcount_t usage;
+	/**
+	 * @allowed_accesses: Mask of absolute known-to-be allowed accesses to
+	 * an object at creation-time (e.g. at open-time for the file hierarchy
+	 * of a file descriptor).  A bit not set doesn't mean that the related
+	 * access is denied.  The type of access is inferred from the type of
+	 * the related object.  The task domain may not be the same as the
+	 * cached one and they must then be checked against each other when
+	 * evaluating @allowed_accesses thanks to landlock_cache_is_valid().
+	 */
+	u16 allowed_accesses;
+};
+
+static inline void landlock_disable_cache(struct landlock_cache *cache)
+{
+	struct landlock_ruleset *const dom = cache->dangling_domain;
+
+	/* Atomically marks the cache as disabled. */
+	WRITE_ONCE(cache->dangling_domain, NULL);
+	/*
+	 * There is no need for other synchronisation mechanism because the
+	 * domain is never dereferenced elsewhere.
+	 */
+	landlock_put_ruleset(dom);
+}
+
+static inline bool landlock_cache_is_valid(const struct landlock_cache *cache,
+		const struct landlock_ruleset *domain)
+{
+	return (domain && domain == READ_ONCE(cache->dangling_domain));
+}
+
+#endif /* _SECURITY_LANDLOCK_CACHE_H */
security/landlock/cred.c +14 -1
index 6725af24c684..8c9408dd46e1 100644
@@ -1,16 +1,19 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * Landlock LSM - Credential hooks
+ * Landlock LSM - Credential and task hooks
  *
  * Copyright © 2017-2020 Mickaël Salaün <[email protected]>
  * Copyright © 2018-2020 ANSSI
+ * Copyright © 2021 Microsoft Corporation
  */
 
 #include <linux/cred.h>
 #include <linux/lsm_hooks.h>
+#include <linux/sched.h>
 
 #include "common.h"
 #include "cred.h"
+#include "fs.h"
 #include "ruleset.h"
 #include "setup.h"
 
@@ -34,9 +37,19 @@ static void hook_cred_free(struct cred *const cred)
 		landlock_put_ruleset_deferred(dom);
 }
 
+static void hook_task_free(struct task_struct *const task)
+{
+	struct landlock_fs_cache *const last_at_cache =
+		landlock_task(task)->cache.last_at;
+
+	landlock_put_fs_cache(last_at_cache);
+}
+
 static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(cred_prepare, hook_cred_prepare),
 	LSM_HOOK_INIT(cred_free, hook_cred_free),
+
+	LSM_HOOK_INIT(task_free, hook_task_free),
 };
 
 __init void landlock_add_cred_hooks(void)
security/landlock/cred.h +18 -2
index 5f99d3decade..0734a14933c1 100644
@@ -1,9 +1,10 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /*
- * Landlock LSM - Credential hooks
+ * Landlock LSM - Credential and task hooks
  *
  * Copyright © 2019-2020 Mickaël Salaün <[email protected]>
  * Copyright © 2019-2020 ANSSI
+ * Copyright © 2021 Microsoft Corporation
  */
 
 #ifndef _SECURITY_LANDLOCK_CRED_H
@@ -13,6 +14,7 @@
 #include <linux/init.h>
 #include <linux/rcupdate.h>
 
+#include "fs.h"
 #include "ruleset.h"
 #include "setup.h"
 
@@ -20,13 +22,27 @@ struct landlock_cred_security {
 	struct landlock_ruleset *domain;
 };
 
+struct landlock_task_cache {
+	struct landlock_fs_cache *last_at;
+};
+
+struct landlock_task_security {
+	struct landlock_task_cache cache;
+};
+
 static inline struct landlock_cred_security *landlock_cred(
 		const struct cred *cred)
 {
 	return cred->security + landlock_blob_sizes.lbs_cred;
 }
 
-static inline const struct landlock_ruleset *landlock_get_current_domain(void)
+static inline struct landlock_task_security *landlock_task(
+		const struct task_struct *task)
+{
+	return task->security + landlock_blob_sizes.lbs_task;
+}
+
+static inline struct landlock_ruleset *landlock_get_current_domain(void)
 {
 	return landlock_cred(current_cred())->domain;
 }
security/landlock/fs.c +210 -14

334 lines not on this page · raw message

security/landlock/fs.h +29 -0
index 187284b421c9..2c57406f73b4 100644
@@ -4,6 +4,7 @@
  *
  * Copyright © 2017-2020 Mickaël Salaün <[email protected]>
  * Copyright © 2018-2020 ANSSI
+ * Copyright © 2021 Microsoft Corporation
  */
 
 #ifndef _SECURITY_LANDLOCK_FS_H
@@ -12,7 +13,9 @@
 #include <linux/fs.h>
 #include <linux/init.h>
 #include <linux/rcupdate.h>
+#include <linux/refcount.h>
 
+#include "object.h"
 #include "ruleset.h"
 #include "setup.h"
 
@@ -50,6 +53,24 @@ struct landlock_superblock_security {
 	atomic_long_t inode_refs;
 };
 
+struct landlock_fs_cache;
+
+/**
+ * struct landlock_file_security - File security blob
+ *
+ * Cached access right for an open file.
+ */
+struct landlock_file_security {
+	/**
+	 * @cache: This cache is set once at the file opening and never change
+	 * after that (except when freeing the file).  This means that a file
+	 * open before a domain transition will not get an updated cache, but
+	 * the domain tied to the cache must always be checked with
+	 * landlock_cache_is_valid().
+	 */
+	struct landlock_fs_cache *cache;
+};
+
 static inline struct landlock_inode_security *landlock_inode(
 		const struct inode *const inode)
 {
@@ -62,8 +83,16 @@ static inline struct landlock_superblock_security *landlock_superblock(
 	return superblock->s_security + landlock_blob_sizes.lbs_superblock;
 }
 
+static inline struct landlock_file_security *landlock_file(
+		const struct file *const file)
+{
+	return file->f_security + landlock_blob_sizes.lbs_file;
+}
+
 __init void landlock_add_fs_hooks(void);
 
+void landlock_put_fs_cache(struct landlock_fs_cache *cache);
+
 int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
 		const struct path *const path, u32 access_hierarchy);
 
security/landlock/setup.c +2 -0
index f8e8e980454c..42d381dd0f0c 100644
@@ -19,8 +19,10 @@ bool landlock_initialized __lsm_ro_after_init = false;
 
 struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = {
 	.lbs_cred = sizeof(struct landlock_cred_security),
+	.lbs_file = sizeof(struct landlock_file_security),
 	.lbs_inode = sizeof(struct landlock_inode_security),
 	.lbs_superblock = sizeof(struct landlock_superblock_security),
+	.lbs_task = sizeof(struct landlock_task_security),
 };
 
 static int __init landlock_init(void)
Signature · 1 line
2.32.0
author maintainer
#3 lore raw

PATCH 3/4 selftests/landlock: Work in a temporary directory

In reply to Mickaël Salaün

From: Mickaël Salaün <[email protected]>

To be able to test the current working directory, run all tests in a temporary directory instead of in its parent directory. This is required for the following commit.

---
 tools/testing/selftests/landlock/fs_test.c | 65 ++++++++++++----------
 1 file changed, 35 insertions(+), 30 deletions(-)
tools/testing/selftests/landlock/fs_test.c +35 -30
index 10c9a1e4ebd9..403c8255311f 100644
@@ -23,31 +23,31 @@
 #include "common.h"
 
 #define TMP_DIR		"tmp"
-#define BINARY_PATH	"./true"
+#define BINARY_PATH	"../true"
 
 /* Paths (sibling number and depth) */
-static const char dir_s1d1[] = TMP_DIR "/s1d1";
-static const char file1_s1d1[] = TMP_DIR "/s1d1/f1";
-static const char file2_s1d1[] = TMP_DIR "/s1d1/f2";
-static const char dir_s1d2[] = TMP_DIR "/s1d1/s1d2";
-static const char file1_s1d2[] = TMP_DIR "/s1d1/s1d2/f1";
-static const char file2_s1d2[] = TMP_DIR "/s1d1/s1d2/f2";
-static const char dir_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3";
-static const char file1_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f1";
-static const char file2_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f2";
-
-static const char dir_s2d1[] = TMP_DIR "/s2d1";
-static const char file1_s2d1[] = TMP_DIR "/s2d1/f1";
-static const char dir_s2d2[] = TMP_DIR "/s2d1/s2d2";
-static const char file1_s2d2[] = TMP_DIR "/s2d1/s2d2/f1";
-static const char dir_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3";
-static const char file1_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f1";
-static const char file2_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f2";
-
-static const char dir_s3d1[] = TMP_DIR "/s3d1";
+static const char dir_s1d1[] = "./s1d1";
+static const char file1_s1d1[] = "./s1d1/f1";
+static const char file2_s1d1[] = "./s1d1/f2";
+static const char dir_s1d2[] = "./s1d1/s1d2";
+static const char file1_s1d2[] = "./s1d1/s1d2/f1";
+static const char file2_s1d2[] = "./s1d1/s1d2/f2";
+static const char dir_s1d3[] = "./s1d1/s1d2/s1d3";
+static const char file1_s1d3[] = "./s1d1/s1d2/s1d3/f1";
+static const char file2_s1d3[] = "./s1d1/s1d2/s1d3/f2";
+
+static const char dir_s2d1[] = "./s2d1";
+static const char file1_s2d1[] = "./s2d1/f1";
+static const char dir_s2d2[] = "./s2d1/s2d2";
+static const char file1_s2d2[] = "./s2d1/s2d2/f1";
+static const char dir_s2d3[] = "./s2d1/s2d2/s2d3";
+static const char file1_s2d3[] = "./s2d1/s2d2/s2d3/f1";
+static const char file2_s2d3[] = "./s2d1/s2d2/s2d3/f2";
+
+static const char dir_s3d1[] = "./s3d1";
 /* dir_s3d2 is a mount point. */
-static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2";
-static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
+static const char dir_s3d2[] = "./s3d1/s3d2";
+static const char dir_s3d3[] = "./s3d1/s3d2/s3d3";
 
 /*
  * layout1 hierarchy:
@@ -140,11 +140,12 @@ static int remove_path(const char *const path)
 		walker[i] = '\0';
 		ret = rmdir(walker);
 		if (ret) {
-			if (errno != ENOTEMPTY && errno != EBUSY)
+			if (errno != ENOTEMPTY && errno != EBUSY
+					&& errno != EINVAL)
 				err = errno;
 			goto out;
 		}
-		if (strcmp(walker, TMP_DIR) == 0)
+		if (strcmp(walker, ".") == 0)
 			goto out;
 	}
 
@@ -168,10 +169,14 @@ static void prepare_layout(struct __test_metadata *const _metadata)
 	ASSERT_EQ(0, mount("tmp", TMP_DIR, "tmpfs", 0, "size=4m,mode=700"));
 	ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL));
 	clear_cap(_metadata, CAP_SYS_ADMIN);
+
+	ASSERT_EQ(0, chdir(TMP_DIR));
 }
 
 static void cleanup_layout(struct __test_metadata *const _metadata)
 {
+	EXPECT_EQ(0, chdir(".."));
+
 	set_cap(_metadata, CAP_SYS_ADMIN);
 	EXPECT_EQ(0, umount(TMP_DIR));
 	clear_cap(_metadata, CAP_SYS_ADMIN);
@@ -1370,7 +1375,7 @@ static void test_relative_path(struct __test_metadata *const _metadata,
 	 */
 	const struct rule layer1_base[] = {
 		{
-			.path = TMP_DIR,
+			.path = ".",
 			.access = ACCESS_RO,
 		},
 		{}
@@ -2095,8 +2100,8 @@ FIXTURE_TEARDOWN(layout1_bind)
 	cleanup_layout(_metadata);
 }
 
-static const char bind_dir_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3";
-static const char bind_file1_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3/f1";
+static const char bind_dir_s1d3[] = "./s2d1/s2d2/s1d3";
+static const char bind_file1_s1d3[] = "./s2d1/s2d2/s1d3/f1";
 
 /*
  * layout1_bind hierarchy:
@@ -2282,7 +2287,7 @@ TEST_F_FORK(layout1_bind, same_content_same_file)
 	ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY));
 }
 
-#define LOWER_BASE	TMP_DIR "/lower"
+#define LOWER_BASE	"./lower"
 #define LOWER_DATA	LOWER_BASE "/data"
 static const char lower_fl1[] = LOWER_DATA "/fl1";
 static const char lower_dl1[] = LOWER_DATA "/dl1";
@@ -2309,7 +2314,7 @@ static const char (*lower_sub_files[])[] = {
 	NULL
 };
 
-#define UPPER_BASE	TMP_DIR "/upper"
+#define UPPER_BASE	"./upper"
 #define UPPER_DATA	UPPER_BASE "/data"
 #define UPPER_WORK	UPPER_BASE "/work"
 static const char upper_fu1[] = UPPER_DATA "/fu1";
@@ -2337,7 +2342,7 @@ static const char (*upper_sub_files[])[] = {
 	NULL
 };
 
-#define MERGE_BASE	TMP_DIR "/merge"
+#define MERGE_BASE	"./merge"
 #define MERGE_DATA	MERGE_BASE "/data"
 static const char merge_fl1[] = MERGE_DATA "/fl1";
 static const char merge_dl1[] = MERGE_DATA "/dl1";
Signature · 1 line
2.32.0
author maintainer
#4 lore raw

PATCH 4/4 selftests/landlock: Check all possible intermediate directories

In reply to Mickaël Salaün

From: Mickaël Salaün <[email protected]>

Open and store file descriptors for the initial test directory at layout initialization and at sandbox creation. This enables checking relative path opening with different scenarios. To be sure to test all combinations, the intermediate path checks are of exponential complexity, which is OK for these use case though. All meaningful tests now also check relative paths.

Use some macros to avoid useless helper call rewrites with a context tied to each test.

---
 tools/testing/selftests/landlock/fs_test.c | 140 ++++++++++++++++++---
 1 file changed, 125 insertions(+), 15 deletions(-)
tools/testing/selftests/landlock/fs_test.c +125 -15
index 403c8255311f..b1a91fdd0f88 100644
@@ -154,7 +154,13 @@ static int remove_path(const char *const path)
 	return err;
 }
 
-static void prepare_layout(struct __test_metadata *const _metadata)
+struct layout_context {
+	int init_layout_cwd_fd;
+	int init_sandbox_cwd_fd;
+};
+
+static void prepare_layout(struct __test_metadata *const _metadata,
+		struct layout_context *const ctx)
 {
 	disable_caps(_metadata);
 	umask(0077);
@@ -171,10 +177,18 @@ static void prepare_layout(struct __test_metadata *const _metadata)
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
 	ASSERT_EQ(0, chdir(TMP_DIR));
+	ctx->init_layout_cwd_fd = open(".", O_DIRECTORY | O_CLOEXEC);
+	ASSERT_LE(0, ctx->init_layout_cwd_fd);
+	ctx->init_sandbox_cwd_fd = -1;
 }
 
-static void cleanup_layout(struct __test_metadata *const _metadata)
+static void cleanup_layout(struct __test_metadata *const _metadata,
+		const struct layout_context *const ctx)
 {
+	EXPECT_EQ(0, close(ctx->init_layout_cwd_fd));
+	if (ctx->init_sandbox_cwd_fd != -1) {
+		EXPECT_EQ(0, close(ctx->init_sandbox_cwd_fd));
+	}
 	EXPECT_EQ(0, chdir(".."));
 
 	set_cap(_metadata, CAP_SYS_ADMIN);
@@ -227,11 +241,12 @@ static void remove_layout1(struct __test_metadata *const _metadata)
 }
 
 FIXTURE(layout1) {
+	struct layout_context ctx;
 };
 
 FIXTURE_SETUP(layout1)
 {
-	prepare_layout(_metadata);
+	prepare_layout(_metadata, &self->ctx);
 
 	create_layout1(_metadata);
 }
@@ -240,7 +255,7 @@ FIXTURE_TEARDOWN(layout1)
 {
 	remove_layout1(_metadata);
 
-	cleanup_layout(_metadata);
+	cleanup_layout(_metadata, &self->ctx);
 }
 
 /*
@@ -264,11 +279,84 @@ static int test_open_rel(const int dirfd, const char *const path, const int flag
 	return 0;
 }
 
-static int test_open(const char *const path, const int flags)
+static int _test_open_all(const int base_fd, char *const path, const int flags)
+{
+	int ret_child, i;
+
+	ret_child = test_open_rel(base_fd, path, flags);
+	if (ret_child == ENOENT)
+		return ret_child;
+
+	for (i = strlen(path); i > 0; i--) {
+		int inter_fd, ret_parent;
+
+		if (path[i] != '/')
+			continue;
+		path[i] = '\0';
+		/* Using a non-O_PATH FD fills the cache. */
+		inter_fd = openat(base_fd, path, O_CLOEXEC | O_DIRECTORY);
+		path[i] = '/';
+		if (inter_fd < 0) {
+			if (errno != EACCES)
+				return -1;
+			/* If the sandbox denies access, then use O_PATH. */
+			path[i] = '\0';
+			inter_fd = openat(base_fd, path, O_CLOEXEC |
+					O_DIRECTORY | O_PATH);
+			path[i] = '/';
+			if (inter_fd < 0)
+				return -1;
+		}
+
+		/*
+		 * Checks all possible inter_fd combinations with
+		 * recursive calls.
+		 */
+		ret_parent = _test_open_all(inter_fd, path + i + 1, flags);
+		close(inter_fd);
+
+		/* Checks inconsistencies that may come from the cache. */
+		if (ret_parent != ret_child)
+			return -1;
+	}
+	return ret_child;
+}
+
+static int _test_open_ctx(const struct layout_context *const ctx,
+		const char *const path, const int flags)
 {
-	return test_open_rel(AT_FDCWD, path, flags);
+	char *walker;
+	int ret_init, ret_next, i;
+	/*
+	 * Checks with a FD opened without sandbox, and another FD opened
+	 * within a sandbox (but maybe not the same).
+	 */
+	const int extra_dirfd[] = {
+		ctx->init_layout_cwd_fd,
+		ctx->init_sandbox_cwd_fd
+	};
+
+	walker = strdup(path);
+	if (!walker)
+		return ENOMEM;
+
+	ret_init = _test_open_all(AT_FDCWD, walker, flags);
+	for (i = 0; i < ARRAY_SIZE(extra_dirfd); i++) {
+		const int dirfd = extra_dirfd[i];
+
+		if (dirfd != -1) {
+			ret_next = _test_open_all(dirfd, walker, flags);
+			if (ret_next != ret_init)
+				return -1;
+		}
+	}
+
+	free(walker);
+	return ret_init;
 }
 
+#define test_open(path, flags)	_test_open_ctx(&self->ctx, path, flags)
+
 TEST_F_FORK(layout1, no_restriction)
 {
 	ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
@@ -493,15 +581,28 @@ static int create_ruleset(struct __test_metadata *const _metadata,
 	return ruleset_fd;
 }
 
-static void enforce_ruleset(struct __test_metadata *const _metadata,
-		const int ruleset_fd)
+static void _enforce_ruleset(struct layout_context *const ctx,
+		struct __test_metadata *const _metadata, const int ruleset_fd)
 {
 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
 	ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0)) {
 		TH_LOG("Failed to enforce ruleset: %s", strerror(errno));
 	}
+
+	if (ctx->init_sandbox_cwd_fd == -1) {
+		ctx->init_sandbox_cwd_fd = open(".", O_DIRECTORY | O_CLOEXEC);
+		if (ctx->init_sandbox_cwd_fd == -1) {
+			ASSERT_EQ(EACCES, errno);
+			ctx->init_sandbox_cwd_fd = open(".", O_DIRECTORY |
+					O_CLOEXEC | O_PATH);
+			ASSERT_LE(0, ctx->init_sandbox_cwd_fd);
+		}
+	}
 }
 
+#define enforce_ruleset(_metadata, ruleset_fd)	\
+	_enforce_ruleset(&self->ctx, _metadata, ruleset_fd)
+
 TEST_F_FORK(layout1, proc_nsfs)
 {
 	const struct rule rules[] = {
@@ -1265,7 +1366,6 @@ TEST_F_FORK(layout1, rule_inside_mount_ns)
 	enforce_ruleset(_metadata, ruleset_fd);
 	ASSERT_EQ(0, close(ruleset_fd));
 
-	ASSERT_EQ(0, test_open("s3d3", O_RDONLY));
 	ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
 }
 
@@ -1366,7 +1466,8 @@ enum relative_access {
 	REL_CHROOT_CHDIR,
 };
 
-static void test_relative_path(struct __test_metadata *const _metadata,
+static void _test_relative_path(struct __test_metadata *const _metadata,
+		FIXTURE_DATA(layout1) *const self,
 		const enum relative_access rel)
 {
 	/*
@@ -1479,6 +1580,8 @@ static void test_relative_path(struct __test_metadata *const _metadata,
 	ASSERT_EQ(0, close(ruleset_fd));
 }
 
+#define test_relative_path(_metadata, rel)	_test_relative_path(_metadata, self, rel)
+
 TEST_F_FORK(layout1, relative_open)
 {
 	test_relative_path(_metadata, REL_OPEN);
@@ -1809,7 +1912,8 @@ TEST_F_FORK(layout1, remove_file)
 	ASSERT_EQ(0, unlinkat(AT_FDCWD, file1_s1d3, 0));
 }
 
-static void test_make_file(struct __test_metadata *const _metadata,
+static void _test_make_file(struct __test_metadata *const _metadata,
+		FIXTURE_DATA(layout1) *self,
 		const __u64 access, const mode_t mode, const dev_t dev)
 {
 	const struct rule rules[] = {
@@ -1860,6 +1964,10 @@ static void test_make_file(struct __test_metadata *const _metadata,
 	ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3));
 }
 
+#define test_make_file(_metadata, access, mode, dev)	\
+	_test_make_file(_metadata, self, access, mode, dev)
+
+
 TEST_F_FORK(layout1, make_char)
 {
 	/* Creates a /dev/null device. */
@@ -2076,11 +2184,12 @@ TEST_F_FORK(layout1, proc_pipe)
 }
 
 FIXTURE(layout1_bind) {
+	struct layout_context ctx;
 };
 
 FIXTURE_SETUP(layout1_bind)
 {
-	prepare_layout(_metadata);
+	prepare_layout(_metadata, &self->ctx);
 
 	create_layout1(_metadata);
 
@@ -2097,7 +2206,7 @@ FIXTURE_TEARDOWN(layout1_bind)
 
 	remove_layout1(_metadata);
 
-	cleanup_layout(_metadata);
+	cleanup_layout(_metadata, &self->ctx);
 }
 
 static const char bind_dir_s1d3[] = "./s2d1/s2d2/s1d3";
@@ -2417,11 +2526,12 @@ static const char (*merge_sub_files[])[] = {
  */
 
 FIXTURE(layout2_overlay) {
+	struct layout_context ctx;
 };
 
 FIXTURE_SETUP(layout2_overlay)
 {
-	prepare_layout(_metadata);
+	prepare_layout(_metadata, &self->ctx);
 
 	create_directory(_metadata, LOWER_BASE);
 	set_cap(_metadata, CAP_SYS_ADMIN);
@@ -2484,7 +2594,7 @@ FIXTURE_TEARDOWN(layout2_overlay)
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 	EXPECT_EQ(0, remove_path(MERGE_DATA));
 
-	cleanup_layout(_metadata);
+	cleanup_layout(_metadata, &self->ctx);
 }
 
 TEST_F_FORK(layout2_overlay, no_restriction)
Signature · 1 line
2.32.0