Commit 273fe3f1 authored by Pablo Neira Ayuso's avatar Pablo Neira Ayuso

netfilter: nf_tables: bogus EBUSY when deleting set after flush

Set 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.
Reported-by: default avatarVáclav Zindulka <vaclav.zindulka@tlapnet.cz>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent 40ba1d9b
...@@ -382,6 +382,7 @@ void nft_unregister_set(struct nft_set_type *type); ...@@ -382,6 +382,7 @@ void nft_unregister_set(struct nft_set_type *type);
* @dtype: data type (verdict or numeric type defined by userspace) * @dtype: data type (verdict or numeric type defined by userspace)
* @objtype: object type (see NFT_OBJECT_* definitions) * @objtype: object type (see NFT_OBJECT_* definitions)
* @size: maximum set size * @size: maximum set size
* @use: number of rules references to this set
* @nelems: number of elements * @nelems: number of elements
* @ndeact: number of deactivated elements queued for removal * @ndeact: number of deactivated elements queued for removal
* @timeout: default timeout value in jiffies * @timeout: default timeout value in jiffies
...@@ -407,6 +408,7 @@ struct nft_set { ...@@ -407,6 +408,7 @@ struct nft_set {
u32 dtype; u32 dtype;
u32 objtype; u32 objtype;
u32 size; u32 size;
u32 use;
atomic_t nelems; atomic_t nelems;
u32 ndeact; u32 ndeact;
u64 timeout; u64 timeout;
...@@ -467,6 +469,10 @@ struct nft_set_binding { ...@@ -467,6 +469,10 @@ struct nft_set_binding {
u32 flags; 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, int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
struct nft_set_binding *binding); struct nft_set_binding *binding);
void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
......
...@@ -3672,6 +3672,9 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk, ...@@ -3672,6 +3672,9 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,
static void nft_set_destroy(struct nft_set *set) static void nft_set_destroy(struct nft_set *set)
{ {
if (WARN_ON(set->use > 0))
return;
set->ops->destroy(set); set->ops->destroy(set);
module_put(to_set_type(set->ops)->owner); module_put(to_set_type(set->ops)->owner);
kfree(set->name); kfree(set->name);
...@@ -3712,7 +3715,7 @@ static int nf_tables_delset(struct net *net, struct sock *nlsk, ...@@ -3712,7 +3715,7 @@ static int nf_tables_delset(struct net *net, struct sock *nlsk,
NL_SET_BAD_ATTR(extack, attr); NL_SET_BAD_ATTR(extack, attr);
return PTR_ERR(set); return PTR_ERR(set);
} }
if (!list_empty(&set->bindings) || if (set->use ||
(nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) { (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) {
NL_SET_BAD_ATTR(extack, attr); NL_SET_BAD_ATTR(extack, attr);
return -EBUSY; return -EBUSY;
...@@ -3742,6 +3745,9 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, ...@@ -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_binding *i;
struct nft_set_iter iter; struct nft_set_iter iter;
if (set->use == UINT_MAX)
return -EOVERFLOW;
if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
return -EBUSY; return -EBUSY;
...@@ -3769,6 +3775,7 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, ...@@ -3769,6 +3775,7 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
binding->chain = ctx->chain; binding->chain = ctx->chain;
list_add_tail_rcu(&binding->list, &set->bindings); list_add_tail_rcu(&binding->list, &set->bindings);
nft_set_trans_bind(ctx, set); nft_set_trans_bind(ctx, set);
set->use++;
return 0; return 0;
} }
...@@ -3788,6 +3795,25 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, ...@@ -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); 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) void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
{ {
if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
......
...@@ -240,11 +240,15 @@ static void nft_dynset_deactivate(const struct nft_ctx *ctx, ...@@ -240,11 +240,15 @@ static void nft_dynset_deactivate(const struct nft_ctx *ctx,
{ {
struct nft_dynset *priv = nft_expr_priv(expr); struct nft_dynset *priv = nft_expr_priv(expr);
if (phase == NFT_TRANS_PREPARE) nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
return; }
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, priv->set->use++;
phase == NFT_TRANS_COMMIT);
} }
static void nft_dynset_destroy(const struct nft_ctx *ctx, static void nft_dynset_destroy(const struct nft_ctx *ctx,
...@@ -292,6 +296,7 @@ static const struct nft_expr_ops nft_dynset_ops = { ...@@ -292,6 +296,7 @@ static const struct nft_expr_ops nft_dynset_ops = {
.eval = nft_dynset_eval, .eval = nft_dynset_eval,
.init = nft_dynset_init, .init = nft_dynset_init,
.destroy = nft_dynset_destroy, .destroy = nft_dynset_destroy,
.activate = nft_dynset_activate,
.deactivate = nft_dynset_deactivate, .deactivate = nft_dynset_deactivate,
.dump = nft_dynset_dump, .dump = nft_dynset_dump,
}; };
......
...@@ -127,11 +127,15 @@ static void nft_lookup_deactivate(const struct nft_ctx *ctx, ...@@ -127,11 +127,15 @@ static void nft_lookup_deactivate(const struct nft_ctx *ctx,
{ {
struct nft_lookup *priv = nft_expr_priv(expr); struct nft_lookup *priv = nft_expr_priv(expr);
if (phase == NFT_TRANS_PREPARE) nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
return; }
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, priv->set->use++;
phase == NFT_TRANS_COMMIT);
} }
static void nft_lookup_destroy(const struct nft_ctx *ctx, static void nft_lookup_destroy(const struct nft_ctx *ctx,
...@@ -222,6 +226,7 @@ static const struct nft_expr_ops nft_lookup_ops = { ...@@ -222,6 +226,7 @@ static const struct nft_expr_ops nft_lookup_ops = {
.size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)), .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
.eval = nft_lookup_eval, .eval = nft_lookup_eval,
.init = nft_lookup_init, .init = nft_lookup_init,
.activate = nft_lookup_activate,
.deactivate = nft_lookup_deactivate, .deactivate = nft_lookup_deactivate,
.destroy = nft_lookup_destroy, .destroy = nft_lookup_destroy,
.dump = nft_lookup_dump, .dump = nft_lookup_dump,
......
...@@ -162,11 +162,15 @@ static void nft_objref_map_deactivate(const struct nft_ctx *ctx, ...@@ -162,11 +162,15 @@ static void nft_objref_map_deactivate(const struct nft_ctx *ctx,
{ {
struct nft_objref_map *priv = nft_expr_priv(expr); struct nft_objref_map *priv = nft_expr_priv(expr);
if (phase == NFT_TRANS_PREPARE) nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
return; }
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, priv->set->use++;
phase == NFT_TRANS_COMMIT);
} }
static void nft_objref_map_destroy(const struct nft_ctx *ctx, 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 = { ...@@ -183,6 +187,7 @@ static const struct nft_expr_ops nft_objref_map_ops = {
.size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)), .size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)),
.eval = nft_objref_map_eval, .eval = nft_objref_map_eval,
.init = nft_objref_map_init, .init = nft_objref_map_init,
.activate = nft_objref_map_activate,
.deactivate = nft_objref_map_deactivate, .deactivate = nft_objref_map_deactivate,
.destroy = nft_objref_map_destroy, .destroy = nft_objref_map_destroy,
.dump = nft_objref_map_dump, .dump = nft_objref_map_dump,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment