Re: [PATCH net] bpf: tcp: fix double sock release on batch realloc

From: Eric Dumazet

Date: Tue Jul 14 2026 - 05:15:07 EST


On Tue, Jul 14, 2026 at 1:32 AM Xiang Mei (Microsoft) <xmei5@xxxxxxx> wrote:
>
> bpf_iter_tcp_batch() releases the current batch via
> bpf_iter_tcp_put_batch(), which drops the socket refs and rewrites
> each slot with the socket cookie, then grows the batch. cur_sk/end_sk
> are kept for bpf_iter_tcp_resume(), but on realloc failure the function
> returns ERR_PTR() before resume runs, leaving cur_sk < end_sk over
> slots that now hold cookies rather than sock pointers.
> bpf_iter_tcp_seq_stop() then calls bpf_iter_tcp_put_batch() again and
> dereferences a cookie as a struct sock.
>
> Empty the batch on the failure path so stop() does not release it
> again. The sockets were already freed by the first
> bpf_iter_tcp_put_batch(), so nothing leaks, and a later read() rescans
> the bucket from the start instead of skipping it. The sibling
> GFP_NOWAIT failure path still holds real socket references and is left
> for stop() to release.

Reviewed-by: Eric Dumazet <edumazet@xxxxxxxxxx>