All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Emil Tsalapatis" <emil@etsalapatis.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 1/3] selftests/bpf: libarena: Add rbtree data structure
Date: Wed, 03 Jun 2026 18:38:41 +0000	[thread overview]
Message-ID: <20260603183841.B938C1F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260603182727.3922-2-emil@etsalapatis.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] rb_print() causes a NULL pointer dereference when called on an empty tree.
- [Medium] rb_destroy() leaks the rbtree structure if node removal fails.
--

commit 02f60ec9e8c02b47259587dc8e3bbe9137e1e4ed
Author: Emil Tsalapatis <emil@etsalapatis.com>
selftests/bpf: libarena: Add rbtree data structure

This commit adds a native red-black tree data structure to libarena. It
supports multiple APIs to query and modify the tree, using the libarena memory
allocator to manage its data.

> diff --git a/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c b/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c
> new file mode 100644
> index 0000000000000..7379eba6570af
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c

[ ... ]

> +__weak
> +int rb_destroy(struct rbtree __arena *rbtree)
> +{
> +	int ret;
> +
> +	arena_subprog_init();
> +
> +	if (rbtree->alloc == RB_NOALLOC) {

[ ... ]

> +		goto out;
> +	}
> +
> +	while (rbtree->root && can_loop) {
> +		ret = rb_remove(rbtree, rbtree->root->key);
> +		if (ret)
> +			return ret;

[Severity: Medium]
Does this leak the rbtree structure if rb_remove() fails?

If rb_remove() encounters an error (for example, due to tree corruption), it
returns early here. This skips the arena_free(rbtree) call at the out label,
potentially leaking the rbtree structure and any remaining nodes.

> +	}
> +
> +out:
> +	arena_free(rbtree);
> +	return 0;
> +}

[ ... ]

> +__weak
> +int rb_print(struct rbtree __arena *rbtree)
> +{
> +	enum rb_print_state stack[RB_MAXLVL_PRINT];
> +	struct rbnode __arena *rbnode = rbtree->root;
> +	enum rb_print_state state;
> +	struct rbnode __arena *next;
> +	u64 next_addr;
> +	u8 depth;
> +	int ret;
> +
> +	if (unlikely(!rbtree))
> +		return -EINVAL;
> +
> +	depth = 0;
> +	state = RB_NONE_VISITED;
> +
> +	arena_stderr("=== RB TREE START ===\n");
> +
> +	/* Even with can_loop, the verifier doesn't like infinite loops. */
> +	while (can_loop) {
> +		if (state == RB_NONE_VISITED)
> +			rbnode_print(depth, rbnode);

[Severity: Medium]
Does this cause a NULL pointer dereference if the tree is empty?

If a user calls rb_print() on an empty tree, rbtree->root is NULL. The loop
is entered and rb_print() passes the NULL rbnode to rbnode_print().

Looking at rbnode_print(), it unconditionally dereferences the node:

inline void rbnode_print(size_t depth, struct rbnode __arena *rbn)
{
        arena_stderr("[DEPTH %d] %p (%s)\n PARENT %p", depth, rbn,
                     rbn->is_red ? "red" : "black", rbn->parent);
        ...
}

Could this lead to a crash when calling rb_print() on an empty tree?

> +
> +		/* Find which child to traverse next. */
> +		state = rb_print_next_state(rbnode, state, &next_addr);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260603182727.3922-1-emil@etsalapatis.com?part=1

  reply	other threads:[~2026-06-03 18:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-03 18:27 [PATCH bpf-next v2 0/3] selftests/bpf: libarena: Add initial data structures Emil Tsalapatis
2026-06-03 18:27 ` [PATCH bpf-next v2 1/3] selftests/bpf: libarena: Add rbtree data structure Emil Tsalapatis
2026-06-03 18:38   ` sashiko-bot [this message]
2026-06-03 18:27 ` [PATCH bpf-next v2 2/3] selftests/bpf: libarena: Add spmc queue " Emil Tsalapatis
2026-06-03 18:46   ` sashiko-bot
2026-06-03 18:27 ` [PATCH bpf-next v2 3/3] selftests/bpf: libarena: parallel test harness and spmc parallel selftest Emil Tsalapatis
2026-06-03 18:55   ` sashiko-bot
2026-06-04 16:51 ` [PATCH bpf-next v2 0/3] selftests/bpf: libarena: Add initial data structures Alexei Starovoitov
2026-06-04 17:05   ` Emil Tsalapatis
2026-06-04 17:27     ` Alexei Starovoitov
2026-06-05  0:29       ` Emil Tsalapatis

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=20260603183841.B938C1F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.