Netfilter fixes for net
Subsystems (4)
Pablo Neira Ayuso
maintainercover Netfilter fixes for net
Hi,
The following patchset contains Netfilter fixes for net:
1) Do not permit to remove rules from chain binding, otherwise double rule release is possible, triggering UaF. This rule deletion support does not make sense and userspace does not use this. Problem exists since the introduction of chain binding support.
2) rbtree GC worker only collects the elements that have expired. This operation is not destructive, therefore, turn write into read spinlock to avoid datapath contention due to GC worker run. This was not fixed in the recent GC fix batch in the 6.5 cycle.
3) pipapo set backend performs sync GC, therefore, catchall elements must use sync GC queue variant. This bug was introduced in the 6.5 cycle with the recent GC fixes.
4) Stop GC run if memory allocation fails in pipapo set backend, otherwise access to NULL pointer to GC transaction object might occur. This bug was introduced in the 6.5 cycle with the recent GC fixes.
5) rhash GC run uses an iterator that might hit EAGAIN to rewind, triggering double-collection of the same element. This bug was introduced in the 6.5 cycle with the recent GC fixes.
6) Do not permit to remove elements in anonymous sets, this type of sets are populated once and then bound to rules. This fix is similar to the chain binding patch coming first in this batch. API permits since the very beginning but it has no use case from userspace.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git nf-23-09-13
Thanks.
----------------------------------------------------------------
The following changes since commit 1b36955cc048c8ff6ba448dbf4be0e52f59f2963:
net: enetc: distinguish error from valid pointers in enetc_fixup_clear_rss_rfs() (2023-09-07 11:19:42 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git tags/nf-23-09-13
for you to fetch changes up to e8dbde59ca3fe925d0105bfb380e8429928b16dd:
selftests: netfilter: Test nf_tables audit logging (2023-09-13 21:57:50 +0200)
----------------------------------------------------------------
netfilter pull request 23-09-13
----------------------------------------------------------------
Florian Westphal (1):
netfilter: conntrack: fix extension size table
Pablo Neira Ayuso (6):
netfilter: nf_tables: disallow rule removal from chain binding
netfilter: nft_set_rbtree: use read spinlock to avoid datapath contention
netfilter: nft_set_pipapo: call nft_trans_gc_queue_sync() in catchall GC
netfilter: nft_set_pipapo: stop GC iteration if GC transaction allocation fails
netfilter: nft_set_hash: try later when GC hits EAGAIN on iteration
netfilter: nf_tables: disallow element removal on anonymous sets
Phil Sutter (2):
netfilter: nf_tables: Fix entries val in rule reset audit log
selftests: netfilter: Test nf_tables audit logging
include/net/netfilter/nf_tables.h | 5 +- net/netfilter/nf_conntrack_extend.c | 4 +- net/netfilter/nf_tables_api.c | 65 ++++++--- net/netfilter/nft_set_hash.c | 11 +- net/netfilter/nft_set_pipapo.c | 4 +- net/netfilter/nft_set_rbtree.c | 8 +- tools/testing/selftests/netfilter/.gitignore | 1 + tools/testing/selftests/netfilter/Makefile | 4 +- tools/testing/selftests/netfilter/audit_logread.c | 165 ++++++++++++++++++++++ tools/testing/selftests/netfilter/config | 1 + tools/testing/selftests/netfilter/nft_audit.sh | 108 ++++++++++++++ 11 files changed, 338 insertions(+), 38 deletions(-) create mode 100644 tools/testing/selftests/netfilter/audit_logread.c create mode 100755 tools/testing/selftests/netfilter/nft_audit.sh
Pablo Neira Ayuso
maintainerPATCH 1/9 netfilter: nf_tables: disallow rule removal from chain binding
Chain binding only requires the rule addition/insertion command within the same transaction. Removal of rules from chain bindings within the same transaction makes no sense, userspace does not utilize this feature. Replace nft_chain_is_bound() check to nft_chain_binding() in rule deletion commands. Replace command implies a rule deletion, reject this command too.
Rule flush command can also safely rely on this nft_chain_binding() check because unbound chains are not allowed since 62e1e94b246e ("netfilter: nf_tables: reject unbound chain set before commit phase").
--- net/netfilter/nf_tables_api.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +13 -5
@@ -1432,7 +1432,7 @@ static int nft_flush_table(struct nft_ctx *ctx)if (!nft_is_active_next (ctx->net, chain))continue ; -if (nft_chain_is_bound (chain)) +if (nft_chain_binding (chain))continue ; ctx->chain = chain;
@@ -1477,7 +1477,7 @@ static int nft_flush_table(struct nft_ctx *ctx)if (!nft_is_active_next (ctx->net, chain))continue ; -if (nft_chain_is_bound (chain)) +if (nft_chain_binding (chain))continue ; ctx->chain = chain;
@@ -2910,6 +2910,9 @@ static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,return PTR_ERR (chain); } +if (nft_chain_binding (chain)) +return -EOPNOTSUPP ; +nft_ctx_init (&ctx, net, skb, info->nlh, family, table, chain, nla);if (nla[NFTA_CHAIN_HOOK ]) {
@@ -3971,6 +3974,11 @@ static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, }if (info->nlh->nlmsg_flags &NLM_F_REPLACE ) { +if (nft_chain_binding (chain)) { + err = -EOPNOTSUPP ; + goto err_destroy_flow_rule; + } + err =nft_delrule (&ctx, old_rule);if (err <0 ) goto err_destroy_flow_rule;
@@ -4078,7 +4086,7 @@ static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info,NL_SET_BAD_ATTR (extack, nla[NFTA_RULE_CHAIN ]);return PTR_ERR (chain); } -if (nft_chain_is_bound (chain)) +if (nft_chain_binding (chain))return -EOPNOTSUPP ; }
@@ -4112,7 +4120,7 @@ static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info,list_for_each_entry (chain, &table->chains, list) {if (!nft_is_active_next (net, chain))continue ; -if (nft_chain_is_bound (chain)) +if (nft_chain_binding (chain))continue ; ctx.chain = chain;
@@ -11054,7 +11062,7 @@ static void __nft_release_table(struct net *net, struct nft_table *table) ctx.family = table->family; ctx.table = table;list_for_each_entry (chain, &table->chains, list) { -if (nft_chain_is_bound (chain)) +if (nft_chain_binding (chain))continue ; ctx.chain = chain;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/9 netfilter: nft_set_rbtree: use read spinlock to avoid datapath contention
In reply to Pablo Neira Ayusorbtree GC does not modify the datastructure, instead it collects expired elements and it enqueues a GC transaction. Use a read spinlock instead to avoid data contention while GC worker is running.
--- net/netfilter/nft_set_rbtree.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
net/netfilter/nft_set_rbtree.c +2 -4
@@ -622,8 +622,7 @@ static void nft_rbtree_gc(struct work_struct *work)if (!gc) goto done; -write_lock_bh (&priv->lock); -write_seqcount_begin (&priv->count); +read_lock_bh (&priv->lock);for (node =rb_first (&priv->root); node !=NULL ; node =rb_next (node)) {/* Ruleset has been updated, try later. */
@@ -673,8 +672,7 @@ static void nft_rbtree_gc(struct work_struct *work) gc =nft_trans_gc_catchall (gc, gc_seq); try_later: -write_seqcount_end (&priv->count); -write_unlock_bh (&priv->lock); +read_unlock_bh (&priv->lock);if (gc)nft_trans_gc_queue_async_done (gc);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/9 netfilter: nft_set_pipapo: call nft_trans_gc_queue_sync() in catchall GC
In reply to Pablo Neira Ayusopipapo needs to enqueue GC transactions for catchall elements through nft_trans_gc_queue_sync(). Add nft_trans_gc_catchall_sync() and nft_trans_gc_catchall_async() to handle GC transaction queueing accordingly.
--- include/net/netfilter/nf_tables.h | 5 +++-- net/netfilter/nf_tables_api.c | 22 +++++++++++++++++++--- net/netfilter/nft_set_hash.c | 2 +- net/netfilter/nft_set_pipapo.c | 2 +- net/netfilter/nft_set_rbtree.c | 2 +- 5 files changed, 25 insertions(+), 8 deletions(-)
include/net/netfilter/nf_tables.h +3 -2
@@ -1700,8 +1700,9 @@ void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);void nft_trans_gc_elem_add (struct nft_trans_gc *gc,void *priv); -struct nft_trans_gc *nft_trans_gc_catchall (struct nft_trans_gc *gc, -unsigned int gc_seq); +struct nft_trans_gc *nft_trans_gc_catchall_async (struct nft_trans_gc *gc, +unsigned int gc_seq); +struct nft_trans_gc *nft_trans_gc_catchall_sync (struct nft_trans_gc *gc); void nft_setelem_data_deactivate(const struct net *net, const struct nft_set *set,
net/netfilter/nf_tables_api.c +19 -3
@@ -9613,8 +9613,9 @@ void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans)call_rcu (&trans->rcu, nft_trans_gc_trans_free); } -struct nft_trans_gc *nft_trans_gc_catchall (struct nft_trans_gc *gc, -unsigned int gc_seq) +static struct nft_trans_gc *nft_trans_gc_catchall (struct nft_trans_gc *gc, +unsigned int gc_seq, +bool sync) {struct nft_set_elem_catchall *catchall;const struct nft_set *set = gc->set;
@@ -9630,7 +9631,11 @@ struct nft_trans_gc *nft_trans_gc_catchall(struct nft_trans_gc *gc,nft_set_elem_dead (ext); dead_elem: - gc =nft_trans_gc_queue_async (gc, gc_seq,GFP_ATOMIC ); +if (sync) + gc =nft_trans_gc_queue_sync (gc,GFP_ATOMIC ); +else + gc =nft_trans_gc_queue_async (gc, gc_seq,GFP_ATOMIC ); +if (!gc)return NULL ;
@@ -9640,6 +9645,17 @@ struct nft_trans_gc *nft_trans_gc_catchall(struct nft_trans_gc *gc,return gc; } +struct nft_trans_gc *nft_trans_gc_catchall_async (struct nft_trans_gc *gc, +unsigned int gc_seq) +{ +return nft_trans_gc_catchall (gc, gc_seq, false); +} + +struct nft_trans_gc *nft_trans_gc_catchall_sync (struct nft_trans_gc *gc) +{ +return nft_trans_gc_catchall (gc,0 , true); +} +static void nf_tables_module_autoload_cleanup (struct net *net) {struct nftables_pernet *nft_net =nft_pernet (net);
net/netfilter/nft_set_hash.c +1 -1
@@ -372,7 +372,7 @@ static void nft_rhash_gc(struct work_struct *work)nft_trans_gc_elem_add (gc, he); } - gc =nft_trans_gc_catchall (gc, gc_seq); + gc =nft_trans_gc_catchall_async (gc, gc_seq); try_later:/* catchall list iteration requires rcu read side lock. */
net/netfilter/nft_set_pipapo.c +1 -1
@@ -1610,7 +1610,7 @@ static void pipapo_gc(const struct nft_set *_set, struct nft_pipapo_match *m) } } - gc =nft_trans_gc_catchall (gc,0 ); + gc =nft_trans_gc_catchall_sync (gc);if (gc) {nft_trans_gc_queue_sync_done (gc); priv->last_gc = jiffies;
net/netfilter/nft_set_rbtree.c +1 -1
@@ -669,7 +669,7 @@ static void nft_rbtree_gc(struct work_struct *work)nft_trans_gc_elem_add (gc, rbe); } - gc =nft_trans_gc_catchall (gc, gc_seq); + gc =nft_trans_gc_catchall_async (gc, gc_seq); try_later:read_unlock_bh (&priv->lock);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/9 netfilter: nft_set_pipapo: stop GC iteration if GC transaction allocation fails
In reply to Pablo Neira Ayusonft_trans_gc_queue_sync() enqueues the GC transaction and it allocates a new one. If this allocation fails, then stop this GC sync run and retry later.
--- net/netfilter/nft_set_pipapo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_set_pipapo.c +1 -1
@@ -1596,7 +1596,7 @@ static void pipapo_gc(const struct nft_set *_set, struct nft_pipapo_match *m) gc =nft_trans_gc_queue_sync (gc,GFP_ATOMIC );if (!gc) -break ; +return ;nft_pipapo_gc_deactivate (net, set, e);pipapo_drop (m, rulemap);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/9 netfilter: nft_set_hash: try later when GC hits EAGAIN on iteration
In reply to Pablo Neira AyusoSkip GC run if iterator rewinds to the beginning with EAGAIN, otherwise GC might collect the same element more than once.
--- net/netfilter/nft_set_hash.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
net/netfilter/nft_set_hash.c +3 -6
@@ -338,12 +338,9 @@ static void nft_rhash_gc(struct work_struct *work)while ((he =rhashtable_walk_next (&hti))) {if (IS_ERR (he)) { -if (PTR_ERR (he) != -EAGAIN ) { -nft_trans_gc_destroy (gc); - gc =NULL ; - goto try_later; - } -continue ; +nft_trans_gc_destroy (gc); + gc =NULL ; + goto try_later; }/* Ruleset has been updated, try later. */
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/9 netfilter: nf_tables: disallow element removal on anonymous sets
In reply to Pablo Neira AyusoAnonymous sets need to be populated once at creation and then they are bound to rule since 938154b93be8 ("netfilter: nf_tables: reject unbound anonymous set before commit phase"), otherwise transaction reports EINVAL.
Userspace does not need to delete elements of anonymous sets that are not yet bound, reject this with EOPNOTSUPP.
From flush command path, skip anonymous sets, they are expected to be bound already. Otherwise, EINVAL is hit at the end of this transaction for unbound sets.
--- net/netfilter/nf_tables_api.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +5 -4
@@ -1446,8 +1446,7 @@ static int nft_flush_table(struct nft_ctx *ctx)if (!nft_is_active_next (ctx->net, set))continue ; -if (nft_set_is_anonymous (set) && - !list_empty (&set->bindings)) +if (nft_set_is_anonymous (set))continue ; err =nft_delset (ctx, set);
@@ -7191,8 +7190,10 @@ static int nf_tables_delsetelem(struct sk_buff *skb,if (IS_ERR (set))return PTR_ERR (set); -if (!list_empty (&set->bindings) && - (set->flags & (NFT_SET_CONSTANT |NFT_SET_ANONYMOUS ))) +if (nft_set_is_anonymous (set)) +return -EOPNOTSUPP ; + +if (!list_empty (&set->bindings) && (set->flags &NFT_SET_CONSTANT ))return -EBUSY ;nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 7/9 netfilter: conntrack: fix extension size table
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
The size table is incorrect due to copypaste error, this reserves more size than needed.
TSTAMP reserved 32 instead of 16 bytes.
TIMEOUT reserved 16 instead of 8 bytes.
--- net/netfilter/nf_conntrack_extend.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_extend.c +2 -2
@@ -40,10 +40,10 @@ static const u8 nf_ct_ext_type_len[NF_CT_EXT_NUM] = { [NF_CT_EXT_ECACHE] = sizeof(struct nf_conntrack_ecache), #endif #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP - [NF_CT_EXT_TSTAMP ] =sizeof (struct nf_conn_acct ), + [NF_CT_EXT_TSTAMP ] =sizeof (struct nf_conn_tstamp ), #endif #ifdef CONFIG_NF_CONNTRACK_TIMEOUT - [NF_CT_EXT_TIMEOUT ] =sizeof (struct nf_conn_tstamp ), + [NF_CT_EXT_TIMEOUT ] =sizeof (struct nf_conn_timeout ), #endif #ifdef CONFIG_NF_CONNTRACK_LABELS [NF_CT_EXT_LABELS] = sizeof(struct nf_conn_labels),
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 8/9 netfilter: nf_tables: Fix entries val in rule reset audit log
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
The value in idx and the number of rules handled in that particular __nf_tables_dump_rules() call is not identical. The former is a cursor to pick up from if multiple netlink messages are needed, so its value is ever increasing. Fixing this is not just a matter of subtracting s_idx from it, though: When resetting rules in multiple chains, __nf_tables_dump_rules() is called for each and cb->args[0] is not adjusted in between. Introduce a dedicated counter to record the number of rules reset in this call in a less confusing way.
While being at it, prevent the direct return upon buffer exhaustion: Any rules previously dumped into that skb would evade audit logging otherwise.
--- net/netfilter/nf_tables_api.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
net/netfilter/nf_tables_api.c +10 -6
@@ -3451,6 +3451,8 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,struct net *net =sock_net (skb->sk);const struct nft_rule *rule, *prule;unsigned int s_idx = cb->args[0 ]; +unsigned int entries =0 ; +int ret =0 ;u64 handle; prule =NULL ;
@@ -3473,9 +3475,11 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, NFT_MSG_NEWRULE, NLM_F_MULTI | NLM_F_APPEND, table->family, - table, chain, rule, handle, reset) < 0) - return 1; - + table, chain, rule, handle, reset) < 0) { + ret =1 ; +break ; + } + entries++;nl_dump_check_consistent (cb,nlmsg_hdr (skb)); cont: prule = rule;
@@ -3483,10 +3487,10 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, (*idx)++; } -if (reset && *idx) -audit_log_rule_reset (table, cb->seq, *idx); +if (reset && entries) +audit_log_rule_reset (table, cb->seq, entries); -return 0 ; +return ret; } static int nf_tables_dump_rules(struct sk_buff *skb,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 9/9 selftests: netfilter: Test nf_tables audit logging
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Compare NETFILTER_CFG type audit logs emitted from kernel upon ruleset modifications against expected output.
--- tools/testing/selftests/netfilter/.gitignore | 1 + tools/testing/selftests/netfilter/Makefile | 4 +- .../selftests/netfilter/audit_logread.c | 165 ++++++++++++++++++ tools/testing/selftests/netfilter/config | 1 + .../testing/selftests/netfilter/nft_audit.sh | 108 ++++++++++++ 5 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/netfilter/audit_logread.c create mode 100755 tools/testing/selftests/netfilter/nft_audit.sh
tools/testing/selftests/netfilter/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only nf-queue connect_close +audit_logread
tools/testing/selftests/netfilter/Makefile +2 -2
@@ -6,13 +6,13 @@ TEST_PROGS := nft_trans_stress.sh nft_fib.sh nft_nat.sh bridge_brouter.sh \ nft_concat_range.sh nft_conntrack_helper.sh \ nft_queue.sh nft_meta.sh nf_nat_edemux.sh \ ipip-conntrack-mtu.sh conntrack_tcp_unreplied.sh \ - conntrack_vrf.sh nft_synproxy.sh rpath.sh + conntrack_vrf.sh nft_synproxy.sh rpath.sh nft_audit.sh HOSTPKG_CONFIG := pkg-config CFLAGS += $(shell $(HOSTPKG_CONFIG) --cflags libmnl 2>/dev/null) LDLIBS += $(shell $(HOSTPKG_CONFIG) --libs libmnl 2>/dev/null || echo -lmnl) -TEST_GEN_FILES = nf-queue connect_close +TEST_GEN_FILES = nf-queue connect_close audit_logread include ../lib.mk
tools/testing/selftests/netfilter/audit_logread.c new file +165 -0
165 lines not on this page · raw message
tools/testing/selftests/netfilter/config +1 -0
@@ -6,3 +6,4 @@ CONFIG_NFT_REDIR=m CONFIG_NFT_MASQ=m CONFIG_NFT_FLOW_OFFLOAD=m CONFIG_NF_CT_NETLINK=m +CONFIG_AUDIT=y
tools/testing/selftests/netfilter/nft_audit.sh new file +108 -0
108 lines not on this page · raw message
Signature · 1 line
2.30.2