Skip to contentSkip to conversation

libelf: small fixes for PVH

PATCH In review Roger Pau Monne 2 participants 7 messages May 20, 2021 – May 28, 2021

lore
Share & tools
lore
#0 lore raw

v3 cover libelf: small fixes for PVH

Hello,

A couple of small fixes for PVH loading. The first one is likely not very relevant, since PVH couldn't be booted anyway with the data in the __xen_guest section, so it's mostly a cleanup.

Second patch fixes the checks for PVH loading, as in that case physical addresses must always be used to perform the bound calculations.

Thanks, Roger.

Roger Pau Monne (2):
libelf: don't attempt to parse __xen_guest for PVH
libelf: improve PVH elfnote parsing

 tools/fuzz/libelf/libelf-fuzzer.c   |  3 +-
 tools/libs/guest/xg_dom_elfloader.c |  6 ++--
 tools/libs/guest/xg_dom_hvmloader.c |  2 +-
 xen/arch/x86/hvm/dom0_build.c       |  2 +-
 xen/arch/x86/pv/dom0_build.c        |  2 +-
 xen/common/libelf/libelf-dominfo.c  | 49 +++++++++++++++++------------
 xen/include/xen/libelf.h            |  2 +-
 7 files changed, 39 insertions(+), 27 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/2 libelf: don't attempt to parse __xen_guest for PVH

The legacy __xen_guest section doesn't support the PHYS32_ENTRY elfnote, so it's pointless to attempt to parse the elfnotes from that section when called from an hvm container.

---
Changes since v2:
- New in this version.

---
 xen/common/libelf/libelf-dominfo.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
xen/common/libelf/libelf-dominfo.c +2 -4
index 69c94b6f3bb..abea1011c18 100644
@@ -577,10 +577,8 @@ elf_errorstatus elf_xen_parse(struct elf_binary *elf,
 
     }
 
-    /*
-     * Finally fall back to the __xen_guest section.
-     */
-    if ( xen_elfnotes == 0 )
+    /* Finally fall back to the __xen_guest section for PV guests only. */
+    if ( xen_elfnotes == 0 && !hvm )
     {
         shdr = elf_shdr_by_name(elf, "__xen_guest");
         if ( ELF_HANDLE_VALID(shdr) )
Signature · 1 line
2.31.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] libelf: don't attempt to parse __xen_guest for PVH

Quoted 18 lines · Roger Pau Monne

The legacy __xen_guest section doesn't support the PHYS32_ENTRY elfnote, so it's pointless to attempt to parse the elfnotes from that section when called from an hvm container.

---
Changes since v2:
- New in this version.

---
 xen/common/libelf/libelf-dominfo.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
xen/common/libelf/libelf-dominfo.c +0 -0
index 69c94b6f3bb..abea1011c18 100644

