summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@kernel.org>2025-02-13 19:35:32 -0800
committerAlexei Starovoitov <ast@kernel.org>2025-02-13 19:35:32 -0800
commit68a41544909eff32df9a84c89cd10ec328a1858c (patch)
tree0eee579fa9aabf4327140d49854d538b0cf1487d /kernel
parentb99f27e90268b1a814c13f8bd72ea1db448ea257 (diff)
parent60c2e1fa9168644db157665a44982861aa67dbef (diff)
Merge branch 'enable-writing-xattr-from-bpf-programs'
Song Liu says: ==================== Enable writing xattr from BPF programs Add support to set and remove xattr from BPF program. Also add security.bpf. xattr name prefix. kfuncs are added to set and remove xattrs with security.bpf. name prefix. Update kfuncs bpf_get_[file|dentry]_xattr to read xattrs with security.bpf. name prefix. Note that BPF programs can read user. xattrs, but not write and remove them. To pick the right version of kfunc to use, a remap logic is added to btf_kfunc_id_set. This helps move some kfunc specific logic off the verifier core code. Also use this remap logic to select bpf_dynptr_from_skb or bpf_dynptr_from_skb_rdonly. Cover letter of v1 and v2: Follow up discussion in LPC 2024 [1], that we need security.bpf xattr prefix. This set adds "security.bpf." xattr name prefix, and allows bpf kfuncs bpf_get_[file|dentry]_xattr() to read these xattrs. [1] https://lpc.events/event/18/contributions/1940/ --- Changes v11 => v12: 1. Drop btf_kfunc_id_set.remap and changes for bpf_dynptr_from_skb. (Alexei) 2. Minor refactoring in patch 1. (Matt Bobrowski) v11: https://lore.kernel.org/bpf/20250129205957.2457655-1-song@kernel.org/ Changes v10 => v11: 1. Add Acked-by from Christian Brauner. 2. Fix selftests build error like this one: https://github.com/kernel-patches/bpf/actions/runs/13022268618/job/36325472992 3. Rename some variables in the selftests. v10: https://lore.kernel.org/bpf/20250124202911.3264715-1-song@kernel.org/ Changes v9 => v10: 1. Refactor bpf_[set|remove]_dentry_xattr[_locked]. (Christian Brauner). v9: https://lore.kernel.org/bpf/20250110011342.2965136-1-song@kernel.org/ Changes v8 => v9 1. Fix build for CONFIG_DEBUG_INFO_BTF=n case. (kernel test robot) v8: https://lore.kernel.org/bpf/20250108225140.3467654-1-song@kernel.org/ Changes v7 => v8 1. Rebase and resolve conflicts. v7: https://lore.kernel.org/bpf/20241219221439.2455664-1-song@kernel.org/ Changes v6 => v7 1. Move btf_kfunc_id_remap() to the right place. (Bug reported by CI) v6: https://lore.kernel.org/bpf/20241219202536.1625216-1-song@kernel.org/ Changes v5 => v6 1. Hide _locked version of the kfuncs from vmlinux.h (Alexei) 2. Add remap logic to btf_kfunc_id_set and use that to pick the correct version of kfuncs to use. 3. Also use the remap logic for bpf_dynptr_from_skb[|_rdonly]. v5: https://lore.kernel.org/bpf/20241218044711.1723221-1-song@kernel.org/ Changes v4 => v5 1. Let verifier pick proper kfunc (_locked or not _locked) based on the calling context. (Alexei) 2. Remove the __failure test (6/6 of v4). v4: https://lore.kernel.org/bpf/20241217063821.482857-1-song@kernel.org/ Changes v3 => v4 1. Do write permission check with inode locked. (Jan Kara) 2. Fix some source_inline warnings. v3: https://lore.kernel.org/bpf/20241210220627.2800362-1-song@kernel.org/ Changes v2 => v3 1. Add kfuncs to set and remove xattr from BPF programs. v2: https://lore.kernel.org/bpf/20241016070955.375923-1-song@kernel.org/ Changes v1 => v2 1. Update comment of bpf_get_[file|dentry]_xattr. (Jiri Olsa) 2. Fix comment for return value of bpf_get_[file|dentry]_xattr. v1: https://lore.kernel.org/bpf/20241002214637.3625277-1-song@kernel.org/ ==================== Link: https://patch.msgid.link/20250130213549.3353349-1-song@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/bpf_lsm.c2
-rw-r--r--kernel/bpf/verifier.c21
2 files changed, 23 insertions, 0 deletions
diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c
index 967492b65185..0a59df1c550a 100644
--- a/kernel/bpf/bpf_lsm.c
+++ b/kernel/bpf/bpf_lsm.c
@@ -316,7 +316,9 @@ BTF_ID(func, bpf_lsm_inode_getxattr)
BTF_ID(func, bpf_lsm_inode_mknod)
BTF_ID(func, bpf_lsm_inode_need_killpriv)
BTF_ID(func, bpf_lsm_inode_post_setxattr)
+BTF_ID(func, bpf_lsm_inode_post_removexattr)
BTF_ID(func, bpf_lsm_inode_readlink)
+BTF_ID(func, bpf_lsm_inode_removexattr)
BTF_ID(func, bpf_lsm_inode_rename)
BTF_ID(func, bpf_lsm_inode_rmdir)
BTF_ID(func, bpf_lsm_inode_setattr)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 9971c03adfd5..04d1d75d9ff9 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11766,6 +11766,8 @@ enum special_kfunc_type {
KF_bpf_iter_num_new,
KF_bpf_iter_num_next,
KF_bpf_iter_num_destroy,
+ KF_bpf_set_dentry_xattr,
+ KF_bpf_remove_dentry_xattr,
};
BTF_SET_START(special_kfunc_set)
@@ -11795,6 +11797,10 @@ BTF_ID(func, bpf_wq_set_callback_impl)
#ifdef CONFIG_CGROUPS
BTF_ID(func, bpf_iter_css_task_new)
#endif
+#ifdef CONFIG_BPF_LSM
+BTF_ID(func, bpf_set_dentry_xattr)
+BTF_ID(func, bpf_remove_dentry_xattr)
+#endif
BTF_SET_END(special_kfunc_set)
BTF_ID_LIST(special_kfunc_list)
@@ -11844,6 +11850,13 @@ BTF_ID(func, bpf_local_irq_restore)
BTF_ID(func, bpf_iter_num_new)
BTF_ID(func, bpf_iter_num_next)
BTF_ID(func, bpf_iter_num_destroy)
+#ifdef CONFIG_BPF_LSM
+BTF_ID(func, bpf_set_dentry_xattr)
+BTF_ID(func, bpf_remove_dentry_xattr)
+#else
+BTF_ID_UNUSED
+BTF_ID_UNUSED
+#endif
static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta)
{
@@ -20924,6 +20937,14 @@ static void specialize_kfunc(struct bpf_verifier_env *env,
*/
env->seen_direct_write = seen_direct_write;
}
+
+ if (func_id == special_kfunc_list[KF_bpf_set_dentry_xattr] &&
+ bpf_lsm_has_d_inode_locked(prog))
+ *addr = (unsigned long)bpf_set_dentry_xattr_locked;
+
+ if (func_id == special_kfunc_list[KF_bpf_remove_dentry_xattr] &&
+ bpf_lsm_has_d_inode_locked(prog))
+ *addr = (unsigned long)bpf_remove_dentry_xattr_locked;
}
static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,