Skip to contentSkip to conversation

xen/efi: Rewrite DOS/PE magic checking without memcmp()

PATCH In review Andrew Cooper 4 participants 8 messages Apr 16, 2024 – Apr 18, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/efi: Rewrite DOS/PE magic checking without memcmp()

Misra Rule 21.16 doesn't like the use of memcmp() between a string literal and a UINT8 array. Rewrite using plain compares.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: [email protected] <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Federico Serafini <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/common/efi/pe.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/common/efi/pe.c +6 -2
index a84992df9afe..ef8a2543e0a1 100644
@@ -111,7 +111,8 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
     UINTN offset, i;
 
     if ( image_size < sizeof(*dos) ||
-         memcmp(dos->Magic, "MZ", 2) != 0 )
+         dos->Magic[0] != 'M' ||
+         dos->Magic[1] != 'Z' )
         return NULL;
 
     offset = dos->ExeHeader;
@@ -119,7 +120,10 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
 
     offset += sizeof(*pe);
     if ( image_size < offset ||
-         memcmp(pe->Magic, "PE\0\0", 4) != 0 )
+         pe->Magic[0] != 'P' ||
+         pe->Magic[1] != 'E' ||
+         pe->Magic[2] != '\0' ||
+         pe->Magic[3] != '\0' )
         return NULL;
 
     if ( pe->FileHeader.Machine != PE_HEADER_MACHINE )

base-commit: c0f890cd9d5fd2c17a1e3110cb26f98c90ce8429

Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Andrew Cooper

Misra Rule 21.16 doesn't like the use of memcmp() between a string literal and a UINT8 array. Rewrite using plain compares.

No functional change.

Quoted 43 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: [email protected] <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Federico Serafini <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/common/efi/pe.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/common/efi/pe.c +6 -2
index a84992df9afe..ef8a2543e0a1 100644
@@ -111,7 +111,8 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
     UINTN offset, i;
 
     if ( image_size < sizeof(*dos) ||
-         memcmp(dos->Magic, "MZ", 2) != 0 )
+         dos->Magic[0] != 'M' ||
+         dos->Magic[1] != 'Z' )
         return NULL;
 
     offset = dos->ExeHeader;
@@ -119,7 +120,10 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
 
     offset += sizeof(*pe);
     if ( image_size < offset ||
-         memcmp(pe->Magic, "PE\0\0", 4) != 0 )
+         pe->Magic[0] != 'P' ||
+         pe->Magic[1] != 'E' ||
+         pe->Magic[2] != '\0' ||
+         pe->Magic[3] != '\0' )
         return NULL;
 
     if ( pe->FileHeader.Machine != PE_HEADER_MACHINE )

base-commit: c0f890cd9d5fd2c17a1e3110cb26f98c90ce8429

Signature · 1 line
2.30.2
Acked-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 2 lines · Andrew Cooper

Misra Rule 21.16 doesn't like the use of memcmp() between a string literal and a UINT8 array. Rewrite using plain compares.

The commit message makes it look like it's a type mismatch issue between the two elements being compared, but from my reading of the rule the issue is with the usage of a char pointer with memcmp().
IOW: even if the two parameters are char pointers it would still be a violation.

"Misra Rule 21.16 forbids the use of memcmp() against character arrays. Rewrite using plain compares since checking for "PE\0\0" cannot be done using strncmp()."

Quoted 4 lines

No functional change.

LGTM (possibly pending the adjustment of the commit message):

One question below to ensure my understating is correct.

Quoted 17 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: [email protected] <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Federico Serafini <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/common/efi/pe.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/common/efi/pe.c +0 -0
index a84992df9afe..ef8a2543e0a1 100644

xen/common/efi/pe.c const void *__init pe_find_section(const void *image, const UINTN image_size, · line 115

Quoted 6 lines
xen/common/efi/pe.c +2 -1
@@ -111,7 +111,8 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
     UINTN offset, i;
 
     if ( image_size < sizeof(*dos) ||
-         memcmp(dos->Magic, "MZ", 2) != 0 )
+         dos->Magic[0] != 'M' ||
+         dos->Magic[1] != 'Z' )

For this one you could likely use strncmp()?

