diff mbox series

[net,v2] net: Fix undefined behavior in netdev name allocation

Message ID 20231114075618.1698547-1-gal@nvidia.com (mailing list archive)
State Accepted
Commit 674e318089468ece99aef4796eaef7add57f36b2
Delegated to: Netdev Maintainers
Headers show
Series [net,v2] net: Fix undefined behavior in netdev name allocation | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1142 this patch: 1142
netdev/cc_maintainers warning 1 maintainers not CCed: daniel@iogearbox.net
netdev/build_clang success Errors and warnings before: 1162 this patch: 1162
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1169 this patch: 1169
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Gal Pressman Nov. 14, 2023, 7:56 a.m. UTC
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(-)

Comments

Simon Horman Nov. 15, 2023, 9:56 a.m. UTC | #1
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>
Jiri Pirko Nov. 15, 2023, 10:14 a.m. UTC | #2
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>
patchwork-bot+netdevbpf@kernel.org Nov. 15, 2023, 11:10 a.m. UTC | #3
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!
diff mbox series

Patch

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;
 }