Skip to contentSkip to conversation

netfilter: nf_nat_h323: fix UDP mangle offset in set_addr()

PATCH New Henry Martin 1 participant 1 message Nov 27

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nf_nat_h323: fix UDP mangle offset in set_addr()

set_addr() unconditionally adds dataoff to the embedded-address offset before mangling the packet. That is correct for TCP, where nf_nat_mangle_tcp_packet() counts match_offset from the TCP header and dataoff carries the TPKT header length. For UDP, nf_nat_mangle_udp_packet() already counts from the UDP payload, which is where the decoded H.245 data starts, so the extra dataoff shifts the write past the address. When the decoded address sits at the end of the payload, the shifted match_end runs past the skb tail and the memmove() length in mangle_contents() underflows, turning into a huge copy that runs off the mapped pages:

e.g. on mainline v7.4-rc4 (KASAN build):

 BUG: unable to handle page fault for address: ffff888140000000
 CPU: 1 UID: 1000 PID: 82 Comm: poc Tainted: G O 7.4.0-rc4 #2
 RIP: 0010:memmove+0x91/0x1c0
 RDX: fffffffffffffffe RSI: ffff88813ffffffe RDI: ffff88813ffffffe
 Call Trace:
  nf_nat_mangle_udp_packet+0x13f/0x420
  set_addr+0x18a/0x830
  nat_rtp_rtcp+0x50e/0x910
  h245_help+0x1eb/0x240
  nf_confirm+0x481/0x8c0
  raw_sendmsg+0x203d/0x2630

Only add dataoff on the TCP path.

This issue was discovered by Tencent CodeBuddy Security.

Fixes f587de0e2feb ("[NETFILTER]: nf_conntrack/nf_nat: add H.323 helper port")
---
 net/ipv4/netfilter/nf_nat_h323.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nf_nat_h323.c +8 -1
index 6bcd6734769b5..764bfc89027f6 100644
@@ -35,9 +35,16 @@ static int set_addr(struct sk_buff *skb, unsigned int protoff,
 
 	buf.ip = ip;
 	buf.port = port;
-	addroff += dataoff;
 
 	if (ip_hdr(skb)->protocol == IPPROTO_TCP) {
+		/* nf_nat_mangle_tcp_packet() counts match_offset from the
+		 * TCP header, so move the offset past the TPKT header.
+		 * nf_nat_mangle_udp_packet() already counts from the UDP
+		 * payload, where the decoded data starts; adding dataoff
+		 * there writes past the address and can underflow the
+		 * memmove() length in mangle_contents().
+		 */
+		addroff += dataoff;
 		if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
 					      protoff, addroff, sizeof(buf),
 					      (char *) &buf, sizeof(buf))) {
Signature · 1 line
2.39.5