All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] linux-user: Fix the load of ELF files that have no "useful" symbol
@ 2011-06-01 12:36 Cédric VINCENT
  2011-06-01 13:19 ` Richard Henderson
  0 siblings, 1 reply; 2+ messages in thread
From: Cédric VINCENT @ 2011-06-01 12:36 UTC (permalink / raw)
  To: qemu-devel; +Cc: Cédric VINCENT, Riku Voipio, Yves JANIN

This patch fixes a "double free()" due to "realloc(syms, 0)" in the
loader when the ELF file has no "useful" symbol, as with the following
example (compiled with "sh4-linux-gcc -nostdlib"):

    .text
    .align 1
    .global _start
    _start:
        mov     #1, r3
        trapa   #40     // syscall(__NR_exit)
        nop

The bug appears when the log (option "-d") is enabled.

Signed-off-by: Cédric VINCENT <cedric.vincent@st.com>
Signed-off-by: Yves JANIN <yves.janin@st.com>
---
 linux-user/elfload.c |   34 +++++++++++++++++++---------------
 1 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 6f67286..4e77c89 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1650,9 +1650,9 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
 {
     int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
     struct elf_shdr *shdr;
-    char *strings;
-    struct syminfo *s;
-    struct elf_sym *syms, *new_syms;
+    char *strings = NULL;
+    struct syminfo *s = NULL;
+    struct elf_sym *new_syms, *syms = NULL;
 
     shnum = hdr->e_shnum;
     i = shnum * sizeof(struct elf_shdr);
@@ -1677,24 +1677,19 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
     /* Now know where the strtab and symtab are.  Snarf them.  */
     s = malloc(sizeof(*s));
     if (!s) {
-        return;
+        goto give_up;
     }
 
     i = shdr[str_idx].sh_size;
     s->disas_strtab = strings = malloc(i);
     if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
-        free(s);
-        free(strings);
-        return;
+        goto give_up;
     }
 
     i = shdr[sym_idx].sh_size;
     syms = malloc(i);
     if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
-        free(s);
-        free(strings);
-        free(syms);
-        return;
+        goto give_up;
     }
 
     nsyms = i / sizeof(struct elf_sym);
@@ -1717,16 +1712,18 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
         }
     }
 
+    /* No "useful" symbol.  */
+    if (nsyms == 0) {
+        goto give_up;
+    }
+
     /* Attempt to free the storage associated with the local symbols
        that we threw away.  Whether or not this has any effect on the
        memory allocation depends on the malloc implementation and how
        many symbols we managed to discard.  */
     new_syms = realloc(syms, nsyms * sizeof(*syms));
     if (new_syms == NULL) {
-        free(s);
-        free(syms);
-        free(strings);
-        return;
+        goto give_up;
     }
     syms = new_syms;
 
@@ -1741,6 +1738,13 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
     s->lookup_symbol = lookup_symbolxx;
     s->next = syminfos;
     syminfos = s;
+
+    return;
+
+give_up:
+    free(s);
+    free(strings);
+    free(syms);
 }
 
 int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
-- 
1.7.5.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Fix the load of ELF files that have no "useful" symbol
  2011-06-01 12:36 [Qemu-devel] [PATCH] linux-user: Fix the load of ELF files that have no "useful" symbol Cédric VINCENT
@ 2011-06-01 13:19 ` Richard Henderson
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Henderson @ 2011-06-01 13:19 UTC (permalink / raw)
  To: Cédric VINCENT; +Cc: Yves JANIN, Riku Voipio, qemu-devel

On 06/01/2011 05:36 AM, Cédric VINCENT wrote:
> This patch fixes a "double free()" due to "realloc(syms, 0)" in the
> loader when the ELF file has no "useful" symbol, as with the following
> example (compiled with "sh4-linux-gcc -nostdlib"):
> 
>     .text
>     .align 1
>     .global _start
>     _start:
>         mov     #1, r3
>         trapa   #40     // syscall(__NR_exit)
>         nop
> 
> The bug appears when the log (option "-d") is enabled.
> 
> Signed-off-by: Cédric VINCENT <cedric.vincent@st.com>
> Signed-off-by: Yves JANIN <yves.janin@st.com>

Reviewed-by: Richard Henderson <rth@twiddle.net>


r~

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-06-01 13:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-01 12:36 [Qemu-devel] [PATCH] linux-user: Fix the load of ELF files that have no "useful" symbol Cédric VINCENT
2011-06-01 13:19 ` Richard Henderson

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.