Re: memory leak in ctnetlink_start

2020-06-09 Thread Pablo Neira Ayuso
On Tue, Jun 09, 2020 at 02:58:12PM -0700, syzbot wrote:
> Hello,
> 
> syzbot found the following crash on:
> 
> HEAD commit:7ae77150 Merge tag 'powerpc-5.8-1' of git://git.kernel.org..
> git tree:   upstream
> console output: https://syzkaller.appspot.com/x/log.txt?x=128a9df210
> kernel config:  https://syzkaller.appspot.com/x/.config?x=9a1aa05456dfd557
> dashboard link: https://syzkaller.appspot.com/bug?extid=b005af2cfb0411e617de
> compiler:   gcc (GCC) 9.0.0 20181231 (experimental)
> syz repro:  https://syzkaller.appspot.com/x/repro.syz?x=17304a9610
> C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=168a9df210
> 
> IMPORTANT: if you fix the bug, please add the following tag to the commit:
> Reported-by: syzbot+b005af2cfb0411e61...@syzkaller.appspotmail.com
> 
> executing program
> executing program
> executing program
> BUG: memory leak
> unreferenced object 0x88811c797900 (size 128):
>   comm "syz-executor256", pid 6458, jiffies 4294947022 (age 13.050s)
>   hex dump (first 32 bytes):
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
>   backtrace:
> [<4276332a>] kmalloc include/linux/slab.h:555 [inline]
> [<4276332a>] kzalloc include/linux/slab.h:669 [inline]
> [<4276332a>] ctnetlink_alloc_filter+0x3a/0x2a0 
> net/netfilter/nf_conntrack_netlink.c:924
> [<0047e6fb>] ctnetlink_start+0x3a/0x80 
> net/netfilter/nf_conntrack_netlink.c:998
> [] __netlink_dump_start+0x1a3/0x2e0 
> net/netlink/af_netlink.c:2343
> [<16b073fa>] netlink_dump_start include/linux/netlink.h:246 
> [inline]
> [<16b073fa>] ctnetlink_get_conntrack+0x26d/0x2f0 
> net/netfilter/nf_conntrack_netlink.c:1611
> [] nfnetlink_rcv_msg+0x32f/0x370 
> net/netfilter/nfnetlink.c:229
> [<08feca87>] netlink_rcv_skb+0x5a/0x180 
> net/netlink/af_netlink.c:2469
> [<88caad78>] nfnetlink_rcv+0x83/0x1b0 
> net/netfilter/nfnetlink.c:563
> [<52160488>] netlink_unicast_kernel net/netlink/af_netlink.c:1303 
> [inline]
> [<52160488>] netlink_unicast+0x20a/0x2f0 
> net/netlink/af_netlink.c:1329
> [] netlink_sendmsg+0x2b5/0x560 
> net/netlink/af_netlink.c:1918
> [] sock_sendmsg_nosec net/socket.c:652 [inline]
> [] sock_sendmsg+0x4c/0x60 net/socket.c:672
> [] sys_sendmsg+0x2c4/0x2f0 net/socket.c:2352
> [<5bba2f8d>] ___sys_sendmsg+0x8a/0xd0 net/socket.c:2406
> [<60ac6563>] __sys_sendmsg+0x77/0xe0 net/socket.c:2439
> [<2f7b34b0>] do_syscall_64+0x6e/0x220 arch/x86/entry/common.c:295
> [<941009bb>] entry_SYSCALL_64_after_hwframe+0x44/0xa9

Hm, filter is not released in the error path.
diff --git a/net/netfilter/nf_conntrack_netlink.c b/net/netfilter/nf_conntrack_netlink.c
index d7bd8b1f27d5..832eabecfbdd 100644
--- a/net/netfilter/nf_conntrack_netlink.c
+++ b/net/netfilter/nf_conntrack_netlink.c
@@ -939,7 +939,8 @@ ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
 			filter->mark.mask = 0x;
 		}
 	} else if (cda[CTA_MARK_MASK]) {
-		return ERR_PTR(-EINVAL);
+		err = -EINVAL;
+		goto err_filter;
 	}
 #endif
 	if (!cda[CTA_FILTER])
@@ -947,15 +948,17 @@ ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
 
 	err = ctnetlink_parse_zone(cda[CTA_ZONE], >zone);
 	if (err < 0)
-		return ERR_PTR(err);
+		goto err_filter;
 
 	err = ctnetlink_parse_filter(cda[CTA_FILTER], filter);
 	if (err < 0)
