bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
Subsystems (12)
v5 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 and claims a slot in it via the new security_lsmxattr_add() LSM helper.
v5:
- make the kfunc non-sleepable to avoid an ext4 deadlock (sashiko)
- move xattr creation logic to security_lsmxattr_add() (Paul)
- use distinct xattr names in init_inode_xattr_slot_limit
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: rework inode_init_security xattr handling
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 | 36 +++++ 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 | 15 +++ security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 +- security/security.c | 108 +++++++++++++-- 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 | 124 ++++++++++++++++++ .../bpf/progs/test_init_inode_xattr.c | 31 +++++ 14 files changed, 344 insertions(+), 47 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_init_inode_xattr.c
base-commit: 602701718649936eb287bf6c7ecf870ec54c6f71
Signature · 1 line
2.53.0
PATCH v5 1/3 security: rework inode_init_security xattr handling
In preparation for bpf_init_inode_xattr(), a kfunc that lets bpf LSM programs atomically label new inodes, rework how inode_init_security xattrs are managed.
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.
Also, add security_lsmxattr_add(), which claims a slot in the inode_init_security xattr array on behalf of the calling LSM and fills it with a copy of the given name and value.
--- 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 | 15 +++++ security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 108 ++++++++++++++++++++++++++---- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 10 files changed, 148 insertions(+), 47 deletions(-)
include/linux/bpf_lsm.h +3 -0
@@ -21,6 +21,9 @@ extern bool bpf_lsm_initialized __ro_after_init;#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 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 +15 -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 */
@@ -401,6 +406,9 @@ void security_inode_free(struct inode *inode);int security_inode_init_security (struct inode *inode,struct inode *dir,const struct qstr *qstr,initxattrs initxattrs,void *fs_data); +int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len);int security_inode_init_security_anon (struct inode *inode,const struct qstr *name,const struct inode *context_inode);
@@ -895,6 +903,13 @@ static inline int security_inode_init_security(struct inode *inode,return 0 ; } +static inline int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len) +{ +return -EOPNOTSUPP ; +} +static inline int security_inode_init_security_anon (struct inode *inode,const struct qstr *name,const struct inode *context_inode)
security/bpf/hooks.c +1 -0
@@ -33,6 +33,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 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 +96 -12
143 lines not on this page · raw message
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
[email protected]
AI reviewRe: [PATCH v5 bpf-next 1/3] security: rework inode_init_security xattr handling
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Heap buffer overflow in `security_lsmxattr_add()` due to xattr quota bypass by BPF programs.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Heap buffer overflow in security_lsmxattr_add() due to xattr quota bypass by BPF programs.
- [Medium] security_lsmxattr_add() incorrectly returns -EINVAL instead of -EOPNOTSUPP when the xattr array is not allocated. --
--- Patch [1]: [PATCH v5 bpf-next 1/3] security: rework inode_init_security xattr handling --- commit 871b3020e8c69372708886992716ee08d2e36bc2
Author: David Windsor <[email protected]>
security: rework inode_init_security xattr handling
In preparation for bpf_init_inode_xattr(), a kfunc that lets bpf LSM programs atomically label new inodes, rework how inode_init_security xattrs are managed.
Quoted 3 lines
security/security.c +0
-0
[ ... ]
Quoted 15 lines
security/security.c +15
-0+/** +* security_lsmxattr_add() - Add an xattr during inode_init_security +* @xattrs: xattr state shared by inode_init_security hooks +* @lsm_id: LSM_ID_* value identifying the calling LSM +* @name: xattr name suffix +* @value: xattr value +* @value_len: length of @value +* +* Claim an xattr slot in @xattrs on behalf of the LSM identified by +* @lsm_id and fill it with a copy of @name and @value. Callers can invoke +* this function from non-sleepable context. +* +* Return: Returns 0 on success, -ENOSPC if the calling LSM's slot budget +* is exhausted, negative values on other errors. +*/
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1421
Quoted 10 lines
security/security.c +10
-0+int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len) +{ +struct xattr *xattr; +void *xattr_value; +size_t name_len; + +if (!xattrs || !xattrs->xattrs || !name || !value) +return -EINVAL ;
[Severity: Medium]
Should this return -EOPNOTSUPP instead of -EINVAL when the xattrs array is not allocated?
During file creation on filesystems without xattr support, security_inode_init_security() is called with initxattrs == NULL, deliberately leaving xattrs.xattrs unallocated.
If a BPF program then calls security_lsmxattr_add(), it will receive -EINVAL. If the BPF program propagates this error, it will abort the inode creation entirely because security_inode_init_security() only treats -EOPNOTSUPP as a graceful fallback.
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1434
Quoted 13 lines
security/security.c +13
-0+ + name_len =strlen (name); +if (name_len ==0 || name_len >XATTR_NAME_MAX ) +return -EINVAL ; +if (value_len ==0 || value_len >XATTR_SIZE_MAX ) +return -EINVAL ; + +switch (lsm_id) { +#ifdef CONFIG_BPF_LSM +case LSM_ID_BPF : +if (lsm_xattrs_used (xattrs,XATTR_BPF_LSM_SUFFIX ) >= +BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ;
[Severity: High]
Can this limit check be bypassed, leading to a heap buffer overflow?
If a BPF program repeatedly calls security_lsmxattr_add() with a name that does not begin with XATTR_BPF_LSM_SUFFIX, lsm_xattrs_used() will not count them.
This allows bypassing the BPF_LSM_INODE_INIT_XATTRS limit. Since lsm_get_xattr_slot() does not check against the actual allocated heap size, it will increment ctx->xattr_count and return out-of-bounds pointers:
include/linux/lsm_hooks.h:lsm_get_xattr_slot() {
...
return &ctx->xattrs[ctx->xattr_count++];
}Could this cause out-of-bounds writes when assigning xattr->value and xattr->name further down in this function?
Quoted 10 lines
security/security.c +10
-0+break ; +#endif /* CONFIG_BPF_LSM */ +default : +return -EINVAL ; + } + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_NOWAIT ); +if (!xattr_value) +return -ENOMEM ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v5 bpf-next 1/3] security: rework inode_init_security xattr handling
Quoted 78 lines · On Tue, Jul 7, 2026 at 8:20 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Heap buffer overflow insecurity_lsmxattr_add()due to xattr quota bypass by BPF programs.
- [Medium]security_lsmxattr_add()incorrectly returns-EINVALinstead of-EOPNOTSUPPwhen the xattr array is not allocated. ----- Patch [1]: [PATCH v5 bpf-next 1/3] security: rework
inode_init_securityxattr handling --- commit871b3020e8c69372708886992716ee08d2e36bc2
Author: David Windsor <[email protected]>security: rework
inode_init_securityxattr handlingIn preparation for
bpf_init_inode_xattr(), a kfunc that lets bpf LSM programs atomically label new inodes, rework howinode_init_securityxattrs are managed.Quoted 3 lines
security/security.c +0
-0[ ... ]
Quoted 15 lines
security/security.c +15
-0+/** +* security_lsmxattr_add() - Add an xattr during inode_init_security +* @xattrs: xattr state shared by inode_init_security hooks +* @lsm_id: LSM_ID_* value identifying the calling LSM +* @name: xattr name suffix +* @value: xattr value +* @value_len: length of @value +* +* Claim an xattr slot in @xattrs on behalf of the LSM identified by +* @lsm_id and fill it with a copy of @name and @value. Callers can invoke +* this function from non-sleepable context. +* +* Return: Returns 0 on success, -ENOSPC if the calling LSM's slot budget +* is exhausted, negative values on other errors. +*/ security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1421
Quoted 10 lines
security/security.c +10
-0+int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len) +{ +struct xattr *xattr; +void *xattr_value; +size_t name_len; + +if (!xattrs || !xattrs->xattrs || !name || !value) +return -EINVAL ;[Severity: Medium]
Should this return-EOPNOTSUPPinstead of-EINVALwhen the xattrs array is not allocated?During file creation on filesystems without xattr support,
security_inode_init_security()is called with initxattrs == NULL, deliberately leaving xattrs.xattrs unallocated.If a BPF program then calls
security_lsmxattr_add(), it will receive-EINVAL. If the BPF program propagates this error, it will abort the inode creation entirely becausesecurity_inode_init_security()only treats-EOPNOTSUPPas a graceful fallback.security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1434
Quoted 13 lines
security/security.c +13
-0+ + name_len =strlen (name); +if (name_len ==0 || name_len >XATTR_NAME_MAX ) +return -EINVAL ; +if (value_len ==0 || value_len >XATTR_SIZE_MAX ) +return -EINVAL ; + +switch (lsm_id) { +#ifdef CONFIG_BPF_LSM +case LSM_ID_BPF : +if (lsm_xattrs_used (xattrs,XATTR_BPF_LSM_SUFFIX ) >= +BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ;[Severity: High]
Can this limit check be bypassed, leading to a heap buffer overflow?
Quoted 4 lines
If a BPF program repeatedly calls
security_lsmxattr_add()with a name that does not begin withXATTR_BPF_LSM_SUFFIX,lsm_xattrs_used()will not count them.
Sorry, all paths to calling this function are guaranteed to have XATTR_BPF_LSM_SUFFIX.
Quoted 26 lines
This allows bypassing the
BPF_LSM_INODE_INIT_XATTRSlimit. Sincelsm_get_xattr_slot()does not check against the actual allocated heap size, it will incrementctx->xattr_countand return out-of-bounds pointers:include/linux/lsm_hooks.h:lsm_get_xattr_slot() { ... return &ctx->xattrs[ctx->xattr_count++]; }Could this cause out-of-bounds writes when assigning
xattr->valueandxattr->namefurther down in this function?security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1444
Quoted 10 lines
security/security.c +10
-0+break ; +#endif /* CONFIG_BPF_LSM */ +default : +return -EINVAL ; + } + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_NOWAIT ); +if (!xattr_value) +return -ENOMEM ;--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Paul Moore
maintainerRe: [PATCH v5 1/3] security: rework inode_init_security xattr handling
In reply to David WindsorQuoted 28 lines · On Jul 7, 2026 David Windsor <[email protected]> wrote:
In preparation for
bpf_init_inode_xattr(), a kfunc that lets bpf LSM programs atomically label new inodes, rework howinode_init_securityxattrs are managed.
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.Also, add
security_lsmxattr_add(), which claims a slot in theinode_init_securityxattr array on behalf of the calling LSM and fills it with a copy of the given name and value.Suggested-by Paul Moore <[email protected]>Signed-off-by David Windsor <[email protected]>--- 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 | 15 +++++ security/bpf/hooks.c | 1 + security/integrity/evm/evm_main.c | 8 ++- security/security.c | 108 ++++++++++++++++++++++++++---- security/selinux/hooks.c | 4 +- security/smack/smack_lsm.c | 27 ++++---- 10 files changed, 148 insertions(+), 47 deletions(-)
...
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 lsm_xattrs { +struct xattr *xattrs; +unsigned int xattr_count; +};
Please separate out the 'struct lsm_xattrs' related changes into a separate patch from the security_lsmxattr_add() changes. I know they are related, but they are different things.
Quoted 71 lines
security/security.c +19
-12@@ -12,6 +12,7 @@#define pr_fmt (fmt) "LSM: " fmt#include <linux/bpf.h> +#include <linux/bpf_lsm.h> #include <linux/capability.h> #include <linux/dcache.h> #include <linux/export.h> @@ -1333,8 +1334,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 +1345,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,18 +1365,101 @@ 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); +#ifdef CONFIG_BPF_LSM +static unsigned int lsm_xattrs_used (const struct lsm_xattrs *xattrs, +const char *prefix) +{ +size_t prefix_len =strlen (prefix); +unsigned int i, n =0 ;
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1395
Quoted 10 lines
security/security.c +10
-0+ +for (i =0 ; i < xattrs->xattr_count; i++) { +const char *name = xattrs->xattrs[i].name; + +if (name && !strncmp (name, prefix, prefix_len)) + n++; + } + return n; +} +#endif /* CONFIG_BPF_LSM */
More on this below, but this function isn't strictly BPF LSM related so let's drop the CONFIG_BPF_LSM macro bracketing.
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1401
Quoted 5 lines
security/security.c +5
-0+/** +* security_lsmxattr_add() - Add an xattr during inode_init_security +* @xattrs: xattr state shared by inode_init_security hooks +* @lsm_id: LSM_ID_* value identifying the calling LSM +* @name: xattr name suffix
For BPF you currently pass "bpf.foo" for the name and other LSMs would pass their own xattr name, likely without a LSM specific sub-namespace (for example, SELinux would be just "selinux"). In both cases the 'name' parameter always starts with a well known suffix as defined by the 'lsm_id" parameter.
Since we are already passing the lsm_id parameter, let's do away with the standard LSM suffixes, e.g. XATTR_BPF_LSM_SUFFIX, and just pass in any additional name components. For example, instead of passing "bpf.foo" in the BPF LSM case, you would just pass "foo"; LSMs without their own sub-namespace, e.g. SELinux, would pass NULL for the name parameter as XATTR_SELINUX_SUFFIX is all that is needed. While doing this I would also suggest changing the name of the 'name' parameter to 'name_extra', 'namespace_extra', or something similar to indicate that it isn't the full name, but rather an additional suffix beyond the standard suffix associated with the given LSM.
Quoted 10 lines
security/security.c +10
-0+* @value: xattr value +* @value_len: length of @value +* +* Claim an xattr slot in @xattrs on behalf of the LSM identified by +* @lsm_id and fill it with a copy of @name and @value. Callers can invoke +* this function from non-sleepable context. +* +* Return: Returns 0 on success, -ENOSPC if the calling LSM's slot budget +* is exhausted, negative values on other errors. +*/
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1421
Quoted 10 lines
security/security.c +10
-0+int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len) +{ +struct xattr *xattr; +void *xattr_value; +size_t name_len; + +if (!xattrs || !xattrs->xattrs || !name || !value) +return -EINVAL ;
Sashiko raised a good point about xattrs->xattrs being NULL not necessarily being a good reason for -EINVAL. If xattrs is NULL, yes, something has gone wrong and -EINVAL seems reasonable, but the xattr->xattrs NULL case does seem like it should simply return early with a value of 0 (see SELinux's handling of this case as an example).
Quoted 4 lines
security/security.c +4
-0+ name_len =strlen (name); +if (name_len ==0 || name_len >XATTR_NAME_MAX ) +return -EINVAL ; +if (value_len ==0 || value_len >XATTR_SIZE_MAX )
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1436
Quoted 10 lines
security/security.c +10
-0+return -EINVAL ; + +switch (lsm_id) { +#ifdef CONFIG_BPF_LSM +case LSM_ID_BPF : +if (lsm_xattrs_used (xattrs,XATTR_BPF_LSM_SUFFIX ) >= +BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ; +break ; +#endif /* CONFIG_BPF_LSM */
I like to avoid macro conditional code inside functions whenever possible, and I think this is a case where we could avoid the conditional block with a little work.
The LSM_ID_BPF macro is already defined as part of the UAPI so that will always be available. While BPF_LSM_INODE_INIT_XATTRS is dependent on CONFIG_BPF_LSM in this revision, that should be easy enough to move outside the CONFIG_BPF_LSM conditional in bpf_lsm.h. Eventually we should probably expand the lsm_id struct to carry this info, likely just a permanent/local copy of the LSM's lsm_blob_sizes passed during registration, but I can take care of that later; just make the BPF_LSM_INODE_INIT_XATTRS macro always accessible now.
We should also probably record the xattr suffix/length when the LSM is registered, but that can also be done later with the other lsm_id additions.
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1448
Quoted 12 lines
security/security.c +12
-0+default : +return -EINVAL ; + } + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_NOWAIT ); +if (!xattr_value) +return -ENOMEM ; + +memcpy (xattr_value, value, value_len); +memcpy (xattr_value + value_len, name, name_len); + ((char *)xattr_value)[value_len + name_len] ='\0' ;
You'll need to add an additional memcpy() here, likely in a switch statement to handle the different LSMs, to copy over the LSM specific prefix. It's a little ugly, but when we have things captured in the lsm_id struct it will get a lot cleaner.
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1461
Quoted 12 lines
security/security.c +12
-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 ; +}
--
paul-moore.com
Re: [PATCH v5 1/3] security: rework inode_init_security xattr handling
Quoted 10 lines · Paul Moore
include/linux/security.h struct watch; · line 74
Quoted 5 lines
include/linux/security.h +4
-0+struct lsm_xattrs { +struct xattr *xattrs; +unsigned int xattr_count; +};Please separate out the 'struct
lsm_xattrs' related changes into a separate patch from thesecurity_lsmxattr_add()changes. I know they are related, but they are different things.
Will do.
Quoted 11 lines
Quoted 11 lines
security/security.c +11
-0+#ifdef CONFIG_BPF_LSM +static unsigned int lsm_xattrs_used (const struct lsm_xattrs *xattrs, +const char *prefix) +{ +size_t prefix_len =strlen (prefix); +unsigned int i, n =0 ; + +for (i =0 ; i < xattrs->xattr_count; i++) { +const char *name = xattrs->xattrs[i].name; + +if (name && !strncmp (name, prefix, prefix_len))
Quoted 9 lines
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1395
Quoted 5 lines
security/security.c +5
-0+ n++; + } +return n; +} +#endif /* CONFIG_BPF_LSM */ More on this below, but this function isn't strictly BPF LSM related so let's drop the
CONFIG_BPF_LSMmacro bracketing.
Ack
Quoted 8 lines
Quoted 8 lines
security/security.c +8
-0+int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len) +{ +struct xattr *xattr; +void *xattr_value; +size_t name_len; +
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!xattrs || !xattrs->xattrs || !name || !value) + return -EINVAL;Sashiko raised a good point about
xattrs->xattrsbeing NULL not necessarily being a good reason for-EINVAL. If xattrs is NULL, yes, something has gone wrong and-EINVALseems reasonable, but thexattr->xattrsNULL case does seem like it should simply return early with a value of 0 (see SELinux's handling of this case as an example).
The problem is, SELinux has already set the sid by before calling lsm_get_xattr_slot, so we do indeed get an in-core label for the inode before returning early here. Returning early here from BPF would not mean the same thing: there is no label anywhere, in memory or on disk.
Quoted 28 lines
Quoted 4 lines
security/security.c +4
-0+ name_len =strlen (name); +if (name_len ==0 || name_len >XATTR_NAME_MAX ) +return -EINVAL ; +if (value_len ==0 || value_len >XATTR_SIZE_MAX )security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1436
Quoted 10 lines
security/security.c +10
-0+return -EINVAL ; + +switch (lsm_id) { +#ifdef CONFIG_BPF_LSM +case LSM_ID_BPF : +if (lsm_xattrs_used (xattrs,XATTR_BPF_LSM_SUFFIX ) >= +BPF_LSM_INODE_INIT_XATTRS ) +return -ENOSPC ; +break ; +#endif /* CONFIG_BPF_LSM */ I like to avoid macro conditional code inside functions whenever possible, and I think this is a case where we could avoid the conditional block with a little work.
The
LSM_ID_BPFmacro is already defined as part of the UAPI so that will always be available. WhileBPF_LSM_INODE_INIT_XATTRSis dependent onCONFIG_BPF_LSMin this revision, that should be easy enough to move outside theCONFIG_BPF_LSMconditional inbpf_lsm.h. Eventually we should probably expand thelsm_idstruct to carry this info, likely just a permanent/local copy of the LSM'slsm_blob_sizespassed during registration, but I can take care of that later; just make theBPF_LSM_INODE_INIT_XATTRSmacro always accessible now.
Quoted 4 lines
We should also probably record the xattr suffix/length when the LSM is registered, but that can also be done later with the other
lsm_idadditions.
Sounds good, will refactor to remove the conditional.
Quoted 9 lines
Quoted 9 lines
security/security.c +9
-0+default : +return -EINVAL ; + } + +/* Combine xattr value + name into one allocation. */ + xattr_value =kmalloc (value_len + name_len +1 ,GFP_NOWAIT ); +if (!xattr_value) +return -ENOMEM ; +
Quoted 9 lines
security/security.c int security_inode_init_security(struct inode *inode, struct inode *dir, · line 1448
Quoted 3 lines
security/security.c +3
-0+memcpy (xattr_value, value, value_len); +memcpy (xattr_value + value_len, name, name_len); + ((char *)xattr_value)[value_len + name_len] ='\0' ;You'll need to add an additional
memcpy()here, likely in a switch statement to handle the different LSMs, to copy over the LSM specific prefix. It's a little ugly, but when we have things captured in thelsm_idstruct it will get a lot cleaner.
Yup
Quoted 3 lines
--
paul-moore.com
Thanks,
David
Paul Moore
maintainerRe: [PATCH v5 1/3] security: rework inode_init_security xattr handling
Quoted 2 lines · David Windsor
Quoted 1 line · Paul Moore
...
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
security/security.c +8
-0+int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len) +{ +struct xattr *xattr; +void *xattr_value; +size_t name_len; +Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!xattrs || !xattrs->xattrs || !name || !value) + return -EINVAL;Sashiko raised a good point about
xattrs->xattrsbeing NULL not necessarily being a good reason for-EINVAL. If xattrs is NULL, yes, something has gone wrong and-EINVALseems reasonable, but thexattr->xattrsNULL case does seem like it should simply return early with a value of 0 (see SELinux's handling of this case as an example).
Quoted 4 lines
The problem is, SELinux has already set the sid by before calling
lsm_get_xattr_slot, so we do indeed get an in-core label for the inode before returning early here. Returning early here from BPF would not mean the same thing: there is no label anywhere, in memory or on disk.
It's important to remember that you are not always guaranteed to have an lsm_xattrs buffer, for some filesystems that is going to be normal and expected. In these instances the 'xattrs->xattrs' value is going to be NULL and each LSM that provides a security_inode_init_security() LSM callback needs to handle that condition gracefully.
As you already know, the SELinux approach to this is to set the in-memory inode label, but refrain from setting a presistent label (these filesystems are typically either transient or don't have a mechanism to allow for persistent label storage). Each BPF program will need to decide how they want to handle this in their security_inode_init_security() callback, but in the security_lsmxattr_add() code where the only purpose is to populate the lsm_xattrs strcut with the persistent BPF label/xattr, there is nothing that can be done, it should simply return early without error.
Signature · 1 line
paul-moore.com
Re: [PATCH v5 1/3] security: rework inode_init_security xattr handling
Quoted 35 lines · Paul Moore
Quoted 2 lines · David Windsor
Quoted 1 line · Paul Moore
...
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
security/security.c +8
-0+int security_lsmxattr_add (struct lsm_xattrs *xattrs,u64 lsm_id, +const char *name,const void *value, +size_t value_len) +{ +struct xattr *xattr; +void *xattr_value; +size_t name_len; +Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!xattrs || !xattrs->xattrs || !name || !value) + return -EINVAL;Sashiko raised a good point about
xattrs->xattrsbeing NULL not necessarily being a good reason for-EINVAL. If xattrs is NULL, yes, something has gone wrong and-EINVALseems reasonable, but thexattr->xattrsNULL case does seem like it should simply return early with a value of 0 (see SELinux's handling of this case as an example).Quoted 4 lines
The problem is, SELinux has already set the sid by before calling
lsm_get_xattr_slot, so we do indeed get an in-core label for the inode before returning early here. Returning early here from BPF would not mean the same thing: there is no label anywhere, in memory or on disk.It's important to remember that you are not always guaranteed to have an
lsm_xattrsbuffer, for some filesystems that is going to be normal and expected. In these instances the 'xattrs->xattrs' value is going to be NULL and each LSM that provides asecurity_inode_init_security()LSM callback needs to handle that condition gracefully.
Quoted 10 lines
As you already know, the SELinux approach to this is to set the in-memory inode label, but refrain from setting a presistent label (these filesystems are typically either transient or don't have a mechanism to allow for persistent label storage). Each BPF program will need to decide how they want to handle this in their
security_inode_init_security()callback, but in thesecurity_lsmxattr_add()code where the only purpose is to populate thelsm_xattrsstrcut with the persistent BPF label/xattr, there is nothing that can be done, it should simply return early without error.
Makes sense. If BPF LSMs want to store an in-core label, they could just use bpf_inode_storage_get.
Quoted 2 lines
--
paul-moore.com
PATCH v5 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 security_lsmxattr_add(). The hook now passes its xattr state as a single struct lsm_xattrs object, which the kfunc takes directly.
This kfunc is only callable from inode_init_security; the verifier rejects attempts to call it elsewhere.
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 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
fs/bpf_fs_kfuncs.c +36 -0
@@ -11,7 +11,9 @@#include <linux/file.h> #include <linux/kernfs.h> #include <linux/mm.h> +#include <linux/security.h> #include <linux/xattr.h> +#include <uapi/linux/lsm.h> __bpf_kfunc_start_defs ();
@@ -374,6 +376,39 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry)return d_real_inode (dentry); } +/** +* 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; +const void *value; +u32 value_len; + +if (!name__str) +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); + value =__bpf_dynptr_data (value_ptr, value_len); +if (!value) +return -EINVAL ; + +return security_lsmxattr_add (xattrs,LSM_ID_BPF , name__str, value, + value_len); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)
@@ -385,6 +420,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)BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usingsecurity_lsmxattr_add(). The hook now passes its xattr state as a single structlsm_xattrsobject, which the kfunc takes directly.This kfunc is only callable from
inode_init_security; the verifier rejects attempts to call it elsewhere.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.
Quoted 4 lines
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=2
Paul Moore
maintainerQuoted 62 lines · On Jul 7, 2026 David Windsor <[email protected]> wrote:
Add
bpf_init_inode_xattr()kfunc for BPF LSM programs to atomically set xattrs via theinode_init_securityhook usingsecurity_lsmxattr_add(). The hook now passes its xattr state as a single structlsm_xattrsobject, which the kfunc takes directly.This kfunc is only callable from
inode_init_security; the verifier rejects attempts to call it elsewhere.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.Suggested-by Song Liu <[email protected]>Signed-off-by David Windsor <[email protected]>--- fs/bpf_fs_kfuncs.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)fs/bpf_fs_kfuncs.c +24
-0@@ -11,7 +11,9 @@#include <linux/file.h> #include <linux/kernfs.h> #include <linux/mm.h> +#include <linux/security.h> #include <linux/xattr.h> +#include <uapi/linux/lsm.h> __bpf_kfunc_start_defs ();@@ -374,6 +376,39 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry)return d_real_inode (dentry); } +/** +* 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; +const void *value; +u32 value_len; + +if (!name__str) +return -EINVAL ; +if (strncmp (name__str,XATTR_BPF_LSM_SUFFIX , +sizeof (XATTR_BPF_LSM_SUFFIX ) -1 ))
fs/bpf_fs_kfuncs.c __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) · line 410
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); + value =__bpf_dynptr_data (value_ptr, value_len); +if (!value) +return -EINVAL ; + +return security_lsmxattr_add (xattrs,LSM_ID_BPF , name__str, value, + value_len); +}
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.
If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Quoted 13 lines
fs/bpf_fs_kfuncs.c +1
-0__bpf_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)@@ -385,6 +420,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)BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)Signature · 1 line
2.53.0
--
paul-moore.com
Quoted 11 lines · Paul Moore
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under
security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Where this code lands doesn't matter to me, so I'll stay out of the decision of where it lives.
If this kfunc goes to security/, would we also want to move eg bpf_set_dentry_xattr similarly?
I'll roll v6 of this series meanwhile and we'll see what the BPF maintainers say.
Kumar Kartikeya Dwivedi
maintainerQuoted 13 lines · David Windsor
Quoted 11 lines · Paul Moore
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under
security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Quoted 8 lines
Where this code lands doesn't matter to me, so I'll stay out of the decision of where it lives.
If this kfunc goes to
security/, would we also want to move egbpf_set_dentry_xattrsimilarly?I'll roll v6 of this series meanwhile and we'll see what the BPF maintainers say.
I don't think there was any preference expressed from the BPF side. Logically, one cannot be faulted for adding a kfunc to set the xattr for inode in the same file where similar kfuncs to set xattr on other FS objects / entities exist.
The question about bpf_set_dentry_xattr is thus valid.
Anyway, I will leave it to Paul and Christian to decide between themselves where it makes most sense for this one to live.
Kumar Kartikeya Dwivedi
maintainerQuoted 23 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · David Windsor
Quoted 11 lines · Paul Moore
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under
security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Quoted 8 lines
Where this code lands doesn't matter to me, so I'll stay out of the decision of where it lives.
If this kfunc goes to
security/, would we also want to move egbpf_set_dentry_xattrsimilarly?I'll roll v6 of this series meanwhile and we'll see what the BPF maintainers say.
Quoted 8 lines
I don't think there was any preference expressed from the BPF side. Logically, one cannot be faulted for adding a kfunc to set the xattr for inode in the same file where similar kfuncs to set xattr on other FS objects / entities exist.
The question about
bpf_set_dentry_xattris thus valid.Anyway, I will leave it to Paul and Christian to decide between themselves where it makes most sense for this one to live.
Also, perhaps it makes sense to use set/get naming for this one too, to keep it consistent with other ones?
Quoted 29 lines · Kumar Kartikeya Dwivedi
Quoted 27 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · David Windsor
Quoted 11 lines · Paul Moore
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under
security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Quoted 8 lines
Where this code lands doesn't matter to me, so I'll stay out of the decision of where it lives.
If this kfunc goes to
security/, would we also want to move egbpf_set_dentry_xattrsimilarly?I'll roll v6 of this series meanwhile and we'll see what the BPF maintainers say.
I don't think there was any preference expressed from the BPF side. Logically, one cannot be faulted for adding a kfunc to set the xattr for inode in the same file where similar kfuncs to set xattr on other FS objects / entities exist.
Quoted 7 lines
Quoted 4 lines
The question about
bpf_set_dentry_xattris thus valid.Anyway, I will leave it to Paul and Christian to decide between themselves where it makes most sense for this one to live.
Also, perhaps it makes sense to use set/get naming for this one too, to keep it consistent with other ones?
I considered that, but we're only allowing one to initialize the xattr on the inode here. Once it's been initialized, one uses bpf_dentry_set_xattr.
Paul Moore
maintainerQuoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 9 lines · David Windsor
Quoted 8 lines · Paul Moore
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under
security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Where this code lands doesn't matter to me, so I'll stay out of the decision of where it lives.
Yes, you're caught up in some territorial wrangling that goes a fair bit beyond what you're looking to accomplish. My apologies for my half of that, it is an unfortunate part of upstream kernel development.
Quoted 5 lines
Quoted 5 lines
If this kfunc goes to
security/, would we also want to move egbpf_set_dentry_xattrsimilarly?I'll roll v6 of this series meanwhile and we'll see what the BPF maintainers say.
Thanks. Fortunately, it should be easy enough to move the kfunc around, that's likely one of the smaller changes needed in v6.
Quoted 1 line
I don't think there was any preference expressed from the BPF side.
Look at the second patch in the v4 patchset, you'll see a preference.
Quoted 3 lines
Logically,
one cannot be faulted for adding a kfunc to set the xattr for inode in the same file where similar kfuncs to set xattr on other FS objects / entities exist.
First off, I can't say that the existing BPF kfuncs intended for use within a BPF LSM program are in a good location. Unlike every other LSM which locates its code under security, the BPF LSM devs have chosen to locate their associated code all over the place. Some of that happened before I was responsible for the LSM framework, and some of that which has happened afterwards has likely intentionally avoided traditional LSM review. Unfortunately, this has muddied things with respect to what is possible generally in BPF versus what is possible only when the BPF LSM is enabled; we just recently saw where this was arguably the root cause of an exploitable vulnerability.
Quoted 1 line
The question about
bpf_set_dentry_xattris thus valid.
It's a very valid question. It is my opinion that kfuncs which are only intended to be called from within BPF LSMs should be located under security/, just like every other LSM. If there is a kfunc that is generally useful from other contexts, that is a different story, but in the bpf_set_dentry_xattr() case, it is explicitly only useful for setting 'security.bpf.*' xattrs.
Signature · 1 line
paul-moore.com
Kumar Kartikeya Dwivedi
maintainerQuoted 51 lines · Paul Moore
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 9 lines · David Windsor
Quoted 8 lines · Paul Moore
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under
security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Where this code lands doesn't matter to me, so I'll stay out of the decision of where it lives.
Yes, you're caught up in some territorial wrangling that goes a fair bit beyond what you're looking to accomplish. My apologies for my half of that, it is an unfortunate part of upstream kernel development.
Quoted 5 lines
Quoted 5 lines
If this kfunc goes to
security/, would we also want to move egbpf_set_dentry_xattrsimilarly?I'll roll v6 of this series meanwhile and we'll see what the BPF maintainers say.
Thanks. Fortunately, it should be easy enough to move the kfunc around, that's likely one of the smaller changes needed in v6.
Quoted 1 line
I don't think there was any preference expressed from the BPF side.
Look at the second patch in the v4 patchset, you'll see a preference.
Quoted 3 lines
Logically,
one cannot be faulted for adding a kfunc to set the xattr for inode in the same file where similar kfuncs to set xattr on other FS objects / entities exist.First off, I can't say that the existing BPF kfuncs intended for use within a BPF LSM program are in a good location. Unlike every other LSM which locates its code under security, the BPF LSM devs have chosen to locate their associated code all over the place. Some of that happened before I was responsible for the LSM framework, and some of that which has happened afterwards has likely intentionally avoided traditional LSM review. Unfortunately, this has muddied things with respect to what is possible generally in BPF versus what is possible only when the BPF LSM is enabled; we just recently saw where this was arguably the root cause of an exploitable vulnerability.
Quoted 8 lines
Quoted 1 line
The question about
bpf_set_dentry_xattris thus valid.It's a very valid question. It is my opinion that kfuncs which are only intended to be called from within BPF LSMs should be located under
security/, just like every other LSM. If there is a kfunc that is generally useful from other contexts, that is a different story, but in thebpf_set_dentry_xattr()case, it is explicitly only useful for setting 'security.bpf.*' xattrs.
I don't think we should do that in v6 though. What you're describing affects all other xattr kfuncs. That's a broader discussion we should have separately when folks are back from vacation, and more importantly involve FS folks where the code currently lives. As you say yourself, the lines are blurry, xattrs are as much a filesystem concept.
So I wouldn't burden David with it right now. David, just respin addressing all other comments and the CI issue.
Paul Moore
maintainerQuoted 54 lines · Kumar Kartikeya Dwivedi
Quoted 53 lines · Paul Moore
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 9 lines · David Windsor
Quoted 8 lines · Paul Moore
I'm sorry David, now that I'm seeing this function again, especially with the LSM specific bits extracted into a LSM function, this absolutely belongs somewhere under
security/. It's only callable from within a BPF LSM callback and all it does outside of some BPF pointer boilerplate is call right back into a LSM helper function.Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
If the BPF maintainers aren't willing to accept that, then we will all need to find another way.
Where this code lands doesn't matter to me, so I'll stay out of the decision of where it lives.
Yes, you're caught up in some territorial wrangling that goes a fair bit beyond what you're looking to accomplish. My apologies for my half of that, it is an unfortunate part of upstream kernel development.
Quoted 5 lines
Quoted 5 lines
If this kfunc goes to
security/, would we also want to move egbpf_set_dentry_xattrsimilarly?I'll roll v6 of this series meanwhile and we'll see what the BPF maintainers say.
Thanks. Fortunately, it should be easy enough to move the kfunc around, that's likely one of the smaller changes needed in v6.
Quoted 1 line
I don't think there was any preference expressed from the BPF side.
Look at the second patch in the v4 patchset, you'll see a preference.
Quoted 3 lines
Logically,
one cannot be faulted for adding a kfunc to set the xattr for inode in the same file where similar kfuncs to set xattr on other FS objects / entities exist.First off, I can't say that the existing BPF kfuncs intended for use within a BPF LSM program are in a good location. Unlike every other LSM which locates its code under security, the BPF LSM devs have chosen to locate their associated code all over the place. Some of that happened before I was responsible for the LSM framework, and some of that which has happened afterwards has likely intentionally avoided traditional LSM review. Unfortunately, this has muddied things with respect to what is possible generally in BPF versus what is possible only when the BPF LSM is enabled; we just recently saw where this was arguably the root cause of an exploitable vulnerability.
Quoted 1 line
The question about
bpf_set_dentry_xattris thus valid.
Quoted 8 lines
Quoted 6 lines
It's a very valid question. It is my opinion that kfuncs which are only intended to be called from within BPF LSMs should be located under
security/, just like every other LSM. If there is a kfunc that is generally useful from other contexts, that is a different story, but in thebpf_set_dentry_xattr()case, it is explicitly only useful for setting 'security.bpf.*' xattrs.I don't think we should do that in v6 though.
Yes, that would be a separate effort and it's one of the reasons I didn't bring it up in my review comments, I was simply responding to those who raised it as a question.
Signature · 1 line
paul-moore.com
PATCH v5 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 | 124 ++++++++++++++++++ .../bpf/progs/test_init_inode_xattr.c | 31 +++++ 3 files changed, 160 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 +124 -0
140 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 ; + +const char xattr_name[16 ] ="bpf.test_label" ; +char xattr_value[] ="unconfined_u:object_r:user_home_t:s0" ; + +SEC("lsm/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 v5 bpf-next 3/3] selftests/bpf: add tests for bpf_init_inode_xattr kfunc
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missing file cleanup before file creation causes flaky tests. If the test files already exist…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing file cleanup before file creation causes flaky tests. If the test files already exist, open(O_CREAT) will not trigger the LSM hook, leading to test failure. --
--- Patch [3]: [PATCH v5 bpf-next 3/3] selftests/bpf: add tests for bpf_init_inode_xattr kfunc --- commit fb13c41f9d223f18ed7c34e0e581869c6933b5e5
Author: David Windsor <[email protected]>
selftests/bpf: add tests for bpf_init_inode_xattr kfunc
This commit adds BPF selftests to verify atomic inode xattr labeling in the inode_init_security hook. It introduces tests that attach to the hook and validate that bpf_init_inode_xattr properly sets xattrs and respects the per-inode slot limit.
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +0
-0
[ ... ]
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +8
-0@@ -268,6 +269,123 @@ static void test_fsverity(void) +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 286
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]
Will this test become flaky if the file already exists from a previously aborted test run?
If the testfile remains on disk from an earlier crash or failure, open() with O_CREAT will simply open the existing file rather than create a new inode. This would bypass the inode_init_security LSM hook entirely, meaning the BPF program won't execute and init_result will remain at its default value, causing the test to fail.
Would it be safer to call remove(testfile_new) before this open(), or use a unique temporary filename to ensure the hook is always triggered?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +4
-0+if (!ASSERT_GE (fd,0 ,"create_file" )) + goto out; + +ASSERT_EQ (skel->data->init_result,0 ,"init_result" );
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +2
-0+static void test_init_inode_xattr_slot_limit (void ) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c static void test_fsverity(void) · line 355
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +2
-0+/* Trigger inode_init_security once with all programs attached. */ + fd =open (testfile_slot,O_CREAT |O_RDWR ,0644 );
[Severity: Medium]
Does this face the same issue where a pre-existing testfile_slot will prevent the LSM hook from firing?
If remove(testfile_slot) is called prior to open(), it would guarantee a new inode is created each time.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +5
-0+if (!ASSERT_GE (fd,0 ,"create_file" )) + goto out; + +/* +* Exactly INIT_INODE_XATTR_MAX programs claim a slot; the program past
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Kumar Kartikeya Dwivedi
maintainerQuoted 14 lines · David Windsor
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_securityhook, 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 likebpf_set_dentry_xattrrequire a dentry that isn't available until after the inode is accessible.
Quoted 4 lines
This series introduces the
bpf_init_inode_xattr()kfunc, which takes the combinedinode_init_securityxattr context argument and claims a slot in it via the newsecurity_lsmxattr_add()LSM helper.
There are various CI failures in newly added tests, I don't think any of them are passing. Please fix before respinning.
https://github.com/kernel-patches/bpf/pull/12730
E.g. both test_init_inode_xattr and test_init_inode_xattr_slot do not have expected results.
pw-bot: cr
Quoted 1 line
[...]
Quoted 21 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · David Windsor
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_securityhook, 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 likebpf_set_dentry_xattrrequire a dentry that isn't available until after the inode is accessible.Quoted 4 lines
This series introduces the
bpf_init_inode_xattr()kfunc, which takes the combinedinode_init_securityxattr context argument and claims a slot in it via the newsecurity_lsmxattr_add()LSM helper.
Quoted 8 lines
There are various CI failures in newly added tests, I don't think any of them are passing. Please fix before respinning.
https://github.com/kernel-patches/bpf/pull/12730
E.g. both
test_init_inode_xattrandtest_init_inode_xattr_slotdo not have expected results.
This is happening because the path "/tmp" in bpf ci isn't mounted on tmpfs, rather on 9p:
[ 5.813072] VFS: Mounted root (9p filesystem) on device 0:22. [ 5.924390] vmtest: Mounting tmpfs at /dev/shm [ 5.971012] vmtest: Mounting tmpfs at /run [ 6.096271] vmtest: Mounting tmpfs at /mnt
... which doesn't call inode_init_security. Changing the test to create its file in /dev/shm, which has a real tmpfs, fixes this. We can update the test to skip on filesystems where the hook doesn't fire.
I'll wait to gather feedback from Alexei and Paul before sending v6.
Quoted 3 lines
pw-bot: cr
Quoted 1 line
[...]