Skip to content

Commit 2413893

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nf_tables: don't skip expired elements during walk
There is an asymmetry between commit/abort and preparation phase if the following conditions are met: 1. set is a verdict map ("1.2.3.4 : jump foo") 2. timeouts are enabled In this case, following sequence is problematic: 1. element E in set S refers to chain C 2. userspace requests removal of set S 3. kernel does a set walk to decrement chain->use count for all elements from preparation phase 4. kernel does another set walk to remove elements from the commit phase (or another walk to do a chain->use increment for all elements from abort phase) If E has already expired in 1), it will be ignored during list walk, so its use count won't have been changed. Then, when set is culled, ->destroy callback will zap the element via nf_tables_set_elem_destroy(), but this function is only safe for elements that have been deactivated earlier from the preparation phase: lack of earlier deactivate removes the element but leaks the chain use count, which results in a WARN splat when the chain gets removed later, plus a leak of the nft_chain structure. Update pipapo_get() not to skip expired elements, otherwise flush command reports bogus ENOENT errors. Fixes: 3c4287f ("nf_tables: Add set type for arbitrary concatenation of ranges") Fixes: 8d8540c ("netfilter: nft_set_rbtree: add timeout support") Fixes: 9d09829 ("netfilter: nft_hash: add support for timeouts") Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent c5ccff7 commit 2413893

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

net/netfilter/nf_tables_api.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5602,8 +5602,12 @@ static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
56025602
const struct nft_set_iter *iter,
56035603
struct nft_set_elem *elem)
56045604
{
5605+
const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
56055606
struct nft_set_dump_args *args;
56065607

5608+
if (nft_set_elem_expired(ext))
5609+
return 0;
5610+
56075611
args = container_of(iter, struct nft_set_dump_args, iter);
56085612
return nf_tables_fill_setelem(args->skb, set, elem, args->reset);
56095613
}

net/netfilter/nft_set_hash.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
278278

279279
if (iter->count < iter->skip)
280280
goto cont;
281-
if (nft_set_elem_expired(&he->ext))
282-
goto cont;
283281
if (!nft_set_elem_active(&he->ext, iter->genmask))
284282
goto cont;
285283

net/netfilter/nft_set_pipapo.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,7 @@ static struct nft_pipapo_elem *pipapo_get(const struct net *net,
566566
goto out;
567567

568568
if (last) {
569-
if (nft_set_elem_expired(&f->mt[b].e->ext) ||
570-
(genmask &&
569+
if ((genmask &&
571570
!nft_set_elem_active(&f->mt[b].e->ext, genmask)))
572571
goto next_match;
573572

@@ -601,8 +600,17 @@ static struct nft_pipapo_elem *pipapo_get(const struct net *net,
601600
static void *nft_pipapo_get(const struct net *net, const struct nft_set *set,
602601
const struct nft_set_elem *elem, unsigned int flags)
603602
{
604-
return pipapo_get(net, set, (const u8 *)elem->key.val.data,
605-
nft_genmask_cur(net));
603+
struct nft_pipapo_elem *ret;
604+
605+
ret = pipapo_get(net, set, (const u8 *)elem->key.val.data,
606+
nft_genmask_cur(net));
607+
if (IS_ERR(ret))
608+
return ret;
609+
610+
if (nft_set_elem_expired(&ret->ext))
611+
return ERR_PTR(-ENOENT);
612+
613+
return ret;
606614
}
607615

608616
/**
@@ -2005,8 +2013,6 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,
20052013
goto cont;
20062014

20072015
e = f->mt[r].e;
2008-
if (nft_set_elem_expired(&e->ext))
2009-
goto cont;
20102016

20112017
elem.priv = e;
20122018

net/netfilter/nft_set_rbtree.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,6 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,
552552

553553
if (iter->count < iter->skip)
554554
goto cont;
555-
if (nft_set_elem_expired(&rbe->ext))
556-
goto cont;
557555
if (!nft_set_elem_active(&rbe->ext, iter->genmask))
558556
goto cont;
559557

0 commit comments

Comments
 (0)