Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: janislacis06@gmail.com
Cc: netdev@vger.kernel.org, willemdebruijn.kernel@gmail.com,
	jasowangio@gmail.com, andrew+netdev@lunn.ch, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	peterpenkov96@gmail.com, maheshb@google.com
Subject: Re: [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode
Date: Mon, 30 Nov 2026 17:15:08 +0000	[thread overview]
Message-ID: <179605890863.3146.9327234366805987306@kernel.org> (raw)
In-Reply-To: <20261129171337.815175-1-janislacis06@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 (1) · Medium: 1 · Low: 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).

- [Medium] The new early return `if (!user_backed_iter(it)) return
  ERR_PTR(-EOPNOTSUPP);` in tun_napi_alloc_frags() runs before…
- [Low] The commit message understates what the bug does.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same 'skb
  lengths do not match the fragment layout' bug stays reachable with a…

--

tun: reject iterators that are not user backed in NAPI frags mode

This commit adds a user_backed_iter() check at the top of
tun_napi_alloc_frags(). Iterators that are not user backed, such as the
ITER_BVEC used by io_uring fixed-buffer writes, are now rejected with
-EOPNOTSUPP. Before this, the fragment loop read bio_vec entries as if
they were iovec entries.

Link: https://lore.kernel.org/netdev/willemdebruijn.kernel.3587ccd7685e2@gmail.com/

> This can result in an SKB where the sum of the fragment sizes does
> not match skb->data_len. This was reproduced using an io_uring
> fixed-buffer write to a TAP device, see the reproducer code below.

[Severity: Low]
The code change itself is fine, but does this description understate
what the bug does?

In tun_napi_alloc_frags(), bvec[i] is read as a struct iovec. On 64-bit
little-endian, iov_len overlays bv_len | (bv_offset << 32), so fragsz is
4096 when bv_offset is 0:

    const struct iovec *iov = iter_iov(it) + i;
    size_t fragsz = iov->iov_len;
    ...
    frag = netdev_alloc_frag(fragsz);

netdev_alloc_frag() does not zero the memory. Later, tun_get_user() calls:

    err = skb_copy_datagram_from_iter(skb, 0, from, len);

This fills only data_len bytes of the frags and returns 0 once len is
used up. The skb is then passed to napi_gro_frags().

With the reproducer output (len=5042, data_len=946, frag_bytes=4096),
3150 bytes of stale page-frag memory sit inside a frag labelled 4096
bytes.

Could anything that walks frags by skb_frag_size() read or transmit that
memory? Two examples are an SG-capable NIC driver after bridging or
forwarding, and skb_segment().

If so, could the commit message mention the possible kernel memory
disclosure, so stable and security triage can weigh it properly?

> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index ae05b1e58abaa..864945f2fac20 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1502,6 +1502,9 @@ static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
>  	int err;
>  	int i;
>  
> +	if (!user_backed_iter(it))
> +		return ERR_PTR(-EOPNOTSUPP);
> +

[Severity: Medium]
Can this early return leak a recycled skb cached in tfile->napi.skb?

The return happens before napi_get_frags(), and before the free label
that calls napi_free_frags().

tfile->napi.skb can still hold an skb from an earlier write. That
happens when napi_gro_frags() returns GRO_MERGED_FREE:

net/core/gro.c:napi_frags_finish() {
    ...
    case GRO_MERGED_FREE:
        if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
            napi_skb_free_stolen_head(skb);
        else
            napi_reuse_skb(napi, skb);
    ...
}

napi_reuse_skb() ends with napi->skb = skb, and tun_get_user() then
just unlocks napi_mutex.

Now suppose the next write is an io_uring IORING_OP_WRITE_FIXED
(ITER_BVEC) to the same IFF_NAPI_FRAGS device. tun_get_user() jumps to
drop with an ERR_PTR skb:

drivers/net/tun.c:tun_get_user() {
    ...
free_skb:
    drop_reason = drop_reason ?: SKB_DROP_REASON_NOT_SPECIFIED;
    if (!IS_ERR_OR_NULL(skb))
        kfree_skb_reason(skb, drop_reason);

unlock_frags:
    if (frags) {
        tfile->napi.skb = NULL;
        mutex_unlock(&tfile->napi_mutex);
    }
    ...
}

The ERR_PTR skb is not freed. Then tfile->napi.skb = NULL drops the
only reference to the recycled sk_buff and its head buffer.
napi_free_frags() at NAPI teardown cannot reclaim it either, because
napi->skb is already NULL.

A write that GRO merges followed by a fixed-buffer write would then leak
one skb per pair, with no limit. Before this patch, the bvec write
reused the cached skb instead.

The existing -EMSGSIZE early return just below has the same flaw.
However, this check adds an easy way to trigger it for a whole class of
iterators.

Would it be better to do the user_backed_iter() check in tun_get_user()
before taking napi_mutex? Another option would be to clear
tfile->napi.skb in unlock_frags only when it was actually used or freed.

>  	if (it->nr_segs > MAX_SKB_FRAGS + 1 ||
>  	    len > (ETH_MAX_MTU - NET_SKB_PAD - NET_IP_ALIGN))
>  		return ERR_PTR(-EMSGSIZE);

[Severity: High]
This is a pre-existing issue and was not introduced by this patch. Can
the same mismatch between skb lengths and frag layout still be reached
with a user-backed ITER_IOVEC iterator? The new check lets those
through.

When tun_get_user() is called with msg_control set (the vhost-net
zerocopy TX path, TUN_MSG_UBUF), zerocopy is true and copylen stays
short:

    copylen = min(hdr_len ? hdr_len : GOODCOPY_LEN, good_linear);
    ...
    if (frags) {
        mutex_lock(&tfile->napi_mutex);
        skb = tun_napi_alloc_frags(tfile, copylen, from);
        ...
        zerocopy = false;
    ...
    err = skb_copy_datagram_from_iter(skb, 0, from, len);

Inside tun_napi_alloc_frags(), linear is the length of the first
segment, which the guest controls:

    linear = iov_iter_single_seg_count(it);
    err = __skb_grow(skb, linear);
    ...
    skb->len = len;
    skb->data_len = len - linear;
    skb->truesize += skb->data_len;

Suppose the first segment is longer than copylen, for example 1500 vs
128. Then skb->data_len wraps to roughly 0xFFFFFAA4 and truesize wraps
as well, while skb->len is 128.

Would the following eth_type_trans()->skb_pull_inline()->__skb_pull()
then see skb->len < skb->data_len and hit BUG()?

If the first segment is shorter than copylen, the frag sizes exceed
data_len instead. That is the symptom this commit describes.

It looks like the cause here is the length argument passed in, not the
iterator type.

Reaching this needs an IFF_NAPI_FRAGS tap used as a vhost-net backend
with experimental_zcopytx=1. Once that is set up, the guest's TX
descriptor layout decides whether it triggers.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129171337.815175-1-janislacis06%40gmail.com

      reply	other threads:[~2026-11-30 17:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 17:13 [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode Janis Edvarts Lacis
2026-11-30 17:15 ` netdev-bot+sashiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179605890863.3146.9327234366805987306@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=janislacis06@gmail.com \
    --cc=jasowangio@gmail.com \
    --cc=kuba@kernel.org \
    --cc=maheshb@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=peterpenkov96@gmail.com \
    --cc=willemdebruijn.kernel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox