Skip to contentSkip to conversation

bpf path iterator

PATCH Superseded Song Liu 8 participants 23 messages Jun 3, 2025 – Jun 6, 2025

Subsystems (8)
VFS Kselftest BPF Security (LSM) +4 more subsystems

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Song Liu

author reviewer
#0 lore raw

v2 cover bpf path iterator

In security use cases, it is common to apply rules to VFS subtrees. However, filtering files in a subtree is not straightforward [1].

One solution to this problem is to start from a path and walk up the VFS tree (towards the root). Among in-tree LSMs, Landlock uses this solution.

BPF LSM solutions, such like Tetragon [2], also use similar approaches. However, due to lack of proper helper/kfunc support, BPF LSM solutions usually do the path walk with probe read, which is racy.

This patchset introduce a reliable helper path_walk_parent, which walks path to its VFS parent. The helper is use in Landlock.

A new BPF iterator, path iterator, is introduced to do the path walking. The BPF path iterator uses the new path_walk_parent help to walk the VFS tree.

Changes v1 => v2:
1. Rename path_parent => path_walk_parent.
2. Remove path_connected check in path_walk_parent.
3. Fix is_access_to_paths_allowed().
4. Remove mode for path iterator, add a flag instead.

v1: https://lore.kernel.org/bpf/[email protected]/

[1] https://lpc.events/event/18/contributions/1940/
[2] https://github.com/cilium/tetragon/

Song Liu (4):
namei: Introduce new helper function path_walk_parent()
landlock: Use path_walk_parent()
bpf: Introduce path iterator
selftests/bpf: Add tests for bpf path iterator

 fs/namei.c                                    |  52 +++++++
 include/linux/namei.h                         |   2 +
 kernel/bpf/Makefile                           |   1 +
 kernel/bpf/helpers.c                          |   3 +
 kernel/bpf/path_iter.c                        |  58 ++++++++
 kernel/bpf/verifier.c                         |   5 +
 security/landlock/fs.c                        |  31 ++--
 .../testing/selftests/bpf/bpf_experimental.h  |   6 +
 .../selftests/bpf/prog_tests/path_iter.c      |  12 ++
 tools/testing/selftests/bpf/progs/path_iter.c | 134 ++++++++++++++++++
 10 files changed, 283 insertions(+), 21 deletions(-)
 create mode 100644 kernel/bpf/path_iter.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/path_iter.c
 create mode 100644 tools/testing/selftests/bpf/progs/path_iter.c

--
2.47.1

author reviewer
#1 lore raw

PATCH v2 1/4 namei: Introduce new helper function path_walk_parent()

This helper walks an input path to its parent. Logic are added to handle walking across mount tree.

This will be used by landlock, and BPF LSM.

---
 fs/namei.c            | 52 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/namei.h |  2 ++
 2 files changed, 54 insertions(+)
fs/namei.c +52 -0
index 4bb889fc980b..7d5bf2bb604f 100644
@@ -1424,6 +1424,58 @@ static bool choose_mountpoint(struct mount *m, const struct path *root,
 	return found;
 }
 
