Skip to contentSkip to conversation

net: ipconfig: replace strncpy with strscpy

PATCH In review Pranav Tyagi 3 participants 5 messages Apr 12, 2025 – Apr 16, 2025

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

PATCH 1/1 net: ipconfig: replace strncpy with strscpy

Replace the deprecated strncpy() with strscpy() as the destination buffer is NUL-terminated and does not require any trailing NUL-padding.

---
 net/ipv4/ipconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ipconfig.c +1 -1
index c56b6fe6f0d7..eb9b32214e60 100644
@@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
 			*v = 0;
 			if (kstrtou8(client_id, 0, dhcp_client_identifier))
 				pr_debug("DHCP: Invalid client identifier type\n");
-			strncpy(dhcp_client_identifier + 1, v + 1, 251);
+			strscpy(dhcp_client_identifier + 1, v + 1, 251);
 			*v = ',';
 		}
 		return 1;
Signature · 1 line
2.49.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Pranav Tyagi

Replace the deprecated strncpy() with strscpy() as the destination buffer is NUL-terminated and does not require any trailing NUL-padding.

Thanks,

I agree that strscpy() is the correct choice here for the reasons you give above.

Quoted 9 lines
---
 net/ipv4/ipconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ipconfig.c +0 -0
index c56b6fe6f0d7..eb9b32214e60 100644

net/ipv4/ipconfig.c static int __init ic_proto_name(char *name) · line 1693

Quoted 5 lines
net/ipv4/ipconfig.c +1 -1
@@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
 			*v = 0;
 			if (kstrtou8(client_id, 0, dhcp_client_identifier))
 				pr_debug("DHCP: Invalid client identifier type\n");
-			strncpy(dhcp_client_identifier + 1, v + 1, 251);
+			strscpy(dhcp_client_identifier + 1, v + 1, 251);

As an aside, I'm curious to know why the length is 251 rather than 252 (sizeof(dhcp_client_identifier) -1). But that isn't strictly related to this patch.

Quoted 6 lines
net/ipv4/ipconfig.c +0 -0
 			*v = ',';
 		}
 		return 1;
Signature · 1 line
2.49.0
maintainer
#2 lore raw
Quoted 10 lines · On Tue, 15 Apr 2025 17:35:36 +0100 Simon Horman wrote:

net/ipv4/ipconfig.c static int __init ic_proto_name(char *name) · line 1693

Quoted 6 lines
net/ipv4/ipconfig.c +1 -1
@@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
 			*v = 0;
 			if (kstrtou8(client_id, 0, dhcp_client_identifier))
 				pr_debug("DHCP: Invalid client identifier type\n");
-			strncpy(dhcp_client_identifier + 1, v + 1, 251);
+			strscpy(dhcp_client_identifier + 1, v + 1, 251);  

As an aside, I'm curious to know why the length is 251 rather than 252 (sizeof(dhcp_client_identifier) -1). But that isn't strictly related to this patch.

Isn't this because strncpy() doesn't nul-terminate, and since this is a static variable if we use len - 1 we guarantee that there will be a null byte at the end? If we switch to strscpy we'll make the max string len 1 char shorter.

reviewer
#3 lore raw
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Tue, 15 Apr 2025 17:35:36 +0100 Simon Horman wrote:

net/ipv4/ipconfig.c static int __init ic_proto_name(char *name) · line 1693

Quoted 6 lines
net/ipv4/ipconfig.c +1 -1
@@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
 			*v = 0;
 			if (kstrtou8(client_id, 0, dhcp_client_identifier))
 				pr_debug("DHCP: Invalid client identifier type\n");
-			strncpy(dhcp_client_identifier + 1, v + 1, 251);
+			strscpy(dhcp_client_identifier + 1, v + 1, 251);  

As an aside, I'm curious to know why the length is 251 rather than 252 (sizeof(dhcp_client_identifier) -1). But that isn't strictly related to this patch.

Quoted 4 lines

Isn't this because strncpy() doesn't nul-terminate, and since this is a static variable if we use len - 1 we guarantee that there will be a null byte at the end? If we switch to strscpy we'll make the max string len 1 char shorter.

Yes, that makes sense to me.
And so I think the patch should also increase 251 to 252.

#4 lore raw
Quoted 14 lines · Simon Horman
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Tue, 15 Apr 2025 17:35:36 +0100 Simon Horman wrote:

net/ipv4/ipconfig.c static int __init ic_proto_name(char *name) · line 1693

Quoted 6 lines
net/ipv4/ipconfig.c +1 -1
@@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
                  *v = 0;
                  if (kstrtou8(client_id, 0, dhcp_client_identifier))
                          pr_debug("DHCP: Invalid client identifier type\n");
-                 strncpy(dhcp_client_identifier + 1, v + 1, 251);
+                 strscpy(dhcp_client_identifier + 1, v + 1, 251);

As an aside, I'm curious to know why the length is 251 rather than 252 (sizeof(dhcp_client_identifier) -1). But that isn't strictly related to this patch.

Quoted 7 lines
Quoted 4 lines

Isn't this because strncpy() doesn't nul-terminate, and since this is a static variable if we use len - 1 we guarantee that there will be a null byte at the end? If we switch to strscpy we'll make the max string len 1 char shorter.

Yes, that makes sense to me.
And so I think the patch should also increase 251 to 252.

Thanks for pointing that out. I appreciate the feedback and will send an updated version with the suggested change. Regards