Fix buffer size warning for strcpy
PATCH v2 1/1 Fix buffer size warning for strcpy
To meet requirements of Common Criteria certification vulnerability assessment. Static code analysis has been run and found the following error:buffer_size_warning: Calling "strncpy" with a maximum size argument of 16 bytes on destination array "ve->name" of size 16 bytes might leave the destination string unterminated.
The change is to make the destination size to fit the allocated size.
V2: Change from zero-terminated to zero-padded on memset and change from using strncpy to memcpy, feedback from Neil Brown.
--- super-ddf.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
super-ddf.c +7 -3
@@ -2637,9 +2637,13 @@ static int init_super_ddf_bvd(struct supertype *st, ve->init_state = DDF_init_not;memset (ve->pad1,0xff ,14 ); -memset (ve->name,' ' ,16 ); -if (name) -strncpy (ve->name, name,16 ); +memset (ve->name,'\0' ,sizeof (ve->name)); +if (name) { +int l =strlen (ve->name); +if (l >16 ) + l =16 ; +memcpy (ve->name, name, l); + } ddf->virt->populated_vdes =cpu_to_be16 (be16_to_cpu (ddf->virt->populated_vdes)+1 );
Signature · 1 line
2.29.2
Quoted 8 lines · Nigel Croxon
To meet requirements of Common Criteria certification vulnerability assessment. Static code analysis has been run and found the following error:
buffer_size_warning: Calling "strncpy" with a maximum size argument of 16 bytes on destination array "ve->name" of size 16 bytes might leave the destination string unterminated.The change is to make the destination size to fit the allocated size.
You really should explain here why we change from filling with spaces to filling with nuls.
Quoted 17 lines
V2: Change from zero-terminated to zero-padded on memset and change from using strncpy to memcpy, feedback from Neil Brown.
Signed-off-by Nigel Croxon <[email protected]>--- super-ddf.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)super-ddf.c +0
-0@@ -2637,9 +2637,13 @@ static int init_super_ddf_bvd(struct supertype *st, ve->init_state = DDF_init_not;memset (ve->pad1,0xff ,14 );
super-ddf.c static int init_super_ddf_bvd(struct supertype *st, · line 2646
Quoted 10 lines
super-ddf.c +7
-3-memset (ve->name,' ' ,16 ); -if (name) -strncpy (ve->name, name,16 ); +memset (ve->name,'\0' ,sizeof (ve->name)); +if (name) { +int l =strlen (ve->name); +if (l >16 ) + l =16 ; +memcpy (ve->name, name, l); + }
Thanks,
NeilBrown
Quoted 7 lines
super-ddf.c +0
-0ddf->virt->populated_vdes =cpu_to_be16 (be16_to_cpu (ddf->virt->populated_vdes)+1 );Signature · 1 line
2.29.2
Quoted 2 lines
You really should explain here why we change from filling with spaces to filling with nuls.
I guess that a commit-comment would still be needed, but I think that a more conventional approach could make it all clearer:
- memset(ve->name, ' ', 16);
- if (name)
- strncpy(ve->name, name, 16);
+ int l = strnlen(ve->name, 16);
+ memcpy(ve->name, name, l);
+ if (l < 16) /* no null-termination expected when all the space is used */
+ ve->name[l] = '\0';
Cheers,
Gal
Quoted 13 lines
Quoted 2 lines
You really should explain here why we change from filling with spaces to filling with nuls.
I guess that a commit-comment would still be needed, but I think that a more conventional approach could make it all clearer:
-
memset(ve->name, ' ', 16);
- if (name)(quoted diff) +4
-1- strncpy(ve->name, name, 16); + int l = strnlen(ve->name, 16); + memcpy(ve->name, name, l); + if (l < 16) /* no null-termination expected when all the space is used */ + ve->name[l] = '\0';
Quoted 2 lines
Cheers,
Gal
my bad - I obviously meant strnlen(name, 16).
Review
Quoted 12 lines · Gal Ofri
Quoted 2 lines
You really should explain here why we change from filling with spaces to filling with nuls.
I guess that a commit-comment would still be needed, but I think that a more conventional approach could make it all clearer:
-
memset(ve->name, ' ', 16);
- if (name)(quoted diff) +4
-1- strncpy(ve->name, name, 16); + int l = strnlen(ve->name, 16); + memcpy(ve->name, name, l); + if (l < 16) /* no null-termination expected when all the space is used */ + ve->name[l] = '\0';
No, that would be wrong. It doesn't clear the whole array.
NeilBrown
super-ddf.c static int init_super_ddf_bvd(struct supertype *st, · line 2646
Quoted 7 lines · Nigel Croxon
super-ddf.c +7
-0+memset (ve->name,'\0' ,sizeof (ve->name)); +if (name) { +int l =strlen (ve->name); +if (l >16 ) + l =16 ; +memcpy (ve->name, name, l); + }
What about:
if (name)
/* * Name might not be null terminated. */ strncpy(ve->name, name, sizeof(ve->name)); else memset(ve->name, '\0', sizeof(ve->name));
If size is less than sizeof(ve->name) then strncpy will automatically fill rest with "\0".
Mariusz
Quoted 12 lines · Mariusz
super-ddf.c static int init_super_ddf_bvd(struct supertype *st, · line 2646
Quoted 7 lines · Nigel Croxon
super-ddf.c +7
-0+memset (ve->name,'\0' ,sizeof (ve->name)); +if (name) { +int l =strlen (ve->name); +if (l >16 ) + l =16 ; +memcpy (ve->name, name, l); + }What about:
if (name)
Quoted 4 lines
/* * Name might not be null terminated. */ strncpy(ve->name, name, sizeof(ve->name));
I really like the idea of using strncpy(). I didn't realize it would nul-pad to the full size, and that is exactly what we want. So
strncpy(ve->name, name?:"", sizeof(ve->name));
would be a complete solution.
Thanks,
NeilBrown
Quoted 18 lines · NeilBrown
Quoted 12 lines · Mariusz
super-ddf.c static int init_super_ddf_bvd(struct supertype *st, · line 2646
Quoted 7 lines · Nigel Croxon
super-ddf.c +7
-0+memset (ve->name,'\0' ,sizeof (ve->name)); +if (name) { +int l =strlen (ve->name); +if (l >16 ) + l =16 ; +memcpy (ve->name, name, l); + }What about:
if (name)Quoted 4 lines
/* * Name might not be null terminated. */ strncpy(ve->name, name, sizeof(ve->name));
Quoted 8 lines
I really like the idea of using
strncpy(). I didn't realize it would nul-pad to the full size, and that is exactly what we want. Sostrncpy(ve->name, name?:"", sizeof(ve->name));would be a complete solution.
Except that won't get rid of the buffer warning that was the point of this patch:
buffer_size_warning: Calling "strncpy" with a maximum size argument of 16 bytes on destination array "ve->name" of size 16 bytes might leave the destination string unterminated.
Looking at the code, I don't think we're relying on the destination string being null terminated anyway (if it's the full 16 bytes), so it's not actually going to cause an issue, but we'll still be left with the warning. Presumably using memcpy doesn't flag on this (as it then doesn't know the value being copied is meant to be a string), which is why that was being proposed.
Cheers.
Robin