[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251126135042.06c1422b@pumpkin>
Date: Wed, 26 Nov 2025 13:50:42 +0000
From: david laight <david.laight@...box.com>
To: Thorsten Blum <thorsten.blum@...ux.dev>
Cc: "David S. Miller" <davem@...emloft.net>, David Ahern
<dsahern@...nel.org>, Eric Dumazet <edumazet@...gle.com>, Jakub Kicinski
<kuba@...nel.org>, Paolo Abeni <pabeni@...hat.com>, Simon Horman
<horms@...nel.org>, netdev@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH net-next] net: ipconfig: Replace strncpy with
strscpy_pad in ic_proto_name
On Wed, 26 Nov 2025 12:13:58 +0100
Thorsten Blum <thorsten.blum@...ux.dev> wrote:
> strncpy() is deprecated [1] for NUL-terminated destination buffers since
> it does not guarantee NUL termination. Replace it with strscpy_pad() to
> ensure NUL termination of the destination buffer while retaining the
> NUL-padding behavior of strncpy().
>
> Even though the identifier buffer has 252 usable bytes, strncpy()
> intentionally copied only 251 bytes into the zero-initialized buffer,
> implicitly relying on the last byte to act as the terminator. Switching
> to strscpy_pad() removes the need for this trick and avoids using magic
> numbers.
>
> The source string is also NUL-terminated and satisfies the
> __must_be_cstr() requirement of strscpy_pad().
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Signed-off-by: Thorsten Blum <thorsten.blum@...ux.dev>
> ---
> net/ipv4/ipconfig.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/net/ipv4/ipconfig.c b/net/ipv4/ipconfig.c
> index 22a7889876c1..27cc6f8070b7 100644
> --- a/net/ipv4/ipconfig.c
> +++ b/net/ipv4/ipconfig.c
> @@ -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_pad(dhcp_client_identifier + 1, v + 1,
> + sizeof(dhcp_client_identifier) - 1);
Wrong change...
There is no reason to pad the destination, and the correct alternative
is to bound 'v - client_id' and then use memcpy().
Then you don't need to modify the input buffer.
Although you might want to worry about the 'strange' strlen(dhcp_client_identifier + 1)
where the string is used.
David
> *v = ',';
> }
> return 1;
Powered by blists - more mailing lists