Skip to contentSkip to conversation

xen/build-id: Fix xen_build_id_check() to be robust against malformed notes

PATCH In review Andrew Cooper 4 participants 8 messages Dec 31, 2018 – Jan 8, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/build-id: Fix xen_build_id_check() to be robust against malformed notes

A NT_GNU_BUILD_ID with namesz longer than 4 will cause the strncmp() to use bytes in adjacent stringtable entries.

Instead, check for namesz exactly equal to 4, and use memcmp() with an explicit size.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Noticed while auditing Xen's use of strncmp() for the command line patch.

---
 xen/common/version.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/common/version.c +3 -3
index 223cb52..1df7e78 100644
@@ -97,17 +97,17 @@ int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
     if ( NT_GNU_BUILD_ID != n->type )
         return -ENODATA;
 
-    if ( n->namesz + n->descsz < n->namesz )
+    if ( n->namesz != 4 /* GNU\0 */)
         return -EINVAL;
 
-    if ( n->namesz < 4 /* GNU\0 */)
+    if ( n->namesz + n->descsz < n->namesz )
         return -EINVAL;
 
     if ( n->namesz + n->descsz > n_sz - sizeof(*n) )
         return -EINVAL;
 
     /* Sanity check, name should be "GNU" for ld-generated build-id. */
-    if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 )
+    if ( memcmp(ELFNOTE_NAME(n), "GNU", 4) != 0 )
         return -ENODATA;
 
     if ( len )
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Andrew Cooper

A NT_GNU_BUILD_ID with namesz longer than 4 will cause the strncmp() to use bytes in adjacent stringtable entries.

Instead, check for namesz exactly equal to 4, and use memcmp() with an explicit size.

This LGTM:

Albeit I wonder the usefulness of some of the checks performed by this function.

I'm not sure the point of the 'n->namesz + n->descsz < n->namesz' check, I assume this is an overflow check. And then 'n->namesz + n->descsz > n_sz - sizeof(*n)' should rather be 'n->namesz + n->descsz != n_sz - sizeof(*n)' I think.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Andrew Cooper
Quoted 24 lines · Andrew Cooper

A NT_GNU_BUILD_ID with namesz longer than 4 will cause the strncmp() to use bytes in adjacent stringtable entries.

Instead, check for namesz exactly equal to 4, and use memcmp() with an explicit size.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Noticed while auditing Xen's use of strncmp() for the command line patch.

---
 xen/common/version.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/common/version.c +0 -0
index 223cb52..1df7e78 100644

