bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
Subsystems (12)
v3 cover bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
Many in-kernel LSMs (SELinux, Smack, IMA) store security labels in extended attributes. For these LSMs, atomic labeling during inode creation is critical: if the inode becomes accessible before its xattr is set, it is briefly unlabeled, which can disrupt LSMs making policy decisions based on file labels.
Existing LSMs solve this by setting xattrs directly in the inode_init_security hook, which runs before the inode becomes accessible. BPF LSM programs currently lack this capability because the hook uses an output parameter (xattr_count) that BPF programs cannot write to, and existing kfuncs like bpf_set_dentry_xattr require a dentry that isn't available until after the inode is accessible.
This series introduces the bpf_init_inode_xattr() kfunc, which takes the combined inode_init_security xattr context argument to access xattrs and xattr_count, and internally writes to xattr_count via lsm_get_xattr_slot().
v3:
- rename struct lsm_xattr_ctx to struct xattr_ctx (Paul)
- increase BPF_LSM_INODE_INIT_XATTRS to 4 (Song)
- enforce per-hook attachment cap at attach time to prevent runtime rejection (Paul)
- add init_inode_xattr_attach_cap selftest
v2:
- pass the xattr state as a combined context object and drop the verifier fixup path (Kumar)
- restrict bpf_init_inode_xattr labels to bpf.* namespace (Matt)
- cap bpf_init_inode_xattr() at BPF_LSM_INODE_INIT_XATTRS slots per invocation (AI)
David Windsor (2):
bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling selftests/bpf: add tests for bpf_init_inode_xattr kfunc
fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++- include/linux/security.h | 5 + kernel/bpf/bpf_lsm.c | 10 ++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 +- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++--- tools/testing/selftests/bpf/bpf_kfuncs.h | 5 + .../selftests/bpf/prog_tests/fs_kfuncs.c | 105 ++++++++++++++++- .../bpf/progs/test_init_inode_xattr.c | 31 +++++ 17 files changed, 306 insertions(+), 39 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_init_inode_xattr.c
base-commit: e771677c937da5808f7b6c1f0e4a97ec1a84f8a8
Signature · 1 line
2.53.0
PATCH v3 1/2 bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
Add bpf_init_inode_xattr() kfunc for BPF LSM programs to atomically set xattrs via the inode_init_security hook using lsm_get_xattr_slot().
The inode_init_security hook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int *xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (struct xattr_ctx) instead, and have bpf_init_inode_xattr() take that context directly. Update the existing in-tree callers of inode_init_security to take and forward the new xattr_ctx.
A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit 6bcdfd2cac55 ("security: Allow all LSMs to provide xattrs for inode_init_security hook") [2], the xattr name is no longer allocated; it is a static constant.
Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)
fs/bpf_fs_kfuncs.c +105 -1
@@ -10,6 +10,7 @@#include <linux/fsnotify.h> #include <linux/file.h> #include <linux/kernfs.h> +#include <linux/lsm_hooks.h> #include <linux/mm.h> #include <linux/xattr.h>
@@ -374,6 +375,97 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry)return d_real_inode (dentry); } +static int bpf_xattrs_used (const struct xattr_ctx *ctx) +{ +const size_t prefix_len =sizeof (XATTR_BPF_LSM_SUFFIX ) -1 ; +int i, n =0 ; + +for (i =0 ; i < *ctx->xattr_count; i++) { +const char *name = ctx->xattrs[i].name; + +if (name && !strncmp (name,XATTR_BPF_LSM_SUFFIX , prefix_len)) + n++; + } +return n; +} + +static int __bpf_init_inode_xattr (struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; +size_t name_len; +void *xattr_value; +struct xattr *xattr; +struct xattr *xattrs; +int *xattr_count; +const void *value; +u32 value_len; + +if (!xattr_ctx || !name__str) +return -EINVAL ; + + xattrs = xattr_ctx->xattrs; + xattr_count = xattr_ctx->xattr_count; +if (!xattrs || !xattr_count) +return -EINVAL ; +if (bpf_xattrs_used (xattr_ctx) >=BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ; + + name_len =strlen (name__str); +if (name_len ==0 || name_len >XATTR_NAME_MAX ) +return -EINVAL ; +if (strncmp (name__str,XATTR_BPF_LSM_SUFFIX , +sizeof (XATTR_BPF_LSM_SUFFIX ) -1 )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); +if (value_len ==0 || value_len >XATTR_SIZE_MAX ) +return -EINVAL ; + + value =__bpf_dynptr_data (value_ptr, value_len); +if (!value) +return -EINVAL ; + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_KERNEL ); +if (!xattr_value) +return -ENOMEM ; + +memcpy (xattr_value, value, value_len); +memcpy (xattr_value + value_len, name__str, name_len); + ((char *)xattr_value)[value_len + name_len] ='\0' ; + + xattr =lsm_get_xattr_slot (xattr_ctx); +if (!xattr) { +kfree (xattr_value); +return -ENOSPC ; + } + + xattr->value = xattr_value; + xattr->name = (const char *)xattr_value + value_len; + xattr->value_len = value_len; + +return 0 ; +} + +/** +* bpf_init_inode_xattr - set an xattr on a new inode from inode_init_security +* @xattr_ctx: inode_init_security xattr state from the hook context +* @name__str: xattr name (e.g., "bpf.file_label") +* @value_p: dynptr containing the xattr value +* +* Only callable from lsm/inode_init_security programs. +* +* Return: 0 on success, negative error on failure. +*/ +__bpf_kfunc int bpf_init_inode_xattr(struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +return __bpf_init_inode_xattr (xattr_ctx, name__str, value_p); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)
@@ -385,13 +477,25 @@ BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_real_inode, KF_SLEEPABLE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_init_inode_xattr, KF_SLEEPABLE)BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) +BTF_ID_LIST (bpf_lsm_inode_init_security_btf_ids ) +BTF_ID (func, bpf_lsm_inode_init_security) + +BTF_ID_LIST (bpf_init_inode_xattr_btf_ids) +BTF_ID (func, bpf_init_inode_xattr) + static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) {if (!btf_id_set8_contains (&bpf_fs_kfunc_set_ids, kfunc_id) || - prog->type ==BPF_PROG_TYPE_LSM ) + prog->type ==BPF_PROG_TYPE_LSM ) { +/* bpf_init_inode_xattr only attaches to inode_init_security. */ +if (kfunc_id == bpf_init_inode_xattr_btf_ids[0 ] && + prog->aux->attach_btf_id != bpf_lsm_inode_init_security_btf_ids[0 ]) +return -EACCES ;return 0 ; + }return -EACCES ; }
include/linux/bpf.h +1 -0
@@ -1752,6 +1752,7 @@ struct bpf_prog_aux {u32 real_func_cnt;/* includes hidden progs, only used for JIT and freeing progs */ u32 func_idx;/* 0 for non-func prog, the index in func array for func prog */ u32 attach_btf_id;/* in-kernel BTF type id to attach to */ +u32 attach_limit;/* max concurrent attachments (0 = unlimited) */ u32 attach_st_ops_member_off;u32 ctx_arg_info_size;u32 max_rdonly_access;
include/linux/bpf_lsm.h +3 -0
@@ -19,6 +19,9 @@#include <linux/lsm_hook_defs.h> #undef LSM_HOOK +/* max bpf xattrs per inode */ +#define BPF_LSM_INODE_INIT_XATTRS 4 +struct bpf_storage_blob {struct bpf_local_storage __rcu *storage; };
include/linux/evm.h +5 -4
@@ -12,6 +12,8 @@#include <linux/integrity.h> #include <linux/xattr.h> +struct xattr_ctx ; +#ifdef CONFIG_EVM extern int evm_set_key (void *key,size_t keylen); extern enum integrity_status evm_verifyxattr(struct dentry *dentry,
@@ -21,8 +23,8 @@ extern enum integrity_status evm_verifyxattr(struct dentry *dentry,int evm_fix_hmac (struct dentry *dentry,const char *xattr_name,const char *xattr_value,size_t xattr_value_len);int evm_inode_init_security (struct inode *inode,struct inode *dir, -const struct qstr *qstr,struct xattr *xattrs, -int *xattr_count); +const struct qstr *qstr, +struct xattr_ctx *xattr_ctx);extern bool evm_revalidate_status (const char *xattr_name);extern int evm_protected_xattr_if_enabled (const char *req_xattr_name); extern int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,
@@ -63,8 +65,7 @@ static inline int evm_fix_hmac(struct dentry *dentry, const char *xattr_name,static inline int evm_inode_init_security (struct inode *inode,struct inode *dir,const struct qstr *qstr, -struct xattr *xattrs, -int *xattr_count) +struct xattr_ctx *xattr_ctx) {return 0 ; }
include/linux/lsm_hook_defs.h +2 -2
@@ -116,8 +116,8 @@ LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode) LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode) LSM_HOOK(void, LSM_RET_VOID, inode_free_security_rcu, void *inode_security) LSM_HOOK(int, -EOPNOTSUPP, inode_init_security, struct inode *inode, - struct inode *dir, const struct qstr *qstr, struct xattr *xattrs, - int *xattr_count) + struct inode *dir, const struct qstr *qstr, + struct xattr_ctx *xattr_ctx) LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode, const struct qstr *name, const struct inode *context_inode) LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
include/linux/lsm_hooks.h +7 -9
@@ -200,20 +200,18 @@ extern struct lsm_static_calls_table static_calls_table __ro_after_init;/** * lsm_get_xattr_slot - Return the next available slot and increment the index -* @xattrs: array storing LSM-provided xattrs -* @xattr_count: number of already stored xattrs (updated) +* @ctx: xattr state shared by inode_init_security hooks * -* Retrieve the first available slot in the @xattrs array to fill with an xattr, -* and increment @xattr_count. +* Retrieve the first available slot in the @ctx->xattrs array to fill with an +* xattr, and increment @ctx->xattr_count. * -* Return: The slot to fill in @xattrs if non-NULL, NULL otherwise. +* Return: The slot to fill in @ctx->xattrs if non-NULL, NULL otherwise. */ -static inline struct xattr *lsm_get_xattr_slot (struct xattr *xattrs, -int *xattr_count) +static inline struct xattr *lsm_get_xattr_slot (struct xattr_ctx *ctx) { -if (unlikely (!xattrs)) +if (unlikely (!ctx || !ctx->xattrs || !ctx->xattr_count))return NULL ; -return &xattrs[(*xattr_count)++]; +return &ctx->xattrs[(*ctx->xattr_count)++]; }#endif /* ! __LINUX_LSM_HOOKS_H */
include/linux/security.h +5 -0
@@ -68,6 +68,11 @@ struct watch;struct watch_notification ;struct lsm_ctx ; +struct xattr_ctx { +struct xattr *xattrs; +int *xattr_count; +}; +/* Default (no) options for the capable function */ #define CAP_OPT_NONE 0x0/* If capable should audit the security request */
kernel/bpf/bpf_lsm.c +10 -0
@@ -113,6 +113,9 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, }#endif +BTF_ID_LIST_SINGLE (bpf_lsm_inode_init_security_btf_ids, func, + bpf_lsm_inode_init_security) +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog) {
@@ -137,6 +140,12 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return -EINVAL ; } +/* bpf reserves a fixed number of xattr slots for itself. +* Set the attach limit so the trampoline rejects excess attaches. +*/ +if (btf_id == bpf_lsm_inode_init_security_btf_ids[0 ]) + prog->aux->attach_limit =BPF_LSM_INODE_INIT_XATTRS ; +return 0 ; }
@@ -315,6 +324,7 @@ BTF_ID(func, bpf_lsm_inode_create) BTF_ID(func, bpf_lsm_inode_free_security) BTF_ID(func, bpf_lsm_inode_getattr) BTF_ID(func, bpf_lsm_inode_getxattr) +BTF_ID(func, bpf_lsm_inode_init_security) BTF_ID(func, bpf_lsm_inode_mknod) BTF_ID(func, bpf_lsm_inode_need_killpriv)BTF_ID (func, bpf_lsm_inode_post_setxattr)
kernel/bpf/trampoline.c +3 -0
@@ -859,6 +859,9 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, }if (cnt >=BPF_MAX_TRAMP_LINKS )return -E2BIG ; +if (node->link->prog->aux->attach_limit && + tr->progs_cnt[kind] >= node->link->prog->aux->attach_limit) +return -E2BIG ;if (!hlist_unhashed (&node->tramp_hlist))/* prog already linked */ return -EBUSY ;
security/bpf/hooks.c +1 -0
@@ -30,6 +30,7 @@ static int __init bpf_lsm_init(void)struct lsm_blob_sizes bpf_lsm_blob_sizes__ro_after_init = { .lbs_inode =sizeof (struct bpf_storage_blob ), + .lbs_xattr_count =BPF_LSM_INODE_INIT_XATTRS , };DEFINE_LSM (bpf) = {
security/integrity/evm/evm_main.c +5 -3
@@ -1062,14 +1062,16 @@ static int evm_inode_copy_up_xattr(struct dentry *src, const char *name)* evm_inode_init_security - initializes security.evm HMAC value */ int evm_inode_init_security (struct inode *inode,struct inode *dir, -const struct qstr *qstr,struct xattr *xattrs, -int *xattr_count) +const struct qstr *qstr, +struct xattr_ctx *xattr_ctx) {struct evm_xattr *xattr_data;struct xattr *xattr, *evm_xattr; +struct xattr *xattrs;bool evm_protected_xattrs = false;int rc; + xattrs = xattr_ctx ? xattr_ctx->xattrs :NULL ;if (!(evm_initialized &EVM_INIT_HMAC ) || !xattrs)return 0 ;
@@ -1087,7 +1089,7 @@ int evm_inode_init_security(struct inode *inode, struct inode *dir,if (!evm_protected_xattrs)return 0 ; - evm_xattr =lsm_get_xattr_slot (xattrs, xattr_count); + evm_xattr =lsm_get_xattr_slot (xattr_ctx);/* * Array terminator (xattr name = NULL) must be the first non-filled * xattr slot.
security/security.c +5 -2
@@ -1334,6 +1334,7 @@ int security_inode_init_security(struct inode *inode, struct inode *dir, {struct lsm_static_call *scall;struct xattr *new_xattrs =NULL ; +struct xattr_ctx xattr_ctx;int ret = -EOPNOTSUPP , xattr_count =0 ;if (unlikely (IS_PRIVATE (inode)))
@@ -1349,10 +1350,12 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,if (!new_xattrs)return -ENOMEM ; } + xattr_ctx.xattrs = new_xattrs; + xattr_ctx.xattr_count = &xattr_count;lsm_for_each_hook (scall, inode_init_security) { - ret = scall->hl->hook.inode_init_security (inode, dir, qstr, new_xattrs, - &xattr_count); + ret = scall->hl->hook.inode_init_security (inode, dir, qstr, + &xattr_ctx);if (ret && ret != -EOPNOTSUPP ) goto out;/*
security/selinux/hooks.c +2 -2
@@ -2962,7 +2962,7 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,static int selinux_inode_init_security (struct inode *inode,struct inode *dir,const struct qstr *qstr, -struct xattr *xattrs,int *xattr_count) +struct xattr_ctx *xattr_ctx) {const struct cred_security_struct *crsec =selinux_cred (current_cred ());struct superblock_security_struct *sbsec;
@@ -2992,7 +2992,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, !(sbsec->flags & SBLABEL_MNT))return -EOPNOTSUPP ; - xattr =lsm_get_xattr_slot (xattrs, xattr_count); + xattr =lsm_get_xattr_slot (xattr_ctx);if (xattr) { rc =security_sid_to_context_force (newsid, &context, &clen);
security/smack/smack_lsm.c +12 -15
@@ -981,10 +981,10 @@ smk_rule_transmutes(struct smack_known *subject, }static int -xattr_dupval (struct xattr *xattrs,int *xattr_count, +xattr_dupval (struct xattr_ctx *xattr_ctx,const char *name,const void *value,unsigned int vallen) { -struct xattr *const xattr =lsm_get_xattr_slot (xattrs, xattr_count); +struct xattr *const xattr =lsm_get_xattr_slot (xattr_ctx);if (!xattr)return 0 ;
@@ -1003,14 +1003,13 @@ xattr_dupval(struct xattr *xattrs, int *xattr_count,* @inode: the newly created inode * @dir: containing directory object * @qstr: unused -* @xattrs: where to put the attributes -* @xattr_count: current number of LSM-provided xattrs (updated) +* @xattr_ctx: where to put attributes and update count * * Returns 0 if it all works out, -ENOMEM if there's no memory */ static int smack_inode_init_security (struct inode *inode,struct inode *dir,const struct qstr *qstr, -struct xattr *xattrs,int *xattr_count) +struct xattr_ctx *xattr_ctx) {struct task_smack *tsp =smack_cred (current_cred ());struct inode_smack *const issp =smack_inode (inode);
@@ -1057,21 +1056,19 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir,if (S_ISDIR (inode->i_mode)) { transflag =SMK_INODE_TRANSMUTE ; -if (xattr_dupval (xattrs, xattr_count, -XATTR_SMACK_TRANSMUTE , -TRANS_TRUE , -TRANS_TRUE_SIZE - )) +if (xattr_dupval (xattr_ctx, +XATTR_SMACK_TRANSMUTE , +TRANS_TRUE , +TRANS_TRUE_SIZE )) rc = -ENOMEM ; } }if (rc ==0 ) -if (xattr_dupval (xattrs, xattr_count, -XATTR_SMACK_SUFFIX , - issp->smk_inode->smk_known, -strlen (issp->smk_inode->smk_known) - )) +if (xattr_dupval (xattr_ctx, +XATTR_SMACK_SUFFIX , + issp->smk_inode->smk_known, +strlen (issp->smk_inode->smk_known))) rc = -ENOMEM ; instant_inode: issp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf_init_inode_xattr incorrectly uses GFP_KERNEL when allocating xattr_value inside the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf_init_inode_xattr incorrectly uses GFP_KERNEL when allocating xattr_value inside the inode_init_security hook, which can trigger filesystem reclaim deadlocks. --
commit 90409fcd43737306bcca3704874f44073960a65f
Author: David Windsor <[email protected]>
bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
This commit adds the bpf_init_inode_xattr() kfunc for BPF LSM programs to atomically set xattrs via the inode_init_security hook. It updates the hook interface to use a single xattr_ctx context object rather than separate arrays and counts.
Quoted 4 lines
fs/bpf_fs_kfuncs.c +0
-0
[ ... ]
Quoted 1 line
fs/bpf_fs_kfuncs.c +0
-0@@ -374,6 +375,97 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry)
[ ... ]
Quoted 4 lines
fs/bpf_fs_kfuncs.c +4
-0+static int __bpf_init_inode_xattr (struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{
[ ... ]
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 431
Quoted 6 lines
fs/bpf_fs_kfuncs.c +6
-0+ value =__bpf_dynptr_data (value_ptr, value_len); +if (!value) +return -EINVAL ; + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_KERNEL );
[Severity: High]
Can this cause a filesystem transaction deadlock?
The inode_init_security hook is called during inode creation, which often happens while filesystems like ext4 or btrfs hold critical filesystem locks or an active journal transaction.
If kmalloc is called with GFP_KERNEL here, the memory allocator can enter direct reclaim. The reclaim path may re-enter the filesystem to write back dirty pages or shrink caches, attempting to acquire the same locks that are already held by the active file creation path.
Other LSMs and the core security framework explicitly use GFP_NOFS during the inode_init_security hook to avoid these reclaim deadlocks. Should this allocation use GFP_NOFS instead?
Quoted 4 lines
fs/bpf_fs_kfuncs.c +4
-0+if (!xattr_value) +return -ENOMEM ; + +memcpy (xattr_value, value, value_len);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewPaul Moore suggested in v2 renaming this to 'struct lsm_xattrs' to avoid overloading the term 'ctx' in the LSM space. The struct was renamed from 'lsm_xattr_ctx' to 'xattr_ctx' in v3, but 'ctx' is…
Quoted 4 lines
fs/bpf_fs_kfuncs.c +0
-0
[ ... ]
include/linux/security.h struct watch; · line 75
Quoted 13 lines
include/linux/security.h +5
-0@@ -68,6 +68,11 @@ struct watch;struct watch_notification ;struct lsm_ctx ; +struct xattr_ctx { +struct xattr *xattrs; +int *xattr_count; +}; +
Paul Moore suggested in v2 renaming this to 'struct lsm_xattrs' to avoid overloading the term 'ctx' in the LSM space. The struct was renamed from 'lsm_xattr_ctx' to 'xattr_ctx' in v3, but 'ctx' is still in the name. Was there a reason to keep 'ctx' in the name, or would renaming to 'lsm_xattrs' as Paul suggested be preferable?
Quoted 3 lines
include/linux/security.h +0
-0/* Default (no) options for the capable function */ #define CAP_OPT_NONE 0x0/* If capable should audit the security request */
[ ... ]
Quoted 15 lines
kernel/bpf/bpf_lsm.c +3
-0@@ -113,6 +113,9 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, }#endif +BTF_ID_LIST_SINGLE (bpf_lsm_inode_init_security_btf_ids, func, + bpf_lsm_inode_init_security) +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog) {
kernel/bpf/bpf_lsm.c int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, · line 145
Quoted 6 lines
kernel/bpf/bpf_lsm.c +3
-0@@ -137,6 +140,12 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return -EINVAL ; } +/* bpf reserves a fixed number of xattr slots for itself. +* Set the attach limit so the trampoline rejects excess attaches. +*/
This isn't a bug, but a subsystem pattern flags this as potentially
concerning: the BPF subsystem comment-style rule for files under kernel/bpf/ requires multi-line comments to have the opening /* on its own line, with comment text starting on the next line. The preferred form would be:
/* * bpf reserves a fixed number of xattr slots for itself. * Set the attach limit so the trampoline rejects excess attaches. */
kernel/bpf/bpf_lsm.c int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, · line 151
Quoted 6 lines
kernel/bpf/bpf_lsm.c +3
-0+if (btf_id == bpf_lsm_inode_init_security_btf_ids[0 ]) + prog->aux->attach_limit =BPF_LSM_INODE_INIT_XATTRS ; +return 0 ; }
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27788616397
Quoted 15 lines · On Thu, Jun 18, 2026 at 5:22 PM <[email protected]> wrote:
[ ... ]
include/linux/security.h struct watch; · line 75
Quoted 6 lines
include/linux/security.h +5
-0+struct xattr_ctx { +struct xattr *xattrs; +int *xattr_count; +}; +Paul Moore suggested in v2 renaming this to 'struct
lsm_xattrs' to avoid overloading the term 'ctx' in the LSM space. The struct was renamed from 'lsm_xattr_ctx' to 'xattr_ctx' in v3, but 'ctx' is still in the name. Was there a reason to keep 'ctx' in the name, or would renaming to 'lsm_xattrs' as Paul suggested be preferable?
Indeed he did; I mistakenly inverted his original request.
Will send a fix for this, as well as converting GFP_KERNEL to GFP_NOFS in the allocation in __bpf_init_inode_xattr.
Quoted 53 lines
Quoted 3 lines
/* Default (no) options for the capable function */ #define
CAP_OPT_NONE0x0
/* If capable should audit the security request */[ ... ]
Quoted 15 lines
kernel/bpf/bpf_lsm.c +3
-0@@ -113,6 +113,9 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, }#endif +BTF_ID_LIST_SINGLE (bpf_lsm_inode_init_security_btf_ids, func, + bpf_lsm_inode_init_security) +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog) {kernel/bpf/bpf_lsm.c int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, · line 145
Quoted 6 lines
kernel/bpf/bpf_lsm.c +3
-0@@ -137,6 +140,12 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return -EINVAL ; } +/* bpf reserves a fixed number of xattr slots for itself. +* Set the attach limit so the trampoline rejects excess attaches. +*/ This isn't a bug, but a subsystem pattern flags this as potentially
concerning: the BPF subsystem comment-style rule for files underkernel/bpf/requires multi-line comments to have the opening /* on its own line, with comment text starting on the next line. The preferred form would be:/*
* bpf reserves a fixed number of xattr slots for itself.
* Set the attach limit so the trampoline rejects excess attaches. */kernel/bpf/bpf_lsm.c int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, · line 151
Quoted 6 lines
kernel/bpf/bpf_lsm.c +3
-0+if (btf_id == bpf_lsm_inode_init_security_btf_ids[0 ]) + prog->aux->attach_limit =BPF_LSM_INODE_INIT_XATTRS ; +return 0 ; }[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/27788616397
Christian Brauner
maintainerQuoted 20 lines · David Windsor
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 6 lines
Suggested-by Song Liu <[email protected]>Signed-off-by David Windsor <[email protected]>--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++-
Please split this into the VFS changes and lsm changes required for this. The api change to the lsm layer can be done independently of any of the actual VFS level wiring. Will also make it a lot nicer to review...
Quoted 13 lines · David Windsor
fs/bpf_fs_kfuncs.c +13
-0+ +static int __bpf_init_inode_xattr (struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; +size_t name_len; +void *xattr_value; +struct xattr *xattr; +struct xattr *xattrs; +int *xattr_count; +const void *value; +u32 value_len;
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 413
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+ +if (!xattr_ctx || !name__str) +return -EINVAL ; + + xattrs = xattr_ctx->xattrs; + xattr_count = xattr_ctx->xattr_count; +if (!xattrs || !xattr_count) +return -EINVAL ; +if (bpf_xattrs_used (xattr_ctx) >=BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ;
This check is good to have, but it's enough. No need to duplicate it. More below.
Quoted 70 lines
fs/bpf_fs_kfuncs.c +61
-0+ + name_len =strlen (name__str); +if (name_len ==0 || name_len >XATTR_NAME_MAX ) +return -EINVAL ; +if (strncmp (name__str,XATTR_BPF_LSM_SUFFIX , +sizeof (XATTR_BPF_LSM_SUFFIX ) -1 )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); +if (value_len ==0 || value_len >XATTR_SIZE_MAX ) +return -EINVAL ; + + value =__bpf_dynptr_data (value_ptr, value_len); +if (!value) +return -EINVAL ; + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_KERNEL ); +if (!xattr_value) +return -ENOMEM ; + +memcpy (xattr_value, value, value_len); +memcpy (xattr_value + value_len, name__str, name_len); + ((char *)xattr_value)[value_len + name_len] ='\0' ; + + xattr =lsm_get_xattr_slot (xattr_ctx); +if (!xattr) { +kfree (xattr_value); +return -ENOSPC ; + } + + xattr->value = xattr_value; + xattr->name = (const char *)xattr_value + value_len; + xattr->value_len = value_len; + +return 0 ; +} + +/** +* bpf_init_inode_xattr - set an xattr on a new inode from inode_init_security +* @xattr_ctx: inode_init_security xattr state from the hook context +* @name__str: xattr name (e.g., "bpf.file_label") +* @value_p: dynptr containing the xattr value +* +* Only callable from lsm/inode_init_security programs. +* +* Return: 0 on success, negative error on failure. +*/ +__bpf_kfunc int bpf_init_inode_xattr(struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +return __bpf_init_inode_xattr (xattr_ctx, name__str, value_p); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)@@ -385,13 +477,25 @@ BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_real_inode, KF_SLEEPABLE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_init_inode_xattr, KF_SLEEPABLE) BTF_KFUNCS_END(bpf_fs_kfunc_set_ids) +BTF_ID_LIST(bpf_lsm_inode_init_security_btf_ids) +BTF_ID(func, bpf_lsm_inode_init_security) + +BTF_ID_LIST(bpf_init_inode_xattr_btf_ids) +BTF_ID(func, bpf_init_inode_xattr)
fs/bpf_fs_kfuncs.c BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) · line 496
Quoted 10 lines
fs/bpf_fs_kfuncs.c +6
-1+static int bpf_fs_kfuncs_filter (const struct bpf_prog *prog,u32 kfunc_id) {if (!btf_id_set8_contains (&bpf_fs_kfunc_set_ids, kfunc_id) || - prog->type ==BPF_PROG_TYPE_LSM ) + prog->type ==BPF_PROG_TYPE_LSM ) { +/* bpf_init_inode_xattr only attaches to inode_init_security. */ +if (kfunc_id == bpf_init_inode_xattr_btf_ids[0 ] && + prog->aux->attach_btf_id != bpf_lsm_inode_init_security_btf_ids[0 ]) +return -EACCES ;
This is unnecessary. Only one hook will have xattr_ctx type. The normal verifier type enforcement will do its work.
Quoted 10 lines
fs/bpf_fs_kfuncs.c +1
-0return 0 ; + }return -EACCES ; }include/linux/bpf.h +0
-0
include/linux/bpf.h struct bpf_prog_aux { · line 1755
Quoted 4 lines
include/linux/bpf.h +1
-0@@ -1752,6 +1752,7 @@ struct bpf_prog_aux {u32 real_func_cnt;/* includes hidden progs, only used for JIT and freeing progs */ u32 func_idx;/* 0 for non-func prog, the index in func array for func prog */ u32 attach_btf_id;/* in-kernel BTF type id to attach to */ +u32 attach_limit;/* max concurrent attachments (0 = unlimited) */
no need.
Quoted 154 lines
include/linux/bpf.h +0
-0u32 attach_st_ops_member_off;u32 ctx_arg_info_size;u32 max_rdonly_access;include/linux/bpf_lsm.h +3
-0@@ -19,6 +19,9 @@#include <linux/lsm_hook_defs.h> #undef LSM_HOOK +/* max bpf xattrs per inode */ +#define BPF_LSM_INODE_INIT_XATTRS 4 +struct bpf_storage_blob {struct bpf_local_storage __rcu *storage; };include/linux/evm.h +5
-4@@ -12,6 +12,8 @@#include <linux/integrity.h> #include <linux/xattr.h> +struct xattr_ctx ; +#ifdef CONFIG_EVM extern int evm_set_key (void *key,size_t keylen); extern enum integrity_status evm_verifyxattr(struct dentry *dentry,@@ -21,8 +23,8 @@ extern enum integrity_status evm_verifyxattr(struct dentry *dentry,int evm_fix_hmac (struct dentry *dentry,const char *xattr_name,const char *xattr_value,size_t xattr_value_len);int evm_inode_init_security (struct inode *inode,struct inode *dir, -const struct qstr *qstr,struct xattr *xattrs, -int *xattr_count); +const struct qstr *qstr, +struct xattr_ctx *xattr_ctx);extern bool evm_revalidate_status (const char *xattr_name);extern int evm_protected_xattr_if_enabled (const char *req_xattr_name); extern int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,@@ -63,8 +65,7 @@ static inline int evm_fix_hmac(struct dentry *dentry, const char *xattr_name,static inline int evm_inode_init_security (struct inode *inode,struct inode *dir,const struct qstr *qstr, -struct xattr *xattrs, -int *xattr_count) +struct xattr_ctx *xattr_ctx) {return 0 ; }include/linux/lsm_hook_defs.h +2
-2@@ -116,8 +116,8 @@ LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode) LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode) LSM_HOOK(void, LSM_RET_VOID, inode_free_security_rcu, void *inode_security) LSM_HOOK(int, -EOPNOTSUPP, inode_init_security, struct inode *inode, - struct inode *dir, const struct qstr *qstr, struct xattr *xattrs, - int *xattr_count) + struct inode *dir, const struct qstr *qstr, + struct xattr_ctx *xattr_ctx) LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode, const struct qstr *name, const struct inode *context_inode) LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,include/linux/lsm_hooks.h +7
-9@@ -200,20 +200,18 @@ extern struct lsm_static_calls_table static_calls_table __ro_after_init;/** * lsm_get_xattr_slot - Return the next available slot and increment the index -* @xattrs: array storing LSM-provided xattrs -* @xattr_count: number of already stored xattrs (updated) +* @ctx: xattr state shared by inode_init_security hooks * -* Retrieve the first available slot in the @xattrs array to fill with an xattr, -* and increment @xattr_count. +* Retrieve the first available slot in the @ctx->xattrs array to fill with an +* xattr, and increment @ctx->xattr_count. * -* Return: The slot to fill in @xattrs if non-NULL, NULL otherwise. +* Return: The slot to fill in @ctx->xattrs if non-NULL, NULL otherwise. */ -static inline struct xattr *lsm_get_xattr_slot (struct xattr *xattrs, -int *xattr_count) +static inline struct xattr *lsm_get_xattr_slot (struct xattr_ctx *ctx) { -if (unlikely (!xattrs)) +if (unlikely (!ctx || !ctx->xattrs || !ctx->xattr_count))return NULL ; -return &xattrs[(*xattr_count)++]; +return &ctx->xattrs[(*ctx->xattr_count)++]; }#endif /* ! __LINUX_LSM_HOOKS_H */ include/linux/security.h +5
-0@@ -68,6 +68,11 @@ struct watch;struct watch_notification ;struct lsm_ctx ; +struct xattr_ctx { +struct xattr *xattrs; +int *xattr_count; +}; +/* Default (no) options for the capable function */ #define CAP_OPT_NONE 0x0/* If capable should audit the security request */ kernel/bpf/bpf_lsm.c +10
-0@@ -113,6 +113,9 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, }#endif +BTF_ID_LIST_SINGLE (bpf_lsm_inode_init_security_btf_ids, func, + bpf_lsm_inode_init_security) +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog) {@@ -137,6 +140,12 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return -EINVAL ; } +/* bpf reserves a fixed number of xattr slots for itself. +* Set the attach limit so the trampoline rejects excess attaches. +*/ +if (btf_id == bpf_lsm_inode_init_security_btf_ids[0 ]) + prog->aux->attach_limit =BPF_LSM_INODE_INIT_XATTRS ; +return 0 ; }@@ -315,6 +324,7 @@ BTF_ID(func, bpf_lsm_inode_create) BTF_ID(func, bpf_lsm_inode_free_security) BTF_ID(func, bpf_lsm_inode_getattr) BTF_ID(func, bpf_lsm_inode_getxattr) +BTF_ID(func, bpf_lsm_inode_init_security) BTF_ID(func, bpf_lsm_inode_mknod) BTF_ID(func, bpf_lsm_inode_need_killpriv)BTF_ID (func, bpf_lsm_inode_post_setxattr)kernel/bpf/trampoline.c +0
-0
kernel/bpf/trampoline.c static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, · line 864
Quoted 6 lines
kernel/bpf/trampoline.c +3
-0@@ -859,6 +859,9 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, }if (cnt >=BPF_MAX_TRAMP_LINKS )return -E2BIG ; +if (node->link->prog->aux->attach_limit && + tr->progs_cnt[kind] >= node->link->prog->aux->attach_limit) +return -E2BIG ;
No need. The check inside kfunc is enough.
Quoted 20 lines
kernel/bpf/trampoline.c +0
-0if (!hlist_unhashed (&node->tramp_hlist))/* prog already linked */ return -EBUSY ;security/bpf/hooks.c +1
-0@@ -30,6 +30,7 @@ static int __init bpf_lsm_init(void)struct lsm_blob_sizes bpf_lsm_blob_sizes__ro_after_init = { .lbs_inode =sizeof (struct bpf_storage_blob ), + .lbs_xattr_count =BPF_LSM_INODE_INIT_XATTRS , };DEFINE_LSM (bpf) = {security/integrity/evm/evm_main.c +0
-0
security/integrity/evm/evm_main.c static int evm_inode_copy_up_xattr(struct dentry *src, const char *name) · line 1066
Quoted 7 lines
security/integrity/evm/evm_main.c +2
-2@@ -1062,14 +1062,16 @@ static int evm_inode_copy_up_xattr(struct dentry *src, const char *name)* evm_inode_init_security - initializes security.evm HMAC value */ int evm_inode_init_security (struct inode *inode,struct inode *dir, -const struct qstr *qstr,struct xattr *xattrs, -int *xattr_count) +const struct qstr *qstr, +struct xattr_ctx *xattr_ctx)
the rest looks good.
Pls split the patch. Introduce xattr_ctx first across the LSMs. Then another patch with a new kfunc.
pw-bot: cr
Quoted 41 lines · Alexei Starovoitov
Quoted 13 lines · David Windsor
fs/bpf_fs_kfuncs.c +13
-0+ +static int __bpf_init_inode_xattr (struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; +size_t name_len; +void *xattr_value; +struct xattr *xattr; +struct xattr *xattrs; +int *xattr_count; +const void *value; +u32 value_len;fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 413
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+ +if (!xattr_ctx || !name__str) +return -EINVAL ; + + xattrs = xattr_ctx->xattrs; + xattr_count = xattr_ctx->xattr_count; +if (!xattrs || !xattr_count) +return -EINVAL ; +if (bpf_xattrs_used (xattr_ctx) >=BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ;This check is good to have, but it's enough. No need to duplicate it. More below.
fs/bpf_fs_kfuncs.c BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) · line 496
Quoted 10 lines
fs/bpf_fs_kfuncs.c +6
-1+static int bpf_fs_kfuncs_filter (const struct bpf_prog *prog,u32 kfunc_id) {if (!btf_id_set8_contains (&bpf_fs_kfunc_set_ids, kfunc_id) || - prog->type ==BPF_PROG_TYPE_LSM ) + prog->type ==BPF_PROG_TYPE_LSM ) { +/* bpf_init_inode_xattr only attaches to inode_init_security. */ +if (kfunc_id == bpf_init_inode_xattr_btf_ids[0 ] && + prog->aux->attach_btf_id != bpf_lsm_inode_init_security_btf_ids[0 ]) +return -EACCES ;
Quoted 3 lines
This is unnecessary. Only one hook will have
xattr_ctxtype. The normal verifier type enforcement will do its work.
Good point, thanks.
Quoted 5 lines
Quoted 5 lines
kernel/bpf/trampoline.c +0
-0@@ -859,6 +859,9 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,
Quoted 9 lines
kernel/bpf/trampoline.c static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, · line 864
Quoted 6 lines
kernel/bpf/trampoline.c +3
-0}if (cnt >=BPF_MAX_TRAMP_LINKS )return -E2BIG ; +if (node->link->prog->aux->attach_limit && + tr->progs_cnt[kind] >= node->link->prog->aux->attach_limit) +return -E2BIG ;No need. The check inside kfunc is enough.
Paul wanted this check because it occurs at bpf prog attach time, whereas the one in the kfunc is at inode creation time.
Quoted 33 lines
Quoted 20 lines
kernel/bpf/trampoline.c +0
-0if (!hlist_unhashed (&node->tramp_hlist))/* prog already linked */ return -EBUSY ;security/bpf/hooks.c +1
-0@@ -30,6 +30,7 @@ static int __init bpf_lsm_init(void)struct lsm_blob_sizes bpf_lsm_blob_sizes__ro_after_init = { .lbs_inode =sizeof (struct bpf_storage_blob ), + .lbs_xattr_count =BPF_LSM_INODE_INIT_XATTRS , };DEFINE_LSM (bpf) = {security/integrity/evm/evm_main.c +0
-0security/integrity/evm/evm_main.c static int evm_inode_copy_up_xattr(struct dentry *src, const char *name) · line 1066
Quoted 7 lines
security/integrity/evm/evm_main.c +2
-2@@ -1062,14 +1062,16 @@ static int evm_inode_copy_up_xattr(struct dentry *src, const char *name)* evm_inode_init_security - initializes security.evm HMAC value */ int evm_inode_init_security (struct inode *inode,struct inode *dir, -const struct qstr *qstr,struct xattr *xattrs, -int *xattr_count) +const struct qstr *qstr, +struct xattr_ctx *xattr_ctx)the rest looks good.
Pls split the patch. Introducexattr_ctxfirst across the LSMs. Then another patch with a new kfunc.pw-bot: cr
Quoted 65 lines · David Windsor
Quoted 41 lines · Alexei Starovoitov
Quoted 13 lines · David Windsor
fs/bpf_fs_kfuncs.c +13
-0+ +static int __bpf_init_inode_xattr (struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; +size_t name_len; +void *xattr_value; +struct xattr *xattr; +struct xattr *xattrs; +int *xattr_count; +const void *value; +u32 value_len;fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 413
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+ +if (!xattr_ctx || !name__str) +return -EINVAL ; + + xattrs = xattr_ctx->xattrs; + xattr_count = xattr_ctx->xattr_count; +if (!xattrs || !xattr_count) +return -EINVAL ; +if (bpf_xattrs_used (xattr_ctx) >=BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ;This check is good to have, but it's enough. No need to duplicate it. More below.
fs/bpf_fs_kfuncs.c BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) · line 496
Quoted 10 lines
fs/bpf_fs_kfuncs.c +6
-1+static int bpf_fs_kfuncs_filter (const struct bpf_prog *prog,u32 kfunc_id) {if (!btf_id_set8_contains (&bpf_fs_kfunc_set_ids, kfunc_id) || - prog->type ==BPF_PROG_TYPE_LSM ) + prog->type ==BPF_PROG_TYPE_LSM ) { +/* bpf_init_inode_xattr only attaches to inode_init_security. */ +if (kfunc_id == bpf_init_inode_xattr_btf_ids[0 ] && + prog->aux->attach_btf_id != bpf_lsm_inode_init_security_btf_ids[0 ]) +return -EACCES ;Quoted 3 lines
This is unnecessary. Only one hook will have
xattr_ctxtype. The normal verifier type enforcement will do its work.Good point, thanks.
Quoted 5 lines
Quoted 5 lines
kernel/bpf/trampoline.c +0
-0@@ -859,6 +859,9 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,Quoted 9 lines
kernel/bpf/trampoline.c static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, · line 864
Quoted 6 lines
kernel/bpf/trampoline.c +3
-0}if (cnt >=BPF_MAX_TRAMP_LINKS )return -E2BIG ; +if (node->link->prog->aux->attach_limit && + tr->progs_cnt[kind] >= node->link->prog->aux->attach_limit) +return -E2BIG ;No need. The check inside kfunc is enough.
Quoted 2 lines
Paul wanted this check because it occurs at bpf prog attach time, whereas the one in the kfunc is at inode creation time.
Sorry, we're not adding redundant code to the verifier.
Quoted 17 lines · Alexei Starovoitov
Quoted 17 lines
Quoted 16 lines
kernel/bpf/trampoline.c static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, · line 864
Quoted 13 lines
kernel/bpf/trampoline.c +3
-0@@ -859,6 +859,9 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, }if (cnt >=BPF_MAX_TRAMP_LINKS )return -E2BIG ; +if (node->link->prog->aux->attach_limit && + tr->progs_cnt[kind] >= node->link->prog->aux->attach_limit) +return -E2BIG ;No need. The check inside kfunc is enough.
Quoted 4 lines
Quoted 2 lines
Paul wanted this check because it occurs at bpf prog attach time, whereas the one in the kfunc is at inode creation time.
Sorry, we're not adding redundant code to the verifier.
Thanks, will send v4 soon.
Paul Moore
maintainerQuoted 25 lines · On Jun 18, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 16 lines
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)
I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.
While moving the kfunc additions to a LSM kfunc file does sort of convert the VFS changes into LSM changes, Christian's comment about splitting this patch two patches, one with the LSM hook changes and one with the BPF additions, is still reasonable and a good suggestion. I would still CC the VFS folks on these patches and I would encourage reviews from the VFS folks as there is a VFS component here, albeit a somewhat small one.
Quoted 38 lines
fs/bpf_fs_kfuncs.c +23
-0@@ -10,6 +10,7 @@#include <linux/fsnotify.h> #include <linux/file.h> #include <linux/kernfs.h> +#include <linux/lsm_hooks.h> #include <linux/mm.h> #include <linux/xattr.h> @@ -374,6 +375,97 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry)return d_real_inode (dentry); } +static int bpf_xattrs_used (const struct xattr_ctx *ctx) +{ +const size_t prefix_len =sizeof (XATTR_BPF_LSM_SUFFIX ) -1 ; +int i, n =0 ; + +for (i =0 ; i < *ctx->xattr_count; i++) { +const char *name = ctx->xattrs[i].name; + +if (name && !strncmp (name,XATTR_BPF_LSM_SUFFIX , prefix_len)) + n++; + } +return n; +} + +static int __bpf_init_inode_xattr (struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; +size_t name_len; +void *xattr_value; +struct xattr *xattr;
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 409
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+struct xattr *xattrs; +int *xattr_count; +const void *value; +u32 value_len; + +if (!xattr_ctx || !name__str) +return -EINVAL ; + + xattrs = xattr_ctx->xattrs; + xattr_count = xattr_ctx->xattr_count;
I'm not sure why the "xattrs" and "xattrs_count" local variables are necessary, especially since they only appear to be used in the sanity check below. I would suggest just using the values in the struct directly.
Quoted 48 lines
fs/bpf_fs_kfuncs.c +48
-0+if (!xattrs || !xattr_count) +return -EINVAL ; +if (bpf_xattrs_used (xattr_ctx) >=BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ; + + name_len =strlen (name__str); +if (name_len ==0 || name_len >XATTR_NAME_MAX ) +return -EINVAL ; +if (strncmp (name__str,XATTR_BPF_LSM_SUFFIX , +sizeof (XATTR_BPF_LSM_SUFFIX ) -1 )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); +if (value_len ==0 || value_len >XATTR_SIZE_MAX ) +return -EINVAL ; + + value =__bpf_dynptr_data (value_ptr, value_len); +if (!value) +return -EINVAL ; + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_KERNEL ); +if (!xattr_value) +return -ENOMEM ; + +memcpy (xattr_value, value, value_len); +memcpy (xattr_value + value_len, name__str, name_len); + ((char *)xattr_value)[value_len + name_len] ='\0' ; + + xattr =lsm_get_xattr_slot (xattr_ctx); +if (!xattr) { +kfree (xattr_value); +return -ENOSPC ; + } + + xattr->value = xattr_value; + xattr->name = (const char *)xattr_value + value_len; + xattr->value_len = value_len; + +return 0 ; +} + +/** +* bpf_init_inode_xattr - set an xattr on a new inode from inode_init_security +* @xattr_ctx: inode_init_security xattr state from the hook context +* @name__str: xattr name (e.g., "bpf.file_label") +* @value_p: dynptr containing the xattr value +*
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 467
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+* Only callable from lsm/inode_init_security programs. +* +* Return: 0 on success, negative error on failure. +*/ +__bpf_kfunc int bpf_init_inode_xattr(struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +return __bpf_init_inode_xattr (xattr_ctx, name__str, value_p); +}
I'm sure there is a reason why you split the code out into __bpf_init_inode_xattr() as opposed to just putting it directly in this kfunc, can you help me understand?
include/linux/security.h struct watch; · line 74
Quoted 12 lines
include/linux/security.h +4
-0@@ -68,6 +68,11 @@ struct watch;struct watch_notification ;struct lsm_ctx ; +struct xattr_ctx { +struct xattr *xattrs; +int *xattr_count; +};
I see the bots already pointed this out, and you acknowledged it, but since I'm looking at this I felt obliged to remind you once again about the rename to "struct lsm_xattrs" :)
Also, looking at this closer, is there a reason why the "xattr_count" field is an integer pointer and not just an integer? We're passing the entire struct by reference to the individual LSMs so we shouldn't need this to get an updated count in the caller and having it as a regular integer should simplify things slightly (you could also make it an unsigned int while you are it).
Quoted 13 lines
include/linux/security.h +1
-0@@ -315,6 +324,7 @@ BTF_ID(func, bpf_lsm_inode_create) BTF_ID(func, bpf_lsm_inode_free_security) BTF_ID(func, bpf_lsm_inode_getattr) BTF_ID(func, bpf_lsm_inode_getxattr) +BTF_ID(func, bpf_lsm_inode_init_security) BTF_ID(func, bpf_lsm_inode_mknod) BTF_ID(func, bpf_lsm_inode_need_killpriv)BTF_ID (func, bpf_lsm_inode_post_setxattr)kernel/bpf/trampoline.c +0
-0
kernel/bpf/trampoline.c static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, · line 864
Quoted 6 lines
kernel/bpf/trampoline.c +3
-0@@ -859,6 +859,9 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, }if (cnt >=BPF_MAX_TRAMP_LINKS )return -E2BIG ; +if (node->link->prog->aux->attach_limit && + tr->progs_cnt[kind] >= node->link->prog->aux->attach_limit) +return -E2BIG ;
Re: Alexei's comments about this - if you look back at my previous comments, my concern was around BPF LSMs using too many slots in the xattr array and causing issues. If the BPF folks want to do that check in the kfunc located in the LSM framework I'm okay with that; the important part is that the BPF LSMs don't use more space than they previously requested.
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1338
Quoted 10 lines
security/security.c +1
-0@@ -1334,6 +1334,7 @@ int security_inode_init_security(struct inode *inode, struct inode *dir, {struct lsm_static_call *scall;struct xattr *new_xattrs =NULL ; +struct xattr_ctx xattr_ctx;int ret = -EOPNOTSUPP , xattr_count =0 ;
Since we have the xattr array/pointer and count in the new "lsm_xattrs" struct, I think we can remove the "new_xattrs" and "xattr_count" local variables in favor of the fields in the new struct.
Quoted 6 lines
security/security.c +1
-0if (unlikely (IS_PRIVATE (inode)))@@ -1349,10 +1350,12 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,if (!new_xattrs)return -ENOMEM ; } + xattr_ctx.xattrs = new_xattrs;
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1361
Quoted 10 lines
security/security.c +3
-2+ xattr_ctx.xattr_count = &xattr_count;lsm_for_each_hook (scall, inode_init_security) { - ret = scall->hl->hook.inode_init_security (inode, dir, qstr, new_xattrs, - &xattr_count); + ret = scall->hl->hook.inode_init_security (inode, dir, qstr, + &xattr_ctx);if (ret && ret != -EOPNOTSUPP ) goto out;/*
--
paul-moore.com
Quoted 8 lines · Paul Moore
I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it
security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.
Makes sense. I will also do similarly for the selftests.
Quoted 7 lines
While moving the kfunc additions to a LSM kfunc file does sort of convert the VFS changes into LSM changes, Christian's comment about splitting this patch two patches, one with the LSM hook changes and one with the BPF additions, is still reasonable and a good suggestion. I would still CC the VFS folks on these patches and I would encourage reviews from the VFS folks as there is a VFS component here, albeit a somewhat small one.
v4 will contain 3 patches: one to introduce struct lsm_xattrs, next to introduce the kfunc, and finally the selftests.
Quoted 16 lines
Quoted 16 lines
fs/bpf_fs_kfuncs.c +16
-0+ +static int __bpf_init_inode_xattr (struct xattr_ctx *xattr_ctx, +const char *name__str, +const struct bpf_dynptr *value_p) +{ +struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; +size_t name_len; +void *xattr_value; +struct xattr *xattr; +struct xattr *xattrs; +int *xattr_count; +const void *value; +u32 value_len; + +if (!xattr_ctx || !name__str) +return -EINVAL ;
Quoted 9 lines
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 409
Quoted 3 lines
fs/bpf_fs_kfuncs.c +3
-0+ + xattrs = xattr_ctx->xattrs; + xattr_count = xattr_ctx->xattr_count;I'm not sure why the "xattrs" and "
xattrs_count" local variables are necessary, especially since they only appear to be used in the sanity check below. I would suggest just using the values in the struct directly.
Leftover from the previous implementation, will fix.
Quoted 12 lines
Quoted 12 lines
fs/bpf_fs_kfuncs.c +12
-0+/** +* bpf_init_inode_xattr - set an xattr on a new inode from inode_init_security +* @xattr_ctx: inode_init_security xattr state from the hook context +* @name__str: xattr name (e.g., "bpf.file_label") +* @value_p: dynptr containing the xattr value +* +* Only callable from lsm/inode_init_security programs. +* +* Return: 0 on success, negative error on failure. +*/ +__bpf_kfunc int bpf_init_inode_xattr(struct xattr_ctx *xattr_ctx, + const char *name__str,
Quoted 8 lines
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 467
Quoted 4 lines
fs/bpf_fs_kfuncs.c +4
-0+ const struct bpf_dynptr *value_p) +{ +return __bpf_init_inode_xattr (xattr_ctx, name__str, value_p); +}I'm sure there is a reason why you split the code out into
__bpf_init_inode_xattr()as opposed to just putting it directly in this kfunc, can you help me understand?
Not sure, perhaps prior convention, or something with the verifier perhaps. I've removed it in -v4 and everything works.
Quoted 8 lines
Quoted 8 lines
include/linux/security.h +0
-0@@ -68,6 +68,11 @@ struct watch;struct watch_notification ;struct lsm_ctx ;
Quoted 9 lines
include/linux/security.h struct watch; · line 74
Quoted 4 lines
include/linux/security.h +4
-0+struct xattr_ctx { +struct xattr *xattrs; +int *xattr_count; +};I see the bots already pointed this out, and you acknowledged it, but since I'm looking at this I felt obliged to remind you once again about the rename to "struct
lsm_xattrs" :)
Yeah, sorry about that. =)
Quoted 7 lines
Also, looking at this closer, is there a reason why the "
xattr_count" field is an integer pointer and not just an integer? We're passing the entire struct by reference to the individual LSMs so we shouldn't need this to get an updated count in the caller and having it as a regular integer should simplify things slightly (you could also make it an unsigned int while you are it).
Copy/paste from v2. Will change xattr_count's type to unsigned int.
Quoted 8 lines
Quoted 8 lines
kernel/bpf/trampoline.c +2
-0@@ -859,6 +859,9 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, }if (cnt >=BPF_MAX_TRAMP_LINKS )return -E2BIG ; +if (node->link->prog->aux->attach_limit && + tr->progs_cnt[kind] >= node->link->prog->aux->attach_limit)
Quoted 9 lines
kernel/bpf/trampoline.c static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, · line 864
Quoted 1 line
kernel/bpf/trampoline.c +1
-0+return -E2BIG ;Re: Alexei's comments about this - if you look back at my previous comments, my concern was around BPF LSMs using too many slots in the xattr array and causing issues. If the BPF folks want to do that check in the kfunc located in the LSM framework I'm okay with that; the important part is that the BPF LSMs don't use more space than they previously requested.
Ack, will remove this attach-time check.
Quoted 6 lines
Quoted 6 lines
security/security.c +0
-0@@ -1334,6 +1334,7 @@ int security_inode_init_security(struct inode *inode, struct inode *dir, {
Quoted 9 lines
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1338
Quoted 4 lines
security/security.c +1
-0struct lsm_static_call *scall;struct xattr *new_xattrs =NULL ; +struct xattr_ctx xattr_ctx;int ret = -EOPNOTSUPP , xattr_count =0 ;Since we have the xattr array/pointer and count in the new "
lsm_xattrs" struct, I think we can remove the "new_xattrs" and "xattr_count" local variables in favor of the fields in the new struct.
Thanks! These will be eliminated in v4.
Best,
David
Christian Brauner
maintainerQuoted 43 lines · Paul Moore
Quoted 24 lines · On Jun 18, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 16 lines
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)
Quoted 6 lines
I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it
security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.
We expose a bunch of VFS heavy operations for various security modules and this is really not different. For xattrs we have it all centralized in the VFS and in general all VFS related bpf kfuncs should continue living there and be registered there. Anything that's just bpf infra specific can go to security/bpf/kfuncs.c instead. But anyway, it's a bpf specific helper so it's the bpf maintainer's call.
Paul Moore
maintainerQuoted 47 lines · Christian Brauner
Quoted 46 lines · Paul Moore
Quoted 24 lines · On Jun 18, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 16 lines
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
Quoted 8 lines
Quoted 3 lines
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it
security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.We expose a bunch of VFS heavy operations for various security modules and this is really not different. For xattrs we have it all centralized in the VFS and in general all VFS related bpf kfuncs should continue living there and be registered there.
This is really LSM specific code dealing with more LSM bits than anything else, it belongs in security/bpf_lsm_kfuncs.c.
Signature · 1 line
paul-moore.com
Quoted 52 lines · Christian Brauner
Quoted 43 lines · Paul Moore
Quoted 24 lines · On Jun 18, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 16 lines
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)Quoted 6 lines
I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it
security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.
Quoted 6 lines
We expose a bunch of VFS heavy operations for various security modules and this is really not different. For xattrs we have it all centralized in the VFS and in general all VFS related bpf kfuncs should continue living there and be registered there. Anything that's just bpf infra specific can go to
security/bpf/kfuncs.cinstead. But anyway, it's a bpf specific helper so it's the bpf maintainer's call.
Completely agree. This is vfs related kfunc and has to be in fs/bpf_fs_kfuncs.c to make sure vfs maintainers review it now and all future changes to it.
Paul Moore
maintainerQuoted 59 lines · Alexei Starovoitov
Quoted 51 lines · Christian Brauner
Quoted 43 lines · Paul Moore
Quoted 24 lines · On Jun 18, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 16 lines
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)Quoted 6 lines
I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it
security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.Quoted 6 lines
We expose a bunch of VFS heavy operations for various security modules and this is really not different. For xattrs we have it all centralized in the VFS and in general all VFS related bpf kfuncs should continue living there and be registered there. Anything that's just bpf infra specific can go to
security/bpf/kfuncs.cinstead. But anyway, it's a bpf specific helper so it's the bpf maintainer's call.
Quoted 3 lines
Completely agree. This is vfs related kfunc and has to be in
fs/bpf_fs_kfuncs.cto make sure vfs maintainers review it now and all future changes to it.
*laughs*
Okay, then split out the LSM specific stuff into security/bpf_lsm_kfuncs.c; all the LSM macros/defines/calls should be in the LSM kfuncs file.
Signature · 1 line
paul-moore.com
Quoted 66 lines · Paul Moore
Quoted 59 lines · Alexei Starovoitov
Quoted 51 lines · Christian Brauner
Quoted 43 lines · Paul Moore
Quoted 24 lines · On Jun 18, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 16 lines
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)Quoted 6 lines
I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it
security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.Quoted 6 lines
We expose a bunch of VFS heavy operations for various security modules and this is really not different. For xattrs we have it all centralized in the VFS and in general all VFS related bpf kfuncs should continue living there and be registered there. Anything that's just bpf infra specific can go to
security/bpf/kfuncs.cinstead. But anyway, it's a bpf specific helper so it's the bpf maintainer's call.Quoted 3 lines
Completely agree. This is vfs related kfunc and has to be in
fs/bpf_fs_kfuncs.cto make sure vfs maintainers review it now and all future changes to it.
Quoted 5 lines
*laughs*
Okay, then split out the LSM specific stuff into
security/bpf_lsm_kfuncs.c; all the LSM macros/defines/calls should be in the LSM kfuncs file.
Paul,
I'm sorry, but you didn't demonstrate the level of understanding of bpf to be trusted to maintain any piece of it.
Paul Moore
maintainerQuoted 68 lines · Alexei Starovoitov
Quoted 67 lines · Paul Moore
Quoted 59 lines · Alexei Starovoitov
Quoted 51 lines · Christian Brauner
Quoted 43 lines · Paul Moore
Quoted 24 lines · On Jun 18, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usinglsm_get_xattr_slot().The
inode_init_securityhook previously took the xattr array and count as two separate output parameters (struct xattr *xattrs, int*xattr_count), which BPF programs cannot write to. Pass the xattr state as a single context object (structxattr_ctx) instead, and havebpf_init_inode_xattr()take that context directly. Update the existing in-tree callers ofinode_init_securityto take and forward the newxattr_ctx.A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit
6bcdfd2cac55("security: Allow all LSMs to provide xattrs forinode_init_securityhook") [2], the xattr name is no longer allocated; it is a static constant.Because we rely on the hook-specific ctx layout, the kfunc is restricted to lsm/inode_init_security. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
Quoted 16 lines
--- fs/bpf_fs_kfuncs.c | 106 +++++++++++++++++++++++++++++- include/linux/bpf.h | 1 + include/linux/bpf_lsm.h | 3 + include/linux/evm.h | 9 +-- include/linux/lsm_hook_defs.h | 4 +- include/linux/lsm_hooks.h | 16 ++--- include/linux/security.h | 5 ++ kernel/bpf/bpf_lsm.c | 10 +++ kernel/bpf/trampoline.c | 3 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 7 +- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 14 files changed, 166 insertions(+), 38 deletions(-)Quoted 6 lines
I have a few specific comments below, inline with the patch, but I wanted to make some general comments too.
The kfunc additions really don't belong in the VFS kfunc file, please create a LSM kfunc file (call it
security/bpf_lsm_kfuncs.c) and add the kfunc code to this new file.Quoted 6 lines
We expose a bunch of VFS heavy operations for various security modules and this is really not different. For xattrs we have it all centralized in the VFS and in general all VFS related bpf kfuncs should continue living there and be registered there. Anything that's just bpf infra specific can go to
security/bpf/kfuncs.cinstead. But anyway, it's a bpf specific helper so it's the bpf maintainer's call.Quoted 3 lines
Completely agree. This is vfs related kfunc and has to be in
fs/bpf_fs_kfuncs.cto make sure vfs maintainers review it now and all future changes to it.*laughs*
Quoted 8 lines
Quoted 3 lines
Okay, then split out the LSM specific stuff into
security/bpf_lsm_kfuncs.c; all the LSM macros/defines/calls should be in the LSM kfuncs file.Paul,
I'm sorry, but you didn't demonstrate the level of understanding of bpf to be trusted to maintain any piece of it.
Alexei,
You haven't demonstrated the understanding or decorum necessary to be entrusted with any part of the LSM framework.
Signature · 1 line
paul-moore.com
PATCH v3 2/2 selftests/bpf: add tests for bpf_init_inode_xattr kfunc
In reply to David WindsorTest bpf atomic inode xattr labeling in inode_init_security.
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 5 + .../selftests/bpf/prog_tests/fs_kfuncs.c | 105 +++++++++++++++++- .../bpf/progs/test_init_inode_xattr.c | 31 ++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_init_inode_xattr.c
tools/testing/selftests/bpf/bpf_kfuncs.h +5 -0
@@ -92,4 +92,9 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str, const struct bpf_dynptr *value_p, int flags) __ksym __weak; extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak; +struct xattr_ctx ; +extern int bpf_init_inode_xattr(struct xattr_ctx *xattr_ctx, + const char *name__str, + const struct bpf_dynptr *value_p) __ksym __weak; +#endif
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +104 -1
125 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_init_inode_xattr.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Cisco Systems, Inc. */ + +#include "vmlinux.h" +#include <bpf/bpf_tracing.h> +#include "bpf_kfuncs.h" + +char _license[] SEC("license") = "GPL"; + +__u32 monitored_pid; +int init_result =-1 ; + +static const char xattr_name[] ="bpf.test_label" ; +char xattr_value[] ="unconfined_u:object_r:user_home_t:s0" ; + +SEC("lsm.s/inode_init_security") +int BPF_PROG (test_init_inode_xattr ,struct inode *inode,struct inode *dir, +const struct qstr *qstr,struct xattr_ctx *xattr_ctx) +{ +struct bpf_dynptr value_ptr; +__u32 pid; + + pid =bpf_get_current_pid_tgid () >>32 ; +if (pid != monitored_pid) +return 0 ; + +bpf_dynptr_from_mem (xattr_value,sizeof (xattr_value),0 , &value_ptr); + init_result =bpf_init_inode_xattr (xattr_ctx, xattr_name, &value_ptr); + +return 0 ; +}
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 2/2] selftests/bpf: add tests for bpf_init_inode_xattr kfunc
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Double free of the `extra` BPF link in the error path of `test_init_inode_xattr_attach_cap()`.…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Double free of the extra BPF link in the error path of test_init_inode_xattr_attach_cap().
- [Medium] Test fails instead of skipping when the filesystem lacks xattr support (EOPNOTSUPP).
- [Low] Test uses open with O_CREAT without O_EXCL or unlinking first, leading to flaky failures if the file already exists. --
commit 5953c8b908f6e9a4380d36de2bc7d988b3ec4923
Author: David Windsor <[email protected]>
selftests/bpf: add tests for bpf_init_inode_xattr kfunc
Test bpf atomic inode xattr labeling in inode_init_security.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +0
-0
[ ... ]
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +10
-0@@ -268,6 +269,102 @@ static void test_fsverity(void)remove (testfile); } +static void test_init_inode_xattr (void ) +{ +struct test_init_inode_xattr *skel =NULL ; +int fd =-1 , err; +char value_out[64 ]; +const char *testfile_new ="/tmp/test_progs_fs_kfuncs_new" ; + + skel =test_init_inode_xattr__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_init_inode_xattr__open_and_load" )) +return ;
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c static void test_fsverity(void) · line 291
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +10
-0+ + skel->bss->monitored_pid =getpid (); + err =test_init_inode_xattr__attach (skel); +if (!ASSERT_OK (err,"test_init_inode_xattr__attach" )) + goto out; + +/* Trigger inode_init_security */ + fd =open (testfile_new,O_CREAT |O_RDWR ,0644 ); +if (!ASSERT_GE (fd,0 ,"create_file" )) + goto out;
[Severity: Low]
If the file already exists from a previously aborted test run, will open() without O_EXCL just open the existing file instead of creating a new one?
If so, this might bypass the inode_init_security hook the test relies on to trigger the BPF program, causing unexpected failures. Would it be safer to add O_EXCL or explicitly unlink the file beforehand?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +4
-0+ +ASSERT_EQ (skel->data->init_result,0 ,"init_result" ); + +/* initxattrs prepends "security." to the name. */
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c static void test_fsverity(void) · line 305
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +10
-0+ err =getxattr (testfile_new,"security.bpf.test_label" , value_out, +sizeof (value_out)); +if (err <0 && errno ==ENODATA ) { +printf ("%s:SKIP:filesystem did not apply LSM xattrs\n" , + __func__); +test__skip (); + goto out; + } +if (!ASSERT_GE (err,0 ,"getxattr" )) + goto out;
[Severity: Medium]
Does this skip logic need to handle errno == EOPNOTSUPP as well?
If the test runs on a filesystem that doesn't support xattrs at all (like some tmpfs configurations), getxattr() might return EOPNOTSUPP. The test would then fall through to the ASSERT_GE() check and fail rather than gracefully skipping.
[ ... ]
Quoted 30 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +30
-0+static void test_init_inode_xattr_attach_cap (void ) +{ +struct test_init_inode_xattr *skel[INIT_INODE_XATTR_MAX +1 ] = {}; +struct bpf_link *link[INIT_INODE_XATTR_MAX +1 ] = {}; +struct bpf_link *extra =NULL ; +int i, err; + +/* Fill all available xattr slots */ +for (i =0 ; i <INIT_INODE_XATTR_MAX ; i++) { + skel[i] =test_init_inode_xattr__open_and_load (); +if (!ASSERT_OK_PTR (skel[i],"open_and_load" )) + goto out; + + link[i] =bpf_program__attach_lsm (skel[i]->progs.test_init_inode_xattr); +if (!ASSERT_OK_PTR (link[i],"attach_within_cap" )) + goto out; + } + + skel[INIT_INODE_XATTR_MAX ] =test_init_inode_xattr__open_and_load (); +if (!ASSERT_OK_PTR (skel[INIT_INODE_XATTR_MAX ],"open_and_load_extra" )) + goto out; + +/* New additions fail with -E2BIG */ + extra =bpf_program__attach_lsm (skel[INIT_INODE_XATTR_MAX ]->progs.test_init_inode_xattr); + err = -errno; +if (!ASSERT_ERR_PTR (extra,"attach_over_cap_should_fail" )) { +bpf_link__destroy (extra); + goto out; + } +ASSERT_EQ (err, -E2BIG ,"attach_over_cap_errno" );
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c static void test_fsverity(void) · line 361
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +10
-0+ +bpf_link__destroy (link[0 ]); + link[0 ] =NULL ;/* avoid double free in cleanup */ + +/* Freeing a slot lets the extra program attach */ + extra =bpf_program__attach_lsm (skel[INIT_INODE_XATTR_MAX ]->progs.test_init_inode_xattr); +ASSERT_OK_PTR (extra,"attach_after_detach" ); + +out: +bpf_link__destroy (extra);
[Severity: Medium]
Can this cause a double free of the extra link if the first assert fails?
If extra is unexpectedly valid in the attach_over_cap_should_fail check, bpf_link__destroy(extra) is called before jumping to the out label. Once at the out label, bpf_link__destroy(extra) will be called a second time on the exact same pointer since it wasn't set to NULL before the jump.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2