* [PATCH 1/2] HID: amd_sfh: Rename the float32 variable
2023-07-07 6:57 [PATCH 0/2] Updates to amd_sfh Basavaraj Natikar
@ 2023-07-07 6:57 ` Basavaraj Natikar
2023-07-07 6:57 ` [PATCH 2/2] HID: amd_sfh: Fix for shift-out-of-bounds Basavaraj Natikar
2023-07-10 8:32 ` [PATCH 0/2] Updates to amd_sfh Benjamin Tissoires
2 siblings, 0 replies; 4+ messages in thread
From: Basavaraj Natikar @ 2023-07-07 6:57 UTC (permalink / raw)
To: jikos, benjamin.tissoires, linux-input, akshata.mukundshetty
Cc: Basavaraj Natikar
As float32 is also used in other places as a data type, it is necessary
to rename the float32 variable in order to avoid confusion.
Signed-off-by: Basavaraj Natikar <Basavaraj.Natikar@amd.com>
Signed-off-by: Akshata MukundShetty <akshata.mukundshetty@amd.com>
---
drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c b/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c
index 6f0d332ccf51..c81d20cd3081 100644
--- a/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c
+++ b/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c
@@ -132,13 +132,13 @@ static void get_common_inputs(struct common_input_property *common, int report_i
common->event_type = HID_USAGE_SENSOR_EVENT_DATA_UPDATED_ENUM;
}
-static int float_to_int(u32 float32)
+static int float_to_int(u32 flt32_val)
{
int fraction, shift, mantissa, sign, exp, zeropre;
- mantissa = float32 & GENMASK(22, 0);
- sign = (float32 & BIT(31)) ? -1 : 1;
- exp = (float32 & ~BIT(31)) >> 23;
+ mantissa = flt32_val & GENMASK(22, 0);
+ sign = (flt32_val & BIT(31)) ? -1 : 1;
+ exp = (flt32_val & ~BIT(31)) >> 23;
if (!exp && !mantissa)
return 0;
@@ -151,10 +151,10 @@ static int float_to_int(u32 float32)
}
shift = 23 - exp;
- float32 = BIT(exp) + (mantissa >> shift);
+ flt32_val = BIT(exp) + (mantissa >> shift);
fraction = mantissa & GENMASK(shift - 1, 0);
- return (((fraction * 100) >> shift) >= 50) ? sign * (float32 + 1) : sign * float32;
+ return (((fraction * 100) >> shift) >= 50) ? sign * (flt32_val + 1) : sign * flt32_val;
}
static u8 get_input_rep(u8 current_index, int sensor_idx, int report_id,
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] HID: amd_sfh: Fix for shift-out-of-bounds
2023-07-07 6:57 [PATCH 0/2] Updates to amd_sfh Basavaraj Natikar
2023-07-07 6:57 ` [PATCH 1/2] HID: amd_sfh: Rename the float32 variable Basavaraj Natikar
@ 2023-07-07 6:57 ` Basavaraj Natikar
2023-07-10 8:32 ` [PATCH 0/2] Updates to amd_sfh Benjamin Tissoires
2 siblings, 0 replies; 4+ messages in thread
From: Basavaraj Natikar @ 2023-07-07 6:57 UTC (permalink / raw)
To: jikos, benjamin.tissoires, linux-input, akshata.mukundshetty
Cc: Basavaraj Natikar
Shift operation of 'exp' and 'shift' variables exceeds the maximum number
of shift values in the u32 range leading to UBSAN shift-out-of-bounds.
...
[ 6.120512] UBSAN: shift-out-of-bounds in drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c:149:50
[ 6.120598] shift exponent 104 is too large for 64-bit type 'long unsigned int'
[ 6.120659] CPU: 4 PID: 96 Comm: kworker/4:1 Not tainted 6.4.0amd_1-next-20230519-dirty #10
[ 6.120665] Hardware name: AMD Birman-PHX/Birman-PHX, BIOS SFH_with_HPD_SEN.FD 04/05/2023
[ 6.120667] Workqueue: events amd_sfh_work_buffer [amd_sfh]
[ 6.120687] Call Trace:
[ 6.120690] <TASK>
[ 6.120694] dump_stack_lvl+0x48/0x70
[ 6.120704] dump_stack+0x10/0x20
[ 6.120707] ubsan_epilogue+0x9/0x40
[ 6.120716] __ubsan_handle_shift_out_of_bounds+0x10f/0x170
[ 6.120720] ? psi_group_change+0x25f/0x4b0
[ 6.120729] float_to_int.cold+0x18/0xba [amd_sfh]
[ 6.120739] get_input_rep+0x57/0x340 [amd_sfh]
[ 6.120748] ? __schedule+0xba7/0x1b60
[ 6.120756] ? __pfx_get_input_rep+0x10/0x10 [amd_sfh]
[ 6.120764] amd_sfh_work_buffer+0x91/0x180 [amd_sfh]
[ 6.120772] process_one_work+0x229/0x430
[ 6.120780] worker_thread+0x4a/0x3c0
[ 6.120784] ? __pfx_worker_thread+0x10/0x10
[ 6.120788] kthread+0xf7/0x130
[ 6.120792] ? __pfx_kthread+0x10/0x10
[ 6.120795] ret_from_fork+0x29/0x50
[ 6.120804] </TASK>
...
Fix this by adding the condition to validate shift ranges.
Fixes: 93ce5e0231d7 ("HID: amd_sfh: Implement SFH1.1 functionality")
Signed-off-by: Basavaraj Natikar <Basavaraj.Natikar@amd.com>
Signed-off-by: Akshata MukundShetty <akshata.mukundshetty@amd.com>
---
drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c | 20 +++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c b/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c
index c81d20cd3081..06bdcf072d10 100644
--- a/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c
+++ b/drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c
@@ -143,16 +143,32 @@ static int float_to_int(u32 flt32_val)
if (!exp && !mantissa)
return 0;
+ /*
+ * Calculate the exponent and fraction part of floating
+ * point representation.
+ */
exp -= 127;
if (exp < 0) {
exp = -exp;
+ if (exp >= BITS_PER_TYPE(u32))
+ return 0;
zeropre = (((BIT(23) + mantissa) * 100) >> 23) >> exp;
return zeropre >= 50 ? sign : 0;
}
shift = 23 - exp;
- flt32_val = BIT(exp) + (mantissa >> shift);
- fraction = mantissa & GENMASK(shift - 1, 0);
+ if (abs(shift) >= BITS_PER_TYPE(u32))
+ return 0;
+
+ if (shift < 0) {
+ shift = -shift;
+ flt32_val = BIT(exp) + (mantissa << shift);
+ shift = 0;
+ } else {
+ flt32_val = BIT(exp) + (mantissa >> shift);
+ }
+
+ fraction = (shift == 0) ? 0 : mantissa & GENMASK(shift - 1, 0);
return (((fraction * 100) >> shift) >= 50) ? sign * (flt32_val + 1) : sign * flt32_val;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH 0/2] Updates to amd_sfh
2023-07-07 6:57 [PATCH 0/2] Updates to amd_sfh Basavaraj Natikar
2023-07-07 6:57 ` [PATCH 1/2] HID: amd_sfh: Rename the float32 variable Basavaraj Natikar
2023-07-07 6:57 ` [PATCH 2/2] HID: amd_sfh: Fix for shift-out-of-bounds Basavaraj Natikar
@ 2023-07-10 8:32 ` Benjamin Tissoires
2 siblings, 0 replies; 4+ messages in thread
From: Benjamin Tissoires @ 2023-07-10 8:32 UTC (permalink / raw)
To: jikos, benjamin.tissoires, linux-input, akshata.mukundshetty,
Basavaraj Natikar
On Fri, 07 Jul 2023 12:27:20 +0530, Basavaraj Natikar wrote:
> This patch series include changes for renaming the float32 variable
> including the fix for shift-out-of-bounds issue.
>
> Basavaraj Natikar (2):
> HID: amd_sfh: Rename the float32 variable
> HID: amd_sfh: Fix for shift-out-of-bounds
>
> [...]
Applied to https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git (for-6.5/upstream-fixes), thanks!
[1/2] HID: amd_sfh: Rename the float32 variable
https://git.kernel.org/hid/hid/c/c1685a862a4b
[2/2] HID: amd_sfh: Fix for shift-out-of-bounds
https://git.kernel.org/hid/hid/c/878543661764
Cheers,
--
Benjamin Tissoires <bentiss@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread