netfilter: nf_tables: fix false negative lookups with ongoing transaction
Subsystems (2)
Florian Westphal
maintainercover netfilter: nf_tables: fix false negative lookups with ongoing transaction
Stefan Hanreich reports spurious false negative results during set lookups while another CPU is processing a transaction.
Quoting from the original bug report:
It seems like we've found an issue with atomicity when reloading nftables rulesets. Sometimes there is a small window where rules containing sets do not seem to apply to incoming traffic, due to the set apparently being empty for a short amount of time when flushing / adding elements.
Exanple ruleset:
table ip filter {
set match {
type ipv4_addr
flags interval
elements = { 0.0.0.0-192.168.2.19, 192.168.2.21-255.255.255.255 }}
chain pre {
type filter hook prerouting priority filter; policy accept; ip saddr @match accept
counter comment "must never match"
}
}
Reproducer transaction:
while true:
nft -f -<<EOF
flush set ip filter match
create element ip filter match { \
0.0.0.0-192.168.2.19, 192.168.2.21-255.255.255.255 }
EOF
done
Then create traffic. to/from e.g. 192.168.2.1 to 192.168.3.10. Once in a while the counter will increment even though the 'ip saddr @match' rule should have accepted the packet.
This series resolves set inconsistencies that occur when a transaction has entered the final commit phase. See individual patches for details.
Thanks to Stefan Hanreich for an initial description and reproducer for this bug and to Pablo Neira Ayuso for reviewing earlier iterations of this patchset.
Florian Westphal (5):
netfilter: nft_set_pipapo: don't check genbit from packetpath lookups
netfilter: nft_set_rbtree: continue traversal if element is inactive
netfilter: nf_tables: place base_seq in struct net
netfilter: nf_tables: make nft_set_do_lookup available unconditionally
netfilter: nf_tables: restart set lookup on base_seq change
include/net/netfilter/nf_tables.h | 1 - include/net/netfilter/nf_tables_core.h | 10 +--- include/net/netns/nftables.h | 1 + net/netfilter/nf_tables_api.c | 66 +++++++++++++------------- net/netfilter/nft_lookup.c | 46 ++++++++++++++++-- net/netfilter/nft_set_pipapo.c | 20 +++++++- net/netfilter/nft_set_pipapo_avx2.c | 4 +- net/netfilter/nft_set_rbtree.c | 6 +-- 8 files changed, 100 insertions(+), 54 deletions(-)
Signature · 1 line
2.49.1
Florian Westphal
maintainerPATCH 1/5 netfilter: nft_set_pipapo: don't check genbit from packetpath lookups
The pipapo set type is special in that it has two copies of its
datastructure: one live copy containing only valid elements and one on-demand clone used during transaction where adds/deletes happen.
This clone is not visible to the datapath.
This is unlike all other set types in nftables, those all link new elements into their live hlist/tree.
For those sets, the lookup functions must skip the new elements while the transaction is ongoing to ensure consistency.
As the clone is shallow, removal does have an effect on the packet path:
once the transaction enters the commit phase the 'gencursor' bit that determines which elements are active and which elements should be ignored (because they are no longer valid) is flipped.
This causes the datapath lookup to ignore these elements if they are found during lookup.
This opens up a small race window where pipapo has an inconsistent view of the dataset from when the transaction-cpu flipped the genbit until the transaction-cpu calls nft_pipapo_commit() to swap live/clone pointers:
cpu0 cpu1
has added new elements to clone
has marked elements as being
inactive in new generation
perform lookup in the set
enters commit phase:
I) increments the genbit
A) observes new genbit
removes elements from the clone so
they won't be found anymore
B) lookup in datastructure can't see new elements yet, but old elements are ignored -> Only matches elements that were not changed in the transaction
II) calls nft_pipapo_commit(), clone
and live pointers are swapped.
C New nft_lookup happening now
will find matching elements.
Consider a packet matching range r1-r2:
cpu0 processes following transaction:
1. remove r1-r2
2. add r1-r3
P is contained in both ranges. Therefore, cpu1 should always find a match for P. Due to above race, this is not the case:
cpu1 does find r1-r2, but then ignores it due to the genbit indicating the range has been removed.
At the same time, r1-r3 is not visible yet, because it can only be found in the clone.
The situation persists for all lookups until after cpu0 hits II).
The fix is easy: Don't check the genbit from pipapo lookup functions. This is possible because unlike the other set types, the new elements are not reachable from the live copy of the dataset.
The clone/live pointer swap is enough to avoid matching on old elements while at the same time all new elements are exposed in one go.
After this change, step B above returns a match in r1-r2. This is fine: r1-r2 only becomes truly invalid the moment they get freed. This happens after a synchronize_rcu() call and rcu read lock is held via netfilter hook traversal (nf_hook_slow()).
--- net/netfilter/nft_set_pipapo.c | 20 ++++++++++++++++++-- net/netfilter/nft_set_pipapo_avx2.c | 4 +--- 2 files changed, 19 insertions(+), 5 deletions(-)
net/netfilter/nft_set_pipapo.c +18 -2
@@ -510,6 +510,23 @@ static struct nft_pipapo_elem *pipapo_get(const struct nft_pipapo_match *m,* * This function is called from the data path. It will search for * an element matching the given key in the current active copy. +* Unlike other set types, this uses NFT_GENMASK_ANY instead of +* nft_genmask_cur(). +* +* This is because new (future) elements are not reachable from +* priv->match, they get added to priv->clone instead. +* When the commit phase flips the generation bitmask, the +* 'now old' entries are skipped but without the 'now current' +* elements becoming visible. Using nft_genmask_cur() thus creates +* inconsistent state: matching old entries get skipped but thew +* newly matching entries are unreachable. +* +* GENMASK will still find the 'now old' entries which ensures consistent +* priv->match view. +* +* nft_pipapo_commit swaps ->clone and ->match shortly after the +* genbit flip. As ->clone doesn't contain the old entries in the first +* place, lookup will only find the now-current ones. * * Return: ntables API extension pointer or NULL if no match. */
@@ -518,12 +535,11 @@ nft_pipapo_lookup(const struct net *net, const struct nft_set *set, const u32 *key) {struct nft_pipapo *priv =nft_set_priv (set); -u8 genmask =nft_genmask_cur (net);const struct nft_pipapo_match *m;const struct nft_pipapo_elem *e; m =rcu_dereference (priv->match); - e =pipapo_get (m, (const u8 *)key, genmask,get_jiffies_64 ()); + e =pipapo_get (m, (const u8 *)key,NFT_GENMASK_ANY ,get_jiffies_64 ());return e ? &e->ext :NULL ; }
net/netfilter/nft_set_pipapo_avx2.c +1 -3
@@ -1152,7 +1152,6 @@ nft_pipapo_avx2_lookup(const struct net *net, const struct nft_set *set,struct nft_pipapo *priv =nft_set_priv (set);const struct nft_set_ext *ext =NULL ;struct nft_pipapo_scratch *scratch; -u8 genmask =nft_genmask_cur (net);const struct nft_pipapo_match *m;const struct nft_pipapo_field *f;const u8 *rp = (const u8 *)key;
@@ -1248,8 +1247,7 @@ nft_pipapo_avx2_lookup(const struct net *net, const struct nft_set *set,if (last) {const struct nft_set_ext *e = &f->mt[ret].e->ext; -if (unlikely (nft_set_elem_expired (e) || - !nft_set_elem_active (e, genmask))) +if (unlikely (nft_set_elem_expired (e))) goto next_match; ext = e;
Signature · 1 line
2.49.1
Florian Westphal
maintainerPATCH 2/5 netfilter: nft_set_rbtree: continue traversal if element is inactive
In reply to Florian WestphalWhen the rbtree lookup function finds a match in the rbtree, it sets the range start interval to a potentially inactive element.
Then, after tree lookup, if the matching element is inactive, it returns NULL and suppresses a matching result.
This is wrong and leads to false negative matches when a transaction has already entered the commit phase.
cpu0 cpu1
has added new elements to clone
has marked elements as being
inactive in new generation
perform lookup in the set
enters commit phase:
I) increments the genbit
A) observes new genbit
B) finds matching range
C) returns no match: found range invalid in new generation
II) removes old elements from the tree
C New nft_lookup happening now
will find matching element, because it is no longer obscured by old, inactive one.
Consider a packet matching range r1-r2:
cpu0 processes following transaction:
1. remove r1-r2
2. add r1-r3
P is contained in both ranges. Therefore, cpu1 should always find a match for P. Due to above race, this is not the case:
cpu1 does find r1-r2, but then ignores it due to the genbit indicating the range has been removed. It does NOT test for further matches.
The situation persists for all lookups until after cpu0 hits II) after which r1-r3 range start node is tested for the first time.
Move the "interval start is valid" check ahead so that tree traversal continues if the starting interval is not valid in this generation.
Thanks to Stefan Hanreich for providing an initial reproducer for this bug.
--- net/netfilter/nft_set_rbtree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/netfilter/nft_set_rbtree.c +3 -3
@@ -77,7 +77,9 @@ __nft_rbtree_lookup(const struct net *net, const struct nft_set *set,nft_rbtree_interval_end (rbe) && nft_rbtree_interval_start(interval)) continue; - interval = rbe; +if (nft_set_elem_active (&rbe->ext, genmask) && + !nft_rbtree_elem_expired (rbe)) + interval = rbe; }else if (d >0 ) parent =rcu_dereference_raw (parent->rb_right);else {
@@ -102,8 +104,6 @@ __nft_rbtree_lookup(const struct net *net, const struct nft_set *set, }if (set->flags &NFT_SET_INTERVAL && interval !=NULL && -nft_set_elem_active (&interval->ext, genmask) && - !nft_rbtree_elem_expired (interval) &&nft_rbtree_interval_start (interval))return &interval->ext;
Signature · 1 line
2.49.1
Florian Westphal
maintainerPATCH 3/5 netfilter: nf_tables: place base_seq in struct net
In reply to Florian WestphalThis will soon be read from packet path around same time as the gencursor.
Both gencursor and base_seq get incremented almost at the same time, so it makes sense to place them in the same structure.
This doesn't increase struct net size on 64bit due to padding.
--- include/net/netfilter/nf_tables.h | 1 - include/net/netns/nftables.h | 1 + net/netfilter/nf_tables_api.c | 65 ++++++++++++++++--------------- 3 files changed, 34 insertions(+), 33 deletions(-)
include/net/netfilter/nf_tables.h +0 -1
@@ -1912,7 +1912,6 @@ struct nftables_pernet {struct mutex commit_mutex;u64 table_handle;u64 tstamp; -unsigned int base_seq;unsigned int gc_seq;u8 validate_state;struct work_struct destroy_work;
include/net/netns/nftables.h +1 -0
@@ -3,6 +3,7 @@#define _NETNS_NFTABLES_H_struct netns_nftables { +unsigned int base_seq;u8 gencursor; };
net/netfilter/nf_tables_api.c +33 -32
235 lines not on this page · raw message
Signature · 1 line
2.49.1
Florian Westphal
maintainerPATCH 4/5 netfilter: nf_tables: make nft_set_do_lookup available unconditionally
In reply to Florian WestphalThis function was added for retpoline mitigation and is replaced by a static inline helper if mitigations are not enabled.
Enable this helper function unconditionally so next patch can add a lookup restart mechanism to fix possible false negatives while transactions are in progress.
Adding lookup restarts in nft_lookup_eval doesn't work as nft_objref would then need the same copypaste loop.
This patch is separate to ease review of the actual bug fix.
--- include/net/netfilter/nf_tables_core.h | 10 ++-------- net/netfilter/nft_lookup.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables_core.h +2 -8
@@ -109,17 +109,11 @@ nft_hash_lookup_fast(const struct net *net, const struct nft_set *set,const struct nft_set_ext *nft_hash_lookup (const struct net *net,const struct nft_set *set,const u32 *key); +#endif +const struct nft_set_ext *nft_set_do_lookup (const struct net *net,const struct nft_set *set,const u32 *key); -#else -static inline const struct nft_set_ext * -nft_set_do_lookup (const struct net *net,const struct nft_set *set, -const u32 *key) -{ -return set->ops->lookup (net, set, key); -} -#endif /* called from nft_pipapo_avx2.c */ const struct nft_set_ext *
net/netfilter/nft_lookup.c +12 -5
@@ -24,11 +24,11 @@ struct nft_lookup {struct nft_set_binding binding; }; -#ifdef CONFIG_MITIGATION_RETPOLINE -const struct nft_set_ext * -nft_set_do_lookup (const struct net *net,const struct nft_set *set, -const u32 *key) +static const struct nft_set_ext * +__nft_set_do_lookup (const struct net *net,const struct nft_set *set, +const u32 *key) { +#ifdef CONFIG_MITIGATION_RETPOLINE if (set->ops == &nft_set_hash_fast_type.ops)return nft_hash_lookup_fast (net, set, key);if (set->ops == &nft_set_hash_type.ops)
@@ -51,10 +51,17 @@ nft_set_do_lookup(const struct net *net, const struct nft_set *set,return nft_rbtree_lookup (net, set, key);WARN_ON_ONCE (1 ); +#endif return set->ops->lookup (net, set, key); } + +const struct nft_set_ext * +nft_set_do_lookup (const struct net *net,const struct nft_set *set, +const u32 *key) +{ +return __nft_set_do_lookup (net, set, key); +}EXPORT_SYMBOL_GPL (nft_set_do_lookup); -#endif void nft_lookup_eval(const struct nft_expr *expr, struct nft_regs *regs,
Signature · 1 line
2.49.1
Florian Westphal
maintainerPATCH 5/5 netfilter: nf_tables: restart set lookup on base_seq change
In reply to Florian WestphalThe hash, hash_fast, rhash and bitwise sets may indicate no result even though a matching element exists during a short time window while other cpu is finalizing the transaction.
This happens when the hash lookup/bitwise lookup function has picked up the old genbit, right before it was toggled by nf_tables_commit(), but then the same cpu managed to unlink the matching old element from the hash table:
cpu0 cpu1
has added new elements to clone
has marked elements as being
inactive in new generation
perform lookup in the set
enters commit phase:
A) observes old genbit
increments base_seq
I) increments the genbit
II) removes old element from the set
B) finds matching element
C) returns no match: found element is not valid in old generation
Next lookup observes new genbit and finds matching e2.
Consider a packet matching element e1, e2.
cpu0 processes following transaction:
1. remove e1
2. adds e2, which has same key as e1.
P matches both e1 and e2. Therefore, cpu1 should always find a match for P. Due to above race, this is not the case:
cpu1 observed the old genbit. e2 will not be considered once it is found. The element e1 is not found anymore if cpu0 managed to unlink it from the hlist before cpu1 found it during list traversal.
The situation only occurs for a brief time period, lookups happening after I) observe new genbit and return e2.
This problem exists in all set types except nft_set_pipapo, so fix it once in nft_lookup rather than each set ops individually.
Sample the base sequence counter, which gets incremented right before the genbit is changed.
Then, if no match is found, retry the lookup if the base sequence was altered in between.
If the base sequence hasn't changed:
- No update took place: no-match result is expected.
This is the common case. or:
- nf_tables_commit() hasn't progressed to genbit update yet. Old elements were still visible and nomatch result is expected, or:
- nf_tables_commit updated the genbit:
We picked up the new base_seq, so the lookup function also picked up the new genbit, no-match result is expected.
If the old genbit was observed, then nft_lookup also picked up the oldbase_seq: nft_lookup_should_retry() returns true and relookup is performed in the new generation.
Thanks to Pablo Neira Ayuso for reviewing an earlier version of this patchset, for suggesting re-use of existing base_seq and placement of the restart loop in nft_set_do_lookup().
--- net/netfilter/nf_tables_api.c | 3 ++- net/netfilter/nft_lookup.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-)
net/netfilter/nf_tables_api.c +2 -1
@@ -10973,7 +10973,8 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)while (++base_seq ==0 ) ; -WRITE_ONCE (net->nft.base_seq, base_seq); +/* pairs with smp_load_acquire in nft_lookup_eval */ +smp_store_release (&net->nft.base_seq, base_seq); gc_seq =nft_gc_seq_begin (nft_net);
net/netfilter/nft_lookup.c +30 -1
@@ -55,11 +55,40 @@ __nft_set_do_lookup(const struct net *net, const struct nft_set *set,return set->ops->lookup (net, set, key); } +static unsigned int nft_base_seq (const struct net *net) +{ +/* pairs with smp_store_release() in nf_tables_commit() */ +return smp_load_acquire (&net->nft.base_seq); +} + +static bool nft_lookup_should_retry (const struct net *net,unsigned int seq) +{ +return unlikely (seq !=nft_base_seq (net)); +} +const struct nft_set_ext *nft_set_do_lookup (const struct net *net,const struct nft_set *set,const u32 *key) { -return __nft_set_do_lookup (net, set, key); +const struct nft_set_ext *ext; +unsigned int base_seq; + +do { + base_seq =nft_base_seq (net); + + ext =__nft_set_do_lookup (net, set, key); +if (ext) +break ; +/* No match? There is a small chance that lookup was +* performed in the old generation, but nf_tables_commit() +* already unlinked a (matching) element. +* +* We need to repeat the lookup to make sure that we didn't +* miss a matching element in the new generation. +*/ + }while (nft_lookup_should_retry (net, base_seq)); + +return ext; }EXPORT_SYMBOL_GPL (nft_set_do_lookup);
Signature · 1 line
2.49.1
I tested this fix using Stefan Hanreich’s reproducer on the latest master branch. The bug was resolved, and I didn't see anything out of the ordinary.
Consider:
Florian Westphal
maintainerQuoted 3 lines · Gabriel Goller
I tested this fix using Stefan Hanreich’s reproducer on the latest master branch. The bug was resolved, and I didn't see anything out of the ordinary.
Thanks for testing!