+/**
+ * path_walk_parent - Walk to the parent of path
+ * @path: input and output path.
+ * @root: root of the path walk, do not go beyond this root. If @root is
+ *        zero'ed, walk all the way to real root.
+ *
+ * Given a path, find the parent path. Replace @path with the parent path.
+ * If we were already at the real root or a disconnected root, @path is
+ * not changed.
+ *
+ * The logic of path_walk_parent() is similar to follow_dotdot(), except
+ * that path_walk_parent() will continue walking for !path_connected case.
+ * This effectively means we are walking from disconnectedbind mount to the
+ * original mount point. If this behavior is not desired, the caller can
+ * add a check like:
+ *
+ *   if (path_walk_parent(&path) && !path_connected(path.mnt, path.dentry)
+ *           // continue walking
+ *   else
+ *           // stop walking
+ *
+ * Returns:
+ *  true  - if @path is updated to its parent.
+ *  false - if @path is already the root (real root or @root).
+ */
+bool path_walk_parent(struct path *path, const struct path *root)
+{
+	struct dentry *parent;
+
+	if (path_equal(path, root))
+		return false;
+
+	if (unlikely(path->dentry == path->mnt->mnt_root)) {
+		struct path p;
+
+		if (!choose_mountpoint(real_mount(path->mnt), root, &p))
+			return false;
+		path_put(path);
+		*path = p;
+		return true;
+	}
+
+	if (unlikely(IS_ROOT(path->dentry)))
+		return false;
+
+	parent = dget_parent(path->dentry);
+	dput(path->dentry);
+	path->dentry = parent;
+	return true;
+}
+EXPORT_SYMBOL_GPL(path_walk_parent);
+
 /*
  * Perform an automount
  * - return -EISDIR to tell follow_managed() to stop and return the path we
include/linux/namei.h +2 -0
index 5d085428e471..cba5373ecf86 100644
@@ -85,6 +85,8 @@ extern int follow_down_one(struct path *);
 extern int follow_down(struct path *path, unsigned int flags);
 extern int follow_up(struct path *);
 
+bool path_walk_parent(struct path *path, const struct path *root);
+
 extern struct dentry *lock_rename(struct dentry *, struct dentry *);
 extern struct dentry *lock_rename_child(struct dentry *, struct dentry *);
 extern void unlock_rename(struct dentry *, struct dentry *);
Signature · 1 line
2.47.1
maintainer
#2 lore raw

Re: [PATCH v2 bpf-next 1/4] namei: Introduce new helper function path_walk_parent()

Quoted 49 lines · Song Liu

This helper walks an input path to its parent. Logic are added to handle walking across mount tree.

This will be used by landlock, and BPF LSM.

---
 fs/namei.c            | 52 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/namei.h |  2 ++
 2 files changed, 54 insertions(+)
fs/namei.c +30 -0
index 4bb889fc980b..7d5bf2bb604f 100644
@@ -1424,6 +1424,58 @@ static bool choose_mountpoint(struct mount *m, const struct path *root,
 	return found;
 }
 
+/**
+ * path_walk_parent - Walk to the parent of path
+ * @path: input and output path.
+ * @root: root of the path walk, do not go beyond this root. If @root is
+ *        zero'ed, walk all the way to real root.
+ *
+ * Given a path, find the parent path. Replace @path with the parent path.
+ * If we were already at the real root or a disconnected root, @path is
+ * not changed.
+ *
+ * The logic of path_walk_parent() is similar to follow_dotdot(), except
+ * that path_walk_parent() will continue walking for !path_connected case.
+ * This effectively means we are walking from disconnectedbind mount to the
+ * original mount point. If this behavior is not desired, the caller can
+ * add a check like:
+ *
+ *   if (path_walk_parent(&path) && !path_connected(path.mnt, path.dentry)
+ *           // continue walking
+ *   else
+ *           // stop walking
+ *
+ * Returns:
+ *  true  - if @path is updated to its parent.
+ *  false - if @path is already the root (real root or @root).
+ */
+bool path_walk_parent(struct path *path, const struct path *root)
+{
+	struct dentry *parent;
+
+	if (path_equal(path, root))

fs/namei.c static bool choose_mountpoint(struct mount *m, const struct path *root, · line 1466

Quoted 10 lines
fs/namei.c +10 -0
+		return false;
+
+	if (unlikely(path->dentry == path->mnt->mnt_root)) {
+		struct path p;
+
+		if (!choose_mountpoint(real_mount(path->mnt), root, &p))
+			return false;
+		path_put(path);
+		*path = p;
+		return true;

It should not return here but continue with the following checks until the potential dget_parent() call.

I sent a test to check this issue:
https://lore.kernel.org/r/[email protected]

Quoted 32 lines
fs/namei.c +12 -0
+	}
+
+	if (unlikely(IS_ROOT(path->dentry)))
+		return false;
+
+	parent = dget_parent(path->dentry);
+	dput(path->dentry);
+	path->dentry = parent;
+	return true;
+}
+EXPORT_SYMBOL_GPL(path_walk_parent);
+
 /*
  * Perform an automount
  * - return -EISDIR to tell follow_managed() to stop and return the path we
include/linux/namei.h +2 -0
index 5d085428e471..cba5373ecf86 100644
@@ -85,6 +85,8 @@ extern int follow_down_one(struct path *);
 extern int follow_down(struct path *path, unsigned int flags);
 extern int follow_up(struct path *);
 
+bool path_walk_parent(struct path *path, const struct path *root);
+
 extern struct dentry *lock_rename(struct dentry *, struct dentry *);
 extern struct dentry *lock_rename_child(struct dentry *, struct dentry *);
 extern void unlock_rename(struct dentry *, struct dentry *);
Signature · 1 line
2.47.1
maintainer
#3 lore raw

Re: [PATCH v2 bpf-next 1/4] namei: Introduce new helper function path_walk_parent()

In reply to Song Liu
Quoted 4 lines · Song Liu

This helper walks an input path to its parent. Logic are added to handle walking across mount tree.

This will be used by landlock, and BPF LSM.

Unless I'm misreading that, it does *NOT* walk to parent - it treats step into mountpoint as a separate step. NAK in that form - it's simply a wrong primitive.

author reviewer
#4 lore raw

Re: [PATCH v2 bpf-next 1/4] namei: Introduce new helper function path_walk_parent()

Quoted 10 lines · Al Viro
Quoted 4 lines · Song Liu

This helper walks an input path to its parent. Logic are added to handle walking across mount tree.

This will be used by landlock, and BPF LSM.

Unless I'm misreading that, it does *NOT* walk to parent - it treats step into mountpoint as a separate step. NAK in that form - it's simply a wrong primitive.

I think this should be fixed by Mickaël's comment. I will send v3 with it.

Thanks,
Song

author reviewer
#5 lore raw

PATCH v2 2/4 landlock: Use path_walk_parent()

In reply to Song Liu

Use path_walk_parent() to walk a path up to its parent.

No functional changes intended.

---
 security/landlock/fs.c | 31 ++++++++++---------------------
 1 file changed, 10 insertions(+), 21 deletions(-)
security/landlock/fs.c +10 -21
index 6fee7c20f64d..3adac544dc9e 100644
@@ -837,8 +837,8 @@ static bool is_access_to_paths_allowed(
 	 * restriction.
 	 */
 	while (true) {
-		struct dentry *parent_dentry;
 		const struct landlock_rule *rule;
+		struct path root = {};
 
 		/*
 		 * If at least all accesses allowed on the destination are
@@ -895,34 +895,23 @@ static bool is_access_to_paths_allowed(
 		/* Stops when a rule from each layer grants access. */
 		if (allowed_parent1 && allowed_parent2)
 			break;
-jump_up:
-		if (walker_path.dentry == walker_path.mnt->mnt_root) {
-			if (follow_up(&walker_path)) {
-				/* Ignores hidden mount points. */
-				goto jump_up;
-			} else {
-				/*
-				 * Stops at the real root.  Denies access
-				 * because not all layers have granted access.
-				 */
-				break;
-			}
-		}
+
+		if (path_walk_parent(&walker_path, &root))
+			continue;
+
 		if (unlikely(IS_ROOT(walker_path.dentry))) {
 			/*
-			 * Stops at disconnected root directories.  Only allows
-			 * access to internal filesystems (e.g. nsfs, which is
-			 * reachable through /proc/<pid>/ns/<namespace>).
+			 * Stops at disconnected or real root directories.
+			 * Only allows access to internal filesystems
+			 * (e.g. nsfs, which is reachable through
+			 * /proc/<pid>/ns/<namespace>).
 			 */
 			if (walker_path.mnt->mnt_flags & MNT_INTERNAL) {
 				allowed_parent1 = true;
 				allowed_parent2 = true;
 			}
-			break;
 		}
-		parent_dentry = dget_parent(walker_path.dentry);
-		dput(walker_path.dentry);
-		walker_path.dentry = parent_dentry;
+		break;
 	}
 	path_put(&walker_path);
 
Signature · 1 line
2.47.1
maintainer
#6 lore raw

Re: [PATCH v2 bpf-next 2/4] landlock: Use path_walk_parent()

Landlock tests with hostfs fail:

ok 126 layout3_fs.hostfs.tag_inode_file
# RUN layout3_fs.hostfs.release_inodes ...
# fs_test.c:5555:release_inodes:Expected EACCES (13) == test_open(TMP_DIR, O_RDONLY) (0)

This specific test checks that an access to a (denied) mount point over an allowed directory is indeed denied.

It's not clear to me the origin of the issue, but it seems to be related to choose_mountpoint().

You can run these tests with check-linux.sh build kselftest from
https://github.com/landlock-lsm/landlock-test-tools

Just in case, please always run clang-format -i security/landlock/*.[ch]

Quoted 33 lines · Song Liu

Use path_walk_parent() to walk a path up to its parent.

No functional changes intended.

---
 security/landlock/fs.c | 31 ++++++++++---------------------
 1 file changed, 10 insertions(+), 21 deletions(-)
security/landlock/fs.c +1 -7
index 6fee7c20f64d..3adac544dc9e 100644
@@ -837,8 +837,8 @@ static bool is_access_to_paths_allowed(
 	 * restriction.
 	 */
 	while (true) {
-		struct dentry *parent_dentry;
 		const struct landlock_rule *rule;
+		struct path root = {};
 
 		/*
 		 * If at least all accesses allowed on the destination are
@@ -895,34 +895,23 @@ static bool is_access_to_paths_allowed(
 		/* Stops when a rule from each layer grants access. */
 		if (allowed_parent1 && allowed_parent2)
 			break;
-jump_up:
-		if (walker_path.dentry == walker_path.mnt->mnt_root) {
-			if (follow_up(&walker_path)) {
-				/* Ignores hidden mount points. */
-				goto jump_up;
-			} else {

security/landlock/fs.c static bool is_access_to_paths_allowed( · line 900

Quoted 10 lines
security/landlock/fs.c +3 -7
-				/*
-				 * Stops at the real root.  Denies access
-				 * because not all layers have granted access.
-				 */
-				break;
-			}
-		}
+
+		if (path_walk_parent(&walker_path, &root))
+			continue;

It would be better to avoid a "continue" statement but to just use an if block.

Quoted 28 lines
security/landlock/fs.c +6 -7
+
 		if (unlikely(IS_ROOT(walker_path.dentry))) {
 			/*
-			 * Stops at disconnected root directories.  Only allows
-			 * access to internal filesystems (e.g. nsfs, which is
-			 * reachable through /proc/<pid>/ns/<namespace>).
+			 * Stops at disconnected or real root directories.
+			 * Only allows access to internal filesystems
+			 * (e.g. nsfs, which is reachable through
+			 * /proc/<pid>/ns/<namespace>).
 			 */
 			if (walker_path.mnt->mnt_flags & MNT_INTERNAL) {
 				allowed_parent1 = true;
 				allowed_parent2 = true;
 			}
-			break;
 		}
-		parent_dentry = dget_parent(walker_path.dentry);
-		dput(walker_path.dentry);
-		walker_path.dentry = parent_dentry;
+		break;
 	}
 	path_put(&walker_path);
 
Signature · 1 line
2.47.1
author reviewer
#7 lore raw

Re: [PATCH v2 bpf-next 2/4] landlock: Use path_walk_parent()

Quoted 9 lines · Mickaël Salaün

Landlock tests with hostfs fail:

ok 126 layout3_fs.hostfs.tag_inode_file
# RUN layout3_fs.hostfs.release_inodes ...
# fs_test.c:5555:release_inodes:Expected EACCES (13) == test_open(TMP_DIR, O_RDONLY) (0)

This specific test checks that an access to a (denied) mount point over an allowed directory is indeed denied.

I am having trouble understanding the test. It appears to me the newly mounted tmpfs on /tmp is allowed, but accesses to / and thus mount point /tmp is denied? What would the walk in is_access_to_paths_allowed look like?

Quoted 5 lines

It's not clear to me the origin of the issue, but it seems to be related to choose_mountpoint().

You can run these tests with check-linux.sh build kselftest from
https://github.com/landlock-lsm/landlock-test-tools

How should I debug this test? printk doesn't seem to work.

Thanks,
Song

author reviewer
#8 lore raw

Re: [PATCH v2 bpf-next 2/4] landlock: Use path_walk_parent()

Quoted 11 lines · Song Liu
Quoted 9 lines · Mickaël Salaün

Landlock tests with hostfs fail:

ok 126 layout3_fs.hostfs.tag_inode_file
# RUN layout3_fs.hostfs.release_inodes ...
# fs_test.c:5555:release_inodes:Expected EACCES (13) == test_open(TMP_DIR, O_RDONLY) (0)

This specific test checks that an access to a (denied) mount point over an allowed directory is indeed denied.

I just realized this only fails on hostfs. AFAICT, hostfs is only used by um. Do we really need this to behave the same on um+hostfs?

Thanks,
Song

Quoted 16 lines

I am having trouble understanding the test. It appears to me the newly mounted tmpfs on /tmp is allowed, but accesses to / and thus mount point /tmp is denied? What would the walk in is_access_to_paths_allowed look like?

Quoted 5 lines

It's not clear to me the origin of the issue, but it seems to be related to choose_mountpoint().

You can run these tests with check-linux.sh build kselftest from
https://github.com/landlock-lsm/landlock-test-tools

How should I debug this test? printk doesn't seem to work.

Thanks,
Song

maintainer
#9 lore raw

Re: [PATCH v2 bpf-next 2/4] landlock: Use path_walk_parent()

Quoted 10 lines · Song Liu
Quoted 9 lines · Song Liu
Quoted 7 lines · Mickaël Salaün

Landlock tests with hostfs fail:

ok 126 layout3_fs.hostfs.tag_inode_file
# RUN layout3_fs.hostfs.release_inodes ...
# fs_test.c:5555:release_inodes:Expected EACCES (13) == test_open(TMP_DIR, O_RDONLY) (0)

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

This specific test checks that an access to a (denied) mount point over an allowed directory is indeed denied.

I just realized this only fails on hostfs. AFAICT, hostfs is only used by um. Do we really need this to behave the same on um+hostfs?

Yes, this would be a regression, and in fact it is not related to hostfs and it would be a new security bug.

The issue is that the path_walk_parent() doesn't return the parent dentry but the underlying mount point if any. When choose_mountpoint() returns true, path_walk_parent() should continue to the following root check and potentiall the dget_parent() call. We need to be careful with the path_put() though.

This issue was only spotted by this hostfs test because this one adds a rule which is tied to the inode of the mount which is in fact the same inode of the mount point because the mount is a bind mount. I'll send a new test that check the same thing but with tmpfs (for convenience, but it would be the same for any filesystem).

Quoted 9 lines

Thanks,
Song

Quoted 5 lines

I am having trouble understanding the test. It appears to me the newly mounted tmpfs on /tmp is allowed, but accesses to / and thus mount point /tmp is denied? What would the walk in is_access_to_paths_allowed look like?

The test checks that a mount is not wrongly identified as the underlying mount point.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

It's not clear to me the origin of the issue, but it seems to be related to choose_mountpoint().

You can run these tests with check-linux.sh build kselftest from
https://github.com/landlock-lsm/landlock-test-tools

How should I debug this test? printk doesn't seem to work.

The console log level is set to warn, so you can use pr_warn().

Quoted 3 lines
Quoted 3 lines

Thanks,
Song

author reviewer
#10 lore raw

PATCH v2 3/4 bpf: Introduce path iterator

In reply to Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
 kernel/bpf/Makefile    |  1 +
 kernel/bpf/helpers.c   |  3 +++
 kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c  |  5 ++++
 4 files changed, 67 insertions(+)
 create mode 100644 kernel/bpf/path_iter.c
kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
 ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
 obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
 endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
 CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +3 -0
index b71e428ad936..b190c78e40f6 100644
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 #endif
 BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 BTF_KFUNCS_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +58 -0

58 lines not on this page · raw message

kernel/bpf/verifier.c +5 -0
index a7d6e0c5928b..45b45cdfb223 100644
@@ -7036,6 +7036,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
 	struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) {
+	struct dentry *dentry;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
 			struct bpf_reg_state *reg,
 			const char *field_name, u32 btf_id)
@@ -7076,6 +7080,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
 				    const char *field_name, u32 btf_id)
 {
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
 					  "__safe_trusted_or_null");
