Skip to contentSkip to conversation

nvmet: re-fix tracing strncpy() warning

PATCH Applied Arnd Bergmann 4 participants 8 messages Jan 3, 2024 – Jan 5, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 nvmet: re-fix tracing strncpy() warning

From: Arnd Bergmann <[email protected]>

An earlier patch had tried to address a warning about a string copy with missing zero termination:

drivers/nvme/target/trace.h:52:3: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation]

The new version causes a different warning with some compiler versions, notably gcc-9 and gcc-10, and also misses the zero padding that was apparently done intentionally in the original code:

drivers/nvme/target/trace.h:56:2: error: 'strncpy' specified bound depends on the length of the source argument [-Werror=stringop-overflow=]

Change it to use strscpy_pad() with the original length, which will give a properly padded and zero-terminated string as well as avoiding the warning.

Fixes d86481e924a7 ("nvmet: use min of device_path and disk len")
---
 drivers/nvme/target/trace.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/nvme/target/trace.h +1 -2
index 6109b3806b12..155334ddc13f 100644
@@ -53,8 +53,7 @@ static inline void __assign_req_name(char *name, struct nvmet_req *req)
 		return;
 	}
 
-	strncpy(name, req->ns->device_path,
-		min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path)));
+	strscpy_pad(name, req->ns->device_path, DISK_NAME_LEN);
 }
 #endif
 
Signature · 1 line
2.39.2
#1 lore raw

PATCH 2/2 nvme: trace: avoid memcpy overflow warning

From: Arnd Bergmann <[email protected]>

A previous patch introduced a struct_group() in nvme_common_command to help stringop fortification figure out the length of the fields, but one function is not currently using them:

In file included from drivers/nvme/target/core.c:7:
In file included from include/linux/string.h:254:
include/linux/fortify-string.h:592:4: error: call to '__read_overflow2_field' declared with 'warning' attribute: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]

                        __read_overflow2_field(q_size_field, size);
                        ^

Change this one to use the correct field name to avoid the warning.

Fixes 5c629dc9609dc ("nvme: use struct group for generic command dwords")
---
 drivers/nvme/target/trace.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/nvme/target/trace.h +1 -2
index 155334ddc13f..dbb911fd502d 100644
@@ -84,8 +84,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,
-			sizeof(__entry->cdw10));
+		memcpy(__entry->cdw10, &cmd->common.cdws, sizeof(__entry->cdw10));
 	),
 	TP_printk("nvmet%s: %sqid=%d, cmdid=%u, nsid=%u, flags=%#x, "
 		  "meta=%#llx, cmd=(%s, %s)",
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 2/2] nvme: trace: avoid memcpy overflow warning

drivers/nvme/target/trace.h TRACE_EVENT(nvmet_req_init, · line 87

Quoted 3 lines · Arnd Bergmann
drivers/nvme/target/trace.h +1 -2
-		memcpy(__entry->cdw10, &cmd->common.cdw10,
-			sizeof(__entry->cdw10));
+		memcpy(__entry->cdw10, &cmd->common.cdws, sizeof(__entry->cdw10));

Please avoid the overly long line.

maintainer
#4 lore raw
In reply to Arnd Bergmann

drivers/nvme/target/trace.h static inline void __assign_req_name(char *name, struct nvmet_req *req) · line 57

Quoted 8 lines · Arnd Bergmann
drivers/nvme/target/trace.h +1 -2
@@ -53,8 +53,7 @@ static inline void __assign_req_name(char *name, struct nvmet_req *req)
 		return;
 	}
 
-	strncpy(name, req->ns->device_path,
-		min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path)));
+	strscpy_pad(name, req->ns->device_path, DISK_NAME_LEN);
 }

I like this one, however Daniel has a different fix for this already staged in nvme-6.8:

https://git.infradead.org/nvme.git/commitdiff/8f6c0eec5fad13785fd53a5b3b5f8b97b722a2a3

maintainer Applied
#3 lore raw

Re: [PATCH 2/2] nvme: trace: avoid memcpy overflow warning

In reply to Arnd Bergmann
Quoted 9 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

A previous patch introduced a struct_group() in nvme_common_command to help stringop fortification figure out the length of the fields, but one function is not currently using them:

In file included from drivers/nvme/target/core.c:7:
In file included from include/linux/string.h:254:
include/linux/fortify-string.h:592:4: error: call to '__read_overflow2_field' declared with 'warning' attribute: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]

