Skip to content

Commit 8785436

Browse files
basuamdBenjamin Tissoires
authored and
Benjamin Tissoires
committed
HID: amd_sfh: Fix for shift-out-of-bounds
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: 93ce5e0 ("HID: amd_sfh: Implement SFH1.1 functionality") Cc: [email protected] Tested-by: Kai-Heng Feng <[email protected]> Signed-off-by: Basavaraj Natikar <[email protected]> Signed-off-by: Akshata MukundShetty <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent c1685a8 commit 8785436

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_desc.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,32 @@ static int float_to_int(u32 flt32_val)
143143
if (!exp && !mantissa)
144144
return 0;
145145

146+
/*
147+
* Calculate the exponent and fraction part of floating
148+
* point representation.
149+
*/
146150
exp -= 127;
147151
if (exp < 0) {
148152
exp = -exp;
153+
if (exp >= BITS_PER_TYPE(u32))
154+
return 0;
149155
zeropre = (((BIT(23) + mantissa) * 100) >> 23) >> exp;
150156
return zeropre >= 50 ? sign : 0;
151157
}
152158

153159
shift = 23 - exp;
154-
flt32_val = BIT(exp) + (mantissa >> shift);
155-
fraction = mantissa & GENMASK(shift - 1, 0);
160+
if (abs(shift) >= BITS_PER_TYPE(u32))
161+
return 0;
162+
163+
if (shift < 0) {
164+
shift = -shift;
165+
flt32_val = BIT(exp) + (mantissa << shift);
166+
shift = 0;
167+
} else {
168+
flt32_val = BIT(exp) + (mantissa >> shift);
169+
}
170+
171+
fraction = (shift == 0) ? 0 : mantissa & GENMASK(shift - 1, 0);
156172

157173
return (((fraction * 100) >> shift) >= 50) ? sign * (flt32_val + 1) : sign * flt32_val;
158174
}

0 commit comments

Comments
 (0)