Signature · 1 line
2.47.1
#11 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

kernel/bpf/path_iter.c

Quoted 47 lines · Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
 kernel/bpf/Makefile    |  1 +
 kernel/bpf/helpers.c   |  3 +++
 kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c  |  5 ++++
 4 files changed, 67 insertions(+)
 create mode 100644 kernel/bpf/path_iter.c
kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
 ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
 obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
 endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
 CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +3 -0
index b71e428ad936..b190c78e40f6 100644
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 #endif
 BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 BTF_KFUNCS_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +0 -0
new file mode 100644
index 000000000000..0d972ec84beb

I think Christian's preference was to keep
everything in fs/bpf_fs_kfuncs.c

Don't add a new file. Just add this iter there.

Quoted 6 lines
kernel/bpf/path_iter.c new file +5 -0
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <linux/bpf.h>
+#include <linux/bpf_mem_alloc.h>
+#include <linux/namei.h>

kernel/bpf/path_iter.c line 15

Quoted 10 lines
kernel/bpf/path_iter.c new file +10 -0
+#include <linux/path.h>
+
+/* open-coded iterator */
+struct bpf_iter_path {
+       __u64 __opaque[3];
+} __aligned(8);
+
+struct bpf_iter_path_kern {
+       struct path path;
+       __u64 flags;

Why? flags is unused. Don't waste space for it.

#12 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

Quoted 42 lines · Alexei Starovoitov
Quoted 41 lines · Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
 kernel/bpf/Makefile    |  1 +
 kernel/bpf/helpers.c   |  3 +++
 kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c  |  5 ++++
 4 files changed, 67 insertions(+)
 create mode 100644 kernel/bpf/path_iter.c
kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
 ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
 obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
 endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
 CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +3 -0
index b71e428ad936..b190c78e40f6 100644
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 #endif
 BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 BTF_KFUNCS_END(common_btf_ids)
 
Quoted 9 lines

kernel/bpf/path_iter.c

Quoted 6 lines
kernel/bpf/helpers.c +0 -0
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +0 -0
new file mode 100644
index 000000000000..0d972ec84beb

I think Christian's preference was to keep
everything in fs/bpf_fs_kfuncs.c

Yes. And since that also adds new fs helpers I want to take that through the VFS tree, please. I'll provide a stable branch as we do with all other subsystems.

#13 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

In reply to Song Liu
Quoted 93 lines · Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
 kernel/bpf/Makefile    |  1 +
 kernel/bpf/helpers.c   |  3 +++
 kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c  |  5 ++++
 4 files changed, 67 insertions(+)
 create mode 100644 kernel/bpf/path_iter.c
kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
 ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
 obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
 endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
 CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +3 -0
index b71e428ad936..b190c78e40f6 100644
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 #endif
 BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 BTF_KFUNCS_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +45 -0
new file mode 100644
index 000000000000..0d972ec84beb
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <linux/bpf.h>
+#include <linux/bpf_mem_alloc.h>
+#include <linux/namei.h>
+#include <linux/path.h>
+
+/* open-coded iterator */
+struct bpf_iter_path {
+       __u64 __opaque[3];
+} __aligned(8);
+
+struct bpf_iter_path_kern {
+       struct path path;
+       __u64 flags;
+} __aligned(8);
+
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it,
+                                 struct path *start,
+                                 __u64 flags)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
+       BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
+
+       if (flags) {
+               memset(&kit->path, 0, sizeof(struct path));
+               return -EINVAL;
+       }
+
+       kit->path = *start;
+       path_get(&kit->path);
+       kit->flags = flags;
+
+       return 0;
+}
+
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+       struct path root = {};
+

