Skip to contentSkip to conversation

scsi: elx: sli4: Replace deprecated strncpy() with strscpy()

PATCH Superseded Thorsten Blum 2 participants 5 messages Feb 26, 2025 – Apr 7, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: elx: sli4: Replace deprecated strncpy() with strscpy()

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

---
 drivers/scsi/elx/libefc_sli/sli4.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/elx/libefc_sli/sli4.c +3 -3
index 5e7fb110bc3f..d9a231fc0e0d 100644
@@ -3804,7 +3804,7 @@ sli_cmd_common_write_object(struct sli4 *sli4, void *buf, u16 noc,
 	wr_obj->desired_write_len_dword = cpu_to_le32(dwflags);
 
 	wr_obj->write_offset = cpu_to_le32(offset);
-	strncpy(wr_obj->object_name, obj_name, sizeof(wr_obj->object_name) - 1);
+	strscpy(wr_obj->object_name, obj_name);
 	wr_obj->host_buffer_descriptor_count = cpu_to_le32(1);
 
 	bde = (struct sli4_bde *)wr_obj->host_buffer_descriptor;
@@ -3833,7 +3833,7 @@ sli_cmd_common_delete_object(struct sli4 *sli4, void *buf, char *obj_name)
 			 SLI4_SUBSYSTEM_COMMON, CMD_V0,
 			 SLI4_RQST_PYLD_LEN(cmn_delete_object));
 
-	strncpy(req->object_name, obj_name, sizeof(req->object_name) - 1);
+	strscpy(req->object_name, obj_name);
 	return 0;
 }
 
@@ -3856,7 +3856,7 @@ sli_cmd_common_read_object(struct sli4 *sli4, void *buf, u32 desired_read_len,
 		cpu_to_le32(desired_read_len & SLI4_REQ_DESIRE_READLEN);
 
 	rd_obj->read_offset = cpu_to_le32(offset);
-	strncpy(rd_obj->object_name, obj_name, sizeof(rd_obj->object_name) - 1);
+	strscpy(rd_obj->object_name, obj_name);
 	rd_obj->host_buffer_descriptor_count = cpu_to_le32(1);
 
 	bde = (struct sli4_bde *)rd_obj->host_buffer_descriptor;
Signature · 1 line
2.48.1
#1 lore raw
Quoted 17 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

---
 drivers/scsi/elx/libefc_sli/sli4.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/elx/libefc_sli/sli4.c +0 -0
index 5e7fb110bc3f..d9a231fc0e0d 100644

drivers/scsi/elx/libefc_sli/sli4.c sli_cmd_common_write_object(struct sli4 *sli4, void *buf, u16 noc, · line 3807

Quoted 5 lines
drivers/scsi/elx/libefc_sli/sli4.c +1 -1
@@ -3804,7 +3804,7 @@ sli_cmd_common_write_object(struct sli4 *sli4, void *buf, u16 noc,
 	wr_obj->desired_write_len_dword = cpu_to_le32(dwflags);
 
 	wr_obj->write_offset = cpu_to_le32(offset);
-	strncpy(wr_obj->object_name, obj_name, sizeof(wr_obj->object_name) - 1);
+	strscpy(wr_obj->object_name, obj_name);

Standard question for these kinds of conversions: Why is it safe that this is not NUL padded? I haven't found where this buffer is being zeroed out, but it probably is (given the "- 1" on the length), but without run-time testing, this needs much more careful analysis.

-Kees

Quoted 24 lines
drivers/scsi/elx/libefc_sli/sli4.c +2 -2
 	wr_obj->host_buffer_descriptor_count = cpu_to_le32(1);
 
 	bde = (struct sli4_bde *)wr_obj->host_buffer_descriptor;
@@ -3833,7 +3833,7 @@ sli_cmd_common_delete_object(struct sli4 *sli4, void *buf, char *obj_name)
 			 SLI4_SUBSYSTEM_COMMON, CMD_V0,
 			 SLI4_RQST_PYLD_LEN(cmn_delete_object));
 
-	strncpy(req->object_name, obj_name, sizeof(req->object_name) - 1);
+	strscpy(req->object_name, obj_name);
 	return 0;
 }
 
@@ -3856,7 +3856,7 @@ sli_cmd_common_read_object(struct sli4 *sli4, void *buf, u32 desired_read_len,
 		cpu_to_le32(desired_read_len & SLI4_REQ_DESIRE_READLEN);
 
 	rd_obj->read_offset = cpu_to_le32(offset);
-	strncpy(rd_obj->object_name, obj_name, sizeof(rd_obj->object_name) - 1);
+	strscpy(rd_obj->object_name, obj_name);
 	rd_obj->host_buffer_descriptor_count = cpu_to_le32(1);
 
 	bde = (struct sli4_bde *)rd_obj->host_buffer_descriptor;
Signature · 1 line
2.48.1
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 11 lines · Kees Cook
Quoted 9 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

---

Quoted 4 lines

Standard question for these kinds of conversions: Why is it safe that this is not NUL padded? I haven't found where this buffer is being zeroed out, but it probably is (given the "- 1" on the length), but without run-time testing, this needs much more careful analysis.

I think this was submitted before I started to explain this better.

'wr_obj' is the zeroed out 'buf' returned from sli_config_cmd_init().

I'll update the description and submit a v2.

Thanks,
Thorsten

#3 lore raw
Quoted 12 lines · Thorsten Blum
Quoted 11 lines · Kees Cook
Quoted 9 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

---

Quoted 8 lines
Quoted 4 lines

Standard question for these kinds of conversions: Why is it safe that this is not NUL padded? I haven't found where this buffer is being zeroed out, but it probably is (given the "- 1" on the length), but without run-time testing, this needs much more careful analysis.

I think this was submitted before I started to explain this better.

'wr_obj' is the zeroed out 'buf' returned from sli_config_cmd_init().

I don't see how dma->virt and buf are associated?

Quoted 2 lines

I'll update the description and submit a v2.

Thanks!

Quoted 4 lines

Thanks,
Thorsten

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 18 lines · Kees Cook
Quoted 17 lines · Thorsten Blum
Quoted 15 lines · Kees Cook
Quoted 9 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

---

Standard question for these kinds of conversions: Why is it safe that this is not NUL padded? I haven't found where this buffer is being zeroed out, but it probably is (given the "- 1" on the length), but without run-time testing, this needs much more careful analysis.

Quoted 5 lines
Quoted 3 lines

I think this was submitted before I started to explain this better.

'wr_obj' is the zeroed out 'buf' returned from sli_config_cmd_init().

I don't see how dma->virt and buf are associated?

Since dma is NULL, sli_config_cmd_init() returns config->payload.embed early (it doesn't get to return dma->virt) and before we have

    memset(buf, 0, SLI4_BMBX_SIZE);
    config = buf;

and SLI4_BMBX_SIZE is 256 which matches the size of sli4_cmd_sli_config.

It's not very obvious tbh.