Skip to contentSkip to conversation

media: cec: avoid wraparound in timer interval calculation

PATCH In review Vitaliy Shevtsov 2 participants 3 messages Mar 1, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

Fixes 865463fc03ed ("media: cec-pin: add error injection support")
---
 drivers/media/cec/core/cec-pin.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/cec/core/cec-pin.c +1 -1
index a70451d99ebc..f15ed5c67a65 100644
@@ -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
#1 lore raw

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 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.

Fixes 865463fc03ed ("media: cec-pin: add error injection support")
---
  drivers/media/cec/core/cec-pin.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/cec/core/cec-pin.c +0 -0
index a70451d99ebc..f15ed5c67a65 100644

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 -0
  		return HRTIMER_RESTART;
  	}
  	pin->wait_usecs = usecs - 100;
#2 lore raw
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 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.

Fixes 865463fc03ed ("media: cec-pin: add error injection support")
---
  drivers/media/cec/core/cec-pin.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/cec/core/cec-pin.c +0 -0
index a70451d99ebc..f15ed5c67a65 100644

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?

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 -0
  		return HRTIMER_RESTART;
  	}
  	pin->wait_usecs = usecs - 100;
Signature · 1 line
Vitaliy Shevtsov <[email protected]>