kernel/bpf/path_iter.c line 55

Quoted 10 lines
kernel/bpf/path_iter.c new file +10 -0
+       if (!path_walk_parent(&kit->path, &root))
+               return NULL;
+       return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       path_put(&kit->path);

note, destroy() will be called even if construction of iterator fails or we exhausted iterator. So you need to make sure that you have bpf_iter_path state where you can detect that there is no path present and skip path_put().

Quoted 29 lines
kernel/bpf/path_iter.c new file +3 -0
+}
+
+__bpf_kfunc_end_defs();
kernel/bpf/verifier.c +5 -1
index a7d6e0c5928b..45b45cdfb223 100644
@@ -7036,6 +7036,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
        struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) {
+       struct dentry *dentry;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
                        struct bpf_reg_state *reg,
                        const char *field_name, u32 btf_id)
@@ -7076,6 +7080,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
                                    const char *field_name, u32 btf_id)
 {
        BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
+       BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path));
 
        return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
                                          "__safe_trusted_or_null");
--

2.47.1

maintainer
#14 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

Quoted 99 lines · Andrii Nakryiko
Quoted 98 lines · Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
  kernel/bpf/Makefile    |  1 +
  kernel/bpf/helpers.c   |  3 +++
  kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
  kernel/bpf/verifier.c  |  5 ++++
  4 files changed, 67 insertions(+)

