Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Thorsten Blum <thorsten.blum@linux.dev>
Link: https://patch.msgid.link/20251126220804.102160-2-thorsten.blum@linux.dev
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Thorsten Blum and committed by
Jakub Kicinski
ff736a28 840a6471

+2 -1
+2 -1
net/ipv4/ipconfig.c
··· 1690 1690 *v = 0; 1691 1691 if (kstrtou8(client_id, 0, dhcp_client_identifier)) 1692 1692 pr_debug("DHCP: Invalid client identifier type\n"); 1693 - strncpy(dhcp_client_identifier + 1, v + 1, 251); 1693 + strscpy(dhcp_client_identifier + 1, v + 1, 1694 + sizeof(dhcp_client_identifier) - 1); 1694 1695 *v = ','; 1695 1696 } 1696 1697 return 1;