linux/drivers/counter
Babanpreet Singh ce9bd34126 counter: microchip-tcb-capture: Fix DT channel validation
[ Upstream commit f1a3a9946a ]

mchp_tc_probe() reads the devicetree "reg" cell - a u32, per the API
contract of of_property_read_u32_index() - into a signed int, so the
bounds check "channel > 2" fails to reject cell values at or above
0x80000000: reinterpreted as a negative int, they compare below 2 and
pass validation.

A malformed devicetree can therefore drive a negative channel into the
ATMEL_TC_REG() offset arithmetic, making the driver access syscon
regmap offsets outside the TC block's register window, and into the
"t%d_clk" clock-name formatting, where it truncates clk_name (sized
for "t0_clk".."t2_clk").

Declare channel as u32, matching the API contract; the unsigned
comparison then rejects everything except channels 0..2. Adjust the
format specifier to %u accordingly, which also resolves the W=1
warning that exposed the gap:

  microchip-tcb-capture.c:520:56: warning: '%d' directive output may
    be truncated writing between 1 and 11 bytes into a region of size
    6 [-Wformat-truncation=]
  note: directive argument in the range [-2147483648, 2]

No behavior change for well-formed devicetrees: channels 0..2 take
identical paths before and after.

Fixes: 106b104137 ("counter: Add microchip TCB capture counter")
Assisted-by: Claude:claude-fable-5 [gcc W=1]
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>
Link: https://lore.kernel.org/r/20260714042910.7-1-bbnpreetsingh@gmail.com
Signed-off-by: William Breathitt Gray <wbg@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
History 2026-08-19 17:14:13 +02:00
..
104-quad-8.c counter: 104-quad-8: Fix race condition between FLAG and CNTR reads 2023-04-26 13:51:55 +02:00
counter-core.c counter: Internalize sysfs interface code 2023-04-20 12:13:55 +02:00
counter-sysfs.c counter: Internalize sysfs interface code 2023-04-20 12:13:55 +02:00
counter-sysfs.h counter: Internalize sysfs interface code 2023-04-20 12:13:55 +02:00
ftm-quaddec.c counter: Internalize sysfs interface code 2023-04-20 12:13:55 +02:00
intel-qep.c counter: Internalize sysfs interface code 2023-04-20 12:13:55 +02:00
interrupt-cnt.c counter: interrupt-cnt: Drop IRQF_NO_THREAD flag 2026-01-19 13:10:18 +01:00
Kconfig counter: Add support for Intel Quadrature Encoder Peripheral 2021-06-03 18:24:14 +01:00
Makefile counter: Internalize sysfs interface code 2023-04-20 12:13:55 +02:00
microchip-tcb-capture.c counter: microchip-tcb-capture: Fix DT channel validation 2026-08-19 17:14:13 +02:00
stm32-lptimer-cnt.c counter: stm32-lptimer-cnt: fix error handling when enabling 2025-04-10 14:31:56 +02:00
stm32-timer-cnt.c counter: stm32-timer-cnt: Add check for clk_enable() 2024-12-14 19:51:11 +01:00
ti-eqep.c counter: Internalize sysfs interface code 2023-04-20 12:13:55 +02:00