From: Noorain Eqbal <nooraineqbal@gmail.com>
To: alexei.starovoitov@gmail.com
Cc: andrii@kernel.org, ast@kernel.org, bpf@vger.kernel.org,
daniel@iogearbox.net, david.hunter@linuxfoundation.org,
eddyz87@gmail.com, haoluo@google.com, john.fastabend@gmail.com,
jolsa@kernel.org, kpsingh@kernel.org,
linux-kernel-mentees@lists.linux.dev,
linux-kernel@vger.kernel.org, martin.lau@linux.dev,
nooraineqbal@gmail.com, sdf@fomichev.me,
skhan@linuxfoundation.org, song@kernel.org,
syzbot+2617fc732430968b45d2@syzkaller.appspotmail.com,
yonghong.song@linux.dev
Subject: Re: [PATCH] bpf: sync pending IRQ work before freeing ring buffer
Date: Mon, 20 Oct 2025 04:00:06 +0530 [thread overview]
Message-ID: <20251019223006.26252-1-nooraineqbal@gmail.com> (raw)
In-Reply-To: <CAADnVQ+pXhEsumx6NapCU0sCJw9vdB3TdLMLtCoHa7_sqCRH1A@mail.gmail.com>
On Sat, Oct 19, 2025 at 1:13 UTC, Alexei Starovoitov wrote:
> Why do you think irq_work_run_list() processes bpf ringbuf in
> the above splat?
In the syzbot reproducer, GDB shows that when bpf_ringbuf_free() is entered
the ring buffer's irq_work was still pending when the map was being freed.
(gdb) p rb->work
$5 = {
node = {llist = {next = 0xffffffff8dc055c0 <wake_up_kfence_timer_work>},
{u_flags = 35, a_flags = {counter = 35}}},
func = 0xffffffff8223ac60 <bpf_ringbuf_notify>,
irqwait = {task = 0x0}
}
Here, `u_flags = 0x23` indicates IRQ_WORK_PENDING and IRQ_WORK_BUSY
are set, which shows that irq_work for the ring buffer was still queued
at the time of free. This confirms that `irq_work_run_list()` could
process the ring buffer after memory was freed.
On Sat, Oct 19, 2025 at 1:13 UTC, Alexei Starovoitov wrote:
> Sort-of kind-of makes sense, but bpf_ringbuf_free() is called
> when no references to bpf map are left. User space and bpf progs
> are not using it anymore, so irq_work callbacks should have completed
> long ago.
You're correct that normally all irq_work callbacks should have completed
by the time bpf_ringbuf_free() is called. However, there is a small
race window. In the syzbot reproducer (https://syzkaller.appspot.com/text?tag=ReproC&x=17a24b34580000),
the BPF program is attached to sched_switch and it also writes to the
ring buffer on every context switch. Each forked child creates the
BPF program and quickly drops the last reference after bpf_ringbuf_commit()
queues an irq_work. Because the irq_work runs asynchronously, it may still
be pending when bpf_ringbuf_free() executes, thus creating a small race
window that can lead to use-after-free.
Adding `irq_work_sync(&rb->work)` ensures that all pending notifications
complete before freeing the buffer.
Thanks,
Noorain Eqbal
next prev parent reply other threads:[~2025-10-19 22:30 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-18 18:11 [PATCH] bpf: sync pending IRQ work before freeing ring buffer neqbal
2025-10-19 1:13 ` Alexei Starovoitov
2025-10-19 22:30 ` Noorain Eqbal [this message]
2025-10-20 16:17 ` Andrii Nakryiko
2025-10-20 18:03 ` [PATCH v2] " Noorain Eqbal
2025-10-21 17:10 ` patchwork-bot+netdevbpf
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=20251019223006.26252-1-nooraineqbal@gmail.com \
--to=nooraineqbal@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=david.hunter@linuxfoundation.org \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel-mentees@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=skhan@linuxfoundation.org \
--cc=song@kernel.org \
--cc=syzbot+2617fc732430968b45d2@syzkaller.appspotmail.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