linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Replace strncpy with strscpy to ensure null-termination
@ 2025-03-23 14:31 kendrajmoore
  2025-06-08 14:59 ` Len Brown
  0 siblings, 1 reply; 2+ messages in thread
From: kendrajmoore @ 2025-03-23 14:31 UTC (permalink / raw)
  To: linux-pm; +Cc: lenb, linux-kernel, Kendra Moore

From: Kendra Moore <kendra.j.moore3443@gmail.com>

The field pcounter->name is used as a null-terminated string.
Using strncpy() with ARRAY_SIZE - 1 does not guarantee
null-termination and can lead to undefined behavior if the source
is too long.

This patch replaces it with strscpy() which ensures
null-termination and returns an error code if truncation occurs.

Signed-off-by: Kendra Moore <kendra.j.moore3443@gmail.com>
---
 tools/power/x86/turbostat/turbostat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 8d5011a0bf60..8f24e18d3de0 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -9345,7 +9345,7 @@ int pmt_add_counter(unsigned int guid, unsigned int seq, const char *name, enum
 	}
 
 	if (new_counter) {
-		strncpy(pcounter->name, name, ARRAY_SIZE(pcounter->name) - 1);
+		strscpy(pcounter->name, name, ARRAY_SIZE(pcounter->name));
 		pcounter->type = type;
 		pcounter->scope = scope;
 		pcounter->lsb = lsb;
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Replace strncpy with strscpy to ensure null-termination
  2025-03-23 14:31 [PATCH] Replace strncpy with strscpy to ensure null-termination kendrajmoore
@ 2025-06-08 14:59 ` Len Brown
  0 siblings, 0 replies; 2+ messages in thread
From: Len Brown @ 2025-06-08 14:59 UTC (permalink / raw)
  To: kendrajmoore; +Cc: linux-pm, linux-kernel

On Sun, Mar 23, 2025 at 10:31 AM kendrajmoore
<kendra.j.moore3443@gmail.com> wrote:

> --- a/tools/power/x86/turbostat/turbostat.c

> +               strscpy(pcounter->name, name, ARRAY_SIZE(pcounter->name));

Nope --  strscpy() is kernel specific, not available to user space utilities.

Len Brown, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-06-08 14:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-23 14:31 [PATCH] Replace strncpy with strscpy to ensure null-termination kendrajmoore
2025-06-08 14:59 ` Len Brown

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).