Skip to contentSkip to conversation

hvmloader/smbios: Change strncpy to memcpy for anchor strings.

PATCH In review Andrew Cooper 4 participants 8 messages Sep 23, 2013 – Sep 26, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hvmloader/smbios: Change strncpy to memcpy for anchor strings.

Coverity complains about the use of strncpy() to completely fill the anchor strings, resulting in an unterminated string.

Although the strncpy result is correct, the anchor strings are not strings in the C sense, and use of memcpy is the prevaling style elsewhere in hvmloader anyway.

While tidying up the style in this function, also remove some trailing whitespace and gratuitous cast.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 tools/firmware/hvmloader/smbios.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/firmware/hvmloader/smbios.c +5 -4
index 9f292cc..900f4e7 100644
@@ -347,18 +347,19 @@ smbios_entry_point_init(void *start,
 {
     uint8_t sum;
     int i;
-    struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
+    struct smbios_entry_point *ep = start;
 
     memset(ep, 0, sizeof(*ep));
 
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));
     ep->length = 0x1f;
     ep->smbios_major_version = 2;
     ep->smbios_minor_version = 4;
     ep->max_structure_size = max_structure_size;
     ep->entry_point_revision = 0;
-    strncpy(ep->intermediate_anchor_string, "_DMI_", 5);
-    
+    memcpy(ep->intermediate_anchor_string, "_DMI_",
+           sizeof(ep->intermediate_anchor_string));
+
     ep->structure_table_length = structure_table_length;
     ep->structure_table_address = structure_table_address;
     ep->number_of_structures = number_of_structures;
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 33 lines

-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Andrew Cooper
Sent: 23 September 2013 19:22
To: Xen-devel
Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich
Subject: [Xen-devel] [PATCH] hvmloader/smbios: Change strncpy to memcpy for anchor strings.

Coverity complains about the use of strncpy() to completely fill the anchor strings, resulting in an unterminated string.

Although the strncpy result is correct, the anchor strings are not strings in the C sense, and use of memcpy is the prevaling style elsewhere in hvmloader
anyway.

While tidying up the style in this function, also remove some trailing whitespace and gratuitous cast.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 tools/firmware/hvmloader/smbios.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/firmware/hvmloader/smbios.c +0 -0
index 9f292cc..900f4e7 100644

tools/firmware/hvmloader/smbios.c smbios_entry_point_init(void *start, · line 354

Quoted 10 lines
tools/firmware/hvmloader/smbios.c +2 -2
@@ -347,18 +347,19 @@ smbios_entry_point_init(void *start,
 {
     uint8_t sum;
     int i;
-    struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
+    struct smbios_entry_point *ep = start;
 
     memset(ep, 0, sizeof(*ep));
 
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));

Why the change from 4 to sizeof(ep->anchor_string) here (and similar below)? Setting the copy length based on the size of the destination rather than the source seems like the wrong thing to do.

Paul

Quoted 21 lines
tools/firmware/hvmloader/smbios.c +3 -3
     ep->length = 0x1f;
     ep->smbios_major_version = 2;
     ep->smbios_minor_version = 4;
     ep->max_structure_size = max_structure_size;
     ep->entry_point_revision = 0;
-    strncpy(ep->intermediate_anchor_string, "_DMI_", 5);
-
+    memcpy(ep->intermediate_anchor_string, "_DMI_",
+           sizeof(ep->intermediate_anchor_string));
+
     ep->structure_table_length = structure_table_length;
     ep->structure_table_address = structure_table_address;
     ep->number_of_structures = number_of_structures;
--

1.7.10.4

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 41 lines
Quoted 10 lines
Quoted 1 line · Paul Durrant

-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Andrew Cooper
Sent: 23 September 2013 19:22
To: Xen-devel
Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich
Subject: [Xen-devel] [PATCH] hvmloader/smbios: Change strncpy to memcpy for anchor strings.

Quoted 4 lines

Coverity complains about the use of strncpy() to completely fill the anchor strings, resulting in an unterminated string.

Although the strncpy result is correct, the anchor strings are not strings

in

Quoted 26 lines

the C sense, and use of memcpy is the prevaling style elsewhere in hvmloader
anyway.

While tidying up the style in this function, also remove some trailing whitespace and gratuitous cast.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 tools/firmware/hvmloader/smbios.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/firmware/hvmloader/smbios.c +1 -1
index 9f292cc..900f4e7 100644
@@ -347,18 +347,19 @@ smbios_entry_point_init(void *start,
 {
     uint8_t sum;
     int i;
-    struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
+    struct smbios_entry_point *ep = start;
 
Quoted 8 lines

tools/firmware/hvmloader/smbios.c smbios_entry_point_init(void *start, · line 354

Quoted 4 lines
tools/firmware/hvmloader/smbios.c +1 -1
     memset(ep, 0, sizeof(*ep));
 
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));

Why the change from 4 to sizeof(ep->anchor_string) here (and similar below)? Setting the copy length based on the size of the destination rather than the source seems like the wrong thing to do.

One can argue either way here:
- passing the destination's size guarantees no memory corruption
- passing the source's size guarantees no uninitialized memory

Since the structure fields involved here aren't going to change, either way is fine imo.

Jan

#3 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines

tools/firmware/hvmloader/smbios.c smbios_entry_point_init(void *start, · line 354

Quoted 2 lines
tools/firmware/hvmloader/smbios.c +1 -1
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));

Why the change from 4 to sizeof(ep->anchor_string) here (and similar below)? Setting the copy length based on the size of the destination rather than the source seems like the wrong thing to do.

Quoted 6 lines

One can argue either way here:
- passing the destination's size guarantees no memory corruption
- passing the source's size guarantees no uninitialized memory

Since the structure fields involved here aren't going to change, either way is fine imo.

As was the unadorned number 4, imo.

-- Keir

#4 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 7 lines · Jan Beulich
Quoted 6 lines

tools/firmware/hvmloader/smbios.c smbios_entry_point_init(void *start, · line 354

Quoted 2 lines
tools/firmware/hvmloader/smbios.c +1 -1
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));

