Skip to contentSkip to conversation

scsi: lpfc: Use struct_size() helper

PATCH Applied Justin Tee 3 participants 4 messages May 31, 2023 – Jun 15, 2023

Subsystems (2)
lore
Share & tools
lore

Justin Tee

author
#0 lore raw

PATCH v3 1/1 scsi: lpfc: Use struct_size() helper

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.

---
Changes in v3:
- Instead of hardcode to 1, use be32_to_cpu(rap->no_of_objects). (Justin Tee).

v2:
- Use literal 1 in call to struct_size(), instead of rap->no_of_objects (Kees Cook).

v1:
- Link:
https://lore.kernel.org/linux-hardening/99e06733f5f35c6cd62e05f530b93107bfd03362.1684358315.git.gustavoars@kernel.org/

---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/scsi/lpfc/lpfc_ct.c +2 -4
index e880d127d7f5..703429512ead 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, be32_to_cpu(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, be32_to_cpu(dap->no_of_objects));
 		write_lock(&vport->vmid_lock);
 		vmid->flag &= ~LPFC_VMID_REGISTERED;
 		write_unlock(&vport->vmid_lock);
Signature · 1 line
2.38.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · Justin Tee

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.

The SoB chain here looks weird -- I think this should include the C-d-b tags as well:

Quoted 1 line
Quoted 1 line
Quoted 1 line

But regardless, thank you -- looks good!

-Kees

Quoted 43 lines

---
Changes in v3:
- Instead of hardcode to 1, use be32_to_cpu(rap->no_of_objects). (Justin Tee).

v2:
- Use literal 1 in call to struct_size(), instead of rap->no_of_objects (Kees Cook).

v1:
- Link:

   https://lore.kernel.org/linux-hardening/99e06733f5f35c6cd62e05f530b93107bfd03362.1684358315.git.gustavoars@kernel.org/
---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/scsi/lpfc/lpfc_ct.c +2 -4
index e880d127d7f5..703429512ead 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, be32_to_cpu(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, be32_to_cpu(dap->no_of_objects));
 		write_lock(&vport->vmid_lock);
 		vmid->flag &= ~LPFC_VMID_REGISTERED;
 		write_unlock(&vport->vmid_lock);
Signature · 1 line
2.38.0
Signature · 1 line
Kees Cook
#2 lore raw
In reply to Justin Tee

Justin,

Quoted 6 lines

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.

Applied to 6.5/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#3 lore raw
In reply to Justin Tee
Quoted 8 lines · Justin Tee

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.

[...]

Applied to 6.5/scsi-queue, thanks!

[1/1] scsi: lpfc: Use struct_size() helper

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