mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Suyuan Wang via B4 Relay <devnull+shyckless.gmail.com@kernel.org>
To: yung-chuan.liao@linux.intel.com, broonie@kernel.org,
	 lgirdwood@gmail.com, peter.ujfalusi@linux.intel.com
Cc: linux-kernel@vger.kernel.org,
	sound-open-firmware@alsa-project.org,
	 kai.vehmanen@linux.intel.com, tiwai@suse.com,
	 pierre-louis.bossart@linux.dev, daniel.baluta@nxp.com,
	 linux-sound@vger.kernel.org, perex@perex.cz
Subject: [PATCH] ASoC: SOF: ipc4-mtrace: fix the wrapped read when the user buffer is short
Date: Sat, 21 Nov 2026 15:26:27 +0000	[thread overview]
Message-ID: <20261121-sof-mtrace-wrap-v1-1-9798707c3439@gmail.com> (raw)

From: Suyuan Wang <stevewong@hust.edu.cn>

When the log data wraps around the end of the slot,
sof_ipc4_mtrace_dfs_read() copies it in two parts:

	sof_mailbox_read(..., log_buffer, avail - write_ptr);
	sof_mailbox_read(..., log_buffer + avail - write_ptr, write_ptr);

This assumes that avail is still the full distance from read_ptr to
write_ptr. But avail has just been cut down to count - sizeof(avail) if
the user asked for less. If it ends up smaller than write_ptr,
avail - write_ptr wraps around, and the first sof_mailbox_read() gets a
length of about 4 GiB for the 4 KiB log_buffer.
For example, with read_ptr = 4080 and write_ptr = 16, a read of 12 bytes
gives avail = 8 and a copy of 0xfffffff8 bytes. If avail is cut but
still larger than write_ptr, the two parts don't line up with read_ptr
and the user gets the wrong bytes.

Copy from read_ptr up to the end of the slot, but no more than avail,
and take the rest from the start of the slot.

Found with a static analysis tool and checked by hand.

Fixes: f4ea22f7aa75 ("ASoC: SOF: ipc4: Add support for mtrace log extraction")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
Compile tested (x86_64, W=1). I don't have SOF hardware, so I checked
the pointer arithmetic in userspace instead: the code of this function
with the mailbox replaced by an array, under ASan. Without the patch a
12-byte read at read_ptr 4080, write_ptr 16 copies 0xfffffff8 bytes,
and a 104-byte read at read_ptr 4000 returns 16 wrong bytes; with it
both return the expected data.
---
 sound/soc/sof/ipc4-mtrace.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/sound/soc/sof/ipc4-mtrace.c b/sound/soc/sof/ipc4-mtrace.c
index 064291e26974..0d5135d256c3 100644
--- a/sound/soc/sof/ipc4-mtrace.c
+++ b/sound/soc/sof/ipc4-mtrace.c
@@ -230,14 +230,17 @@ static ssize_t sof_ipc4_mtrace_dfs_read(struct file *file, char __user *buffer,
 		/* Read data between read pointer and write pointer */
 		sof_mailbox_read(sdev, log_buffer_offset + read_ptr, log_buffer, avail);
 	} else {
+		/* avail may have been cut to the user's count above */
+		u32 tail = min(avail, log_buffer_size - read_ptr);
+
 		/* read from read pointer to end of the slot */
 		sof_mailbox_read(sdev, log_buffer_offset + read_ptr, log_buffer,
-				 avail - write_ptr);
-		/* read from slot start to write pointer */
-		if (write_ptr)
+				 tail);
+		/* read the rest from slot start */
+		if (avail > tail)
 			sof_mailbox_read(sdev, log_buffer_offset,
-					 (u8 *)(log_buffer) + avail - write_ptr,
-					 write_ptr);
+					 (u8 *)(log_buffer) + tail,
+					 avail - tail);
 	}
 
 	/* first write the number of bytes we have gathered */





                 reply	other threads:[~2026-11-21 15:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261121-sof-mtrace-wrap-v1-1-9798707c3439@gmail.com \
    --to=devnull+shyckless.gmail.com@kernel.org \
    --cc=broonie@kernel.org \
    --cc=daniel.baluta@nxp.com \
    --cc=kai.vehmanen@linux.intel.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=peter.ujfalusi@linux.intel.com \
    --cc=pierre-louis.bossart@linux.dev \
    --cc=shyckless@gmail.com \
    --cc=sound-open-firmware@alsa-project.org \
    --cc=tiwai@suse.com \
    --cc=yung-chuan.liao@linux.intel.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

all inboxes | Powered by JetHome®