diff mbox series

[NET] netfilter: nat: fix ICMP header corruption on ICMP errors

Message ID 20200114072548.23426-1-eyal.birger@gmail.com
State Changes Requested
Delegated to: Pablo Neira
Headers show
Series [NET] netfilter: nat: fix ICMP header corruption on ICMP errors | expand

Commit Message

Eyal Birger Jan. 14, 2020, 7:25 a.m. UTC
Commit 8303b7e8f018 ("netfilter: nat: fix spurious connection timeouts")
made nf_nat_icmp_reply_translation() use icmp_manip_pkt() as the l4
manipulation function for the outer packet on ICMP errors.

However, icmp_manip_pkt() assumes the packet is an ICMP echo packet
and therefore that the ICMP header's 'un' field is an ICMP echo id.

This is not correct for ICMP error packets, and leads to bogus bytes
being written the ICMP header, which can be wrongfully regarded as
'length' bytes by RFC 4884 compliant receivers.

Fix by assigning the 'id' field only for ICMP echo packets similar
to the treatment in ICMPv6.

Reported-by: Shmulik Ladkani <shmulik.ladkani@gmail.com>
Fixes: 8303b7e8f018 ("netfilter: nat: fix spurious connection timeouts")
Signed-off-by: Eyal Birger <eyal.birger@gmail.com>
---
 net/netfilter/nf_nat_proto.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
index 0a59c14b5177..92ef91c120f4 100644
--- a/net/netfilter/nf_nat_proto.c
+++ b/net/netfilter/nf_nat_proto.c
@@ -233,9 +233,12 @@  icmp_manip_pkt(struct sk_buff *skb,
 		return false;
 
 	hdr = (struct icmphdr *)(skb->data + hdroff);
-	inet_proto_csum_replace2(&hdr->checksum, skb,
-				 hdr->un.echo.id, tuple->src.u.icmp.id, false);
-	hdr->un.echo.id = tuple->src.u.icmp.id;
+	if (hdr->type == ICMP_ECHO || hdr->type == ICMP_ECHOREPLY) {
+		inet_proto_csum_replace2(&hdr->checksum, skb,
+					 hdr->un.echo.id, tuple->src.u.icmp.id,
+					 false);
+		hdr->un.echo.id = tuple->src.u.icmp.id;
+	}
 	return true;
 }