Skip to contentSkip to conversation

elfload: Fix alignment when unmapping excess reservation

RFC PATCH In review Fabiano Rosas 3 participants 5 messages Feb 13, 2025 – Feb 19, 2025

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 elfload: Fix alignment when unmapping excess reservation

When complying with the alignment requested in the ELF and unmapping the excess reservation, having align_end not aligned to the guest page causes the unmap to be rejected by the alignment check at target_munmap and later brk adjustments hit an EEXIST.

Fix by aligning the start of region to be unmapped.

Fixes c81d1fafa6 ("linux-user: Honor elf alignment when placing images")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1913

---
In the bug there was mention of the vdso landing in the wrong spot, but I don't see evidence of this in my testing. Looking at the addresses in the bug report, there seems to have been a mistake because I don't see an overlap there either.

---
 linux-user/elfload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +1 -1
index a2c152e5ad..05ee5e74fd 100644
@@ -3351,7 +3351,7 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
 
     if (align_size != reserve_size) {
         abi_ulong align_addr = ROUND_UP(load_addr, align);
-        abi_ulong align_end = align_addr + reserve_size;
+        abi_ulong align_end = TARGET_PAGE_ALIGN(align_addr + reserve_size);
         abi_ulong load_end = load_addr + align_size;
 
         if (align_addr != load_addr) {
Signature · 1 line
2.35.3
#1 lore raw
Quoted 24 lines · Fabiano Rosas

When complying with the alignment requested in the ELF and unmapping the excess reservation, having align_end not aligned to the guest page causes the unmap to be rejected by the alignment check at target_munmap and later brk adjustments hit an EEXIST.

Fix by aligning the start of region to be unmapped.

Fixes c81d1fafa6 ("linux-user: Honor elf alignment when placing images")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1913

---
In the bug there was mention of the vdso landing in the wrong spot, but I don't see evidence of this in my testing. Looking at the addresses in the bug report, there seems to have been a mistake because I don't see an overlap there either.

---
  linux-user/elfload.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +0 -0
index a2c152e5ad..05ee5e74fd 100644

linux-user/elfload.c static void load_elf_image(const char *image_name, const ImageSource *src, · line 3355

Quoted 6 lines
linux-user/elfload.c +1 -1
@@ -3351,7 +3351,7 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
 
      if (align_size != reserve_size) {
          abi_ulong align_addr = ROUND_UP(load_addr, align);
-        abi_ulong align_end = align_addr + reserve_size;
+        abi_ulong align_end = TARGET_PAGE_ALIGN(align_addr + reserve_size);
          abi_ulong load_end = load_addr + align_size;

Both align_end and load_end must be aligned.

r~

#2 lore raw
Quoted 20 lines · Richard Henderson
Quoted 19 lines · Fabiano Rosas

When complying with the alignment requested in the ELF and unmapping the excess reservation, having align_end not aligned to the guest page causes the unmap to be rejected by the alignment check at target_munmap and later brk adjustments hit an EEXIST.

Fix by aligning the start of region to be unmapped.

Fixes c81d1fafa6 ("linux-user: Honor elf alignment when placing images")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1913

---
In the bug there was mention of the vdso landing in the wrong spot, but I don't see evidence of this in my testing. Looking at the addresses in the bug report, there seems to have been a mistake because I don't see an overlap there either.
---
  linux-user/elfload.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

linux-user/elfload.c static void load_elf_image(const char *image_name, const · line 3355

Quoted 11 lines
linux-user/elfload.c +1 -1
index a2c152e5ad..05ee5e74fd 100644
@@ -3351,7 +3351,7 @@ static void load_elf_image(const char *image_name, const 
 ImageSource *src,
       if (align_size != reserve_size) {
           abi_ulong align_addr = ROUND_UP(load_addr, align);
-        abi_ulong align_end = align_addr + reserve_size;
+        abi_ulong align_end = TARGET_PAGE_ALIGN(align_addr + reserve_size);
           abi_ulong load_end = load_addr + align_size;

Both align_end and load_end must be aligned.

Bah, hit ctrl-enter, not enter. I'll fix and queue.

r~

#3 lore raw
In reply to Fabiano Rosas
Quoted 10 lines · Fabiano Rosas

When complying with the alignment requested in the ELF and unmapping the excess reservation, having align_end not aligned to the guest page causes the unmap to be rejected by the alignment check at target_munmap and later brk adjustments hit an EEXIST.

Fix by aligning the start of region to be unmapped.

Fixes c81d1fafa6 ("linux-user: Honor elf alignment when placing images")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1913

Is this a qemu-stable material? That issue was quite hot..

Thanks,

/mjt

#4 lore raw
Quoted 6 lines · Michael Tokarev
Quoted 5 lines · Fabiano Rosas

When complying with the alignment requested in the ELF and unmapping the excess reservation, having align_end not aligned to the guest page causes the unmap to be rejected by the alignment check at target_munmap and later brk adjustments hit an EEXIST.

Quoted 7 lines
Quoted 5 lines

Fix by aligning the start of region to be unmapped.

Fixes c81d1fafa6 ("linux-user: Honor elf alignment when placing images")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1913

Is this a qemu-stable material? That issue was quite hot..

Yes, I think it's good for stable.

Quoted 4 lines

Thanks,

/mjt