xen/common/libelf/libelf-dominfo.c elf_errorstatus elf_xen_parse(struct elf_binary *elf, · line 581

Quoted 9 lines
xen/common/libelf/libelf-dominfo.c +2 -4
@@ -577,10 +577,8 @@ elf_errorstatus elf_xen_parse(struct elf_binary *elf,
 
     }
 
-    /*
-     * Finally fall back to the __xen_guest section.
-     */
-    if ( xen_elfnotes == 0 )
+    /* Finally fall back to the __xen_guest section for PV guests only. */
+    if ( xen_elfnotes == 0 && !hvm )

Isn't this depending on the 2nd patch adding the function parameter? IOW doesn't this break the build, even if just transiently? With the respective hunk from patch 2 moved here

Jan

#3 lore raw

Re: [PATCH v3 1/2] libelf: don't attempt to parse __xen_guest for PVH

Quoted 32 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monne

The legacy __xen_guest section doesn't support the PHYS32_ENTRY elfnote, so it's pointless to attempt to parse the elfnotes from that section when called from an hvm container.

---
Changes since v2:
- New in this version.

---
 xen/common/libelf/libelf-dominfo.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
xen/common/libelf/libelf-dominfo.c +0 -0
index 69c94b6f3bb..abea1011c18 100644

xen/common/libelf/libelf-dominfo.c elf_errorstatus elf_xen_parse(struct elf_binary *elf, · line 581

Quoted 9 lines
xen/common/libelf/libelf-dominfo.c +2 -4
@@ -577,10 +577,8 @@ elf_errorstatus elf_xen_parse(struct elf_binary *elf,
 
     }
 
-    /*
-     * Finally fall back to the __xen_guest section.
-     */
-    if ( xen_elfnotes == 0 )
+    /* Finally fall back to the __xen_guest section for PV guests only. */
+    if ( xen_elfnotes == 0 && !hvm )

Isn't this depending on the 2nd patch adding the function parameter? IOW doesn't this break the build, even if just transiently? With the respective hunk from patch 2 moved here

Quoted 1 line

With the intention of committing I did this hunk movement, noticing that
- it's more than just one hunk,
- a tool stack maintainer ack is actually going to be needed (all respective hunks are now in the first patch).
I'll keep the massaged patches locally, until the missing ack arrives.

Jan

#4 lore raw

Re: [PATCH v3 1/2] libelf: don't attempt to parse __xen_guest for PVH

Quoted 35 lines · Jan Beulich
Quoted 32 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monne

The legacy __xen_guest section doesn't support the PHYS32_ENTRY elfnote, so it's pointless to attempt to parse the elfnotes from that section when called from an hvm container.

---
Changes since v2:
- New in this version.

---
 xen/common/libelf/libelf-dominfo.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
xen/common/libelf/libelf-dominfo.c +0 -0
index 69c94b6f3bb..abea1011c18 100644

xen/common/libelf/libelf-dominfo.c elf_errorstatus elf_xen_parse(struct elf_binary *elf, · line 581

Quoted 9 lines
xen/common/libelf/libelf-dominfo.c +2 -4
@@ -577,10 +577,8 @@ elf_errorstatus elf_xen_parse(struct elf_binary *elf,
 
     }
 
-    /*
-     * Finally fall back to the __xen_guest section.
-     */
-    if ( xen_elfnotes == 0 )
+    /* Finally fall back to the __xen_guest section for PV guests only. */
+    if ( xen_elfnotes == 0 && !hvm )

Isn't this depending on the 2nd patch adding the function parameter? IOW doesn't this break the build, even if just transiently? With the respective hunk from patch 2 moved here

Quoted 1 line
Quoted 6 lines

With the intention of committing I did this hunk movement, noticing that
- it's more than just one hunk,
- a tool stack maintainer ack is actually going to be needed (all respective hunks are now in the first patch).
I'll keep the massaged patches locally, until the missing ack arrives.

I've timed out waiting for an ack and committed the patches, considering the tool stack parts of them are mechanical enough.

Jan

#5 lore raw

PATCH v3 2/2 libelf: improve PVH elfnote parsing

In reply to Roger Pau Monne

Pass an hvm boolean parameter to the elf note parsing and checking routines, so that better checking can be done in case libelf is dealing with an hvm container.

elf_xen_note_check shouldn't return early unless PHYS32_ENTRY is set and the container is of type HVM, or else the loader and version checks would be avoided for kernels intended to be booted as PV but that also have PHYS32_ENTRY set.

Adjust elf_xen_addr_calc_check so that the virtual addresses are actually physical ones (by setting virt_base and elf_paddr_offset to zero) when the container is of type HVM, as that container is always started with paging disabled.

---
Changes since v2:
- Do not print messages about VIRT_BASE/ELF_PADDR_OFFSET unset on HVM.
- Make it explicit that elf_paddr_offset will be set to 0 regardless of elf_note_start value for HVM.

Changes since v1:
- Expand comments.
- Do not set virt_entry to phys_entry unless it's an HVM container.

---
 tools/fuzz/libelf/libelf-fuzzer.c   |  3 +-
 tools/libs/guest/xg_dom_elfloader.c |  6 ++--
 tools/libs/guest/xg_dom_hvmloader.c |  2 +-
 xen/arch/x86/hvm/dom0_build.c       |  2 +-
 xen/arch/x86/pv/dom0_build.c        |  2 +-
 xen/common/libelf/libelf-dominfo.c  | 43 ++++++++++++++++++-----------
 xen/include/xen/libelf.h            |  2 +-
 7 files changed, 37 insertions(+), 23 deletions(-)
tools/fuzz/libelf/libelf-fuzzer.c +2 -1
index 1ba85717114..84fb84720fa 100644
@@ -17,7 +17,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
         return -1;
 
     elf_parse_binary(elf);
-    elf_xen_parse(elf, &parms);
+    elf_xen_parse(elf, &parms, false);
+    elf_xen_parse(elf, &parms, true);
 
     return 0;
 }
tools/libs/guest/xg_dom_elfloader.c +4 -2
index 06e713fe111..ad71163dd92 100644
@@ -135,7 +135,8 @@ static elf_negerrnoval xc_dom_probe_elf_kernel(struct xc_dom_image *dom)
      * or else we might be trying to load a plain ELF.
      */
     elf_parse_binary(&elf);
-    rc = elf_xen_parse(&elf, dom->parms);
+    rc = elf_xen_parse(&elf, dom->parms,
+                       dom->container_type == XC_DOM_HVM_CONTAINER);
     if ( rc != 0 )
         return rc;
 
@@ -166,7 +167,8 @@ static elf_negerrnoval xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
 
     /* parse binary and get xen meta info */
     elf_parse_binary(elf);
-    if ( elf_xen_parse(elf, dom->parms) != 0 )
+    if ( elf_xen_parse(elf, dom->parms,
+                       dom->container_type == XC_DOM_HVM_CONTAINER) != 0 )
     {
         rc = -EINVAL;
         goto out;
tools/libs/guest/xg_dom_hvmloader.c +1 -1
index ec6ebad7fd5..3a63b23ba39 100644
@@ -73,7 +73,7 @@ static elf_negerrnoval xc_dom_probe_hvm_kernel(struct xc_dom_image *dom)
      * else we might be trying to load a PV kernel.
      */
     elf_parse_binary(&elf);
-    rc = elf_xen_parse(&elf, dom->parms);
+    rc = elf_xen_parse(&elf, dom->parms, true);
     if ( rc == 0 )
         return -EINVAL;
 
xen/arch/x86/hvm/dom0_build.c +1 -1
index 878dc1d808e..c24b9efdb0a 100644
@@ -561,7 +561,7 @@ static int __init pvh_load_kernel(struct domain *d, const module_t *image,
     elf_set_verbose(&elf);
 #endif
     elf_parse_binary(&elf);
-    if ( (rc = elf_xen_parse(&elf, &parms)) != 0 )
+    if ( (rc = elf_xen_parse(&elf, &parms, true)) != 0 )
     {
         printk("Unable to parse kernel for ELFNOTES\n");
         return rc;
xen/arch/x86/pv/dom0_build.c +1 -1
index e0801a9e6d1..af47615b226 100644
@@ -353,7 +353,7 @@ int __init dom0_construct_pv(struct domain *d,
         elf_set_verbose(&elf);
 
     elf_parse_binary(&elf);
-    if ( (rc = elf_xen_parse(&elf, &parms)) != 0 )
+    if ( (rc = elf_xen_parse(&elf, &parms, false)) != 0 )
         goto out;
 
     /* compatibility check */
xen/common/libelf/libelf-dominfo.c +27 -16

99 lines not on this page · raw message

xen/include/xen/libelf.h +1 -1
index b73998150fc..be47b0cc366 100644
@@ -454,7 +454,7 @@ int elf_xen_parse_note(struct elf_binary *elf,
 int elf_xen_parse_guest_info(struct elf_binary *elf,
                              struct elf_dom_parms *parms);
 int elf_xen_parse(struct elf_binary *elf,
-                  struct elf_dom_parms *parms);
+                  struct elf_dom_parms *parms, bool hvm);
 
 static inline void *elf_memcpy_unchecked(void *dest, const void *src, size_t n)
     { return memcpy(dest, src, n); }
Signature · 1 line
2.31.1
Reviewed-by on v3 2/2
#6 lore raw

Re: [PATCH v3 2/2] libelf: improve PVH elfnote parsing

Quoted 9 lines · Roger Pau Monne

Pass an hvm boolean parameter to the elf note parsing and checking routines, so that better checking can be done in case libelf is dealing with an hvm container.

elf_xen_note_check shouldn't return early unless PHYS32_ENTRY is set and the container is of type HVM, or else the loader and version checks would be avoided for kernels intended to be booted as PV but that also have PHYS32_ENTRY set.

Quoted 6 lines

Adjust elf_xen_addr_calc_check so that the virtual addresses are actually physical ones (by setting virt_base and elf_paddr_offset to zero) when the container is of type HVM, as that container is always started with paging disabled.

With the one hunk moved to patch 1

Jan