xen/common/version.c int xen_build_id_check(const Elf_Note *n, unsigned int n_sz, · line 103

Quoted 9 lines
xen/common/version.c +2 -2
@@ -97,17 +97,17 @@ int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
     if ( NT_GNU_BUILD_ID != n->type )
         return -ENODATA;
 
-    if ( n->namesz + n->descsz < n->namesz )
+    if ( n->namesz != 4 /* GNU\0 */)
         return -EINVAL;
 
-    if ( n->namesz < 4 /* GNU\0 */)
+    if ( n->namesz + n->descsz < n->namesz )

The reordering of two predicates doesn't seem to serve any particular purpose? You could've just changed "<" to "!=" for less code churn?

xen/common/version.c int xen_build_id_check(const Elf_Note *n, unsigned int n_sz, · line 110

Quoted 8 lines
xen/common/version.c +1 -1
         return -EINVAL;
 
     if ( n->namesz + n->descsz > n_sz - sizeof(*n) )
         return -EINVAL;
 
     /* Sanity check, name should be "GNU" for ld-generated build-id. */
-    if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 )
+    if ( memcmp(ELFNOTE_NAME(n), "GNU", 4) != 0 )

OOI what is the advantage of memcmp compared to strncmp?

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 28 lines · Wei Liu
Quoted 27 lines · Andrew Cooper

A NT_GNU_BUILD_ID with namesz longer than 4 will cause the strncmp() to use bytes in adjacent stringtable entries.

Instead, check for namesz exactly equal to 4, and use memcmp() with an explicit size.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Noticed while auditing Xen's use of strncmp() for the command line patch.

---
 xen/common/version.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/common/version.c +0 -0
index 223cb52..1df7e78 100644
@@ -97,17 +97,17 @@ int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
     if ( NT_GNU_BUILD_ID != n->type )
         return -ENODATA;
 
Quoted 8 lines

xen/common/version.c int xen_build_id_check(const Elf_Note *n, unsigned int n_sz, · line 103

Quoted 6 lines
xen/common/version.c +2 -2
-    if ( n->namesz + n->descsz < n->namesz )
+    if ( n->namesz != 4 /* GNU\0 */)
         return -EINVAL;
 
-    if ( n->namesz < 4 /* GNU\0 */)
+    if ( n->namesz + n->descsz < n->namesz )

The reordering of two predicates doesn't seem to serve any particular purpose? You could've just changed "<" to "!=" for less code churn?

Logically, the != 4 check should be ahead of the truncation check, but yes - patch did collapse it into something rather harder to read in this case.

Quoted 10 lines

xen/common/version.c int xen_build_id_check(const Elf_Note *n, unsigned int n_sz, · line 110

Quoted 9 lines
xen/common/version.c +1 -1
         return -EINVAL;
 
     if ( n->namesz + n->descsz > n_sz - sizeof(*n) )
         return -EINVAL;
 
     /* Sanity check, name should be "GNU" for ld-generated build-id. */
-    if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 )
+    if ( memcmp(ELFNOTE_NAME(n), "GNU", 4) != 0 )

OOI what is the advantage of memcmp compared to strncmp?

Erm.  I suppose in this exact case, personal preference.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Wei Liu
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 02.01.19 at 11:43, <[email protected]> wrote:

xen/common/version.c int xen_build_id_check(const Elf_Note *n, unsigned int n_sz, · line 110

Quoted 3 lines · Andrew Cooper
     /* Sanity check, name should be "GNU" for ld-generated build-id. */
xen/common/version.c +1 -1
-    if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 )
+    if ( memcmp(ELFNOTE_NAME(n), "GNU", 4) != 0 )

OOI what is the advantage of memcmp compared to strncmp?

memcmp() generally is more performant than strncmp(), due to it not needing to look for nul terminators.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
In reply to Andrew Cooper
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 31.12.18 at 18:34, <[email protected]> wrote:

A NT_GNU_BUILD_ID with namesz longer than 4 will cause the strncmp() to use bytes in adjacent stringtable entries.

Instead, check for namesz exactly equal to 4,

Is that a requirement spelled out anywhere? Till now I've been under the impression that e.g. 8 bytes of name are fine as well, as long as the first four of them are "GNU\0".

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 8 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 31.12.18 at 18:34, <[email protected]> wrote:

A NT_GNU_BUILD_ID with namesz longer than 4 will cause the strncmp() to use bytes in adjacent stringtable entries.

Instead, check for namesz exactly equal to 4,

Is that a requirement spelled out anywhere? Till now I've been under the impression that e.g. 8 bytes of name are fine as well, as long as the first four of them are "GNU\0".

No idea, but if this is true then we've got bigger problems with parsing the notes.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#7 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 07.01.19 at 18:34, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 31.12.18 at 18:34, <[email protected]> wrote:

A NT_GNU_BUILD_ID with namesz longer than 4 will cause the strncmp() to use bytes in adjacent stringtable entries.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Instead, check for namesz exactly equal to 4,

Is that a requirement spelled out anywhere? Till now I've been under the impression that e.g. 8 bytes of name are fine as well, as long as the first four of them are "GNU\0".

No idea, but if this is true then we've got bigger problems with parsing the notes.

Okay, I've gone and checked the spec
(http://www.sco.com/developers/gabi/latest/ch5.pheader.html#note_section)
and to me it is not entirely unambiguous but matches up better with the behavior that you want to establish than the more relaxed one I was suggesting. IOW

albeit I'd like to note that we're still not in line with the doc above as far as padding is concerned. But there was a lengthy discussion on the gABI mailing list not so long ago, because the spelled out behavior also is not in line with what binutils does, nor with what older gABI versions did mandate.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel