]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
HID: pidff: Fix integer overflow in pidff_rescale
authorTomasz Pakuła <tomasz.pakula.oficjalny@gmail.com>
Sun, 10 May 2026 12:23:52 +0000 (14:23 +0200)
committerJiri Kosina <jkosina@suse.com>
Tue, 12 May 2026 16:13:40 +0000 (18:13 +0200)
Rescaling values close to the max (U16_MAX) temporarily creates values
that exceed the s32 range. This caused value overflow in case when, for
example, a periodic effect phase was higer than 180 degrees. In turn,
rescale function could return values outised of the logical range of the
HID field.

Fix by using 64 bit signed integer to store the value during calculation
but still return only 32 bit integer.

Closes: https://github.com/JacKeTUs/universal-pidff/issues/116
Fixes: 224ee88fe395 ("Input: add force feedback driver for PID devices")
Cc: stable@vger.kernel.org
Signed-off-by: Tomasz Pakuła <tomasz.pakula.oficjalny@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
drivers/hid/usbhid/hid-pidff.c

index aee8a44433059e578dd2c3bd6674470780293bb3..c45f182d044800e6d6c8103c14f6143542244858 100644 (file)
@@ -11,6 +11,7 @@
 #include "hid-pidff.h"
 #include <linux/hid.h>
 #include <linux/input.h>
+#include <linux/math64.h>
 #include <linux/minmax.h>
 #include <linux/slab.h>
 #include <linux/stringify.h>
@@ -326,8 +327,10 @@ static s32 pidff_clamp(s32 i, struct hid_field *field)
  */
 static int pidff_rescale(int i, int max, struct hid_field *field)
 {
-       return i * (field->logical_maximum - field->logical_minimum) / max +
-              field->logical_minimum;
+       /* 64 bits needed for big values during rescale */
+       s64 result = field->logical_maximum - field->logical_minimum;
+
+       return div_s64(result * i, max) + field->logical_minimum;
 }
 
 /*