Commit ff736a28 authored by Thorsten Blum's avatar Thorsten Blum Committed by Jakub Kicinski
Browse files

net: ipconfig: Replace strncpy with strscpy in ic_proto_name

strncpy() is deprecated [1] for NUL-terminated destination buffers
because it does not guarantee NUL termination. Replace it with strscpy()
to ensure the destination buffer is always NUL-terminated and to avoid
any additional NUL padding.

Although the identifier buffer has 252 usable bytes, strncpy() copied
only up to 251 bytes to the zero-initialized buffer, relying on the last
byte to act as an implicit NUL terminator. Switching to strscpy() avoids
this implicit behavior and does not use magic numbers.

The source string is also NUL-terminated and satisfies the
__must_be_cstr() requirement of strscpy().

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings

 [1]
Signed-off-by: default avatarThorsten Blum <thorsten.blum@linux.dev>
Link: https://patch.msgid.link/20251126220804.102160-2-thorsten.blum@linux.dev


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 840a6471
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -1690,7 +1690,8 @@ 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,
				sizeof(dhcp_client_identifier) - 1);
			*v = ',';
		}
		return 1;