From: Chris Mason <mason@kernel.org>
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 [thread overview]
Message-ID: <20261203163706.50213-1-mason@kernel.org> (raw)
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 <mason@kernel.org>
---
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 <linux/lsm_hook_defs.h>
#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
reply other threads:[~2026-12-03 16:37 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261203163706.50213-1-mason@kernel.org \
--to=mason@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jmorris@namei.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=matt@bobrowski.net \
--cc=memxor@gmail.com \
--cc=mykyta.yatsenko5@gmail.com \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.com \
--cc=song@kernel.org \
--cc=yatsenko@meta.com \
--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