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/54EE…%40kylinsec.com.cn