Skip to contentSkip to conversation

xen/build-id: Further cleanup

PATCH In review Andrew Cooper 3 participants 8 messages Nov 17 – Nov 18

lore
Share & tools
lore
#0 lore raw

v2 cover xen/build-id: Further cleanup

This is the final piece of work 32b63424e4e4 "x86/EFI: Fix detection of buildid" which is rather overdue a first posting.

v2: Fix pointer arithmatic, and allow for padding.

Andrew Cooper (2):
xen/build-id: Introduce is_gnu_build_id()
xen/build-id: Drop xen_build_id_check()

 xen/common/livepatch.c       | 11 ++-----
 xen/common/version.c         | 59 ++++++++++++++++++++----------------
 xen/include/xen/elfstructs.h |  2 ++
 xen/include/xen/version.h    |  4 +--
 4 files changed, 40 insertions(+), 36 deletions(-)

base-commit: ab77da87e9b38022341eb6a54593e681485d203a

Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 1/2 xen/build-id: Introduce is_gnu_build_id()

Broken out of the subsequent change for legibility reasons. It will be used to replace xen_build_id_check().

---
CC: Ross Lagerwall <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>

v2:
* Check for n->descsz within len, not exactly len, to accomodate possible padding.

---
 xen/common/version.c         | 34 ++++++++++++++++++++++++++++++++++
 xen/include/xen/elfstructs.h |  2 ++
 xen/include/xen/version.h    |  2 ++
 3 files changed, 38 insertions(+)
xen/common/version.c +34 -0
index 80e7a9b981cf..5e1f5ecfb68b 100644
@@ -114,6 +114,38 @@ void print_version(void)
 /* Defined in linker script. */
 extern const Elf_Note __note_gnu_build_id_start[], __note_gnu_build_id_end[];
 
+/*
+ * Check whether @n is a GNU Build ID note.
+ *
+ * We check that:
+ *  - type is NT_GNU_BUILD_ID
+ *  - name is GNU\0
+ *  - desc is non-empty and fits within the buffer.
+ *
+ * If this returns true, the contents starts at n + NT_GNU_HDR_SIZE, and is
+ * n->descsz long.
+ */
+bool is_gnu_build_id(const Elf_Note *n, unsigned int len)
+{
+    const char *name;
+
+    if ( len <= NT_GNU_HDR_SIZE )
+        return false;
+
+    if ( n->type != NT_GNU_BUILD_ID ||
+         n->namesz != 4 )
+        return false;
+
+    name = ELFNOTE_NAME(n);
+    if ( strncmp(name, "GNU", 4) != 0 )
+        return false;
+
+    if ( n->descsz > (len - NT_GNU_BUILD_ID) )
+        return false;
+
+    return true;
+}
+
 int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
                        const void **p, unsigned int *len)
 {
@@ -234,6 +266,8 @@ static void __init __maybe_unused build_assertions(void)
 {
     BUILD_BUG_ON(sizeof(unsigned long) != sizeof(void (*)(void)));
     BUILD_BUG_ON(sizeof(void *) != sizeof(void (*)(void)));
+
+    BUILD_BUG_ON(NT_GNU_HDR_SIZE != sizeof(Elf_Note) + 4);
 }
 
 /*
xen/include/xen/elfstructs.h +2 -0
index 8ee0d5aa5b15..1f0287aeed49 100644
@@ -46,6 +46,8 @@ typedef uint32_t	Elf64_Word;
 typedef int64_t		Elf64_Sxword;
 typedef uint64_t	Elf64_Xword;
 
+#define NT_GNU_HDR_SIZE 16 /* sizeof(Elf_note) + "GNU\0" */
+
 /* Unique build id string format when using --build-id. */
 #define NT_GNU_BUILD_ID 3
 
xen/include/xen/version.h +2 -0
index b9fa8d70520d..ee857616f9e4 100644
@@ -2,6 +2,7 @@
 #define __XEN_VERSION_H__
 
 #include <xen/elfstructs.h>
+#include <xen/stdbool.h>
 
 const char *xen_compile_date(void);
 const char *xen_compile_time(void);
@@ -26,6 +27,7 @@ extern unsigned int xen_build_id_len; /* 0 -> No build id. */
 void xen_build_init(void);
 int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
                        const void **p, unsigned int *len);
+bool is_gnu_build_id(const Elf_Note *n, unsigned int len);
 #else
 static inline void xen_build_init(void) {};
 #endif
Signature · 1 line
2.39.5
#3 lore raw

Re: [PATCH v2 1/2] xen/build-id: Introduce is_gnu_build_id()

Quoted 7 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper

Broken out of the subsequent change for legibility reasons. It will be used to replace xen_build_id_check().

Thanks, but it's just occurred to me that I need to make the logic:

    if ( n->descsz == 0 ||
         n->descsz > (len - NT_GNU_BUILD_ID) )
        return false;

