Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Gabriele Monaco" <gmonaco@redhat.com>
Cc: linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH v2 08/15] tools/rv: Implement BPF monitor loading and tracing
Date: Thu, 01 Oct 2026 15:41:45 +0000	[thread overview]
Message-ID: <20261001154145.924D61F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20261001152042.124445-9-gmonaco@redhat.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] bpf: path traversal allows loading arbitrary BPF objects
- [Medium] bpf: missing validation of required BTF fields leads to out-of-bounds read
- [Medium] bpf: missing data_sz bounds checks in handle_event leads to out-of-bounds read
- [Medium] bpf: type punning in read_field violates strict aliasing
--

commit 91a3dbdeaaa49f37b21710db6d56a6658ad6fe6b
Author: Gabriele Monaco <gmonaco@redhat.com>

tools/rv: Implement BPF monitor loading and tracing

This commit implements BPF monitor loading, execution, and optional tracing
functionality in the rv tool. It parses event and state names from BTF and
hooks into the ring buffer for output formatting.

> diff --git a/tools/verification/rv/src/bpf_monitor.c b/tools/verification/rv/src/bpf_monitor.c
> --- a/tools/verification/rv/src/bpf_monitor.c
> +++ b/tools/verification/rv/src/bpf_monitor.c

[ ... ]

> 		for (int j = 0; j < FIELD_MAX; j++) {
> 			if (strcmp(name, field_names[j]) == 0) {
> 				ctx->field_metadata[j].offset = offset;
> 				ctx->field_metadata[j].size = size;
> 				if (j == FIELD_ID)
> 					config.has_id = true;
> 				break;
> 			}
> 		}
> 	}
> 
> 	return 0;
> }

[Severity: Medium]
Is there a need to verify that all required fields were found after this
loop?

If the BTF data does not contain a required field (like comm), its offset
in ctx->field_metadata remains 0. Later, handle_event() relies on this
offset unconditionally, which would point to the start of the payload
and result in an out-of-bounds read.

[ ... ]

