All of lore.kernel.org
 help / color / mirror / Atom feed
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
To: Ross Lagerwall <ross.lagerwall@citrix.com>
Cc: Tim Deegan <tim@xen.org>, Ian Jackson <ian.jackson@eu.citrix.com>,
	Ian Campbell <ian.campbell@citrix.com>,
	Jan Beulich <jbeulich@suse.com>,
	xen-devel@lists.xen.org
Subject: Re: [PATCH v1 06/11] xsplice: Add helper elf routines
Date: Wed, 4 Nov 2015 16:49:00 -0500	[thread overview]
Message-ID: <20151104214900.GA23069@char.us.oracle.com> (raw)
In-Reply-To: <1446574568-9644-6-git-send-email-ross.lagerwall@citrix.com>

On Tue, Nov 03, 2015 at 06:16:03PM +0000, Ross Lagerwall wrote:
> Add some elf routines and data structures in preparation for loading an
> xsplice payload.
> 
> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
> ---
>  xen/common/Makefile           |   1 +
>  xen/common/xsplice_elf.c      | 122 ++++++++++++++++++++++++++++++++++++++++++
>  xen/include/xen/xsplice_elf.h |  44 +++++++++++++++
>  3 files changed, 167 insertions(+)
>  create mode 100644 xen/common/xsplice_elf.c
>  create mode 100644 xen/include/xen/xsplice_elf.h
> 
> diff --git a/xen/common/Makefile b/xen/common/Makefile
> index 1b17c9d..de7c08a 100644
> --- a/xen/common/Makefile
> +++ b/xen/common/Makefile
> @@ -57,6 +57,7 @@ obj-y += vsprintf.o
>  obj-y += wait.o
>  obj-y += xmalloc_tlsf.o
>  obj-y += xsplice.o
> +obj-y += xsplice_elf.o
>  
>  obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
>  
> diff --git a/xen/common/xsplice_elf.c b/xen/common/xsplice_elf.c
> new file mode 100644
> index 0000000..13a9229
> --- /dev/null
> +++ b/xen/common/xsplice_elf.c
> @@ -0,0 +1,122 @@

Do you want to add your company copyright header here?

