From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796315830; b=nhwULqLPW8B0LyWCa1S9ril3GnrDDcQueZc7F4Cwd8Po8OM33JHi1eFlgavMXFiIU91o vi2MWLFkHmC6PwS6ulqJxhtAhRk0TuorHRUla+IiIeM5YZJsfdMKkAClBUFgDEMYlcLqS OvQKkyik0MuTj1icXZn2GNYsav/n7XL+ObWLQLEhKn78cwxu/YUZbEmw4NtXvttQKEmiQ OR+TQ9meb5Cq1o5QG58zRoZpqecGBcTNw+NbxXyWMtY5+kRDkxPjXo1j+E5ph+EOQEyd2 0gqcnz6SZ83bq+z44/EBOc23anfhUEBykqOwhpEygiL4KLQftYUYsHt9S6mMkMFxnIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796315830; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=hHrGD5JTQhNiyptGQA1prUFPemolxanlD4ujvCprlcA=; b=AqZ3yPcMzXHRCv5MYt0viQg4CtteB7KObmEaM9zeq0s28T8j/cUWHcTMiMrIX9oCQMLI jSBVWelhvnqbCMPE6VmzHFnuQ1TpbMc0HEc36veat1Kn8S70kHCCojuJwZ1OWZzBNLOgc OPEwBTnNzYwAaWT0wL8M0A4AJ4gjgCsUdMkJMh9ssQZXZHRe6odBsYlU0Qz6fDR3Hijoq vYN3+wyGUyU2VVe2Cod0vCmaLeZsyIGj/taAQ+GCOUgFCWpxxXr2oAUV9Qi8sBCbh+fe6 XuZxLTId3rRp+8KIusC/IV3wcWWGKypBkdcvTJBmpTuL8TSHJ/f78yYBtPDpN4MX7yg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aSfE2XLE; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A385606A322; Thu, 3 Dec 2026 16:37:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aSfE2XLE Received: by smtp.kernel.org (Postfix) with ESMTPSA id D18051F00893; Thu, 3 Dec 2026 16:37:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796315830; bh=hHrGD5JTQhNiyptGQA1prUFPemolxanlD4ujvCprlcA=; h=From:To:Cc:Subject:Date; b=aSfE2XLEAH+1s1FG4urGiUXJuWuNl1XpS2Pzut4sVsC9LaFe0uE3BChzPArcHWJ9O j/GHL50hAGQjMfhgDQ9+jOZM1BFXx/B8Zinoab00U7/dufsNk0gyRtwp/R53enb94f POrdTJ5mxAmtMYZ0eC+/qBu6kkHFlDRylfjYPn7pb7guFaxxUluEDI8tYblvE3W47S m8+DbT0DtsHxSUaL2OriU/jWKmz/km/NRjKOAn9BcozmMh+E5jd7z82FSdB4mxtGL5 yUx5HDTfAE+SeyMlAU+w3z1kRl+n0zZPBOfVaoQUkCTNZ5NuBUjpG+NMFlY2KFJsed WByt+PdYle/oA== From: Chris Mason To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kpsingh@kernel.org, matt@bobrowski.net Cc: eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, john.fastabend@gmail.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, yatsenko@meta.com, mykyta.yatsenko5@gmail.com, linux-security-module@vger.kernel.org, mason@kernel.org Subject: [PATCH bpf] bpf: Destroy inode storage after an RCU grace period Date: Thu, 3 Dec 2026 11:37:06 -0500 Message-ID: <20261203163706.50213-1-mason@kernel.org> X-Mailer: git-send-email 2.55.0 X-Mailing-List: linux-security-module@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 BPF inode storage hits a use-after-free on the inode if an RCU path walk creates an element after security_inode_free(). bpf_local_storage_destroy() expects that BPF programs and syscalls can no longer add or delete elements. But bpf_inode_storage_free() calls it from security_inode_free(), and an RCU path walk can still run a BPF LSM program on that inode until a grace period has passed: RCU path walk evict() ============= ======= security_inode_permission() security_inode_free() bpf_inode_storage_free() bpf_inode_storage_get(F_CREATE) storage->owner = inode call_rcu(i_callback) bpf_local_storage_map_free() later dereferences the stale storage->owner. The fix used here is to destroy the storage from the inode_free_security_rcu() hook, and to make the LSM blob its owner. Fixes: 8ea636848aca ("bpf: Implement bpf_local_storage for inodes") Signed-off-by: Chris Mason --- This is an AI generated fix, on top of v7.4-rc5. Mykyta, this conflicts with patch 3 of your inline value series: [PATCH bpf-next v2 03/13] bpf: Add bpf_local_storage_delete() https://lore.kernel.org/all/20261123-sls_f1-v2-3-76474de9b052@meta.com/ This patch makes the LSM blob the owner of the storage, but your inode_storage_delete() passes the inode as the owner. I think the two will still compile together, but the delete will use the wrong pointer. It should check for a NULL blob, and then pass bpf_inode(inode). That's just from reading the code, I haven't tried them together. include/linux/bpf_lsm.h | 4 ++-- kernel/bpf/bpf_inode_storage.c | 40 +++++++++++++++++++--------------- security/bpf/hooks.c | 2 +- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h index eb4a38eef87ec..d4148197b2b08 100644 --- a/include/linux/bpf_lsm.h +++ b/include/linux/bpf_lsm.h @@ -51,7 +51,7 @@ static inline struct bpf_storage_blob *bpf_inode( extern const struct bpf_func_proto bpf_inode_storage_get_proto; extern const struct bpf_func_proto bpf_inode_storage_delete_proto; -void bpf_inode_storage_free(struct inode *inode); +void bpf_inode_storage_free(void *inode_security); void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, bpf_func_t *bpf_func); @@ -89,7 +89,7 @@ static inline struct bpf_storage_blob *bpf_inode( return NULL; } -static inline void bpf_inode_storage_free(struct inode *inode) +static inline void bpf_inode_storage_free(void *inode_security) { } diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c index f9e81060c1f45..fdae59b25cd34 100644 --- a/kernel/bpf/bpf_inode_storage.c +++ b/kernel/bpf/bpf_inode_storage.c @@ -20,15 +20,12 @@ DEFINE_BPF_STORAGE_CACHE(inode_cache); +/* The owner is the blob: the inode can be freed ahead of it. */ static struct bpf_local_storage __rcu ** inode_storage_ptr(void *owner) { - struct inode *inode = owner; - struct bpf_storage_blob *bsb; + struct bpf_storage_blob *bsb = owner; - bsb = bpf_inode(inode); - if (!bsb) - return NULL; return &bsb->storage; } @@ -53,14 +50,12 @@ static struct bpf_local_storage_data *inode_storage_lookup(struct inode *inode, return bpf_local_storage_lookup(inode_storage, smap, cacheit_lockit); } -void bpf_inode_storage_free(struct inode *inode) +void bpf_inode_storage_free(void *inode_security) { struct bpf_local_storage *local_storage; struct bpf_storage_blob *bsb; - bsb = bpf_inode(inode); - if (!bsb) - return; + bsb = inode_security + bpf_lsm_blob_sizes.lbs_inode; rcu_read_lock_dont_migrate(); @@ -89,14 +84,16 @@ static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key, void *value, u64 map_flags) { struct bpf_local_storage_data *sdata; + struct bpf_storage_blob *bsb; CLASS(fd_raw, f)(*(int *)key); if (fd_empty(f)) return -EBADF; - if (!inode_storage_ptr(file_inode(fd_file(f)))) + bsb = bpf_inode(file_inode(fd_file(f))); + if (!bsb) return -EBADF; - sdata = bpf_local_storage_update(file_inode(fd_file(f)), + sdata = bpf_local_storage_update(bsb, (struct bpf_local_storage_map *)map, value, map_flags, false); return PTR_ERR_OR_ZERO(sdata); @@ -126,29 +123,35 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, void *, value, u64, flags) { struct bpf_local_storage_data *sdata; + struct bpf_storage_blob *bsb; WARN_ON_ONCE(!bpf_rcu_lock_held()); if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) return (unsigned long)NULL; - /* explicitly check that the inode_storage_ptr is not + if (!inode) + return (unsigned long)NULL; + + /* explicitly check that the blob is not * NULL as inode_storage_lookup returns NULL in this case and * bpf_local_storage_update expects the owner to have a * valid storage pointer. */ - if (!inode || !inode_storage_ptr(inode)) + bsb = bpf_inode(inode); + if (!bsb) return (unsigned long)NULL; sdata = inode_storage_lookup(inode, map, true); if (sdata) return (unsigned long)sdata->data; - /* This helper must only called from where the inode is guaranteed - * to have a refcount and cannot be freed. + /* This helper must only be called from places where the lifetime of the + * inode is guaranteed. Either by being refcounted or by being protected + * by an RCU read-side critical section. */ if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) { sdata = bpf_local_storage_update( - inode, (struct bpf_local_storage_map *)map, value, + bsb, (struct bpf_local_storage_map *)map, value, BPF_NOEXIST, false); return IS_ERR(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data; @@ -164,8 +167,9 @@ BPF_CALL_2(bpf_inode_storage_delete, if (!inode) return -EINVAL; - /* This helper must only called from where the inode is guaranteed - * to have a refcount and cannot be freed. + /* This helper must only be called from places where the lifetime of the + * inode is guaranteed. Either by being refcounted or by being protected + * by an RCU read-side critical section. */ return inode_storage_delete(inode, map); } diff --git a/security/bpf/hooks.c b/security/bpf/hooks.c index 8f8c3de3035f0..e4b5c9599130d 100644 --- a/security/bpf/hooks.c +++ b/security/bpf/hooks.c @@ -14,7 +14,7 @@ static struct security_hook_list bpf_lsm_hooks[] __ro_after_init = { LSM_HOOK_INIT(NAME, bpf_lsm_##NAME), #include #undef LSM_HOOK - LSM_HOOK_INIT(inode_free_security, bpf_inode_storage_free), + LSM_HOOK_INIT(inode_free_security_rcu, bpf_inode_storage_free), }; static const struct lsm_id bpf_lsmid = { base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.55.0