xen/common/efi/pe.c const void *__init pe_find_section(const void *image, const UINTN image_size, · line 126

Quoted 12 lines
xen/common/efi/pe.c +4 -1
         return NULL;
 
     offset = dos->ExeHeader;
@@ -119,7 +120,10 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
 
     offset += sizeof(*pe);
     if ( image_size < offset ||
-         memcmp(pe->Magic, "PE\0\0", 4) != 0 )
+         pe->Magic[0] != 'P' ||
+         pe->Magic[1] != 'E' ||
+         pe->Magic[2] != '\0' ||
+         pe->Magic[3] != '\0' )

This one with the double null terminator is indeed not suitable to be checked using strncmp().

Thanks, Roger.

#3 lore raw
Quoted 6 lines · Roger Pau Monné
Quoted 5 lines · Andrew Cooper
xen/common/efi/pe.c +0 -0
@@ -111,7 +111,8 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
     UINTN offset, i;
 
Quoted 6 lines

xen/common/efi/pe.c const void *__init pe_find_section(const void *image, const UINTN image_size, · line 115

Quoted 4 lines
xen/common/efi/pe.c +2 -1
     if ( image_size < sizeof(*dos) ||
-         memcmp(dos->Magic, "MZ", 2) != 0 )
+         dos->Magic[0] != 'M' ||
+         dos->Magic[1] != 'Z' )

For this one you could likely use strncmp()?

strncmp() against UINT8[2] wouldn't be liked by the compiler, I guess.

Jan

#4 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Roger Pau Monné
Quoted 5 lines · Andrew Cooper
xen/common/efi/pe.c +0 -0
@@ -111,7 +111,8 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
     UINTN offset, i;
 
Quoted 6 lines
Quoted 5 lines

xen/common/efi/pe.c const void *__init pe_find_section(const void *image, const UINTN image_size, · line 115

Quoted 4 lines
xen/common/efi/pe.c +2 -1
     if ( image_size < sizeof(*dos) ||
-         memcmp(dos->Magic, "MZ", 2) != 0 )
+         dos->Magic[0] != 'M' ||
+         dos->Magic[1] != 'Z' )

For this one you could likely use strncmp()?

strncmp() against UINT8[2] wouldn't be liked by the compiler, I guess.

Indeed.  And this MISRA rule is very much "you are mixing string and non-string types.  Don't do that."

This is a very rare patten, where we are looking for a binary marker than just happens to also make sense when expressed as an ASCII string.

Although the MISRA complaint does raise a good point.   The memcmp() form would malfunction on any system with CHAR_BIT != 8, in a way that the {u,}int8_t-at-a-time form wouldn't.

~Andrew

#5 lore raw
In reply to Roger Pau Monné
Quoted 9 lines · Roger Pau Monné
Quoted 2 lines · Andrew Cooper

Misra Rule 21.16 doesn't like the use of memcmp() between a string literal and a UINT8 array. Rewrite using plain compares.

The commit message makes it look like it's a type mismatch issue between the two elements being compared, but from my reading of the rule the issue is with the usage of a char pointer with memcmp().
IOW: even if the two parameters are char pointers it would still be a violation.

Quoted 3 lines

"Misra Rule 21.16 forbids the use of memcmp() against character arrays. Rewrite using plain compares since checking for "PE\0\0" cannot be done using strncmp()."

I've tweaked the sentence to say character array, but IMO it's really not about strncmp() which wouldn't be correct to use here even if it happened to produce a correct answer.

Quoted 6 lines
Quoted 3 lines

No functional change.

LGTM (possibly pending the adjustment of the commit message):

Thanks.

Reviewed-by on v1 1/1
#6 lore raw
In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper

Misra Rule 21.16 doesn't like the use of memcmp() between a string literal and a UINT8 array. Rewrite using plain compares.

No functional change.

after having realized that sadly gcc13 instantiates the compound literals (that I had thought of using) in .rodata (or one of its variants), rather than leveraging them being as constant as string literals.

Jan

#7 lore raw
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper

Misra Rule 21.16 doesn't like the use of memcmp() between a string literal and a UINT8 array. Rewrite using plain compares.

No functional change.

Thanks.

Quoted 3 lines

after having realized that sadly gcc13 instantiates the compound literals (that I had thought of using) in .rodata (or one of its variants), rather than leveraging them being as constant as string literals.

gcc10 manages to optimise both checks to a cmp{w,l}, which I did check before sending the patch.

However, it chooses a different base register for the cmpl and there's a sad cascade effect where a bunch of JMP disp8's turn into disp32's.

But oh well - it's init code.

~Andrew