create mode 100644 kernel/bpf/path_iter.c

kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
  ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
  obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
  endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
  CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +3 -0
index b71e428ad936..b190c78e40f6 100644
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
  BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
  #endif
  BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
  BTF_KFUNCS_END(common_btf_ids)
 
  static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +51 -0
new file mode 100644
index 000000000000..0d972ec84beb
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <linux/bpf.h>
+#include <linux/bpf_mem_alloc.h>
+#include <linux/namei.h>
+#include <linux/path.h>
+
+/* open-coded iterator */
+struct bpf_iter_path {
+       __u64 __opaque[3];
+} __aligned(8);
+
+struct bpf_iter_path_kern {
+       struct path path;
+       __u64 flags;
+} __aligned(8);
+
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it,
+                                 struct path *start,
+                                 __u64 flags)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
+       BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
+
+       if (flags) {
+               memset(&kit->path, 0, sizeof(struct path));
+               return -EINVAL;
+       }
+
+       kit->path = *start;
+       path_get(&kit->path);
+       kit->flags = flags;
+
+       return 0;
+}
+
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+       struct path root = {};
+
+       if (!path_walk_parent(&kit->path, &root))
+               return NULL;
+       return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
Quoted 8 lines

kernel/bpf/path_iter.c line 55

Quoted 4 lines
kernel/bpf/path_iter.c new file +4 -0
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       path_put(&kit->path);

note, destroy() will be called even if construction of iterator fails or we exhausted iterator. So you need to make sure that you have bpf_iter_path state where you can detect that there is no path present and skip path_put().

In rare cases, it is possible &kit->path address could be destroyed and reused, right? Maybe we need more state in kit to detect the change?

Quoted 30 lines
Quoted 30 lines
kernel/bpf/path_iter.c new file +3 -0
+}
+
+__bpf_kfunc_end_defs();
kernel/bpf/verifier.c +5 -1
index a7d6e0c5928b..45b45cdfb223 100644
@@ -7036,6 +7036,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
         struct sock *sk;
  };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) {
+       struct dentry *dentry;
+};
+
  static bool type_is_rcu(struct bpf_verifier_env *env,
                         struct bpf_reg_state *reg,
                         const char *field_name, u32 btf_id)
@@ -7076,6 +7080,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
                                     const char *field_name, u32 btf_id)
  {
         BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
+       BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path));
 
         return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
                                           "__safe_trusted_or_null");
--

2.47.1

author reviewer
#15 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

