HID: wacom: fix shift OOB in kfifo allocation for zero pktlen
Subsystems (3)
PATCH RESEND v2 1/1 HID: wacom: fix shift OOB in kfifo allocation for zero pktlen
During wacom_parse_and_register() the code calls wacom_devm_kfifo_alloc to allocate a fifo. During this operation it passes kfifo_alloc a fifo_size of 0. Kfifo attempts to round the size passed to it to the next power of 2 via roundup_pow_of_two (queue-type data structures do this to maintain efficiency of operations).
However during this phase a problem arises when the roundup_pow_of_two() function utilises a shift exponent of fls_long(n-1), where n is the fifo_size. Since n is 0 in this case and n is also an unsigned long, doing n-1 causes unsigned integer wrap-around to occur making the fifo_size 4294967295. So the code effectively does fls_long(4294967295) which results in 64. Returning back to roundup_pow_of_two(), the code utilises a shift exponent of 64. When a shift exponent of 64 is used on a 64-bit type such as 1UL it results in a shift-out-of-bounds.
The root cause of the issue seems to stem from insufficient validation of wacom_compute_pktlen(), since in this case the fifo_size comes from wacom_wac->features.pktlen. During wacom_parse_and_register() the wacom_compute_pktlen() function sets the pktlen as 0.
To fix this, we should handle cases where wacom_compute_pktlen() results in 0.
---
v2:
- Added Fixes tag as suggested by Jason Gerecke
drivers/hid/wacom_sys.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/hid/wacom_sys.c +2 -0
@@ -2361,6 +2361,8 @@ static int wacom_parse_and_register(struct wacom *wacom, bool wireless)unsigned int connect_mask =HID_CONNECT_HIDRAW ; features->pktlen =wacom_compute_pktlen (hdev); +if (!features->pktlen) +return -ENODEV ;if (!devres_open_group (&hdev->dev, wacom,GFP_KERNEL ))return -ENOMEM ;
Signature · 1 line
2.39.5
Quoted 23 lines · Qasim Ijaz
During
wacom_parse_and_register()the code callswacom_devm_kfifo_allocto allocate a fifo. During this operation it passeskfifo_allocafifo_sizeof 0. Kfifo attempts to round the size passed to it to the next power of 2 viaroundup_pow_of_two(queue-type data structures do this to maintain efficiency of operations).However during this phase a problem arises when the
roundup_pow_of_two()function utilises a shift exponent offls_long(n-1), where n is thefifo_size. Since n is 0 in this case and n is also an unsigned long, doing n-1 causes unsigned integer wrap-around to occur making thefifo_size4294967295. So the code effectively doesfls_long(4294967295)which results in 64. Returning back toroundup_pow_of_two(), the code utilises a shift exponent of 64. When a shift exponent of 64 is used on a 64-bit type such as 1UL it results in a shift-out-of-bounds.The root cause of the issue seems to stem from insufficient validation of
wacom_compute_pktlen(), since in this case thefifo_sizecomes fromwacom_wac->features.pktlen. Duringwacom_parse_and_register()thewacom_compute_pktlen()function sets the pktlen as 0.To fix this, we should handle cases where
wacom_compute_pktlen()results in 0.
Quoted 7 lines
Reported-by syzbot <[email protected]>Fixes 5e013ad20689 ("HID: wacom: Remove static WACOM_PKGLEN_MAX limit")Tested-by Qasim Ijaz <[email protected]>Reviewed-by Jason Gerecke <[email protected]>Signed-off-by Qasim Ijaz <[email protected]>
Applied, thanks.
Signature · 2 lines
Jiri Kosina SUSE Labs