Re: [PATCH v6 bpf-next 3/4] bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling
Paul Moore <[email protected]> Thu, 30 Jul 2026 19:53:43 -0400
| Newsgroups | org.kernel.vger.linux-integrity,org.kernel.vger.bpf,org.kernel.vger.linux-fsdevel,org.kernel.vger.linux-kernel,org.kernel.vger.linux-kselftest,org.kernel.vger.linux-security-module,org.kernel.vger.selinux |
|---|---|
| Message-ID | <CAHC9VhT=m-GtOZx2AHym7uN1CNx94arKBiXeVC7LTEJ-b=1EVQ@mail.gmail.com> |
On Thu, Jul 30, 2026 at 7:45=E2=80=AFPM David Windsor <[email protected]> = wrote: > > Add 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. > > Link: https://kernsec.org/pipermail/linux-security-module-archive/2022-Oc= tober/034878.html [1] > Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/= commit/?id=3D6bcdfd2cac55 [2] > Suggested-by: Song Liu <[email protected]> > Signed-off-by: David Windsor <[email protected]> > --- > fs/bpf_fs_kfuncs.c | 41 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 41 insertions(+) > > diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c > index f1863a891db6..7b55073934e0 100644 > --- a/fs/bpf_fs_kfuncs.c > +++ b/fs/bpf_fs_kfuncs.c > @@ -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(); > > @@ -379,6 +381,44 @@ __bpf_kfunc struct inode *bpf_real_data_inode(struct= file *file) > return d_real_inode(file_dentry(file)); > } > > +/** > + * bpf_init_inode_xattr - set an xattr on a new inode from inode_init_se= curity > + * @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, -EOPNOTSUPP if the filesystem does not accept > + * xattrs at inode creation, negative error on other failures. > + */ > +__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 =3D (struct bpf_dynptr_kern *)v= alue_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; > + > + if (!xattrs->xattrs) > + return -EOPNOTSUPP; > + > + value_len =3D __bpf_dynptr_size(value_ptr); > + value =3D __bpf_dynptr_data(value_ptr, value_len); > + if (!value) > + return -EINVAL; > + > + return security_lsmxattr_add(xattrs, LSM_ID_BPF, > + name__str + sizeof(XATTR_BPF_LSM_SUF= FIX) - 1, > + value, value_len); > +} > + Based on our discussion in the v5 patchset, I was expecting to see this under security/; did something happen, or was there some confusion? --=20 paul-moore.com