Skip to contentSkip to conversation

scsi: lpfc: Replace one-element array with flexible-array member

PATCH Applied Gustavo A. R. Silva 5 participants 13 messages May 17, 2023 – Jun 1, 2023

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

cover scsi: lpfc: Replace one-element array with flexible-array member

Hi!

This small series aims to replace a one-element array with a flexible-array member in a couple of structures.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

Gustavo A. R. Silva (2):
scsi: lpfc: Replace one-element array with flexible-array member
scsi: lpfc: Use struct_size() helper

 drivers/scsi/lpfc/lpfc_ct.c | 4 ++--
 drivers/scsi/lpfc/lpfc_hw.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 scsi: lpfc: Replace one-element array with flexible-array member

One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element arrays with flexible-array members in a couple of structures, and refactor the rest of the code, accordingly.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

This results in no differences in binary output.

---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++++--
 drivers/scsi/lpfc/lpfc_hw.h | 4 ++--
 2 files changed, 6 insertions(+), 4 deletions(-)
drivers/scsi/lpfc/lpfc_ct.c +4 -2
index f3bdcebe67f5..e880d127d7f5 100644
@@ -3748,7 +3748,8 @@ lpfc_vmid_cmd(struct lpfc_vport *vport,
 		rap->obj[0].entity_id_len = vmid->vmid_len;
 		memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
 		size = RAPP_IDENT_OFFSET +
-			sizeof(struct lpfc_vmid_rapp_ident_list);
+			sizeof(struct lpfc_vmid_rapp_ident_list) +
+			sizeof(struct entity_id_object);
 		retry = 1;
 		break;
 
@@ -3767,7 +3768,8 @@ lpfc_vmid_cmd(struct lpfc_vport *vport,
 		dap->obj[0].entity_id_len = vmid->vmid_len;
 		memcpy(dap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
 		size = DAPP_IDENT_OFFSET +
-			sizeof(struct lpfc_vmid_dapp_ident_list);
+			sizeof(struct lpfc_vmid_dapp_ident_list) +
+			sizeof(struct entity_id_object);
 		write_lock(&vport->vmid_lock);
 		vmid->flag &= ~LPFC_VMID_REGISTERED;
 		write_unlock(&vport->vmid_lock);
drivers/scsi/lpfc/lpfc_hw.h +2 -2
index 19b2d2754f32..b2123ec4df88 100644
@@ -1415,12 +1415,12 @@ struct app_id_object {
 
 struct lpfc_vmid_rapp_ident_list {
 	uint32_t no_of_objects;
-	struct entity_id_object obj[1];
+	struct entity_id_object obj[];
 };
 
 struct lpfc_vmid_dapp_ident_list {
 	uint32_t no_of_objects;
-	struct entity_id_object obj[1];
+	struct entity_id_object obj[];
 };
 
 #define GALLAPPIA_ID_LAST  0x80
Signature · 1 line
2.34.1
Reviewed-by on v1 1/2
#2 lore raw
Quoted 9 lines · Gustavo A. R. Silva

One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element arrays with flexible-array members in a couple of structures, and refactor the rest of the code, accordingly.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

Quoted 6 lines

This results in no differences in binary output.

Signature · 1 line
Kees Cook
#3 lore raw
In reply to Gustavo A. R. Silva

Gustavo,

Quoted 10 lines

One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element arrays with flexible-array members in a couple of structures, and refactor the rest of the code, accordingly.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

This results in no differences in binary output.

Applied to 6.5/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#4 lore raw
Quoted 2 lines · Martin K. Petersen

Applied to 6.5/scsi-staging, thanks!

Thanks, Martin.
--
Gustavo

#5 lore raw
In reply to Martin K. Petersen
Quoted 8 lines · Martin K. Petersen

Gustavo,

Quoted 5 lines

One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element arrays with flexible-array members in a couple of structures, and refactor the rest of the code, accordingly.

Quoted 7 lines
Quoted 5 lines

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

This results in no differences in binary output.

Applied to 6.5/scsi-staging, thanks!

Martin, I think this patch is not right -- can you drop this from staging for the moment?

Signature · 1 line
Kees Cook
#6 lore raw
Quoted 13 lines · Kees Cook
Quoted 12 lines · Martin K. Petersen

Gustavo,

Quoted 9 lines

One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element arrays with flexible-array members in a couple of structures, and refactor the rest of the code, accordingly.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

This results in no differences in binary output.

Applied to 6.5/scsi-staging, thanks!

Martin, I think this patch is not right -- can you drop this from staging for the moment?

Martin just took patch 1/2, which is correct:

https://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git/commit/?h=6.5/scsi-staging&id=e90644b0ce2d700a65579ac74ff594414e8ba30f

--
Gustavo

#7 lore raw
Quoted 18 lines · Gustavo A. R. Silva
Quoted 15 lines · Kees Cook
Quoted 14 lines · Martin K. Petersen

Gustavo,

Quoted 10 lines

One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element arrays with flexible-array members in a couple of structures, and refactor the rest of the code, accordingly.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

This results in no differences in binary output.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Applied to 6.5/scsi-staging, thanks!

Martin, I think this patch is not right -- can you drop this from staging for the moment?

Martin just took patch 1/2, which is correct:

https://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git/commit/?h=6.5/scsi-staging&id=e90644b0ce2d700a65579ac74ff594414e8ba30f

Oops, yes, I thought both got pulled. 1/2 is fine. :)

-Kees

Signature · 1 line
Kees Cook
#8 lore raw

PATCH 2/2 scsi: lpfc: Use struct_size() helper

In reply to Gustavo A. R. Silva

Prefer struct_size() over open-coded versions of idiom:

sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count

where count is the max number of items the flexible array is supposed to contain.

---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/scsi/lpfc/lpfc_ct.c +2 -4
index e880d127d7f5..3b95c56023bf 100644
@@ -3748,8 +3748,7 @@ lpfc_vmid_cmd(struct lpfc_vport *vport,
 		rap->obj[0].entity_id_len = vmid->vmid_len;
 		memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
 		size = RAPP_IDENT_OFFSET +
-			sizeof(struct lpfc_vmid_rapp_ident_list) +
-			sizeof(struct entity_id_object);
+			struct_size(rap, obj, rap->no_of_objects);
 		retry = 1;
 		break;
 
@@ -3768,8 +3767,7 @@ lpfc_vmid_cmd(struct lpfc_vport *vport,
 		dap->obj[0].entity_id_len = vmid->vmid_len;
 		memcpy(dap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
 		size = DAPP_IDENT_OFFSET +
-			sizeof(struct lpfc_vmid_dapp_ident_list) +
-			sizeof(struct entity_id_object);
+			struct_size(dap, obj, dap->no_of_objects);
 		write_lock(&vport->vmid_lock);
 		vmid->flag &= ~LPFC_VMID_REGISTERED;
 		write_unlock(&vport->vmid_lock);
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH 2/2][next] scsi: lpfc: Use struct_size() helper

Quoted 18 lines · Gustavo A. R. Silva

Prefer struct_size() over open-coded versions of idiom:

sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count

where count is the max number of items the flexible array is supposed to contain.

---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/scsi/lpfc/lpfc_ct.c +0 -0
index e880d127d7f5..3b95c56023bf 100644

drivers/scsi/lpfc/lpfc_ct.c lpfc_vmid_cmd(struct lpfc_vport *vport, · line 3751

Quoted 6 lines
drivers/scsi/lpfc/lpfc_ct.c +1 -2
@@ -3748,8 +3748,7 @@ lpfc_vmid_cmd(struct lpfc_vport *vport,
 		rap->obj[0].entity_id_len = vmid->vmid_len;
 		memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
 		size = RAPP_IDENT_OFFSET +
-			sizeof(struct lpfc_vmid_rapp_ident_list) +
-			sizeof(struct entity_id_object);
+			struct_size(rap, obj, rap->no_of_objects);

Has rap->no_of_objects always been "1"? (i.e. there was a prior multiplication here before...

Signature · 1 line
Kees Cook
#10 lore raw

Re: [PATCH 2/2][next] scsi: lpfc: Use struct_size() helper

Quoted 14 lines · Kees Cook
Quoted 13 lines · Gustavo A. R. Silva

Prefer struct_size() over open-coded versions of idiom:

sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count

where count is the max number of items the flexible array is supposed to contain.

---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
Quoted 14 lines

drivers/scsi/lpfc/lpfc_ct.c lpfc_vmid_cmd(struct lpfc_vport *vport, · line 3751

Quoted 11 lines
drivers/scsi/lpfc/lpfc_ct.c +1 -2
index e880d127d7f5..3b95c56023bf 100644
@@ -3748,8 +3748,7 @@ lpfc_vmid_cmd(struct lpfc_vport *vport,
 		rap->obj[0].entity_id_len = vmid->vmid_len;
 		memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
 		size = RAPP_IDENT_OFFSET +
-			sizeof(struct lpfc_vmid_rapp_ident_list) +
-			sizeof(struct entity_id_object);
+			struct_size(rap, obj, rap->no_of_objects);

Has rap->no_of_objects always been "1"? (i.e. there was a prior multiplication here before...

Mmh.. not sure what multiplication you are talking about. I based these changes on the fact that rap->no_of_objects is set to cpu_to_be32(1); for both instances. It doesn't show up in the context of the patch, so here you go:

3747                 rap->no_of_objects = cpu_to_be32(1);
3748                 rap->obj[0].entity_id_len = vmid->vmid_len;
3749                 memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
3750                 size = RAPP_IDENT_OFFSET +

3751 sizeof(struct lpfc_vmid_rapp_ident_list) +
3752 sizeof(struct entity_id_object);

--
Gustavo

#11 lore raw

Re: [PATCH 2/2][next] scsi: lpfc: Use struct_size() helper

Quoted 30 lines · Gustavo A. R. Silva
Quoted 14 lines · Kees Cook
Quoted 13 lines · Gustavo A. R. Silva

Prefer struct_size() over open-coded versions of idiom:

sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count

where count is the max number of items the flexible array is supposed to contain.

---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
Quoted 14 lines

drivers/scsi/lpfc/lpfc_ct.c lpfc_vmid_cmd(struct lpfc_vport *vport, · line 3751

Quoted 11 lines
drivers/scsi/lpfc/lpfc_ct.c +1 -2
index e880d127d7f5..3b95c56023bf 100644
@@ -3748,8 +3748,7 @@ lpfc_vmid_cmd(struct lpfc_vport *vport,
 		rap->obj[0].entity_id_len = vmid->vmid_len;
 		memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
 		size = RAPP_IDENT_OFFSET +
-			sizeof(struct lpfc_vmid_rapp_ident_list) +
-			sizeof(struct entity_id_object);
+			struct_size(rap, obj, rap->no_of_objects);

Has rap->no_of_objects always been "1"? (i.e. there was a prior multiplication here before...

Quoted 6 lines

Mmh.. not sure what multiplication you are talking about. I based these changes on the fact that rap->no_of_objects is set to cpu_to_be32(1); for both instances. It doesn't show up in the context of the patch, so here you go:

3747 rap->no_of_objects = cpu_to_be32(1);

Ah-ha! So, yeah, this patch is bad then, since no_of_objects will be a big-endian "1".

This change:

+ struct_size(rap, obj, rap->no_of_objects);

needs to explicitly be:

+ struct_size(rap, obj, 1);

Or, alternatively, just drop the patch entirely.

Signature · 1 line
Kees Cook
#12 lore raw
In reply to Gustavo A. R. Silva
Quoted 8 lines · Gustavo A. R. Silva

This small series aims to replace a one-element array with a flexible-array member in a couple of structures.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].

[...]

Applied to 6.5/scsi-queue, thanks!

[1/2] scsi: lpfc: Replace one-element array with flexible-array member

      https://git.kernel.org/mkp/scsi/c/e90644b0ce2d
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering