mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: huxinyao@kylinsec.com.cn
Cc: pablo@netfilter.org, fw@strlen.de, phil@nwl.cc,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, willemb@google.com, horms@kernel.org,
	netfilter-devel@vger.kernel.org, coreteam@netfilter.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] netfilter: nft_set_rbtree: clear stale rbe_end on unexpired start element
Date: Tue, 01 Dec 2026 02:58:50 +0000	[thread overview]
Message-ID: <179609393053.3146.3884162948995523449@kernel.org> (raw)
In-Reply-To: <54EEAAE000DA2911+1796007358.199103-1-huxinyao@kylinsec.com.cn>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 2 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix is incomplete: the insert-time GC has the same flaw and
  the patch does not touch it.
- [High] The fix is also incomplete inside nft_rbtree_gc_scan().

--

netfilter: nft_set_rbtree: clear stale rbe_end on unexpired start element

This patch resets rbe_end to NULL in nft_rbtree_gc_scan() when the loop
skips an unexpired start element. The aim is to stop the scan from erasing
a live interval's end element along with a lower, expired start element
whose own end was removed separately.

> diff --git a/net/netfilter/nft_set_rbtree.c b/net/netfilter/nft_set_rbtree.c
> index 9d685899da701..2f7887e97ce1e 100644
> --- a/net/netfilter/nft_set_rbtree.c
> +++ b/net/netfilter/nft_set_rbtree.c
> @@ -911,8 +911,10 @@ static void nft_rbtree_gc_scan(struct nft_set *set)
>  			rbe_end = rbe;
>  			continue;
>  		}
> -		if (!__nft_set_elem_expired(&rbe->ext, tstamp))
> +		if (!__nft_set_elem_expired(&rbe->ext, tstamp)) {
> +			rbe_end = NULL;
>  			continue;
> +		}

[Severity: High]
Does the insert-time GC in nft_rbtree_gc_elem() need the same fix?

Its rb_prev() walk skips every node that is not an active end element.
That includes the start elements of live intervals:

net/netfilter/nft_set_rbtree.c:nft_rbtree_gc_elem() {
    ...
	while (prev) {
		rbe_prev = rb_entry(prev, struct nft_rbtree_elem, node);
		if (nft_rbtree_interval_end(rbe_prev) &&
		    nft_set_elem_active(&rbe_prev->ext, NFT_GENMASK_ANY))
			break;

		prev = rb_prev(prev);
	}

	rbe_prev = NULL;
	if (prev) {
		rbe_prev = rb_entry(prev, struct nft_rbtree_elem, node);
		nft_rbtree_gc_elem_move(net, set, priv, rbe_prev);
	}
    ...
}

Take the precondition from the commit message: start_B has expired and
end_B was removed via NFT_MSG_DELSETELEM. The tree, from high to low key,
is then:

    end_A, start_A (live), start_B (expired)

Suppose a NEWSETELEM's overlap walk in __nft_rbtree_insert() reaches
start_B. It would then do:

__nft_rbtree_insert()
    nft_rbtree_gc_elem(set, priv, start_B)
        skips start_A
        nft_rbtree_gc_elem_move(end_A)

end_A matches neither rbe_le nor rbe_ge, so the -EAGAIN branch is not
taken and the insert goes on:

			removed_end = nft_rbtree_gc_elem(set, priv, rbe);
			if (removed_end == rbe_le || removed_end == rbe_ge)
				return -EAGAIN;

At commit, nft_rbtree_commit() rebuilds the array with start_A unpaired.
There are two possible results:

  - start_A's .from slot is overwritten by the next start element, and
    interval A drops out of lookups.
  - If A is the highest interval, it is emitted with to == NULL and
    becomes open-ended.

The insert path and the commit-time scan use the same batch tstamp. So
this can happen before nft_rbtree_gc_scan() ever runs, and the change
above does not prevent it.

Should the walk in nft_rbtree_gc_elem() stop at the first active start
element without erasing anything?

[Severity: High]
Can rbe_end still go stale through the earlier inactive-element check in
this loop?

		if (!nft_set_elem_active(&rbe->ext, NFT_GENMASK_ANY))
			continue;

This path leaves rbe_end unchanged.

nft_rbtree_gc_scan() is called from nft_rbtree_commit(), which
nft_set_commit_update() calls from nf_tables_commit() before the
generation cursor flips. At that point, elements added in the current
batch still have ext->genmask = nft_genmask_cur() from nft_add_set_elem().
This check therefore skips them.

Start from end_X, start_X, end_Y, start_Y, with start_Y expired, and
consider a single batch:

  1) DELSETELEM start_X
  2) NEWSETELEM start_X' between end_Y and end_X. The overlap walk stops
     at end_Y, so the insert-path GC does not run on start_Y.
  3) DELSETELEM end_Y only. This is allowed because __nft_rbtree_insert()
     cleared start_rbe_cookie in step 2, so
     nft_rbtree_deactivate_same_interval() returns true.

At commit, nft_trans_elems_remove() erases start_X and end_Y before
set->ops->commit() is called. nft_rbtree_gc_scan() then does:

    sees end_X                -> rbe_end = end_X
    sees start_X' (pending)   -> inactive continue, rbe_end kept
    sees start_Y (expired)    -> nft_rbtree_gc_elem_move(end_X)

After the generation flip, start_X' is live, but its end element has
already been erased and queued for freeing.

Should rbe_end also be cleared when an inactive start element is skipped?
Alternatively, should the code check that rbe_end is the immediate in-tree
neighbour of rbe before collecting it?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/54EEAAE000DA2911%2B1796007358.199103-1-huxinyao%40kylinsec.com.cn

      parent reply	other threads:[~2026-12-01  2:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  2:55 Hu Xinyao
2026-11-30  2:59 ` netdev-bot+sinfo
2026-11-30  3:09   ` Hu Xinyao
2026-11-30 11:45 ` Pablo Neira Ayuso
2026-12-01  2:58 ` netdev-bot+sashiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179609393053.3146.3884162948995523449@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=coreteam@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=huxinyao@kylinsec.com.cn \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®