> +#include <xen/lib.h>
> +#include <xen/errno.h>
> +#include <xen/xsplice_elf.h>
> +
> +struct xsplice_elf_sec *xsplice_elf_sec_by_name(const struct xsplice_elf *elf,
> +                                                const char *name)
> +{
> +    int i;

unsigned int ?

> +
> +    for ( i = 0; i < elf->hdr->e_shnum; i++ )
> +    {
> +        if ( !strcmp(name, elf->sec[i].name) )
> +            return &elf->sec[i];
> +    }
> +
> +    return NULL;
> +}
> +
> +static int elf_get_sections(struct xsplice_elf *elf, uint8_t *data)
> +{
> +    struct xsplice_elf_sec *sec;
> +    int i;

unsigned int;

Should we check the e_shnum for out of bound values? Hmm,
uint16t so not that bad..

> +
> +    sec = xmalloc_array(struct xsplice_elf_sec, elf->hdr->e_shnum);
> +    if ( !sec )
> +    {
> +        printk(XENLOG_ERR "Could not find section table\n");

Well that is not exactly right. It couldnt' allocate the memory.

Perhaps we should say:
	"Could not allocate memory for %s which has %u sections!\n", elf->name, elf->hdr->e_shnum);

> +        return -ENOMEM;
> +    }
> +
> +    for ( i = 0; i < elf->hdr->e_shnum; i++ )
> +    {
> +#ifdef CONFIG_ARM_32
> +        sec[i].sec = (Elf32_Shdr *)(data + elf->hdr->e_shoff +
> +                                    i * elf->hdr->e_shentsize);
> +#else
> +        sec[i].sec = (Elf64_Shdr *)(data + elf->hdr->e_shoff +
> +                                    i * elf->hdr->e_shentsize);
> +#endif

> +        sec[i].data = data + sec[i].sec->sh_offset;

We should validate that the 'sec[i].data' pointers are not outside
the memory allocated for 'data'.

> +    }
> +    elf->sec = sec;
> +
> +    return 0;
> +}
> +
> +static int elf_get_sym(struct xsplice_elf *elf, uint8_t *data)
> +{
> +    struct xsplice_elf_sec *symtab, *strtab_sec;
> +    struct xsplice_elf_sym *sym;
> +    const char *strtab;
> +    int i;
> +
> +    symtab = xsplice_elf_sec_by_name(elf, ".symtab");
> +    if ( !symtab )
> +    {
> +        printk(XENLOG_ERR "Could not find symbol table\n");
> +        return -EINVAL;
> +    }
> +
> +    strtab_sec = xsplice_elf_sec_by_name(elf, ".strtab");
> +    if ( !strtab_sec )
> +    {
> +        printk(XENLOG_ERR "Could not find string table\n");
> +        return -EINVAL;
> +    }
> +    strtab = (const char *)(data + strtab_sec->sec->sh_offset);

Should we do a check to make sure that 'strtab' is not bigger
than the amount of memory allocated for 'data'?

> +
> +    elf->nsym = symtab->sec->sh_size / symtab->sec->sh_entsize;
> +
> +    sym = xmalloc_array(struct xsplice_elf_sym, elf->nsym);

Perhaps also a validity check on the elf->nsym?..
> +    if ( !sym )
> +    {
> +        printk(XENLOG_ERR "Could not allocate memory for symbols\n");
> +        return -ENOMEM;
> +    }
> +
> +    for ( i = 0; i < elf->nsym; i++ )
> +    {
> +#ifdef CONFIG_ARM_32
> +        sym[i].sym = (Elf32_Sym *)(symtab->data + i * symtab->sec->sh_entsize);
> +#else
> +        sym[i].sym = (Elf64_Sym *)(symtab->data + i * symtab->sec->sh_entsize);
> +#endif
> +        sym[i].name = strtab + sym[i].sym->st_name;

Could we check that the 'sym[i].name is not outside the memory allocated
for data' ?
> +    }
> +    elf->sym = sym;
> +
> +    return 0;
> +}
> +
> +int xsplice_elf_load(struct xsplice_elf *elf, uint8_t *data, ssize_t len)
> +{
> +    const char *shstrtab;
> +    int i, rc;

unsigned int i;

> +
> +#ifdef CONFIG_ARM_32
> +    elf->hdr = (Elf32_Ehdr *)data;
> +#else
> +    elf->hdr = (Elf64_Ehdr *)data;
> +#endif
> +
> +    rc = elf_get_sections(elf, data);
> +    if ( rc )
> +        return rc;
> +
> +    shstrtab = (const char *)(data + elf->sec[elf->hdr->e_shstrndx].sec->sh_offset);

if (shstrtab > data + len)
	return -EINVAL;

> +    for ( i = 0; i < elf->hdr->e_shnum; i++ )
> +        elf->sec[i].name = shstrtab + elf->sec[i].sec->sh_name;
> +
> +    rc = elf_get_sym(elf, data);
> +    if ( rc )
> +        return rc;
> +
> +    return 0;
> +}
> +
> +void xsplice_elf_free(struct xsplice_elf *elf)
> +{
> +    xfree(elf->sec);
> +    xfree(elf->sym);

elf->sec = NULL;
elf->sym = NULL

just in case..
> +}
> diff --git a/xen/include/xen/xsplice_elf.h b/xen/include/xen/xsplice_elf.h
> new file mode 100644
> index 0000000..bac0053
> --- /dev/null
> +++ b/xen/include/xen/xsplice_elf.h
> @@ -0,0 +1,44 @@
> +#ifndef __XEN_XSPLICE_ELF_H__
> +#define __XEN_XSPLICE_ELF_H__
> +
> +#include <xen/types.h>
> +#include <xen/elfstructs.h>
> +
> +/* The following describes an Elf file as consumed by xsplice. */
> +struct xsplice_elf_sec {
> +#ifdef CONFIG_ARM_32
> +    Elf32_Shdr *sec;
> +#else
> +    Elf64_Shdr *sec;
> +#endif
> +    const char *name;
> +    const uint8_t *data;           /* A pointer to the data section */
> +    uint8_t *load_addr;            /* A pointer to the allocated destination */

Missing full stop.
> +};
> +
> +struct xsplice_elf_sym {
> +#ifdef CONFIG_ARM_32
> +    Elf32_Sym *sym;
> +#else
> +    Elf64_Sym *sym;
> +#endif
> +    const char *name;
> +};
> +
> +struct xsplice_elf {
> +#ifdef CONFIG_ARM_32
> +    Elf32_Ehdr *hdr;
> +#else
> +    Elf64_Ehdr *hdr;
> +#endif
> +    struct xsplice_elf_sec *sec;   /* Array of sections */
> +    struct xsplice_elf_sym *sym;   /* Array of symbols */

Missing full stop.
> +    int nsym;

unsigned int nsym;
> +};
> +
> +struct xsplice_elf_sec *xsplice_elf_sec_by_name(const struct xsplice_elf *elf,
> +                                                const char *name);
> +int xsplice_elf_load(struct xsplice_elf *elf, uint8_t *data, ssize_t len);
> +void xsplice_elf_free(struct xsplice_elf *elf);
> +
> +#endif /* __XEN_XSPLICE_ELF_H__ */
> -- 
> 2.4.3
> 
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> http://lists.xen.org/xen-devel

  reply	other threads:[~2015-11-04 21:49 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-03 18:15 [PATCH v1 01/11] xsplice: Design document (v2) Ross Lagerwall
2015-11-03 18:15 ` [PATCH v1 02/11] xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op Ross Lagerwall
2015-11-04 21:17   ` Konrad Rzeszutek Wilk
2015-11-12 16:28   ` Jan Beulich
2015-11-13 14:13     ` Konrad Rzeszutek Wilk
2015-11-13 23:50   ` Daniel De Graaf
2015-11-03 18:16 ` [PATCH v1 03/11] libxc: Implementation of XEN_XSPLICE_op in libxc Ross Lagerwall
2015-11-03 18:16 ` [PATCH v1 04/11] xen-xsplice: Tool to manipulate xsplice payloads Ross Lagerwall
2015-11-04 21:27   ` Konrad Rzeszutek Wilk
2015-11-03 18:16 ` [PATCH v1 05/11] elf: Add relocation types to elfstructs.h Ross Lagerwall
2015-11-05 10:38   ` Jan Beulich
2015-11-05 11:52     ` Ross Lagerwall
2015-11-03 18:16 ` [PATCH v1 06/11] xsplice: Add helper elf routines Ross Lagerwall
2015-11-04 21:49   ` Konrad Rzeszutek Wilk [this message]
2015-11-03 18:16 ` [PATCH v1 07/11] xsplice: Implement payload loading Ross Lagerwall
2015-11-04 22:21   ` Konrad Rzeszutek Wilk
2015-11-05 10:35     ` Jan Beulich
2015-11-05 11:51       ` Ross Lagerwall
2015-11-05 12:13         ` Jan Beulich
2015-11-05 11:15     ` Ross Lagerwall
2015-11-05 20:12       ` Konrad Rzeszutek Wilk
2015-11-03 18:16 ` [PATCH v1 08/11] xsplice: Implement support for applying patches Ross Lagerwall
2015-11-05  3:17   ` Konrad Rzeszutek Wilk
2015-11-05 11:45     ` Ross Lagerwall
2015-11-05 20:08       ` Konrad Rzeszutek Wilk
2015-11-05  3:19   ` Konrad Rzeszutek Wilk
2015-11-27 13:51   ` Martin Pohlack
2015-11-03 18:16 ` [PATCH v1 09/11] xsplice: Add support for bug frames Ross Lagerwall
2015-11-05 19:43   ` Konrad Rzeszutek Wilk
2015-11-03 18:16 ` [PATCH v1 10/11] xsplice: Add support for exception tables Ross Lagerwall
2015-11-05 19:47   ` Konrad Rzeszutek Wilk
2015-11-27 16:28   ` Martin Pohlack
2015-11-27 17:05     ` Ross Lagerwall
2015-11-03 18:16 ` [PATCH v1 11/11] xsplice: Add support for alternatives Ross Lagerwall
2015-11-05 19:48   ` Konrad Rzeszutek Wilk
2015-11-04 21:10 ` [PATCH v1 01/11] xsplice: Design document (v2) Konrad Rzeszutek Wilk
2015-11-05 10:49   ` Ross Lagerwall
2015-11-05 19:56     ` Konrad Rzeszutek Wilk
2015-11-10  9:55 ` Ross Lagerwall
2015-11-27 12:48 ` Martin Pohlack

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151104214900.GA23069@char.us.oracle.com \
    --to=konrad.wilk@oracle.com \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=ross.lagerwall@citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.