From: Gabriele Monaco <gmonaco@redhat.com>
To: unknownbbqrx <dev@unknownbbqr.xyz>
Cc: rostedt@goodmis.org, linux-trace-kernel@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tools/rv: harden monitor name lookup bounds checks
Date: Mon, 27 Apr 2026 11:38:42 +0200 [thread overview]
Message-ID: <76fb702caa9aa7cd18deaea4fd7e2cbf32a76f44.camel@redhat.com> (raw)
In-Reply-To: <69972ccf-31ee-4906-9907-0ead76bd60b9@smtp-relay.sendinblue.com>
On Thu, 2026-04-23 at 17:44 +0300, unknownbbqrx wrote:
>
> Bound monitor-name derived copies in __ikm_find_monitor_name() and
> avoid unbounded writes from sprintf()/memcpy().
>
> Pass the output buffer size from the caller, validate extracted line
> length from rv/available_monitors, and use snprintf() with truncation
> checks when building container monitor names.
>
> Signed-off-by: unknownbbqrx <dev@unknownbbqr.xyz>
Hi,
thanks for the fix, however __ikm_find_monitor_name() is already a bit
sloppy (strstr can take any substring as a valid monitor) so I have a
patch to refactor it, which I'm about to send.
This will make your fix obsolete so I'm likely not going to take this
patch.
Thanks anyway,
Gabriele
> ---
> tools/verification/rv/src/in_kernel.c | 34 +++++++++++++++++++++----
> --
> 1 file changed, 27 insertions(+), 7 deletions(-)
>
> diff --git a/tools/verification/rv/src/in_kernel.c
> b/tools/verification/rv/src/in_kernel.c
> index d32453824..f17eac9b6 100644
> --- a/tools/verification/rv/src/in_kernel.c
> +++ b/tools/verification/rv/src/in_kernel.c
> @@ -56,9 +56,12 @@ static int __ikm_read_enable(char *monitor_name)
> * The string out_name is populated with the full name, which can be
> * equal to monitor_name or container/monitor_name if nested
> */
> -static int __ikm_find_monitor_name(char *monitor_name, char
> *out_name)
> +static int __ikm_find_monitor_name(char *monitor_name, char
> *out_name,
> + size_t out_name_size)
> {
> - char *available_monitors, container[MAX_DA_NAME_LEN+1],
> *cursor, *end;
> + char *available_monitors, container[MAX_DA_NAME_LEN + 2],
> *cursor, *end;
> + size_t len;
> + int n;
> int retval = 1;
>
> available_monitors = tracefs_instance_file_read(NULL,
> "rv/available_monitors", NULL);
> @@ -72,17 +75,34 @@ static int __ikm_find_monitor_name(char
> *monitor_name, char *out_name)
> }
>
> for (; cursor > available_monitors; cursor--)
> - if (*(cursor-1) == '\n')
> + if (*(cursor - 1) == '\n')
> break;
> +
> end = strstr(cursor, "\n");
> - memcpy(out_name, cursor, end-cursor);
> - out_name[end-cursor] = '\0';
> + if (!end) {
> + retval = -1;
> + goto out_free;
> + }
> +
> + len = end - cursor;
> + if (len >= out_name_size) {
> + retval = -1;
> + goto out_free;
> + }
> +
> + memcpy(out_name, cursor, len);
> + out_name[len] = '\0';
>
> cursor = strstr(out_name, ":");
> if (cursor)
> *cursor = '/';
> else {
> - sprintf(container, "%s:", monitor_name);
> + n = snprintf(container, sizeof(container), "%s:",
> monitor_name);
> + if (n < 0 || (size_t)n >= sizeof(container)) {
> + retval = -1;
> + goto out_free;
> + }
> +
> if (strstr(available_monitors, container))
> config_is_container = 1;
> }
> @@ -782,7 +802,7 @@ int ikm_run_monitor(char *monitor_name, int argc,
> char **argv)
> else
> nested_name = monitor_name;
>
> - retval = __ikm_find_monitor_name(monitor_name, full_name);
> + retval = __ikm_find_monitor_name(monitor_name, full_name,
> sizeof(full_name));
> if (!retval)
> return 0;
> if (retval < 0) {
>
> base-commit: 2e68039281932e6dc37718a1ea7cbb8e2cda42e6
> prerequisite-patch-id: b61dd51dee390277603975bf729a687113185c3a
prev parent reply other threads:[~2026-04-27 9:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-23 14:44 [PATCH] tools/rv: harden monitor name lookup bounds checks unknownbbqrx
2026-04-27 9:38 ` Gabriele Monaco [this message]
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=76fb702caa9aa7cd18deaea4fd7e2cbf32a76f44.camel@redhat.com \
--to=gmonaco@redhat.com \
--cc=dev@unknownbbqr.xyz \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/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