Thread View: linux.kernel.netdev
1 messages
1 total messages
Started by Eric Dumazet
Tue, 03 Nov 2026 19:09
Re: [SECURITY] ipv4: heap out-of-bounds write in IP-options echo
Author: Eric Dumazet
Date: Tue, 03 Nov 2026 19:09
Date: Tue, 03 Nov 2026 19:09
384 lines
17285 bytes
17285 bytes
Le mar. 3 nov. 2026 ร 19:49, Sam Dlinn <sledge@meta.com> a รฉcrit :
>
> Hello,
>
>
> With AI assistance, we found a locally exploitable heap out-of-bounds write in the Linux IPv4 IP-options echo path and confirmed local privilege escalation and container escape. The container escape was reproduced on Ubuntu 7.0.0-39-generic.
>
>
> All source references below are against upstream stable v6.18.52, commit
>
> 8f3741e6feb045da5b406df0a80b42a1adfb289b.
>
>
> tcp_v4_save_options() in include/net/tcp.h:2651-2667 allocates the saved
>
> options using the compiled opt->optlen, then passes them to
>
> __ip_options_echo():
>
>
> const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
>
> struct ip_options_rcu *dopt = NULL;
>
>
> if (opt->optlen) {
>
> int opt_size = sizeof(*dopt) + opt->optlen;
>
>
> dopt = kmalloc(opt_size, GFP_ATOMIC);
>
> if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
>
>
> __ip_options_echo() in net/ipv4/ip_options.c:89-96 instead reads the Record
>
> Route length from the packet:
>
>
> sptr = skb_network_header(skb);
>
> dptr = dopt->__data;
>
>
> if (sopt->rr) {
>
> optlen = sptr[sopt->rr+1];
>
> soffset = sptr[sopt->rr+2];
>
> dopt->rr = dopt->optlen + sizeof(struct iphdr);
>
> memcpy(dptr, sptr+sopt->rr, optlen);
>
>
> An nftables LOCAL_IN payload rule can change the RR length byte after
>
> ip_options_compile() validates the options. The compiled opt->optlen
>
> remains unchanged, allowing the copy to exceed the allocation.
>
> Our exploit used an eight-byte Record Route option on a TCP connection request making tcp_v4_save_options() allocate 40 bytes. After ip_options_compile() validates it, an attacker-controlled nftables payload rule in the attacker's own network namespace changes the RR length byte up to 255. When __ip_options_echo() copies using the modified length, it writes attacker-selected bytes past the allocation. Duplicate SYNs repeat the operation. The echoed options in the SYNACK can be turned into an adjacent-object memory disclosure.
>
> The unchecked copy is also present at mainline commit
>
> 05441d697f7f9a927264f4058cbbc6a03dfcac9d. The demonstrated trigger is
>
> restricted by 968cc2c96390 ("netfilter: disable payload mangling in
>
> userns") and df07998dfd40 ("netfilter: nftables: restrict linklayer and
>
> network header writes").
>
>
> The proposed patch below validates the source bounds, option fields and
>
> cumulative output length, including padding. (This was written by AI, so please review it with a grain of salt)
>
>
> Best,
>
> Sam Dlinn
>
> Offensive Security Engineer @ Meta
>
>
> Proposed patch:
>
>
> ---
>
> diff --git a/net/ipv4/ip_options.c b/net/ipv4/ip_options.c
>
> --- a/net/ipv4/ip_options.c
>
> +++ b/net/ipv4/ip_options.c
>
> @@ -65,13 +65,34 @@ void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
>
> }
>
> }
>
>
>
> +static int ip_options_echo_len(const unsigned char *sptr,
>
> + const struct ip_options *sopt,
>
> + const struct ip_options *dopt,
>
> + unsigned int offset, unsigned int minlen)
>
> +{
>
> + unsigned int len = sopt->optlen;
>
> + unsigned int end = sizeof(struct iphdr) + len;
>
> + unsigned int optlen;
>
> +
>
> + if (offset < sizeof(struct iphdr) || offset > end ||
>
> + minlen > end - offset)
>
> + return -EINVAL;
>
> +
>
> + optlen = sptr[offset + 1];
>
> + if (optlen < minlen || optlen > end - offset ||
>
> + dopt->optlen > len || optlen > len - dopt->optlen)
>
> + return -EINVAL;
>
> +
>
> + return optlen;
>
> +}
>
> +
>
> /*
>
> * Provided (sopt, skb) points to received options,
>
> * build in dopt compiled option set appropriate for answering.
>
> * i.e. invert SRR option, copy anothers,
>
> * and grab room in RR/TS options.
>
> *
>
> - * NOTE: dopt cannot point to skb.
>
> + * NOTE: dopt cannot point to skb and must hold sopt->optlen option bytes.
>
> */
>
>
>
> int __ip_options_echo(struct net *net, struct ip_options *dopt,
>
> @@ -85,13 +106,23 @@ int __ip_options_echo(struct net *net, struct ip_options *dopt,
>
>
>
> if (sopt->optlen == 0)
>
> return 0;
>
> + if (sopt->optlen > MAX_IPOPTLEN || (sopt->optlen & 3))
>
> + return -EINVAL;
>
>
>
> sptr = skb_network_header(skb);
>
> + if (sptr > skb_tail_pointer(skb) ||
>
> + (size_t)(skb_tail_pointer(skb) - sptr) <
>
> + sizeof(struct iphdr) + sopt->optlen)
>
> + return -EINVAL;
>
> dptr = dopt->__data;
>
>
>
> if (sopt->rr) {
>
> - optlen = sptr[sopt->rr+1];
>
> - soffset = sptr[sopt->rr+2];
>
> + optlen = ip_options_echo_len(sptr, sopt, dopt, sopt->rr, 3);
>
> + if (optlen < 0 || sptr[sopt->rr] != IPOPT_RR)
>
> + return -EINVAL;
>
> + soffset = sptr[sopt->rr + 2];
>
> + if (soffset < IPOPT_MINOFF)
>
> + return -EINVAL;
>
> dopt->rr = dopt->optlen + sizeof(struct iphdr);
>
> memcpy(dptr, sptr+sopt->rr, optlen);
>
> if (sopt->rr_needaddr && soffset <= optlen) {
>
> @@ -104,8 +135,12 @@ int __ip_options_echo(struct net *net, struct ip_options *dopt,
>
> dopt->optlen += optlen;
>
> }
>
> if (sopt->ts) {
>
> - optlen = sptr[sopt->ts+1];
>
> - soffset = sptr[sopt->ts+2];
>
> + optlen = ip_options_echo_len(sptr, sopt, dopt, sopt->ts, 4);
>
> + if (optlen < 0 || sptr[sopt->ts] != IPOPT_TIMESTAMP)
>
> + return -EINVAL;
>
> + soffset = sptr[sopt->ts + 2];
>
> + if (soffset < 5)
>
> + return -EINVAL;
>
> dopt->ts = dopt->optlen + sizeof(struct iphdr);
>
> memcpy(dptr, sptr+sopt->ts, optlen);
>
> if (soffset <= optlen) {
>
> @@ -141,11 +176,18 @@ int __ip_options_echo(struct net *net, struct ip_options *dopt,
>
> dopt->optlen += optlen;
>
> }
>
> if (sopt->srr) {
>
> - unsigned char *start = sptr+sopt->srr;
>
> + unsigned char *start;
>
> __be32 faddr;
>
>
>
> - optlen = start[1];
>
> + optlen = ip_options_echo_len(sptr, sopt, dopt, sopt->srr, 3);
>
> + if (optlen < 0)
>
> + return -EINVAL;
>
> + start = sptr + sopt->srr;
>
> + if (start[0] != (sopt->is_strictroute ? IPOPT_SSRR : IPOPT_LSRR))
>
> + return -EINVAL;
>
> soffset = start[2];
>
> + if (soffset < IPOPT_MINOFF)
>
> + return -EINVAL;
>
> doffset = 0;
>
> if (soffset > optlen)
>
> soffset = optlen + 1;
>
> @@ -173,12 +215,16 @@ int __ip_options_echo(struct net *net, struct ip_options *dopt,
>
> }
>
> }
>
> if (sopt->cipso) {
>
> - optlen = sptr[sopt->cipso+1];
>
> + optlen = ip_options_echo_len(sptr, sopt, dopt, sopt->cipso, 2);
>
> + if (optlen < 0 || sptr[sopt->cipso] != IPOPT_CIPSO)
>
> + return -EINVAL;
>
> dopt->cipso = dopt->optlen+sizeof(struct iphdr);
>
> memcpy(dptr, sptr+sopt->cipso, optlen);
>
> dptr += optlen;
>
> dopt->optlen += optlen;
>
> }
>
> + if (ALIGN(dopt->optlen, 4) > sopt->optlen)
>
> + return -EINVAL;
>
> while (dopt->optlen & 3) {
>
> *dptr++ = IPOPT_END;
>
> dopt->optlen++;
>
>
> base-commit: 647b81ddefc6364290e67bf33c89d54971f8995e
>
Thanks for the report.
The IPv4 stack relies on the IP header and its options not being changed
after ip_options_compile() has validated them. The bug is netfilter
allowing such writes, and this has been addressed in netfilter rather
than in every consumer of IPCB(skb)->opt:
968cc2c96390 ("netfilter: disable payload mangling in userns")
54f34607d184 ("netfilter: nfnetlink_queue: restrict writes to network header")
df07998dfd40 ("netfilter: nftables: restrict linklayer and network
header writes")
112e447d17f7 ("netfilter: validate L4 headers after userspace packet writes")
With them, nft payload can no longer write past iph->daddr, nfqueue
rejects any ihl or IP options change, and neither is available from a
user namespace. Other netfilter writers (xt targets, NAT, helpers,
nft_exthdr, BPF netfilter programs) do not touch the IPv4 options.
Current upstream is therefore not affected, and I do not think we
want to add checks to __ip_options_echo() (and similar helpers like
ip_forward_options()) to cope with a header modified after validation.
These commits have no Fixes: or Cc: stable tags, which is probably why
v6.18.52 and the Ubuntu 7.0 kernel are still exploitable. Florian, could
you check whether they can be sent to stable? Distros should pick
them up as well.
Thanks.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads