Re: KASAN: slab-use-after-free Read in ip_finish_output

From: Florian Westphal
Date: Tue Mar 12 2024 - 09:21:28 EST


Eric Dumazet <edumazet@xxxxxxxxxx> wrote:
> > so skb->sk gets propagated down to __ip_finish_output(), long
> > after connrack defrag has called skb_orphan().
> >
> > No idea yet how to fix it,
>
> My plan was to refine "inet: frag: Always orphan skbs inside
> ip_defrag()" and only do the skb_orphan()
> for skb added to a frag_list.
>
> The head skb would keep a reference to the socket.

I tried to follow this but its beyond my abilities.

Defrag messes with skb->truesize, and I do not know how to
fix that up safely so later calls to destructor won't underflow sk
accouting.

Furthermore, depending on delivery order, the skb that gets
passed to rest of stack might not be the head skb (the one with
full l4 header and sk reference), its always the last one that arrived.

Existing code skb_morphs() this, see inet_frag_reasm_prepare() and also
the ->truesize munging (which is fine only because all skbs are
orphans...).

So in order to not pass already-released sk to inet output somehow
the skb->sk reference needs to be stolen and moved from one sk
to another.

No idea how to do this, let alone do regression testing for this.
see e.g. 48cac18ecf1de82f76259a54402c3adb7839ad01 which added
unconditional orphaning in ipv6 netfilter defrag.

ATM the only "solution" I see is to completely remove netfilter defrag
support for outgoing packets.