Skip to contentSkip to conversation

net: Fix undefined behavior in netdev name allocation

PATCH Superseded Gal Pressman 3 participants 7 messages Nov 13, 2023 – Nov 15, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

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

When allocating a netdev, 'res' and 'name' pointers are equal, but 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 9a810468126c ("net: reduce indentation of __dev_alloc_name()")
Cc Jakub Kicinski <[email protected]>
---
 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
#1 lore raw
Quoted 10 lines · Gal Pressman

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

When allocating a netdev, 'res' and 'name' pointers are equal, but 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 9a810468126c ("net: reduce indentation of __dev_alloc_name()")

Hi Gal,

perhaps my eyes are deceiving me, but I wonder if this fixes the following:

7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")

Quoted 26 lines
---
 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
#2 lore raw
Quoted 10 lines · Simon Horman
Quoted 9 lines · Gal Pressman

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

When allocating a netdev, 'res' and 'name' pointers are equal, but 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.

Quoted 7 lines
Quoted 1 line
Fixes 9a810468126c ("net: reduce indentation of __dev_alloc_name()")

Hi Gal,

perhaps my eyes are deceiving me, but I wonder if this fixes the following:

7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")

Thanks Simon, you're right.

Should I resubmit?

maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to Gal Pressman
Quoted 8 lines · On Mon, 13 Nov 2023 10:35:44 +0200 Gal Pressman wrote:

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

When allocating a netdev, 'res' and 'name' pointers are equal, but 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.

It may be worth mentioning that it is fairly common to put the format in dev->name before device is registered, IOW this condition takes place a lot? IIUC once we cross into 3-digit IDs we may crash?

With that and the right fixes tag:

Thanks!

Signature · 1 line
pw-bot: cr
maintainer
#5 lore raw
Quoted 1 line · On Mon, 13 Nov 2023 23:09:02 -0500 Jakub Kicinski wrote:

IIUC once we cross into 3-digit IDs we may crash?

Not really crash I guess just repeat some numbers until the end of the buffer.

#6 lore raw
In reply to Jakub Kicinski
Quoted 8 lines · Jakub Kicinski
Quoted 7 lines · On Mon, 13 Nov 2023 10:35:44 +0200 Gal Pressman wrote:

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

When allocating a netdev, 'res' and 'name' pointers are equal, but 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 5 lines
Quoted 1 line

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

It may be worth mentioning that it is fairly common to put the format in dev->name before device is registered, IOW this condition takes place a lot?

I'll mention it.

Quoted 1 line

IIUC once we cross into 3-digit IDs we may crash?

Right, the bitmap and names get out of sync, it results in sysfs name collision call traces.

Quoted 6 lines

With that and the right fixes tag:

Thanks!

Thanks Jakub!

reviewer Reviewed-by
#3 lore raw
In reply to Gal Pressman
Quoted 15 lines · Gal Pressman
Quoted 14 lines · Simon Horman
Quoted 10 lines · Gal Pressman

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

When allocating a netdev, 'res' and 'name' pointers are equal, but 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 9a810468126c ("net: reduce indentation of __dev_alloc_name()")

Hi Gal,

Quoted 7 lines
Quoted 3 lines

perhaps my eyes are deceiving me, but I wonder if this fixes the following:

7ad17b04dc7b ("net: trust the bitmap in __dev_alloc_name()")

Thanks Simon, you're right.

Should I resubmit?

I guess that it is not strictly necessary,
but it might be a good idea as I imagine it makes things slightly easier for the maintainers.

In any case, thanks for confirming and with this changed (somehow) this patch looks good to me.