netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Subject: [nft PATCH] libnftables: Fix for multiple context instances
Date: Thu, 16 Nov 2017 20:10:24 +0100	[thread overview]
Message-ID: <20171116191024.18908-1-phil@nwl.cc> (raw)

If a second context is created, the second call to nft_ctx_free() leads
to freeing invalid pointers in nft_exit(). Fix this by introducing a
reference counter so that neither nft_init() nor nft_exit() run more
than once in a row.

This reference counter has to be protected from parallel access. Do this
using a mutex in a way that once nft_init() returns, the first call to
that function running in parallel is guaranteed to be finished -
otherwise it could happen that things being initialized in one thread
are already accessed in another one.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/libnftables.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/libnftables.c b/src/libnftables.c
index e8fa6742f7d17..9df9658930c39 100644
--- a/src/libnftables.c
+++ b/src/libnftables.c
@@ -14,6 +14,7 @@
 #include <iface.h>
 
 #include <errno.h>
+#include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -102,8 +103,16 @@ err1:
 	return ret;
 }
 
+static int nft_refcnt;
+static pthread_mutex_t nft_refcnt_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 static void nft_init(void)
 {
+	pthread_mutex_lock(&nft_refcnt_mutex);
+
+	if (nft_refcnt++)
+		goto unlock;
+
 	mark_table_init();
 	realm_table_rt_init();
 	devgroup_table_init();
@@ -113,15 +122,26 @@ static void nft_init(void)
 #ifdef HAVE_LIBXTABLES
 	xt_init();
 #endif
+
+unlock:
+	pthread_mutex_unlock(&nft_refcnt_mutex);
 }
 
 static void nft_exit(void)
 {
+	pthread_mutex_lock(&nft_refcnt_mutex);
+
+	if (--nft_refcnt)
+		goto unlock;
+
 	ct_label_table_exit();
 	realm_table_rt_exit();
 	devgroup_table_exit();
 	realm_table_meta_exit();
 	mark_table_exit();
+
+unlock:
+	pthread_mutex_unlock(&nft_refcnt_mutex);
 }
 
 int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path)
-- 
2.13.1


             reply	other threads:[~2017-11-16 19:10 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-16 19:10 Phil Sutter [this message]
2017-11-20 12:37 ` [nft PATCH] libnftables: Fix for multiple context instances Pablo Neira Ayuso
2017-11-20 12:54   ` Phil Sutter
2017-11-20 13:07     ` Pablo Neira Ayuso
2017-11-20 15:58       ` Phil Sutter
2017-11-20 16:53         ` Pablo Neira Ayuso
2017-11-22 17:49           ` Phil Sutter
2017-11-22 18:18             ` Pablo Neira Ayuso
     [not found]             ` <20171204100955.GA1822@salvia>
     [not found]               ` <20171204105324.GX32305@orbyte.nwl.cc>
     [not found]                 ` <20171204110142.GA19776@salvia>
     [not found]                   ` <20171204164327.GA32305@orbyte.nwl.cc>
     [not found]                     ` <20171204184604.GA1556@salvia>
2017-12-05 13:43                       ` libnftables extended API proposal (Was: Re: [nft PATCH] libnftables: Fix for multiple context instances) Phil Sutter
2017-12-07  0:05                         ` Pablo Neira Ayuso
2017-12-07 11:34                           ` Phil Sutter
2017-12-10 21:55                             ` Pablo Neira Ayuso
2017-12-16 16:06                               ` libnftables extended API proposal Phil Sutter
2017-12-18 23:00                                 ` Pablo Neira Ayuso
2017-12-20 12:32                                   ` Phil Sutter
2017-12-20 22:23                                     ` Pablo Neira Ayuso
2017-12-22 13:08                                       ` Phil Sutter
2017-12-22 13:49                                         ` Pablo Neira Ayuso
2017-12-22 15:30                                           ` Phil Sutter
2017-12-22 20:39                                             ` Pablo Neira Ayuso
2017-12-23 13:19                                               ` Phil Sutter
2017-12-28 19:21                                                 ` Pablo Neira Ayuso
2017-12-29 14:58                                                   ` Phil Sutter
2018-01-02 18:02                                                     ` Pablo Neira Ayuso
2018-01-05 17:52                                                       ` Phil Sutter
2018-01-09 23:31                                                         ` Pablo Neira Ayuso
2018-01-10  4:46                                                           ` mark diener
2018-01-10 10:39                                                             ` Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171116191024.18908-1-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).