mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Anton Protopopov <a.s.protopopov@gmail.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	KP Singh <kpsingh@kernel.org>,
	Matt Bobrowski <matt@bobrowski.net>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Anton Protopopov <a.s.protopopov@gmail.com>
Subject: [PATCH v1 bpf-next 1/3] bpf: Let sleepable LSM programs attach to six more inode hooks
Date: Thu,  3 Dec 2026 13:44:05 +0000	[thread overview]
Message-ID: <20261203134411.1092433-2-a.s.protopopov@gmail.com> (raw)
In-Reply-To: <20261203134411.1092433-1-a.s.protopopov@gmail.com>

The bpf_get_dentry_xattr() and bpf_get_file_xattr() kfuncs may sleep,
so only sleepable LSM programs can call them. The inode_mkdir,
inode_link, inode_set_acl, inode_remove_acl, inode_file_setattr and
inode_copy_up hooks are not in sleepable_lsm_hooks. So a program on
these hooks can't read an xattr, for example to check a label kept
in a security.bpf. xattr before a mkdir or an ACL change.

All of these hooks are called in process context and may sleep. The
vfs_mkdir() and vfs_link() functions call their hooks with the
parent directory locked. Cachefiles calls inode_mkdir with no lock
held. The vfs_set_acl(), vfs_remove_acl() and vfs_fileattr_set()
functions call their hooks with the target inode locked. Overlayfs
calls inode_copy_up under its own copy-up lock of the inode. Add the
hooks to the set.

The bpf_set_dentry_xattr() and bpf_remove_dentry_xattr() kfuncs lock
the inode themselves, unless the hook is in d_inode_locked_hooks.
For such hooks the verifier replaces them with the _locked variants.
Add the ACL and file_setattr hooks to this list, otherwise a program
which calls the setters there deadlocks. On inode_copy_up the setters
can't be used at all: overlayfs would try to copy the inode up again
and wait on the lock which the caller already holds. Refuse them
there. Reading an xattr on inode_copy_up is fine. On inode_mkdir and
inode_link the dentries passed to the hook are either negative or
not locked, so the plain setters are right.

Co-developed-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
 fs/bpf_fs_kfuncs.c   | 18 +++++++++++++++++-
 kernel/bpf/bpf_lsm.c |  6 ++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c
index 35ccd98a4de6..1b973bd4e535 100644
--- a/fs/bpf_fs_kfuncs.c
+++ b/fs/bpf_fs_kfuncs.c
@@ -527,6 +527,15 @@ BTF_SET_START(bpf_inode_init_xattr_hooks)
 BTF_ID(func, bpf_lsm_inode_init_security)
 BTF_SET_END(bpf_inode_init_xattr_hooks)
 
+/*
+ * Hooks that may not set or remove an xattr. A setter on the overlay
+ * dentry of inode_copy_up copies it up again and waits on the copy-up
+ * lock the caller holds.
+ */
+BTF_SET_START(bpf_xattr_read_only_hooks)
+BTF_ID(func, bpf_lsm_inode_copy_up)
+BTF_SET_END(bpf_xattr_read_only_hooks)
+
 static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
 {
 	if (!btf_id_set8_contains(&bpf_fs_kfunc_set_ids, kfunc_id))
@@ -539,8 +548,12 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
 			return -EACCES;
 		return 0;
 	}
-	if (prog->type == BPF_PROG_TYPE_LSM)
+	if (prog->type == BPF_PROG_TYPE_LSM) {
+		if (btf_id_set_contains(&bpf_fs_kfunc_lsm_only_ids, kfunc_id) &&
+		    btf_id_set_contains(&bpf_xattr_read_only_hooks, prog->aux->attach_btf_id))
+			return -EACCES;
 		return 0;
+	}
 	if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
 		return -EACCES;
 	/* ->st_ops is unset during the cfg pass; enforced once it is set. */
@@ -563,11 +576,14 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
  * should call bpf_[set|remove]_dentry_xattr.
  */
 BTF_SET_START(d_inode_locked_hooks)
+BTF_ID(func, bpf_lsm_inode_file_setattr)
 BTF_ID(func, bpf_lsm_inode_post_removexattr)
 BTF_ID(func, bpf_lsm_inode_post_setattr)
 BTF_ID(func, bpf_lsm_inode_post_setxattr)
+BTF_ID(func, bpf_lsm_inode_remove_acl)
 BTF_ID(func, bpf_lsm_inode_removexattr)
 BTF_ID(func, bpf_lsm_inode_rmdir)
+BTF_ID(func, bpf_lsm_inode_set_acl)
 BTF_ID(func, bpf_lsm_inode_setattr)
 BTF_ID(func, bpf_lsm_inode_setxattr)
 BTF_ID(func, bpf_lsm_inode_unlink)
diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c
index 1e2acb7581e7..62303210dde9 100644
--- a/kernel/bpf/bpf_lsm.c
+++ b/kernel/bpf/bpf_lsm.c
@@ -340,18 +340,24 @@ BTF_ID(func, bpf_lsm_file_open)
 BTF_ID(func, bpf_lsm_file_post_open)
 BTF_ID(func, bpf_lsm_file_receive)
 
+BTF_ID(func, bpf_lsm_inode_copy_up)
 BTF_ID(func, bpf_lsm_inode_create)
+BTF_ID(func, bpf_lsm_inode_file_setattr)
 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_link)
+BTF_ID(func, bpf_lsm_inode_mkdir)
 BTF_ID(func, bpf_lsm_inode_mknod)
 BTF_ID(func, bpf_lsm_inode_need_killpriv)
 BTF_ID(func, bpf_lsm_inode_post_setxattr)
 BTF_ID(func, bpf_lsm_inode_post_removexattr)
 BTF_ID(func, bpf_lsm_inode_readlink)
+BTF_ID(func, bpf_lsm_inode_remove_acl)
 BTF_ID(func, bpf_lsm_inode_removexattr)
 BTF_ID(func, bpf_lsm_inode_rename)
 BTF_ID(func, bpf_lsm_inode_rmdir)
+BTF_ID(func, bpf_lsm_inode_set_acl)
 BTF_ID(func, bpf_lsm_inode_setattr)
 BTF_ID(func, bpf_lsm_inode_setxattr)
 BTF_ID(func, bpf_lsm_inode_symlink)
-- 
2.43.0


  reply	other threads:[~2026-12-03 13:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 13:44 [PATCH v1 bpf-next 0/3] bpf: Use the xattr kfuncs on " Anton Protopopov
2026-12-03 13:44 ` Anton Protopopov [this message]
2026-12-03 13:44 ` [PATCH v1 bpf-next 2/3] bpf: Add a bpf_get_inode_xattr kfunc Anton Protopopov
2026-12-03 13:44 ` [PATCH v1 bpf-next 3/3] selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks Anton Protopopov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203134411.1092433-2-a.s.protopopov@gmail.com \
    --to=a.s.protopopov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=matt@bobrowski.net \
    --cc=memxor@gmail.com \
    --cc=song@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®