Article View: linux.kernel.netdev
Article #6562Re: [PATCH net 3/5] net: tun: keep TUN short linear copy hints safe
From: Eric Dumazet
Date: Thu, 22 Oct 2026 18:00
Date: Thu, 22 Oct 2026 18:00
173 lines
15849 bytes
15849 bytes
Le jeu. 22 oct. 2026 à 18:17, Jamal Hadi Salim <jhs@mojatatu.com> a écrit :
>
> On Thu, Oct 22, 2026 at 11:22 AM Willem de Bruijn
> <willemdebruijn.kernel@gmail.com> wrote:
> >
> > Jamal Hadi Salim wrote:
> > > The issue:
> > > A TUN skb can be redirected by tc mirred to an Ethernet xmit path, which
> > > calls eth_type_trans() and needs a full Ethernet header in the linear
> > > area. tun_get_user() currently validates len and the GSO header length
> > > only for IFF_TAP, so an IFF_TUN | IFF_VNET_HDR packet can set hdr_len to
> > > 1..13 and leave fewer than ETH_HLEN linear bytes. The redirected packet
> > > then BUGs in eth_type_trans().
> >
> > TUN mode does not imply Ethernet. Indeed, TUN implies an L3 packet?
>
> I have a feeling i am missing something: True, TUN is L3 but we are
> not adding or parsing eth headers to TUN.
>
> The patch only bounds the linear copy hint and receive headroom so a
> TUN skb redirected by tc to an some ethernet xmit path satisfies
> eth_type_trans()'s precondition in the patchset. IOW, with current
> code does it matter if TUN is l3? The other side will assume the
> payload to be ethernet..
I agree with Willem: TUN is not the producer here, mirred is.
TUN builds a valid L3 skb (mac_len == 0). mirred then sends it to a
device expecting a link layer header, and the IP header is parsed
as an Ethernet one. With this patch we no longer crash, but we still
transmit garbage.
Also, rejecting len < ETH_HLEN in IFF_TUN mode changes the uAPI for
a constraint that has nothing to do with TUN itself.
> If the redirect itself should be rejected for TUN, that is a different
> fix; earlier ask from Eric is to fix the producer not for example the
> driver (like loopback).
> The "reject redirect from TUN" could probably be done but i dont see a
> cheap way of doing it.
It does not need to be TUN specific, and bpf_redirect() already does it.
__bpf_redirect_common() refuses to send a packet without a link layer
header to a device expecting one, since 3a0af8fd61f9 ("bpf: BPF for
lightweight tunnel infrastructure"):
/* Verify that a link layer header is carried */
if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
kfree_skb(skb);
return -ERANGE;
}
mirred could do the same in tcf_mirred_to_dev(), after the
push/pull adjustment. Something like (untested):
if (!expects_nh &&
(unlikely(skb_network_offset(skb_to_send) < dev->min_header_len) ||
!pskb_may_pull(skb_to_send, dev->min_header_len)))
/* drop, freeing skb_to_send if it was cloned */
This is one compare in the fast path, covers TUN and any other L3
source (including a vlan pop_eth / mpls pop chain followed by mirred),
and keeps the legit case working (vlan push_eth, then mirred to an
Ethernet device).
Then this tun patch is not needed, unless there is another path
from an IFF_TUN device to eth_type_trans() that I missed.
Thanks.
>
> Thoughts?
>
> cheers,
> jamal
> > >
> > > The Fix:
> > > Reject packets shorter than ETH_HLEN in both modes. For IFF_TUN, where
> > > hdr_len is only an allocation/copy hint when GSO and checksum offload are
> > > off, raise a short nonzero hdr_len to ETH_HLEN instead of rejecting the
> > > write. Keep the TAP rejection and its NET_IP_ALIGN adjustment unchanged.
> > > This preserves valid no-offload TUN writes with hints below ETH_HLEN while
> > > guaranteeing at least ETH_HLEN linear bytes for the redirected consumer.
> > >
> > > The maximum receive headroom advertised through tun_set_headroom() must
> > > reserve the same linear minimum. A large upper-device needed_headroom
> > > (for example an L2 netkit port behind a VLAN in an OVS datapath) otherwise
> > > caps good_linear below ETH_HLEN and defeats the clamp. Reserve ETH_HLEN
> > > for TUN; the TAP branch already reserves ETH_HLEN + NET_IP_ALIGN.
> > >
> > > Conditions to recreate the bug (repro available):
> > > - create a TUN device with IFF_TUN | IFF_VNET_HDR | IFF_NO_PI;
> > > - attach ingress mirred egress redirect dev lo;
> > > - write a virtio_net_hdr with hdr_len=1 and an 8192-byte payload.
> > > Unfixed kernel will panic in eth_type_trans via loopback_xmit.
> > > In a fixed kernel the write is accepted with hdr_len clamped to ETH_HLEN and the
> > > packet is forwarded without a BUG.
> > > - no-offload acceptance: a separate check writes a 28-byte IPv4/UDP-shaped
> > > packet with hdr_len 0, 1, 13, 14 and 20; all writes are accepted.
> > > - high headroom: create an L2 netkit pair with headroom 4096, put a VLAN
> > > over it, and add both the VLAN and the TUN to one OVS datapath so the
> > > TUN receive headroom is raised to 4100. The same hdr_len=1 jumbo must
> > > keep at least ETH_HLEN linear bytes; without the setter reservation it
> > > still BUGs.
> > >
> > > Fixes: f42157cb568c ("tun: fallback if skb_alloc() fails on big packets")
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
> > > ---
> > > drivers/net/tun.c | 7 ++++++-
> > > 1 file changed, 6 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> > > index 242899f7fd07..1e235c2f0aee 100644
> > > --- a/drivers/net/tun.c
> > > +++ b/drivers/net/tun.c
> > > @@ -1193,7 +1193,7 @@ static void tun_set_headroom(struct net_device *dev, int new_hr)
> > > if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP)
> > > max_headroom -= ETH_HLEN + NET_IP_ALIGN;
> > > else
> > > - max_headroom -= 1;
> > > + max_headroom -= ETH_HLEN;
> > >
> > > tun->align = clamp_t(int, new_hr, NET_SKB_PAD, max_headroom);
> > > }
> > > @@ -1830,6 +1830,11 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
> > > align += NET_IP_ALIGN;
> > > if (unlikely(len < ETH_HLEN || (hdr_len && hdr_len < ETH_HLEN)))
> > > return -EINVAL;
> > > + } else {
> > > + if (unlikely(len < ETH_HLEN))
> > > + return -EINVAL;
> > > + if (hdr_len)
> > > + hdr_len = max_t(int, hdr_len, ETH_HLEN);
> > > }
> > >
> > > good_linear = SKB_MAX_HEAD(align);
> > > --
> > > 2.43.0
> > >
> >
> >
Technical Details
Message-ID:
<CAL4Wiir8Y03fhsevoqjvO8bH9PyGL0TWy312KOjip0SngPmVfg@mail.gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-350741-linux-kernel-netdev=dewy.ruReferences:
<ACT-N6N4.v1.r0.20261022051412@mojatatu.com> <ACT-N6N4.v1.r0.20261022051412-1@mojatatu.com> <ACT-N6N4.v1.r0.20261022051412-2@mojatatu.com> <ACT-N6N4.v1.r0.20261022051412-3@mojatatu.com> <willemdebruijn.kernel.1c05560ef167@gmail.com> <CAM0EoMmeOCQT8dRYKvfYa0EUtEsLM6EZcNCsQZSF+HfoK61Y2w@mail.gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-350741-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net 3/5] net: tun: keep TUN short linear copy hints safe
Date: Thu, 22 Oct 2026 20:00:33 +0200
Organization: Usenet Network
Message-ID: <CAL4Wiir8Y03fhsevoqjvO8bH9PyGL0TWy312KOjip0SngPmVfg@mail.gmail.com>
References: <ACT-N6N4.v1.r0.20261022051412@mojatatu.com> <ACT-N6N4.v1.r0.20261022051412-1@mojatatu.com>
<ACT-N6N4.v1.r0.20261022051412-2@mojatatu.com> <ACT-N6N4.v1.r0.20261022051412-3@mojatatu.com>
<willemdebruijn.kernel.1c05560ef167@gmail.com> <CAM0EoMmeOCQT8dRYKvfYa0EUtEsLM6EZcNCsQZSF+HfoK61Y2w@mail.gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
logging-data="285069"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:/WSgeAK5t1D+Vwbz2+h+WoFVOIA= sha256:guQTGbV1eI1q0LZ3YVjP+pakrahYroKqACPY2QanKfQ=
sha1:9m6Vy/ORS6Ni4QZBVhhWy5ewG9w= sha256:Cq0oXHU3c/a0++xS3Huhj21kJW9FlX1XHxX3tzHSEHA=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id 2DAC611132C; Thu, 22 Oct 2026 21:01:12 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.739
X-Spam-Status: No, score=-3.739 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.339, DKIM_SIGNED=0.1,
DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id vvCKPQ_R7LjZ;
Thu, 22 Oct 2026 21:01:09 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-350741-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
by mail.dewy.ru (Postfix) with ESMTPS id CC86611130A; Thu, 22 Oct 2026 21:00:59 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sin.lore.kernel.org (Postfix) with ESMTP id 9433526CC8; Thu, 22 Oct 2026 18:00:53 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1792692050;
b=prjh8TRUQs6xGmdolLxHh4L+UEDtcEQZJJRKaDwSdBNrJi3IZ3VlYsYSAVggj+QbLsBg
aLQZRmgQv0VjoCEfIe5z/2XWzksVXzJ2aGeJowgO63XCzyx9ivjZATHiSXj7FeXaAjQhv
sBvoWOmXaMPllc5zp1m/iR5WEiBuMurmlc4/yQjKeaxTc6s17gzmTq16PFSQgSNlrlCPK
j2ybMsPDuGCTaLVDiY1cWW9OAuIH5pogD8FfMW1WHdw2oVpQPnKmVjSKJOIz1JoGzTXS/
1aNTDx2ki62hYsNiu8hswio5mi1HnzC1KQMXtT1/FdeS+ba3QXRjszJbqqAWp5CRxTg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1792692050;
h=X-Original-To:Received:Received:DKIM-Signature:Received:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:X-Mailing-List:
List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
List-Owner:Precedence:MIME-Version:References:In-Reply-To:From:Date:
X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:Subject:To:Cc:
Content-Type:Content-Transfer-Encoding;
bh=OezdbdKysFqgjMHGtNKMfceio5G1Rt0ImqaD/QPQ7L4=;
b=H3s5v6JuhZSGXBiEsBHGT+VfFj0v7WO0TxUcmewjXIU76KvJYQMCeEzIy0NO4ONE7SVo
1vZzZ1XNi3wYxFlkr1zOOpXty/HLQl6se0YcDLcZisWbd7EjwkyBZX9WnTL0s3C/ghozC
fsWQfqXIFO6rw8E/k6OTG31CsR6Pw3qBGV6IELEa2jAnU/4pXQv6/UojYL8CsMg6t82vm
pXkAXl1eUMTAMumrhjnVEChCeQcEp/9SC1PmW8ICHf4rQ0vL+qQXgpfl4jAnFmtshKzVN
LdPQ23KNbsC9q4rgFfR9ZDmq33wDliEESaT5b4It+SEiI3nXqTa9JUdbPsHh6aQfNlw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GtxZxABz;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 99677606A321;
Thu, 22 Oct 2026 18:00:50 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GtxZxABz
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1792692049;
b=C3VxnKn/ASmme0yj0HiqcrX0pP1lg56JHAj2QDZSinTIccfwcS2NQRPx38JOXIn3ysZ6
irxmxHn+jQ/iR0jMoHH15rsK8lgW4HGbis2TzgNNWxsSHmYXLSeU4Vr3vFlcR0/eGa18m
4N/A0rAB9zTMX3KP0Nq9ljl5TpfadgI23NOJY0pLJiYNKtfKzlyqGR98xjNW12F9WWsyK
rP6W+/8AfILWtCs+v1dk00T4l6vUzZ2lwVqEeai+upp/o0aCHeuZ1rw8UhZgC+9nBFkiI
t9eVnbHZ054BzmdSP+TAMhsoe3HBrgWpF+UXQfgA3psmFdCoQnDv9A1qkn+V5ryyrTg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1792692049;
h=Received:DKIM-Signature:Received:X-Forwarded-Encrypted:
X-Gm-Message-State:X-Received:MIME-Version:References:In-Reply-To:
From:Date:X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:
Subject:To:Cc:Content-Type:Content-Transfer-Encoding;
bh=OezdbdKysFqgjMHGtNKMfceio5G1Rt0ImqaD/QPQ7L4=;
b=AnU4H1uBSMA/Dn2B3tiZhYUgOGiZs17rp1xxjhKVG7Orj9zA/tDGwhidH5XuPh+jMpHo
Hp/XYbu0cAfp15WjmwgkJd/+G7KWhWvwTRvtJO0J1PcJsEmkRUIyuGPa142S7RWSKCbzW
1gdTf0J3/tbcTj3Y54eiDAOSb1AamCUseN7xr6l5Z04JW6Adin2ZotIUuXzd/ivrgcukl
FRc8+T+UslatfOEC5Qw8DKfB2m5TXSBW75HlRhsKul3Mj7nYot/++GS0UnFGuNc2ECMpc
n36PQdAmfVwjfqncSkFKtgi1QDFLV7lAfktheRjmcRjiEgGfukENcCRD8jOXlSxExRg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GtxZxABz;
arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature ECDSA (prime256v1))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2312606A31A
for <netdev@vger.kernel.org>; Thu, 22 Oct 2026 18:00:48 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 950E31F031E4
for <netdev@vger.kernel.org>; Thu, 22 Oct 2026 18:00:48 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
s=k20260515; t=1792692048;
bh=OezdbdKysFqgjMHGtNKMfceio5G1Rt0ImqaD/QPQ7L4=;
h=References:In-Reply-To:From:Date:Subject:To:Cc;
b=GtxZxABzY7Qio4KKG3pdTrfVAn45+YIVlwOPYi4E5Z0jtJ6WNw4pLoPa8T8P/1GHB
j14RVnJQTZlzdzZXyVEjN/RBzuK15pX2rGJOv+5/0HnMgcOBcWSgFYrMYFBYmfFhNv
24L9dKXClr+Oh7FNtYRSD/4xnjsFUAW62GwBTO6gP7eft25Mq0jOcKasOpXcEtvdF3
kHOGxUDJL2sveO1xcU1zwzyxsOaYR7mN25mzpv7PCspgacEU2Ei6Oba6QhaZ12+eSz
Bn9m2Yn9sKRD/ehrnvobUPdtioHdDUNtuhZWdSnGFJ5f3m9a1z8NcmhPdgtu0SObtN
6VCDrnkYNwXvg==
X-Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-3ab46699a09so20384921fa.0
for <netdev@vger.kernel.org>; Thu, 22 Oct 2026 11:00:48 -0700 (PDT)
X-Forwarded-Encrypted: i=1; AKwUvByUjw3dajWVEMS/I7wIbyhfiAIM5t6GOZ8mnFaAZEFFADyqJZGvGmIJuEatz4VJFIujpP1gzkU=@vger.kernel.org
X-Gm-Message-State: AFq9FYKpNnUB4APon+0THAYtqhAQ3e95qSkMZhQIyeYTrM2baaYJiWiX
pl1uzKUnzaLTGITXDZeDFI+ROxKmX+CsNHg0MxOBnWg0JlYEu45NQhiNLVn4x1mmJ09Sm0Hm8kL
HkyjB9pmiNOBWbRAPRlAGq305oPfss3U=
X-Received: by 2002:a2e:a7ca:0:b0:3a9:9c1f:1b9e with SMTP id
38308e7fff4ca-3ac4413d5eamr17333041fa.4.1792692047249; Thu, 22 Oct 2026
11:00:47 -0700 (PDT)
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <CAM0EoMmeOCQT8dRYKvfYa0EUtEsLM6EZcNCsQZSF+HfoK61Y2w@mail.gmail.com>
X-Gmail-Original-Message-ID: <CAL4Wiir8Y03fhsevoqjvO8bH9PyGL0TWy312KOjip0SngPmVfg@mail.gmail.com>
X-Gm-Features: AclHuK9tGCezuKXL3ZQaoeKiwEcpj7XQlNue34RgbxGGna_VvvqlguiKkWN0igM
X-Cc: Willem de Bruijn <willemdebruijn.kernel@gmail.com>, netdev@vger.kernel.org,
Jason Wang <jasowangio@gmail.com>, Andrew Lunn <andrew+netdev@lunn.ch>,
"David S . Miller" <davem@davemloft.net>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Rusty Russell <rusty@rustcorp.com.au>, Max Krasnyansky <maxk@qualcomm.com>, Jiri Pirko <jiri@resnulli.us>,
Victor Nogueira <victor@mojatatu.com>, stable@vger.kernel.org