Fix UDP length overflow in edge cases
Subsystems (4)
v4 cover Fix UDP length overflow in edge cases
From: Alice Mikityanska <[email protected]>
These are fixes for rare edge cases of 16-bit UDP length field overflow that might happen on netdevs with MTU >= 64k.
Exposed by the new WARN added to udp_set_len_short, reported by syzbot.
v4 changes: Fixed IPv6 commit message; deduplicated code in the selftest and fixed pylint and ruff warnings.
v3: https://lore.kernel.org/netdev/[email protected]/
v3 changes: Split IPv4, IPv6 and selftest; limited the clamp to UDP sockets to support UDP jumbograms over raw sockets; added a selftest for raw sockets; added the kernel config check to the selftest.
v2: https://lore.kernel.org/netdev/[email protected]/
v2 changes: Restored the MTU clamp in ip6_dst_mtu_maybe_forward.
v1: https://lore.kernel.org/netdev/[email protected]/
Alice Mikityanska (4):
net: ipv4: Fix UDP length overflow with PMTU discover and big MTU
net: ipv6: Fix UDP length overflow with PMTU discover and big MTU
selftests: net: Test UDP length overflow with PMTU discover and big
MTU
net: ipv6: Clamp to IP6_MAX_MTU in ip6_dst_mtu_maybe_forward
include/net/ip6_route.h | 2 + net/ipv4/ip_output.c | 1 + net/ipv6/ip6_output.c | 2 + tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/cork_fragsize.py | 140 +++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100755 tools/testing/selftests/net/cork_fragsize.py
Signature · 1 line
2.55.0
PATCH v4 1/4 net: ipv4: Fix UDP length overflow with PMTU discover and big MTU
From: Alice Mikityanska <[email protected]>
This commit bounds cork->base.fragsize to IP_MAX_MTU to avoid a possible overflow of UDP length that triggers a WARN in udp_set_len_short when setsockopt IP_MTU_DISCOVER is set to IP_PMTUDISC_PROBE, and a large packet is sent over a netdev with an unusually large MTU.
Steps to reproduce:
1. Set device MTU bigger than IP_MAX_MTU + 20. cork->base.fragsize will be set to that MTU in ip_setup_cork.
2. Set IP_MTU_DISCOVER to IP_PMTUDISC_PROBE. It lets maxnonfragsize be set to device MTU (cork->fragsize) in __ip_append_data, rather than to IP_MAX_MTU.
3. Send 65528 bytes of payload (+8 bytes of UDP header, +20 bytes of IPv4 header). Device MTU allows it (it's only one byte bigger than IP_MAX_MTU + IPv4 header, and the device MTU is bigger than that).
4. The UDP length in the built packet is 65536, which overflows the 16-bit length field and triggers the WARN in udp_set_len_short.
Note: IP_PMTUDISC_DO with IPv4 is safe, because ip_dst_mtu_maybe_forward always clamps at IP_MAX_MTU, unlike ip6_dst_mtu_maybe_forward.
The Fixes tag points at the first commit where I could reproduce the overflow with IPv4 and IP_PMTUDISC_PROBE.
--- net/ipv4/ip_output.c | 1 + 1 file changed, 1 insertion(+)
net/ipv4/ip_output.c +1 -0
@@ -1303,6 +1303,7 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, cork->fragsize =ip_sk_use_pmtu (sk) ?dst4_mtu (&rt->dst) :READ_ONCE (rt->dst.dev->mtu); + cork->fragsize =min (cork->fragsize,IP_MAX_MTU );if (!inetdev_valid_mtu (cork->fragsize))return -ENETUNREACH ;
Signature · 1 line
2.55.0
PATCH v4 2/4 net: ipv6: Fix UDP length overflow with PMTU discover and big MTU
In reply to Alice MikityanskaFrom: Alice Mikityanska <[email protected]>
This commit bounds cork->base.fragsize to IP6_MAX_MTU for UDP sockets to avoid a possible overflow of UDP length that triggers a WARN in udp_set_len_short when setsockopt IPV6_MTU_DISCOVER is set to IPV6_PMTUDISC_DO or IPV6_PMTUDISC_PROBE, and a large packet is sent over a netdev with an unusually large MTU.
Steps to reproduce (included in the new selftest):
1. Set device MTU bigger than IP6_MAX_MTU. cork->base.fragsize will be set to that MTU in ip6_setup_cork.
2. Set IPV6_MTU_DISCOVER to IPV6_PMTUDISC_PROBE or IPV6_PMTUDISC_DO. It lets maxnonfragsize be set to device MTU (cork->fragsize) in __ip6_append_data, rather than to IP6_MAX_MTU.
3. Send 65528 bytes of payload (+8 bytes of UDP header, +40 bytes of IPv6 header). Device MTU allows it (it's only one byte bigger than IP6_MAX_MTU, and the device MTU is bigger than that).
4. The UDP length in the built packet is 65536, which overflows the 16-bit length field and triggers the WARN in udp_set_len_short.
To avoid breaking sending UDP jumbograms over raw IPv6 sockets, limit the change to UDP sockets only.
The original overflow bug with IPv6 and IPV6_PMTUDISC_DO seems to predate git history (verified reproduction on 2.6.21), was fixed later, and then reappeared in commit 427faee167bc ("net: ipv6: introduce ip6_dst_mtu_maybe_forward"), which is chosen as the Fixes tag here. The overflow with IPV6_PMTUDISC_PROBE reproduces since its introduction in commit 628a5c561890 ("[INET]: Add IP(V6)_PMTUDISC_RPOBE").
--- net/ipv6/ip6_output.c | 2 ++ 1 file changed, 2 insertions(+)
net/ipv6/ip6_output.c +2 -0
@@ -1432,6 +1432,8 @@ static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,if (frag_size && frag_size < mtu) mtu = frag_size; +if (sk_is_udp (sk)) + mtu =min (mtu,IP6_MAX_MTU ); cork->base.fragsize = mtu; cork->base.gso_size = ipc6->gso_size; cork->base.tx_flags =0 ;
Signature · 1 line
2.55.0
PATCH v4 3/4 selftests: net: Test UDP length overflow with PMTU discover and big MTU
In reply to Alice MikityanskaFrom: Alice Mikityanska <[email protected]>
Two previous commits fixed overflow of UDP length when setsockopt IP(V6)_MTU_DISCOVER is set to IPV6_PMTUDISC_DO or IP(V6)_PMTUDISC_PROBE, and a large packet is sent over a netdev with an unusually large MTU.
This commit adds the selftests that replicate the described steps to reproduce for IPv6 and IPv4, and also one more test that ensures that sending UDP jumbograms over a raw socket is still possible after the fix.
--- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/cork_fragsize.py | 140 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100755 tools/testing/selftests/net/cork_fragsize.py
tools/testing/selftests/net/Makefile +1 -0
@@ -25,6 +25,7 @@ TEST_PROGS := \ cmsg_so_mark.sh \ cmsg_so_priority.sh \ cmsg_time.sh \ + cork_fragsize.py \ double_udp_encap.sh \ drop_monitor_tests.sh \ ecmp_rehash.sh \
tools/testing/selftests/net/cork_fragsize.py new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v4 4/4 net: ipv6: Clamp to IP6_MAX_MTU in ip6_dst_mtu_maybe_forward
In reply to Alice MikityanskaFrom: Alice Mikityanska <[email protected]>
Commit 427faee167bc ("net: ipv6: introduce ip6_dst_mtu_maybe_forward") dropped the IP6_MAX_MTU clamp that used to be present in ip6_mtu(). A similar IPv4 commit ac6627a28dbf ("net: ipv4: Consolidate ipv4_mtu and ip_dst_mtu_maybe_forward") preserves the IP_MAX_MTU clamp.
Restore the upper bound in the IPv6 flow to avoid potential 16-bit overflows in forwarding paths.
--- include/net/ip6_route.h | 2 ++ 1 file changed, 2 insertions(+)
include/net/ip6_route.h +2 -0
@@ -384,6 +384,8 @@ static inline unsigned int ip6_dst_mtu_maybe_forward(const struct dst_entry *dstrcu_read_unlock (); out: + mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); +return mtu -lwtunnel_headroom (dst->lwtstate, mtu); }
Signature · 1 line
2.55.0
Re: [PATCH net v4 3/4] selftests: net: Test UDP length overflow with PMTU discover and big MTU
In reply to Alice MikityanskaQuoted 6 lines · Alice Mikityanska
From: Alice Mikityanska <[email protected]>
Two previous commits fixed overflow of UDP length when setsockopt IP(V6)_MTU_DISCOVER is set to
IPV6_PMTUDISC_DOor IP(V6)_PMTUDISC_PROBE, and a large packet is sent over a netdev with an unusually large MTU.
Quoted 6 lines
This commit adds the selftests that replicate the described steps to reproduce for IPv6 and IPv4, and also one more test that ensures that sending UDP jumbograms over a raw socket is still possible after the fix.
Signed-off-by Alice Mikityanska <[email protected]>
Thanks for respinning for the code dedup between ipv4 and ipv6
Quoted 71 lines
--- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/cork_fragsize.py | 140 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100755 tools/testing/selftests/net/cork_fragsize.pytools/testing/selftests/net/Makefile +1
-0@@ -25,6 +25,7 @@ TEST_PROGS := \ cmsg_so_mark.sh \ cmsg_so_priority.sh \ cmsg_time.sh \ + cork_fragsize.py \ double_udp_encap.sh \ drop_monitor_tests.sh \ ecmp_rehash.sh \tools/testing/selftests/net/cork_fragsize.py new file +47
-0@@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +# Test possible UDP length overflow in udp_send_skb/udp_v6_send_skb. + +import errno +import gzip +import os +import socket +import struct +import subprocess +from contextlib import contextmanager + +from lib.py import KsftSkipEx, ksft_eq, ksft_raises, ksft_true +from lib.py import KsftNamedVariant, ksft_exit, ksft_pr, ksft_run, ksft_variants +from lib.py import NetNS, NetNSEnter, ip + + +IP_MTU_DISCOVER = 10 +IP_PMTUDISC_PROBE = 3 +IPV6_MTU_DISCOVER = 23 +IPV6_PMTUDISC_DO = 2 +IPV6_PMTUDISC_PROBE = 3 +IPV6_TLV_JUMBO = 194 + + +def check_kernel_config(option: str) -> bool | None: + for filename, method in [ + ('/proc/config.gz', gzip.open), + (f'/boot/config-{os.uname().release}', open), + ]: + try: + with method(filename, 'rt') as config: + for line in config: + if line.rstrip() == f'{option}=y': + return True + return False + except OSError: + continue + return None + + +def assert_debug_kernel() -> None: + res = check_kernel_config('CONFIG_DEBUG_NET') + if res is None: + ksft_pr("WARN: Can't read kernel config; assuming debug kernel, and running the test") + elif not res:
tools/testing/selftests/net/cork_fragsize.py line 57
Quoted 10 lines
tools/testing/selftests/net/cork_fragsize.py new file +10
-0+ raise KsftSkipEx('CONFIG_DEBUG_NET is not set') + + +def check_dmesg_clean(func: str) -> bool: + with subprocess.Popen(['dmesg'], stdout=subprocess.PIPE) as dmesg: + res = subprocess.run(['grep', '-q', f'WARNING:.*{func}'], stdin=dmesg.stdout, check=False) + return res.returncode != 0 and dmesg.returncode == 0 + + +@contextmanager
Interesting approach. In ksft the built-in defer() mechanism may be preferred. Not sure.
Quoted 16 lines
tools/testing/selftests/net/cork_fragsize.py new file +16
-0+def dummy_netdev(ns: NetNS, mtu: int, ipv6: bool) -> None: + try: + ip('link add dummy type dummy', ns=ns) + ip(f'link set dummy mtu {mtu}', ns=ns) + ip('link set dummy up', ns=ns) + flag = '-6' if ipv6 else '' + nodad = 'nodad' if ipv6 else '' + local = 'fd00::1/64' if ipv6 else '10.0.0.1/24' + remote = 'fd00::2' if ipv6 else '10.0.0.2' + ip(f'{flag} addr add {local} dev dummy {nodad}', ns=ns) + ip(f'{flag} neigh add {remote} lladdr 02:00:00:00:00:02 dev dummy nud permanent', ns=ns) + yield + finally: + ip('link del dummy', ns=ns) + +
Jakub Kicinski
maintainerRe: [PATCH net v4 3/4] selftests: net: Test UDP length overflow with PMTU discover and big MTU
Quoted 10 lines · On Wed, 26 Aug 2026 13:18:18 -0400 Willem de Bruijn wrote:
tools/testing/selftests/net/cork_fragsize.py line 57
Quoted 7 lines
tools/testing/selftests/net/cork_fragsize.py new file +7
-0+def check_dmesg_clean(func: str) -> bool: + with subprocess.Popen(['dmesg'], stdout=subprocess.PIPE) as dmesg: + res = subprocess.run(['grep', '-q', f'WARNING:.*{func}'], stdin=dmesg.stdout, check=False) + return res.returncode != 0 and dmesg.returncode == 0 + + +@contextmanagerInteresting approach. In ksft the built-in
defer()mechanism may be preferred. Not sure.
+1 small preference towards defer() if Alice is willing. It's be more idiomatic for our selftests
More importantly ruff check and pylink --disable=R have to be clean for new tests. The test cases need a docstring explaining what they are trying to test. The helpers can probably get a _ prefix to mark them as private and silence the warning.
Signature · 1 line
pw-bot: cr
Re: [PATCH net v4 3/4] selftests: net: Test UDP length overflow with PMTU discover and big MTU
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Wed, 26 Aug 2026 13:18:18 -0400 Willem de Bruijn wrote:
tools/testing/selftests/net/cork_fragsize.py line 57
Quoted 7 lines
tools/testing/selftests/net/cork_fragsize.py new file +7
-0+def check_dmesg_clean(func: str) -> bool: + with subprocess.Popen(['dmesg'], stdout=subprocess.PIPE) as dmesg: + res = subprocess.run(['grep', '-q', f'WARNING:.*{func}'], stdin=dmesg.stdout, check=False) + return res.returncode != 0 and dmesg.returncode == 0 + + +@contextmanagerInteresting approach. In ksft the built-in
defer()mechanism may be preferred. Not sure.
Quoted 2 lines
+1 small preference towards
defer()if Alice is willing. It's be more idiomatic for our selftests
No problem, I can switch to defer, thanks for the suggestion.
Quoted 4 lines
More importantly ruff check and pylink --disable=R have to be clean for new tests. The test cases need a docstring explaining what they are trying to test. The helpers can probably get a _ prefix to mark them as private and silence the warning.
Got it, will do. I thought it was OK, because all tests I saw didn't have docstrings.
Quoted 2 lines
Signature · 1 line
pw-bot: cr
Re: [PATCH net v4 3/4] selftests: net: Test UDP length overflow with PMTU discover and big MTU
Quoted 13 lines · Alice Mikityanska
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Wed, 26 Aug 2026 13:18:18 -0400 Willem de Bruijn wrote:
tools/testing/selftests/net/cork_fragsize.py line 57
Quoted 7 lines
tools/testing/selftests/net/cork_fragsize.py new file +7
-0+def check_dmesg_clean(func: str) -> bool: + with subprocess.Popen(['dmesg'], stdout=subprocess.PIPE) as dmesg: + res = subprocess.run(['grep', '-q', f'WARNING:.*{func}'], stdin=dmesg.stdout, check=False) + return res.returncode != 0 and dmesg.returncode == 0 + + +@contextmanagerInteresting approach. In ksft the built-in
defer()mechanism may be preferred. Not sure.
Quoted 4 lines
Quoted 2 lines
+1 small preference towards
defer()if Alice is willing. It's be more idiomatic for our selftestsNo problem, I can switch to defer, thanks for the suggestion.
I replaced try-finally with defer, but I also kept dummy_netdev() with @contextmanager, because otherwise it leads to code repetition between the two tests (even if I put ip setup into a function) and increases indent by two levels (because it would be no longer possible to compact these four with statements into one). I.e.:
with (
NetNS() as ns,
dummy_netdev(ns, 65556 + 20 * ipv6, ipv6),
NetNSEnter(ns),
socket.socket(af, socket.SOCK_DGRAM) as fd,
):vs
with NetNS() as ns:
ip('link add dummy type dummy', ns=ns) # this repeats
with defer(ip, 'link del dummy', ns=ns): # and this
ip_setup(ns, 65556 + 20 * ipv6, ipv6) with (
NetNSEnter(ns),
socket.socket(af, socket.SOCK_DGRAM) as fd,
):
I think the first one is better. And yes, I need to scope defer with with, otherwise it's scheduled after ns is deleted. I haven't found any other selftest that would do that, but I see this functionality is implemented in the defer class. Hope that works for you, respinnning :)
Quoted 10 lines
Quoted 4 lines
More importantly ruff check and pylink --disable=R have to be clean for new tests. The test cases need a docstring explaining what they are trying to test. The helpers can probably get a _ prefix to mark them as private and silence the warning.
Got it, will do. I thought it was OK, because all tests I saw didn't have docstrings.
Quoted 2 lines
Signature · 1 line
pw-bot: cr