A data placement policy BPF program may want more than the name of the file being opened, e.g. its full path from bpf_path_d_path(). However, the FS kfuncs are registered for struct_ops programs only with CONFIG_BINFMT_MISC_BPF, and bpf_fs_kfuncs_filter() only lets binfmt_misc programs call them. Register the kfunc set for struct_ops programs with CONFIG_FS_DP_BPF as well, and allow fs_dp_ops programs in the filter. As with binfmt_misc, the kfuncs are available only when CONFIG_BPF_LSM is enabled, since fs/bpf_fs_kfuncs.c is built only with it. Most of them are not tied to BPF LSM, so this should eventually be fixed by building the file without CONFIG_BPF_LSM and keeping only the LSM-specific parts under it. That is left for a separate change. Signed-off-by: Naohiro Aota --- fs/bpf_fs_kfuncs.c | 6 ++++-- fs/fs_dp.c | 6 ++++++ include/linux/fs_dp.h | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 35ccd98a4de6..fdb5b5f796fe 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -546,7 +547,7 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) /* ->st_ops is unset during the cfg pass; enforced once it is set. */ if (!prog->aux->st_ops) return 0; - if (bpf_prog_is_binfmt_misc_ops(prog) && + if ((bpf_prog_is_binfmt_misc_ops(prog) || bpf_prog_is_fs_dp_ops(prog)) && !btf_id_set_contains(&bpf_fs_kfunc_lsm_only_ids, kfunc_id)) return 0; return -EACCES; @@ -589,7 +590,8 @@ static int __init bpf_fs_kfuncs_init(void) int ret; ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set); - if (ret || !IS_ENABLED(CONFIG_BINFMT_MISC_BPF)) + if (ret || + (!IS_ENABLED(CONFIG_BINFMT_MISC_BPF) && !IS_ENABLED(CONFIG_FS_DP_BPF))) return ret; return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_fs_kfunc_set); diff --git a/fs/fs_dp.c b/fs/fs_dp.c index 1c370f6ffa42..5b0aba2c182a 100644 --- a/fs/fs_dp.c +++ b/fs/fs_dp.c @@ -191,6 +191,12 @@ static struct bpf_struct_ops bpf_fs_dp_ops = { .owner = THIS_MODULE, }; +bool bpf_prog_is_fs_dp_ops(const struct bpf_prog *prog) +{ + return prog->type == BPF_PROG_TYPE_STRUCT_OPS && + prog->aux->st_ops == &bpf_fs_dp_ops; +} + static int __init fs_dp_init(void) { int ret; diff --git a/include/linux/fs_dp.h b/include/linux/fs_dp.h index ec918c316751..e3a65dedd940 100644 --- a/include/linux/fs_dp.h +++ b/include/linux/fs_dp.h @@ -11,6 +11,7 @@ #define FS_DP_OPS_NAME_MAX 16 +struct bpf_prog; struct file; struct file_kattr; @@ -41,12 +42,17 @@ struct fs_dp_ops { #ifdef CONFIG_FS_DP_BPF struct fs_dp_ops *fs_dp_ops_get(const char *name); void fs_dp_ops_put(struct fs_dp_ops *ops); +bool bpf_prog_is_fs_dp_ops(const struct bpf_prog *prog); #else static inline struct fs_dp_ops *fs_dp_ops_get(const char *name) { return NULL; } static inline void fs_dp_ops_put(struct fs_dp_ops *ops) { } +static inline bool bpf_prog_is_fs_dp_ops(const struct bpf_prog *prog) +{ + return false; +} #endif #endif /* _LINUX_FS_DP_H */ -- 2.56.1