Skip to content

Commit a6b87bf

Browse files
AlanSternBenjamin Tissoires
authored andcommitted
HID: core: Harden s32ton() against conversion to 0 bits
Testing by the syzbot fuzzer showed that the HID core gets a shift-out-of-bounds exception when it tries to convert a 32-bit quantity to a 0-bit quantity. Ideally this should never occur, but there are buggy devices and some might have a report field with size set to zero; we shouldn't reject the report or the device just because of that. Instead, harden the s32ton() routine so that it returns a reasonable result instead of crashing when it is called with the number of bits set to 0 -- the same as what snto32() does. Signed-off-by: Alan Stern <[email protected]> Reported-by: [email protected] Closes: https://lore.kernel.org/linux-usb/[email protected]/ Tested-by: [email protected] Fixes: dde5845 ("[PATCH] Generic HID layer - code split") Cc: [email protected] Link: https://patch.msgid.link/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 12f33ef commit a6b87bf

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

drivers/hid/hid-core.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ static s32 snto32(__u32 value, unsigned int n)
6666

6767
static u32 s32ton(__s32 value, unsigned int n)
6868
{
69-
s32 a = value >> (n - 1);
69+
s32 a;
7070

71+
if (!value || !n)
72+
return 0;
73+
74+
a = value >> (n - 1);
7175
if (a && a != -1)
7276
return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
7377
return value & ((1 << n) - 1);

0 commit comments

Comments
 (0)