Introduce a path iterator, which reliably walk a struct path. Current version only support walking towards the root, which helper path_parent. But the path iterator API can be extended to cover other use cases, for example, walking the mount tree. Signed-off-by: Song Liu <song@xxxxxxxxxx> --- kernel/bpf/Makefile | 1 + kernel/bpf/helpers.c | 3 ++ kernel/bpf/path_iter.c | 74 ++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 5 +++ 4 files changed, 83 insertions(+) create mode 100644 kernel/bpf/path_iter.c diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 70502f038b92..8075a83d5e08 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -53,6 +53,7 @@ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o +obj-$(CONFIG_BPF_SYSCALL) += path_iter.o CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index c1113b74e1e2..d77b055092e7 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3386,6 +3386,9 @@ BTF_ID_FLAGS(func, bpf_copy_from_user_dynptr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_copy_from_user_str_dynptr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_copy_from_user_task_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_copy_from_user_task_str_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +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 = { diff --git a/kernel/bpf/path_iter.c b/kernel/bpf/path_iter.c new file mode 100644 index 000000000000..838ebbeac6c2 --- /dev/null +++ b/kernel/bpf/path_iter.c @@ -0,0 +1,74 @@ +// 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> + +enum bpf_path_iter_mode { + BPF_PATH_ITER_MODE_PARENT = 1, +}; + +/* open-coded iterator */ +struct bpf_iter_path { + __u64 __opaque[3]; +} __aligned(8); + +struct bpf_iter_path_kern { + struct path path; + enum bpf_path_iter_mode mode; +} __aligned(8); + +__bpf_kfunc_start_defs(); + +__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it, + struct path *start, + enum bpf_path_iter_mode mode) +{ + struct bpf_iter_path_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(*kit) > sizeof(*it)); + BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it)); + + kit->mode = mode; + + switch (mode) { + case BPF_PATH_ITER_MODE_PARENT: + break; + default: + memset(&kit->path, 0, sizeof(struct path)); + return -EINVAL; + } + + kit->path = *start; + path_get(&kit->path); + return 0; +} + +__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it) +{ + struct bpf_iter_path_kern *kit = (void *)it; + + switch (kit->mode) { + case BPF_PATH_ITER_MODE_PARENT: + enum path_parent_status status = path_parent(&kit->path); + + /* If already at a root, return NULL */ + if (status == PATH_PARENT_REAL_ROOT || + status == PATH_PARENT_DISCONNECTED_ROOT) + return NULL; + break; + default: + 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(); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d5807d2efc92..734c06809563 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -7034,6 +7034,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) @@ -7074,6 +7078,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