Skip to contentSkip to conversation

HID: magicmouse: prevent division by 0 on scroll

PATCH Applied Claudia Pellegrino 3 participants 4 messages Nov 14, 2021 – Nov 19, 2021 HID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: magicmouse: prevent division by 0 on scroll

In hid_magicmouse, if the user has set scroll_speed to a value between 55 and 63 and scrolls seven times in quick succession, the step_hr variable in the magicmouse_emit_touch function becomes 0.

That causes a division by zero further down in the function when it does step_x_hr /= step_hr.

To reproduce, create /etc/modprobe.d/hid_magicmouse.conf with the following content:

```
options hid_magicmouse scroll_acceleration=1 scroll_speed=55
```

Then reboot, connect a Magic Mouse and scroll seven times quickly. The system will freeze for a minute, and after that dmesg will confirm that a division by zero occurred.

Enforce a minimum of 1 for the variable so the high resolution step count can never reach 0 even at maximum scroll acceleration.

Fixes d4b9f10a0eb6 ("HID: magicmouse: enable high-resolution scroll")
---
 drivers/hid/hid-magicmouse.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/hid/hid-magicmouse.c +5 -2
index 686788ebf3e1..d7687ce70614 100644
@@ -256,8 +256,11 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
 		unsigned long now = jiffies;
 		int step_x = msc->touches[id].scroll_x - x;
 		int step_y = msc->touches[id].scroll_y - y;
-		int step_hr = ((64 - (int)scroll_speed) * msc->scroll_accel) /
-			      SCROLL_HR_STEPS;
+		int step_hr =
+			max_t(int,
+			      ((64 - (int)scroll_speed) * msc->scroll_accel) /
+					SCROLL_HR_STEPS,
+			      1);
 		int step_x_hr = msc->touches[id].scroll_x_hr - x;
 		int step_y_hr = msc->touches[id].scroll_y_hr - y;
 
Signature · 1 line
2.33.1
Tested-by on v1 1/1
#1 lore raw
Quoted 49 lines · Claudia Pellegrino

In hid_magicmouse, if the user has set scroll_speed to a value between 55 and 63 and scrolls seven times in quick succession, the step_hr variable in the magicmouse_emit_touch function becomes 0.

That causes a division by zero further down in the function when it does step_x_hr /= step_hr.

To reproduce, create /etc/modprobe.d/hid_magicmouse.conf with the following content:

```
options hid_magicmouse scroll_acceleration=1 scroll_speed=55
```

Then reboot, connect a Magic Mouse and scroll seven times quickly. The system will freeze for a minute, and after that dmesg will confirm that a division by zero occurred.

Enforce a minimum of 1 for the variable so the high resolution step count can never reach 0 even at maximum scroll acceleration.

Fixes d4b9f10a0eb6 ("HID: magicmouse: enable high-resolution scroll")
---
 drivers/hid/hid-magicmouse.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/hid/hid-magicmouse.c +5 -2
index 686788ebf3e1..d7687ce70614 100644
@@ -256,8 +256,11 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
 		unsigned long now = jiffies;
 		int step_x = msc->touches[id].scroll_x - x;
 		int step_y = msc->touches[id].scroll_y - y;
-		int step_hr = ((64 - (int)scroll_speed) * msc->scroll_accel) /
-			      SCROLL_HR_STEPS;
+		int step_hr =
+			max_t(int,
+			      ((64 - (int)scroll_speed) * msc->scroll_accel) /
+					SCROLL_HR_STEPS,
+			      1);
 		int step_x_hr = msc->touches[id].scroll_x_hr - x;
 		int step_y_hr = msc->touches[id].scroll_y_hr - y;
 
Signature · 1 line
2.33.1

Hi Caludia,

Thanks for ccing me.

I can confirm both that the bug is present and that your patch fixes it.

Best wishes,
Jose

#2 lore raw

Hi José,

Quoted 1 line

I can confirm both that the bug is present and that your patch fixes it.

Thanks for your testing and feedback!
And kudos for implementing the feature in the first place.

Regards
Claudia

maintainer Applied
#3 lore raw
In reply to Claudia Pellegrino
Quoted 18 lines · Claudia Pellegrino

In hid_magicmouse, if the user has set scroll_speed to a value between 55 and 63 and scrolls seven times in quick succession, the step_hr variable in the magicmouse_emit_touch function becomes 0.

That causes a division by zero further down in the function when it does step_x_hr /= step_hr.

To reproduce, create /etc/modprobe.d/hid_magicmouse.conf with the following content:

```
options hid_magicmouse scroll_acceleration=1 scroll_speed=55
```

Then reboot, connect a Magic Mouse and scroll seven times quickly. The system will freeze for a minute, and after that dmesg will confirm that a division by zero occurred.

Quoted 6 lines

Enforce a minimum of 1 for the variable so the high resolution step count can never reach 0 even at maximum scroll acceleration.

Fixes d4b9f10a0eb6 ("HID: magicmouse: enable high-resolution scroll")

Applied, thank you.

Signature · 2 lines
Jiri Kosina
SUSE Labs