Skip to contentSkip to conversation

{linux, bsd}-user: Fail mmap() if size doesn't fit into host's size_t

PATCH In review Ilya Leoshkevich 3 participants 3 messages Jan 25, 2024 – Jan 29, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 {linux, bsd}-user: Fail mmap() if size doesn't fit into host's size_t

s390x's branch-relative-long test fails with the following error message on 32-bit hosts:

    qemu-s390x: ../accel/tcg/user-exec.c:493: page_set_flags: Assertion `last <= GUEST_ADDR_MAX' failed.

The root cause is that the size passed to mmap() by this test does not fit into 32 bits and gets truncated. Since there is no chance for such mmap() to succeed, detect this condition and fail the mmap() right away.

---
 bsd-user/mmap.c   | 4 ++++
 linux-user/mmap.c | 4 ++++
 2 files changed, 8 insertions(+)
bsd-user/mmap.c +4 -0
index 3ef11b28079..5dc327d0ad3 100644
@@ -256,6 +256,10 @@ static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
 
     size = HOST_PAGE_ALIGN(size);
 
+    if (size != (size_t)size) {
+        return (abi_ulong)(-1);
+    }
+
     if (reserved_va) {
         return mmap_find_vma_reserved(start, size,
             (alignment != 0 ? 1 << alignment :
linux-user/mmap.c +4 -0
index 96c9433e271..ae59d70fb67 100644
@@ -389,6 +389,10 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align)
 
     size = HOST_PAGE_ALIGN(size);
 
+    if (size != (size_t)size) {
+        return (abi_ulong)(-1);
+    }
+
     if (reserved_va) {
         return mmap_find_vma_reserved(start, size, align);
     }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Re: [PATCH] {linux,bsd}-user: Fail mmap() if size doesn't fit into host's size_t

Quoted 20 lines · Ilya Leoshkevich

s390x's branch-relative-long test fails with the following error message on 32-bit hosts:

     qemu-s390x: ../accel/tcg/user-exec.c:493: page_set_flags: Assertion `last <= GUEST_ADDR_MAX' failed.

The root cause is that the size passed to mmap() by this test does not fit into 32 bits and gets truncated. Since there is no chance for such mmap() to succeed, detect this condition and fail the mmap() right away.

---
  bsd-user/mmap.c   | 4 ++++
  linux-user/mmap.c | 4 ++++
  2 files changed, 8 insertions(+)
bsd-user/mmap.c +0 -0
index 3ef11b28079..5dc327d0ad3 100644

bsd-user/mmap.c static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size, · line 262

Quoted 7 lines
bsd-user/mmap.c +4 -0
@@ -256,6 +256,10 @@ static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
 
      size = HOST_PAGE_ALIGN(size);
 
+    if (size != (size_t)size) {
+        return (abi_ulong)(-1);
+    }
+

I have this same fix in

https://lore.kernel.org/qemu-devel/[email protected]/

so as far as that's concerned,

But perhaps you got cast your eye across the larger reorg,

https://lore.kernel.org/qemu-devel/[email protected]/

?

r~

Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH] {linux,bsd}-user: Fail mmap() if size doesn't fit into host's size_t

In reply to Ilya Leoshkevich
Quoted 31 lines · Ilya Leoshkevich

s390x's branch-relative-long test fails with the following error message on 32-bit hosts:

    qemu-s390x: ../accel/tcg/user-exec.c:493: page_set_flags: Assertion

`last <= GUEST_ADDR_MAX' failed.

The root cause is that the size passed to mmap() by this test does not fit into 32 bits and gets truncated. Since there is no chance for such mmap() to succeed, detect this condition and fail the mmap() right away.

---
 bsd-user/mmap.c   | 4 ++++
 linux-user/mmap.c | 4 ++++
 2 files changed, 8 insertions(+)
bsd-user/mmap.c +4 -0
index 3ef11b28079..5dc327d0ad3 100644
@@ -256,6 +256,10 @@ static abi_ulong mmap_find_vma_aligned(abi_ulong
 start, abi_ulong size,
 
     size = HOST_PAGE_ALIGN(size);
 
+    if (size != (size_t)size) {
+        return (abi_ulong)(-1);
+    }
+
     if (reserved_va) {
         return mmap_find_vma_reserved(start, size,
Quoted 2 lines
             (alignment != 0 ? 1 << alignment :

Seems good to me.. I can queue it to this month's landing code, unless Richard beats me to it.

Warner