netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] net: Fix undefined behavior in netdev name allocation
@ 2023-11-13  8:35 Gal Pressman
  2023-11-13  9:53 ` Simon Horman
  2023-11-14  4:09 ` Jakub Kicinski
  0 siblings, 2 replies; 7+ messages in thread
From: Gal Pressman @ 2023-11-13  8:35 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski; +Cc: netdev, Gal Pressman, Vlad Buslov

Cited commit removed the strscpy() call and kept the snprintf() only.

When allocating a netdev, 'res' and 'name' pointers are equal, but
according to POSIX, if copying takes place between objects that overlap
as a result of a call to sprintf() or snprintf(), the results are
undefined.

Add back the strscpy() and use 'buf' as an intermediate buffer.

Fixes: 9a810468126c ("net: reduce indentation of __dev_alloc_name()")
Cc: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Vlad Buslov <vladbu@nvidia.com>
Signed-off-by: Gal Pressman <gal@nvidia.com>
---
 net/core/dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index 0d548431f3fa..af53f6d838ce 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1119,7 +1119,9 @@ static int __dev_alloc_name(struct net *net, const char *name, char *res)
 	if (i == max_netdevices)
 		return -ENFILE;
 
-	snprintf(res, IFNAMSIZ, name, i);
+	/* 'res' and 'name' could overlap, use 'buf' as an intermediate buffer */
+	strscpy(buf, name, IFNAMSIZ);
+	snprintf(res, IFNAMSIZ, buf, i);
 	return i;
 }
 
-- 
2.40.1


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

end of thread, other threads:[~2023-11-15  9:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-13  8:35 [PATCH net] net: Fix undefined behavior in netdev name allocation Gal Pressman
2023-11-13  9:53 ` Simon Horman
2023-11-13 14:01   ` Gal Pressman
2023-11-15  9:28     ` Simon Horman
2023-11-14  4:09 ` Jakub Kicinski
2023-11-14  4:12   ` Jakub Kicinski
2023-11-14  7:26   ` Gal Pressman

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).