netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert
@ 2018-02-05 22:41 Cong Wang
  2018-02-05 22:55 ` Florian Westphal
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Cong Wang @ 2018-02-05 22:41 UTC (permalink / raw)
  To: netdev; +Cc: netfilter-devel, Cong Wang, Pablo Neira Ayuso, Eric Dumazet

rateest_hash is supposed to be protected by xt_rateest_mutex,
and, as suggested by Eric, lookup and insert should be atomic,
so we should acquire the xt_rateest_mutex once for both.

So introduce a non-locking helper for internal use and keep the
locking one for external.

Reported-by: <syzbot+5cb189720978275e4c75@syzkaller.appspotmail.com>
Fixes: 5859034d7eb8 ("[NETFILTER]: x_tables: add RATEEST target")
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
---
 net/netfilter/xt_RATEEST.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/net/netfilter/xt_RATEEST.c b/net/netfilter/xt_RATEEST.c
index 498b54fd04d7..141c295191f6 100644
--- a/net/netfilter/xt_RATEEST.c
+++ b/net/netfilter/xt_RATEEST.c
@@ -39,23 +39,31 @@ static void xt_rateest_hash_insert(struct xt_rateest *est)
 	hlist_add_head(&est->list, &rateest_hash[h]);
 }
 
-struct xt_rateest *xt_rateest_lookup(const char *name)
+static struct xt_rateest *__xt_rateest_lookup(const char *name)
 {
 	struct xt_rateest *est;
 	unsigned int h;
 
 	h = xt_rateest_hash(name);
-	mutex_lock(&xt_rateest_mutex);
 	hlist_for_each_entry(est, &rateest_hash[h], list) {
 		if (strcmp(est->name, name) == 0) {
 			est->refcnt++;
-			mutex_unlock(&xt_rateest_mutex);
 			return est;
 		}
 	}
-	mutex_unlock(&xt_rateest_mutex);
+
 	return NULL;
 }
+
+struct xt_rateest *xt_rateest_lookup(const char *name)
+{
+	struct xt_rateest *est;
+
+	mutex_lock(&xt_rateest_mutex);
+	est = __xt_rateest_lookup(name);
+	mutex_unlock(&xt_rateest_mutex);
+	return est;
+}
 EXPORT_SYMBOL_GPL(xt_rateest_lookup);
 
 void xt_rateest_put(struct xt_rateest *est)
@@ -100,8 +108,10 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
 
 	net_get_random_once(&jhash_rnd, sizeof(jhash_rnd));
 
-	est = xt_rateest_lookup(info->name);
+	mutex_lock(&xt_rateest_mutex);
+	est = __xt_rateest_lookup(info->name);
 	if (est) {
+		mutex_unlock(&xt_rateest_mutex);
 		/*
 		 * If estimator parameters are specified, they must match the
 		 * existing estimator.
@@ -139,11 +149,13 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
 
 	info->est = est;
 	xt_rateest_hash_insert(est);
+	mutex_unlock(&xt_rateest_mutex);
 	return 0;
 
 err2:
 	kfree(est);
 err1:
+	mutex_unlock(&xt_rateest_mutex);
 	return ret;
 }
 
-- 
2.13.0

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

* Re: [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert
  2018-02-05 22:41 [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert Cong Wang
@ 2018-02-05 22:55 ` Florian Westphal
  2018-02-05 23:25 ` Eric Dumazet
  2018-02-06 12:26 ` Pablo Neira Ayuso
  2 siblings, 0 replies; 4+ messages in thread
From: Florian Westphal @ 2018-02-05 22:55 UTC (permalink / raw)
  To: Cong Wang; +Cc: netdev, netfilter-devel, Pablo Neira Ayuso, Eric Dumazet

Cong Wang <xiyou.wangcong@gmail.com> wrote:
> rateest_hash is supposed to be protected by xt_rateest_mutex,
> and, as suggested by Eric, lookup and insert should be atomic,
> so we should acquire the xt_rateest_mutex once for both.
> 
> So introduce a non-locking helper for internal use and keep the
> locking one for external.

Looks good, thanks Cong.

Reviewed-by: Florian Westphal <fw@strlen.de>

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

* Re: [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert
  2018-02-05 22:41 [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert Cong Wang
  2018-02-05 22:55 ` Florian Westphal
@ 2018-02-05 23:25 ` Eric Dumazet
  2018-02-06 12:26 ` Pablo Neira Ayuso
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Dumazet @ 2018-02-05 23:25 UTC (permalink / raw)
  To: Cong Wang, netdev; +Cc: netfilter-devel, Pablo Neira Ayuso

On Mon, 2018-02-05 at 14:41 -0800, Cong Wang wrote:
> rateest_hash is supposed to be protected by xt_rateest_mutex,
> and, as suggested by Eric, lookup and insert should be atomic,
> so we should acquire the xt_rateest_mutex once for both.
> 
> So introduce a non-locking helper for internal use and keep the
> locking one for external.
> 
> Reported-by: <syzbot+5cb189720978275e4c75@syzkaller.appspotmail.com>
> Fixes: 5859034d7eb8 ("[NETFILTER]: x_tables: add RATEEST target")
> Cc: Pablo Neira Ayuso <pablo@netfilter.org>
> Cc: Eric Dumazet <eric.dumazet@gmail.com>
> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
> ---

Reviewed-by: Eric Dumazet <edumazet@google.com>

Thanks !


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

* Re: [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert
  2018-02-05 22:41 [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert Cong Wang
  2018-02-05 22:55 ` Florian Westphal
  2018-02-05 23:25 ` Eric Dumazet
@ 2018-02-06 12:26 ` Pablo Neira Ayuso
  2 siblings, 0 replies; 4+ messages in thread
From: Pablo Neira Ayuso @ 2018-02-06 12:26 UTC (permalink / raw)
  To: Cong Wang; +Cc: netdev, netfilter-devel, Eric Dumazet

On Mon, Feb 05, 2018 at 02:41:45PM -0800, Cong Wang wrote:
> rateest_hash is supposed to be protected by xt_rateest_mutex,
> and, as suggested by Eric, lookup and insert should be atomic,
> so we should acquire the xt_rateest_mutex once for both.
> 
> So introduce a non-locking helper for internal use and keep the
> locking one for external.

Applied, thanks!

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

end of thread, other threads:[~2018-02-06 12:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-05 22:41 [Patch net v2] xt_RATEEST: acquire xt_rateest_mutex for hash insert Cong Wang
2018-02-05 22:55 ` Florian Westphal
2018-02-05 23:25 ` Eric Dumazet
2018-02-06 12:26 ` Pablo Neira Ayuso

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).