diff mbox series

libxc: elf_kernel loader: Remove check for shstrtab

Message ID 20190515114015.25492-1-anthony.perard@citrix.com (mailing list archive)
State Superseded
Headers show
Series libxc: elf_kernel loader: Remove check for shstrtab | expand

Commit Message

Anthony PERARD May 15, 2019, 11:40 a.m. UTC
This was probably useful to load ELF Note, but now ELF notes
"should live in a PT_NOTE segment" (elfnote.h).

With notes living in segment, there are no need for sections, so there
is nothing to be stored in the shstrtab.

This patch would allow to write a simpler ELF header for an OVMF blob
(which isn't an ELF) and allow it to be loaded as a PVH kernel. The
header only needs to declare two program segments:
- one to tell an ELF loader where to put the blob,
- one for a Xen ELFNOTE.

The ELFNOTE is to comply to the pvh design which wants the
XEN_ELFNOTE_PHYS32_ENTRY to declare a blob as compaptible with the PVH
boot ABI.

Note that without the ELFNOTE, libxc will load an ELF but with
the plain ELF loader, which doesn't check for shstrtab.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
---
 tools/libxc/xc_dom_elfloader.c | 9 ---------
 1 file changed, 9 deletions(-)

Comments

Wei Liu May 15, 2019, 12:02 p.m. UTC | #1
On Wed, May 15, 2019 at 12:40:15PM +0100, Anthony PERARD wrote:
> This was probably useful to load ELF Note, but now ELF notes
> "should live in a PT_NOTE segment" (elfnote.h).
> 
> With notes living in segment, there are no need for sections, so there
> is nothing to be stored in the shstrtab.
> 
> This patch would allow to write a simpler ELF header for an OVMF blob
> (which isn't an ELF) and allow it to be loaded as a PVH kernel. The
> header only needs to declare two program segments:
> - one to tell an ELF loader where to put the blob,
> - one for a Xen ELFNOTE.
> 
> The ELFNOTE is to comply to the pvh design which wants the
> XEN_ELFNOTE_PHYS32_ENTRY to declare a blob as compaptible with the PVH
> boot ABI.
> 
> Note that without the ELFNOTE, libxc will load an ELF but with
> the plain ELF loader, which doesn't check for shstrtab.
> 
> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>
Andrew Cooper May 15, 2019, 12:07 p.m. UTC | #2
On 15/05/2019 12:40, Anthony PERARD wrote:
> This was probably useful to load ELF Note, but now ELF notes
> "should live in a PT_NOTE segment" (elfnote.h).
>
> With notes living in segment, there are no need for sections, so there
> is nothing to be stored in the shstrtab.
>
> This patch would allow to write a simpler ELF header for an OVMF blob
> (which isn't an ELF) and allow it to be loaded as a PVH kernel. The
> header only needs to declare two program segments:
> - one to tell an ELF loader where to put the blob,
> - one for a Xen ELFNOTE.
>
> The ELFNOTE is to comply to the pvh design which wants the
> XEN_ELFNOTE_PHYS32_ENTRY to declare a blob as compaptible with the PVH
> boot ABI.
>
> Note that without the ELFNOTE, libxc will load an ELF but with
> the plain ELF loader, which doesn't check for shstrtab.
>
> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> ---
>  tools/libxc/xc_dom_elfloader.c | 9 ---------
>  1 file changed, 9 deletions(-)
>
> diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
> index 82b5f2ee79..b327db219d 100644
> --- a/tools/libxc/xc_dom_elfloader.c
> +++ b/tools/libxc/xc_dom_elfloader.c
> @@ -165,15 +165,6 @@ static elf_negerrnoval xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
>          return rc;
>      }
>  
> -    /* Find the section-header strings table. */
> -    if ( ELF_PTRVAL_INVALID(elf->sec_strtab) )
> -    {
> -        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: ELF image"
> -                     " has no shstrtab", __FUNCTION__);
> -        rc = -EINVAL;
> -        goto out;
> -    }

This might be fine for newer binaries, but you'll break older ones.

Instead, you should skip searching for strtab if we've already located
the Xen notes.

~Andrew
Wei Liu May 15, 2019, 12:09 p.m. UTC | #3
On Wed, May 15, 2019 at 01:07:03PM +0100, Andrew Cooper wrote:
> On 15/05/2019 12:40, Anthony PERARD wrote:
> > This was probably useful to load ELF Note, but now ELF notes
> > "should live in a PT_NOTE segment" (elfnote.h).
> >
> > With notes living in segment, there are no need for sections, so there
> > is nothing to be stored in the shstrtab.
> >
> > This patch would allow to write a simpler ELF header for an OVMF blob
> > (which isn't an ELF) and allow it to be loaded as a PVH kernel. The
> > header only needs to declare two program segments:
> > - one to tell an ELF loader where to put the blob,
> > - one for a Xen ELFNOTE.
> >
> > The ELFNOTE is to comply to the pvh design which wants the
> > XEN_ELFNOTE_PHYS32_ENTRY to declare a blob as compaptible with the PVH
> > boot ABI.
> >
> > Note that without the ELFNOTE, libxc will load an ELF but with
> > the plain ELF loader, which doesn't check for shstrtab.
> >
> > Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> > ---
> >  tools/libxc/xc_dom_elfloader.c | 9 ---------
> >  1 file changed, 9 deletions(-)
> >
> > diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
> > index 82b5f2ee79..b327db219d 100644
> > --- a/tools/libxc/xc_dom_elfloader.c
> > +++ b/tools/libxc/xc_dom_elfloader.c
> > @@ -165,15 +165,6 @@ static elf_negerrnoval xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
> >          return rc;
> >      }
> >  
> > -    /* Find the section-header strings table. */
> > -    if ( ELF_PTRVAL_INVALID(elf->sec_strtab) )
> > -    {
> > -        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: ELF image"
> > -                     " has no shstrtab", __FUNCTION__);
> > -        rc = -EINVAL;
> > -        goto out;
> > -    }
> 
> This might be fine for newer binaries, but you'll break older ones.
> 
> Instead, you should skip searching for strtab if we've already located
> the Xen notes.

