kdb: Reject non-positive BYTESPERWORD before division
Subsystems (2)
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.
---
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
@@ -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;
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 isINT_MIN, the quotient overflows and raises a divide error on x86.Return
KDB_BADWIDTHfor 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)")Signed-off-by Alex Kazinsky <[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(+)
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 isINT_MIN, the quotient overflows and raises a divide error on x86.Return
KDB_BADWIDTHfor 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)")Signed-off-by Alex Kazinsky <[email protected]>
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
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!