bpf_rbtree_remove() clears the node's links with RB_CLEAR_NODE() and then sets node->owner to NULL with WRITE_ONCE(), which does not order the two stores. On weakly ordered CPUs __bpf_rbtree_add() on another tree can then see owner == NULL, link the node, and have the late RB_CLEAR_NODE() store make the node its own parent, corrupting the tree. This needs verifier-accepted programs that move a refcounted node between two trees; the corruption is derived from the code and was not reproduced on hardware. Use smp_store_release(), as bpf_rb_root_free() does and as commit cb339ac61d72 ("bpf: refactor __bpf_list_del to take list node pointer") did for __bpf_list_del(). The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch. Fixes: c3c510ce431c ("bpf: Add 'owner' field to bpf_{list,rb}_node") Signed-off-by: Zhenwen Liang --- Compile-tested only (arm64, W=1). In an arm64 build the two stores are adjacent plain str instructions; smp_store_release() is a plain store on x86, so only weakly ordered architectures pay for the change. kernel/bpf/helpers.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index cebf9d8..e042cd1 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2784,7 +2784,8 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, rb_erase_cached(n, r); RB_CLEAR_NODE(n); - WRITE_ONCE(node_internal->owner, NULL); + /* Ensure __bpf_rbtree_add() sees the node as unlinked. */ + smp_store_release(&node_internal->owner, NULL); return (struct bpf_rb_node *)n; }