Skip to contentSkip to conversation

Fix buffer size warning for strcpy

PATCH Superseded Nigel Croxon 2 participants 2 messages Aug 24, 2021 MD RAID

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

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.

---
 super-ddf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
super-ddf.c +2 -2
index dc8e512f..db2aaa7a 100644
@@ -2637,9 +2637,9 @@ static int init_super_ddf_bvd(struct supertype *st,
 		ve->init_state = DDF_init_not;
 
 	memset(ve->pad1, 0xff, 14);
-	memset(ve->name, ' ', 16);
+	memset(ve->name, '\0', sizeof(ve->name));
 	if (name)
-		strncpy(ve->name, name, 16);
+		memcpy(ve->name, name, strnlen(ve->name, sizeof(ve->name)));
 	ddf->virt->populated_vdes =
 		cpu_to_be16(be16_to_cpu(ddf->virt->populated_vdes)+1);
 
Signature · 1 line
2.29.2
#1 lore raw

Hello Nigel,

super-ddf.c static int init_super_ddf_bvd(struct supertype *st, · line 2644

Quoted 7 lines
super-ddf.c +2 -2
-	memset(ve->name, ' ', 16);
+	memset(ve->name, '\0', sizeof(ve->name));
  	if (name)
-		strncpy(ve->name, name, 16);
+		memcpy(ve->name, name, strnlen(ve->name, sizeof(ve->name)));
  	ddf->virt->populated_vdes =
  		cpu_to_be16(be16_to_cpu(ddf->virt->populated_vdes)+1);

As I wrote under v3, you should use 'name' instead 've->name' in strnlen. 've->name' has length 0. You can also consider usage of memccpy.

Thanks,
Mariusz