bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
Subsystems (12)
v4 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().
v4:
- introduce struct lsm_xattrs in separate patch (Alexei, Paul)
- rename struct xattr_ctx to struct lsm_xattrs (Paul)
- make lsm_xattrs.xattr_count unsigned int (Paul)
- drop new_xattrs/xattr_count locals in security_inode_init_security() (Paul)
- fold __bpf_init_inode_xattr() into bpf_init_inode_xattr() (Paul)
- drop bpf_fs_kfuncs_filter() attach-point check; rely on verifier type enforcement (Alexei)
- drop attach-time cap; enforce slot budget in the kfunc (Alexei)
- allocate the combined xattr with GFP_NOFS (sashiko-bot)
- replace init_inode_xattr_attach_cap selftest with runtime init_inode_xattr_slot_limit
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 (3):
security: pass inode_init_security xattrs via struct lsm_xattrs
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 | 79 +++++++++++ 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 | 1 + security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 +- security/security.c | 24 ++-- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++-- tools/testing/selftests/bpf/bpf_kfuncs.h | 5 + .../selftests/bpf/prog_tests/lsm_kfuncs.c | 129 ++++++++++++++++++ .../bpf/progs/test_init_inode_xattr.c | 31 +++++ 15 files changed, 299 insertions(+), 47 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/test_init_inode_xattr.c
base-commit: e771677c937da5808f7b6c1f0e4a97ec1a84f8a8
Signature · 1 line
2.53.0
PATCH v4 1/3 security: pass inode_init_security xattrs via struct lsm_xattrs
inode_init_security receives the LSM xattr array and its count as separate parameters. For better compatibility with the bpf verifier, update inode_init_security and its callers to consolidate these parameters into a single context object: struct lsm_xattrs.
--- include/linux/evm.h | 9 +++++---- include/linux/lsm_hook_defs.h | 4 ++-- include/linux/lsm_hooks.h | 16 +++++++--------- include/linux/security.h | 5 +++++ security/integrity/evm/evm_main.c | 8 +++++--- security/security.c | 24 ++++++++++++------------ security/selinux/hooks.c | 4 ++-- security/smack/smack_lsm.c | 27 ++++++++++++--------------- 8 files changed, 50 insertions(+), 47 deletions(-)
include/linux/evm.h +5 -4
@@ -12,6 +12,8 @@#include <linux/integrity.h> #include <linux/xattr.h> +struct lsm_xattrs ; +#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 lsm_xattrs *xattrs);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 lsm_xattrs *xattrs) {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 lsm_xattrs *xattrs) 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 lsm_xattrs *ctx) { -if (unlikely (!xattrs)) +if (unlikely (!ctx || !ctx->xattrs))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 lsm_xattrs { +struct xattr *xattrs; +unsigned int xattr_count; +}; +/* Default (no) options for the capable function */ #define CAP_OPT_NONE 0x0/* If capable should audit the security request */
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 lsm_xattrs *lsm_xattrs) {struct evm_xattr *xattr_data;struct xattr *xattr, *evm_xattr; +struct xattr *xattrs;bool evm_protected_xattrs = false;int rc; + xattrs = lsm_xattrs ? lsm_xattrs->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 (lsm_xattrs);/* * Array terminator (xattr name = NULL) must be the first non-filled * xattr slot.
security/security.c +12 -12
@@ -1333,8 +1333,8 @@ int security_inode_init_security(struct inode *inode, struct inode *dir, const initxattrs initxattrs, void *fs_data) { struct lsm_static_call *scall; -struct xattr *new_xattrs =NULL ; -int ret = -EOPNOTSUPP , xattr_count =0 ; +struct lsm_xattrs xattrs = {}; +int ret = -EOPNOTSUPP ;if (unlikely (IS_PRIVATE (inode)))return 0 ;
@@ -1344,15 +1344,15 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,if (initxattrs) {/* Allocate +1 as terminator. */ - new_xattrs =kcalloc (blob_sizes.lbs_xattr_count +1 , -sizeof (*new_xattrs),GFP_NOFS ); -if (!new_xattrs) + xattrs.xattrs =kcalloc (blob_sizes.lbs_xattr_count +1 , +sizeof (*xattrs.xattrs),GFP_NOFS ); +if (!xattrs.xattrs)return -ENOMEM ; }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, + &xattrs);if (ret && ret != -EOPNOTSUPP ) goto out;/*
@@ -1364,14 +1364,14 @@ int security_inode_init_security(struct inode *inode, struct inode *dir, }/* If initxattrs() is NULL, xattr_count is zero, skip the call. */ -if (!xattr_count) +if (!xattrs.xattr_count) goto out; - ret =initxattrs (inode, new_xattrs, fs_data); + ret =initxattrs (inode, xattrs.xattrs, fs_data); out: -for (; xattr_count >0 ; xattr_count--) -kfree (new_xattrs[xattr_count -1 ].value); -kfree (new_xattrs); +for (; xattrs.xattr_count >0 ; xattrs.xattr_count--) +kfree (xattrs.xattrs[xattrs.xattr_count -1 ].value); +kfree (xattrs.xattrs);return (ret == -EOPNOTSUPP ) ?0 : ret; }EXPORT_SYMBOL (security_inode_init_security);
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 lsm_xattrs *xattrs) {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 (xattrs);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 lsm_xattrs *xattrs,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 (xattrs);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) +* @xattrs: 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 lsm_xattrs *xattrs) {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 (xattrs, +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 (xattrs, +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
PATCH v4 2/3 bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
In reply to David WindsorAdd 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 hook now passes its xattr state as a single struct lsm_xattrs object, which the kfunc takes directly.
The kfunc is only usable from lsm/inode_init_security programs: no other hook exposes a struct lsm_xattrs argument, so the verifier rejects calls from elsewhere. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.
BPF reserves BPF_LSM_INODE_INIT_XATTRS slots via lbs_xattr_count, and the kfunc enforces that BPF never consumes more slots than it reserved, returning -ENOSPC once the budget is exhausted.
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.
--- fs/bpf_fs_kfuncs.c | 79 +++++++++++++++++++++++++++++++++++++++++ include/linux/bpf_lsm.h | 3 ++ kernel/bpf/bpf_lsm.c | 1 + security/bpf/hooks.c | 1 + 4 files changed, 84 insertions(+)
fs/bpf_fs_kfuncs.c +79 -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,83 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry)return d_real_inode (dentry); } +static int bpf_xattrs_used (const struct lsm_xattrs *ctx) +{ +const size_t prefix_len =sizeof (XATTR_BPF_LSM_SUFFIX ) -1 ; +unsigned 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; +} + +/** +* bpf_init_inode_xattr - set an xattr on a new inode from inode_init_security +* @xattrs: 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 lsm_xattrs *xattrs, +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; +const void *value; +u32 value_len; + +if (!xattrs || !xattrs->xattrs || !name__str) +return -EINVAL ; +if (bpf_xattrs_used (xattrs) >=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_NOFS ); +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 (xattrs); +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_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)
@@ -385,6 +463,7 @@ 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) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
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; };
kernel/bpf/bpf_lsm.c +1 -0
@@ -315,6 +315,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)
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) = {
Signature · 1 line
2.53.0
PATCH v4 3/3 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 | 117 ++++++++++++++++++ .../bpf/progs/test_init_inode_xattr.c | 31 +++++ 3 files changed, 153 insertions(+) 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 lsm_xattrs ; +extern int bpf_init_inode_xattr(struct lsm_xattrs *xattrs, + const char *name__str, + const struct bpf_dynptr *value_p) __ksym __weak; +#endif
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +117 -0
133 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 lsm_xattrs *xattrs) +{ +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 (xattrs, xattr_name, &value_ptr); + +return 0 ; +}
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v4 bpf-next 1/3] security: pass inode_init_security xattrs via struct lsm_xattrs
In reply to David WindsorSashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
inode_init_securityreceives the LSM xattr array and its count as separate parameters. For better compatibility with the bpf verifier, updateinode_init_securityand its callers to consolidate these parameters into a single context object: structlsm_xattrs.Signed-off-by David Windsor <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
On Thu, Jun 25, 2026 at 7:23 AM Christian Brauner <[email protected]> wrote:
<snip>
Quoted 7 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.
After Alexei's requested changes removing the attach-time checks, there's really not much left to go in an LSM-specific kfuncs file. The bpf infra plumbing for registering the kfunc and bpf_xattrs_used() seem to be the only LSM-specific bits aside from the kfunc.
I am willing to put this code anywhere. I've tried to CC all involved in all 3 patches, even though there's some split in concerns.
[email protected]
AI reviewRe: [PATCH v4 bpf-next 3/3] selftests/bpf: add tests for bpf_init_inode_xattr kfunc
In reply to David WindsorThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The tests use predictable temporary file paths with `open(..., O_CREAT)` but without `O_EXCL` or…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The tests use predictable temporary file paths with open(..., O_CREAT) but without O_EXCL or prior cleanup, leading to test flakiness and a symlink attack vulnerability.
- [Medium] The slot limit test triggers duplicate xattr name creations during inode_init_security, causing the test to fail on filesystems like ext4 that enforce XATTR_CREATE. --
commit 9defa5666243f46c3a058ad117a6ee0ace1b9f5c
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 16 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +8
-0@@ -268,6 +269,116 @@ 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 ();
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c static void test_fsverity(void) · line 289
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +10
-0+if (!ASSERT_OK_PTR (skel,"test_init_inode_xattr__open_and_load" )) +return ; + + 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 );
[Severity: Medium]
Since this test runs as root and uses predictable paths in /tmp/, can this allow an attacker to place a symlink to overwrite arbitrary system files?
Additionally, if a previous run leaves the file behind (e.g., via panic or early abort), open(O_CREAT) without O_EXCL will open the existing file instead of creating a new one. This bypasses the inode_init_security hook entirely, which can cause the test's assertions on init_result to fail spuriously.
Would it be better to add O_EXCL to the open() flags or unlink the file before opening?
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +2
-0+if (!ASSERT_GE (fd,0 ,"create_file" )) + goto out;
[ ... ]
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +13
-0+static void test_init_inode_xattr_slot_limit (void ) +{ +struct test_init_inode_xattr *skel[INIT_INODE_XATTR_MAX +1 ] = {}; +struct bpf_link *link[INIT_INODE_XATTR_MAX +1 ] = {}; +const char *testfile_slot ="/tmp/test_progs_fs_kfuncs_slot" ; +int ok =0 , nospc =0 , other =0 ; +int i, fd =-1 ; + +/* All programs attach successfully; there is no attach-time cap. */ +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;
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c static void test_fsverity(void) · line 348
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +10
-0+ + skel[i]->bss->monitored_pid =getpid (); + + link[i] =bpf_program__attach_lsm (skel[i]->progs.test_init_inode_xattr); +if (!ASSERT_OK_PTR (link[i],"attach" )) + goto out; + } + +/* Trigger inode_init_security once with all programs attached. */ + fd =open (testfile_slot,O_CREAT |O_RDWR ,0644 );
[Severity: Medium]
Will this test reliably fail on filesystems like ext4?
The test attaches 5 identical BPF programs to inode_init_security, which all attempt to add an xattr with the exact same name (bpf.test_label).
On ext4, ext4_initxattrs() passes the XATTR_CREATE flag to ext4_xattr_set_handle(), meaning the second identical xattr creation fails with -EEXIST. This failure propagates up to the VFS, causing the open() syscall here to fail with -EEXIST and breaking the test before it can validate the slot limits.
Should the attached BPF programs generate unique xattr names?
(Also, similar to test_init_inode_xattr(), this open() call might benefit from O_EXCL to prevent symlink attacks and test flakiness).
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +2
-0+if (!ASSERT_GE (fd,0 ,"create_file" )) + goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Marking `bpf_lsm_inode_init_security` as a sleepable hook allows BPF programs to trigger page…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Marking bpf_lsm_inode_init_security as a sleepable hook allows BPF programs to trigger page faults while holding a filesystem transaction handle, leading to deadlocks. --
commit 78be3e5567e8821124f5c2f9f979f1274f553583
Author: David Windsor <[email protected]>
bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
This patch adds the bpf_init_inode_xattr kfunc to allow BPF LSM programs to atomically set xattrs via the inode_init_security hook. The kfunc uses lsm_get_xattr_slot and restricts usage to the bpf.* namespace.
Quoted 4 lines
fs/bpf_fs_kfuncs.c +0
-0
[ ... ]
fs/bpf_fs_kfuncs.c BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) · line 466
Quoted 5 lines
fs/bpf_fs_kfuncs.c +1
-0@@ -385,6 +463,7 @@ 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 )
[Severity: High]
Does marking this kfunc as sleepable create a risk of deadlocks?
Since the kfunc is only callable from the inode_init_security hook, marking the hook as sleepable allows BPF programs to trigger page faults (e.g., via bpf_copy_from_user). When a file is created (e.g., ext4_create), the filesystem acquires a transaction handle before invoking the hook. If the BPF program triggers a page fault, it will attempt to take mmap_read_lock.
Concurrently, another thread doing a write page fault (or mmap/mprotect) might acquire mmap_lock and attempt to start a transaction (e.g., in ext4_page_mkwrite), which blocks on the transaction handle held by the first process. This creates an AB-BA deadlock between the filesystem transaction and mmap_lock.
Quoted 3 lines
fs/bpf_fs_kfuncs.c +0
-0BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
[ ... ]
kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_inode_create) · line 318
Quoted 9 lines
kernel/bpf/bpf_lsm.c +1
-0@@ -315,6 +315,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)
[Severity: High]
Is it safe to add bpf_lsm_inode_init_security to the sleepable hooks list?
As mentioned above, marking this hook as sleepable permits BPF programs to take page faults from within a filesystem transaction, which violates filesystem locking rules and can freeze filesystem operations.
Quoted 3 lines
kernel/bpf/bpf_lsm.c +0
-0BTF_ID(func, bpf_lsm_inode_mknod) BTF_ID(func, bpf_lsm_inode_need_killpriv)BTF_ID (func, bpf_lsm_inode_post_setxattr)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Paul Moore
maintainerQuoted 112 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 hook now passes its xattr state as a single structlsm_xattrsobject, which the kfunc takes directly.The kfunc is only usable from lsm/inode_init_security programs: no other hook exposes a struct
lsm_xattrsargument, so the verifier rejects calls from elsewhere. Restrict the xattr names that may be set via this kfunc to the bpf.* namespace.BPF reserves
BPF_LSM_INODE_INIT_XATTRSslots vialbs_xattr_count, and the kfunc enforces that BPF never consumes more slots than it reserved, returning-ENOSPConce the budget is exhausted.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.Signed-off-by David Windsor <[email protected]>--- fs/bpf_fs_kfuncs.c | 79 +++++++++++++++++++++++++++++++++++++++++ include/linux/bpf_lsm.h | 3 ++ kernel/bpf/bpf_lsm.c | 1 + security/bpf/hooks.c | 1 + 4 files changed, 84 insertions(+)fs/bpf_fs_kfuncs.c +67
-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,83 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry)return d_real_inode (dentry); } +static int bpf_xattrs_used (const struct lsm_xattrs *ctx) +{ +const size_t prefix_len =sizeof (XATTR_BPF_LSM_SUFFIX ) -1 ; +unsigned 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; +} + +/** +* bpf_init_inode_xattr - set an xattr on a new inode from inode_init_security +* @xattrs: 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 lsm_xattrs *xattrs, +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; +const void *value; +u32 value_len; + +if (!xattrs || !xattrs->xattrs || !name__str) +return -EINVAL ; +if (bpf_xattrs_used (xattrs) >=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_NOFS ); +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 (xattrs); +if (!xattr) {
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 453
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+kfree (xattr_value); +return -ENOSPC ; + } + + xattr->value = xattr_value; + xattr->name = (const char *)xattr_value + value_len; + xattr->value_len = value_len; + +return 0 ; +}
This is not a generic VFS function, it is a LSM specific function, it belongs under security/, please move the code as discussed previously.
--
paul-moore.com
Quoted 47 lines · Paul Moore
Quoted 47 lines
fs/bpf_fs_kfuncs.c +47
-0+__bpf_kfunc int bpf_init_inode_xattr(struct lsm_xattrs *xattrs, +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; +const void *value; +u32 value_len; + +if (!xattrs || !xattrs->xattrs || !name__str) +return -EINVAL ; +if (bpf_xattrs_used (xattrs) >=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_NOFS ); +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 (xattrs); +if (!xattr) { +kfree (xattr_value); +return -ENOSPC ; + } + + xattr->value = xattr_value;
Quoted 8 lines
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 453
Quoted 5 lines
fs/bpf_fs_kfuncs.c +5
-0+ xattr->name = (const char *)xattr_value + value_len; + xattr->value_len = value_len; + +return 0 ; +}This is not a generic VFS function, it is a LSM specific function, it belongs under
security/, please move the code as discussed previously.
Paul,
Not quite. It's all about xattrs.
Having "struct lsm_xattrs" in the arguments doesn't make it lsm related. You needs to study existing kfuncs and tracepoints.
A bunch of them have "*lsm*" in the arguments.
All,
CI found issues, so this set needs another respin. After that it's hopefully good to go.
David,
you're on the right track. The patchset is getting close. Thank you for working on it.
Paul Moore
maintainerQuoted 57 lines · Alexei Starovoitov
Quoted 47 lines · Paul Moore
Quoted 47 lines
fs/bpf_fs_kfuncs.c +47
-0+__bpf_kfunc int bpf_init_inode_xattr(struct lsm_xattrs *xattrs, +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; +const void *value; +u32 value_len; + +if (!xattrs || !xattrs->xattrs || !name__str) +return -EINVAL ; +if (bpf_xattrs_used (xattrs) >=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_NOFS ); +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 (xattrs); +if (!xattr) { +kfree (xattr_value); +return -ENOSPC ; + } + + xattr->value = xattr_value;Quoted 8 lines
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 453
Quoted 5 lines
fs/bpf_fs_kfuncs.c +5
-0+ xattr->name = (const char *)xattr_value + value_len; + xattr->value_len = value_len; + +return 0 ; +}This is not a generic VFS function, it is a LSM specific function, it belongs under
security/, please move the code as discussed previously.
Quoted 5 lines
Paul,
Not quite. It's all about xattrs.
Having "structlsm_xattrs" in the arguments doesn't make it lsm related. You needs to study existing kfuncs and tracepoints.
A bunch of them have "*lsm*" in the arguments.
Alexei,
I'm sorry you don't understand the basics of the LSM concept, but please look at evm_inode_init_security(), xattr_dupval(), and selinux_inode_init_security() for some background. There should not be any usage of lsm_get_xattr_slot() or BPF_LSM_INODE_INIT_XATTRS outside of security/; you argued a similar idea to justify your NACK of Hornet, I'm simply applying the same logic here. We also have the very recent security issue caused by the BPF subsystem which failed to acknowledge that the admin disabled the BPF LSM and then walked all over kernel memory when it shouldn't. Moving LSM internals outside of the LSM creates an environment where flaws like this can go undetected.
As I said previously, if you absolutely insist on the kfunc being in the VFS kfunc file, the LSM specific bits need to be abstracted out into an LSM function.
kfunc bpf_init_inode_xattr(...)
{
/* sanity check params */
if (!xattrs ...)
return -EINVAL;/* get value/len from bpf dynptr */
/* hook will check for LSM specific xattr count/limits, allocate,
copy value*/
rc = security_lsmxattr_add(xattrs, LSM_ID_BPF, value, value_len); if (rc)
return rc;
}
David, if you like I can provide you a patch that implements the security_lsmxattr_add() hook above if you aren't comfortable writing that, but if you want to give it a shot that's all the better :)
Signature · 1 line
paul-moore.com
Paul Moore
maintainerQuoted 96 lines · Paul Moore
Quoted 57 lines · Alexei Starovoitov
Quoted 47 lines · Paul Moore
Quoted 47 lines
fs/bpf_fs_kfuncs.c +47
-0+__bpf_kfunc int bpf_init_inode_xattr(struct lsm_xattrs *xattrs, +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; +const void *value; +u32 value_len; + +if (!xattrs || !xattrs->xattrs || !name__str) +return -EINVAL ; +if (bpf_xattrs_used (xattrs) >=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_NOFS ); +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 (xattrs); +if (!xattr) { +kfree (xattr_value); +return -ENOSPC ; + } + + xattr->value = xattr_value;Quoted 8 lines
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 453
Quoted 5 lines
fs/bpf_fs_kfuncs.c +5
-0+ xattr->name = (const char *)xattr_value + value_len; + xattr->value_len = value_len; + +return 0 ; +}This is not a generic VFS function, it is a LSM specific function, it belongs under
security/, please move the code as discussed previously.Quoted 5 lines
Paul,
Not quite. It's all about xattrs.
Having "structlsm_xattrs" in the arguments doesn't make it lsm related. You needs to study existing kfuncs and tracepoints.
A bunch of them have "*lsm*" in the arguments.Alexei,
I'm sorry you don't understand the basics of the LSM concept, but please look at
evm_inode_init_security(),xattr_dupval(), andselinux_inode_init_security()for some background. There should not be any usage oflsm_get_xattr_slot()orBPF_LSM_INODE_INIT_XATTRSoutside ofsecurity/; you argued a similar idea to justify your NACK of Hornet, I'm simply applying the same logic here. We also have the very recent security issue caused by the BPF subsystem which failed to acknowledge that the admin disabled the BPF LSM and then walked all over kernel memory when it shouldn't. Moving LSM internals outside of the LSM creates an environment where flaws like this can go undetected.As I said previously, if you absolutely insist on the kfunc being in the VFS kfunc file, the LSM specific bits need to be abstracted out into an LSM function.
kfunc bpf_init_inode_xattr(...) { /* sanity check params */ if (!xattrs ...) return -EINVAL;/* get value/len from bpf dynptr */ /* hook will check for LSM specific xattr count/limits, allocate, copy value*/ rc = security_lsmxattr_add(xattrs, LSM_ID_BPF, value, value_len); if (rc) return rc;
Quoted 5 lines
}
David, if you like I can provide you a patch that implements the
security_lsmxattr_add()hook above if you aren't comfortable writing that, but if you want to give it a shot that's all the better :)
One other thing - as this patchset is primarily LSM related, it needs to be merged via the LSM tree. If Alexei can't tolerate the LSM tree merging a minor BPF patch he can either choose to pull from an LSM tree topic branch or we can merge the LSM infrastructure bits now and he can merge the BPF changes when the LSM bits hit Linus' tree.
Signature · 1 line
paul-moore.com
Quoted 16 lines · On Tue, Jun 30, 2026 at 2:54 PM <[email protected]> wrote:
[ ... ]
fs/bpf_fs_kfuncs.c BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) · line 466
Quoted 5 lines
fs/bpf_fs_kfuncs.c +1
-0@@ -385,6 +463,7 @@ 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 )[Severity: High]
Does marking this kfunc as sleepable create a risk of deadlocks?Since the kfunc is only callable from the
inode_init_securityhook, marking the hook as sleepable allows BPF programs to trigger page faults (e.g., viabpf_copy_from_user). When a file is created (e.g.,ext4_create), the filesystem acquires a transaction handle before invoking the hook. If the BPF program triggers a page fault, it will attempt to takemmap_read_lock.
Quoted 6 lines
Concurrently, another thread doing a write page fault (or mmap/mprotect) might acquire
mmap_lockand attempt to start a transaction (e.g., inext4_page_mkwrite), which blocks on the transaction handle held by the first process. This creates an AB-BA deadlock between the filesystem transaction andmmap_lock.
This is real. We can eliminate the deadlock by removing KF_SLEEPABLE, which is fine here once we change the single allocation made by this function to GFP_NOWAIT.
Quoted 27 lines
Quoted 3 lines
fs/bpf_fs_kfuncs.c +0
-0BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)[ ... ]
kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_inode_create) · line 318
Quoted 9 lines
kernel/bpf/bpf_lsm.c +1
-0@@ -315,6 +315,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)[Severity: High]
Is it safe to addbpf_lsm_inode_init_securityto the sleepable hooks list?As mentioned above, marking this hook as sleepable permits BPF programs to take page faults from within a filesystem transaction, which violates filesystem locking rules and can freeze filesystem operations.
kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_inode_create) · line 321
Quoted 3 lines
kernel/bpf/bpf_lsm.c +0
-0BTF_ID(func, bpf_lsm_inode_mknod) BTF_ID(func, bpf_lsm_inode_need_killpriv)BTF_ID (func, bpf_lsm_inode_post_setxattr)--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
On Wed, Jul 1, 2026 at 8:55 AM Paul Moore <[email protected]> wrote:
[...]
Quoted 14 lines
kfunc bpf_init_inode_xattr(...) { /* sanity check params */ if (!xattrs ...) return -EINVAL;/* get value/len from bpf dynptr */ /* hook will check for LSM specific xattr count/limits, allocate, copy value*/ rc = security_lsmxattr_add(xattrs, LSM_ID_BPF, value, value_len); if (rc) return rc;
Quoted 6 lines
}
David, if you like I can provide you a patch that implements the
security_lsmxattr_add()hook above if you aren't comfortable writing that, but if you want to give it a shot that's all the better :)
Makes sense, I can do it while I'm fixing the remaining issue flagged by sashiko.
I'll route the LSM preparation patch containing struct lsm_xattrs and security_lsmxattr_add() through security and the kfunc and selftest through bpf. Does that work for you?
Thanks,
David
Paul Moore
maintainerQuoted 22 lines · David Windsor
On Wed, Jul 1, 2026 at 8:55 AM Paul Moore <[email protected]> wrote:
[...]Quoted 14 lines
kfunc bpf_init_inode_xattr(...) { /* sanity check params */ if (!xattrs ...) return -EINVAL;/* get value/len from bpf dynptr */ /* hook will check for LSM specific xattr count/limits, allocate, copy value*/ rc = security_lsmxattr_add(xattrs, LSM_ID_BPF, value, value_len); if (rc) return rc;Quoted 5 lines
}
David, if you like I can provide you a patch that implements the
security_lsmxattr_add()hook above if you aren't comfortable writing that, but if you want to give it a shot that's all the better :)
Quoted 6 lines
Makes sense, I can do it while I'm fixing the remaining issue flagged by sashiko.
I'll route the LSM preparation patch containing struct
lsm_xattrsandsecurity_lsmxattr_add()through security and the kfunc and selftest through bpf. Does that work for you?
Yep.
Signature · 1 line
paul-moore.com