[Qemu-devel] [PATCH] linux-user: elf: mmap all the target-pages of hostpage for data segment

Shivaprasad G Bhat posted 1 patch 7 years, 2 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/153537327445.92657.5884855110375406189.stgit@lep8c.aus.stglabs.ibm.com
Test docker-clang@ubuntu failed
Test checkpatch passed
There is a newer version of this series
linux-user/elfload.c |   24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
[Qemu-devel] [PATCH] linux-user: elf: mmap all the target-pages of hostpage for data segment
Posted by Shivaprasad G Bhat 7 years, 2 months ago
If the hostpage size is greater than the TARGET_PAGESIZE, the
target-pages of size TARGET_PAGESIZE are marked valid only till the
length requested during the elfload. The glibc attempts to consume unused
space in the last page of data segment(__libc_memalign() in
elf/dl-minimal.c). The GLRO(dl_pagesize) is actually the host pagesize as
set in the auxillary vectors. So, there is no explicit mmap request for
the remaining target-pages on the last hostpage. The glibc assumes that
particular space as available and subsequent attempts to use
those addresses lead to crash as the target_mmap has not marked them valid
for those target-pages.

The issue is seen when trying to chroot to 16.04-x86_64 ubuntu on a PPC64
host where the fork fails to access the thread_id as it is allocated on a
page not marked valid. The recent glibc doesnt have checks for thread-id in
fork, but the issue can manifest somewhere else, none the less.

The fix here is to map all the target-pages of the hostpage during the
ELF load for data segment to allow the glibc for proper consumption.

Signed-off-by: Shivaprasad G Bhat <sbhat@linux.vnet.ibm.com>
---
 linux-user/elfload.c |   24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 8638612aec..1d86034c8d 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1438,9 +1438,17 @@ struct exec
 
 /* Necessary parameters */
 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
-#define TARGET_ELF_PAGESTART(_v) ((_v) & \
-                                 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
-#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
+#define TARGET_ELF_PAGESTART(_v, _s) \
+        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
+         (_v) & ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE - 1) : \
+         (_v) & ~(abi_ulong)(_s - 1));
+#define TARGET_ELF_PAGEOFFSET(_v, _s) \
+        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
+         (_v) & (TARGET_ELF_EXEC_PAGESIZE - 1) : \
+         (_v) & (_s - 1));
+#define TARGET_ELF_PAGELENGTH(_v, _s) \
+        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
+         TARGET_PAGE_ALIGN(_v) : HOST_PAGE_ALIGN(_v));
 
 #define DLINFO_ITEMS 15
 
@@ -2279,7 +2287,7 @@ static void load_elf_image(const char *image_name, int image_fd,
     for (i = 0; i < ehdr->e_phnum; i++) {
         struct elf_phdr *eppnt = phdr + i;
         if (eppnt->p_type == PT_LOAD) {
-            abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
+            abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
             int elf_prot = 0;
 
             if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
@@ -2287,10 +2295,12 @@ static void load_elf_image(const char *image_name, int image_fd,
             if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
 
             vaddr = load_bias + eppnt->p_vaddr;
-            vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
-            vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
+            vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr, qemu_host_page_size);
+            vaddr_ps = TARGET_ELF_PAGESTART(vaddr, qemu_host_page_size);
+            vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po,
+                                              qemu_host_page_size);
 
-            error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
+            error = target_mmap(vaddr_ps, vaddr_len,
                                 elf_prot, MAP_PRIVATE | MAP_FIXED,
                                 image_fd, eppnt->p_offset - vaddr_po);
             if (error == -1) {


Re: [Qemu-devel] [PATCH] linux-user: elf: mmap all the target-pages of hostpage for data segment
Posted by Laurent Vivier 7 years, 2 months ago
Le 27/08/2018 à 14:37, Shivaprasad G Bhat a écrit :
> If the hostpage size is greater than the TARGET_PAGESIZE, the
> target-pages of size TARGET_PAGESIZE are marked valid only till the
> length requested during the elfload. The glibc attempts to consume unused
> space in the last page of data segment(__libc_memalign() in
> elf/dl-minimal.c). The GLRO(dl_pagesize) is actually the host pagesize as
> set in the auxillary vectors. So, there is no explicit mmap request for
> the remaining target-pages on the last hostpage. The glibc assumes that
> particular space as available and subsequent attempts to use
> those addresses lead to crash as the target_mmap has not marked them valid
> for those target-pages.
> 
> The issue is seen when trying to chroot to 16.04-x86_64 ubuntu on a PPC64
> host where the fork fails to access the thread_id as it is allocated on a
> page not marked valid. The recent glibc doesnt have checks for thread-id in
> fork, but the issue can manifest somewhere else, none the less.
> 
> The fix here is to map all the target-pages of the hostpage during the
> ELF load for data segment to allow the glibc for proper consumption.
> 
> Signed-off-by: Shivaprasad G Bhat <sbhat@linux.vnet.ibm.com>
> ---
>  linux-user/elfload.c |   24 +++++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/linux-user/elfload.c b/linux-user/elfload.c
> index 8638612aec..1d86034c8d 100644
> --- a/linux-user/elfload.c
> +++ b/linux-user/elfload.c
> @@ -1438,9 +1438,17 @@ struct exec
>  
>  /* Necessary parameters */
>  #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
> -#define TARGET_ELF_PAGESTART(_v) ((_v) & \
> -                                 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
> -#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
> +#define TARGET_ELF_PAGESTART(_v, _s) \
> +        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
> +         (_v) & ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE - 1) : \
> +         (_v) & ~(abi_ulong)(_s - 1));
> +#define TARGET_ELF_PAGEOFFSET(_v, _s) \
> +        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
> +         (_v) & (TARGET_ELF_EXEC_PAGESIZE - 1) : \
> +         (_v) & (_s - 1));
> +#define TARGET_ELF_PAGELENGTH(_v, _s) \
> +        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
> +         TARGET_PAGE_ALIGN(_v) : HOST_PAGE_ALIGN(_v));

