[PATCH V5] Fix buffer size warning for strcpy

Subsystems: the rest

STALE1769d REVIEWED: 1 (0M)

1 review trailer.

3 messages, 3 authors, 2021-10-08 · open the first message on its own page

[PATCH V5] Fix buffer size warning for strcpy

From: Nigel Croxon <hidden>
Date: 2021-08-25 15:30:19

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.
https://people.redhat.com/ncroxon/mdadm-4.2-rc2-scan-results.html

The change is to make the destination size to fit the allocated size.

V5:
Simplify the the strnlen call.

V4:
Code cleanup of the interim "if" statement.

V3: Doc change only:
The code change from filling ve->name with spaces to filling it with
null-terminated is to comform to the SNIA - Common RAID Disk Data
Format Specification. The format for VD_Name (ve->name) specifies
the field to be either ASCII or UNICODE. Bit 2 of the VD_Type field
MUST be used to determine the Unicode or ASCII format of this field.
If this field is not used, all bytes MUST be set to zero.

V2: Change from zero-terminated to zero-padded on memset and
change from using strncpy to memcpy, feedback from Neil Brown.

Tested-by: Mariusz Tkaczyk <redacted>
Signed-off-by: Nigel Croxon <redacted>
---
 super-ddf.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/super-ddf.c b/super-ddf.c
index dc8e512f..2eb617e6 100644
--- a/super-ddf.c
+++ b/super-ddf.c
@@ -2637,9 +2637,11 @@ 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 = strnlen(name, sizeof(ve->name));
+		memcpy(ve->name, name, l);
+	}
 	ddf->virt->populated_vdes =
 		cpu_to_be16(be16_to_cpu(ddf->virt->populated_vdes)+1);
 
-- 
2.29.2

Re: [PATCH V5] Fix buffer size warning for strcpy

From: Tkaczyk, Mariusz <hidden>
Date: 2021-08-25 15:37:53

On 25.08.2021 17:30, Nigel Croxon wrote:
quoted hunk
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.
https://people.redhat.com/ncroxon/mdadm-4.2-rc2-scan-results.html

The change is to make the destination size to fit the allocated size.

V5:
Simplify the the strnlen call.

V4:
Code cleanup of the interim "if" statement.

V3: Doc change only:
The code change from filling ve->name with spaces to filling it with
null-terminated is to comform to the SNIA - Common RAID Disk Data
Format Specification. The format for VD_Name (ve->name) specifies
the field to be either ASCII or UNICODE. Bit 2 of the VD_Type field
MUST be used to determine the Unicode or ASCII format of this field.
If this field is not used, all bytes MUST be set to zero.

V2: Change from zero-terminated to zero-padded on memset and
change from using strncpy to memcpy, feedback from Neil Brown.

Tested-by: Mariusz Tkaczyk <redacted>
Signed-off-by: Nigel Croxon <redacted>
---
  super-ddf.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/super-ddf.c b/super-ddf.c
index dc8e512f..2eb617e6 100644
--- a/super-ddf.c
+++ b/super-ddf.c
@@ -2637,9 +2637,11 @@ 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 = strnlen(name, sizeof(ve->name));
+		memcpy(ve->name, name, l);
+	}
  	ddf->virt->populated_vdes =
  		cpu_to_be16(be16_to_cpu(ddf->virt->populated_vdes)+1);
  
Reviewed-by: Mariusz Tkaczyk <redacted>

Re: [PATCH V5] Fix buffer size warning for strcpy

From: Jes Sorensen <hidden>
Date: 2021-10-08 15:46:35

On 8/25/21 11:30 AM, Nigel Croxon wrote:
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.
https://people.redhat.com/ncroxon/mdadm-4.2-rc2-scan-results.html

The change is to make the destination size to fit the allocated size.

V5:
Simplify the the strnlen call.

V4:
Code cleanup of the interim "if" statement.

V3: Doc change only:
The code change from filling ve->name with spaces to filling it with
null-terminated is to comform to the SNIA - Common RAID Disk Data
Format Specification. The format for VD_Name (ve->name) specifies
the field to be either ASCII or UNICODE. Bit 2 of the VD_Type field
MUST be used to determine the Unicode or ASCII format of this field.
If this field is not used, all bytes MUST be set to zero.

V2: Change from zero-terminated to zero-padded on memset and
change from using strncpy to memcpy, feedback from Neil Brown.

Tested-by: Mariusz Tkaczyk <redacted>
Signed-off-by: Nigel Croxon <redacted>
---
 super-ddf.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Applied!

Thanks
Jes
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help