Skip to contentSkip to conversation

netdevsim: nsim_nexthop_bucket_activity_write(): Add a terminating \0

PATCH New Gax-c 1 participant 1 message Oct 23, 2024

Subsystems (3)
lore
Share & tools
lore

Gax-c

author
#0 lore raw

PATCH v2 1/1 netdevsim: nsim_nexthop_bucket_activity_write(): Add a terminating \0

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.

Fixes c6385c0b67c5 ("netdevsim: Allow reporting activity on nexthop buckets")

---
v2: Adjust subject and code format.

---
 drivers/net/netdevsim/fib.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/netdevsim/fib.c +3 -1
index 41e80f78b316..16c382c42227 100644
@@ -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 · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/