Skip to contentSkip to conversation

xfs: trace: replace strncpy() with strscpy()

PATCH New Arnd Bergmann 1 participant 1 message Jul 30, 2024 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: trace: replace strncpy() with strscpy()

From: Arnd Bergmann <[email protected]>

I got a warning about an strncpy() with possibly missing NUL-termination:

In file included from include/trace/define_trace.h:102,

                 from fs/xfs/scrub/trace.h:3584,
                 from fs/xfs/scrub/trace.c:58:

fs/xfs/./scrub/trace.h: In function 'trace_event_raw_event_xfile_create':
fs/xfs/./scrub/trace.h:973:3: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]

Use strscpy() to auto-size the output, and zero-pad the string as before.

Fixes 3934e8ebb7cc ("xfs: create a big array data structure")

---
I'm not entirely sure about whether to pad or not, as about half of the ftrace macros with strscpy() pad and the other half don't. We probably want them all to do the same thing here.

If there is a chance of leaking kernel stack data to userspace here, we need to pad the string, otherwise not.

---
 fs/xfs/scrub/trace.h | 2 +-
 fs/xfs/xfs_trace.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/xfs/scrub/trace.h +1 -1
index 92ef4cdc486e..6f7fa24e1273 100644
@@ -970,7 +970,7 @@ TRACE_EVENT(xfile_create,
 		path = file_path(xf->file, pathname, sizeof(pathname) - 1);
 		if (IS_ERR(path))
 			path = "(unknown)";
-		strncpy(__entry->pathname, path, sizeof(__entry->pathname));
+		strscpy_pad(__entry->pathname, path);
 	),
 	TP_printk("xfino 0x%lx path '%s'",
 		  __entry->ino,
fs/xfs/xfs_trace.h +1 -1
index 5646d300b286..79d159fab2e6 100644
@@ -4728,7 +4728,7 @@ TRACE_EVENT(xmbuf_create,
 		path = file_path(file, pathname, sizeof(pathname) - 1);
 		if (IS_ERR(path))
 			path = "(unknown)";
-		strncpy(__entry->pathname, path, sizeof(__entry->pathname));
+		strscpy_pad(__entry->pathname, path);
 	),
 	TP_printk("dev %d:%d xmino 0x%lx path '%s'",
 		  MAJOR(__entry->dev), MINOR(__entry->dev),
Signature · 1 line
2.39.2