Skip to contentSkip to conversation

netfilter: conntrack: fix calculation of next bucket number in early_drop

PATCH Superseded Vasily Khoruzhick 2 participants 3 messages Oct 25, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: conntrack: fix calculation of next bucket number in early_drop

If there's no entry to drop in bucket that corresponds to the hash, early_drop() should look for it in other buckets. But since it increments hash instead of bucket number, it actually looks in the same bucket 8
times: hsize is 16k by default (14 bits) and hash is 32-bit value, so reciprocal_scale(hash, hsize) returns the same value for hash..hash+7 in most cases.

Fix it by increasing bucket number instead of hash and rename _hash to bucket to avoid future confusion.

Fixes 3e86638e9a0b ("netfilter: conntrack: consider ct netns in early_drop logic")
Cc <[email protected]> # v4.7+
---
 net/netfilter/nf_conntrack_core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/netfilter/nf_conntrack_core.c +7 -4
index ca1168d67fac..a04af246b184 100644
@@ -1073,19 +1073,22 @@ static unsigned int early_drop_list(struct net *net,
 	return drops;
 }
 
-static noinline int early_drop(struct net *net, unsigned int _hash)
+static noinline int early_drop(struct net *net, unsigned int hash)
 {
 	unsigned int i;
 
 	for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
 		struct hlist_nulls_head *ct_hash;
-		unsigned int hash, hsize, drops;
+		unsigned int bucket, hsize, drops;
 
 		rcu_read_lock();
 		nf_conntrack_get_ht(&ct_hash, &hsize);
-		hash = reciprocal_scale(_hash++, hsize);
+		if (!i)
+			bucket = reciprocal_scale(hash, hsize);
+		else
+			bucket = (bucket + 1) % hsize;
 
-		drops = early_drop_list(net, &ct_hash[hash]);
+		drops = early_drop_list(net, &ct_hash[bucket]);
 		rcu_read_unlock();
 
 		if (drops) {
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Vasily Khoruzhick

If there's no entry to drop in bucket that corresponds to the hash, early_drop() should look for it in other buckets. But since it increments hash instead of bucket number, it actually looks in the same bucket 8
times: hsize is 16k by default (14 bits) and hash is 32-bit value, so reciprocal_scale(hash, hsize) returns the same value for hash..hash+7 in most cases.

Quoted 6 lines

Fix it by increasing bucket number instead of hash and rename _hash to bucket to avoid future confusion.

Fixes 3e86638e9a0b ("netfilter: conntrack: consider ct netns in early_drop logic")
Cc <[email protected]> # v4.7+

Nice work!

Quoted 36 lines
---
  net/netfilter/nf_conntrack_core.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
net/netfilter/nf_conntrack_core.c +7 -4
index ca1168d67fac..a04af246b184 100644
@@ -1073,19 +1073,22 @@ static unsigned int early_drop_list(struct net *net,
  	return drops;
  }
 
-static noinline int early_drop(struct net *net, unsigned int _hash)
+static noinline int early_drop(struct net *net, unsigned int hash)
  {
  	unsigned int i;
 
  	for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
  		struct hlist_nulls_head *ct_hash;
-		unsigned int hash, hsize, drops;
+		unsigned int bucket, hsize, drops;
 
  		rcu_read_lock();
  		nf_conntrack_get_ht(&ct_hash, &hsize);
-		hash = reciprocal_scale(_hash++, hsize);
+		if (!i)
+			bucket = reciprocal_scale(hash, hsize);
+		else
+			bucket = (bucket + 1) % hsize;
 
-		drops = early_drop_list(net, &ct_hash[hash]);
+		drops = early_drop_list(net, &ct_hash[bucket]);
  		rcu_read_unlock();
 
  		if (drops) {
Signature · 1 line
          Dima
#2 lore raw
Quoted 18 lines · Dmitry Safonov
Quoted 8 lines · Vasily Khoruzhick

If there's no entry to drop in bucket that corresponds to the hash, early_drop() should look for it in other buckets. But since it increments hash instead of bucket number, it actually looks in the same bucket 8
times: hsize is 16k by default (14 bits) and hash is 32-bit value, so reciprocal_scale(hash, hsize) returns the same value for hash..hash+7 in most cases.

Quoted 7 lines

Fix it by increasing bucket number instead of hash and rename _hash to bucket to avoid future confusion.

Fixes 3e86638e9a0b ("netfilter: conntrack: consider ct netns in

early_drop logic")

Quoted 3 lines

Nice work!

Oops, found a bug in it - 'bucket' should be moved outside of the loop. I'll send v2 tomorrow morning.

Quoted 37 lines
Quoted 24 lines
---
  net/netfilter/nf_conntrack_core.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
net/netfilter/nf_conntrack_core.c +2 -1
index ca1168d67fac..a04af246b184 100644
@@ -1073,19 +1073,22 @@ static unsigned int early_drop_list(struct net
 *net,
        return drops;
  }
  -static noinline int early_drop(struct net *net, unsigned int _hash)
+static noinline int early_drop(struct net *net, unsigned int hash)
  {
        unsigned int i;
        for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
                struct hlist_nulls_head *ct_hash;
-               unsigned int hash, hsize, drops;
+               unsigned int bucket, hsize, drops;
                rcu_read_lock();
                nf_conntrack_get_ht(&ct_hash, &hsize);

net/netfilter/nf_conntrack_core.c static unsigned int early_drop_list(struct net · line 1093

Quoted 10 lines
net/netfilter/nf_conntrack_core.c +5 -1
-               hash = reciprocal_scale(_hash++, hsize);
+               if (!i)
+                       bucket = reciprocal_scale(hash, hsize);
+               else
+                       bucket = (bucket + 1) % hsize;
  -             drops = early_drop_list(net, &ct_hash[hash]);
+               drops = early_drop_list(net, &ct_hash[bucket]);
                rcu_read_unlock();
                if (drops) {
 

--
Dima