netfilter: nft_compat: relax chain type validation
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/3 netfilter: nft_compat: relax chain type validation
Check for nat chain dependency only, which is the one that can actually crash the kernel. Don't care if mangle, filter and security specific match and targets are used out of their scope, they are harmless.
This restores iptables-compat with mangle specific match/target when used out of the OUTPUT chain, that are actually emulated through filter chains, which broke when performing strict validation.
--- net/netfilter/nft_compat.c | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-)
net/netfilter/nft_compat.c +2 -30
@@ -21,45 +21,17 @@#include <linux/netfilter_ipv6/ip6_tables.h> #include <net/netfilter/nf_tables.h> -static const struct { -const char *name; -u8 type; -} table_to_chaintype[] = { - {"filter" ,NFT_CHAIN_T_DEFAULT }, - {"raw" ,NFT_CHAIN_T_DEFAULT }, - {"security" ,NFT_CHAIN_T_DEFAULT }, - {"mangle" ,NFT_CHAIN_T_ROUTE }, - {"nat" ,NFT_CHAIN_T_NAT }, - { }, -}; - -static int nft_compat_table_to_chaintype (const char *table) -{ -int i; - -for (i =0 ; table_to_chaintype[i].name !=NULL ; i++) { -if (strcmp (table_to_chaintype[i].name, table) ==0 ) -return table_to_chaintype[i].type; - } - -return -1 ; -} -static int nft_compat_chain_validate_dependency (const char *tablename,const struct nft_chain *chain) { -enum nft_chain_type type;const struct nft_base_chain *basechain;if (!tablename || !(chain->flags &NFT_BASE_CHAIN ))return 0 ; - type =nft_compat_table_to_chaintype (tablename); -if (type <0 ) -return -EINVAL ; - basechain =nft_base_chain (chain); -if (basechain->type->type != type) +if (strcmp (tablename,"nat" ) ==0 && + basechain->type->type !=NFT_CHAIN_T_NAT )return -EINVAL ;return 0 ;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/3 netfilter: nft_compat: use the match->table to validate dependencies
Instead of the match->name, which is of course not relevant.
--- net/netfilter/nft_compat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nft_compat.c +2 -2
@@ -346,7 +346,7 @@ nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,union nft_entry e = {};int ret; - ret =nft_compat_chain_validate_dependency (match->name, ctx->chain); + ret =nft_compat_chain_validate_dependency (match->table, ctx->chain);if (ret <0 ) goto err;
@@ -420,7 +420,7 @@ static int nft_match_validate(const struct nft_ctx *ctx,if (!(hook_mask & match->hooks))return -EINVAL ; - ret =nft_compat_chain_validate_dependency (match->name, + ret =nft_compat_chain_validate_dependency (match->table, ctx->chain);if (ret <0 )return ret;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/3 netfilter: nf_tables: restore synchronous object release from commit/abort
In reply to Pablo Neira AyusoThe existing xtables matches and targets, when used from nft_compat, may sleep from the destroy path, ie. when removing rules. Since the objects are released via call_rcu from softirq context, this results in lockdep splats and possible lockups that may be hard to reproduce.
Patrick also indicated that delayed object release via call_rcu can cause us problems in the ordering of event notifications when anonymous sets are in place.
So, this patch restores the synchronous object release from the commit and abort paths. This includes a call to synchronize_rcu() to make sure that no packets are walking on the objects that are going to be released. This is slowier though, but it's simple and it resolve the aforementioned problems.
This is a partial revert of c7c32e7 ("netfilter: nf_tables: defer all object release via rcu") that was introduced in 3.16 to speed up interaction with userspace.
--- include/net/netfilter/nf_tables.h | 2 -- net/netfilter/nf_tables_api.c | 24 ++++++++---------------- 2 files changed, 8 insertions(+), 18 deletions(-)
include/net/netfilter/nf_tables.h +0 -2
@@ -396,14 +396,12 @@ struct nft_rule {/** * struct nft_trans - nf_tables object update in transaction * -* @rcu_head: rcu head to defer release of transaction data * @list: used internally * @msg_type: message type * @ctx: transaction context * @data: internal information related to the transaction */ struct nft_trans { -struct rcu_head rcu_head;struct list_head list;int msg_type;struct nft_ctx ctx;
net/netfilter/nf_tables_api.c +8 -16
@@ -3484,13 +3484,8 @@ static void nft_chain_commit_update(struct nft_trans *trans) } } -/* Schedule objects for release via rcu to make sure no packets are accesing -* removed rules. -*/ -static void nf_tables_commit_release_rcu (struct rcu_head *rt) +static void nf_tables_commit_release (struct nft_trans *trans) { - struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head); -switch (trans->msg_type) {case NFT_MSG_DELTABLE :nf_tables_table_destroy (&trans->ctx);
@@ -3612,10 +3607,11 @@ static int nf_tables_commit(struct sk_buff *skb) } } +synchronize_rcu (); +list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) {list_del (&trans->list); - trans->ctx.nla =NULL ; -call_rcu (&trans->rcu_head, nf_tables_commit_release_rcu); +nf_tables_commit_release (trans); }nf_tables_gen_notify (net, skb,NFT_MSG_NEWGEN );
@@ -3623,13 +3619,8 @@ static int nf_tables_commit(struct sk_buff *skb)return 0 ; } -/* Schedule objects for release via rcu to make sure no packets are accesing -* aborted rules. -*/ -static void nf_tables_abort_release_rcu (struct rcu_head *rt) +static void nf_tables_abort_release (struct nft_trans *trans) { - struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head); -switch (trans->msg_type) {case NFT_MSG_NEWTABLE :nf_tables_table_destroy (&trans->ctx);
@@ -3725,11 +3716,12 @@ static int nf_tables_abort(struct sk_buff *skb) } } +synchronize_rcu (); +list_for_each_entry_safe_reverse (trans, next, &net->nft.commit_list, list) {list_del (&trans->list); - trans->ctx.nla =NULL ; -call_rcu (&trans->rcu_head, nf_tables_abort_release_rcu); +nf_tables_abort_release (trans); }return 0 ;
Signature · 1 line
1.7.10.4