All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Aaron Esau <aaron1esau@gmail.com>,
	Puranjay Mohan <puranjay12@gmail.com>,
	bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org
Subject: Re: [BUG] bpf: use-after-free in hashtab BPF_F_LOCK in-place update path
Date: Tue, 31 Mar 2026 12:39:48 +0100	[thread overview]
Message-ID: <4f53d7bc-d0a9-409c-81c9-3fe255f08b98@gmail.com> (raw)
In-Reply-To: <CAP01T75bZRbgNXJa_rq_MX6AKm0XBTpOO6V-RSy35b+TFA_Hiw@mail.gmail.com>



On 3/31/26 1:55 AM, Kumar Kartikeya Dwivedi wrote:
> On Fri, 27 Mar 2026 at 17:09, Mykyta Yatsenko
> <mykyta.yatsenko5@gmail.com> wrote:
>>
>> Kumar Kartikeya Dwivedi <memxor@gmail.com> writes:
>>
>>> On Fri, 27 Mar 2026 at 03:54, Aaron Esau <aaron1esau@gmail.com> wrote:
>>>>
>>>> Puranjay Mohan <puranjay12@gmail.com> writes:
>>>>
>>>>> Yes, if all the users have BPF_F_LOCK then it will work because the
>>>>> re-used element will have the same lock and everything is serialized.
>>>>> But this reproducer is trying to show that the elements are being
>>>>> re-used, which they are.
>>>>
>>>> Updated reproducer attached. All operations now use BPF_F_LOCK (updates,
>>>> re-add after delete, and lookups), so value access is serialized via the
>>>> element's embedded spin_lock. The value size is ~4000 bytes. Torn writes
>>>> are still observed (3 in 2.7M).
>>>>
>>>> With full spin_lock coverage, the only remaining unlocked write path is
>>>> alloc_htab_elem() -> copy_map_value(), which copies into a newly
>>>> allocated element prior to insertion. For this to race with a locked
>>>> reader or writer, the reader must be dereferencing a stale pointer to
>>>> memory that has been freed and reallocated -- a use-after-free.
>>>>
>>>> The root cause is bpf_mem_cache_free() in htab_elem_free(), which
>>>> immediately returns memory to the per-CPU freelist rather than deferring
>>>> via RCU. This allows reuse while the syscall path is still within an RCU
>>>> read-side critical section. Preallocated maps do not have this problem
>>>> because freed elements remain within a pool of valid htab_elems.
>>>>
>>>> Switching to bpf_mem_cache_free_rcu(), as Puranjay showed earlier in this
>>>> thread, eliminates the corruption.
>>>
>>> As I said, the reuse is intended behavior. We won't be switching to
>>> bpf_mem_cache_free_rcu().
>>> This is mostly for performance reasons, in case you're perplexed as to why.
>>> That said, we could do copy_map_value_locked() if map_flags &
>>> BPF_F_LOCK to fix this particular case.
>>> Such that if the value is being reused we will still have serialization.
>> I think we should do copy_map_value_locked() on the reuse path.
> 
> Do you want to follow up on this? Otherwise I will get to it
> eventually, either way is fine.
> Thanks

I'll send a patch.

      reply	other threads:[~2026-03-31 11:39 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-26  8:49 [BUG] bpf: use-after-free in hashtab BPF_F_LOCK in-place update path Aaron Esau
2026-03-26 13:39 ` Puranjay Mohan
2026-03-26 14:58   ` Kumar Kartikeya Dwivedi
2026-03-26 15:02   ` Puranjay Mohan
2026-03-26 15:26   ` Mykyta Yatsenko
2026-03-26 15:33     ` Puranjay Mohan
2026-03-26 15:43       ` Mykyta Yatsenko
2026-03-26 15:47         ` Mykyta Yatsenko
2026-03-26 15:57           ` Puranjay Mohan
2026-03-27  2:44             ` Aaron Esau
2026-03-27  3:21               ` Kumar Kartikeya Dwivedi
2026-03-27 16:09                 ` Mykyta Yatsenko
2026-03-31  0:55                   ` Kumar Kartikeya Dwivedi
2026-03-31 11:39                     ` Mykyta Yatsenko [this message]

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=4f53d7bc-d0a9-409c-81c9-3fe255f08b98@gmail.com \
    --to=mykyta.yatsenko5@gmail.com \
    --cc=aaron1esau@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=memxor@gmail.com \
    --cc=puranjay12@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.