AIUI old binaries always have shstrtab while it isn't always true for
new ones.

Unfortunately my attempt to figure out the history of this piece of code
is futile.

Wei.

> 
> ~Andrew
Anthony PERARD May 15, 2019, 12:55 p.m. UTC | #4
On Wed, May 15, 2019 at 01:07:03PM +0100, Andrew Cooper wrote:
> On 15/05/2019 12:40, Anthony PERARD wrote:
> > This was probably useful to load ELF Note, but now ELF notes
> > "should live in a PT_NOTE segment" (elfnote.h).
> >
> > With notes living in segment, there are no need for sections, so there
> > is nothing to be stored in the shstrtab.
> >
> > This patch would allow to write a simpler ELF header for an OVMF blob
> > (which isn't an ELF) and allow it to be loaded as a PVH kernel. The
> > header only needs to declare two program segments:
> > - one to tell an ELF loader where to put the blob,
> > - one for a Xen ELFNOTE.
> >
> > The ELFNOTE is to comply to the pvh design which wants the
> > XEN_ELFNOTE_PHYS32_ENTRY to declare a blob as compaptible with the PVH
> > boot ABI.
> >
> > Note that without the ELFNOTE, libxc will load an ELF but with
> > the plain ELF loader, which doesn't check for shstrtab.
> >
> > Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> > ---
> >  tools/libxc/xc_dom_elfloader.c | 9 ---------
> >  1 file changed, 9 deletions(-)
> >
> > diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
> > index 82b5f2ee79..b327db219d 100644
> > --- a/tools/libxc/xc_dom_elfloader.c
> > +++ b/tools/libxc/xc_dom_elfloader.c
> > @@ -165,15 +165,6 @@ static elf_negerrnoval xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
> >          return rc;
> >      }
> >  
> > -    /* Find the section-header strings table. */
> > -    if ( ELF_PTRVAL_INVALID(elf->sec_strtab) )
> > -    {
> > -        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: ELF image"
> > -                     " has no shstrtab", __FUNCTION__);
> > -        rc = -EINVAL;
> > -        goto out;
> > -    }
> 
> This might be fine for newer binaries, but you'll break older ones.
> 
> Instead, you should skip searching for strtab if we've already located
> the Xen notes.

