[PATCH] staging: comedi: ni_tio: Adjust clock period calculation in ni_tio_set_clock_src()

Aleksandr Mishin posted 1 patch 2 months, 1 week ago
drivers/comedi/drivers/ni_tio.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] staging: comedi: ni_tio: Adjust clock period calculation in ni_tio_set_clock_src()
Posted by Aleksandr Mishin 2 months, 1 week ago
In ni_tio_set_clock_src() clock period is a subject to overflow because
'period_ns' (which comes from user, may have any value and is not
validated anywhere) is not cast to a larger data type before performing
arithmetic.

Cast 'period_ns' to u64 to prevent overflow.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 3e90b1c7ebe9 ("staging: comedi: ni_tio: tidy up ni_tio_set_clock_src() and helpers")
Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
---
 drivers/comedi/drivers/ni_tio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/comedi/drivers/ni_tio.c b/drivers/comedi/drivers/ni_tio.c
index da6826d77e60..98b5b44416cf 100644
--- a/drivers/comedi/drivers/ni_tio.c
+++ b/drivers/comedi/drivers/ni_tio.c
@@ -800,7 +800,7 @@ static int ni_tio_set_clock_src(struct ni_gpct *counter,
 				GI_PRESCALE_X2(counter_dev->variant) |
 				GI_PRESCALE_X8(counter_dev->variant), bits);
 	}
-	counter->clock_period_ps = period_ns * 1000;
+	counter->clock_period_ps = (u64)period_ns * 1000;
 	ni_tio_set_sync_mode(counter);
 	return 0;
 }
-- 
2.30.2
Re: [PATCH] staging: comedi: ni_tio: Adjust clock period calculation in ni_tio_set_clock_src()
Posted by Ian Abbott 2 months, 1 week ago
On 17/09/2024 07:23, Aleksandr Mishin wrote:
> In ni_tio_set_clock_src() clock period is a subject to overflow because
> 'period_ns' (which comes from user, may have any value and is not
> validated anywhere) is not cast to a larger data type before performing
> arithmetic.
> 
> Cast 'period_ns' to u64 to prevent overflow.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: 3e90b1c7ebe9 ("staging: comedi: ni_tio: tidy up ni_tio_set_clock_src() and helpers")
> Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
> ---
>   drivers/comedi/drivers/ni_tio.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/comedi/drivers/ni_tio.c b/drivers/comedi/drivers/ni_tio.c
> index da6826d77e60..98b5b44416cf 100644
> --- a/drivers/comedi/drivers/ni_tio.c
> +++ b/drivers/comedi/drivers/ni_tio.c
> @@ -800,7 +800,7 @@ static int ni_tio_set_clock_src(struct ni_gpct *counter,
>   				GI_PRESCALE_X2(counter_dev->variant) |
>   				GI_PRESCALE_X8(counter_dev->variant), bits);
>   	}
> -	counter->clock_period_ps = period_ns * 1000;
> +	counter->clock_period_ps = (u64)period_ns * 1000;
>   	ni_tio_set_sync_mode(counter);
>   	return 0;
>   }

Thanks.  The patch looks good but since comedi was migrated out of 
staging, it should no longer be tagged as "staging:" in the title.

Reviewed-by: Ian Abbott <abbotti@mev.co.uk>

-- 
-=( Ian Abbott <abbotti@mev.co.uk> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
[PATCH v2] comedi: ni_tio: Adjust clock period calculation in ni_tio_set_clock_src()
Posted by Aleksandr Mishin 2 months, 1 week ago
In ni_tio_set_clock_src() clock period is a subject to overflow because
'period_ns' (which comes from user, may have any value and is not
validated anywhere) is not cast to a larger data type before performing
arithmetic.

Cast 'period_ns' to u64 to prevent overflow.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 3e90b1c7ebe9 ("staging: comedi: ni_tio: tidy up ni_tio_set_clock_src() and helpers")
Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
---
v1->v2: Remove "staging:" from the title,
 add "Reviewed-by: Ian Abbott <abbotti@mev.co.uk>"
 (https://lore.kernel.org/all/5c3172d8-517f-4d23-a713-fc41406c00d8@mev.co.uk/)

 drivers/comedi/drivers/ni_tio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/comedi/drivers/ni_tio.c b/drivers/comedi/drivers/ni_tio.c
index da6826d77e60..98b5b44416cf 100644
--- a/drivers/comedi/drivers/ni_tio.c
+++ b/drivers/comedi/drivers/ni_tio.c
@@ -800,7 +800,7 @@ static int ni_tio_set_clock_src(struct ni_gpct *counter,
 				GI_PRESCALE_X2(counter_dev->variant) |
 				GI_PRESCALE_X8(counter_dev->variant), bits);
 	}
-	counter->clock_period_ps = period_ns * 1000;
+	counter->clock_period_ps = (u64)period_ns * 1000;
 	ni_tio_set_sync_mode(counter);
 	return 0;
 }
-- 
2.30.2