diff mbox series

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

Message ID 99e06733f5f35c6cd62e05f530b93107bfd03362.1684358315.git.gustavoars@kernel.org (mailing list archive)
State Superseded
Headers show
Series scsi: lpfc: Replace one-element array with flexible-array member | expand

Commit Message

Gustavo A. R. Silva May 17, 2023, 9:23 p.m. UTC
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.

Link: https://github.com/KSPP/linux/issues/160
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Kees Cook May 17, 2023, 11:01 p.m. UTC | #1
On Wed, May 17, 2023 at 03:23:01PM -0600, Gustavo A. R. Silva wrote:
> 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.
> 
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/scsi/lpfc/lpfc_ct.c b/drivers/scsi/lpfc/lpfc_ct.c
> index e880d127d7f5..3b95c56023bf 100644
> --- a/drivers/scsi/lpfc/lpfc_ct.c
> +++ b/drivers/scsi/lpfc/lpfc_ct.c
> @@ -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...
Gustavo A. R. Silva May 23, 2023, 2:41 p.m. UTC | #2
On Wed, May 17, 2023 at 04:01:47PM -0700, Kees Cook wrote:
> On Wed, May 17, 2023 at 03:23:01PM -0600, Gustavo A. R. Silva wrote:
> > 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.
> > 
> > Link: https://github.com/KSPP/linux/issues/160
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> >  drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
> >  1 file changed, 2 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/scsi/lpfc/lpfc_ct.c b/drivers/scsi/lpfc/lpfc_ct.c
> > index e880d127d7f5..3b95c56023bf 100644
> > --- a/drivers/scsi/lpfc/lpfc_ct.c
> > +++ b/drivers/scsi/lpfc/lpfc_ct.c
> > @@ -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
Kees Cook May 23, 2023, 5:27 p.m. UTC | #3
On Tue, May 23, 2023 at 08:41:59AM -0600, Gustavo A. R. Silva wrote:
> On Wed, May 17, 2023 at 04:01:47PM -0700, Kees Cook wrote:
> > On Wed, May 17, 2023 at 03:23:01PM -0600, Gustavo A. R. Silva wrote:
> > > 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.
> > > 
> > > Link: https://github.com/KSPP/linux/issues/160
> > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > > ---
> > >  drivers/scsi/lpfc/lpfc_ct.c | 6 ++----
> > >  1 file changed, 2 insertions(+), 4 deletions(-)
> > > 
> > > diff --git a/drivers/scsi/lpfc/lpfc_ct.c b/drivers/scsi/lpfc/lpfc_ct.c
> > > index e880d127d7f5..3b95c56023bf 100644
> > > --- a/drivers/scsi/lpfc/lpfc_ct.c
> > > +++ b/drivers/scsi/lpfc/lpfc_ct.c
> > > @@ -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);

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.
diff mbox series

Patch

diff --git a/drivers/scsi/lpfc/lpfc_ct.c b/drivers/scsi/lpfc/lpfc_ct.c
index e880d127d7f5..3b95c56023bf 100644
--- a/drivers/scsi/lpfc/lpfc_ct.c
+++ b/drivers/scsi/lpfc/lpfc_ct.c
@@ -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);