Quoted 7 lines
                        __read_overflow2_field(q_size_field, size);
                        ^

Change this one to use the correct field name to avoid the warning.

Fixes 5c629dc9609dc ("nvme: use struct group for generic command dwords")

Thanks, applied to nvme-6.8 with Christoph's line-wrap suggestion.

#5 lore raw
In reply to Keith Busch
Quoted 14 lines · Keith Busch

drivers/nvme/target/trace.h static inline void __assign_req_name(char *name, struct nvmet_req *req) · line 57

Quoted 8 lines · Arnd Bergmann
drivers/nvme/target/trace.h +1 -2
@@ -53,8 +53,7 @@ static inline void __assign_req_name(char *name, struct nvmet_req *req)
 		return;
 	}
 
-	strncpy(name, req->ns->device_path,
-		min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path)));
+	strscpy_pad(name, req->ns->device_path, DISK_NAME_LEN);
 }

I like this one, however Daniel has a different fix for this already staged in nvme-6.8:

Quoted 1 line

https://git.infradead.org/nvme.git/commitdiff/8f6c0eec5fad13785fd53a5b3b5f8b97b722a2a3

+ snprintf(name,
+ min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path) + 1),
+ "%s", req->ns->device_path);

Don't we still need the zero-padding here to avoid leaking kernel data to userspace?

Arnd

maintainer
#6 lore raw
Quoted 21 lines · Arnd Bergmann
Quoted 15 lines · Keith Busch

drivers/nvme/target/trace.h static inline void __assign_req_name(char *name, struct nvmet_req *req) · line 57

Quoted 8 lines · Arnd Bergmann
drivers/nvme/target/trace.h +1 -2
@@ -53,8 +53,7 @@ static inline void __assign_req_name(char *name, struct nvmet_req *req)
 		return;
 	}
 
-	strncpy(name, req->ns->device_path,
-		min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path)));
+	strscpy_pad(name, req->ns->device_path, DISK_NAME_LEN);
 }

I like this one, however Daniel has a different fix for this already staged in nvme-6.8:

https://git.infradead.org/nvme.git/commitdiff/8f6c0eec5fad13785fd53a5b3b5f8b97b722a2a3

(quoted diff) +3 -0
+       snprintf(name,
+                min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path) + 1),
+                "%s", req->ns->device_path);
Quoted 2 lines

Don't we still need the zero-padding here to avoid leaking kernel data to userspace?

I'm not sure. This potentially leaves trace buffer memory uninitialized after the string, but isn't the trace buffer user accessible when it was initially allocated?

For correctness, though, yes, I think you're right so I may just back out this one and replace with yours since we haven't sent a recent 6.8 pull request yet.

#7 lore raw
Quoted 22 lines · Keith Busch
Quoted 21 lines · Arnd Bergmann
Quoted 15 lines · Keith Busch

drivers/nvme/target/trace.h static inline void __assign_req_name(char *name, struct nvmet_req *req) · line 57

Quoted 8 lines · Arnd Bergmann
drivers/nvme/target/trace.h +1 -2
@@ -53,8 +53,7 @@ static inline void __assign_req_name(char *name, struct nvmet_req *req)
 		return;
 	}
 
-	strncpy(name, req->ns->device_path,
-		min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path)));
+	strscpy_pad(name, req->ns->device_path, DISK_NAME_LEN);
 }

I like this one, however Daniel has a different fix for this already staged in nvme-6.8:

https://git.infradead.org/nvme.git/commitdiff/8f6c0eec5fad13785fd53a5b3b5f8b97b722a2a3

(quoted diff) +3 -0
+       snprintf(name,
+                min_t(size_t, DISK_NAME_LEN, strlen(req->ns->device_path) + 1),
+                "%s", req->ns->device_path);
Quoted 6 lines
Quoted 2 lines

Don't we still need the zero-padding here to avoid leaking kernel data to userspace?

I'm not sure. This potentially leaves trace buffer memory uninitialized after the string, but isn't the trace buffer user accessible when it was initially allocated?

I'm always confused by how the tracing macros work exactly, so I don't know either. Looking through the other tracing headers with string copies, I see that about half of them use the padding variants (strncpy or strscpy_pad), while the other half use non-padding strscpy.

Arnd