Re: [PATCH v2] kdb: Reject non-positive BYTESPERWORD before division

From: Bradley Morgan

Date: Wed Nov 18 2026 - 15:31:02 EST


On 18 November 2026 07:28:52 GMT, Alex Kazinsky <a.kazinsky@xxxxxxxx>
wrote:
>kdb_md() uses BYTESPERWORD as a divisor when calculating the initial
>repeat count, before validating the word width. A zero width causes a
>division by zero. kdbgetintenv() also casts an unsigned long to int,
>so the resulting width can be negative. If the width is -1 and the
>scaled count is INT_MIN, the quotient overflows and raises a divide
>error on x86.
>
>Return KDB_BADWIDTH for non-positive widths before calculating the
>repeat count.
>
>Found by Linux Verification Center (linuxtesting.org) with SVACE static
>analysis tool.
>
>Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
>Cc: stable@xxxxxxxxxxxxxxx
>Signed-off-by: Alex Kazinsky <a.kazinsky@xxxxxxxx>

Nice!

Reviewed-by: Bradley Morgan <bradley.morgan@xxxxxxxxx>


>---
>v1: https://lore.kernel.org/all/20261113111152.4190309-1-a.kazinsky@xxxxxxxx/
>
>Changes in v2:
>- Reject negative BYTESPERWORD values as well as zero.
>- Explain the signed division overflow in the commit message.
>
> kernel/debug/kdb/kdb_main.c | 3 +++
> 1 file changed, 3 insertions(+)
>
>diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
>index 40c332d2179d..b1719382144b 100644
>--- a/kernel/debug/kdb/kdb_main.c
>+++ b/kernel/debug/kdb/kdb_main.c
>@@ -1527,6 +1527,9 @@ static int kdb_md(int argc, const char **argv)
> kdbgetintenv("RADIX", &radix);
> kdbgetintenv("BYTESPERWORD", &bytesperword);
>
>+ if (bytesperword <= 0)
>+ return KDB_BADWIDTH;
>+
> /* Assume 'md <addr>' and start with environment values */
> repeat = mdcount * 16 / bytesperword;
>
>
>
>

--- Thanks!