Re: [PATCH v2] kgdb: Fix buffer overflow in the 'M' and 'X' packet handlers
From: Fang Xieyan
Date: Wed Sep 23 2026 - 05:48:02 EST
Hi Doug,
Thanks for the review.
> My analysis shows that the above isn't true and that zero-length 'X'
> packets are no longer valid with your patch. I haven't tested this
> myself, though. Yell if I got it wrong.
You're right. For "X0,0:", ptr reaches buf_end after parsing ':',
and the `ptr >= buf_end` check returns -EINVAL before the length
check. I'll change it to `ptr > buf_end` so zero-length 'X' packets
remain valid.
> I don't love making this a global. Can we just pass it down? ...and
> maybe for functions where you pass the length you also pass
> "remcom_in_buffer" as a parameter and stop accessing the global?
My plan is to pass both remcom_in_buffer and the received length
through the relevant functions:
gdb_serial_stub() -> gdb_cmd_memwrite() / gdb_cmd_binwrite()
-> write_mem_msg()
so that write_mem_msg() no longer accesses remcom_in_buffer
directly. These functions are all static and only used within
gdbstub.c, so the signature changes stay local.