Skip to contentSkip to conversation

net: ipconfig: replace strncpy with strscpy_pad

PATCH In review Pranav Tyagi 3 participants 5 messages Apr 8, 2025 – Apr 10, 2025

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

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

Replace the deprecated strncpy() function with strscpy_pad() as the destination buffer is NUL-terminated and requires trailing NUL-padding

---
 net/ipv4/ipconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ipconfig.c +1 -1
index c56b6fe6f0d7..7c238d19328f 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_pad(dhcp_client_identifier + 1, v + 1, 251);
 			*v = ',';
 		}
 		return 1;
Signature · 1 line
2.49.0
#1 lore raw
Quoted 6 lines · Pranav Tyagi

Replace the deprecated strncpy() function with strscpy_pad() as the destination buffer is NUL-terminated and requires trailing NUL-padding

There should be a Fixes tag here, and usually we put the 'net' tree indicator inside the tag, like this: [PATCH net]

Quoted 9 lines
---
  net/ipv4/ipconfig.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ipconfig.c +0 -0
index c56b6fe6f0d7..7c238d19328f 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_pad(dhcp_client_identifier + 1, v + 1, 251);

The strncpy() action, as well as the memcpy() into dhcp_client_identifier elsewhere, are not padding to the end, so I think this only needs to be null-terminated, not fully padded. If full padding is needed, please let us know why.

sln

Quoted 7 lines
net/ipv4/ipconfig.c +0 -1
                         *v = ',';
                 }
                 return 1;
--

2.49.0

#2 lore raw
Quoted 33 lines · Shannon
Quoted 6 lines · Pranav Tyagi

Replace the deprecated strncpy() function with strscpy_pad() as the destination buffer is NUL-terminated and requires trailing NUL-padding

There should be a Fixes tag here, and usually we put the 'net' tree indicator inside the tag, like this: [PATCH net]

Quoted 9 lines
---
  net/ipv4/ipconfig.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ipconfig.c +0 -0
index c56b6fe6f0d7..7c238d19328f 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_pad(dhcp_client_identifier + 1, v + 1, 251);

The strncpy() action, as well as the memcpy() into dhcp_client_identifier elsewhere, are not padding to the end, so I think this only needs to be null-terminated, not fully padded. If full padding is needed, please let us know why.

Quoted 10 lines

sln

Quoted 7 lines
net/ipv4/ipconfig.c +0 -1
                         *v = ',';
                 }
                 return 1;
--

2.49.0

My initial assumption was on the fact that dhcp_client_identifier is directly used in DHCP packet construction
and may be parsed byte-wise. But on going through the code again I see that it does not require to be fully padded. Would strscpy() suffice? as it ensures null-termination and does not fully pad the buffer.

Regards
Pranav Tyagi

#3 lore raw
Quoted 46 lines · Pranav Tyagi
Quoted 33 lines · Shannon
Quoted 6 lines · Pranav Tyagi

Replace the deprecated strncpy() function with strscpy_pad() as the destination buffer is NUL-terminated and requires trailing NUL-padding

There should be a Fixes tag here, and usually we put the 'net' tree indicator inside the tag, like this: [PATCH net]

Quoted 9 lines
---
   net/ipv4/ipconfig.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ipconfig.c +0 -0
index c56b6fe6f0d7..7c238d19328f 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_pad(dhcp_client_identifier + 1, v + 1, 251);

The strncpy() action, as well as the memcpy() into dhcp_client_identifier elsewhere, are not padding to the end, so I think this only needs to be null-terminated, not fully padded. If full padding is needed, please let us know why.

Quoted 10 lines

sln

Quoted 7 lines
net/ipv4/ipconfig.c +0 -1
                          *v = ',';
                  }
                  return 1;
--

2.49.0

Quoted 6 lines

My initial assumption was on the fact that dhcp_client_identifier is directly used in DHCP packet construction
and may be parsed byte-wise. But on going through the code again I see that it does not require to be fully padded. Would strscpy() suffice? as it ensures null-termination and does not fully pad the buffer.

Yes, strscpy() should work.
sln

Quoted 3 lines

Regards
Pranav Tyagi

reviewer
#4 lore raw
In reply to Nelson, Shannon
Quoted 10 lines · Shannon
Quoted 6 lines · Pranav Tyagi

Replace the deprecated strncpy() function with strscpy_pad() as the destination buffer is NUL-terminated and requires trailing NUL-padding

There should be a Fixes tag here, and usually we put the 'net' tree indicator inside the tag, like this: [PATCH net]

FWIIW, this feels more line net-next material to me:
I'm not seeing a bug that is being fixed.

Quoted 32 lines
Quoted 11 lines

---
  net/ipv4/ipconfig.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ipconfig.c +0 -0
index c56b6fe6f0d7..7c238d19328f 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_pad(dhcp_client_identifier + 1, v + 1, 251);

The strncpy() action, as well as the memcpy() into dhcp_client_identifier elsewhere, are not padding to the end, so I think this only needs to be null-terminated, not fully padded. If full padding is needed, please let us know why.

sln

Quoted 7 lines
net/ipv4/ipconfig.c +0 -1
                         *v = ',';
                 }
                 return 1;
--

2.49.0