Quoted 112 lines · Yonghong Song
Quoted 99 lines · Andrii Nakryiko
Quoted 98 lines · Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
  kernel/bpf/Makefile    |  1 +
  kernel/bpf/helpers.c   |  3 +++
  kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
  kernel/bpf/verifier.c  |  5 ++++
  4 files changed, 67 insertions(+)

create mode 100644 kernel/bpf/path_iter.c

kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
  ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
  obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
  endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
  CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +3 -0
index b71e428ad936..b190c78e40f6 100644
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
  BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
  #endif
  BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
  BTF_KFUNCS_END(common_btf_ids)
 
  static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +51 -0
new file mode 100644
index 000000000000..0d972ec84beb
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <linux/bpf.h>
+#include <linux/bpf_mem_alloc.h>
+#include <linux/namei.h>
+#include <linux/path.h>
+
+/* open-coded iterator */
+struct bpf_iter_path {
+       __u64 __opaque[3];
+} __aligned(8);
+
+struct bpf_iter_path_kern {
+       struct path path;
+       __u64 flags;
+} __aligned(8);
+
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it,
+                                 struct path *start,
+                                 __u64 flags)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
+       BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
+
+       if (flags) {
+               memset(&kit->path, 0, sizeof(struct path));
+               return -EINVAL;
+       }
+
+       kit->path = *start;
+       path_get(&kit->path);
+       kit->flags = flags;
+
+       return 0;
+}
+
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+       struct path root = {};
+
+       if (!path_walk_parent(&kit->path, &root))
+               return NULL;
+       return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
Quoted 8 lines

kernel/bpf/path_iter.c line 55

Quoted 4 lines
kernel/bpf/path_iter.c new file +4 -0
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       path_put(&kit->path);

note, destroy() will be called even if construction of iterator fails or we exhausted iterator. So you need to make sure that you have bpf_iter_path state where you can detect that there is no path present and skip path_put().

Quoted 2 lines

In rare cases, it is possible &kit->path address could be destroyed and reused, right? Maybe we need more state in kit to detect the change?

kit->path is always referenced, so this should not happen.

Thanks,
Song

author reviewer
#16 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

In reply to Andrii Nakryiko

On Tue, Jun 3, 2025 at 11:40 AM Andrii Nakryiko
<[email protected]> wrote:
[...]

Quoted 12 lines
Quoted 12 lines
kernel/bpf/path_iter.c new file +12 -0
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+       struct path root = {};
+
+       if (!path_walk_parent(&kit->path, &root))
+               return NULL;
+       return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
+{
Quoted 8 lines

kernel/bpf/path_iter.c line 55

Quoted 3 lines
kernel/bpf/path_iter.c new file +3 -0
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       path_put(&kit->path);

note, destroy() will be called even if construction of iterator fails or we exhausted iterator. So you need to make sure that you have bpf_iter_path state where you can detect that there is no path present and skip path_put().

In bpf_iter_path_next(), when path_walk_parent() returns false, we still hold reference to kit->path, then _destroy() will release it. So we should be fine, no?

Thanks,
Song

Quoted 31 lines
Quoted 30 lines
kernel/bpf/path_iter.c new file +3 -0
+}
+
+__bpf_kfunc_end_defs();
kernel/bpf/verifier.c +5 -1
index a7d6e0c5928b..45b45cdfb223 100644
@@ -7036,6 +7036,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
        struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) {
+       struct dentry *dentry;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
                        struct bpf_reg_state *reg,
                        const char *field_name, u32 btf_id)
@@ -7076,6 +7080,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
                                    const char *field_name, u32 btf_id)
 {
        BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
+       BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path));
 
        return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
                                          "__safe_trusted_or_null");
--

2.47.1

#17 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

Quoted 25 lines · Song Liu

On Tue, Jun 3, 2025 at 11:40 AM Andrii Nakryiko
<[email protected]> wrote:
[...]

Quoted 12 lines
Quoted 12 lines
kernel/bpf/path_iter.c new file +12 -0
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+       struct path root = {};
+
+       if (!path_walk_parent(&kit->path, &root))
+               return NULL;
+       return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
+{
Quoted 8 lines

kernel/bpf/path_iter.c line 55

Quoted 3 lines
kernel/bpf/path_iter.c new file +3 -0
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       path_put(&kit->path);

note, destroy() will be called even if construction of iterator fails or we exhausted iterator. So you need to make sure that you have bpf_iter_path state where you can detect that there is no path present and skip path_put().

Quoted 3 lines

In bpf_iter_path_next(), when path_walk_parent() returns false, we still hold reference to kit->path, then _destroy() will release it. So we should be fine, no?

you still need to handle iterators that failed to be initialized, though? And one can argue that if path_walk_parent() returns false, we need to put that last path before returning NULL, no?

Quoted 35 lines

Thanks,
Song

Quoted 31 lines
Quoted 30 lines
kernel/bpf/path_iter.c new file +3 -0
+}
+
+__bpf_kfunc_end_defs();
kernel/bpf/verifier.c +5 -1
index a7d6e0c5928b..45b45cdfb223 100644
@@ -7036,6 +7036,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
        struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) {
+       struct dentry *dentry;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
                        struct bpf_reg_state *reg,
                        const char *field_name, u32 btf_id)
@@ -7076,6 +7080,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
                                    const char *field_name, u32 btf_id)
 {
        BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
+       BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path));
 
        return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
                                          "__safe_trusted_or_null");
--

2.47.1

author reviewer
#18 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

Quoted 27 lines · Andrii Nakryiko
Quoted 25 lines · Song Liu

On Tue, Jun 3, 2025 at 11:40 AM Andrii Nakryiko
<[email protected]> wrote:
[...]

