Skip to content

Commit 3c86548

Browse files
AlanSterngregkh
authored andcommitted
HID: core: Harden s32ton() against conversion to 0 bits
[ Upstream commit a6b87bf ] 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]> [ s32ton() was moved by c653ffc ("HID: stop exporting hid_snto32()"). Minor context change fixed. ] Signed-off-by: Wenshan Lan <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 66731f7 commit 3c86548

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

drivers/hid/hid-core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,12 @@ EXPORT_SYMBOL_GPL(hid_snto32);
13541354

13551355
static u32 s32ton(__s32 value, unsigned n)
13561356
{
1357-
s32 a = value >> (n - 1);
1357+
s32 a;
1358+
if (!value || !n)
1359+
return 0;
1360+
1361+
a = value >> (n - 1);
1362+
13581363
if (a && a != -1)
13591364
return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
13601365
return value & ((1 << n) - 1);

0 commit comments

Comments
 (0)