:-(, maybe I should have gone futher on explaining why this check is
useless (and probably at the wrong place, at least now).

The next thing that's done after that check is:
elf_parse_binary()
elf_xen_parse()
Those are located in "xen/common/libelf", and those are the functions
that actually takes care of extracting data from the elf.

elf_xen_parse() first look for Xen ELFNOTE in the program segments
(phdr, PT_NOTE) and skip reading section and strtab if found.

So, libelf already does what you asked for ;-).

The shstrtab are only used to look for legacy __xen_guest section names.
Since ELFNOTEs was used, the name of section aren't looked at.

I hope that help.

Thanks,
Wei Liu May 16, 2019, 1:23 p.m. UTC | #5
On Wed, May 15, 2019 at 01:55:30PM +0100, Anthony PERARD wrote:
> On Wed, May 15, 2019 at 01:07:03PM +0100, Andrew Cooper wrote:
> > On 15/05/2019 12:40, Anthony PERARD wrote:
> > > This was probably useful to load ELF Note, but now ELF notes
> > > "should live in a PT_NOTE segment" (elfnote.h).
> > >
> > > With notes living in segment, there are no need for sections, so there
> > > is nothing to be stored in the shstrtab.
> > >
> > > This patch would allow to write a simpler ELF header for an OVMF blob
> > > (which isn't an ELF) and allow it to be loaded as a PVH kernel. The
> > > header only needs to declare two program segments:
> > > - one to tell an ELF loader where to put the blob,
> > > - one for a Xen ELFNOTE.
> > >
> > > The ELFNOTE is to comply to the pvh design which wants the
> > > XEN_ELFNOTE_PHYS32_ENTRY to declare a blob as compaptible with the PVH
> > > boot ABI.
> > >
> > > Note that without the ELFNOTE, libxc will load an ELF but with
> > > the plain ELF loader, which doesn't check for shstrtab.
> > >
> > > Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> > > ---
> > >  tools/libxc/xc_dom_elfloader.c | 9 ---------
> > >  1 file changed, 9 deletions(-)
> > >
> > > diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
> > > index 82b5f2ee79..b327db219d 100644
> > > --- a/tools/libxc/xc_dom_elfloader.c
> > > +++ b/tools/libxc/xc_dom_elfloader.c
> > > @@ -165,15 +165,6 @@ static elf_negerrnoval xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
> > >          return rc;
> > >      }
> > >  
> > > -    /* Find the section-header strings table. */
> > > -    if ( ELF_PTRVAL_INVALID(elf->sec_strtab) )
> > > -    {
> > > -        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: ELF image"
> > > -                     " has no shstrtab", __FUNCTION__);
> > > -        rc = -EINVAL;
> > > -        goto out;
> > > -    }
> > 
> > This might be fine for newer binaries, but you'll break older ones.
> > 
> > Instead, you should skip searching for strtab if we've already located
> > the Xen notes.
> 
> :-(, maybe I should have gone futher on explaining why this check is
> useless (and probably at the wrong place, at least now).
> 
> The next thing that's done after that check is:
> elf_parse_binary()
> elf_xen_parse()
> Those are located in "xen/common/libelf", and those are the functions
> that actually takes care of extracting data from the elf.
> 
> elf_xen_parse() first look for Xen ELFNOTE in the program segments
> (phdr, PT_NOTE) and skip reading section and strtab if found.
> 
> So, libelf already does what you asked for ;-).
> 
> The shstrtab are only used to look for legacy __xen_guest section names.
> Since ELFNOTEs was used, the name of section aren't looked at.
> 
> I hope that help.
> 

Andrew, do you still have concern?

Wei.

