Skip to content

Commit 48d1677

Browse files
LawstorantJiri Kosina
authored andcommitted
HID: pidff: Fix integer overflow in pidff_rescale
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: JacKeTUs/universal-pidff#116 Fixes: 224ee88 ("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>
1 parent a991aa5 commit 48d1677

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

drivers/hid/usbhid/hid-pidff.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "hid-pidff.h"
1212
#include <linux/hid.h>
1313
#include <linux/input.h>
14+
#include <linux/math64.h>
1415
#include <linux/minmax.h>
1516
#include <linux/slab.h>
1617
#include <linux/stringify.h>
@@ -326,8 +327,10 @@ static s32 pidff_clamp(s32 i, struct hid_field *field)
326327
*/
327328
static int pidff_rescale(int i, int max, struct hid_field *field)
328329
{
329-
return i * (field->logical_maximum - field->logical_minimum) / max +
330-
field->logical_minimum;
330+
/* 64 bits needed for big values during rescale */
331+
s64 result = field->logical_maximum - field->logical_minimum;
332+
333+
return div_s64(result * i, max) + field->logical_minimum;
331334
}
332335

333336
/*

0 commit comments

Comments
 (0)