Netfilter-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH RFC nft] src: avoid re-initing core library when a second context struct is allocated
@ 2019-08-05 21:49 Florian Westphal
  2019-08-07 22:41 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Westphal @ 2019-08-05 21:49 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal

Calling nft_ctx_new() a second time leaks memory, and calling
nft_ctx_free a second time -- on a different context -- causes
double-free.

This patch won't work in case we assume libnftables should be
thread-safe, in such case we either need a mutex or move all resources
under nft_ctx scope.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 src/libnftables.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/libnftables.c b/src/libnftables.c
index 4a139c58b2b3..880fd4284555 100644
--- a/src/libnftables.c
+++ b/src/libnftables.c
@@ -17,6 +17,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+static unsigned int context_count;
+
 static int nft_netlink(struct nft_ctx *nft,
 		       struct list_head *cmds, struct list_head *msgs,
 		       struct mnl_socket *nf_sock)
@@ -86,6 +88,9 @@ out:
 
 static void nft_init(void)
 {
+	if (context_count++)
+		return;
+
 	mark_table_init();
 	realm_table_rt_init();
 	devgroup_table_init();
@@ -99,6 +104,9 @@ static void nft_init(void)
 
 static void nft_exit(void)
 {
+	if (--context_count)
+		return;
+
 	ct_label_table_exit();
 	realm_table_rt_exit();
 	devgroup_table_exit();
-- 
2.21.0


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

* Re: [PATCH RFC nft] src: avoid re-initing core library when a second context struct is allocated
  2019-08-05 21:49 [PATCH RFC nft] src: avoid re-initing core library when a second context struct is allocated Florian Westphal
@ 2019-08-07 22:41 ` Pablo Neira Ayuso
  2019-08-07 23:37   ` Florian Westphal
  0 siblings, 1 reply; 3+ messages in thread
From: Pablo Neira Ayuso @ 2019-08-07 22:41 UTC (permalink / raw)
  To: Florian Westphal; +Cc: netfilter-devel

On Mon, Aug 05, 2019 at 11:49:17PM +0200, Florian Westphal wrote:
> Calling nft_ctx_new() a second time leaks memory, and calling
> nft_ctx_free a second time -- on a different context -- causes
> double-free.
> 
> This patch won't work in case we assume libnftables should be
> thread-safe, in such case we either need a mutex or move all resources
> under nft_ctx scope.

These two should avoid the memleak / double free I think:

https://patchwork.ozlabs.org/patch/1143742/
https://patchwork.ozlabs.org/patch/1143743/

Not thread-safe yet, there is a bunch global variables still in place.

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

* Re: [PATCH RFC nft] src: avoid re-initing core library when a second context struct is allocated
  2019-08-07 22:41 ` Pablo Neira Ayuso
@ 2019-08-07 23:37   ` Florian Westphal
  0 siblings, 0 replies; 3+ messages in thread
From: Florian Westphal @ 2019-08-07 23:37 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Florian Westphal, netfilter-devel

Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> On Mon, Aug 05, 2019 at 11:49:17PM +0200, Florian Westphal wrote:
> > Calling nft_ctx_new() a second time leaks memory, and calling
> > nft_ctx_free a second time -- on a different context -- causes
> > double-free.
> > 
> > This patch won't work in case we assume libnftables should be
> > thread-safe, in such case we either need a mutex or move all resources
> > under nft_ctx scope.
> 
> These two should avoid the memleak / double free I think:
> 
> https://patchwork.ozlabs.org/patch/1143742/
> https://patchwork.ozlabs.org/patch/1143743/

Thanks, I will give them a try.

> Not thread-safe yet, there is a bunch global variables still in place.

I don't need thread-safety at the moment, I just found this double-free
crash when creating another nft_ctx inside nftables (don't ask why, its
fugly...)

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

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-05 21:49 [PATCH RFC nft] src: avoid re-initing core library when a second context struct is allocated Florian Westphal
2019-08-07 22:41 ` Pablo Neira Ayuso
2019-08-07 23:37   ` Florian Westphal

Netfilter-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/netfilter-devel/0 netfilter-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 netfilter-devel netfilter-devel/ https://lore.kernel.org/netfilter-devel \
		netfilter-devel@vger.kernel.org netfilter-devel@archiver.kernel.org
	public-inbox-index netfilter-devel


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.netfilter-devel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox