mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] kgdb: Fix buffer overflow in the qfThreadInfo packet handler
@ 2026-11-10  8:19 Christian Aurich Zanettini Martins
  2026-11-13  5:33 ` Doug Anderson
  0 siblings, 1 reply; 2+ messages in thread
From: Christian Aurich Zanettini Martins @ 2026-11-10  8:19 UTC (permalink / raw)
  To: Daniel Thompson, Douglas Anderson
  Cc: Jason Wessel, kgdb-bugreport, linux-kernel,
	Christian Aurich Zanettini Martins, stable

gdb_cmd_query() answers qfThreadInfo with a thread ID and a comma for
every online CPU, 9 bytes each, followed by the first page of tasks.
Only the tasks are paged, KGDB_MAX_THREAD_QUERY at a time, so the CPU
entries overrun remcom_out_buffer[BUFMAX] once there are enough CPUs.
On x86, where BUFMAX is 1024, a typical boot hits this from 109 online
CPUs (222 on arm64 and riscv), and GDB sends qfThreadInfo as soon as it
connects:

  BUG: KASAN: global-out-of-bounds in pack_threadid+0x2a8/0x2e0
  Write of size 1 at addr ffffffff86eecc20 by task swapper/0/1
  ...
  The buggy address belongs to the variable:
   remcom_out_buffer+0x400/0x420

Page the CPU entries together with the tasks, with one helper for both
loops, so no reply holds more than KGDB_MAX_THREAD_QUERY threads. The
CPUs still come first, which scripts/gdb relies on to detect KGDB.

Fixes: 25fc99991383 ("kgdb: fix gdb serial thread queries")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
---

Notes:
    Changes in v2:
    - Move the shared paging code into append_threadid() (Doug)
    - Link to v1: https://lore.kernel.org/all/20261103190622.925741-1-christian.aurichzm@gmail.com/
    
    Tested in QEMU with KASAN and GDB 15.1. Unpatched, connecting GDB to
    a 109-CPU guest gives the report above; patched, 109, 128 and 255 CPUs
    return the full thread list with no report.
    
    Based on v7.4-rc1.

 kernel/debug/gdbstub.c | 45 ++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/kernel/debug/gdbstub.c b/kernel/debug/gdbstub.c
index fc6a7a8c1f6e..679fab102760 100644
--- a/kernel/debug/gdbstub.c
+++ b/kernel/debug/gdbstub.c
@@ -732,6 +732,22 @@ static int gdb_cmd_reboot(struct kgdb_state *ks)
 	return 0;
 }
 
+static void append_threadid(struct kgdb_state *ks, char **ptr, int *i,
+			    int *finished, int threadid)
+{
+	unsigned char thref[BUF_THREAD_ID_SIZE];
+
+	if (*i >= ks->thr_query && !*finished) {
+		int_to_threadref(thref, threadid);
+		*ptr = pack_threadid(*ptr, thref);
+		*(*ptr)++ = ',';
+		ks->thr_query++;
+		if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
+			*finished = 1;
+	}
+	(*i)++;
+}
+
 /* Handle the 'q' query packets */
 static void gdb_cmd_query(struct kgdb_state *ks)
 {
@@ -752,28 +768,15 @@ static void gdb_cmd_query(struct kgdb_state *ks)
 		i = 0;
 		remcom_out_buffer[0] = 'm';
 		ptr = remcom_out_buffer + 1;
-		if (remcom_in_buffer[1] == 'f') {
-			/* Each cpu is a shadow thread */
-			for_each_online_cpu(cpu) {
-				ks->thr_query = 0;
-				int_to_threadref(thref, -cpu - 2);
-				ptr = pack_threadid(ptr, thref);
-				*(ptr++) = ',';
-				i++;
-			}
-		}
+		if (remcom_in_buffer[1] == 'f')
+			ks->thr_query = 0;
 
-		for_each_process_thread(g, p) {
-			if (i >= ks->thr_query && !finished) {
-				int_to_threadref(thref, p->pid);
-				ptr = pack_threadid(ptr, thref);
-				*(ptr++) = ',';
-				ks->thr_query++;
-				if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
-					finished = 1;
-			}
-			i++;
-		}
+		/* Each cpu is a shadow thread */
+		for_each_online_cpu(cpu)
+			append_threadid(ks, &ptr, &i, &finished, -cpu - 2);
+
+		for_each_process_thread(g, p)
+			append_threadid(ks, &ptr, &i, &finished, p->pid);
 
 		*(--ptr) = '\0';
 		break;

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.43.0


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

* Re: [PATCH v2] kgdb: Fix buffer overflow in the qfThreadInfo packet handler
  2026-11-10  8:19 [PATCH v2] kgdb: Fix buffer overflow in the qfThreadInfo packet handler Christian Aurich Zanettini Martins
@ 2026-11-13  5:33 ` Doug Anderson
  0 siblings, 0 replies; 2+ messages in thread
From: Doug Anderson @ 2026-11-13  5:33 UTC (permalink / raw)
  To: Christian Aurich Zanettini Martins
  Cc: Daniel Thompson, Jason Wessel, kgdb-bugreport, linux-kernel, stable

Hi,

On Tue, Nov 10, 2026 at 12:20 AM Christian Aurich Zanettini Martins
<christian.aurichzm@gmail.com> wrote:
>
> gdb_cmd_query() answers qfThreadInfo with a thread ID and a comma for
> every online CPU, 9 bytes each, followed by the first page of tasks.
> Only the tasks are paged, KGDB_MAX_THREAD_QUERY at a time, so the CPU
> entries overrun remcom_out_buffer[BUFMAX] once there are enough CPUs.
> On x86, where BUFMAX is 1024, a typical boot hits this from 109 online
> CPUs (222 on arm64 and riscv), and GDB sends qfThreadInfo as soon as it
> connects:
>
>   BUG: KASAN: global-out-of-bounds in pack_threadid+0x2a8/0x2e0
>   Write of size 1 at addr ffffffff86eecc20 by task swapper/0/1
>   ...
>   The buggy address belongs to the variable:
>    remcom_out_buffer+0x400/0x420
>
> Page the CPU entries together with the tasks, with one helper for both
> loops, so no reply holds more than KGDB_MAX_THREAD_QUERY threads. The
> CPUs still come first, which scripts/gdb relies on to detect KGDB.
>
> Fixes: 25fc99991383 ("kgdb: fix gdb serial thread queries")
> Cc: stable@vger.kernel.org
> Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
> ---
>
> Notes:
>     Changes in v2:
>     - Move the shared paging code into append_threadid() (Doug)
>     - Link to v1: https://lore.kernel.org/all/20261103190622.925741-1-christian.aurichzm@gmail.com/
>
>     Tested in QEMU with KASAN and GDB 15.1. Unpatched, connecting GDB to
>     a 109-CPU guest gives the report above; patched, 109, 128 and 255 CPUs
>     return the full thread list with no report.
>
>     Based on v7.4-rc1.
>
>  kernel/debug/gdbstub.c | 45 ++++++++++++++++++++++--------------------
>  1 file changed, 24 insertions(+), 21 deletions(-)

Reviewed-by: Douglas Anderson <dianders@chromium.org>

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

end of thread, other threads:[~2026-11-13  5:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-10  8:19 [PATCH v2] kgdb: Fix buffer overflow in the qfThreadInfo packet handler Christian Aurich Zanettini Martins
2026-11-13  5:33 ` Doug Anderson

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®