Skip to contentSkip to conversation

vmcore: replace strncpy with strscpy_pad

PATCH Applied Justin Stitt 3 participants 3 messages Apr 1, 2024 – Apr 4, 2024

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

PATCH v2 1/1 vmcore: replace strncpy with strscpy_pad

strncpy() is in the process of being replaced as it is deprecated [1]. We should move towards safer and less ambiguous string interfaces.

Looking at vmcoredd_header's definition:

|	struct vmcoredd_header {
|		__u32 n_namesz; /* Name size */
|		__u32 n_descsz; /* Content size */
|		__u32 n_type;   /* NT_VMCOREDD */
|		__u8 name[8];   /* LINUX\0\0\0 */
|		__u8 dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Device dump's name */
|	};

... we see that @name wants to be NUL-padded.

We're copying data->dump_name which is defined as:
| char dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Unique name of the dump */
... which shares the same size as vdd_hdr->dump_name. Let's make sure we NUL-pad this as well.

Use strscpy_pad() which NUL-terminates and NUL-pads its destination buffers. Specifically, use the new 2-argument version of strscpy_pad introduced in Commit e6584c3964f2f ("string: Allow 2-argument strscpy()").

---
Changes in v2:
- don't mark buffers as __nonstring, instead use a string API (thanks Kees)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Note: build-tested only.

Found with: $ rg "strncpy\("

---
 fs/proc/vmcore.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/proc/vmcore.c +2 -3
index 1fb213f379a5..5d08d4d159d3 100644
@@ -1370,9 +1370,8 @@ static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
 	vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
 	vdd_hdr->n_type = NT_VMCOREDD;
 
-	strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
-		sizeof(vdd_hdr->name));
-	memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
+	strscpy_pad(vdd_hdr->name, VMCOREDD_NOTE_NAME);
+	strscpy_pad(vdd_hdr->dump_name, data->dump_name);
 }
 
 /**

---
base-commit: 928a87efa42302a23bb9554be081a28058495f22
change-id: 20240327-strncpy-fs-proc-vmcore-c-b18d761feaef

Best regards,
--
Justin Stitt <[email protected]>

Acked-by on v2 1/1
#1 lore raw
Quoted 44 lines · Justin Stitt

strncpy() is in the process of being replaced as it is deprecated [1]. We should move towards safer and less ambiguous string interfaces.

Looking at vmcoredd_header's definition:

|	struct vmcoredd_header {
|		__u32 n_namesz; /* Name size */
|		__u32 n_descsz; /* Content size */
|		__u32 n_type;   /* NT_VMCOREDD */
|		__u8 name[8];   /* LINUX\0\0\0 */
|		__u8 dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Device dump's name */
|	};

... we see that @name wants to be NUL-padded.

We're copying data->dump_name which is defined as:
| char dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Unique name of the dump */
... which shares the same size as vdd_hdr->dump_name. Let's make sure we NUL-pad this as well.

Use strscpy_pad() which NUL-terminates and NUL-pads its destination buffers. Specifically, use the new 2-argument version of strscpy_pad introduced in Commit e6584c3964f2f ("string: Allow 2-argument strscpy()").

---
Changes in v2:
- don't mark buffers as __nonstring, instead use a string API (thanks Kees)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Note: build-tested only.

Found with: $ rg "strncpy\("

---
 fs/proc/vmcore.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/proc/vmcore.c +0 -0
index 1fb213f379a5..5d08d4d159d3 100644

fs/proc/vmcore.c static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data, · line 1374

Quoted 8 lines
fs/proc/vmcore.c +2 -3
@@ -1370,9 +1370,8 @@ static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data,
 	vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name);
 	vdd_hdr->n_type = NT_VMCOREDD;
 
-	strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME,
-		sizeof(vdd_hdr->name));
-	memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name));
+	strscpy_pad(vdd_hdr->name, VMCOREDD_NOTE_NAME);
+	strscpy_pad(vdd_hdr->dump_name, data->dump_name);

LGTM, thx

Reviewed-by on v2 1/1
#2 lore raw
In reply to Justin Stitt
Quoted 23 lines · Justin Stitt

strncpy() is in the process of being replaced as it is deprecated [1]. We should move towards safer and less ambiguous string interfaces.

Looking at vmcoredd_header's definition:

|	struct vmcoredd_header {
|		__u32 n_namesz; /* Name size */
|		__u32 n_descsz; /* Content size */
|		__u32 n_type;   /* NT_VMCOREDD */
|		__u8 name[8];   /* LINUX\0\0\0 */
|		__u8 dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Device dump's name */
|	};

... we see that @name wants to be NUL-padded.

We're copying data->dump_name which is defined as:
| char dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Unique name of the dump */
... which shares the same size as vdd_hdr->dump_name. Let's make sure we NUL-pad this as well.

Use strscpy_pad() which NUL-terminates and NUL-pads its destination buffers. Specifically, use the new 2-argument version of strscpy_pad introduced in Commit e6584c3964f2f ("string: Allow 2-argument strscpy()").

Quoted 4 lines

Looks good; thanks!

Signature · 1 line
Kees Cook