netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
Subsystems (3)
PATCH v2 1/1 netdevsim: Add trailing zero to terminate the string in nsim_nexthop_bucket_activity_write()
From: Zichen Xie <[email protected]>
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.
---
v2: adjust code format.
--- drivers/net/netdevsim/fib.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/netdevsim/fib.c +3 -1
@@ -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 ;
Signature · 1 line
2.34.1
Quoted 10 lines · Gax-c
From: Zichen Xie <[email protected]>
This was found by a static analyzer.
We should not forget the trailing zero aftercopy_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 <[email protected]>
The subject is a bit long though. Maybe make it just this?
netdevsim: nsim_nexthop_bucket_activity_write(): Add a terminating \0
Quoted 10 lines · Gax-c
From: Zichen Xie <[email protected]>
This was found by a static analyzer.
We should not forget the trailing zero aftercopy_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 <[email protected]>
Quoted 3 lines
---
v2: adjust code format.
---
For next time:
1. Subject prefix should be "[PATCH net v2]"
2. Wait 24h before reposting
Simon Horman
reviewerQuoted 21 lines · Gax-c
From: Zichen Xie <[email protected]>
This was found by a static analyzer.
We should not forget the trailing zero aftercopy_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 <[email protected]>---
v2: adjust code format.--- drivers/net/netdevsim/fib.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/net/netdevsim/fib.c +0
-0
drivers/net/netdevsim/fib.c static ssize_t nsim_nexthop_bucket_activity_write(struct file *file, · line 1380
Quoted 5 lines
drivers/net/netdevsim/fib.c +1
-1@@ -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.
Quoted 12 lines
drivers/net/netdevsim/fib.c +2
-0return -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 ;Signature · 1 line
2.34.1
Quoted 17 lines · Simon Horman
drivers/net/netdevsim/fib.c static ssize_t nsim_nexthop_bucket_activity_write(struct file *file, · line 1380
Quoted 10 lines · Gax-c
drivers/net/netdevsim/fib.c +1
-1@@ -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.
Quoted 2 lines
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?
Simon Horman
reviewerQuoted 20 lines · Petr Machata
Quoted 17 lines · Simon Horman
drivers/net/netdevsim/fib.c static ssize_t nsim_nexthop_bucket_activity_write(struct file *file, · line 1380
Quoted 10 lines · Gax-c
drivers/net/netdevsim/fib.c +1
-1@@ -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.
Quoted 8 lines
Quoted 2 lines
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.
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Tue, 22 Oct 2024 12:19:08 -0500 you wrote:
From: Zichen Xie <[email protected]>
This was found by a static analyzer.
We should not forget the trailing zero aftercopy_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!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html