[PATCH] nvme: fix fortify warnings

Dmitry Antipov dmantipov at yandex.ru
Wed Dec 6 04:56:57 PST 2023


When compiling with gcc version 14.0.0 20231129 (experimental) and
CONFIG_FORTIFY_SOURCE=y, I've noticed the following warnings:

...
In function 'strncpy',
    inlined from '__assign_req_name' at drivers/nvme/target/trace.h:56:2:
./include/linux/fortify-string.h:68:33: warning: '__builtin_strncpy' specified
bound depends on the length of the source argument [-Wstringop-truncation]
   68 | #define __underlying_strncpy    __builtin_strncpy
      |                                 ^
./include/linux/fortify-string.h:147:16: note: in expansion of macro
'__underlying_strncpy'
  147 |         return __underlying_strncpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~~
...

and:

...
In function 'fortify_memcpy_chk',
    inlined from 'perf_trace_nvmet_req_init' at drivers/nvme/target/trace.h:61:1:
./include/linux/fortify-string.h:588:25: warning: call to '__read_overflow2_field'
declared with attribute warning: detected read beyond size of field (2nd parameter);
maybe use struct_group()? [-Wattribute-warning]
  588 |                         __read_overflow2_field(q_size_field, size);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...

So replace unsafe and deprecated 'strncpy()' with 'strscpy()' and
pass 'struct_group(cdws,...)' member of 'struct nvme_common_command'
to 'memcpy()' (just like it's done in host/trace.h).

Fixes: 5c629dc9609d ("nvme: use struct group for generic command dwords")
Signed-off-by: Dmitry Antipov <dmantipov at yandex.ru>
---
 drivers/nvme/target/trace.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/target/trace.h b/drivers/nvme/target/trace.h
index 6109b3806b12..2d5e75643e6e 100644
--- a/drivers/nvme/target/trace.h
+++ b/drivers/nvme/target/trace.h
@@ -53,7 +53,7 @@ static inline void __assign_req_name(char *name, struct nvmet_req *req)
 		return;
 	}
 
-	strncpy(name, req->ns->device_path,
+	strscpy(name, req->ns->device_path,
 		min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path)));
 }
 #endif
@@ -85,7 +85,7 @@ TRACE_EVENT(nvmet_req_init,
 		__entry->flags = cmd->common.flags;
 		__entry->nsid = le32_to_cpu(cmd->common.nsid);
 		__entry->metadata = le64_to_cpu(cmd->common.metadata);
-		memcpy(__entry->cdw10, &cmd->common.cdw10,
+		memcpy(__entry->cdw10, &cmd->common.cdws,
 			sizeof(__entry->cdw10));
 	),
 	TP_printk("nvmet%s: %sqid=%d, cmdid=%u, nsid=%u, flags=%#x, "
-- 
2.43.0




More information about the Linux-nvme mailing list