> Thanks,
> 
> -- 
> Anthony PERARD
Andrew Cooper May 16, 2019, 1:38 p.m. UTC | #6
On 16/05/2019 14:23, Wei Liu wrote:
> On Wed, May 15, 2019 at 01:55:30PM +0100, Anthony PERARD wrote:
>> On Wed, May 15, 2019 at 01:07:03PM +0100, Andrew Cooper wrote:
>>> On 15/05/2019 12:40, Anthony PERARD wrote:
>>>> This was probably useful to load ELF Note, but now ELF notes
>>>> "should live in a PT_NOTE segment" (elfnote.h).
>>>>
>>>> With notes living in segment, there are no need for sections, so there
>>>> is nothing to be stored in the shstrtab.
>>>>
>>>> This patch would allow to write a simpler ELF header for an OVMF blob
>>>> (which isn't an ELF) and allow it to be loaded as a PVH kernel. The
>>>> header only needs to declare two program segments:
>>>> - one to tell an ELF loader where to put the blob,
>>>> - one for a Xen ELFNOTE.
>>>>
>>>> The ELFNOTE is to comply to the pvh design which wants the
>>>> XEN_ELFNOTE_PHYS32_ENTRY to declare a blob as compaptible with the PVH
>>>> boot ABI.
>>>>
>>>> Note that without the ELFNOTE, libxc will load an ELF but with
>>>> the plain ELF loader, which doesn't check for shstrtab.
>>>>
>>>> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
>>>> ---
>>>>  tools/libxc/xc_dom_elfloader.c | 9 ---------
>>>>  1 file changed, 9 deletions(-)
>>>>
>>>> diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
>>>> index 82b5f2ee79..b327db219d 100644
>>>> --- a/tools/libxc/xc_dom_elfloader.c
>>>> +++ b/tools/libxc/xc_dom_elfloader.c
>>>> @@ -165,15 +165,6 @@ static elf_negerrnoval xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
>>>>          return rc;
>>>>      }
>>>>  
>>>> -    /* Find the section-header strings table. */
>>>> -    if ( ELF_PTRVAL_INVALID(elf->sec_strtab) )
>>>> -    {
>>>> -        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: ELF image"
>>>> -                     " has no shstrtab", __FUNCTION__);
>>>> -        rc = -EINVAL;
>>>> -        goto out;
>>>> -    }
>>> This might be fine for newer binaries, but you'll break older ones.
>>>
>>> Instead, you should skip searching for strtab if we've already located
>>> the Xen notes.
>> :-(, maybe I should have gone futher on explaining why this check is
>> useless (and probably at the wrong place, at least now).
>>
>> The next thing that's done after that check is:
>> elf_parse_binary()
>> elf_xen_parse()
>> Those are located in "xen/common/libelf", and those are the functions
>> that actually takes care of extracting data from the elf.
>>
>> elf_xen_parse() first look for Xen ELFNOTE in the program segments
>> (phdr, PT_NOTE) and skip reading section and strtab if found.
>>
>> So, libelf already does what you asked for ;-).
>>
>> The shstrtab are only used to look for legacy __xen_guest section names.
>> Since ELFNOTEs was used, the name of section aren't looked at.
>>
>> I hope that help.
>>
> Andrew, do you still have concern?

If libelf goes on to DTRT then fine, but this full explanation needs to
be in the commit message.

~Andrew
diff mbox series

Patch

diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
index 82b5f2ee79..b327db219d 100644
--- a/tools/libxc/xc_dom_elfloader.c
+++ b/tools/libxc/xc_dom_elfloader.c
@@ -165,15 +165,6 @@  static elf_negerrnoval xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
         return rc;
     }
 
-    /* Find the section-header strings table. */
-    if ( ELF_PTRVAL_INVALID(elf->sec_strtab) )
-    {
-        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: ELF image"
-                     " has no shstrtab", __FUNCTION__);
-        rc = -EINVAL;
-        goto out;
-    }
-
     /* parse binary and get xen meta info */
     elf_parse_binary(elf);
     if ( elf_xen_parse(elf, &dom->parms) != 0 )