-		return ERR_PTR(err);
+		goto err_filter;
 
 	if (filter->orig_flags) {
-		if (!cda[CTA_TUPLE_ORIG])
-			return ERR_PTR(-EINVAL);
+		if (!cda[CTA_TUPLE_ORIG]) {
+			err = -EINVAL;
+			goto err_filter;
+		}
 
 		err = ctnetlink_parse_tuple_filter(cda, >orig,
 		   CTA_TUPLE_ORIG,
@@ -963,23 +966,32 @@ ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
 		   >zone,
 		   filter->orig_flags);
 		if (err < 0)
-			return ERR_PTR(err);
+			goto err_filter;
 	}
 
 	if (filter->reply_flags) {
-		if (!cda[CTA_TUPLE_REPLY])
-			return ERR_PTR(-EINVAL);
+		if (!cda[CTA_TUPLE_REPLY]) {
+			err = -EINVAL;
+			goto err_filter;
+		}
 
 		err = ctnetlink_parse_tuple_filter(cda, >reply,
 		   CTA_TUPLE_REPLY,
 		   filter->family,
 		   >zone,
 		   filter->orig_flags);
-		if (err < 0)
-			return ERR_PTR(err);
+		if (err < 0) {
+			err = -EINVAL;
+			goto err_filter;
+		}
 	}
 
 	return filter;
+
+err_filter:
+	kfree(filter);
+
+	return ERR_PTR(err);
 }
 
 static bool ctnetlink_needs_filter(u8 family, const struct nlattr * const *cda)
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index 073aa1051d43..5792e9dcd9bc 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -6550,12 +6550,22 @@ static int nf_tables_newflowtable(struct 

memory leak in ctnetlink_start

2020-06-09 Thread syzbot
Hello,

syzbot found the following crash on:

HEAD commit:7ae77150 Merge tag 'powerpc-5.8-1' of git://git.kernel.org..
git tree:   upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=128a9df210
kernel config:  https://syzkaller.appspot.com/x/.config?x=9a1aa05456dfd557
dashboard link: https://syzkaller.appspot.com/bug?extid=b005af2cfb0411e617de
compiler:   gcc (GCC) 9.0.0 20181231 (experimental)
syz repro:  https://syzkaller.appspot.com/x/repro.syz?x=17304a9610
C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=168a9df210

IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by: syzbot+b005af2cfb0411e61...@syzkaller.appspotmail.com

executing program
executing program
executing program
BUG: memory leak
unreferenced object 0x88811c797900 (size 128):
  comm "syz-executor256", pid 6458, jiffies 4294947022 (age 13.050s)
  hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
  backtrace:
[<4276332a>] kmalloc include/linux/slab.h:555 [inline]
[<4276332a>] kzalloc include/linux/slab.h:669 [inline]
[<4276332a>] ctnetlink_alloc_filter+0x3a/0x2a0 
net/netfilter/nf_conntrack_netlink.c:924
[<0047e6fb>] ctnetlink_start+0x3a/0x80 
net/netfilter/nf_conntrack_netlink.c:998
[] __netlink_dump_start+0x1a3/0x2e0 
net/netlink/af_netlink.c:2343
[<16b073fa>] netlink_dump_start include/linux/netlink.h:246 [inline]
[<16b073fa>] ctnetlink_get_conntrack+0x26d/0x2f0 
net/netfilter/nf_conntrack_netlink.c:1611
[] nfnetlink_rcv_msg+0x32f/0x370 
net/netfilter/nfnetlink.c:229
[<08feca87>] netlink_rcv_skb+0x5a/0x180 
net/netlink/af_netlink.c:2469
[<88caad78>] nfnetlink_rcv+0x83/0x1b0 net/netfilter/nfnetlink.c:563
[<52160488>] netlink_unicast_kernel net/netlink/af_netlink.c:1303 
[inline]
[<52160488>] netlink_unicast+0x20a/0x2f0 
net/netlink/af_netlink.c:1329
[] netlink_sendmsg+0x2b5/0x560 
net/netlink/af_netlink.c:1918
[] sock_sendmsg_nosec net/socket.c:652 [inline]
[] sock_sendmsg+0x4c/0x60 net/socket.c:672
[] sys_sendmsg+0x2c4/0x2f0 net/socket.c:2352
[<5bba2f8d>] ___sys_sendmsg+0x8a/0xd0 net/socket.c:2406
[<60ac6563>] __sys_sendmsg+0x77/0xe0 net/socket.c:2439
[<2f7b34b0>] do_syscall_64+0x6e/0x220 arch/x86/entry/common.c:295
[<941009bb>] entry_SYSCALL_64_after_hwframe+0x44/0xa9



---
This bug is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkal...@googlegroups.com.

syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
syzbot can test patches for this bug, for details see:
https://goo.gl/tpsmEJ#testing-patches