Article View: linux.kernel.netdev
Article #10905Re: [PATCH net] inet: frags: bound the wmem charge of the socket completing a reassembly
From: Eric Dumazet
Date: Wed, 04 Nov 2026 09:51
Date: Wed, 04 Nov 2026 09:51
137 lines
14085 bytes
14085 bytes
Le mer. 4 nov. 2026 à 10:30, <netdev-bot+sashiko@kernel.org> a écrit :
>
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue(s) to consider.
>
> Critical: 0 · High: 1 · Medium: 0 · Low: 0
>
> 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] In net/ipv4/inet_fragment.c, inet_frag_wmem_ok() (lines 533-540)
> reads sk->sk_wmem_alloc without any socket-wide lock or reservation…
>
> --
>
> inet: frags: bound the wmem charge of the socket completing a reassembly
>
> This patch adds inet_frag_wmem_ok(). The new helper refuses a reassembly
> when charging the queue's truesize to the completing socket would push
> sk_wmem_alloc above 2 * sk_sndbuf + 4MB, capped at INET_FRAG_MAX_TRUESIZE.
> The aim is to keep sk_wmem_alloc from saturating while several reassembled
> skbs owned by the same socket are alive at the same time.
>
> Closes: https://lore.kernel.org/netdev/24feda7d-ae49-45af-9596-2cc5f149f28e@redhat.com/
>
> > diff --git a/net/ipv4/inet_fragment.c b/net/ipv4/inet_fragment.c
> > index 0b8d22ffafca6..c11d7e976dfa5 100644
> > --- a/net/ipv4/inet_fragment.c
> > +++ b/net/ipv4/inet_fragment.c
>
> [ ... ]
>
> > @@ -519,6 +520,25 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
> > }
> > EXPORT_SYMBOL(inet_frag_queue_insert);
> >
> > +/* When a socket owned fragment completes a queue, the truesize of all
> > + * other fragments is charged to its sk_wmem_alloc, although they were
> > + * orphaned when queued and possibly sent by someone else.
> > + * Bound this like ip_append_data() bounds the memory a socket can
> > + * allocate (2 * sk_sndbuf), plus slack for fragments inflated by a
> > + * refragmentation using a small MTU. Otherwise, several reassembled skbs
> > + * alive at the same time can saturate sk_wmem_alloc, and leak the socket.
> > + */
> > +#define INET_FRAG_WMEM_SLACK SZ_4M
> > +
> > +static bool inet_frag_wmem_ok(const struct inet_frag_queue *q,
> > + const struct sock *sk)
> > +{
> > + u64 limit = 2ULL * READ_ONCE(sk->sk_sndbuf) + INET_FRAG_WMEM_SLACK;
> > + u64 wmem = (u64)refcount_read(&sk->sk_wmem_alloc) + q->truesize;
> > +
> > + return wmem <= min_t(u64, limit, INET_FRAG_MAX_TRUESIZE);
> > +}
>
> [Severity: High]
> Is this check safe when other reassemblies charge the same socket at the
> same time?
>
> inet_frag_wmem_ok() reads sk_wmem_alloc without a lock. The charge it
> approves is applied later, in two steps:
>
> inet_frag_reasm_prepare()
> refcount_add(ts_delta, &sk->sk_wmem_alloc);
>
> inet_frag_reasm_finish()
> /* after the loop over every fragment */
> refcount_add(sum_truesize - head_truesize, &sk->sk_wmem_alloc);
>
> The only lock held across this is the per-queue lock, for example
> spin_lock(&qp->q.lock) in ip_defrag(). The IPv6, nf_conntrack and 6lowpan
> callers follow the same pattern. That lock serializes one queue, not all
> the queues charging the same socket.
>
> Suppose several CPUs complete different queues for the same socket. Can
> they all read the same sk_wmem_alloc value W, all see
> W + q->truesize <= limit, and then all add their charge?
>
> The limit is at most INET_FRAG_MAX_TRUESIZE (INT_MAX / 2). So three such
> completions racing would seem to be enough to push sk_wmem_alloc past
> INT_MAX. That is the saturation and socket leak the commit message
> describes.
>
> One possible path from an unprivileged user/net namespace:
>
> tun_chr_open() sets tfile->sk.sk_sndbuf = INT_MAX, so the limit works out
> to min(2 * INT_MAX + 4M, INT_MAX / 2) = INT_MAX / 2.
>
> tun_build_skb() and the sock_alloc_send_pskb() path leave the skbs owned
> by the tun socket via skb_set_owner_w(), so is_skb_wmem() is true. These
> skbs are not orphaned before tc ingress, and tun_get_user() takes no
> per-write lock outside the napi_frags mode.
>
> Add clsact plus act_ct on the tun device and raise ipfrag_high_thresh.
> Then several threads writing the final fragments of different datagrams
> to the same tun fd would each reach this path on their own CPU, each
> under a different q->lock:
>
> netif_receive_skb()
> tcf_ct_handle_fragments()
> nf_ct_handle_fragments()
> ip_defrag()
> inet_frag_reasm_prepare()
> inet_frag_wmem_ok()
>
> The window between the check and the second refcount_add() covers
> skb_clone(), skb_morph(), skb_unclone() and the whole fragment loop in
> inet_frag_reasm_finish().
>
> Would this need an atomic reservation on sk_wmem_alloc instead? For
> example, add the charge first and then recheck and roll back, or use a
> cmpxchg loop.
Right, and tun sockets default to sk_sndbuf = INT_MAX, so the limit is
INT_MAX/2 there.
I will send v2 with an atomic reservation (cmpxchg) in
inet_frag_reasm_prepare().
pw-bot: cr
Technical Details
Message-ID:
<CAL4Wiiowpe6mFG2tZ3Qtqod3Rkv4V0Ed3M5V1B1Y8P1vq0kTcQ@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-356350-linux-kernel-netdev=dewy.ruReferences:
<20261103092957.4164530-1-edumazet@kernel.org> <179378464765.488856.7246331085173485104@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-356350-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net] inet: frags: bound the wmem charge of the socket
completing a reassembly
Date: Wed, 4 Nov 2026 10:51:04 +0100
Organization: Usenet Network
Message-ID: <CAL4Wiiowpe6mFG2tZ3Qtqod3Rkv4V0Ed3M5V1B1Y8P1vq0kTcQ@mail.gmail.com>
References: <20261103092957.4164530-1-edumazet@kernel.org> <179378464765.488856.7246331085173485104@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
logging-data="3365357"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:FJQmwePfWSpp6LGkLidYjDSU0d8= sha256:mLA5651fM6nDQ80Q6zhTCxNegIHckmKiyvUYTt6YYGU=
sha1:cGLl4v0iWfNm/gQ7G0dZ9kc95c4= sha256:3LKjs0mHov6UjqG6PYBhi1JM6XTI/29knmMf0GIlXM4=
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 BAD1D112B77; Wed, 4 Nov 2026 12:54:17 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, 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 mQlv1I9PkJ4a;
Wed, 4 Nov 2026 12:54:14 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-356350-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 sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id D92C7112B74; Wed, 4 Nov 2026 12:54:05 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id B3CA52832C; Wed, 4 Nov 2026 09:51:18 +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=1793785878;
b=Ho4PrnJqulYwJ6k9p6Cw56+L+l46Gm+H2eElFte0/mYbOO2J35bT8VYb9CrViIZGP043
lQma26eWsZLUlkWTV2XJDFZo19O/3C3dXEa2ukWl6LTf125ZqwOD9Oxzx2IGT2B+koFgy
6QJsHXSwb6vIemNr/xxjaOE7J9gVp2Hjfa2Se5NhTyG94fQsF/a/26ztm5r2B8YcyOByi
MKNBAI2T1bMne6SqzRdeHDUp4OsSOVMo9WMJ0ApZV//ufdjTp2q26Q6LMhrJaDc/JlBkA
dgfVNYyPpXhuwPXH9oDVodh7KIQZIkaNbK50VPx2II/7FR5grHHh1cEu8C1XwIy/H2g==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793785878;
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=oojRhALGnxSxyxmQajAogvjEsQGOsY9/BMU8rYXtxHA=;
b=e9lxcr1ZUJSaLDr51jP41OgXE3+M72/hd8v+wwAOOdUlJw1Jcl6y9PxieyMTTI97KgC+
sWxoK9Qi0raU+vOP9UoxnRdOOnpo6TmWwPuZ3RNjNNZo2htqjTRLMt6G+2on0yLneHUU3
B94+HPz38oz+5XM6Bnfm5aaqXhC8t5rej5XZOKazo40U2dTerD8CdlypYSm4XtUPCRmDf
qDwGv4rpGPxIQHx/ks1AGJzEIWLI3PI9qEGTYuW53uWYbStBp/3wC41Wd71Q52Jp0kicK
EJx3H2uGURCgkkqvp6eeTSY7GCuF4DWkjyAVpWvZNCrwMW52BFw+ZCaZZR7DCs8UTHw==
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=gNEvKN31;
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 F01A5606A339;
Wed, 4 Nov 2026 09:51:17 +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=gNEvKN31
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=1793785877;
b=M1pcJuwWsdqnz8pE9j0hZB1uMWhoP1nWM2m58dAHLrVBcs1VNIH1YOExWTucbNI9Vvpz
/++/THvDrvKdo1vT4LGiLNLLoSbbCYdBKMINoJkaMwMNFhYtUieRLmBRG0GJJg1+j2apP
gt30wKN76z/KTbhBCIGnTzUyAEl2c5sQrExLggQh2KdmoYntu709s0XvB4OHXfdXllk00
w2gpJP3sfo9cyuuoHFPdXWFm9s73xJJALGFMW3zQJjBWQ38QW0HkLjpk8zJO/49syBij9
J7qPoAuz0PEltLtTcVXqxOe3SvhtrycfMhaxzn9BfyPeA82AcpiMfle4iUpKgDn3/qA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793785877;
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=oojRhALGnxSxyxmQajAogvjEsQGOsY9/BMU8rYXtxHA=;
b=IPEk5elmlofqj4koF+BeNgrkgTjEA2VO9hUJayDnTupg3jUQ6xJ3OuzRWWnT9YDk2r3S
aTikaYlVJX0uPZARIowB+Jaw1ZuqIjnntIjdeU3vg2uYwrN2vBfV6oZVHRXRi8+5iZ5Kc
tzPFj1hXVvierV9u/xMl623HtEfYtkP6cr9U6iVZsZ8q03zElrOuegwPiBPrxLiQMpQD/
WKJw/4xCr5GyMcsncRsn0+lLJ5F0MmWRSsR2Iwls1bXqTT7AWhgzxNo7zddh/JP9MdycT
JyFXTHWbbC53W4VCicNGetTFLmWD6GPT7yCy0of1MGqCRyLUMCv2zb+Ei8USxMBJbCQ==
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=gNEvKN31;
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 CD008606A328
for <netdev@vger.kernel.org>; Wed, 4 Nov 2026 09:51:16 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 627EE1F00A15
for <netdev@vger.kernel.org>; Wed, 4 Nov 2026 09:51:16 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
s=k20260515; t=1793785876;
bh=oojRhALGnxSxyxmQajAogvjEsQGOsY9/BMU8rYXtxHA=;
h=References:In-Reply-To:From:Date:Subject:To:Cc;
b=gNEvKN31QMSPR2Y/tFdlB1WbCay38cMmkGAxG8T+kCgAVdRTnq/s1XaG5JFrLoFMX
dTXnsU5evxZJfNlQofHA/WAs67ObYLzKigwzuIoPab23Nkblc6MjjWQIwdqf8Ijd2r
6EZ43ul1VuQyR869uns+JlIIQKtgmav5C+hgEqMuFG7DccDgO+OmUzZ8TXXNVlpTU5
qjYHiUs8gUJz6zCHqLpxXot5Hf5urVAVktCUjAcdRx2s7AfW18HgdDt9P7qBrHCFW7
tO11pu9mONt39IYvgcgmFSZsroW112vi3kiJsb6ys+BLk+fuLJico+S+NBYA8NRs5e
vITiyyJqoLt/A==
X-Received: by mail-oa1-f42.google.com with SMTP id 586e51a60fabf-49e08af5fd4so229943fac.1
for <netdev@vger.kernel.org>; Wed, 04 Nov 2026 01:51:16 -0800 (PST)
X-Forwarded-Encrypted: i=1; AKwUvBwsj4FjsQBk/1y8VLMog51pZOcJPkYWSp/u/RcvMKMLswsdEGvL5k5LBQfnxyLsmfv03Use1w4=@vger.kernel.org
X-Gm-Message-State: APuU4Z+ID4qfCypdmVL1Phzc/ko8oSUg0xj6ecG/qIbMF6nATCdg8iE+
2LWRYhGcrrF0w2BfZaYV6gqaTP1nLZyVrEB1/6T04OCmjGMgVObH2PvYx1NMNL9G8gUC+vtK2L3
8kvRWUOGD9cdQT3DNXoD70+60X8oluLY=
X-Received: by 2002:a05:6871:ec4a:20b0:4a9:58d3:b60c with SMTP id
586e51a60fabf-4a958d3b7d2mr4822097fac.32.1793785875663; Wed, 04 Nov 2026
01:51:15 -0800 (PST)
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: <179378464765.488856.7246331085173485104@kernel.org>
X-Gmail-Original-Message-ID: <CAL4Wiiowpe6mFG2tZ3Qtqod3Rkv4V0Ed3M5V1B1Y8P1vq0kTcQ@mail.gmail.com>
X-Gm-Features: AclHuK_i8Q3E2HW_M_daroL4NRGLMWacYRtS_V3G7bXliFwkea254yA8nCoUAzo
X-Cc: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com,
dsahern@kernel.org, idosch@nvidia.com, horms@kernel.org,
netdev@vger.kernel.org, fw@strlen.de