Skip to contentSkip to conversation

linux-user: Allow 64-bit offsets in mmap2() syscall on 32-bit targets

PATCH New Helge Deller 1 participant 1 message Dec 22, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Allow 64-bit offsets in mmap2() syscall on 32-bit targets

The mmap2() syscall allows 32-bit guests to specify the offset into a mapping in 4096-byte units (instead of bytes, as is done by mmap(2)). This enables applications that use a 32-bit off_t to map large files (up to 2^44 bytes).

So when emulating the mmap2 syscall the offset parameter is shifted by 12 bits, which then may exceed what the abi_ulong (32-bit) type of the offset parameter in target_mmap() can hold.

Overcome this possible issue by switching hard to uint64_t in target_mmap() and mmap_frag().

Note: compile-tested only.

---
 linux-user/mmap.c      | 9 +++++----
 linux-user/syscall.c   | 2 +-
 linux-user/user-mmap.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
linux-user/mmap.c +5 -4
index c75342108c..43fbd8d957 100644
@@ -192,7 +192,7 @@ error:
 /* map an incomplete host page */
 static int mmap_frag(abi_ulong real_start,
                      abi_ulong start, abi_ulong end,
-                     int prot, int flags, int fd, abi_ulong offset)
+                     int prot, int flags, int fd, uint64_t offset)
 {
     abi_ulong real_end, addr;
     void *host_start;
@@ -430,9 +430,10 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align)
 
 /* NOTE: all the constants are the HOST ones */
 abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
-                     int flags, int fd, abi_ulong offset)
+                     int flags, int fd, uint64_t offset)
 {
-    abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len,
+    uint64_t host_offset;
+    abi_ulong ret, end, real_start, real_end, retaddr, host_len,
               passthrough_start = -1, passthrough_end = -1;
     int page_flags, host_prot;
 
@@ -621,7 +622,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
         /* map the middle (easier) */
         if (real_start < real_end) {
             void *p;
-            unsigned long offset1;
+            uint64_t offset1;
             if (flags & MAP_ANONYMOUS)
                 offset1 = 0;
             else
linux-user/syscall.c +1 -1
index 52693b4239..4fee882cd7 100644
@@ -10144,7 +10144,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
 #endif
         ret = target_mmap(arg1, arg2, arg3,
                           target_to_host_bitmask(arg4, mmap_flags_tbl),
-                          arg5, arg6 << MMAP_SHIFT);
+                          arg5, ((uint64_t)arg6) << MMAP_SHIFT);
         return get_errno(ret);
 #endif
     case TARGET_NR_munmap:
linux-user/user-mmap.h +1 -1
index 480ce1c114..0aed3df2c4 100644
@@ -20,7 +20,7 @@
 
 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
-                     int flags, int fd, abi_ulong offset);
+                     int flags, int fd, uint64_t offset);
 int target_munmap(abi_ulong start, abi_ulong len);
 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
                        abi_ulong new_size, unsigned long flags,

--
2.38.1