* [PATCH] nfp: replace deprecated strncpy with strscpy
@ 2023-10-11 21:48 Justin Stitt
2023-10-11 23:50 ` Kees Cook
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Justin Stitt @ 2023-10-11 21:48 UTC (permalink / raw)
To: Louis Peens, Jakub Kicinski, David S. Miller, Eric Dumazet, Paolo Abeni
Cc: oss-drivers, netdev, linux-kernel, linux-hardening, Justin Stitt
strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.
We expect res->name to be NUL-terminated based on its usage with format
strings:
| dev_err(cpp->dev.parent, "Dangling area: %d:%d:%d:0x%0llx-0x%0llx%s%s\n",
| NFP_CPP_ID_TARGET_of(res->cpp_id),
| NFP_CPP_ID_ACTION_of(res->cpp_id),
| NFP_CPP_ID_TOKEN_of(res->cpp_id),
| res->start, res->end,
| res->name ? " " : "",
| res->name ? res->name : "");
... and with strcmp()
| if (!strcmp(res->name, NFP_RESOURCE_TBL_NAME)) {
Moreover, NUL-padding is not required as `res` is already
zero-allocated:
| res = kzalloc(sizeof(*res), GFP_KERNEL);
Considering the above, a suitable replacement is `strscpy` [2] due to
the fact that it guarantees NUL-termination on the destination buffer
without unnecessarily NUL-padding.
Let's also opt to use the more idiomatic strscpy() usage of (dest, src,
sizeof(dest)) rather than (dest, src, SOME_LEN).
Typically the pattern of 1) allocate memory for string, 2) copy string
into freshly-allocated memory is a candidate for kmemdup_nul() but in
this case we are allocating the entirety of the `res` struct and that
should stay as is. As mentioned above, simple 1:1 replacement of strncpy
-> strscpy :)
Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.
Found with: $ rg "strncpy\("
---
drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
index ce7492a6a98f..279ea0b56955 100644
--- a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
+++ b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
@@ -159,7 +159,7 @@ nfp_resource_acquire(struct nfp_cpp *cpp, const char *name)
if (!res)
return ERR_PTR(-ENOMEM);
- strncpy(res->name, name, NFP_RESOURCE_ENTRY_NAME_SZ);
+ strscpy(res->name, name, sizeof(res->name));
dev_mutex = nfp_cpp_mutex_alloc(cpp, NFP_RESOURCE_TBL_TARGET,
NFP_RESOURCE_TBL_BASE,
---
base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2
change-id: 20231011-strncpy-drivers-net-ethernet-netronome-nfp-nfpcore-nfp_resource-c-1812b8357fcd
Best regards,
--
Justin Stitt <justinstitt@google.com>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] nfp: replace deprecated strncpy with strscpy
2023-10-11 21:48 [PATCH] nfp: replace deprecated strncpy with strscpy Justin Stitt
@ 2023-10-11 23:50 ` Kees Cook
2023-10-12 15:05 ` Louis Peens
2023-10-14 0:20 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2023-10-11 23:50 UTC (permalink / raw)
To: Justin Stitt
Cc: Louis Peens, Jakub Kicinski, David S. Miller, Eric Dumazet,
Paolo Abeni, oss-drivers, netdev, linux-kernel, linux-hardening
On Wed, Oct 11, 2023 at 09:48:39PM +0000, Justin Stitt wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
>
> We expect res->name to be NUL-terminated based on its usage with format
> strings:
> | dev_err(cpp->dev.parent, "Dangling area: %d:%d:%d:0x%0llx-0x%0llx%s%s\n",
> | NFP_CPP_ID_TARGET_of(res->cpp_id),
> | NFP_CPP_ID_ACTION_of(res->cpp_id),
> | NFP_CPP_ID_TOKEN_of(res->cpp_id),
> | res->start, res->end,
> | res->name ? " " : "",
> | res->name ? res->name : "");
> ... and with strcmp()
> | if (!strcmp(res->name, NFP_RESOURCE_TBL_NAME)) {
>
> Moreover, NUL-padding is not required as `res` is already
> zero-allocated:
> | res = kzalloc(sizeof(*res), GFP_KERNEL);
>
> Considering the above, a suitable replacement is `strscpy` [2] due to
> the fact that it guarantees NUL-termination on the destination buffer
> without unnecessarily NUL-padding.
>
> Let's also opt to use the more idiomatic strscpy() usage of (dest, src,
> sizeof(dest)) rather than (dest, src, SOME_LEN).
>
> Typically the pattern of 1) allocate memory for string, 2) copy string
> into freshly-allocated memory is a candidate for kmemdup_nul() but in
> this case we are allocating the entirety of the `res` struct and that
> should stay as is. As mentioned above, simple 1:1 replacement of strncpy
> -> strscpy :)
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Note: build-tested only.
>
> Found with: $ rg "strncpy\("
> ---
> drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
> index ce7492a6a98f..279ea0b56955 100644
> --- a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
> +++ b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
> @@ -159,7 +159,7 @@ nfp_resource_acquire(struct nfp_cpp *cpp, const char *name)
> if (!res)
> return ERR_PTR(-ENOMEM);
>
> - strncpy(res->name, name, NFP_RESOURCE_ENTRY_NAME_SZ);
> + strscpy(res->name, name, sizeof(res->name));
struct nfp_resource {
char name[NFP_RESOURCE_ENTRY_NAME_SZ + 1];
Yup, this is doing the implicit string size reduction.
Reviewed-by: Kees Cook <keescook@chromium.org>
--
Kees Cook
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nfp: replace deprecated strncpy with strscpy
2023-10-11 21:48 [PATCH] nfp: replace deprecated strncpy with strscpy Justin Stitt
2023-10-11 23:50 ` Kees Cook
@ 2023-10-12 15:05 ` Louis Peens
2023-10-14 0:20 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Louis Peens @ 2023-10-12 15:05 UTC (permalink / raw)
To: Justin Stitt
Cc: Jakub Kicinski, David S. Miller, Eric Dumazet, Paolo Abeni,
oss-drivers, netdev, linux-kernel, linux-hardening
On Wed, Oct 11, 2023 at 09:48:39PM +0000, Justin Stitt wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
>
> We expect res->name to be NUL-terminated based on its usage with format
> strings:
> | dev_err(cpp->dev.parent, "Dangling area: %d:%d:%d:0x%0llx-0x%0llx%s%s\n",
> | NFP_CPP_ID_TARGET_of(res->cpp_id),
> | NFP_CPP_ID_ACTION_of(res->cpp_id),
> | NFP_CPP_ID_TOKEN_of(res->cpp_id),
> | res->start, res->end,
> | res->name ? " " : "",
> | res->name ? res->name : "");
> ... and with strcmp()
> | if (!strcmp(res->name, NFP_RESOURCE_TBL_NAME)) {
>
> Moreover, NUL-padding is not required as `res` is already
> zero-allocated:
> | res = kzalloc(sizeof(*res), GFP_KERNEL);
>
> Considering the above, a suitable replacement is `strscpy` [2] due to
> the fact that it guarantees NUL-termination on the destination buffer
> without unnecessarily NUL-padding.
>
> Let's also opt to use the more idiomatic strscpy() usage of (dest, src,
> sizeof(dest)) rather than (dest, src, SOME_LEN).
>
> Typically the pattern of 1) allocate memory for string, 2) copy string
> into freshly-allocated memory is a candidate for kmemdup_nul() but in
> this case we are allocating the entirety of the `res` struct and that
> should stay as is. As mentioned above, simple 1:1 replacement of strncpy
> -> strscpy :)
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Note: build-tested only.
>
> Found with: $ rg "strncpy\("
Thanks Justin, I did also now check it on a nfp.
Acked-by: Louis Peens <louis.peens@corigine.com>
> ---
> drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
> index ce7492a6a98f..279ea0b56955 100644
> --- a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
> +++ b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c
> @@ -159,7 +159,7 @@ nfp_resource_acquire(struct nfp_cpp *cpp, const char *name)
> if (!res)
> return ERR_PTR(-ENOMEM);
>
> - strncpy(res->name, name, NFP_RESOURCE_ENTRY_NAME_SZ);
> + strscpy(res->name, name, sizeof(res->name));
>
> dev_mutex = nfp_cpp_mutex_alloc(cpp, NFP_RESOURCE_TBL_TARGET,
> NFP_RESOURCE_TBL_BASE,
>
> ---
> base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2
> change-id: 20231011-strncpy-drivers-net-ethernet-netronome-nfp-nfpcore-nfp_resource-c-1812b8357fcd
>
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nfp: replace deprecated strncpy with strscpy
2023-10-11 21:48 [PATCH] nfp: replace deprecated strncpy with strscpy Justin Stitt
2023-10-11 23:50 ` Kees Cook
2023-10-12 15:05 ` Louis Peens
@ 2023-10-14 0:20 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-10-14 0:20 UTC (permalink / raw)
To: Justin Stitt
Cc: louis.peens, kuba, davem, edumazet, pabeni, oss-drivers, netdev,
linux-kernel, linux-hardening
Hello:
This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Wed, 11 Oct 2023 21:48:39 +0000 you wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
>
> We expect res->name to be NUL-terminated based on its usage with format
> strings:
> | dev_err(cpp->dev.parent, "Dangling area: %d:%d:%d:0x%0llx-0x%0llx%s%s\n",
> | NFP_CPP_ID_TARGET_of(res->cpp_id),
> | NFP_CPP_ID_ACTION_of(res->cpp_id),
> | NFP_CPP_ID_TOKEN_of(res->cpp_id),
> | res->start, res->end,
> | res->name ? " " : "",
> | res->name ? res->name : "");
> ... and with strcmp()
> | if (!strcmp(res->name, NFP_RESOURCE_TBL_NAME)) {
>
> [...]
Here is the summary with links:
- nfp: replace deprecated strncpy with strscpy
https://git.kernel.org/netdev/net-next/c/d273e99b5623
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-10-14 0:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-11 21:48 [PATCH] nfp: replace deprecated strncpy with strscpy Justin Stitt
2023-10-11 23:50 ` Kees Cook
2023-10-12 15:05 ` Louis Peens
2023-10-14 0:20 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).