Linux Trace Kernel
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Jeongjun Park <aha310510@gmail.com>
Cc: mhiramat@kernel.org, linux-kernel@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org,
	syzbot+c8cd2d2c412b868263fb@syzkaller.appspotmail.com
Subject: Re: [PATCH] tracing: fix oob write in trace_seq_to_buffer()
Date: Mon, 21 Apr 2025 10:19:12 -0400	[thread overview]
Message-ID: <20250421101912.4fd7c11a@gandalf.local.home> (raw)
In-Reply-To: <20250421134936.89104-1-aha310510@gmail.com>

On Mon, 21 Apr 2025 22:49:36 +0900
Jeongjun Park <aha310510@gmail.com> wrote:

> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 8ddf6b17215c..8ba6ea38411d 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -1827,6 +1827,8 @@ static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
>  	len = trace_seq_used(s) - s->readpos;
>  	if (cnt > len)
>  		cnt = len;
> +	if (cnt > PAGE_SIZE)
> +		return -EINVAL;

You fixed the wrong location. The caller should know how much size the
buffer is, and that's passed in by cnt.

>  	memcpy(buf, s->buffer + s->readpos, cnt);
>  
>  	s->readpos += cnt;

The correct fix would be:

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index b6e40e8791fa..c23b5ab27314 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -6729,7 +6864,8 @@ static ssize_t tracing_splice_read_pipe(struct file *filp,
 		/* Copy the data into the page, so we can start over. */
 		ret = trace_seq_to_buffer(&iter->seq,
 					  page_address(spd.pages[i]),
-					  trace_seq_used(&iter->seq));
+					  min(trace_seq_used(&iter->seq),
+					      PAGE_SIZE));
 		if (ret < 0) {
 			__free_page(spd.pages[i]);
 			break;

Especially since the trace_seq_to_buffer() code should be moved out of this
file and should have no idea how big the buffer passed in is.

-- Steve

  reply	other threads:[~2025-04-21 14:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-21 13:49 [PATCH] tracing: fix oob write in trace_seq_to_buffer() Jeongjun Park
2025-04-21 14:19 ` Steven Rostedt [this message]
2025-04-21 14:36   ` Jeongjun Park

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=20250421101912.4fd7c11a@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=aha310510@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=syzbot+c8cd2d2c412b868263fb@syzkaller.appspotmail.com \
    /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