netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
@ 2024-10-22 17:19 Gax-c
  2024-10-23 10:29 ` Petr Machata
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Gax-c @ 2024-10-22 17:19 UTC (permalink / raw)
  To: kuba, andrew+netdev, davem, edumazet, pabeni, petrm, idosch
  Cc: netdev, zzjas98, chenyuan0y, Zichen Xie

From: Zichen Xie <zichenxie0106@gmail.com>

This was found by a static analyzer.
We should not forget the trailing zero after copy_from_user()
if we will further do some string operations, sscanf() in this
case. Adding a trailing zero will ensure that the function
performs properly.

Fixes: c6385c0b67c5 ("netdevsim: Allow reporting activity on nexthop buckets")
Signed-off-by: Zichen Xie <zichenxie0106@gmail.com>
---
v2: adjust code format.
---
 drivers/net/netdevsim/fib.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c
index 41e80f78b316..16c382c42227 100644
--- a/drivers/net/netdevsim/fib.c
+++ b/drivers/net/netdevsim/fib.c
@@ -1377,10 +1377,12 @@ static ssize_t nsim_nexthop_bucket_activity_write(struct file *file,
 
 	if (pos != 0)
 		return -EINVAL;
-	if (size > sizeof(buf))
+	if (size > sizeof(buf) - 1)
 		return -EINVAL;
 	if (copy_from_user(buf, user_buf, size))
 		return -EFAULT;
+	buf[size] = 0;
+
 	if (sscanf(buf, "%u %hu", &nhid, &bucket_index) != 2)
 		return -EINVAL;
 
-- 
2.34.1


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

* Re: [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
  2024-10-22 17:19 [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write() Gax-c
@ 2024-10-23 10:29 ` Petr Machata
  2024-10-23 10:32 ` Petr Machata
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Petr Machata @ 2024-10-23 10:29 UTC (permalink / raw)
  To: Gax-c
  Cc: kuba, andrew+netdev, davem, edumazet, pabeni, petrm, idosch,
	netdev, zzjas98, chenyuan0y


Gax-c <zichenxie0106@gmail.com> writes:

> From: Zichen Xie <zichenxie0106@gmail.com>
>
> This was found by a static analyzer.
> We should not forget the trailing zero after copy_from_user()
> if we will further do some string operations, sscanf() in this
> case. Adding a trailing zero will ensure that the function
> performs properly.
>
> Fixes: c6385c0b67c5 ("netdevsim: Allow reporting activity on nexthop buckets")
> Signed-off-by: Zichen Xie <zichenxie0106@gmail.com>

Reviewed-by: Petr Machata <petrm@nvidia.com>

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

* Re: [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
  2024-10-22 17:19 [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write() Gax-c
  2024-10-23 10:29 ` Petr Machata
@ 2024-10-23 10:32 ` Petr Machata
  2024-10-23 13:38 ` Ido Schimmel
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Petr Machata @ 2024-10-23 10:32 UTC (permalink / raw)
  To: Gax-c
  Cc: kuba, andrew+netdev, davem, edumazet, pabeni, petrm, idosch,
	netdev, zzjas98, chenyuan0y

The subject is a bit long though. Maybe make it just this?

netdevsim: nsim_nexthop_bucket_activity_write(): Add a terminating \0

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

