Linux Serial subsystem development
 help / color / mirror / Atom feed
* [PATCH v2] powerpc/pseries/hvconsole: Do not read past count in hvc_put_chars()
@ 2026-12-05  8:30 Praneeth Balasubramanian
  2026-12-05  8:46 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  8:30 UTC (permalink / raw)
  To: Madhavan Srinivasan, Greg Kroah-Hartman, Jiri Slaby
  Cc: Praneeth Balasubramanian, stable, Michael Ellerman,
	Nicholas Piggin, Christophe Leroy (CS GROUP),
	Ritesh Harjani (IBM), Shrikanth Hegde, linuxppc-dev, linux-kernel,
	linux-serial

hvc_put_chars() always loads two unsigned long words from the buffer, 16
bytes in total, whatever count its caller passes. Commit 934bda59f286d
("powerpc/pseries/hvconsole: Fix stack overread via udbg") documented
that the buffer must be at least 16 bytes and gave udbg_hvc_putc() a
bounce buffer. Other callers still pass smaller stack objects:

- drivers/tty/hvc/hvsi.c sends a 6 byte struct hvsi_query from
  hvsi_query(), a 12 byte struct hvsi_query_response from
  hvsi_version_respond() and a 14 byte struct hvsi_control from
  hvsi_set_mctrl() and hvsi_close_protocol().
- drivers/tty/hvc/hvsi_lib.c sends the same three packet types through
  hvsi_send_packet(). hvc_vio.c passes hvc_put_chars() as its put_chars.
- hvc_poll_put_char() sends a single char through hvterm_raw_put_chars().

Each of these reads up to 15 bytes past the end of the stack object and
passes them to the hypervisor in the H_PUT_TERM_CHAR registers. The HVSI
version query and response happen on every HVSI handshake.

Copy count bytes into a zeroed local buffer and pass that instead. This
fixes every caller at once. Drop the 16 byte requirement from the
comments on hvc_put_chars() and hvterm_raw_put_chars().

This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    Changes in v2:
    - v1 was "tty: hvsi: fix stack overread in hvsi_version_respond()". It
      copied one packet into a 16 byte buffer. Sashiko pointed out the other
      callers that pass short buffers. This version fixes hvc_put_chars()
      itself, so it is a powerpc patch with a new subject.
    
    v1: https://lore.kernel.org/all/20261205063536.2519566-2-praneethb2002@gmail.com/
    
    This was found by an LLM-based audit, part of research at UCSB SecLab.
    An LLM turned a kernel comment into a rule, and three LLM votes checked
    the code against it. v1 was drafted with GLM-5.3 and checked with Claude
    Opus 5.5. This version was written with Claude Opus 5.5 after it checked
    the Sashiko review of v1 against the source. I reviewed the patch and
    take responsibility for it.
    
    Testing: builds without new warnings with W=1 (powerpc pseries_defconfig
    with HVC_OLD_HVSI=y), clang 23. checkpatch.pl is clean. Not
    runtime-tested, and there is no reproducer. I do not have this hardware.

 arch/powerpc/platforms/pseries/hvconsole.c | 8 ++++++--
 drivers/tty/hvc/hvc_vio.c                  | 4 +---
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/platforms/pseries/hvconsole.c b/arch/powerpc/platforms/pseries/hvconsole.c
index 8803c947998e..3190cb7f9a16 100644
--- a/arch/powerpc/platforms/pseries/hvconsole.c
+++ b/arch/powerpc/platforms/pseries/hvconsole.c
@@ -13,6 +13,7 @@
 #include <linux/kernel.h>
 #include <linux/export.h>
 #include <linux/errno.h>
+#include <linux/string.h>
 #include <asm/hvcall.h>
 #include <asm/hvconsole.h>
 #include <asm/plpar_wrappers.h>
@@ -49,12 +50,12 @@ EXPORT_SYMBOL(hvc_get_chars);
  * @vtermno: The vtermno or unit_address of the adapter from which the data
  *	originated.
  * @buf: The character buffer that contains the character data to send to
- *	firmware. Must be at least 16 bytes, even if count is less than 16.
+ *	firmware.
  * @count: Send this number of characters.
  */
 ssize_t hvc_put_chars(uint32_t vtermno, const u8 *buf, size_t count)
 {
-	unsigned long *lbuf = (unsigned long *) buf;
+	unsigned long lbuf[2] = { 0 };
 	long ret;
 
 
@@ -62,6 +63,9 @@ ssize_t hvc_put_chars(uint32_t vtermno, const u8 *buf, size_t count)
 	if (count > MAX_VIO_PUT_CHARS)
 		count = MAX_VIO_PUT_CHARS;
 
+	/* The hcall takes 16 bytes in two registers; do not read past count. */
+	memcpy(lbuf, buf, count);
+
 	ret = plpar_hcall_norets(H_PUT_TERM_CHAR, vtermno, count,
 				 cpu_to_be64(lbuf[0]),
 				 cpu_to_be64(lbuf[1]));
diff --git a/drivers/tty/hvc/hvc_vio.c b/drivers/tty/hvc/hvc_vio.c
index 14c6a5564617..f385978baa9e 100644
--- a/drivers/tty/hvc/hvc_vio.c
+++ b/drivers/tty/hvc/hvc_vio.c
@@ -110,9 +110,7 @@ static ssize_t hvterm_raw_get_chars(uint32_t vtermno, u8 *buf, size_t count)
 /**
  * hvterm_raw_put_chars: send characters to firmware for given vterm adapter
  * @vtermno: The virtual terminal number.
- * @buf: The characters to send. Because of the underlying hypercall in
- *       hvc_put_chars(), this buffer must be at least 16 bytes long, even if
- *       you are sending fewer chars.
+ * @buf: The characters to send.
  * @count: number of chars to send.
  */
 static ssize_t hvterm_raw_put_chars(uint32_t vtermno, const u8 *buf,

base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
-- 
2.25.1


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

end of thread, other threads:[~2026-12-05  8:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  8:30 [PATCH v2] powerpc/pseries/hvconsole: Do not read past count in hvc_put_chars() Praneeth Balasubramanian
2026-12-05  8:46 ` sashiko-bot

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