Skip to contentSkip to conversation

kdb: Reject non-positive BYTESPERWORD before division

PATCH In review Alex Kazinsky 3 participants 3 messages Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kdb: Reject non-positive BYTESPERWORD before division

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)")

---
v1: https://lore.kernel.org/all/[email protected]/

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(+)
kernel/debug/kdb/kdb_main.c +3 -0
index 40c332d2179d..b1719382144b 100644
@@ -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;
 
reviewer Reviewed-by on v2 1/1
#1 lore raw

Hi,

Quoted 20 lines · Alex Kazinsky

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)")

---
v1: https://lore.kernel.org/all/[email protected]/

Quoted 6 lines

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(+)
Reviewed-by on v2 1/1
#2 lore raw
In reply to Alex Kazinsky
Quoted 10 lines · Alex Kazinsky

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.

Quoted 6 lines

Found by Linux Verification Center (linuxtesting.org) with SVACE static analysis tool.

Fixes 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")

Nice!

Quoted 18 lines

---
v1: https://lore.kernel.org/all/[email protected]/

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(+)
kernel/debug/kdb/kdb_main.c +0 -0
index 40c332d2179d..b1719382144b 100644

kernel/debug/kdb/kdb_main.c static int kdb_md(int argc, const char **argv) · line 1535

Quoted 9 lines
kernel/debug/kdb/kdb_main.c +3 -0
@@ -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!