From: sashiko-bot@kernel.org
To: "Gabriele Monaco" <gmonaco@redhat.com>
Cc: linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [RFC PATCH 13/20] tools/rv: Implement BPF monitor loading and tracing
Date: Mon, 31 Aug 2026 09:34:16 +0000 [thread overview]
Message-ID: <20260831093416.E17981F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260831090524.106845-14-gmonaco@redhat.com>
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Medium] A failure to reset one BPF map aborts the reset process for all subsequent maps.
- [Medium] Memory and file descriptor leak when `bpf_object__load` fails.
- [Medium] Reused BPF links are not unpinned during cleanup, leaving their BPF programs permanently attached to the kernel.
- [Low] Calling `get_state_name` twice in the same printf argument list causes both fields to print the same value if BTF lookup fails.
- [Low] `extract_enum_names` fails to strip the monitor name suffix if the monitor name contains an underscore.
- [Low] Missing bounds check on `data_sz` in `handle_event` can lead to out-of-bounds memory reads.
--
commit 18d2a63fa7007876f35f76e7406322321e0a0eb2
Author: Gabriele Monaco <gmonaco@redhat.com>
tools/rv: Implement BPF monitor loading and tracing
Implement BPF monitor loading, execution, and optional tracing
functionality.
> diff --git a/tools/verification/rv/src/bpf_monitor.c b/tools/verification/rv/src/bpf_monitor.c
> new file mode 100644
> index 0000000000000..e13ae35678cb8
> --- /dev/null
> +++ b/tools/verification/rv/src/bpf_monitor.c
[ ... ]
> +static int handle_event(void *ctx, void *data, size_t data_sz)
> +{
[Severity: Low]
Should this callback validate that data_sz is large enough to cover the
fields accessed by read_field()?
Without verifying that the BTF metadata offsets fall within the provided
data_sz, could a truncated or malformed ring buffer event lead to an
out-of-bounds memory read?
[ ... ]
> + if (entry[FIELD_EVENT_TYPE] == RV_TRACE_ERROR) {
> + printf("error ");
> + if (config.has_id)
> + printf(" %8"PRIu64"", entry[FIELD_ID]);
> + printf(" %24s x %-24s\n",
> + get_state_name(mon_ctx, entry[FIELD_CURR_STATE]),
> + get_event_name(mon_ctx, entry[FIELD_EVENT]));
> + } else {
> + printf("event ");
> + read_field(entry, FIELD_IS_FINAL, raw, mon_ctx);
> + read_field(entry, FIELD_NEXT_STATE, raw, mon_ctx);
> +
> + if (config.has_id)
> + printf(" %8"PRIu64"", entry[FIELD_ID]);
> + printf(" %24s x %-24s -> %-24s %c\n",
> + get_state_name(mon_ctx, entry[FIELD_CURR_STATE]),
> + get_event_name(mon_ctx, entry[FIELD_EVENT]),
> + get_state_name(mon_ctx, entry[FIELD_NEXT_STATE]),
> + entry[FIELD_IS_FINAL] ? 'Y' : 'N');
[Severity: Low]
Since get_state_name() returns a pointer to a single static buffer when
the state ID is not found, will calling it twice in the same printf
argument list cause both fields to print the exact same string as the
buffer is overwritten?
> + }
> +
> + return 0;
> +}
[ ... ]
> +static int extract_enum_names(const struct btf *btf, const char *enum_kind,
> + char dest[][MAX_DA_NAME_LEN], struct bpf_monitor_ctx *ctx)
> +{
[ ... ]
> + padding = strrchr(name, '_');
> + name_len = strlen(name);
> + if (padding && !strcmp(ctx->monitor_name, padding + 1))
> + name_len = (size_t)(padding - name);
[Severity: Low]
Could this fail to strip the suffix if the monitor name itself contains
an underscore?
Since strrchr() finds the last occurrence of '_', if ctx->monitor_name
is something like my_monitor, padding + 1 will evaluate to monitor,
causing the strcmp check to fail.
[ ... ]
> +static void reset_monitor_maps(struct bpf_object *obj)
> +{
> + struct bpf_map *map;
> + bool err = false;
> +
> + bpf_object__for_each_map(map, obj) {
[ ... ]
> + case BPF_MAP_TYPE_HASH:
> + case BPF_MAP_TYPE_PERCPU_HASH: {
> + void *key = malloc(bpf_map__key_size(map));
> +
> + if (!key) {
> + err = true;
> + break;
> + }
> +
> + while (!err && bpf_map_get_next_key(fd, NULL, key) == 0)
> + err |= bpf_map_delete_elem(fd, key);
[Severity: Medium]
Since the err flag is declared outside the loop and not reset to false
per map, does a failure to clear one map cause the while loop to be
skipped for all subsequent maps? Stale data might be left behind,
potentially causing incorrect monitor behavior.
[ ... ]
> +static struct bpf_object *open_bpf_monitor(const char *monitor_name, const char *path)
> +{
[ ... ]
> + obj = bpf_object__open_file(path, &opts);
> + if (!obj) {
> + err_msg("bpf: error opening object file: %s\n", strerror(errno));
> + return NULL;
> + }
> +
> + res = bpf_object__load(obj);
> + if (res) {
> + err_msg("bpf: error loading object file: %s\n", strerror(-res));
> + return NULL;
> + }
[Severity: Medium]
If bpf_object__load() fails, should this error path call
bpf_object__close() to prevent leaking the bpf_object and its open
file descriptors?
[ ... ]
> +static int attach_bpf_handlers(const char *monitor_name, struct bpf_object *obj,
> + struct bpf_link **links, int *link_count)
> +{
[ ... ]
> + if (access(pin_path, F_OK) == 0)
> + link = bpf_link__open(pin_path);
[ ... ]
> +int bpf_run_monitor(char *monitor_name, int argc, char **argv)
> +{
[ ... ]
> + for (int i = 0; i < link_count; i++) {
> + bpf_link__unpin(links[i]);
[Severity: Medium]
If a link is reused and reopened via bpf_link__open(), will
bpf_link__unpin() fail during cleanup?
Libbpf does not populate the internal pin_path property for reopened
links, which means the pin file could remain on the filesystem and the
BPF program might stay attached to the kernel tracepoint indefinitely
after the tool exits.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260831090524.106845-1-gmonaco@redhat.com?part=13
next prev parent reply other threads:[~2026-08-31 9:34 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-31 9:05 [RFC PATCH 00/20] rv: Add support for BPF monitors Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 01/20] sched: Add task enqueue/dequeue trace points Gabriele Monaco
2026-08-31 9:32 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 02/20] tools/rv: Skip empty pid error in selftest if command failed Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 03/20] rv: Refactor da_trace() functions to get strings internally Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 04/20] rv: Use static arrays for rv_monitor name and description Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 05/20] rv: Add in-kernel support for BPF monitors Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 06/20] rv: Add rv_get_monitor_by_name() Gabriele Monaco
2026-08-31 9:24 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 07/20] rv: Add reactors support to BPF monitors Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 08/20] rv: Cast result of model_get_*_name() Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 09/20] rv: Handle unregistered monitors safely in tracefs Gabriele Monaco
2026-08-31 9:24 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 10/20] tools/build: Add a feature test for bpftool-btf Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 11/20] tools/rv: Move argument parsing from in_kernel to utils Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 12/20] tools/rv: Export functionality for in_kernel monitors Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 13/20] tools/rv: Implement BPF monitor loading and tracing Gabriele Monaco
2026-08-31 9:34 ` sashiko-bot [this message]
2026-08-31 9:05 ` [RFC PATCH 14/20] tools/rv: Implement BPF monitor registration logic Gabriele Monaco
2026-08-31 9:38 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 15/20] tools/rv: Copy stripped bpf_atomic.h from libarena Gabriele Monaco
2026-08-31 9:38 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 16/20] tools/rv: Add BPF monitors Gabriele Monaco
2026-08-31 9:40 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 17/20] tools/rv: Define CONFIG_X86_64 statically for " Gabriele Monaco
2026-08-31 9:05 ` [RFC PATCH 18/20] verification/rvgen: Add support " Gabriele Monaco
2026-08-31 9:41 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 19/20] tools/rv: Add selftest for rv bpf Gabriele Monaco
2026-08-31 9:44 ` sashiko-bot
2026-08-31 9:05 ` [RFC PATCH 20/20] verification/rvgen: Add selftest for rvgen -b Gabriele Monaco
2026-09-01 18:35 ` [RFC PATCH 00/20] rv: Add support for BPF monitors Nam Cao
2026-09-02 6:52 ` Gabriele Monaco
2026-09-02 7:46 ` Nam Cao
2026-09-03 1:57 ` Alexei Starovoitov
2026-09-03 7:21 ` Gabriele Monaco
2026-09-03 13:02 ` Steven Rostedt
2026-09-04 3:30 ` Alexei Starovoitov
2026-09-04 11:43 ` Steven Rostedt
2026-09-04 16:16 ` Alexei Starovoitov
2026-09-04 16:31 ` Steven Rostedt
2026-09-04 17:24 ` Steven Rostedt
2026-09-04 11:23 ` Tomas Glozar
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=20260831093416.E17981F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=gmonaco@redhat.com \
--cc=linux-trace-kernel@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox