From: david.laight.linux@gmail.com
To: Kees Cook <kees@kernel.org>, linux-hardening@vger.kernel.org
Cc: Arnd Bergmann <arnd@kernel.org>,
David Laight <david.laight.linux@gmail.com>
Subject: [PATCH next] nfp_nsp_hwinfo_lookup_optional: use strscpy() to copy default_val
Date: Mon, 8 Jun 2026 10:55:12 +0100 [thread overview]
Message-ID: <20260608095523.2606-28-david.laight.linux@gmail.com> (raw)
From: David Laight <david.laight.linux@gmail.com>
Use the result from strscpy() to detect overflow, only do so when the default
is used.
Replace min_t() with min().
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
This is one of a group of patches that remove potentially unbounded
strcpy() calls.
They are mostly replaced by strscpy() or, when strlen() has just been
called, with memcpy() (usually including the '\0').
Calls with copy string literals into arrays are left unchanged.
They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and
then fixing the code by hand.
Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain strcpy().
As well as 'difficult to fix' files, this included 'show' functions
as they really need to use sysfs_emit() or seq_printf().
All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags.
(There are about 100 patches in total.)
.../ethernet/netronome/nfp/nfpcore/nfp_nsp.c | 27 ++++++++-----------
1 file changed, 11 insertions(+), 16 deletions(-)
diff --git a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.c b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.c
index 5a1aadf6e17e..9fbc80334d8b 100644
--- a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.c
+++ b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.c
@@ -921,7 +921,7 @@ int nfp_nsp_hwinfo_lookup(struct nfp_nsp *state, void *buf, unsigned int size)
{
int err;
- size = min_t(u32, size, NFP_HWINFO_LOOKUP_SIZE);
+ size = min(size, NFP_HWINFO_LOOKUP_SIZE);
err = __nfp_nsp_hwinfo_lookup(state, buf, size, false);
if (err)
@@ -940,25 +940,15 @@ int nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state, void *buf,
{
int err;
- /* Ensure that the default value is usable irrespective of whether
- * it is actually going to be used.
- */
- if (strnlen(default_val, size) == size)
- return -EINVAL;
-
- if (!nfp_nsp_has_hwinfo_lookup(state)) {
- strcpy(buf, default_val);
- return 0;
- }
+ size = min(size, NFP_HWINFO_LOOKUP_SIZE);
- size = min_t(u32, size, NFP_HWINFO_LOOKUP_SIZE);
+ if (!nfp_nsp_has_hwinfo_lookup(state))
+ goto set_default;
err = __nfp_nsp_hwinfo_lookup(state, buf, size, true);
if (err) {
- if (err == -ENOENT) {
- strcpy(buf, default_val);
- return 0;
- }
+ if (err == -ENOENT)
+ goto set_default;
nfp_err(state->cpp, "NSP HWinfo lookup failed: %d\n", err);
return err;
@@ -970,6 +960,11 @@ int nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state, void *buf,
}
return 0;
+
+set_default:
+ if (strscpy(buf, default_val, size) < 0)
+ return -EINVAL;
+ return 0;
}
int nfp_nsp_hwinfo_set(struct nfp_nsp *state, void *buf, unsigned int size)
--
2.39.5
reply other threads:[~2026-06-08 9:55 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260608095523.2606-28-david.laight.linux@gmail.com \
--to=david.laight.linux@gmail.com \
--cc=arnd@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.