Skip to contentSkip to conversation

netfilter: nf_nat_sip: validate exp->dir in nf_nat_sip_expected()

PATCH In review Jenny Guanni Qu 3 participants 5 messages Mar 13 – Mar 15

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

PATCH 1/1 netfilter: nf_nat_sip: validate exp->dir in nf_nat_sip_expected()

nf_nat_sip_expected() uses exp->dir to index into the 2-element tuplehash[] array without bounds checking. If exp->dir has an out-of-range value, this causes a slab-out-of-bounds read.

KASAN reports:

  BUG: KASAN: slab-out-of-bounds in nf_nat_sip_expected+0x804/0x938
  Read of size 8 at addr ffff0000d113e3b8
  The buggy address is located 72 bytes to the right of
   allocated 240-byte region

Add a bounds check to ensure exp->dir is less than IP_CT_DIR_MAX.

Fixes 9a6648210687 ("netfilter: nf_nat: support IPv6 in SIP NAT helper")
---
 net/netfilter/nf_nat_sip.c | 3 +++
 1 file changed, 3 insertions(+)
net/netfilter/nf_nat_sip.c +3 -0
index cf4aeb299bde..48b1b2d70a1e 100644
@@ -326,6 +326,9 @@ static void nf_nat_sip_expected(struct nf_conn *ct,
 	/* This must be a fresh one. */
 	BUG_ON(ct->status & IPS_NAT_DONE_MASK);
 
+	if (exp->dir >= IP_CT_DIR_MAX)
+		return;
+
 	/* For DST manip, map port here to where it's expected. */
 	range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
 	range.min_proto = range.max_proto = exp->saved_proto;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 4 lines · Jenny Guanni Qu

nf_nat_sip_expected() uses exp->dir to index into the 2-element tuplehash[] array without bounds checking. If exp->dir has an out-of-range value, this causes a slab-out-of-bounds read.

Quoted 8 lines

KASAN reports:

  BUG: KASAN: slab-out-of-bounds in nf_nat_sip_expected+0x804/0x938
  Read of size 8 at addr ffff0000d113e3b8
  The buggy address is located 72 bytes to the right of
   allocated 240-byte region

Add a bounds check to ensure exp->dir is less than IP_CT_DIR_MAX.

Ok, but exp->dir isn't expected to contain crap.

How does exp->dir become >= IP_CT_DIR_MAX? Are you sure this isn't papering over another bug?

In particular, there is missing validation in the ctnetlink code for the dir argument.

https://lore.kernel.org/netdev/[email protected]/

#2 lore raw
Quoted 16 lines · Florian Westphal
Quoted 4 lines · Jenny Guanni Qu

nf_nat_sip_expected() uses exp->dir to index into the 2-element tuplehash[] array without bounds checking. If exp->dir has an out-of-range value, this causes a slab-out-of-bounds read.

Quoted 8 lines

KASAN reports:

  BUG: KASAN: slab-out-of-bounds in nf_nat_sip_expected+0x804/0x938
  Read of size 8 at addr ffff0000d113e3b8
  The buggy address is located 72 bytes to the right of
   allocated 240-byte region

Add a bounds check to ensure exp->dir is less than IP_CT_DIR_MAX.

Ok, but exp->dir isn't expected to contain crap.

Quoted 7 lines

How does exp->dir become >= IP_CT_DIR_MAX? Are you sure this isn't papering over another bug?

In particular, there is missing validation in the ctnetlink code for the dir argument.

https://lore.kernel.org/netdev/[email protected]/

Yes, this sounds like a duplicated bug.

author
#3 lore raw

Hi Florian, Pablo,

You're right, the root cause is the missing ctnetlink validation that Florian's patch fixes. I hit this crash while testing SIP NAT with crafted expectations via ctnetlink, which is how exp->dir ended up out of range.

Happy to drop this in favor of Florian's ctnetlink fix.

Jenny

Quoted 27 lines · Pablo Neira Ayuso
Quoted 16 lines · Florian Westphal
Quoted 4 lines · Jenny Guanni Qu

nf_nat_sip_expected() uses exp->dir to index into the 2-element tuplehash[] array without bounds checking. If exp->dir has an out-of-range value, this causes a slab-out-of-bounds read.

Quoted 8 lines

KASAN reports:

  BUG: KASAN: slab-out-of-bounds in nf_nat_sip_expected+0x804/0x938
  Read of size 8 at addr ffff0000d113e3b8
  The buggy address is located 72 bytes to the right of
   allocated 240-byte region

Add a bounds check to ensure exp->dir is less than IP_CT_DIR_MAX.

Ok, but exp->dir isn't expected to contain crap.

Quoted 7 lines

How does exp->dir become >= IP_CT_DIR_MAX? Are you sure this isn't papering over another bug?

In particular, there is missing validation in the ctnetlink code for the dir argument.

https://lore.kernel.org/netdev/[email protected]/

Yes, this sounds like a duplicated bug.

maintainer
#4 lore raw
Quoted 6 lines · Guanni Qu

Hi Florian, Pablo,

You're right, the root cause is the missing ctnetlink validation that Florian's patch fixes. I hit this crash while testing SIP NAT with crafted expectations via ctnetlink, which is how exp->dir ended up out of range.

Thanks for confirming, I'll drop it from pachwork.