From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:49148) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RznBZ-0007Zi-FA for qemu-devel@nongnu.org; Tue, 21 Feb 2012 05:43:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RznBU-0005lI-NM for qemu-devel@nongnu.org; Tue, 21 Feb 2012 05:43:05 -0500 Received: from mga06.intel.com ([134.134.136.21]:17968 helo=orsmga101.jf.intel.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RznBU-0005l5-6S for qemu-devel@nongnu.org; Tue, 21 Feb 2012 05:43:00 -0500 Received: by lagy4 with SMTP id y4so4834759lag.25 for ; Tue, 21 Feb 2012 02:42:55 -0800 (PST) MIME-Version: 1.0 Date: Tue, 21 Feb 2012 12:42:55 +0200 Message-ID: From: "Damian, Alexandru" Content-Type: multipart/alternative; boundary=bcaec554d9e2d9fd1904b977120e Subject: [Qemu-devel] fix ELF loading for 0-length sections List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org --bcaec554d9e2d9fd1904b977120e Content-Type: text/plain; charset=ISO-8859-1 Hi, I got a problem with QEMU refusing to load an ELF binary with 0-length sections, while the kernel has no issue doing this. This patch adds a check that has been in kernel since 2008 at least. Cheers, Alex ---------------- commit a42e5231c1be5f09caeb6c73e34933cd7efa7023 Author: Alexandru DAMIAN Date: Tue Feb 21 12:34:36 2012 +0200 Do not attempt to map 0-length sections Mmap will return an invalid argument, but 0-length sections are valid in any case. The kernel as a similar check when loading elf binaries courtesy of jkosina@suse.cz. Signed-off-by: Alexandru Damian diff --git a/linux-user/elfload.c b/linux-user/elfload.c index ea61d0d..71d0ae3 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -918,9 +918,9 @@ static inline void init_thread(struct target_pt_regs *regs, #define elf_check_arch(x) ( (x) == ELF_ARCH ) -#define ELF_CLASS ELFCLASS64 -#define ELF_DATA ELFDATA2MSB -#define ELF_ARCH EM_S390 +#define ELF_CLASS ELFCLASS64 +#define ELF_DATA ELFDATA2MSB +#define ELF_ARCH EM_S390 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) { @@ -1565,11 +1565,16 @@ static void load_elf_image(const char *image_name, int image_fd, vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr); vaddr_ps = TARGET_ELF_PAGESTART(vaddr); - error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, - elf_prot, MAP_PRIVATE | MAP_FIXED, - image_fd, eppnt->p_offset - vaddr_po); - if (error == -1) { - goto exit_perror; + /* Don't attempt to map 0 bytes len sections. + Kernel also has this check. + */ + if (eppnt->p_filesz + vaddr_po != 0) { + error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, + elf_prot, MAP_PRIVATE | MAP_FIXED, + image_fd, eppnt->p_offset - vaddr_po); + if (error == -1) { + goto exit_perror; + } } vaddr_ef = vaddr + eppnt->p_filesz; --bcaec554d9e2d9fd1904b977120e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi,

I got a problem with QEMU refusing to load an ELF binary with 0-= length sections,
while the kernel has no issue doing this.

This = patch adds a check that has been in kernel since 2008 at least.

Chee= rs,
Alex

----------------
commit a42e5231c1be5f09caeb6c73e34933cd7efa= 7023
Author: Alexandru DAMIAN <alexandru.damian@intel.com>
Date:=A0=A0 Tue Feb 21 12:34:= 36 2012 +0200

=A0=A0=A0 Do not attempt to map 0-length sections
=A0=A0=A0
=A0= =A0=A0 Mmap will return an invalid argument, but 0-length sections
=A0= =A0=A0 are valid in any case. The kernel as a similar check
=A0=A0=A0 wh= en loading elf binaries courtesy of jkos= ina@suse.cz.
=A0=A0=A0
=A0=A0=A0 Signed-off-by: Alexandru Damian <alexandru.damian@intel.com>

di= ff --git a/linux-user/elfload.c b/linux-user/elfload.c
index ea61d0d..71= d0ae3 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -918,9 +918,= 9 @@ static inline void init_thread(struct target_pt_regs *regs,
=A0
= =A0#define elf_check_arch(x) ( (x) =3D=3D ELF_ARCH )
=A0
-#define ELF= _CLASS=A0=A0=A0 ELFCLASS64
-#define ELF_DATA=A0=A0=A0 ELFDATA2MSB
-#define ELF_ARCH=A0=A0=A0 EM_S39= 0
+#define ELF_CLASS=A0=A0=A0=A0=A0=A0=A0 ELFCLASS64
+#define ELF_DAT= A=A0=A0=A0=A0=A0=A0=A0 ELFDATA2MSB
+#define ELF_ARCH=A0=A0=A0=A0=A0=A0= =A0 EM_S390
=A0
=A0static inline void init_thread(struct target_pt_re= gs *regs, struct image_info *infop)
=A0{
@@ -1565,11 +1565,16 @@ static void load_elf_image(const char *imag= e_name, int image_fd,
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 vaddr_po =3D = TARGET_ELF_PAGEOFFSET(vaddr);
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 vaddr= _ps =3D TARGET_ELF_PAGESTART(vaddr);
=A0
-=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 error =3D target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
-=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 elf_prot, MAP_PRIVATE | MAP_FIXED,
-=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 image_fd, eppnt->p_offset - vaddr_po);
-=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 if (error =3D=3D -1) {
-=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 goto exit_perror;
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 /* Don't attempt to map 0 bytes len = sections.
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Kernel also has th= is check.
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 */
+=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0 if (eppnt->p_filesz + vaddr_po !=3D 0) {
+=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 error =3D target_mmap(vadd= r_ps, eppnt->p_filesz + vaddr_po,
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 elf_prot, MAP_PRIVATE | MAP_F= IXED,
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 image_fd, eppnt->= p_offset - vaddr_po);
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0 if (error =3D=3D -1) {
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 goto exit_perror;
+=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 }
=A0
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 vaddr_ef =3D vaddr + eppnt->p_filesz;

--bcaec554d9e2d9fd1904b977120e--