mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] media: cec: avoid wraparound in timer interval calculation
@ 2025-03-01 11:09 Vitaliy Shevtsov
  2025-03-01 11:32 ` Christophe JAILLET
  0 siblings, 1 reply; 3+ messages in thread
From: Vitaliy Shevtsov @ 2025-03-01 11:09 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Vitaliy Shevtsov, Mauro Carvalho Chehab, Jani Nikula,
	linux-media, linux-kernel, lvc-project

[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")
Signed-off-by: Vitaliy Shevtsov <v.shevtsov@mt-integration.ru>
---
 drivers/media/cec/core/cec-pin.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/cec/core/cec-pin.c b/drivers/media/cec/core/cec-pin.c
index a70451d99ebc..f15ed5c67a65 100644
--- a/drivers/media/cec/core/cec-pin.c
+++ b/drivers/media/cec/core/cec-pin.c
@@ -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;
-- 
2.48.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] media: cec: avoid wraparound in timer interval calculation
  2025-03-01 11:09 [PATCH] media: cec: avoid wraparound in timer interval calculation Vitaliy Shevtsov
@ 2025-03-01 11:32 ` Christophe JAILLET
  2025-03-01 21:28   ` Vitaliy Shevtsov
  0 siblings, 1 reply; 3+ messages in thread
From: Christophe JAILLET @ 2025-03-01 11:32 UTC (permalink / raw)
  To: Vitaliy Shevtsov, Hans Verkuil
  Cc: Mauro Carvalho Chehab, Jani Nikula, linux-media, linux-kernel,
	lvc-project

Le 01/03/2025 à 12:09, Vitaliy Shevtsov a écrit :
> [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")
> Signed-off-by: Vitaliy Shevtsov <v.shevtsov@mt-integration.ru>
> ---
>   drivers/media/cec/core/cec-pin.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/media/cec/core/cec-pin.c b/drivers/media/cec/core/cec-pin.c
> index a70451d99ebc..f15ed5c67a65 100644
> --- a/drivers/media/cec/core/cec-pin.c
> +++ b/drivers/media/cec/core/cec-pin.c
> @@ -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

>   		return HRTIMER_RESTART;
>   	}
>   	pin->wait_usecs = usecs - 100;


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] media: cec: avoid wraparound in timer interval calculation
  2025-03-01 11:32 ` Christophe JAILLET
@ 2025-03-01 21:28   ` Vitaliy Shevtsov
  0 siblings, 0 replies; 3+ messages in thread
From: Vitaliy Shevtsov @ 2025-03-01 21:28 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Hans Verkuil, Mauro Carvalho Chehab, Jani Nikula, linux-media,
	linux-kernel, lvc-project

On Sat, 1 Mar 2025 12:32:30 +0100, Christophe JAILLET wrote:

> Le 01/03/2025 à 12:09, Vitaliy Shevtsov a écrit :
> > [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")
> > Signed-off-by: Vitaliy Shevtsov <v.shevtsov@mt-integration.ru>
> > ---
> >   drivers/media/cec/core/cec-pin.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/media/cec/core/cec-pin.c b/drivers/media/cec/core/cec-pin.c
> > index a70451d99ebc..f15ed5c67a65 100644
> > --- a/drivers/media/cec/core/cec-pin.c
> > +++ b/drivers/media/cec/core/cec-pin.c
> > @@ -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
> 

That makes sense but let me check first. I will send a new one soon.

> >   		return HRTIMER_RESTART;
> >   	}
> >   	pin->wait_usecs = usecs - 100;
> 


-- 
Vitaliy Shevtsov <v.shevtsov@mt-integration.ru>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-03-01 21:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-01 11:09 [PATCH] media: cec: avoid wraparound in timer interval calculation Vitaliy Shevtsov
2025-03-01 11:32 ` Christophe JAILLET
2025-03-01 21:28   ` Vitaliy Shevtsov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®