netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][RFC] netfilter: Fix small leak in ipq_build_packet_message()
@ 2011-07-17 17:46 Jesper Juhl
  2011-07-29 14:39 ` Patrick McHardy
  0 siblings, 1 reply; 2+ messages in thread
From: Jesper Juhl @ 2011-07-17 17:46 UTC (permalink / raw)
  To: linux-kernel
  Cc: netdev, coreteam, netfilter, netfilter-devel, Hideaki YOSHIFUJI,
	James Morris, Pekka Savola (ipv6), Alexey Kuznetsov,
	David S. Miller, Patrick McHardy

ipq_build_packet_message() in net/ipv4/netfilter/ip_queue.c and
net/ipv6/netfilter/ip6_queue.c contain a small potential mem leak as
far as I can tell.

We allocate memory for 'skb' with alloc_skb() annd then call
 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));

NLMSG_PUT is a macro
 NLMSG_PUT(skb, pid, seq, type, len) \
  		NLMSG_NEW(skb, pid, seq, type, len, 0)

that expands to NLMSG_NEW, which is also a macro which expands to:
 NLMSG_NEW(skb, pid, seq, type, len, flags) \
  	({	if (unlikely(skb_tailroom(skb) < (int)NLMSG_SPACE(len))) \
  			goto nlmsg_failure; \
  		__nlmsg_put(skb, pid, seq, type, len, flags); })

If we take the true branch of the 'if' statement and 'goto
nlmsg_failure', then we'll, at that point, return from
ipq_build_packet_message() without having assigned 'skb' to anything
and we'll leak the memory we allocated for it when it goes out of
scope.

Fix this by placing a 'kfree(skb)' at 'nlmsg_failure'.

I admit that I do not know how likely this to actually happen or even
if there's something that guarantees that it will never happen - I'm
not that familiar with this code, but if that is so, I've not been
able to spot it.

Please review and commit if you believe this is correct. Thanks.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 net/ipv4/netfilter/ip_queue.c  |    1 +
 net/ipv6/netfilter/ip6_queue.c |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)

  Disclaimer: Patch is compile tested only.

diff --git a/net/ipv4/netfilter/ip_queue.c b/net/ipv4/netfilter/ip_queue.c
index 5c9b9d9..48f7d5b 100644
--- a/net/ipv4/netfilter/ip_queue.c
+++ b/net/ipv4/netfilter/ip_queue.c
@@ -218,6 +218,7 @@ ipq_build_packet_message(struct nf_queue_entry *entry, int *errp)
 	return skb;
 
 nlmsg_failure:
+	kfree_skb(skb);
 	*errp = -EINVAL;
 	printk(KERN_ERR "ip_queue: error creating packet message\n");
 	return NULL;
diff --git a/net/ipv6/netfilter/ip6_queue.c b/net/ipv6/netfilter/ip6_queue.c
index 2493948..87b243a 100644
--- a/net/ipv6/netfilter/ip6_queue.c
+++ b/net/ipv6/netfilter/ip6_queue.c
@@ -218,6 +218,7 @@ ipq_build_packet_message(struct nf_queue_entry *entry, int *errp)
 	return skb;
 
 nlmsg_failure:
+	kfree_skb(skb);
 	*errp = -EINVAL;
 	printk(KERN_ERR "ip6_queue: error creating packet message\n");
 	return NULL;
-- 
1.7.6


PS. Please CC me on replies since I'm not subscribed to all the lists 
copied on this mail.

-- 
Jesper Juhl <jj@chaosbits.net>       http://www.chaosbits.net/
Don't top-post http://www.catb.org/jargon/html/T/top-post.html
Plain text mails only, please.


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH][RFC] netfilter: Fix small leak in ipq_build_packet_message()
  2011-07-17 17:46 [PATCH][RFC] netfilter: Fix small leak in ipq_build_packet_message() Jesper Juhl
@ 2011-07-29 14:39 ` Patrick McHardy
  0 siblings, 0 replies; 2+ messages in thread
From: Patrick McHardy @ 2011-07-29 14:39 UTC (permalink / raw)
  To: Jesper Juhl
  Cc: linux-kernel, netdev, coreteam, netfilter, netfilter-devel,
	Hideaki YOSHIFUJI, James Morris, Pekka Savola (ipv6),
	Alexey Kuznetsov, David S. Miller

On 17.07.2011 19:46, Jesper Juhl wrote:
> ipq_build_packet_message() in net/ipv4/netfilter/ip_queue.c and
> net/ipv6/netfilter/ip6_queue.c contain a small potential mem leak as
> far as I can tell.
> 
> We allocate memory for 'skb' with alloc_skb() annd then call
>  nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
> 
> NLMSG_PUT is a macro
>  NLMSG_PUT(skb, pid, seq, type, len) \
>   		NLMSG_NEW(skb, pid, seq, type, len, 0)
> 
> that expands to NLMSG_NEW, which is also a macro which expands to:
>  NLMSG_NEW(skb, pid, seq, type, len, flags) \
>   	({	if (unlikely(skb_tailroom(skb) < (int)NLMSG_SPACE(len))) \
>   			goto nlmsg_failure; \
>   		__nlmsg_put(skb, pid, seq, type, len, flags); })
> 
> If we take the true branch of the 'if' statement and 'goto
> nlmsg_failure', then we'll, at that point, return from
> ipq_build_packet_message() without having assigned 'skb' to anything
> and we'll leak the memory we allocated for it when it goes out of
> scope.
> 
> Fix this by placing a 'kfree(skb)' at 'nlmsg_failure'.
> 
> I admit that I do not know how likely this to actually happen or even
> if there's something that guarantees that it will never happen - I'm
> not that familiar with this code, but if that is so, I've not been
> able to spot it.
> 
> Please review and commit if you believe this is correct. Thanks.
> 

Looks correct, applied, thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-07-29 14:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-17 17:46 [PATCH][RFC] netfilter: Fix small leak in ipq_build_packet_message() Jesper Juhl
2011-07-29 14:39 ` Patrick McHardy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).