Netdev List
 help / color / mirror / Atom feed
From: Henry Martin <bsdhenrymartin@gmail.com>
To: David Ahern <dsahern@kernel.org>,
	Ido Schimmel <idosch@nvidia.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Pablo Neira Ayuso <pablo@netfilter.org>,
	Florian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>,
	Peter Oskolkov <posk@google.com>
Cc: netdev@vger.kernel.org, netfilter-devel@vger.kernel.org,
	coreteam@netfilter.org, stable@vger.kernel.org
Subject: [PATCH net] netfilter: ipv6: fix out-of-bounds write in fragment reassembly
Date: Sat, 28 Nov 2026 22:37:09 +0800	[thread overview]
Message-ID: <20261128143710.811389-1-bsdhenrymartin@gmail.com> (raw)

nf_ct_frag6_queue() trusts the on-wire payload_len, which IPV6_HDRINCL
lets diverge from the real packet length.  A fragment whose payload_len
is smaller than its extension header chain yields end < offset.  Only
end == offset was rejected, pskb_trim_rcsum() takes the negative length
as a huge unsigned value and does not trim, and the lying fragment is
queued as is.

inet_frag_queue_insert() walks the rb-tree by offset, so a fragment with
end < offset lands on the wrong side and breaks the invariant that
rb_first() is the first fragment.  When the queue completes,
nf_ct_frag6_reasm() mistakes a small later fragment for the head and
stores the trailing nexthdr byte at
skb_network_header(skb)[fq->nhoffset].  nhoffset is controlled through
the extension header chain and can point far beyond the head buffer.

The result is a one-byte controlled out-of-bounds write past a heap
object, which corrupts neighbouring slab objects and panics the kernel,
e.g. on mainline v7.4.0-rc4 (KASAN build):

 Oops: general protection fault, probably for non-canonical address
 0xed6d696d6d6d6d6d: 0000 [#1] SMP KASAN NOPTI
 CPU: 0 UID: 1000 PID: 78 Comm: poc Tainted: G        W
 RIP: 0010:skb_release_data+0x1e7/0x640
 Call Trace:
  <IRQ>
  sk_skb_reason_drop+0xbd/0x320
  nf_ct_frag6_gather+0x13f2/0x28a0
  ipv6_defrag+0xa6/0x190
  nf_hook_slow+0xa5/0x1c0
  ipv6_rcv+0x147/0x260
 Kernel panic - not syncing: Fatal exception in interrupt

Fix the class rather than a single comparison:

 * reject a payload_len that promises more than the skb holds before it
   is used to derive end;
 * reject end <= offset right after the range check and before the
   MF/LAST_IN state machine, so a dropped fragment cannot leave
   fq->q.len and INET_FRAG_LAST_IN modified;
 * apply the same ordering to ip6_frag_queue() and ip_frag_queue(),
   which share the check and the rb-tree invariant.

end == offset was already rejected; end < offset is never a valid
fragment.

This issue was discovered by Tencent CodeBuddy Security.

Cc: stable@vger.kernel.org
Fixes: fa0f527358bd9 ("ip: use rb trees for IP frag queue.")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 net/ipv4/ip_fragment.c                  | 13 +++++++++++--
 net/ipv6/netfilter/nf_conntrack_reasm.c | 18 +++++++++++++++---
 net/ipv6/reassembly.c                   | 17 ++++++++++++++---
 3 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/net/ipv4/ip_fragment.c b/net/ipv4/ip_fragment.c
index 0688be8142c4b..3cc12e8b1786b 100644
--- a/net/ipv4/ip_fragment.c
+++ b/net/ipv4/ip_fragment.c
@@ -294,6 +294,17 @@ static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb, int *refs)
 	end = offset + skb->len - skb_network_offset(skb) - ihl;
 	err = -EINVAL;
 
