public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	kernel test robot <lkp@intel.com>
Subject: [for-next][PATCH 03/12] tracing: Fix last_cmd_set() string management in histogram code
Date: Sat, 12 Mar 2022 18:25:28 -0500	[thread overview]
Message-ID: <20220312232550.431988486@goodmis.org> (raw)
In-Reply-To: 20220312232525.234705244@goodmis.org

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Using strnlen(dest, str, n) is confusing, as the size of dest must be
strlen(dest) + n + 1. Even more confusing, using sizeof(string constant)
gives you strlen(string constant) + 1 and not just strlen().

These two together made using strncat() with a constant string a bit off
in the calculations as we have:

	len = sizeof(HIST_PREFIX) + strlen(str) + 1;
	kfree(last_cmd);
	last_cmd = kzalloc(len, GFP_KERNEL);
	strcpy(last_cmd, HIST_PREFIX);
	len -= sizeof(HIST_PREFIX) + 1;
	strncat(last_cmd, str, len);

The above works if we s/sizeof/strlen/ with HIST_PREFIX (which is defined
as "hist:", but because sizeof(HIST_PREFIX) is equal to
strlen(HIST_PREFIX) + 1, we can drop the +1 in the code. But at least
comment that we are doing so.

Link: https://lore.kernel.org/all/202203082112.Iu7tvFl4-lkp@intel.com/

Fixes: 9f8e5aee93ed2 ("tracing: Fix allocation of last_cmd in last_cmd_set()")
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/trace_events_hist.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index 78788049f3d3..954b19e2f196 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -749,14 +749,16 @@ static void last_cmd_set(struct trace_event_file *file, char *str)
 	if (!str)
 		return;
 
-	len = sizeof(HIST_PREFIX) + strlen(str) + 1;
+	/* sizeof() contains the nul byte */
+	len = sizeof(HIST_PREFIX) + strlen(str);
 	kfree(last_cmd);
 	last_cmd = kzalloc(len, GFP_KERNEL);
 	if (!last_cmd)
 		return;
 
 	strcpy(last_cmd, HIST_PREFIX);
-	len -= sizeof(HIST_PREFIX) + 1;
+	/* Again, sizeof() contains the nul byte */
+	len -= sizeof(HIST_PREFIX);
 	strncat(last_cmd, str, len);
 
 	if (file) {
-- 
2.35.1

  parent reply	other threads:[~2022-03-12 23:26 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-12 23:25 [for-next][PATCH 00/12] tracing: Updates for v5.18 Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 01/12] tracing: Fix allocation of last_cmd in last_cmd_set() Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 02/12] user_events: Fix potential uninitialized pointer while parsing field Steven Rostedt
2022-03-12 23:25 ` Steven Rostedt [this message]
2022-03-12 23:25 ` [for-next][PATCH 04/12] tracing: Allow custom events to be added to the tracefs directory Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 05/12] tracing: Add sample code for custom trace events Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 06/12] tracing: Move the defines to create TRACE_EVENTS into their own files Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 07/12] tracing: Add TRACE_CUSTOM_EVENT() macro Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 08/12] user_events: Prevent dyn_event delete racing with ioctl add/delete Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 09/12] tracing: Fix strncpy warning in trace_events_synth.c Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 10/12] tracing: Have TRACE_DEFINE_ENUM affect trace event types as well Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 11/12] tracing: Add snapshot at end of kernel boot up Steven Rostedt
2022-03-12 23:25 ` [for-next][PATCH 12/12] tracing/user_events: Use alloc_pages instead of kzalloc() for register pages Steven Rostedt

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=20220312232550.431988486@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=mingo@kernel.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