From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796398986; b=CfyRXgvZQr1kv0q0bvsCwN/D8BW/KEZ2xiGF46sBCUqEjhU+dxG6IgrcaB2BSFeO7tpB M6yu6eIWAQ2RjLk479JXcsu+qXJ4E6/e/l1DsgE0O9bCWiRMDMuyYLMKyRL+ukAkgQ/Dh zIVWKaYClM5Ub5lJpUUJPSIM1laZU8y1Chy9JYnIb5hm/4HjPNgt3/yC/zNn65sG33m0T Btsc3YH+I6CftgCAabGWem4HkaKaiPK7c/RT73JLL1VRC6xjIL4FM3NC/v7BrDpZnyeg4 8qDrkAEzawJinjqK0rFT9kl8/zBOrPwMIzNcxWTG4rm3bJjshlJkH2CaYfCp/e6UtsA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796398986; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=QeUupYGwBVYxT52t0dwWBaIc+eOo/1B0RZxf8Xu+Bj4=; b=yIePrW64MLC2/F8mmzpravQMUGvfFNuUOexEafP56GFTfBb2DMt5YeuM9mwcfKhAF1Og 6daJihJPtnxkeMUKJ3n86SH+e3DXk0UMt51E8OHAOCJ7ipngA8YJF0vcIqHkoYvCJkG2K CnJOWQnLzfAD44tc9hFQM1SS738RZ2TZFppJLLJRkURQujVsSHI365L/2sdX1Q44u1cNw FECNgwPXBPKNbC5l656WXtVWcIrNQ0uCeW1MwbibuTW44kuTr9aRlj89mrMw6T/Do3gM/ njpmSnRNMbjneGYn+XA88QZRHCyPrdYlrNzTFR8Z1ABuPANb+Xq+DIxxKxGEJ9hzeDw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=YbO0+nam; arc=none smtp.remote-ip=209.85.128.45 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A87D606A6BD for ; Fri, 4 Dec 2026 15:43:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8A87D606A6BD Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=YbO0+nam Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4bbcb9bccf6so16745555e9.1 for ; Fri, 04 Dec 2026 07:43:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796398985; x=1797003785; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QeUupYGwBVYxT52t0dwWBaIc+eOo/1B0RZxf8Xu+Bj4=; b=YbO0+namKT8t/iYXvbSy5ynTgnvPEnDx5K2WKG83/+75U+e9BfTSevY/n2YCi5iHhJ aoqCF2ApXLa3uZpDFzL553MbJ5XRUb0KoLHZNYWRrATF5twA6E8PHSDBupRzHQwZmpkd NDc4LcxlPG7onI99eZqO1XK5HWDzbNdrwxqPJdR+aplUbZpHAT3GEoWCB8KGzMG5dsx1 87PjrIc1TG0chjKijnq6Pa+KoxmVK4VOvlIkY4wdzRhuRQzaWAXGiIHMwm/41x8b+/IO YuHjgTgBsR5I1kmNQpkiFffT8iu0BTKp/GXVxRuw71Lw817DmdDkMgrAg1hcRv8b9524 3lzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796398985; x=1797003785; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=QeUupYGwBVYxT52t0dwWBaIc+eOo/1B0RZxf8Xu+Bj4=; b=QBrsJ8gTJCcT6ZFzAdbp09Y7bI1Nf+DV3MaiyXFi7zKj3Xre1aFpyi/MOfcZzXtk/J ycLiXdBckDfhMm/5394nZw7kfhpOauwSKdsp5W0cmJOEDpQe/8EGb/RKQK5Tn+fD1L6f magC6js4QnTnxm7DJ+IlIfcBMzZmCwzHUAwy3IQYhYK88jII45dVpmYd5wU1sP5oIWuk NSaaYU5z5geMOrX4ciPBH1/ZGgK3KtGZg8jxvxN/IHf+0THF+B2aq6fUmkEEdYO0eGCv Zw7CcwDqhsy9J8ah/75hNSiURMyS5k9F1t3JvGiGorWa8Q828s4dcshtYBVtkQbefq2n n7eg== X-Forwarded-Encrypted: i=1; AL+hErFrQTdm73TSOJBYIKIfEjMhWUqNYCXe+6R3yJALzbqxUcZBOeGWXxdSRhtkbWa9YHs2tUbw5M0iEA2+7hs=@vger.kernel.org X-Gm-Message-State: APuU4Z/HI99POd7sjYTeIg9jmBbb2eAayuaW0II/7BEm9Z/XHskbYAzr 9CrNBu0qQXcODC7McxssjCsO32AtRWjNrTS5EcodBQCe6Vt35fkRnQw9 X-Gm-Gg: AZFdTZeCiUwSrdgdbSKuog8OgOPX7cTyyUKSj9c9OIcceyjNPtAl1HhXk7v0HWNmv8M F7H93jSR9mIt9LtHQamamZjC9EzFfjDb4XTWE4VEXL1RqGNbnWfoJ2pfEMK0ht7msCIsIUGv4AG DDe/OsHAi+Cf9c9mChLdQeByrjzq3KUwIGVvxTjPhce0AlPY1T7AC2yapdVHz5EBfioJfI/7TZ1 vhiqBKMrbyUzAFy2JtFd+wxc6DXAvCYrpNAQXOrzogPisz5oT12+mAtEHmgE/ufrbg6MFAhzj7L Ae4cXRa29Q0CIfWaUZ0xtrJY8fUGT5Q3z3SgPCOwpr2Qdvmf9wQ0ZigBJq2gIq1lfXEFSpt/GBj CiN4UhnPXarFXKrOJ/tdCactKZpB4J0/PY6JDNKKnXuiKkUYdcEvKRAtsNxykit3DKPFV6tLM+D 9Ir0JEQz66vHko1RsAJaloXpH95fPFmXXD+c8OeXyzW4v5AN0meVGmddqtBmUCnHqK/18Y/ks+M l/W3NjHJha8jchgTKfhpZKspLVbtQrsr0den7syqkKcNbbiZOvV4kZFA18IXZWlWYrTcHdy+/lP O5swH5GNufF25oqMaAmwta+4cEtfwNRganr0r5FnsWIBlUx3OviO7jLK9wwKiTlyxmg9 X-Received: by 2002:a05:600c:628f:b0:4bb:ab2c:67f7 with SMTP id 5b1f17b1804b1-4bbeb88b99dmr50559295e9.8.1796398984747; Fri, 04 Dec 2026 07:43:04 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbea25ce51sm63147535e9.5.2026.12.04.07.43.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 07:43:04 -0800 (PST) From: Anton Protopopov To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , KP Singh , Matt Bobrowski , Alexander Viro , Christian Brauner , Jan Kara , Mimi Zohar , Roberto Sassu , bpf@vger.kernel.org, linux-integrity@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 4/6] bpf: Let sleepable LSM programs attach to six more inode hooks Date: Fri, 4 Dec 2026 15:54:34 +0000 Message-ID: <20261204155438.1413411-5-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204155438.1413411-1-a.s.protopopov@gmail.com> References: <20261204155438.1413411-1-a.s.protopopov@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. The bpf_ima_inode_hash() and bpf_ima_file_hash() helpers take iint->mutex, which IMA holds while it locks the inode or sets security.ima. The ACL and file_setattr hooks are in d_inode_locked_hooks, so the helpers are not allowed there. IMA may copy up an overlay file when it sets security.ima, so don't allow them on inode_copy_up either. Co-developed-by: Daniel Borkmann Signed-off-by: Daniel Borkmann Signed-off-by: Anton Protopopov --- fs/bpf_fs_kfuncs.c | 18 +++++++++++++++++- kernel/bpf/bpf_lsm.c | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index a512d449b54d..6021e9f60ad2 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 6c79d4786f67..c4b626b0dbda 100644 --- a/kernel/bpf/bpf_lsm.c +++ b/kernel/bpf/bpf_lsm.c @@ -200,6 +200,7 @@ BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size) /* Hooks which may be called with the inode locked or with iint->mutex held */ BTF_SET_START(bpf_ima_inode_hash_denied_hooks) +BTF_ID(func, bpf_lsm_inode_copy_up) BTF_ID(func, bpf_lsm_inode_getattr) BTF_ID(func, bpf_lsm_inode_getxattr) BTF_SET_END(bpf_ima_inode_hash_denied_hooks) @@ -349,18 +350,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