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: 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 --- 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