diff mbox series

ima: Fix sizeof mismatches

Message ID 20201007110243.19033-1-colin.king@canonical.com (mailing list archive)
State New, archived
Headers show
Series ima: Fix sizeof mismatches | expand

Commit Message

Colin King Oct. 7, 2020, 11:02 a.m. UTC
From: Colin Ian King <colin.king@canonical.com>

An incorrect sizeof is being used, sizeof(*fields) is not correct,
it should be sizeof(**fields). This is not causing a problem since
the size of these is the same. Fix this in the kmalloc_array and
memcpy calls.

Addresses-Coverity: ("Sizeof not portable (SIZEOF_MISMATCH)")
Fixes: 1bd7face7439 ("ima: allocate field pointers array on demand in template_desc_init_fields()")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 security/integrity/ima/ima_template.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Roberto Sassu Oct. 7, 2020, 12:38 p.m. UTC | #1
> From: Colin King [mailto:colin.king@canonical.com]
> Sent: Wednesday, October 7, 2020 1:03 PM
> From: Colin Ian King <colin.king@canonical.com>
> 
> An incorrect sizeof is being used, sizeof(*fields) is not correct,
> it should be sizeof(**fields). This is not causing a problem since
> the size of these is the same. Fix this in the kmalloc_array and
> memcpy calls.
> 
> Addresses-Coverity: ("Sizeof not portable (SIZEOF_MISMATCH)")
> Fixes: 1bd7face7439 ("ima: allocate field pointers array on demand in
> template_desc_init_fields()")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Thanks Colin.

Reviewed-by: Roberto Sassu <roberto.sassu@huawei.com>

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063
Managing Director: Li Peng, Li Jian, Shi Yanli

> ---
>  security/integrity/ima/ima_template.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/security/integrity/ima/ima_template.c
> b/security/integrity/ima/ima_template.c
> index 1e89e2d3851f..8884bbf03b43 100644
> --- a/security/integrity/ima/ima_template.c
> +++ b/security/integrity/ima/ima_template.c
> @@ -216,11 +216,11 @@ int template_desc_init_fields(const char
> *template_fmt,
>  	}
> 
>  	if (fields && num_fields) {
> -		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
> +		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
>  		if (*fields == NULL)
>  			return -ENOMEM;
> 
> -		memcpy(*fields, found_fields, i * sizeof(*fields));
> +		memcpy(*fields, found_fields, i * sizeof(**fields));
>  		*num_fields = i;
>  	}
> 
> --
> 2.27.0
Jarkko Sakkinen Oct. 7, 2020, 4:49 p.m. UTC | #2
On Wed, Oct 07, 2020 at 12:02:43PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> An incorrect sizeof is being used, sizeof(*fields) is not correct,
> it should be sizeof(**fields). This is not causing a problem since
> the size of these is the same. Fix this in the kmalloc_array and
> memcpy calls.
> 
> Addresses-Coverity: ("Sizeof not portable (SIZEOF_MISMATCH)")
> Fixes: 1bd7face7439 ("ima: allocate field pointers array on demand in template_desc_init_fields()")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Acked-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

/Jarkko
Joe Perches Oct. 7, 2020, 6:27 p.m. UTC | #3
On Wed, 2020-10-07 at 12:02 +0100, Colin King wrote:
> An incorrect sizeof is being used, sizeof(*fields) is not correct,
> it should be sizeof(**fields). This is not causing a problem since
> the size of these is the same. Fix this in the kmalloc_array and
> memcpy calls.
[]
> diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
[]
> @@ -216,11 +216,11 @@ int template_desc_init_fields(const char *template_fmt,
>  	}
>  
>  	if (fields && num_fields) {
> -		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
> +		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
>  		if (*fields == NULL)
>  			return -ENOMEM;
>  
> -		memcpy(*fields, found_fields, i * sizeof(*fields));
> +		memcpy(*fields, found_fields, i * sizeof(**fields));

Maybe use kmemdup instead.

	if (fields && num_fields) {
		*fields = kmemdup(found_fields, i * sizeof(**fields), GFP_KERNEL);
		etc...
Mimi Zohar Oct. 12, 2020, 5:51 p.m. UTC | #4
On Wed, 2020-10-07 at 11:27 -0700, Joe Perches wrote:
> On Wed, 2020-10-07 at 12:02 +0100, Colin King wrote:
> > An incorrect sizeof is being used, sizeof(*fields) is not correct,
> > it should be sizeof(**fields). This is not causing a problem since
> > the size of these is the same. Fix this in the kmalloc_array and
> > memcpy calls.
> []
> > diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
> []
> > @@ -216,11 +216,11 @@ int template_desc_init_fields(const char *template_fmt,
> >  	}
> >  
> >  	if (fields && num_fields) {
> > -		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
> > +		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
> >  		if (*fields == NULL)
> >  			return -ENOMEM;
> >  
> > -		memcpy(*fields, found_fields, i * sizeof(*fields));
> > +		memcpy(*fields, found_fields, i * sizeof(**fields));
> 
> Maybe use kmemdup instead.
> 
> 	if (fields && num_fields) {
> 		*fields = kmemdup(found_fields, i * sizeof(**fields), GFP_KERNEL);
> 		etc...
> 

Thanks, Joe.  Since this patch will be backported, perhaps it would be
better to leave this as a bug fix and upstream other changes
independently.

Mimi
Joe Perches Oct. 12, 2020, 6:06 p.m. UTC | #5
On Mon, 2020-10-12 at 13:51 -0400, Mimi Zohar wrote:
> On Wed, 2020-10-07 at 11:27 -0700, Joe Perches wrote:
> > On Wed, 2020-10-07 at 12:02 +0100, Colin King wrote:
> > > An incorrect sizeof is being used, sizeof(*fields) is not correct,
> > > it should be sizeof(**fields). This is not causing a problem since
> > > the size of these is the same. Fix this in the kmalloc_array and
> > > memcpy calls.
> > []
> > > diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
> > []
> > > @@ -216,11 +216,11 @@ int template_desc_init_fields(const char *template_fmt,
> > >  	}
> > >  
> > >  	if (fields && num_fields) {
> > > -		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
> > > +		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
> > >  		if (*fields == NULL)
> > >  			return -ENOMEM;
> > >  
> > > -		memcpy(*fields, found_fields, i * sizeof(*fields));
> > > +		memcpy(*fields, found_fields, i * sizeof(**fields));
> > 
> > Maybe use kmemdup instead.
> > 
> > 	if (fields && num_fields) {
> > 		*fields = kmemdup(found_fields, i * sizeof(**fields), GFP_KERNEL);
> > 		etc...
> > 
> 
> Thanks, Joe.  Since this patch will be backported, perhaps it would be
> better to leave this as a bug fix and upstream other changes
> independently.

IMO:

This patch doesn't need need backporting as it doesn't
actually fix anything other than a style defect.

void * and void ** are the same size.

cheers, Joe
Colin King Oct. 12, 2020, 6:10 p.m. UTC | #6
On 12/10/2020 19:06, Joe Perches wrote:
> On Mon, 2020-10-12 at 13:51 -0400, Mimi Zohar wrote:
>> On Wed, 2020-10-07 at 11:27 -0700, Joe Perches wrote:
>>> On Wed, 2020-10-07 at 12:02 +0100, Colin King wrote:
>>>> An incorrect sizeof is being used, sizeof(*fields) is not correct,
>>>> it should be sizeof(**fields). This is not causing a problem since
>>>> the size of these is the same. Fix this in the kmalloc_array and
>>>> memcpy calls.
>>> []
>>>> diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
>>> []
>>>> @@ -216,11 +216,11 @@ int template_desc_init_fields(const char *template_fmt,
>>>>  	}
>>>>  
>>>>  	if (fields && num_fields) {
>>>> -		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
>>>> +		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
>>>>  		if (*fields == NULL)
>>>>  			return -ENOMEM;
>>>>  
>>>> -		memcpy(*fields, found_fields, i * sizeof(*fields));
>>>> +		memcpy(*fields, found_fields, i * sizeof(**fields));
>>>
>>> Maybe use kmemdup instead.
>>>
>>> 	if (fields && num_fields) {
>>> 		*fields = kmemdup(found_fields, i * sizeof(**fields), GFP_KERNEL);
>>> 		etc...
>>>
>>
>> Thanks, Joe.  Since this patch will be backported, perhaps it would be
>> better to leave this as a bug fix and upstream other changes
>> independently.
> 
> IMO:
> 
> This patch doesn't need need backporting as it doesn't
> actually fix anything other than a style defect.
> 
> void * and void ** are the same size.

indeed, same size, it's a semantic difference *and* a style fix :-)

Colin

> 
> cheers, Joe
>
Mimi Zohar Oct. 13, 2020, 4:17 p.m. UTC | #7
On Mon, 2020-10-12 at 19:10 +0100, Colin Ian King wrote:
> On 12/10/2020 19:06, Joe Perches wrote:
> > On Mon, 2020-10-12 at 13:51 -0400, Mimi Zohar wrote:
> >> On Wed, 2020-10-07 at 11:27 -0700, Joe Perches wrote:
> >>> On Wed, 2020-10-07 at 12:02 +0100, Colin King wrote:
> >>>> An incorrect sizeof is being used, sizeof(*fields) is not correct,
> >>>> it should be sizeof(**fields). This is not causing a problem since
> >>>> the size of these is the same. Fix this in the kmalloc_array and
> >>>> memcpy calls.
> >>> []
> >>>> diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
> >>> []
> >>>> @@ -216,11 +216,11 @@ int template_desc_init_fields(const char *template_fmt,
> >>>>  	}
> >>>>  
> >>>>  	if (fields && num_fields) {
> >>>> -		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
> >>>> +		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
> >>>>  		if (*fields == NULL)
> >>>>  			return -ENOMEM;
> >>>>  
> >>>> -		memcpy(*fields, found_fields, i * sizeof(*fields));
> >>>> +		memcpy(*fields, found_fields, i * sizeof(**fields));
> >>>
> >>> Maybe use kmemdup instead.
> >>>
> >>> 	if (fields && num_fields) {
> >>> 		*fields = kmemdup(found_fields, i * sizeof(**fields), GFP_KERNEL);
> >>> 		etc...
> >>>
> >>
> >> Thanks, Joe.  Since this patch will be backported, perhaps it would be
> >> better to leave this as a bug fix and upstream other changes
> >> independently.
> > 
> > IMO:
> > 
> > This patch doesn't need need backporting as it doesn't
> > actually fix anything other than a style defect.
> > 
> > void * and void ** are the same size.
> 
> indeed, same size, it's a semantic difference *and* a style fix :-)

Colin, based on Joe's suggestion of using kmemdup and his opinion of
not backporting this change, can I assume you'll address his comments
and re-post v3?

thanks,

Mimi
Colin King Oct. 13, 2020, 4:23 p.m. UTC | #8
On 13/10/2020 17:17, Mimi Zohar wrote:
> On Mon, 2020-10-12 at 19:10 +0100, Colin Ian King wrote:
>> On 12/10/2020 19:06, Joe Perches wrote:
>>> On Mon, 2020-10-12 at 13:51 -0400, Mimi Zohar wrote:
>>>> On Wed, 2020-10-07 at 11:27 -0700, Joe Perches wrote:
>>>>> On Wed, 2020-10-07 at 12:02 +0100, Colin King wrote:
>>>>>> An incorrect sizeof is being used, sizeof(*fields) is not correct,
>>>>>> it should be sizeof(**fields). This is not causing a problem since
>>>>>> the size of these is the same. Fix this in the kmalloc_array and
>>>>>> memcpy calls.
>>>>> []
>>>>>> diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
>>>>> []
>>>>>> @@ -216,11 +216,11 @@ int template_desc_init_fields(const char *template_fmt,
>>>>>>  	}
>>>>>>  
>>>>>>  	if (fields && num_fields) {
>>>>>> -		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
>>>>>> +		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
>>>>>>  		if (*fields == NULL)
>>>>>>  			return -ENOMEM;
>>>>>>  
>>>>>> -		memcpy(*fields, found_fields, i * sizeof(*fields));
>>>>>> +		memcpy(*fields, found_fields, i * sizeof(**fields));
>>>>>
>>>>> Maybe use kmemdup instead.
>>>>>
>>>>> 	if (fields && num_fields) {
>>>>> 		*fields = kmemdup(found_fields, i * sizeof(**fields), GFP_KERNEL);
>>>>> 		etc...
>>>>>
>>>>
>>>> Thanks, Joe.  Since this patch will be backported, perhaps it would be
>>>> better to leave this as a bug fix and upstream other changes
>>>> independently.
>>>
>>> IMO:
>>>
>>> This patch doesn't need need backporting as it doesn't
>>> actually fix anything other than a style defect.
>>>
>>> void * and void ** are the same size.
>>
>> indeed, same size, it's a semantic difference *and* a style fix :-)
> 
> Colin, based on Joe's suggestion of using kmemdup and his opinion of
> not backporting this change, can I assume you'll address his comments
> and re-post v3?

Oops, I missed that email. Yep, I'll address that later today

Colin
> 
> thanks,
> 
> Mimi
>
diff mbox series

Patch

diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
index 1e89e2d3851f..8884bbf03b43 100644
--- a/security/integrity/ima/ima_template.c
+++ b/security/integrity/ima/ima_template.c
@@ -216,11 +216,11 @@  int template_desc_init_fields(const char *template_fmt,
 	}
 
 	if (fields && num_fields) {
-		*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
+		*fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
 		if (*fields == NULL)
 			return -ENOMEM;
 
-		memcpy(*fields, found_fields, i * sizeof(*fields));
+		memcpy(*fields, found_fields, i * sizeof(**fields));
 		*num_fields = i;
 	}