public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] um: proc/exitcode: fix simple_strtol() out-of-bounds read
@ 2026-04-22 17:39 Shengzhuo Wei
  2026-04-22 20:45 ` David Laight
  2026-04-27 13:36 ` Shengzhuo Wei
  0 siblings, 2 replies; 4+ messages in thread
From: Shengzhuo Wei @ 2026-04-22 17:39 UTC (permalink / raw)
  To: Richard Weinberger, Anton Ivanov, Johannes Berg, Dan Carpenter,
	Andrew Morton, Jeff Dike
  Cc: linux-um, linux-kernel, Shengzhuo Wei

The stack buffer 'buf' is declared as char[sizeof("nnnnn\0")] (7 bytes)
and the copy size is min(count, sizeof(buf)).  When a user writes 7 or
more bytes, copy_from_user fills all 7 bytes without a NUL terminator.
The subsequent call to simple_strtol() expects a NUL-terminated string
and will read past the end of buf on the stack.

write(2) should report the number of bytes consumed. Returning the original
count would claim success even when the input was truncated, so userspace
cannot detect it.

Clamp the copy length to sizeof(buf)-1, add a terminator, and return the
consumed length.

Fixes: 201f99f170df ("uml: check length in exitcode_proc_write()")
Fixes: e16f5350d4cf ("uml: get declaration of simple_strtoul")
Signed-off-by: Shengzhuo Wei <me@cherr.cc>
---
 arch/um/kernel/exitcode.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/um/kernel/exitcode.c b/arch/um/kernel/exitcode.c
index 43edc2aa57e4fbd4a3d24f96878c76f9f8fd4eaa..8de404ff21a213918c5351bc20a6e047bf1b93f5 100644
--- a/arch/um/kernel/exitcode.c
+++ b/arch/um/kernel/exitcode.c
@@ -43,16 +43,17 @@ static ssize_t exitcode_proc_write(struct file *file,
 	size_t size;
 	int tmp;
 
-	size = min(count, sizeof(buf));
+	size = min(count, sizeof(buf) - 1);
 	if (copy_from_user(buf, buffer, size))
 		return -EFAULT;
+	buf[size] = '\0';
 
 	tmp = simple_strtol(buf, &end, 0);
 	if ((*end != '\0') && !isspace(*end))
 		return -EINVAL;
 
 	uml_exitcode = tmp;
-	return count;
+	return size;
 }
 
 static const struct proc_ops exitcode_proc_ops = {

---
base-commit: 6596a02b207886e9e00bb0161c7fd59fea53c081
change-id: 20260423-fix_exitcode-908061ece624

Best regards,
-- 
Shengzhuo Wei <me@cherr.cc>

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

end of thread, other threads:[~2026-04-27 13:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-22 17:39 [PATCH] um: proc/exitcode: fix simple_strtol() out-of-bounds read Shengzhuo Wei
2026-04-22 20:45 ` David Laight
2026-04-23  3:28   ` Shengzhuo Wei
2026-04-27 13:36 ` Shengzhuo Wei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox