netfilter: nf_tables: clone set on flush only
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nf_tables: clone set on flush only
Restrict set clone to the flush set command in the preparation phase. Add NFT_ITER_UPDATE_CLONE and use it for this purpose, update the rbtree and pipapo backends to only clone the set when this iteration type is used.
As for the existing NFT_ITER_UPDATE type, update the pipapo backend to use the existing set clone if available, otherwise use the existing set representation. After this update, there is no need to clone a set that is being deleted, this includes bound anonymous set.
An alternative approach to NFT_ITER_UPDATE_CLONE is to add a .clone interface and call it from the flush set path.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 2 +- net/netfilter/nft_set_hash.c | 1 + net/netfilter/nft_set_pipapo.c | 11 +++++++++-- net/netfilter/nft_set_rbtree.c | 8 +++++--- 5 files changed, 18 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -320,11 +320,13 @@ static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv)* @NFT_ITER_UNSPEC: unspecified, to catch errors * @NFT_ITER_READ: read-only iteration over set elements * @NFT_ITER_UPDATE: iteration under mutex to update set element state +* @NFT_ITER_UPDATE_CLONE: clone set before iteration under mutex to update element */ enum nft_iter_type {NFT_ITER_UNSPEC ,NFT_ITER_READ ,NFT_ITER_UPDATE , +NFT_ITER_UPDATE_CLONE , };struct nft_set ;
net/netfilter/nf_tables_api.c +1 -1
@@ -7906,7 +7906,7 @@ static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) {struct nft_set_iter iter = { .genmask = genmask, - .type =NFT_ITER_UPDATE , + .type =NFT_ITER_UPDATE_CLONE , .fn = nft_setelem_flush, };
net/netfilter/nft_set_hash.c +1 -0
@@ -374,6 +374,7 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set, {switch (iter->type) {case NFT_ITER_UPDATE : +case NFT_ITER_UPDATE_CLONE :/* only relevant for netlink dumps which use READ type */ WARN_ON_ONCE (iter->skip !=0 );
net/netfilter/nft_set_pipapo.c +9 -2
@@ -2145,13 +2145,20 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,const struct nft_pipapo_match *m;switch (iter->type) { -case NFT_ITER_UPDATE : +case NFT_ITER_UPDATE_CLONE : m =pipapo_maybe_clone (set);if (!m) { iter->err = -ENOMEM ;return ; } - +nft_pipapo_do_walk (ctx, set, m, iter); +break ; +case NFT_ITER_UPDATE : +if (priv->clone) + m = priv->clone; +else + m =rcu_dereference_protected (priv->match, +nft_pipapo_transaction_mutex_held (set));nft_pipapo_do_walk (ctx, set, m, iter);break ;case NFT_ITER_READ :
net/netfilter/nft_set_rbtree.c +5 -3
@@ -861,13 +861,15 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,struct nft_rbtree *priv =nft_set_priv (set);switch (iter->type) { -case NFT_ITER_UPDATE : -lockdep_assert_held (&nft_pernet (ctx->net)->commit_mutex); - +case NFT_ITER_UPDATE_CLONE :if (nft_array_may_resize (set) <0 ) { iter->err = -ENOMEM ;break ; } + fallthrough; +case NFT_ITER_UPDATE : +lockdep_assert_held (&nft_pernet (ctx->net)->commit_mutex); +nft_rbtree_do_walk (ctx, set, iter);break ;case NFT_ITER_READ :
Signature · 1 line
2.47.3
Florian Westphal
maintainerQuoted 9 lines · Pablo Neira Ayuso
Restrict set clone to the flush set command in the preparation phase. Add
NFT_ITER_UPDATE_CLONEand use it for this purpose, update the rbtree and pipapo backends to only clone the set when this iteration type is used.As for the existing
NFT_ITER_UPDATEtype, update the pipapo backend to use the existing set clone if available, otherwise use the existing set representation. After this update, there is no need to clone a set that is being deleted, this includes bound anonymous set.
Thanks Pablo, this looks good to me. Two small nits/suggestions:
1. Add to commit message that this affects fault injection resp. that this requires failing GFP_KERNEL allocation to trigger the WARN splat.
net/netfilter/nf_tables_api.c static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) · line 7911
Quoted 13 lines
struct
nft_set;net/netfilter/nf_tables_api.c +1
-1@@ -7906,7 +7906,7 @@ static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) {struct nft_set_iter iter = { .genmask = genmask, - .type =NFT_ITER_UPDATE , + .type =NFT_ITER_UPDATE_CLONE , .fn = nft_setelem_flush, };
2. I think it would help to add a comment to the existing NFT_ITER_UPDATE users as to why they use UPDATE and not CLONE.
Or, alternatively, add comment here why this one needs the _CLONE variant.
But this looks correct to me, nft_set_flush() makes clone mandatory while others either can re-use the live copy OR are guaranteed to have a clone, e.g. because the function is always called after a delete/insert operation that already did the clone.