netfilter: nf_nat_sip: validate exp->dir in nf_nat_sip_expected()
Subsystems (2)
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.
--- net/netfilter/nf_nat_sip.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nf_nat_sip.c +3 -0
@@ -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
Florian Westphal
maintainerQuoted 4 lines · Jenny Guanni Qu
nf_nat_sip_expected()usesexp->dirto index into the 2-element tuplehash[] array without bounds checking. Ifexp->dirhas 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 regionAdd a bounds check to ensure
exp->diris less thanIP_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.
Pablo Neira Ayuso
maintainerQuoted 16 lines · Florian Westphal
Quoted 4 lines · Jenny Guanni Qu
nf_nat_sip_expected()usesexp->dirto index into the 2-element tuplehash[] array without bounds checking. Ifexp->dirhas 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 regionAdd a bounds check to ensure
exp->diris less thanIP_CT_DIR_MAX.Ok, but
exp->dirisn't expected to contain crap.
Quoted 7 lines
How does
exp->dirbecome >=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.
Yes, this sounds like a duplicated bug.
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()usesexp->dirto index into the 2-element tuplehash[] array without bounds checking. Ifexp->dirhas 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 regionAdd a bounds check to ensure
exp->diris less thanIP_CT_DIR_MAX.Ok, but
exp->dirisn't expected to contain crap.Quoted 7 lines
How does
exp->dirbecome >=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.
Yes, this sounds like a duplicated bug.
Florian Westphal
maintainerQuoted 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->dirended up out of range.
Thanks for confirming, I'll drop it from pachwork.