Netfilter fixes for net
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter fixes for net
Hi David,
The following patchset contains Netfilter fixes for your net tree:
1) Fix list corruption in device notifier in the masquerade infrastructure, from Florian Westphal.
2) Fix double-free of sets and use-after-free when deleting elements.
3) Don't bogusly return EBUSY when removing a set after flush command.
4) Use-after-free in dynamically allocate operations.
5) Don't report a new ruleset generation to userspace if transaction list is empty, this invalidates the userspace cache innecessarily. From Florian Westphal.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Thanks!
----------------------------------------------------------------
The following changes since commit 1e027960edfaa6a43f9ca31081729b716598112b:
net/hsr: fix possible crash in add_timer() (2019-03-07 11:02:08 -0800)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git HEAD
for you to fetch changes up to b8b27498659c65034032af79842913844a6cc79a:
netfilter: nf_tables: return immediately on empty commit (2019-03-11 20:01:20 +0100)
----------------------------------------------------------------
Florian Westphal (2):
netfilter: nat: don't register device notifier twice
netfilter: nf_tables: return immediately on empty commit
Pablo Neira Ayuso (3):
netfilter: nf_tables: fix set double-free in abort path
netfilter: nf_tables: bogus EBUSY when deleting set after flush
netfilter: nf_tables: use-after-free in dynamic operations
include/net/netfilter/nf_tables.h | 12 ++++++--- net/netfilter/nf_nat_masquerade.c | 35 +++++++++++++------------ net/netfilter/nf_tables_api.c | 54 +++++++++++++++++++++++++++++++++------ net/netfilter/nft_dynset.c | 13 +++++++--- net/netfilter/nft_lookup.c | 13 +++++++--- net/netfilter/nft_objref.c | 13 +++++++--- 6 files changed, 100 insertions(+), 40 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/5 netfilter: nat: don't register device notifier twice
From: Florian Westphal <[email protected]>
Otherwise, we get notifier list corruption.
This is the most simple fix: remove the device notifier call chain from the ipv6 masquerade register function and handle it only in the ipv4 version.
The better fix is merge
nf_nat_masquerade_ipv4/6_(un)register_notifier
into a single
nf_nat_masquerade_(un)register_notifiers
but to do this its needed to first merge the two masquerade modules into a single xt_MASQUERADE.
Furthermore, we need to use different refcounts for ipv4/ipv6 until we can merge MASQUERADE.
--- net/netfilter/nf_nat_masquerade.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-)
net/netfilter/nf_nat_masquerade.c +19 -16
@@ -11,7 +11,8 @@#include <net/netfilter/ipv6/nf_nat_masquerade.h> static DEFINE_MUTEX (masq_mutex); -static unsigned int masq_refcnt__read_mostly ; +static unsigned int masq_refcnt4__read_mostly ; +static unsigned int masq_refcnt6__read_mostly ; unsigned int nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum,
@@ -141,8 +142,13 @@ int nf_nat_masquerade_ipv4_register_notifier(void)int ret =0 ;mutex_lock (&masq_mutex); +if (WARN_ON_ONCE (masq_refcnt4 ==UINT_MAX )) { + ret = -EOVERFLOW ; + goto out_unlock; + } +/* check if the notifier was already set */ -if (++masq_refcnt >1 ) +if (++masq_refcnt4 >1 ) goto out_unlock;/* Register for device down reports */
@@ -160,7 +166,7 @@ int nf_nat_masquerade_ipv4_register_notifier(void) err_unregister:unregister_netdevice_notifier (&masq_dev_notifier); err_dec: - masq_refcnt--; + masq_refcnt4--; out_unlock:mutex_unlock (&masq_mutex);return ret;
@@ -171,7 +177,7 @@ void nf_nat_masquerade_ipv4_unregister_notifier(void) {mutex_lock (&masq_mutex);/* check if the notifier still has clients */ -if (--masq_refcnt >0 ) +if (--masq_refcnt4 >0 ) goto out_unlock;unregister_netdevice_notifier (&masq_dev_notifier);
@@ -321,25 +327,23 @@ int nf_nat_masquerade_ipv6_register_notifier(void)int ret =0 ;mutex_lock (&masq_mutex); -/* check if the notifier is already set */ -if (++masq_refcnt >1 ) +if (WARN_ON_ONCE (masq_refcnt6 ==UINT_MAX )) { + ret = -EOVERFLOW ; goto out_unlock; + } - ret =register_netdevice_notifier (&masq_dev_notifier); -if (ret) - goto err_dec; +/* check if the notifier is already set */ +if (++masq_refcnt6 >1 ) + goto out_unlock; ret =register_inet6addr_notifier (&masq_inet6_notifier);if (ret) - goto err_unregister; + goto err_dec;mutex_unlock (&masq_mutex);return ret; - -err_unregister: -unregister_netdevice_notifier (&masq_dev_notifier); err_dec: - masq_refcnt--; + masq_refcnt6--; out_unlock:mutex_unlock (&masq_mutex);return ret;
@@ -350,11 +354,10 @@ void nf_nat_masquerade_ipv6_unregister_notifier(void) {mutex_lock (&masq_mutex);/* check if the notifier still has clients */ -if (--masq_refcnt >0 ) +if (--masq_refcnt6 >0 ) goto out_unlock;unregister_inet6addr_notifier (&masq_inet6_notifier); -unregister_netdevice_notifier (&masq_dev_notifier); out_unlock:mutex_unlock (&masq_mutex); }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/5 netfilter: nf_tables: fix set double-free in abort path
In reply to Pablo Neira AyusoThe abort path can cause a double-free of an anonymous set. Added-and-to-be-aborted rule looks like this:
udp dport { 137, 138 } drop
The to-be-aborted transaction list looks like this:
newset
newsetelem
newsetelem
rule
This gets walked in reverse order, so first pass disables the rule, the set elements, then the set.
After synchronize_rcu(), we then destroy those in same order: rule, set element, set element, newset.
Problem is that the anonymous set has already been bound to the rule, so the rule (lookup expression destructor) already frees the set, when then cause use-after-free when trying to delete the elements from this set, then try to free the set again when handling the newset expression.
Rule releases the bound set in first place from the abort path, this causes the use-after-free on set element removal when undoing the new element transactions. To handle this, skip new element transaction if set is bound from the abort path.
This is still causes the use-after-free on set element removal. To handle this, remove transaction from the list when the set is already bound.
Joint work with Florian Westphal.
Bugzilla: https://bugzilla.netfilter.org/show_bug.cgi?id=1325
--- include/net/netfilter/nf_tables.h | 6 ++---- net/netfilter/nf_tables_api.c | 17 +++++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-)
include/net/netfilter/nf_tables.h +2 -4
@@ -416,7 +416,8 @@ struct nft_set {unsigned char *udata;/* runtime data below here */ const struct nft_set_ops *ops____cacheline_aligned ; - u16 flags:14, + u16 flags:13, + bound:1, genmask:2;u8 klen;u8 dlen;
@@ -1344,15 +1345,12 @@ struct nft_trans_rule {struct nft_trans_set {struct nft_set *set;u32 set_id; -bool bound; };#define nft_trans_set (trans) \ (((struct nft_trans_set *)trans->data)->set)#define nft_trans_set_id (trans) \ (((struct nft_trans_set *)trans->data)->set_id) -#define nft_trans_set_bound (trans) \ - (((struct nft_trans_set *)trans->data)->bound)struct nft_trans_chain {bool update;
net/netfilter/nf_tables_api.c +11 -6
@@ -142,7 +142,7 @@ static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)list_for_each_entry_reverse (trans, &net->nft.commit_list, list) {if (trans->msg_type ==NFT_MSG_NEWSET &&nft_trans_set (trans) == set) { -nft_trans_set_bound (trans) = true; + set->bound = true;break ; } }
@@ -6709,8 +6709,7 @@ static void nf_tables_abort_release(struct nft_trans *trans)nf_tables_rule_destroy (&trans->ctx,nft_trans_rule (trans));break ;case NFT_MSG_NEWSET : -if (!nft_trans_set_bound (trans)) -nft_set_destroy (nft_trans_set (trans)); +nft_set_destroy (nft_trans_set (trans));break ;case NFT_MSG_NEWSETELEM : nft_set_elem_destroy(nft_trans_elem_set(trans),
@@ -6783,8 +6782,11 @@ static int __nf_tables_abort(struct net *net)break ;case NFT_MSG_NEWSET : trans->ctx.table->use--; -if (!nft_trans_set_bound (trans)) -list_del_rcu (&nft_trans_set (trans)->list); +if (nft_trans_set (trans)->bound) { +nft_trans_destroy (trans); +break ; + } +list_del_rcu (&nft_trans_set (trans)->list);break ;case NFT_MSG_DELSET : trans->ctx.table->use++;
@@ -6792,8 +6794,11 @@ static int __nf_tables_abort(struct net *net)nft_trans_destroy (trans);break ;case NFT_MSG_NEWSETELEM : +if (nft_trans_elem_set (trans)->bound) { +nft_trans_destroy (trans); +break ; + } te = (struct nft_trans_elem *)trans->data; - te->set->ops->remove (net, te->set, &te->elem);atomic_dec (&te->set->nelems);break ;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/5 netfilter: nf_tables: bogus EBUSY when deleting set after flush
In reply to Pablo Neira AyusoSet deletion after flush coming in the same batch results in EBUSY. Add set use counter to track the number of references to this set from rules. We cannot rely on the list of bindings for this since such list is still populated from the preparation phase.
--- include/net/netfilter/nf_tables.h | 6 ++++++ net/netfilter/nf_tables_api.c | 28 +++++++++++++++++++++++++++- net/netfilter/nft_dynset.c | 13 +++++++++---- net/netfilter/nft_lookup.c | 13 +++++++++---- net/netfilter/nft_objref.c | 13 +++++++++---- 5 files changed, 60 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
@@ -382,6 +382,7 @@ void nft_unregister_set(struct nft_set_type *type);* @dtype: data type (verdict or numeric type defined by userspace) * @objtype: object type (see NFT_OBJECT_* definitions) * @size: maximum set size +* @use: number of rules references to this set * @nelems: number of elements * @ndeact: number of deactivated elements queued for removal * @timeout: default timeout value in jiffies
@@ -407,6 +408,7 @@ struct nft_set {u32 dtype;u32 objtype;u32 size; +u32 use;atomic_t nelems;u32 ndeact;u64 timeout;
@@ -467,6 +469,10 @@ struct nft_set_binding {u32 flags; }; +enum nft_trans_phase ; +void nf_tables_deactivate_set (const struct nft_ctx *ctx,struct nft_set *set, +struct nft_set_binding *binding, +enum nft_trans_phase phase);int nf_tables_bind_set (const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_binding *binding); void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
net/netfilter/nf_tables_api.c +27 -1
@@ -3672,6 +3672,9 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,static void nft_set_destroy (struct nft_set *set) { +if (WARN_ON (set->use >0 )) +return ; + set->ops->destroy (set);module_put (to_set_type (set->ops)->owner);kfree (set->name);
@@ -3712,7 +3715,7 @@ static int nf_tables_delset(struct net *net, struct sock *nlsk,NL_SET_BAD_ATTR (extack, attr);return PTR_ERR (set); } -if (!list_empty (&set->bindings) || +if (set->use || (nlh->nlmsg_flags &NLM_F_NONREC &&atomic_read (&set->nelems) >0 )) {NL_SET_BAD_ATTR (extack, attr);return -EBUSY ;
@@ -3742,6 +3745,9 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,struct nft_set_binding *i;struct nft_set_iter iter; +if (set->use ==UINT_MAX ) +return -EOVERFLOW ; +if (!list_empty (&set->bindings) &&nft_set_is_anonymous (set))return -EBUSY ;
@@ -3769,6 +3775,7 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, binding->chain = ctx->chain;list_add_tail_rcu (&binding->list, &set->bindings);nft_set_trans_bind (ctx, set); + set->use++;return 0 ; }
@@ -3788,6 +3795,25 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, }EXPORT_SYMBOL_GPL (nf_tables_unbind_set); +void nf_tables_deactivate_set (const struct nft_ctx *ctx,struct nft_set *set, +struct nft_set_binding *binding, +enum nft_trans_phase phase) +{ +switch (phase) { +case NFT_TRANS_PREPARE : + set->use--; +return ; +case NFT_TRANS_ABORT : +case NFT_TRANS_RELEASE : + set->use--; +/* fall through */ +default : +nf_tables_unbind_set (ctx, set, binding, + phase ==NFT_TRANS_COMMIT ); + } +} +EXPORT_SYMBOL_GPL (nf_tables_deactivate_set); +void nf_tables_destroy_set (const struct nft_ctx *ctx,struct nft_set *set) {if (list_empty (&set->bindings) &&nft_set_is_anonymous (set))
net/netfilter/nft_dynset.c +9 -4
@@ -240,11 +240,15 @@ static void nft_dynset_deactivate(const struct nft_ctx *ctx, {struct nft_dynset *priv =nft_expr_priv (expr); -if (phase ==NFT_TRANS_PREPARE ) -return ; +nf_tables_deactivate_set (ctx, priv->set, &priv->binding, phase); +} + +static void nft_dynset_activate (const struct nft_ctx *ctx, +const struct nft_expr *expr) +{ +struct nft_dynset *priv =nft_expr_priv (expr); -nf_tables_unbind_set (ctx, priv->set, &priv->binding, - phase ==NFT_TRANS_COMMIT ); + priv->set->use++; } static void nft_dynset_destroy(const struct nft_ctx *ctx,
@@ -292,6 +296,7 @@ static const struct nft_expr_ops nft_dynset_ops = { .eval = nft_dynset_eval, .init = nft_dynset_init, .destroy = nft_dynset_destroy, + .activate = nft_dynset_activate, .deactivate = nft_dynset_deactivate, .dump = nft_dynset_dump, };
net/netfilter/nft_lookup.c +9 -4
@@ -127,11 +127,15 @@ static void nft_lookup_deactivate(const struct nft_ctx *ctx, {struct nft_lookup *priv =nft_expr_priv (expr); -if (phase ==NFT_TRANS_PREPARE ) -return ; +nf_tables_deactivate_set (ctx, priv->set, &priv->binding, phase); +} + +static void nft_lookup_activate (const struct nft_ctx *ctx, +const struct nft_expr *expr) +{ +struct nft_lookup *priv =nft_expr_priv (expr); -nf_tables_unbind_set (ctx, priv->set, &priv->binding, - phase ==NFT_TRANS_COMMIT ); + priv->set->use++; } static void nft_lookup_destroy(const struct nft_ctx *ctx,
@@ -222,6 +226,7 @@ static const struct nft_expr_ops nft_lookup_ops = { .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)), .eval = nft_lookup_eval, .init = nft_lookup_init, + .activate = nft_lookup_activate, .deactivate = nft_lookup_deactivate, .destroy = nft_lookup_destroy, .dump = nft_lookup_dump,
net/netfilter/nft_objref.c +9 -4
@@ -162,11 +162,15 @@ static void nft_objref_map_deactivate(const struct nft_ctx *ctx, {struct nft_objref_map *priv =nft_expr_priv (expr); -if (phase ==NFT_TRANS_PREPARE ) -return ; +nf_tables_deactivate_set (ctx, priv->set, &priv->binding, phase); +} + +static void nft_objref_map_activate (const struct nft_ctx *ctx, +const struct nft_expr *expr) +{ +struct nft_objref_map *priv =nft_expr_priv (expr); -nf_tables_unbind_set (ctx, priv->set, &priv->binding, - phase ==NFT_TRANS_COMMIT ); + priv->set->use++; } static void nft_objref_map_destroy(const struct nft_ctx *ctx,
@@ -183,6 +187,7 @@ static const struct nft_expr_ops nft_objref_map_ops = { .size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)), .eval = nft_objref_map_eval, .init = nft_objref_map_init, + .activate = nft_objref_map_activate, .deactivate = nft_objref_map_deactivate, .destroy = nft_objref_map_destroy, .dump = nft_objref_map_dump,
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 4/5 netfilter: nf_tables: use-after-free in dynamic operations
In reply to Pablo Neira AyusoSmatch reports:
net/netfilter/nf_tables_api.c:2167 nf_tables_expr_destroy()
error: dereferencing freed memory 'expr->ops'net/netfilter/nf_tables_api.c
2162 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
2163 struct nft_expr *expr)
2164 {
2165 if (expr->ops->destroy)
2166 expr->ops->destroy(ctx, expr);
^^^^
--> 2167 module_put(expr->ops->type->owner);
^^^^^^^^^
2168 }Smatch says there are three functions which free expr->ops.
--- net/netfilter/nf_tables_api.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +3 -1
@@ -2162,9 +2162,11 @@ static int nf_tables_newexpr(const struct nft_ctx *ctx,static void nf_tables_expr_destroy (const struct nft_ctx *ctx,struct nft_expr *expr) { +const struct nft_expr_type *type = expr->ops->type; +if (expr->ops->destroy) expr->ops->destroy (ctx, expr); -module_put (expr->ops->type->owner); +module_put (type->owner); } struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 5/5 netfilter: nf_tables: return immediately on empty commit
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
When running 'nft flush ruleset' while no rules exist, we will increment the generation counter and announce a new genid to userspace, yet nothing had changed in the first place.
--- net/netfilter/nf_tables_api.c | 5 +++++ 1 file changed, 5 insertions(+)
net/netfilter/nf_tables_api.c +5 -0
@@ -6564,6 +6564,11 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)struct nft_chain *chain;struct nft_table *table; +if (list_empty (&net->nft.commit_list)) { +mutex_unlock (&net->nft.commit_mutex); +return 0 ; + } +/* 0. Validate ruleset, otherwise roll back for error reporting. */ if (nf_tables_validate (net) <0 )return -EAGAIN ;
Signature · 1 line
2.11.0
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Mon, 11 Mar 2019 23:50:30 +0100
Quoted 11 lines
The following patchset contains Netfilter fixes for your net tree:
1) Fix list corruption in device notifier in the masquerade infrastructure, from Florian Westphal.
2) Fix double-free of sets and use-after-free when deleting elements.
3) Don't bogusly return EBUSY when removing a set after flush command.
4) Use-after-free in dynamically allocate operations.
Quoted 7 lines
5) Don't report a new ruleset generation to userspace if transaction list is empty, this invalidates the userspace cache innecessarily. From Florian Westphal.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Pulled, thanks Pablo.