[PATCH bpf] bpf: Destroy inode storage after an RCU grace period

Mykyta Yatsenko mykyta.yatsenko5 at gmail.com
Thu Dec 3 18:40:17 UTC 2026



On 12/3/26 4:37 PM, Chris Mason wrote:
> 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 at 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:

I'm reworking my series significantly and that code in patch3
is no longer needed. Thanks!

> 
>   [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




More information about the Linux-security-module-archive mailing list