Skip to content

Commit 674e318

Browse files
gal-pressmandavem330
authored andcommitted
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: 7ad17b0 ("net: trust the bitmap in __dev_alloc_name()") Cc: Jakub Kicinski <[email protected]> Reviewed-by: Vlad Buslov <[email protected]> Signed-off-by: Gal Pressman <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> Reviewed-by: Simon Horman <[email protected]> Reviewed-by: Jiri Pirko <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent efc0c83 commit 674e318

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

net/core/dev.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,9 @@ static int __dev_alloc_name(struct net *net, const char *name, char *res)
11191119
if (i == max_netdevices)
11201120
return -ENFILE;
11211121

1122-
snprintf(res, IFNAMSIZ, name, i);
1122+
/* 'res' and 'name' could overlap, use 'buf' as an intermediate buffer */
1123+
strscpy(buf, name, IFNAMSIZ);
1124+
snprintf(res, IFNAMSIZ, buf, i);
11231125
return i;
11241126
}
11251127

0 commit comments

Comments
 (0)