public inbox for linux-kernel-mentees@lists.linux-foundation.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Devaansh Kumar <devaanshk840@gmail.com>,
	mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org,
	linux-kernel@vger.kernel.org, skhan@linuxfoundation.org,
	linux-kernel-mentees@lists.linuxfoundation.org
Subject: Re: [PATCH] tracing: Replace deprecated strncpy() with memcpy() for stack_trace_filter_buf
Date: Fri, 4 Apr 2025 09:38:37 -0400	[thread overview]
Message-ID: <20250404093837.154d1239@gandalf.local.home> (raw)
In-Reply-To: <2e5aae65-316a-48c1-b293-041bfbd1ed80@efficios.com>

On Fri, 4 Apr 2025 08:54:33 -0400
Mathieu Desnoyers <mathieu.desnoyers@efficios.com> wrote:

> >>> -     if ((len = str_has_prefix(str, "_filter=")))
> >>> -             strncpy(stack_trace_filter_buf, str + len, COMMAND_LINE_SIZE);
> >>> +     len = str_has_prefix(str, "_filter=");
> >>> +
> >>> +     if (len)
> >>> +             memcpy(stack_trace_filter_buf, str + len, sizeof(stack_trace_filter_buf));  
> >>
> >> Hmm, this location looks like it can just use strscpy().  
> > 
> > Yes strscpy() also works. But since stack_trace_filter_buf is length
> > bounded, shouldn't memcpy be the right choice?  
> 
> It's not only about the destination, but also about the source length.

Correct.

> 
> AFAIU, turning a strncpy into a memcpy here will overflow reading the
> input @str if the input string is smaller than
> sizeof(stack_trace_filter_buf) + len.

The old code just read str + len and what was after it until it hit a '\0'
or the COMMAND_LINE_SIZE limit.

memcpy() always reads COMMAND_LINE_SIZE (which is sizeof(stack_trace_filter_buf))
and will read more of the source "str" than may exist. Which as Mathieu
pointed out, is a bug.

-- Steve


      parent reply	other threads:[~2025-04-04 13:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-03 19:13 [PATCH] tracing: Replace deprecated strncpy() with memcpy() for stack_trace_filter_buf Devaansh Kumar
2025-04-03 19:36 ` Steven Rostedt
2025-04-04 12:28   ` Devaansh Kumar
2025-04-04 12:54     ` Mathieu Desnoyers
2025-04-04 13:30       ` Devaansh Kumar
2025-04-04 13:38       ` Steven Rostedt [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=20250404093837.154d1239@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=devaanshk840@gmail.com \
    --cc=linux-kernel-mentees@lists.linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=skhan@linuxfoundation.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