a544825
From f9688b8f6755c3c2eb5c7e2e22ab168d0cb97644 Mon Sep 17 00:00:00 2001
a544825
From: Munehisa Kamata <kamatam@amazon.com>
a544825
Date: Mon, 26 Oct 2015 19:10:52 -0700
a544825
Subject: [PATCH] netfilter: nf_nat_redirect: add missing NULL pointer check
a544825
a544825
Upstream commit 94f9cd81436c85d8c3a318ba92e236ede73752fc
a544825
a544825
Commit 8b13eddfdf04cbfa561725cfc42d6868fe896f56 ("netfilter: refactor NAT
a544825
redirect IPv4 to use it from nf_tables") has introduced a trivial logic
a544825
change which can result in the following crash.
a544825
a544825
BUG: unable to handle kernel NULL pointer dereference at 0000000000000030
a544825
IP: [<ffffffffa033002d>] nf_nat_redirect_ipv4+0x2d/0xa0 [nf_nat_redirect]
a544825
PGD 3ba662067 PUD 3ba661067 PMD 0
a544825
Oops: 0000 [#1] SMP
a544825
Modules linked in: ipv6(E) xt_REDIRECT(E) nf_nat_redirect(E) xt_tcpudp(E) iptable_nat(E) nf_conntrack_ipv4(E) nf_defrag_ipv4(E) nf_nat_ipv4(E) nf_nat(E) nf_conntrack(E) ip_tables(E) x_tables(E) binfmt_misc(E) xfs(E) libcrc32c(E) evbug(E) evdev(E) psmouse(E) i2c_piix4(E) i2c_core(E) acpi_cpufreq(E) button(E) ext4(E) crc16(E) jbd2(E) mbcache(E) dm_mirror(E) dm_region_hash(E) dm_log(E) dm_mod(E)
a544825
CPU: 0 PID: 2536 Comm: ip Tainted: G            E   4.1.7-15.23.amzn1.x86_64 #1
a544825
Hardware name: Xen HVM domU, BIOS 4.2.amazon 05/06/2015
a544825
task: ffff8800eb438000 ti: ffff8803ba664000 task.ti: ffff8803ba664000
a544825
[...]
a544825
Call Trace:
a544825
 <IRQ>
a544825
 [<ffffffffa0334065>] redirect_tg4+0x15/0x20 [xt_REDIRECT]
a544825
 [<ffffffffa02e2e99>] ipt_do_table+0x2b9/0x5e1 [ip_tables]
a544825
 [<ffffffffa0328045>] iptable_nat_do_chain+0x25/0x30 [iptable_nat]
a544825
 [<ffffffffa031777d>] nf_nat_ipv4_fn+0x13d/0x1f0 [nf_nat_ipv4]
a544825
 [<ffffffffa0328020>] ? iptable_nat_ipv4_fn+0x20/0x20 [iptable_nat]
a544825
 [<ffffffffa031785e>] nf_nat_ipv4_in+0x2e/0x90 [nf_nat_ipv4]
a544825
 [<ffffffffa03280a5>] iptable_nat_ipv4_in+0x15/0x20 [iptable_nat]
a544825
 [<ffffffff81449137>] nf_iterate+0x57/0x80
a544825
 [<ffffffff814491f7>] nf_hook_slow+0x97/0x100
a544825
 [<ffffffff814504d4>] ip_rcv+0x314/0x400
a544825
a544825
unsigned int
a544825
nf_nat_redirect_ipv4(struct sk_buff *skb,
a544825
...
a544825
{
a544825
...
a544825
		rcu_read_lock();
a544825
		indev = __in_dev_get_rcu(skb->dev);
a544825
		if (indev != NULL) {
a544825
			ifa = indev->ifa_list;
a544825
			newdst = ifa->ifa_local; <---
a544825
		}
a544825
		rcu_read_unlock();
a544825
...
a544825
}
a544825
a544825
Before the commit, 'ifa' had been always checked before access. After the
a544825
commit, however, it could be accessed even if it's NULL. Interestingly,
a544825
this was once fixed in 2003.
a544825
a544825
http://marc.info/?l=netfilter-devel&m=106668497403047&w=2
a544825
a544825
In addition to the original one, we have seen the crash when packets that
a544825
need to be redirected somehow arrive on an interface which hasn't been
a544825
yet fully configured.
a544825
a544825
This change just reverts the logic to the old behavior to avoid the crash.
a544825
a544825
Fixes: 8b13eddfdf04 ("netfilter: refactor NAT redirect IPv4 to use it from nf_tables")
a544825
Signed-off-by: Munehisa Kamata <kamatam@amazon.com>
a544825
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
a544825
---
a544825
 net/netfilter/nf_nat_redirect.c | 2 +-
a544825
 1 file changed, 1 insertion(+), 1 deletion(-)
a544825
a544825
diff --git a/net/netfilter/nf_nat_redirect.c b/net/netfilter/nf_nat_redirect.c
a544825
index 97b75f9bfbcd..d43869879fcf 100644
a544825
--- a/net/netfilter/nf_nat_redirect.c
a544825
+++ b/net/netfilter/nf_nat_redirect.c
a544825
@@ -55,7 +55,7 @@ nf_nat_redirect_ipv4(struct sk_buff *skb,
a544825
 
a544825
 		rcu_read_lock();
a544825
 		indev = __in_dev_get_rcu(skb->dev);
a544825
-		if (indev != NULL) {
a544825
+		if (indev && indev->ifa_list) {
a544825
 			ifa = indev->ifa_list;
a544825
 			newdst = ifa->ifa_local;
a544825
 		}
a544825
-- 
a544825
2.5.0
a544825