I think it's only possible if the PT_LOAD p_align value is greater or
equal to qemu_host_page_size.

See 33143c446e ("linux-user: fix ELF load alignment error").

You could check this with qemu-s390x or qemu-arm on ppc64 host.

Thanks,
Laurent


Re: [Qemu-devel] [PATCH] linux-user: elf: mmap all the target-pages of hostpage for data segment
Posted by Shivaprasad G Bhat 7 years, 2 months ago

On 08/27/2018 06:55 PM, Laurent Vivier wrote:
> Le 27/08/2018 à 14:37, Shivaprasad G Bhat a écrit :
>> If the hostpage size is greater than the TARGET_PAGESIZE, the
>> target-pages of size TARGET_PAGESIZE are marked valid only till the
>> length requested during the elfload. The glibc attempts to consume unused
>> space in the last page of data segment(__libc_memalign() in
>> elf/dl-minimal.c). The GLRO(dl_pagesize) is actually the host pagesize as
>> set in the auxillary vectors. So, there is no explicit mmap request for
>> the remaining target-pages on the last hostpage. The glibc assumes that
>> particular space as available and subsequent attempts to use
>> those addresses lead to crash as the target_mmap has not marked them valid
>> for those target-pages.
>>
>> The issue is seen when trying to chroot to 16.04-x86_64 ubuntu on a PPC64
>> host where the fork fails to access the thread_id as it is allocated on a
>> page not marked valid. The recent glibc doesnt have checks for thread-id in
>> fork, but the issue can manifest somewhere else, none the less.
>>
>> The fix here is to map all the target-pages of the hostpage during the
>> ELF load for data segment to allow the glibc for proper consumption.
>>
>> Signed-off-by: Shivaprasad G Bhat <sbhat@linux.vnet.ibm.com>
>> ---
>>   linux-user/elfload.c |   24 +++++++++++++++++-------
>>   1 file changed, 17 insertions(+), 7 deletions(-)
>>
>> diff --git a/linux-user/elfload.c b/linux-user/elfload.c
>> index 8638612aec..1d86034c8d 100644
>> --- a/linux-user/elfload.c
>> +++ b/linux-user/elfload.c
>> @@ -1438,9 +1438,17 @@ struct exec
>>   
>>   /* Necessary parameters */
>>   #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
>> -#define TARGET_ELF_PAGESTART(_v) ((_v) & \
>> -                                 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
>> -#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
>> +#define TARGET_ELF_PAGESTART(_v, _s) \
>> +        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
>> +         (_v) & ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE - 1) : \
>> +         (_v) & ~(abi_ulong)(_s - 1));
>> +#define TARGET_ELF_PAGEOFFSET(_v, _s) \
>> +        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
>> +         (_v) & (TARGET_ELF_EXEC_PAGESIZE - 1) : \
>> +         (_v) & (_s - 1));
>> +#define TARGET_ELF_PAGELENGTH(_v, _s) \
>> +        ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \
>> +         TARGET_PAGE_ALIGN(_v) : HOST_PAGE_ALIGN(_v));
> I think it's only possible if the PT_LOAD p_align value is greater or
> equal to qemu_host_page_size.
>
> See 33143c446e ("linux-user: fix ELF load alignment error").
>
> You could check this with qemu-s390x or qemu-arm on ppc64 host.
Ah, right! I should have added the extra conditional to check the 
p_align with qemu_host_page_mask
along with the existing ones.

Posted the v2 accordingly.

Thanks and Regards,
Shivaprasad

> Thanks,
> Laurent
>