Commit 674e3180 authored by Gal Pressman's avatar Gal Pressman Committed by David S. Miller
Browse files

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: 7ad17b04 ("net: trust the bitmap in __dev_alloc_name()")
Cc: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: default avatarVlad Buslov <vladbu@nvidia.com>
Signed-off-by: default avatarGal Pressman <gal@nvidia.com>
Reviewed-by: default avatarJakub Kicinski <kuba@kernel.org>
Reviewed-by: default avatarSimon Horman <horms@kernel.org>
Reviewed-by: default avatarJiri Pirko <jiri@nvidia.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent efc0c836
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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;
}