All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ASoC: SOF: ipc4-mtrace: fix the wrapped read when the user buffer is short
@ 2026-11-21 15:26 ` Suyuan Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-21 15:26 UTC (permalink / raw)
  To: yung-chuan.liao, broonie, lgirdwood, peter.ujfalusi
  Cc: linux-kernel, sound-open-firmware, kai.vehmanen, tiwai,
	pierre-louis.bossart, daniel.baluta, linux-sound, perex

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 */





^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-21 15:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 15:26 [PATCH] ASoC: SOF: ipc4-mtrace: fix the wrapped read when the user buffer is short Suyuan Wang via B4 Relay
2026-11-21 15:26 ` Suyuan Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.