> static inline uint64_t read_field(uint64_t *entry, enum field_id id,
> 				  const uint8_t *raw,
> 				  const struct bpf_monitor_ctx *ctx)
> {
> 	const struct field *field = &ctx->field_metadata[id];
> 
> 	switch (field->size) {
> 	case 1:
> 		return entry[id] = *(const uint8_t *)(raw + field->offset);
> 	case 2:
> 		return entry[id] = *(const uint16_t *)(raw + field->offset);
> 	case 4:
> 		return entry[id] = *(const uint32_t *)(raw + field->offset);

[Severity: Medium]
Does casting a const uint8_t * byte pointer to larger scalar types like
uint32_t * violate strict aliasing rules?

The tools directory is compiled with -fstrict-aliasing enabled, so this type
punning can result in undefined behavior and miscompilation.

Also, is it possible for the offset + size to exceed the data_sz provided
by the ring buffer? read_field() dereferences memory based purely on
BTF offsets without validating that the access fits within the actual
payload size.

[ ... ]

> static int handle_event(void *ctx, void *data, size_t data_sz)
> {
> 	struct bpf_monitor_ctx *mon_ctx = ctx;
> 	const uint8_t *raw = data;

[Severity: Medium]
Is the data_sz parameter ignored in this function?

If a BPF program submits a truncated event to the ring buffer, the missing
bounds checks in this function and read_field() could lead to out-of-bounds
memory reads.

[ ... ]

> 	comm = (const char *)(raw + mon_ctx->field_metadata[FIELD_COMM].offset);
> 	read_field(entry, FIELD_CURR_STATE, raw, mon_ctx);
> 	read_field(entry, FIELD_EVENT, raw, mon_ctx);
> 
> 	printf("%16s-%-8"PRIu64" [%.3"PRIu64"] ", comm, entry[FIELD_PID], entry[FIELD_CPU]);

[Severity: Medium]
Could this printf() format specifier lead to unbounded memory reads?

Using %16s instead of %.16s allows printf to read indefinitely until it
encounters a null byte. If the string is not null-terminated or was absent,
this might read beyond data_sz.

[ ... ]

> static int find_bpf_file(const char *subdir, const char *name, char *path_out, size_t path_len)
> {
> 	char path[MAX_PATH];
> 
> 	for (int i = 0; bpf_base_paths[i][0]; i++) {
> 		size_t size = snprintf(path, sizeof(path), "%s/%s/%s.o",
> 				       bpf_base_paths[i], subdir, name);
> 
> 		if (size < MAX_PATH && access(path, R_OK) == 0) {

[Severity: High]
Can a malicious user supply a name containing path traversal characters like
../../../ to load arbitrary object files?

The user-controlled monitor_name is passed to find_bpf_file() and blindly
appended to the path string. If an administrator configures sudo to allow
running rv mon, an attacker could exploit this traversal to load their
own BPF program and gain arbitrary kernel code execution.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261001152042.124445-1-gmonaco@redhat.com?part=8

  reply	other threads:[~2026-10-01 15:41 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-01 15:20 [PATCH v2 00/15] rv: Add support for BPF monitors Gabriele Monaco
2026-10-01 15:20 ` [PATCH v2 01/15] sched: Add task enqueue/dequeue trace points Gabriele Monaco
2026-10-01 15:49   ` Peter Zijlstra
2026-10-02  7:09     ` Gabriele Monaco
2026-10-02 10:29       ` Peter Zijlstra
2026-10-02 11:55         ` Gabriele Monaco
2026-10-02 19:18           ` Peter Zijlstra
2026-10-02 19:40             ` Gabriele Monaco
2026-10-04  7:57             ` Steven Rostedt
2026-10-04  7:45         ` Steven Rostedt
2026-10-02  0:42   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 02/15] tools/rv: Skip empty pid error in selftest if command failed Gabriele Monaco
2026-10-02  0:42   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 03/15] rv: Refactor da_trace() functions to get strings internally Gabriele Monaco
2026-10-01 15:20 ` [PATCH v2 04/15] rv: Cast result of model_get_*_name() Gabriele Monaco
2026-10-01 15:20 ` [PATCH v2 05/15] tools/rv: Move argument parsing from in_kernel to utils Gabriele Monaco
2026-10-02  0:25   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 06/15] tools/build: Add a feature test for bpftool-btf Gabriele Monaco
2026-10-01 15:20 ` [PATCH v2 07/15] tools/rv: Implement BPF monitor discovery and listing Gabriele Monaco
2026-10-01 15:40   ` sashiko-bot
2026-10-02  0:42   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 08/15] tools/rv: Implement BPF monitor loading and tracing Gabriele Monaco
2026-10-01 15:41   ` sashiko-bot [this message]
2026-10-02  0:43   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 09/15] tools/rv: Copy stripped bpf_atomic.h from libarena Gabriele Monaco
2026-10-02  0:42   ` bot+bpf-ci
2026-10-07 12:59   ` Nam Cao
2026-10-08  9:00     ` Gabriele Monaco
2026-10-08 11:16       ` Nam Cao
2026-10-01 15:20 ` [PATCH v2 10/15] tools/rv: Add BPF monitors Gabriele Monaco
2026-10-01 15:55   ` sashiko-bot
2026-10-02  0:43   ` bot+bpf-ci
2026-10-06 13:29   ` Alexei Starovoitov
2026-10-08  9:54     ` Gabriele Monaco
2026-10-01 15:20 ` [PATCH v2 11/15] tools/rv: Define CONFIG_X86_64 statically for " Gabriele Monaco
2026-10-01 15:49   ` sashiko-bot
2026-10-01 15:20 ` [PATCH v2 12/15] tools/rv: Add reactors support to " Gabriele Monaco
2026-10-01 15:57   ` sashiko-bot
2026-10-02  0:43   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 13/15] verification/rvgen: Add support for " Gabriele Monaco
2026-10-02  0:25   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 14/15] tools/rv: Add selftest for rv bpf monitors Gabriele Monaco
2026-10-01 16:03   ` sashiko-bot
2026-10-02  0:43   ` bot+bpf-ci
2026-10-01 15:20 ` [PATCH v2 15/15] verification/rvgen: Add selftest for rvgen -b Gabriele Monaco

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=20261001154145.924D61F000FF@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