All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH next] nfp_nsp_hwinfo_lookup_optional: use strscpy() to copy default_val
@ 2026-06-08  9:55 david.laight.linux
  0 siblings, 0 replies; only message in thread
From: david.laight.linux @ 2026-06-08  9:55 UTC (permalink / raw)
  To: Kees Cook, linux-hardening; +Cc: Arnd Bergmann, David Laight

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-06-08  9:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-08  9:55 [PATCH next] nfp_nsp_hwinfo_lookup_optional: use strscpy() to copy default_val david.laight.linux

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.