From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796323221; b=EPf5RmSxnBxWiDMt69o+PinlKVho/wpc+sEAxV9Sdzp+pIHZmgHRcaxqTKI3Pdad6A6K 5H9zKRwBLWlMoZ/pQHCBHJ7ElfUoWPYsgd7w9D8udrVQsTxJEb+Nsp8W4QoxVjceh+THi QRzIIBPnDNPINmmmcfCofWtTg4GgW+lSGJvh/pJUQbHaoDZGcPeRmOioNZI1ewQ/M5b0b LabGx3g7lqJC0zxy+X5G00sTF85hNGx8C5NH4GZepkWpXO0VAjrfYWabkrkPdNv9JlsEs hLcPBoMpQvpitEV8mbam2ab2ep8yTg+ANYW3SvN45MIuzLBqLM9MVAKtZlAgLzya89A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796323221; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=uMGp13qtOHGtU+gehhHi0apwQWt5SvPAdIX1b3OhMEk=; b=j9Evolj+sRhM6T/KRIAiqNGa4CoBR41KYUNG7lbEA0jOV42VoxS6zxn8wfcnhotGtir6 +AZhMM64cwge+rDiRmUyuQQolkE+JGDOQ/mCriwbPTDG/1WXFb4tR2J4sW045NwrIPnee Xqh2YTkUHA9DU6APKbghvSImQ6rzxJMdsip2Rv0J9L4Tav7rE/lZaJZKrIjMs3NsVndk5 w1O75YGzbWnF1qz8ISK4GL1v15XJq98qDzPCVxFrFZviAn0d7+/7Ab+clxhDJdN79byIK g0QOfxTsulsBI3ZOvulIFlcnoFb83NbpZEkSLH60x7qIS76kOx7CleGaVJ8FtUg6opg== 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=FL6WIEaQ; arc=none smtp.remote-ip=74.125.82.52 Received: from mail-dl1-f52.google.com (mail-dl1-f52.google.com [74.125.82.52]) (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 6C160606A322 for ; Thu, 3 Dec 2026 18:40:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6C160606A322 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=FL6WIEaQ Received: by mail-dl1-f52.google.com with SMTP id a92af1059eb24-1b59dfe466bso146179c88.1 for ; Thu, 03 Dec 2026 10:40:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796323220; x=1796928020; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=uMGp13qtOHGtU+gehhHi0apwQWt5SvPAdIX1b3OhMEk=; b=FL6WIEaQ9QZ34m3iyxkvekMImyOfZJaQc6l7LTWFMfdVk9KvKCI4ztdJO8mdnaBo3f +8y2M0hYA7klEvNZ6lCKRCK1MeypyxpbKjERf2NtfDdRuxbLn29RfdIt/mSzq1mL9Qp4 tW4q2irrGayWQS9BPr1Dr6LnDwFQ4Cex20+g93jeY115kJ9dyYbx49EhBfFFeN5GuQEi m38J7eyWetKIEQqdNzM7Fx06b1FKoZrwCmaJo/1Pc6/apLReEp5Sa24eqHWLiAJUyAqh qPM5jXDdkY3Ri6kTgVhlWu2/f/aA1ZAuUYIZgjSKwDOo/ybgHRqq/V9zB/XCt39+mKJP ak0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796323220; x=1796928020; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uMGp13qtOHGtU+gehhHi0apwQWt5SvPAdIX1b3OhMEk=; b=IRCxdTNMt6VnE5xEPZp/eiaee3+mukkaK5FWaYoyph4ssdCxcUaIL8aSze+ntlvCf8 XUrFVDxaKzWSmDY8ze1y9ivQdn5HFpmaoEH6v1S/cKFg5818Atcw64Uv2DsNmr9EHAVm UyaacTTxuGFMnhlpM9IfAuSJji5/b4YP6fQHAk2IvkIytBezjySQnQLWBPFoh3rSwKWU WhWu6UjigHSsuyWi/UfE/F3x++pv13uJERmOrvE8uzegc2vnUqv62O6VHOrdh332hPf5 B+9nDAOG5WKd9Ue7+GhSpVwv2rC7x1H/36ZZHDKObuuVYf21iLheXn3RU3JwBIfgqR29 3vOg== X-Forwarded-Encrypted: i=1; AL+hErG/dER0ygS8E+qCbs2oEBBs0LfPw7ooWJnxvpOI+gXvZ+xfA+P/cPCeNMl/q/L7Tcm1SyiKkKda6hltozIq9NZxsvvi7iw=@vger.kernel.org X-Gm-Message-State: APuU4Z8p7nGiRMiXxuNlOb5VFPi39qOeXh7iFFcXV1hKW1F3cQdYTLME cZCiZFsOPFW3MF8srYlEecyW9sR8x+Pl53I6M6liepfYRTbW85mRSovg X-Gm-Gg: AZFdTZc8hUh2+kdu5SPWXv8AIb3CYVfqrk3VerhMAQsJ/erLDiVQGBPaz1WfmmqMrlI CVjeRbJl3nOY0sM2RGq5RIgHyn13cyIKef7Z446Hw9aXtnM3Nc7O54ZCIK8dBK6vRE4tLKoTFnc jozYziA0lsoBQYGi1LGpr4IguZS7PIvKO95CZl9u9vDuucDnigyhxAtHe3dx2VqqoUQGp/hxK94 nVEKfYMtAtKtaWk7K1I1+zcIQGFcTnOGsyC05flmH6GQGdK8yTuByvnmrCzXdadov5dDJO8ISvx n0V6q1GhA2KCydd0RXqcvIwY7j5BFjKNV7D42qs7rPEfZfEuRnt8FarJLNlu2s4B6Xuu6g9rP4D rJZ4ITEpKbKC22D3Zvjul61tUuN4774evbLVv4X/22Dd6x4MPXnkKBZhTDlQ358ONvllTPiC5jJ +hjDfttmrruNg+v1C+tZXWLoYt2b3/Y5nddkg6gp8SFgUsWOiLJzi76nib58JPV34iUf990n2nX yHA1DIi398Obcp5bW+s11Gl0l6cnRzeVXHQh3vOvjBI9lTTa2xB/45onpS0yRsBhus4By+RbY5Z 8bZSlxqXEOALdmeUuvtc8z19yz61u96Z3DG+VPPuAHKi+rfUvyr46/kFwacxAJjU X-Received: by 2002:a05:701b:4591:10b0:143:297c:276c with SMTP id a92af1059eb24-1b55691aac2mr37277548c88.33.1796323220378; Thu, 03 Dec 2026 10:40:20 -0800 (PST) Received: from ?IPV6:2a03:83e0:115c:1:bbe1:dcf2:889b:346e? ([2620:10d:c090:500::5:878]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b55682d301sm107489958c88.10.2026.12.03.10.40.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Dec 2026 10:40:20 -0800 (PST) Message-ID: <61abb49f-4694-4846-bf04-0f9e62d73c24@gmail.com> Date: Thu, 3 Dec 2026 18:40:17 +0000 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf] bpf: Destroy inode storage after an RCU grace period To: Chris Mason , 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, linux-security-module@vger.kernel.org References: <20261203163706.50213-1-mason@kernel.org> Content-Language: en-US From: Mykyta Yatsenko In-Reply-To: <20261203163706.50213-1-mason@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 > --- > 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 > #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