Skip to contentSkip to conversation

net: Fix undefined behavior in netdev name allocation

PATCH Applied Gal Pressman 4 participants 4 messages Nov 14, 2023 – Nov 15, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: Fix undefined behavior in netdev name allocation

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 <[email protected]>

---
Changelog -
v1->v2: https://lore.kernel.org/all/[email protected]/
* 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(-)
net/core/dev.c +3 -1
index 0d548431f3fa..af53f6d838ce 100644
@@ -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;
 }
 
Signature · 1 line
2.40.1
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Gal Pressman

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.

Quoted 7 lines

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

Fixes 7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")
Cc Jakub Kicinski <[email protected]>
Reviewed-by on v2 1/1
#2 lore raw
In reply to Gal Pressman
Quoted 8 lines · Tue, Nov 14, 2023 at 08:56:18AM CET, [email protected] 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.

Quoted 7 lines

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

Fixes 7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")
Cc Jakub Kicinski <[email protected]>
#3 lore raw
In reply to Gal Pressman
This patch was applied to netdev/net.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:

Quoted 9 lines · 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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html