Why the change from 4 to sizeof(ep->anchor_string) here (and similar below)? Setting the copy length based on the size of the destination rather than the source seems like the wrong thing to do.

Quoted 8 lines
Quoted 6 lines

One can argue either way here:
- passing the destination's size guarantees no memory corruption
- passing the source's size guarantees no uninitialized memory

Since the structure fields involved here aren't going to change, either way is fine imo.

As was the unadorned number 4, imo.

Yes - I'm fine with both; I don't even favor one over the other.

Jan

#5 lore raw
In reply to Keir Fraser
Quoted 11 lines · Keir Fraser
Quoted 9 lines · Jan Beulich
Quoted 5 lines

tools/firmware/hvmloader/smbios.c smbios_entry_point_init(void *start, · line 354

Quoted 2 lines
tools/firmware/hvmloader/smbios.c +1 -1
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));

Why the change from 4 to sizeof(ep->anchor_string) here (and similar below)? Setting the copy length based on the size of the destination rather than the source seems like the wrong thing to do.

One can argue either way here:
- passing the destination's size guarantees no memory corruption
- passing the source's size guarantees no uninitialized memory

Quoted 7 lines
Quoted 2 lines

Since the structure fields involved here aren't going to change, either way is fine imo.

As was the unadorned number 4, imo.

-- Keir

So what is the verdict here? I changed 4 to sizeof to match the prevailing style of other anchor strings in hvmloader.

I can resubmit and change back to hard coded numbers if that would cause the patch to be accepted.

~Andrew

Acked-by
#6 lore raw
Quoted 19 lines · Andrew Cooper
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

tools/firmware/hvmloader/smbios.c smbios_entry_point_init(void *start, · line 354

Quoted 2 lines
tools/firmware/hvmloader/smbios.c +1 -1
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));

Why the change from 4 to sizeof(ep->anchor_string) here (and similar below)?
Setting the copy length based on the size of the destination rather than the
source seems like the wrong thing to do.

One can argue either way here:
- passing the destination's size guarantees no memory corruption
- passing the source's size guarantees no uninitialized memory

Quoted 7 lines
Quoted 2 lines

Since the structure fields involved here aren't going to change, either way is fine imo.

As was the unadorned number 4, imo.

-- Keir

Quoted 5 lines

So what is the verdict here? I changed 4 to sizeof to match the prevailing style of other anchor strings in hvmloader.

I can resubmit and change back to hard coded numbers if that would cause the patch to be accepted.

Oh I don't care that much, though I would have left as hard coded numbers.

Did I give an Ack yet? Here you can have one:

-- Keir

#7 lore raw
In reply to Andrew Cooper
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

Coverity complains about the use of strncpy() to completely fill the anchor strings, resulting in an unterminated string.

Although the strncpy result is correct, the anchor strings are not strings in
the C sense, and use of memcpy is the prevaling style elsewhere in hvmloader anyway.

Quoted 5 lines

While tidying up the style in this function, also remove some trailing whitespace and gratuitous cast.

Reviewed by: Jan Beulich <[email protected]>

Quoted 35 lines
---
 tools/firmware/hvmloader/smbios.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/firmware/hvmloader/smbios.c +5 -4
index 9f292cc..900f4e7 100644
@@ -347,18 +347,19 @@ smbios_entry_point_init(void *start,
 {
     uint8_t sum;
     int i;
-    struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
+    struct smbios_entry_point *ep = start;
 
     memset(ep, 0, sizeof(*ep));
 
-    strncpy(ep->anchor_string, "_SM_", 4);
+    memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string));
     ep->length = 0x1f;
     ep->smbios_major_version = 2;
     ep->smbios_minor_version = 4;
     ep->max_structure_size = max_structure_size;
     ep->entry_point_revision = 0;
-    strncpy(ep->intermediate_anchor_string, "_DMI_", 5);
-    
+    memcpy(ep->intermediate_anchor_string, "_DMI_",
+           sizeof(ep->intermediate_anchor_string));
+
     ep->structure_table_length = structure_table_length;
     ep->structure_table_address = structure_table_address;
     ep->number_of_structures = number_of_structures;
Signature · 1 line
1.7.10.4