netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net v2] net: Fix undefined behavior in netdev name allocation
@ 2023-11-14  7:56 Gal Pressman
  2023-11-15  9:56 ` Simon Horman
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Gal Pressman @ 2023-11-14  7:56 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski
  Cc: Eric Dumazet, Paolo Abeni, Jiri Pirko, netdev, Simon Horman,
	Gal Pressman, Vlad Buslov

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

It is common to use 'dev->name' as the format string before a netdev is
registered, this results in 'res' and 'name' pointers being equal.
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: 7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")
Cc: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Vlad Buslov <vladbu@nvidia.com>
Signed-off-by: Gal Pressman <gal@nvidia.com>
Reviewed-by: Jakub Kicinski <kuba@kernel.org>
---
Changelog -
v1->v2: https://lore.kernel.org/all/20231113083544.1685919-1-gal@nvidia.com/
* Mention that dev->name is usually used as the format string in the
  commit message (Jakub).
* Put the right commit in the Fixes tag (Simon).
---
 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] 4+ messages in thread

* Re: [PATCH net v2] net: Fix undefined behavior in netdev name allocation
  2023-11-14  7:56 [PATCH net v2] net: Fix undefined behavior in netdev name allocation Gal Pressman
@ 2023-11-15  9:56 ` Simon Horman
  2023-11-15 10:14 ` Jiri Pirko
  2023-11-15 11:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Simon Horman @ 2023-11-15  9:56 UTC (permalink / raw)
  To: Gal Pressman
  Cc: David S. Miller, Jakub Kicinski, Eric Dumazet, Paolo Abeni,
	Jiri Pirko, netdev, Vlad Buslov

On Tue, Nov 14, 2023 at 09:56:18AM +0200, Gal Pressman wrote:
> Cited commit removed the strscpy() call and kept the snprintf() only.
> 
> It is common to use 'dev->name' as the format string before a netdev is
> registered, this results in 'res' and 'name' pointers being equal.
> 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: 7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")
> Cc: Jakub Kicinski <kuba@kernel.org>
> Reviewed-by: Vlad Buslov <vladbu@nvidia.com>
> Signed-off-by: Gal Pressman <gal@nvidia.com>
> Reviewed-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Simon Horman <horms@kernel.org>

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

* Re: [PATCH net v2] net: Fix undefined behavior in netdev name allocation
  2023-11-14  7:56 [PATCH net v2] net: Fix undefined behavior in netdev name allocation Gal Pressman
  2023-11-15  9:56 ` Simon Horman
@ 2023-11-15 10:14 ` Jiri Pirko
  2023-11-15 11:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Jiri Pirko @ 2023-11-15 10:14 UTC (permalink / raw)
  To: Gal Pressman
  Cc: David S. Miller, Jakub Kicinski, Eric Dumazet, Paolo Abeni,
	netdev, Simon Horman, Vlad Buslov

Tue, Nov 14, 2023 at 08:56:18AM CET, gal@nvidia.com wrote:
>Cited commit removed the strscpy() call and kept the snprintf() only.
>
>It is common to use 'dev->name' as the format string before a netdev is
>registered, this results in 'res' and 'name' pointers being equal.
>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: 7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")
>Cc: Jakub Kicinski <kuba@kernel.org>
>Reviewed-by: Vlad Buslov <vladbu@nvidia.com>
>Signed-off-by: Gal Pressman <gal@nvidia.com>
>Reviewed-by: Jakub Kicinski <kuba@kernel.org>


Reviewed-by: Jiri Pirko <jiri@nvidia.com>

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

* Re: [PATCH net v2] net: Fix undefined behavior in netdev name allocation
  2023-11-14  7:56 [PATCH net v2] net: Fix undefined behavior in netdev name allocation Gal Pressman
  2023-11-15  9:56 ` Simon Horman
  2023-11-15 10:14 ` Jiri Pirko
@ 2023-11-15 11:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-11-15 11:10 UTC (permalink / raw)
  To: Gal Pressman; +Cc: davem, kuba, edumazet, pabeni, jiri, netdev, horms, vladbu

Hello:

This patch was applied to netdev/net.git (main)
by David S. Miller <davem@davemloft.net>:

On Tue, 14 Nov 2023 09:56:18 +0200 you wrote:
> Cited commit removed the strscpy() call and kept the snprintf() only.
> 
> It is common to use 'dev->name' as the format string before a netdev is
> registered, this results in 'res' and 'name' pointers being equal.
> 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.
> 
> [...]

Here is the summary with links:
  - [net,v2] net: Fix undefined behavior in netdev name allocation
    https://git.kernel.org/netdev/net/c/674e31808946

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] 4+ messages in thread

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-14  7:56 [PATCH net v2] net: Fix undefined behavior in netdev name allocation Gal Pressman
2023-11-15  9:56 ` Simon Horman
2023-11-15 10:14 ` Jiri Pirko
2023-11-15 11:10 ` 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).