Quoted 20 lines
Quoted 5 lines
kernel/bpf/path_iter.c new file +5 -0
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+       struct path root = {};
+

kernel/bpf/path_iter.c line 55

Quoted 10 lines
kernel/bpf/path_iter.c new file +10 -0
+       if (!path_walk_parent(&kit->path, &root))
+               return NULL;
+       return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       path_put(&kit->path);

note, destroy() will be called even if construction of iterator fails or we exhausted iterator. So you need to make sure that you have bpf_iter_path state where you can detect that there is no path present and skip path_put().

Quoted 7 lines
Quoted 3 lines

In bpf_iter_path_next(), when path_walk_parent() returns false, we still hold reference to kit->path, then _destroy() will release it. So we should be fine, no?

you still need to handle iterators that failed to be initialized, though? And one can argue that if path_walk_parent() returns false, we need to put that last path before returning NULL, no?

kit->path is zero'ed on initialization failures, so we can path_put() it safely. For _next() returns NULL case, we can either put kit->path in _destroy(), which is the logic now, or put kit->path in the last _next() call and make _destroy() a no-op in that case. I don't have a strong preference either way.

Thanks,
Song

#19 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

Quoted 38 lines · Song Liu
Quoted 27 lines · Andrii Nakryiko
Quoted 25 lines · Song Liu

On Tue, Jun 3, 2025 at 11:40 AM Andrii Nakryiko
<[email protected]> wrote:
[...]

Quoted 20 lines
Quoted 5 lines
kernel/bpf/path_iter.c new file +5 -0
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+       struct path root = {};
+

kernel/bpf/path_iter.c line 55

Quoted 10 lines
kernel/bpf/path_iter.c new file +10 -0
+       if (!path_walk_parent(&kit->path, &root))
+               return NULL;
+       return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
+{
+       struct bpf_iter_path_kern *kit = (void *)it;
+
+       path_put(&kit->path);

note, destroy() will be called even if construction of iterator fails or we exhausted iterator. So you need to make sure that you have bpf_iter_path state where you can detect that there is no path present and skip path_put().

Quoted 7 lines
Quoted 3 lines

In bpf_iter_path_next(), when path_walk_parent() returns false, we still hold reference to kit->path, then _destroy() will release it. So we should be fine, no?

you still need to handle iterators that failed to be initialized, though? And one can argue that if path_walk_parent() returns false, we need to put that last path before returning NULL, no?

Quoted 5 lines

kit->path is zero'ed on initialization failures, so we can path_put() it safely. For _next() returns NULL case, we can either put kit->path in _destroy(), which is the logic now, or put kit->path in the last _next() call and make _destroy() a no-op in that case. I don't have a strong preference either way.

I didn't realize path_put() is a no-op for zeroed-out struct path. I'd probably leave a comment for future selves, I don't have strong preference otherwise.

Quoted 3 lines

Thanks,
Song

#20 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

In reply to Song Liu
Quoted 32 lines · Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
 kernel/bpf/Makefile    |  1 +
 kernel/bpf/helpers.c   |  3 +++
 kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c  |  5 ++++
 4 files changed, 67 insertions(+)
 create mode 100644 kernel/bpf/path_iter.c
kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
 ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
 obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
 endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
 CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +0 -0
index b71e428ad936..b190c78e40f6 100644

kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB · line 3400

Quoted 4 lines
kernel/bpf/helpers.c +1 -0
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 #endif
 BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)

Hm, I'd expect KF_TRUSTED_ARGS to be enforced onto bpf_iter_path_new(), no? Shouldn't this only be operating on a stable struct path reference?

kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB · line 3402

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)

At this point, the claim is that such are only to be used from the context of the BPF LSM. If true, I'd expect these BPF kfuncs to be part of bpf_fs_kfunc_set_ids once moved into fs/bpf_fs_kfuncs.c.

Quoted 27 lines
kernel/bpf/helpers.c +0 -0
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +20 -0
new file mode 100644
index 000000000000..0d972ec84beb
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <linux/bpf.h>
+#include <linux/bpf_mem_alloc.h>
+#include <linux/namei.h>
+#include <linux/path.h>
+
+/* open-coded iterator */
+struct bpf_iter_path {
+	__u64 __opaque[3];
+} __aligned(8);
+
+struct bpf_iter_path_kern {
+	struct path path;
+	__u64 flags;
+} __aligned(8);
+
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it,

kernel/bpf/path_iter.c line 30

Quoted 10 lines
kernel/bpf/path_iter.c new file +10 -0
+				  struct path *start,
+				  __u64 flags)
+{
+	struct bpf_iter_path_kern *kit = (void *)it;
+
+	BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
+	BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
+
+	if (flags) {
+		memset(&kit->path, 0, sizeof(struct path));

This warrants a comment for sure. Also why not just zero it out entirely?

Quoted 4 lines
kernel/bpf/path_iter.c new file +4 -0
+		return -EINVAL;
+	}
+
+	kit->path = *start;

kernel/bpf/path_iter.c line 44

Quoted 10 lines
kernel/bpf/path_iter.c new file +10 -0
+	path_get(&kit->path);
+	kit->flags = flags;
+
+	return 0;
+}
+
+__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it)
+{
+	struct bpf_iter_path_kern *kit = (void *)it;
+	struct path root = {};

I think this also warrants a comment. Specifically, that unless the loop is explicitly terminated, bpf_iter_path_next() will continue looping until we've reached the global root of the VFS.

Quoted 18 lines
kernel/bpf/path_iter.c new file +13 -0
+	if (!path_walk_parent(&kit->path, &root))
+		return NULL;
+	return &kit->path;
+}
+
+__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it)
+{
+	struct bpf_iter_path_kern *kit = (void *)it;
+
+	path_put(&kit->path);
+}
+
+__bpf_kfunc_end_defs();
kernel/bpf/verifier.c +0 -0
index a7d6e0c5928b..45b45cdfb223 100644