+	/* A fragment must carry at least one byte after its IP header,
+	 * otherwise end < offset and the rb-tree ordering breaks.  Reject
+	 * before touching any qp state.
+	 */
+	if (end <= offset)
+		goto discard_qp;
+
+	/* tot_len must fit in the skb; IP_HDRINCL lets it diverge. */
+	if (ntohs(ip_hdr(skb)->tot_len) > skb->len - skb_network_offset(skb))
+		goto discard_qp;
+
 	/* Is this the final fragment? */
 	if ((flags & IP_MF) == 0) {
 		/* If we already have some bits beyond end
@@ -317,8 +328,6 @@ static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb, int *refs)
 			qp->q.len = end;
 		}
 	}
-	if (end == offset)
-		goto discard_qp;
 
 	err = -ENOMEM;
 	if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
diff --git a/net/ipv6/netfilter/nf_conntrack_reasm.c b/net/ipv6/netfilter/nf_conntrack_reasm.c
index f25ff2a12a824..953eb25e440c7 100644
--- a/net/ipv6/netfilter/nf_conntrack_reasm.c
+++ b/net/ipv6/netfilter/nf_conntrack_reasm.c
@@ -184,6 +184,14 @@ static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
 
 	payload_len = ntohs(ipv6_hdr(skb)->payload_len);
 
+	/* IPV6_HDRINCL allows payload_len to differ from the real packet
+	 * length.  Never let it promise more than the skb actually holds.
+	 */
+	if (payload_len + sizeof(struct ipv6hdr) > skb->len) {
+		pr_debug("payload_len exceeds skb length.\n");
+		goto err;
+	}
+
 	offset = ntohs(fhdr->frag_off) & ~0x7;
 	end = offset + (payload_len -
 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
@@ -193,6 +201,13 @@ static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
 		return -EINVAL;
 	}
 
+	/* A fragment must carry at least one byte after its fragment
+	 * header, otherwise end < offset and the rb-tree ordering breaks.
+	 * Reject before touching any fq state.
+	 */
+	if (end <= offset)
+		goto err;
+
 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
 
 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
@@ -236,9 +251,6 @@ static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
 		}
 	}
 
-	if (end == offset)
-		goto err;
-
 	/* Point into the IP datagram 'data' part. */
 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
 		pr_debug("queue: message is too short.\n");
diff --git a/net/ipv6/reassembly.c b/net/ipv6/reassembly.c
index 91a65fe673500..2f20b0f745cf5 100644
--- a/net/ipv6/reassembly.c
+++ b/net/ipv6/reassembly.c
@@ -124,6 +124,13 @@ static int ip6_frag_queue(struct net *net,
 	}
 
 	err = -EINVAL;
+
+	/* IPV6_HDRINCL allows payload_len to differ from the real packet
+	 * length.  Never let it promise more than the skb actually holds.
+	 */
+	if (ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr) > skb->len)
+		goto discard_fq;
+
 	offset = ntohs(fhdr->frag_off) & ~0x7;
 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
@@ -138,6 +145,13 @@ static int ip6_frag_queue(struct net *net,
 		return -1;
 	}
 
+	/* A fragment must carry at least one byte after its fragment
+	 * header, otherwise end < offset and the rb-tree ordering breaks.
+	 * Reject before touching any fq state.
+	 */
+	if (end <= offset)
+		goto discard_fq;
+
 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
 
 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
@@ -178,9 +192,6 @@ static int ip6_frag_queue(struct net *net,
 		}
 	}
 
-	if (end == offset)
-		goto discard_fq;
-
 	err = -ENOMEM;
 	/* Point into the IP datagram 'data' part. */
 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
-- 
2.53.0


             reply	other threads:[~2026-11-28 14:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 14:37 Henry Martin [this message]
2026-11-28 17:04 ` [PATCH net] netfilter: ipv6: fix out-of-bounds write in fragment reassembly henry martin
  -- strict thread matches above, loose matches on Subject: below --
2026-11-28 14:44 Henry Martin

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=20261128143710.811389-1-bsdhenrymartin@gmail.com \
    --to=bsdhenrymartin@gmail.com \
    --cc=coreteam@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=posk@google.com \
    --cc=stable@vger.kernel.org \
    --cc=willemb@google.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