Re: [PATCH net] net: tun: fix bugs for oversize packet when napi frags enabled

From: Eric Dumazet
Date: Sat Oct 29 2022 - 00:08:28 EST


On Fri, Oct 28, 2022 at 8:32 PM Ziyang Xuan
<william.xuanziyang@xxxxxxxxxx> wrote:
>
> Recently, we got two syzkaller problems because of oversize packet
> when napi frags enabled.
>
> One of the problems is because the first seg size of the iov_iter
> from user space is very big, it is 2147479538 which is bigger than
> the threshold value for bail out early in __alloc_pages(). And
> skb->pfmemalloc is true, __kmalloc_reserve() would use pfmemalloc
> reserves without __GFP_NOWARN flag. Thus we got a warning as following:
>
> ========================================================
>

> Restrict the packet size less than ETH_MAX_MTU to fix the problems.
> Add len check in tun_napi_alloc_frags() simply. Athough that makes
> some kinds of packets payload size slightly smaller than the length
> allowed by the protocol, for example, ETH_HLEN + sizeof(struct ipv6hdr)
> smaller when the tun device type is IFF_TAP and the packet is IPv6. But
> I think that the effect is small and can be ignored.

I am not sure about ETH_MAX_MTU being completely safe.

napi_get_frags() / napi_alloc_skb() is reserving NET_SKB_PAD +
NET_IP_ALIGN bytes.

transport_header being an offset from skb->head,
we probably want to use (ETH_MAX_MTU - NET_SKB_PAD - NET_IP_ALIGN)

My objection to your initial patch was that you were using PAGE_SIZE,
while Ethernet MTU can easily be ~9000

But 0xFFFF is a bit too much/risky.

Thanks.

>
> Fixes: 90e33d459407 ("tun: enable napi_gro_frags() for TUN/TAP driver")
> Signed-off-by: Ziyang Xuan <william.xuanziyang@xxxxxxxxxx>
> ---
> drivers/net/tun.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 27c6d235cbda..98d3160fcae2 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1459,7 +1459,7 @@ static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
> int err;
> int i;
>
> - if (it->nr_segs > MAX_SKB_FRAGS + 1)
> + if (it->nr_segs > MAX_SKB_FRAGS + 1 || len > ETH_MAX_MTU)
> return ERR_PTR(-EMSGSIZE);
>
> local_bh_disable();
> --
> 2.25.1
>