kernel/bpf/verifier.c BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) { · line 7041

Quoted 6 lines
kernel/bpf/verifier.c +3 -0
@@ -7036,6 +7036,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
 	struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) {
+	struct dentry *dentry;
+};

Only trusted if struct path is trusted, and hence why KF_TRUSTED_ARGS should be enforced.

Quoted 14 lines
kernel/bpf/verifier.c +1 -0
 static bool type_is_rcu(struct bpf_verifier_env *env,
 			struct bpf_reg_state *reg,
 			const char *field_name, u32 btf_id)
@@ -7076,6 +7080,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
 				    const char *field_name, u32 btf_id)
 {
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
 					  "__safe_trusted_or_null");
Signature · 1 line
2.47.1
author reviewer
#21 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Introduce path iterator

Quoted 27 lines · Matt Bobrowski
Quoted 25 lines · Song Liu

Introduce a path iterator, which reliably walk a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.

---
 kernel/bpf/Makefile    |  1 +
 kernel/bpf/helpers.c   |  3 +++
 kernel/bpf/path_iter.c | 58 ++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c  |  5 ++++
 4 files changed, 67 insertions(+)
 create mode 100644 kernel/bpf/path_iter.c
kernel/bpf/Makefile +1 -0
index 3a335c50e6e3..454a650d934e 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
 ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
 obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
 endif
+obj-$(CONFIG_BPF_SYSCALL) += path_iter.o
 
Quoted 15 lines

kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB · line 3400

Quoted 11 lines
kernel/bpf/Makefile +0 -0
 CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/helpers.c +1 -0
index b71e428ad936..b190c78e40f6 100644
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 #endif
 BTF_ID_FLAGS(func, __bpf_trap)
+BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_SLEEPABLE)

Hm, I'd expect KF_TRUSTED_ARGS to be enforced onto bpf_iter_path_new(), no? Shouldn't this only be operating on a stable struct path reference?

Good catch! Added KF_TRUSTED_ARGS. also added a test with untrusted pointer.

Quoted 7 lines

kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB · line 3402

Quoted 3 lines
kernel/bpf/helpers.c +2 -0
+BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)

At this point, the claim is that such are only to be used from the context of the BPF LSM. If true, I'd expect these BPF kfuncs to be part of bpf_fs_kfunc_set_ids once moved into fs/bpf_fs_kfuncs.c.

I moved this to fs/bpf_fs_kfuncs.c in the next version.

Quoted 33 lines
Quoted 33 lines
kernel/bpf/helpers.c +0 -0
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/path_iter.c new file +25 -0
new file mode 100644
index 000000000000..0d972ec84beb
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <linux/bpf.h>
+#include <linux/bpf_mem_alloc.h>
+#include <linux/namei.h>
+#include <linux/path.h>
+
+/* open-coded iterator */
+struct bpf_iter_path {
+     __u64 __opaque[3];
+} __aligned(8);
+
+struct bpf_iter_path_kern {
+     struct path path;
+     __u64 flags;
+} __aligned(8);
+
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it,
+                               struct path *start,
+                               __u64 flags)
+{
+     struct bpf_iter_path_kern *kit = (void *)it;
+
Quoted 8 lines

kernel/bpf/path_iter.c line 30

Quoted 5 lines
kernel/bpf/path_iter.c new file +5 -0
+     BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
+     BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
+
+     if (flags) {
+             memset(&kit->path, 0, sizeof(struct path));

This warrants a comment for sure. Also why not just zero it out entirely?

Added some comments in v3. Also "flags" is removed in v3.

Thanks,
Song

[...]

author reviewer
#22 lore raw

PATCH v2 4/4 selftests/bpf: Add tests for bpf path iterator

In reply to Song Liu

Add tests for bpf path iterator, including test cases similar to real workload (call bpf_path_d_path and bpf_get_dentry_xattr), and test cases where the verifier rejects invalid use of the iterator.

---
 .../testing/selftests/bpf/bpf_experimental.h  |   6 +
 .../selftests/bpf/prog_tests/path_iter.c      |  12 ++
 tools/testing/selftests/bpf/progs/path_iter.c | 134 ++++++++++++++++++
 3 files changed, 152 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/path_iter.c
 create mode 100644 tools/testing/selftests/bpf/progs/path_iter.c
tools/testing/selftests/bpf/bpf_experimental.h +6 -0
index 5e512a1d09d1..cbb759b473df 100644
@@ -596,4 +596,10 @@ extern int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it) __weak __ksym;
 extern struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it) __weak __ksym;
 extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __ksym;
 
+struct bpf_iter_path;
+extern int bpf_iter_path_new(struct bpf_iter_path *it, struct path *start,
+			     __u64 flags) __weak __ksym;
+extern struct path *bpf_iter_path_next(struct bpf_iter_path *it) __weak __ksym;
+extern void bpf_iter_path_destroy(struct bpf_iter_path *it) __weak __ksym;
+
 #endif
tools/testing/selftests/bpf/prog_tests/path_iter.c new file +12 -0
new file mode 100644
index 000000000000..3c99c24fbd96
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <bpf/libbpf.h>
+#include <bpf/btf.h>
+#include "path_iter.skel.h"
+
+void test_path_iter(void)
+{
+	RUN_TESTS(path_iter);
+}
tools/testing/selftests/bpf/progs/path_iter.c new file +134 -0

134 lines not on this page · raw message

Signature · 1 line
2.47.1