diff mbox series

[RFC,v2] linux-user: Use FD_32BIT_MODE fd flag for 32-bit guests

Message ID 20200623100101.6041-1-peter.maydell@linaro.org
State Superseded
Headers show
Series [RFC,v2] linux-user: Use FD_32BIT_MODE fd flag for 32-bit guests | expand

Commit Message

Peter Maydell June 23, 2020, 10:01 a.m. UTC
If the guest is 32 bit then there is a potential problem if the
host gives us back a 64-bit sized value that we can't fit into
the ABI the guest requires. This is a theoretical issue for many
syscalls, but a real issue for directory reads where the host
is using ext3 or ext4. There the 'offset' values retured via
the getdents syscall are hashes, and on a 64-bit system they
will always fill the full 64 bits.

Use the FD_32BIT_MODE fd flag to tell the kernel to stick
to 32-bit sized hashes for fds used by the guest.

This is an RFC patch because the kernel patch that adds the
new fd flag hasn't yet been accepted upstream. The kernel
patch is:
 https://patchew.org/QEMU/20200529072017.2906-1-linus.walleij@linaro.org/

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

---
This is just a minor update of the testing patch I sent out in the
thread discussing Linus's v1 kernel patch.

 linux-user/syscall.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

-- 
2.20.1

Comments

Laurent Vivier June 23, 2020, 2:57 p.m. UTC | #1
Le 23/06/2020 à 12:01, Peter Maydell a écrit :
> If the guest is 32 bit then there is a potential problem if the

> host gives us back a 64-bit sized value that we can't fit into

> the ABI the guest requires. This is a theoretical issue for many

> syscalls, but a real issue for directory reads where the host

> is using ext3 or ext4. There the 'offset' values retured via

> the getdents syscall are hashes, and on a 64-bit system they

> will always fill the full 64 bits.

> 

> Use the FD_32BIT_MODE fd flag to tell the kernel to stick

> to 32-bit sized hashes for fds used by the guest.

> 

> This is an RFC patch because the kernel patch that adds the

> new fd flag hasn't yet been accepted upstream. The kernel

> patch is:

>  https://patchew.org/QEMU/20200529072017.2906-1-linus.walleij@linaro.org/

> 

> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

> ---

> This is just a minor update of the testing patch I sent out in the

> thread discussing Linus's v1 kernel patch.

> 

>  linux-user/syscall.c | 32 ++++++++++++++++++++++++++++++++

>  1 file changed, 32 insertions(+)

> 


Reviewed-by: Laurent Vivier <laurent@vivier.eu>
diff mbox series

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 97de9fb5c92..265b9a096a0 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -884,6 +884,33 @@  static inline int host_to_target_sock_type(int host_type)
     return target_type;
 }
 
+/*
+ * If the guest is using a 32 bit ABI then we should try to ask the kernel
+ * to provide 32-bit offsets in getdents syscalls, as otherwise some
+ * filesystems will return 64-bit hash values which we can't fit into
+ * the field sizes the guest ABI mandates.
+ */
+#ifndef FD_32BIT_MODE
+#define FD_32BIT_MODE 2
+#endif
+
+static inline void request_32bit_fs(int fd)
+{
+#if HOST_LONG_BITS > TARGET_ABI_BITS
+    /*
+     * Ignore errors, which are likely due to the host kernel being
+     * too old to support FD_32BIT_MODE. We'll continue anyway, which
+     * might or might not work, depending on the guest code and on the
+     * host filesystem.
+     */
+    int flags = fcntl(fd, F_GETFD);
+    if (flags == -1) {
+        return;
+    }
+    fcntl(fd, F_SETFD, flags | FD_32BIT_MODE);
+#endif
+}
+
 static abi_ulong target_brk;
 static abi_ulong target_original_brk;
 static abi_ulong brk_page;
@@ -7725,6 +7752,7 @@  static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                                   target_to_host_bitmask(arg2, fcntl_flags_tbl),
                                   arg3));
         fd_trans_unregister(ret);
+        request_32bit_fs(ret);
         unlock_user(p, arg1, 0);
         return ret;
 #endif
@@ -7735,6 +7763,7 @@  static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                                   target_to_host_bitmask(arg3, fcntl_flags_tbl),
                                   arg4));
         fd_trans_unregister(ret);
+        request_32bit_fs(ret);
         unlock_user(p, arg2, 0);
         return ret;
 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
@@ -7746,6 +7775,7 @@  static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_open_by_handle_at:
         ret = do_open_by_handle_at(arg1, arg2, arg3);
         fd_trans_unregister(ret);
+        request_32bit_fs(ret);
         return ret;
 #endif
     case TARGET_NR_close:
@@ -7790,6 +7820,7 @@  static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
             return -TARGET_EFAULT;
         ret = get_errno(creat(p, arg2));
         fd_trans_unregister(ret);
+        request_32bit_fs(ret);
         unlock_user(p, arg1, 0);
         return ret;
 #endif
@@ -12419,6 +12450,7 @@  static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         }
         ret = get_errno(memfd_create(p, arg2));
         fd_trans_unregister(ret);
+        request_32bit_fs(ret);
         unlock_user(p, arg1, 0);
         return ret;
 #endif