Article View: linux.kernel.netdev
Article #3048Re: [PATCH] tun: fix skb length underflow in NAPI frags path
From: Willem de Bruijn
Date: Wed, 14 Oct 2026 03:59
Date: Wed, 14 Oct 2026 03:59
270 lines
16231 bytes
16231 bytes
netdev-bot+sashiko@ wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue(s) to consider.
>
> Critical: 0 · High: 1 (1) · Medium: 0 · Low: 0 (1)
>
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
>
> - [High] This fix is incomplete.
>
> Pre-existing issues:
> - [High] tun_napi_alloc_frags() builds fragments from `iter_iov(it) + i`
> and the raw `iov->iov_len` (drivers/net/tun.c:1505-1506).
> - [Low] In tun_get_user(), the `if (msg_control)` block
> (drivers/net/tun.c:1837-1849) still computes copylen/linear and walks…
>
> --
>
> tun: fix skb length underflow in NAPI frags path
>
> This patch moves the clearing of zerocopy for the IFF_NAPI_FRAGS path in
> tun_get_user() so it happens before copylen is computed. The NAPI frags
> allocation is then sized for the complete iterator instead of the zerocopy
> linear prefix. This avoids a skb->data_len underflow and a BUG() in
> __skb_pull().
>
> > diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> > index 5a302709a68aa..1124b8b33664e 100644
> > --- a/drivers/net/tun.c
> > +++ b/drivers/net/tun.c
> > @@ -1848,6 +1848,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
> > zerocopy = true;
> > }
> >
> > + /* The NAPI frags path copies the complete iterator. */
> > + if (frags)
> > + zerocopy = false;
> > +
>
> [Severity: Low]
> This isn't a bug introduced by this patch, but with this override in
> place, the msg_control block just above in tun_get_user() has no useful
> effect when frags is set:
>
> drivers/net/tun.c:tun_get_user() {
> ...
> if (msg_control) {
> struct iov_iter i = *from;
>
> /* There are 256 bytes to be copied in skb, so there is
> * enough room for skb expand head in case it is used.
> * The rest of the buffer is mapped from userspace.
> */
> copylen = min(hdr_len ? hdr_len : GOODCOPY_LEN, good_linear);
> linear = copylen;
> iov_iter_advance(&i, copylen);
> if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
> zerocopy = true;
> }
> ...
> }
>
> For frags, the !zerocopy branch overwrites copylen and linear, and the
> result of the iov_iter_npages() walk is discarded.
>
> The comment doesn't describe the frags path either. It also says 256
> bytes, but GOODCOPY_LEN is defined as 128.
>
> Would gating the block with msg_control && !frags make the code and the
> comment agree, and also skip the extra iov_iter_npages() walk?
This is nice suggestion.
>
> > if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
> > /* For the packet that is not easy to be processed
> > * (e.g gso or jumbo packet), we will do it at after
> > @@ -1868,11 +1872,6 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
> > if (frags) {
> > mutex_lock(&tfile->napi_mutex);
> > skb = tun_napi_alloc_frags(tfile, copylen, from);
>
> [Severity: High]
> Does this fully close the underflow described in the commit message?
>
> copylen is an int, but len is a size_t taken from iov_iter_count(). The
> frags path now always goes through this assignment:
>
> drivers/net/tun.c:tun_get_user() {
> ...
> if (!zerocopy) {
> copylen = len;
> linear = min(hdr_len, good_linear);
> }
> ...
> }
>
> tun_napi_alloc_frags() checks the truncated value against ETH_MAX_MTU. It
> then takes linear from the first segment, which is not truncated:
>
> drivers/net/tun.c:tun_napi_alloc_frags() {
> ...
> linear = iov_iter_single_seg_count(it);
> err = __skb_grow(skb, linear);
> if (err)
> goto free;
>
> skb->len = len;
> skb->data_len = len - linear;
> ...
> }
>
> So when len is over 4 GiB, the first segment can still be larger than the
This is not a normal length. Not sure whether it is feasible (quite
plausible), but would warrant a separate patch, not included here.
> allocation length.
>
> vhost-net appears able to deliver such an iterator to an IFF_NAPI_FRAGS
> TAP backend. init_iov_iter() adds the guest descriptor lengths into a
> size_t, and get_tx_bufs() only rejects a length of 0. The default copy
> mode then calls handle_tx_copy()->tun_sendmsg()->tun_get_user() with
> msg_control = NULL.
>
> For example, this needs a guest memslot of at least 4 GiB. The guest
> builds a chain whose first iovec is about 0xfffffff3 bytes after the
> vnet header, followed by a 272 byte iovec. That gives len = 0x100000103
> and copylen = 259:
>
> - __skb_grow() with diff = 0xfffffff3 passes a negative ntail to
> pskb_expand_head(), so it makes a small allocation instead of failing
> - skb->len = 259 and skb->data_len = 272
> - skb_copy_datagram_from_iter() and pskb_may_pull(ETH_HLEN) both succeed
> - eth_type_trans()->__skb_pull() sees skb->len < skb->data_len and
> calls BUG()
>
> Copy mode already reached copylen = len before this patch. With this
> change, the msg_control case reaches it too.
>
> Would it make sense to pass the size_t len directly to
> tun_napi_alloc_frags(), or to reject lengths that don't fit in an int
> before narrowing?
>
> [Severity: High]
> This is a pre-existing issue, but tun_napi_alloc_frags(), which this fix
> relies on, assumes the iterator is ITER_IOVEC. It also doesn't clamp the
> last fragment to the remaining count:
>
> drivers/net/tun.c:tun_napi_alloc_frags() {
> ...
> for (i = 1; i < it->nr_segs; i++) {
> const struct iovec *iov = iter_iov(it) + i;
> size_t fragsz = iov->iov_len;
> ...
> }
>
> io_uring IORING_OP_WRITE_FIXED on a tun fd reaches tun_get_user() through
If this is a bug only reachable by io_uring x napi_frags, we should
just disable the combination of these two.
napi_frags was added as a way to test insertion of packets with
specific segment layout. No need to be able to do that from io_uring.
> tun_chr_write_iter() with an ITER_BVEC iterator. io_import_fixed() sets
> the count to len, but the last bvec keeps its full bv_len:
>
> io_uring/rsrc.c:io_import_fixed() {
> ...
> nr_segs = (offset + len + bvec->bv_offset + folio_mask) >> imu->folio_shift;
> iov_iter_bvec(iter, ddir, bvec, nr_segs, len);
> ...
> }
>
> In this case iter_iov() returns the bio_vec array cast to struct iovec.
> On 64-bit little-endian, iov_len reads as bv_len | (u64)bv_offset << 32.
> For page-aligned bvecs that is just bv_len.
>
> Take a 5000 byte write from a page-aligned registered buffer. linear is
> about 4084 and data_len is about 904, but the second fragment is given a
> size of 4096. skb_copy_datagram_from_iter() writes only 904 bytes into it.
>
> Can this leave about 3192 bytes of uninitialized netdev_alloc_frag()
> memory inside the fragment? The fragment sizes would then add up to more
> than skb->data_len.
>
> If so, anything that later walks the frags by skb_frag_size() could expose
> that memory.
>
> [ ... ]
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261005054224.4154727-1-harshit.m.mogalapalli%40oracle.com
Technical Details
Message-ID:
<willemdebruijn.kernel.3587ccd7685e2@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-346006-linux-kernel-netdev=dewy.ruReferences:
<20261005054224.4154727-1-harshit.m.mogalapalli@oracle.com> <179152553052.434549.7841966987517379353@kernel.org>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-346006-linux-kernel-netdev=dewy.ru
From: willemdebruijn.kernel@gmail.com (Willem de Bruijn)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH] tun: fix skb length underflow in NAPI frags path
Date: Tue, 13 Oct 2026 23:59:10 -0400
Organization: Usenet Network
Message-ID: <willemdebruijn.kernel.3587ccd7685e2@gmail.com>
References: <20261005054224.4154727-1-harshit.m.mogalapalli@oracle.com>
<179152553052.434549.7841966987517379353@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
logging-data="2212016"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:T2wvuTbl/e4zhnI7vm6qDHU4ykE= sha256:eUj+GzNWWake0pRDFVNrmsm+1okeEklkfxOnjuQ63sM=
sha1:eQunznajPdHCrzDdPHALcg3ymSA= sha256:AS2uUPDqHJuVZwAwDYq6CCwiHpvBQNMqJtzecSPS/YE=
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 C6E1C10FFD6; Wed, 14 Oct 2026 07:02:31 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -0.746
X-Spam-Status: No, score=-0.746 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, FSL_HELO_FAKE=0.001,
HEADER_FROM_DIFFERENT_DOMAINS=0.25, MAILING_LIST_MULTI=-1,
SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001]
autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
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 2ixuozY5CUPF;
Wed, 14 Oct 2026 07:02:28 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-346006-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id B9E9110FF52; Wed, 14 Oct 2026 07:02:18 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
by sea.lore.kernel.org (Postfix) with ESMTP id 2E11D3D3F9; Wed, 14 Oct 2026 03:59:16 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 6EB2240DB4B;
Wed, 14 Oct 2026 03:59:15 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ISy/JFHa"
X-Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6C9540B364
for <netdev@vger.kernel.org>; Wed, 14 Oct 2026 03:59:13 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.177
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1791950355; cv=none; b=Jvq75JMIXRKA9LN52MZG/7cqkNSm9U54CNLBfufvFe/P+KCfmpb5efTMGyj/DowV1wA92hvgXNyLyokOEGPMjytLz6Up46+WdUcpuT7IT9Bn4j5lNPYRGye9hFFfZvy+Tgl5+qy5CPSCStqPo0FelMMqwPzOw2a47S6ZTZfXSZk=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1791950355; c=relaxed/simple;
bh=UHe31DZB0nVpQKek8RtayuPW7SrG61Ae4OAKhFVXocs=;
h=Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject:
MIME-Version:Content-Type; b=Kkst8N65LfI62LEQQictILrRBsF6oEpYcegcU8RgqD0EglF9Pi3/ofZ7HPekdQ5Xxir3ZTJSlIQyQHBGM+292dGROW8qNQbId8U3kHdgfZvx4PiZ0xSWsbeQhYUto6TcNmTUfvjbQVvBEO93oBBLcmHYyh90WzpysKH56uVh240=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ISy/JFHa; arc=none smtp.client-ip=209.85.128.177
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-8565d77c277so26105777b3.0
for <netdev@vger.kernel.org>; Tue, 13 Oct 2026 20:59:13 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1791950353; x=1792555153; darn=vger.kernel.org;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:from:to:cc
:subject:date:message-id:reply-to:content-type;
bh=WkMUe/NsYTM0ez+xe3tw/2n8+K1FB1iD4W4PefzB3ik=;
b=ISy/JFHabc3VHYlQWREj9rlzFSTwum64aJK5yMDNgPbQc3PNbTPWy32+G9zhKEkcGb
CLFsBMYuj4YF6g+XKBkFFfDngibR3YTG7VcN9w/KaQ7Xnk4QUYn1dRm/HRVnaLPwbD/s
i8awDCpRuMY0dMym4PDMB2xBTrwyaRs+rpUgY5u2iKPsjCIayIIonZiSIT516jp6b9vr
/sdbakXLLN8kkCHcKec+v438t1NTByGx66m6yXjFnA9is1wY9eNJ2Nk6gDP4IKIjCKTD
5qFvR1KMkpfjJVtu1wUv5TvITYy99ABsGyEZPJYZ/EZrH06qoEnrBJnNDYZ0INJ6ncc3
qfmg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1791950353; x=1792555153;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=WkMUe/NsYTM0ez+xe3tw/2n8+K1FB1iD4W4PefzB3ik=;
b=S8EnZ930CKY7u7xK4ScIo4sAKnrC+LRxBJR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sw0
AW67xesRixVVejvlbyPu8VHriXM7CUsk7hYAOfjy1dbGVI0aDzu0dtnpWSprUl1nr2MHuUE5EHI
EpZo2nEyw8At37iAW43cDY4oA2YPQfNNNvCcwA==
X-Received: by 2002:a05:690c:854:b0:8ab:44a4:6362 with SMTP id 00721157ae682-8b07ebf6791mr32329597b3.4.1791950352647;
Tue, 13 Oct 2026 20:59:12 -0700 (PDT)
X-Received: from gmail.com (111.46.245.35.bc.googleusercontent.com. [35.245.46.111])
by smtp.gmail.com with ESMTPSA id 00721157ae682-8b0a9d8e52csm23584347b3.11.2026.10.13.20.59.11
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Tue, 13 Oct 2026 20:59:11 -0700 (PDT)
X-Cc: maheshb@google.com,
willemdebruijn.kernel@gmail.com,
jasowangio@gmail.com,
andrew+netdev@lunn.ch,
davem@davemloft.net,
edumazet@google.com,
kuba@kernel.org,
pabeni@redhat.com,
peterpenkov96@gmail.com,
netdev@vger.kernel.org,
linux-kernel@vger.kernel.org,
kernel-janitors@vger.kernel.org,
error27@gmail.com,
stable@vger.kernel.org
X-In-Reply-To: <179152553052.434549.7841966987517379353@kernel.org>
X-Precedence: bulk
X-Mailing-List: netdev@vger.kernel.org
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>