to retain the property of rejecting a zero-size note, now that descsz is not strictly checked against a length that was confirmed to be 17 or more.

I'll fold this in on commit.

~Andrew

Reviewed-by
#4 lore raw

Re: [PATCH v2 1/2] xen/build-id: Introduce is_gnu_build_id()

Quoted 11 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper

Broken out of the subsequent change for legibility reasons. It will be used to replace xen_build_id_check().

Thanks, but it's just occurred to me that I need to make the logic:

Quoted 8 lines
     if ( n->descsz == 0 ||
          n->descsz > (len - NT_GNU_BUILD_ID) )
         return false;

to retain the property of rejecting a zero-size note, now that descsz is not strictly checked against a length that was confirmed to be 17 or more.

I'll fold this in on commit.

Shouldn't this be NT_GNU_HDR_SIZE, not NT_GNU_BUILD_ID?

With that change,

#5 lore raw

Re: [PATCH v2 1/2] xen/build-id: Introduce is_gnu_build_id()

Quoted 21 lines · Ross Lagerwall
Quoted 20 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper

Broken out of the subsequent change for legibility reasons.  It will be used
to replace xen_build_id_check().

Thanks, but it's just occurred to me that I need to make the logic:

     if ( n->descsz == 0 ||
          n->descsz > (len - NT_GNU_BUILD_ID) )
         return false;

to retain the property of rejecting a zero-size note, now that descsz is not strictly checked against a length that was confirmed to be 17 or more.

Quoted 7 lines
Quoted 1 line

I'll fold this in on commit.

Shouldn't this be NT_GNU_HDR_SIZE, not NT_GNU_BUILD_ID?

With that change,

Oh, yes it should be.  Thanks.  I'll fix that too.

~Andrew

#6 lore raw

PATCH v2 2/2 xen/build-id: Drop xen_build_id_check()

In reply to Andrew Cooper

The function is misnamed; it really is looking for a GNU Build ID note, not a Xen note as the name suggests.

It also has an awkward API to use. For a GNU Build ID, the content is always at +16 from the start, so really doesn't need to be returned by pointer, nor is it necessary be doing additional checks on the returned pointers being valid.

Switching to is_gnu_build_id() allows xen_build_init() to be simplified, returning after the first successful find rather than passing an rc down the function to skip later checks.

No functional change.

---
CC: Ross Lagerwall <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>

v2:
* Fix pointer arithmatic on n.
* Use n->descsz instead of assuming that there is no padding.

---
 xen/common/livepatch.c    | 11 +++--------
 xen/common/version.c      | 41 +++++++--------------------------------
 xen/include/xen/version.h |  2 --
 3 files changed, 10 insertions(+), 44 deletions(-)
xen/common/livepatch.c +3 -8
index 7515a040ad46..b789c91797f0 100644
@@ -453,22 +453,17 @@ static int parse_buildid(const struct livepatch_elf_sec *sec,
                          struct livepatch_build_id *id)
 {
     const Elf_Note *n;
-    int rc;
 
     /* Presence of the sections is ensured by check_special_sections(). */
     ASSERT(sec);
 
     n = sec->addr;
 
-    if ( sec->sec->sh_size <= sizeof(*n) )
+    if ( !is_gnu_build_id(n, sec->sec->sh_size) )
         return -EINVAL;
 
-    rc = xen_build_id_check(n, sec->sec->sh_size, &id->p, &id->len);
-    if ( rc )
-        return rc;
-
-    if ( !id->len || !id->p )
-        return -EINVAL;
+    id->p   = (const void *)n + NT_GNU_HDR_SIZE;
+    id->len = n->descsz
 
    return 0;
 }
xen/common/version.c +7 -34

71 lines not on this page · raw message

xen/include/xen/version.h +0 -2
index ee857616f9e4..6e9a24b7fadf 100644
@@ -25,8 +25,6 @@ extern unsigned int xen_build_id_len; /* 0 -> No build id. */
 
 #ifdef BUILD_ID
 void xen_build_init(void);
-int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
-                       const void **p, unsigned int *len);
 bool is_gnu_build_id(const Elf_Note *n, unsigned int len);
 #else
 static inline void xen_build_init(void) {};
Signature · 1 line
2.39.5
Reviewed-by on v2 2/2
#7 lore raw

Re: [PATCH v2 2/2] xen/build-id: Drop xen_build_id_check()

Quoted 8 lines · Andrew Cooper

The function is misnamed; it really is looking for a GNU Build ID note, not a Xen note as the name suggests.

It also has an awkward API to use. For a GNU Build ID, the content is always at +16 from the start, so really doesn't need to be returned by pointer, nor is it necessary be doing additional checks on the returned pointers being valid.

Quoted 6 lines

Switching to is_gnu_build_id() allows xen_build_init() to be simplified, returning after the first successful find rather than passing an rc down the function to skip later checks.

No functional change.

Thanks,
Ross