media: cec: avoid wraparound in timer interval calculation
Subsystems (2)
PATCH 1/1 media: cec: avoid wraparound in timer interval calculation
[Why]
The timer function code may have an integer wraparound issue. Since both pin->tx_custom_low_usecs and pin->tx_custom_high_usecs can be set to up to 9999999 from the user space via cec_pin_error_inj_parse_line(), this may cause usecs to be overflowed when adap->monitor_pin_cnt is zero and usecs is multiplied by 1000.
[How]
Fix this by casting usecs to u64 when it is being converted from microseconds to nanoseconds.
Found by Linux Verification Center (linuxtesting.org) with Svace.
--- drivers/media/cec/core/cec-pin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/cec/core/cec-pin.c +1 -1
@@ -1021,7 +1021,7 @@ static enum hrtimer_restart cec_pin_timer(struct hrtimer *timer) pin->wait_usecs =0 ; pin->timer_ts =ktime_add_us (ts, usecs);hrtimer_forward_now (timer, -ns_to_ktime (usecs *1000 )); +ns_to_ktime ((u64 )usecs *1000 ));return HRTIMER_RESTART ; } pin->wait_usecs = usecs -100 ;
Signature · 1 line
2.48.1
Le 01/03/2025 à 12:09, Vitaliy Shevtsov a écrit :
Quoted 24 lines
[Why]
The timer function code may have an integer wraparound issue. Since bothpin->tx_custom_low_usecsandpin->tx_custom_high_usecscan be set to up to 9999999 from the user space viacec_pin_error_inj_parse_line(), this may cause usecs to be overflowed whenadap->monitor_pin_cntis zero and usecs is multiplied by 1000.[How]
Fix this by casting usecs to u64 when it is being converted from microseconds to nanoseconds.Found by Linux Verification Center (linuxtesting.org) with Svace.
Fixes 865463fc03ed ("media: cec-pin: add error injection support")Signed-off-by Vitaliy Shevtsov <[email protected]>--- drivers/media/cec/core/cec-pin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/media/cec/core/cec-pin.c +0
-0
drivers/media/cec/core/cec-pin.c static enum hrtimer_restart cec_pin_timer(struct hrtimer *timer) · line 1024
Quoted 5 lines
drivers/media/cec/core/cec-pin.c +1
-1@@ -1021,7 +1021,7 @@ static enum hrtimer_restart cec_pin_timer(struct hrtimer *timer) pin->wait_usecs =0 ; pin->timer_ts =ktime_add_us (ts, usecs);hrtimer_forward_now (timer, -ns_to_ktime (usecs *1000 )); +ns_to_ktime ((u64 )usecs *1000 ));
Or maybe us_to_ktime() to be less verbose?
CJ
Quoted 3 lines
drivers/media/cec/core/cec-pin.c +0
-0return HRTIMER_RESTART ; } pin->wait_usecs = usecs -100 ;
Quoted 33 lines · Christophe JAILLET
Le 01/03/2025 à 12:09, Vitaliy Shevtsov a écrit :
Quoted 24 lines
[Why]
The timer function code may have an integer wraparound issue. Since bothpin->tx_custom_low_usecsandpin->tx_custom_high_usecscan be set to up to 9999999 from the user space viacec_pin_error_inj_parse_line(), this may cause usecs to be overflowed whenadap->monitor_pin_cntis zero and usecs is multiplied by 1000.[How]
Fix this by casting usecs to u64 when it is being converted from microseconds to nanoseconds.Found by Linux Verification Center (linuxtesting.org) with Svace.
Fixes 865463fc03ed ("media: cec-pin: add error injection support")Signed-off-by Vitaliy Shevtsov <[email protected]>--- drivers/media/cec/core/cec-pin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/media/cec/core/cec-pin.c +0
-0drivers/media/cec/core/cec-pin.c static enum hrtimer_restart cec_pin_timer(struct hrtimer *timer) · line 1024
Quoted 5 lines
drivers/media/cec/core/cec-pin.c +1
-1@@ -1021,7 +1021,7 @@ static enum hrtimer_restart cec_pin_timer(struct hrtimer *timer) pin->wait_usecs =0 ; pin->timer_ts =ktime_add_us (ts, usecs);hrtimer_forward_now (timer, -ns_to_ktime (usecs *1000 )); +ns_to_ktime ((u64 )usecs *1000 ));Or maybe
us_to_ktime()to be less verbose?
Quoted 2 lines
CJ
That makes sense but let me check first. I will send a new one soon.
Quoted 4 lines
Quoted 3 lines
drivers/media/cec/core/cec-pin.c +0
-0return HRTIMER_RESTART ; } pin->wait_usecs = usecs -100 ;
Signature · 1 line
Vitaliy Shevtsov <[email protected]>