The inode_create, inode_mkdir, inode_mknod, inode_symlink, inode_link and inode_rename hooks get the parent directory as an inode and the new entry as a dentry. To check a label of the directory, a program needs to read its xattr, but bpf_get_dentry_xattr() takes a dentry. The only dentry of the directory at hand is dentry->d_parent, and the verifier doesn't trust it.
Making d_parent trusted would be wrong. It would be trusted on every hook, and many hooks don't hold the lock of the parent directory. For example, inode_getxattr and inode_readlink hold no lock at all. A concurrent rename can then move the dentry to another directory, and the old parent can be freed while a sleepable program still uses it. Add the bpf_get_inode_xattr() kfunc which reads an xattr of an inode through one of its dentries. Xattrs belong to the inode, but some filesystems like cifs build a path from the dentry. So take a hashed dentry with d_find_alias() if there is one. An unlinked file which is still open has none, so take any with d_find_any_alias() then, as inode_doinit_with_dentry() in SELinux does. The kfunc doesn't change xattrs, so the setters still get only the dentries passed to the hook, and d_inode_locked_hooks stays correct for them. Signed-off-by: Anton Protopopov <[email protected]> --- fs/bpf_fs_kfuncs.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 6021e9f60ad2..4e2b371fd0b5 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -169,6 +169,38 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, return bpf_get_dentry_xattr(dentry, name__str, value_p); } +/** + * bpf_get_inode_xattr - get xattr of an inode + * @inode: inode to get xattr from + * @name__str: name of the xattr + * @value_p: output buffer of the xattr value + * + * Get xattr *name__str* of *inode* and store the output in *value_ptr*. + * The xattr is read through a hashed dentry of *inode* if it has one and + * through any dentry otherwise. + * + * For security reasons, only *name__str* with prefixes "user." or + * "security.bpf." are allowed. + * + * Return: length of the xattr value on success, -ENOENT if *inode* has + * no dentry, another negative value on error. + */ +__bpf_kfunc int bpf_get_inode_xattr(struct inode *inode, const char *name__str, + struct bpf_dynptr *value_p) +{ + struct dentry *dentry; + int ret; + + dentry = d_find_alias(inode); + if (!dentry) + dentry = d_find_any_alias(inode); + if (!dentry) + return -ENOENT; + ret = bpf_get_dentry_xattr(dentry, name__str, value_p); + dput(dentry); + return ret; +} + __bpf_kfunc_end_defs(); static int bpf_xattr_write_permission(const char *name, struct inode *inode) @@ -506,6 +538,7 @@ BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) BTF_ID_FLAGS(func, bpf_path_d_path) BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_get_inode_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_data_inode, KF_SLEEPABLE | KF_RET_NULL) -- 2.43.0