* Re: [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
  2024-10-22 17:19 [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write() Gax-c
  2024-10-23 10:29 ` Petr Machata
  2024-10-23 10:32 ` Petr Machata
@ 2024-10-23 13:38 ` Ido Schimmel
  2024-10-24 10:55 ` Simon Horman
  2024-10-29 18:50 ` patchwork-bot+netdevbpf
  4 siblings, 0 replies; 8+ messages in thread
From: Ido Schimmel @ 2024-10-23 13:38 UTC (permalink / raw)
  To: Gax-c
  Cc: kuba, andrew+netdev, davem, edumazet, pabeni, petrm, netdev,
	zzjas98, chenyuan0y

On Tue, Oct 22, 2024 at 12:19:08PM -0500, Gax-c wrote:
> From: Zichen Xie <zichenxie0106@gmail.com>
> 
> This was found by a static analyzer.
> We should not forget the trailing zero after copy_from_user()
> if we will further do some string operations, sscanf() in this
> case. Adding a trailing zero will ensure that the function
> performs properly.
> 
> Fixes: c6385c0b67c5 ("netdevsim: Allow reporting activity on nexthop buckets")
> Signed-off-by: Zichen Xie <zichenxie0106@gmail.com>

Reviewed-by: Ido Schimmel <idosch@nvidia.com>

> ---
> v2: adjust code format.
> ---

For next time:

1. Subject prefix should be "[PATCH net v2]"
2. Wait 24h before reposting

See:
https://docs.kernel.org/process/maintainer-netdev.html

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

* Re: [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
  2024-10-22 17:19 [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write() Gax-c
                   ` (2 preceding siblings ...)
  2024-10-23 13:38 ` Ido Schimmel
@ 2024-10-24 10:55 ` Simon Horman
  2024-10-24 12:15   ` Petr Machata
  2024-10-29 18:50 ` patchwork-bot+netdevbpf
  4 siblings, 1 reply; 8+ messages in thread
From: Simon Horman @ 2024-10-24 10:55 UTC (permalink / raw)
  To: Gax-c
  Cc: kuba, andrew+netdev, davem, edumazet, pabeni, petrm, idosch,
	netdev, zzjas98, chenyuan0y

On Tue, Oct 22, 2024 at 12:19:08PM -0500, Gax-c wrote:
> From: Zichen Xie <zichenxie0106@gmail.com>
> 
> This was found by a static analyzer.
> We should not forget the trailing zero after copy_from_user()
> if we will further do some string operations, sscanf() in this
> case. Adding a trailing zero will ensure that the function
> performs properly.
> 
> Fixes: c6385c0b67c5 ("netdevsim: Allow reporting activity on nexthop buckets")
> Signed-off-by: Zichen Xie <zichenxie0106@gmail.com>
> ---
> v2: adjust code format.
> ---
>  drivers/net/netdevsim/fib.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c
> index 41e80f78b316..16c382c42227 100644
> --- a/drivers/net/netdevsim/fib.c
> +++ b/drivers/net/netdevsim/fib.c
> @@ -1377,10 +1377,12 @@ static ssize_t nsim_nexthop_bucket_activity_write(struct file *file,
>  
>  	if (pos != 0)
>  		return -EINVAL;
> -	if (size > sizeof(buf))
> +	if (size > sizeof(buf) - 1)

I don't think this change for the best.
If the input data is well formatted it will end with a '\0'.
Which may be copied into the last byte of buf.

With this change the maximum size of the input data is
unnecessarily reduced by one.

>  		return -EINVAL;
>  	if (copy_from_user(buf, user_buf, size))
>  		return -EFAULT;
> +	buf[size] = 0;
> +
>  	if (sscanf(buf, "%u %hu", &nhid, &bucket_index) != 2)
>  		return -EINVAL;
>  
> -- 
> 2.34.1
> 
> 

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

* Re: [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
  2024-10-24 10:55 ` Simon Horman
@ 2024-10-24 12:15   ` Petr Machata
  2024-10-24 15:22     ` Simon Horman
  0 siblings, 1 reply; 8+ messages in thread
From: Petr Machata @ 2024-10-24 12:15 UTC (permalink / raw)
  To: Simon Horman
  Cc: Gax-c, kuba, andrew+netdev, davem, edumazet, pabeni, petrm,
	idosch, netdev, zzjas98, chenyuan0y


Simon Horman <horms@kernel.org> writes:

> On Tue, Oct 22, 2024 at 12:19:08PM -0500, Gax-c wrote:
>
>> diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c
>> index 41e80f78b316..16c382c42227 100644
>> --- a/drivers/net/netdevsim/fib.c
>> +++ b/drivers/net/netdevsim/fib.c
>> @@ -1377,10 +1377,12 @@ static ssize_t nsim_nexthop_bucket_activity_write(struct file *file,
>>  
>>  	if (pos != 0)
>>  		return -EINVAL;
>> -	if (size > sizeof(buf))
>> +	if (size > sizeof(buf) - 1)
>
> I don't think this change for the best.
> If the input data is well formatted it will end with a '\0'.
> Which may be copied into the last byte of buf.
>
> With this change the maximum size of the input data is
> unnecessarily reduced by one.

The buffer is 128 bytes, and sscanf'd into it is a u32 and u16, say 18
bytes or so total. Arguably the buffer is unnecessarily large. I think
the -1 above doesn't hurt.

Though if (user_buf[size - 1]) return -EINVAL; would work, too?

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

* Re: [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
  2024-10-24 12:15   ` Petr Machata
@ 2024-10-24 15:22     ` Simon Horman
  0 siblings, 0 replies; 8+ messages in thread
From: Simon Horman @ 2024-10-24 15:22 UTC (permalink / raw)
  To: Petr Machata
  Cc: Gax-c, kuba, andrew+netdev, davem, edumazet, pabeni, idosch,
	netdev, zzjas98, chenyuan0y

On Thu, Oct 24, 2024 at 02:15:54PM +0200, Petr Machata wrote:
> 
> Simon Horman <horms@kernel.org> writes:
> 
> > On Tue, Oct 22, 2024 at 12:19:08PM -0500, Gax-c wrote:
> >
> >> diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c
> >> index 41e80f78b316..16c382c42227 100644
> >> --- a/drivers/net/netdevsim/fib.c
> >> +++ b/drivers/net/netdevsim/fib.c
> >> @@ -1377,10 +1377,12 @@ static ssize_t nsim_nexthop_bucket_activity_write(struct file *file,
> >>  
> >>  	if (pos != 0)
> >>  		return -EINVAL;
> >> -	if (size > sizeof(buf))
> >> +	if (size > sizeof(buf) - 1)
> >
> > I don't think this change for the best.
> > If the input data is well formatted it will end with a '\0'.
> > Which may be copied into the last byte of buf.
> >
> > With this change the maximum size of the input data is
> > unnecessarily reduced by one.
> 
> The buffer is 128 bytes, and sscanf'd into it is a u32 and u16, say 18
> bytes or so total. Arguably the buffer is unnecessarily large. I think
> the -1 above doesn't hurt.
> 
> Though if (user_buf[size - 1]) return -EINVAL; would work, too?

It might be fun if size is 0.

I realised after sending that 128 is really just an arbitrary value,
and losing 1 byte is unlikely to hurt. So I withdraw my previous comment:
I think this patch is fine module the review already given by yourself
and Ido.

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

* Re: [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
  2024-10-22 17:19 [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write() Gax-c
                   ` (3 preceding siblings ...)
  2024-10-24 10:55 ` Simon Horman
@ 2024-10-29 18:50 ` patchwork-bot+netdevbpf
  4 siblings, 0 replies; 8+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-10-29 18:50 UTC (permalink / raw)
  To: Gax-c
  Cc: kuba, andrew+netdev, davem, edumazet, pabeni, petrm, idosch,
	netdev, zzjas98, chenyuan0y

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Tue, 22 Oct 2024 12:19:08 -0500 you wrote:
> From: Zichen Xie <zichenxie0106@gmail.com>
> 
> This was found by a static analyzer.
> We should not forget the trailing zero after copy_from_user()
> if we will further do some string operations, sscanf() in this
> case. Adding a trailing zero will ensure that the function
> performs properly.
> 
> [...]

Here is the summary with links:
  - [v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
    https://git.kernel.org/netdev/net/c/4ce1f56a1eac

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2024-10-29 18:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-22 17:19 [PATCH v2] netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write() Gax-c
2024-10-23 10:29 ` Petr Machata
2024-10-23 10:32 ` Petr Machata
2024-10-23 13:38 ` Ido Schimmel
2024-10-24 10:55 ` Simon Horman
2024-10-24 12:15   ` Petr Machata
2024-10-24 15:22     ` Simon Horman
2024-10-29 18:50 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).