summary refs log tree commit diff stats
path: root/linux-user/syscall.c
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2024-10-09 15:06:42 +0100
committerPeter Maydell <peter.maydell@linaro.org>2024-10-09 15:06:42 +0100
commit54cdddc6dc29e557de33138fc0d89e73510c8487 (patch)
tree545b1c7f95928c3aaa570dbf30ee8c43f4b62c40 /linux-user/syscall.c
parentc9beb029b5bd8cc028411db2270995f5e0dc3567 (diff)
parent25f4e71722417db1f7d5140847849197053b23dd (diff)
downloadfocaccia-qemu-54cdddc6dc29e557de33138fc0d89e73510c8487.tar.gz
focaccia-qemu-54cdddc6dc29e557de33138fc0d89e73510c8487.zip
Merge tag 'pull-tcg-20241008' of https://gitlab.com/rth7680/qemu into staging
linux-user: Fix parse_elf_properties GNU0_MAGIC check
linux-user: Various improvements to strace
linux-user: Add openat2 support
linux-user/flatload: Take mmap_lock in load_flt_binary()
accel/tcg: Make page_set_flags() documentation public
tcg/ppc: Use TCG_REG_TMP2 for scratch tcg_out_qemu_st
tcg/ppc: Use TCG_REG_TMP2 for scratch index in prepare_host_addr
target/m68k: Always return a temporary from gen_lea_mode

# -----BEGIN PGP SIGNATURE-----
#
# iQFRBAABCgA7FiEEekgeeIaLTbaoWgXAZN846K9+IV8FAmcFeLQdHHJpY2hhcmQu
# aGVuZGVyc29uQGxpbmFyby5vcmcACgkQZN846K9+IV+8Wgf9FJb/71zPAg7idUo8
# k+P5hOmE0PzPKdkoCh5WP0+TF4m/gMc3ABMFO2ABx4EcAkWxUmNP1Po5Lb0/A9Gq
# LuSDRJVk3jSeGebmMsIH0SVzKY3ewQv7ZGB6A2uFdXtkwDUCxgHUQcfQY9yvjFut
# eU11zWkPbmbI4bwz9Y+Tyv9LOXRk/Br5B/uQEzQ9/Go/0OeJP0iTXre/veGufqND
# wK35AoDCVNomjw6h2Fg0t3kCt/mtR8W8D16+4QhsH+iE9IQMmhj4itz8Uq2ikh29
# lQIUT8acHC8fNUQJXw0nPXbNsimp/GyRGJiYCH7g7V67o/fjDUSbYMxk2TMZPuJV
# dGy3KQ==
# =UeX8
# -----END PGP SIGNATURE-----
# gpg: Signature made Tue 08 Oct 2024 19:23:48 BST
# gpg:                using RSA key 7A481E78868B4DB6A85A05C064DF38E8AF7E215F
# gpg:                issuer "richard.henderson@linaro.org"
# gpg: Good signature from "Richard Henderson <richard.henderson@linaro.org>" [full]
# Primary key fingerprint: 7A48 1E78 868B 4DB6 A85A  05C0 64DF 38E8 AF7E 215F

* tag 'pull-tcg-20241008' of https://gitlab.com/rth7680/qemu:
  accel/tcg: Make page_set_flags() documentation public
  target/m68k: Always return a temporary from gen_lea_mode
  tcg/ppc: Use TCG_REG_TMP2 for scratch index in prepare_host_addr
  tcg/ppc: Use TCG_REG_TMP2 for scratch tcg_out_qemu_st
  linux-user: Add strace for recvfrom()
  linux-user: Add strace for sendto()
  linux-user: Factor print_buf_len() out
  linux-user: Display sockaddr buffer as pointer
  linux-user: Correct print_sockaddr() format
  linux-user: Trace wait4()'s and waitpid()'s wstatus
  linux-user: add strace support for openat2
  linux-user: add openat2 support in linux-user
  linux-user: Fix parse_elf_properties GNU0_MAGIC check
  linux-user/flatload: Take mmap_lock in load_flt_binary()

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'linux-user/syscall.c')
-rw-r--r--linux-user/syscall.c99
1 files changed, 97 insertions, 2 deletions
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index a666986189..1354e75694 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -602,6 +602,33 @@ static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
     return 1;
 }
 
+/*
+ * Copies a target struct to a host struct, in a way that guarantees
+ * backwards-compatibility for struct syscall arguments.
+ *
+ * Similar to kernels uaccess.h:copy_struct_from_user()
+ */
+int copy_struct_from_user(void *dst, size_t ksize, abi_ptr src, size_t usize)
+{
+    size_t size = MIN(ksize, usize);
+    size_t rest = MAX(ksize, usize) - size;
+
+    /* Deal with trailing bytes. */
+    if (usize < ksize) {
+        memset(dst + size, 0, rest);
+    } else if (usize > ksize) {
+        int ret = check_zeroed_user(src, ksize, usize);
+        if (ret <= 0) {
+            return ret ?: -TARGET_E2BIG;
+        }
+    }
+    /* Copy the interoperable parts of the struct. */
+    if (copy_from_user(dst, src, size)) {
+        return -TARGET_EFAULT;
+    }
+    return 0;
+}
+
 #define safe_syscall0(type, name) \
 static type safe_##name(void) \
 { \
@@ -653,6 +680,10 @@ safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
 safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
               int, flags, mode_t, mode)
+
+safe_syscall4(int, openat2, int, dirfd, const char *, pathname, \
+              const struct open_how_ver0 *, how, size_t, size)
+
 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
 safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
               struct rusage *, rusage)
@@ -8332,8 +8363,9 @@ static int open_net_route(CPUArchState *cpu_env, int fd)
 }
 #endif
 
-int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
-                    int flags, mode_t mode, bool safe)
+static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd,
+                              const char *fname, int flags, mode_t mode,
+                              int openat2_resolve, bool safe)
 {
     g_autofree char *proc_name = NULL;
     const char *pathname;
@@ -8370,6 +8402,12 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
     }
 
     if (is_proc_myself(pathname, "exe")) {
+        /* Honor openat2 resolve flags */
+        if ((openat2_resolve & RESOLVE_NO_MAGICLINKS) ||
+            (openat2_resolve & RESOLVE_NO_SYMLINKS)) {
+            errno = ELOOP;
+            return -1;
+        }
         if (safe) {
             return safe_openat(dirfd, exec_path, flags, mode);
         } else {
@@ -8416,6 +8454,17 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
         return fd;
     }
 
+    return -2;
+}
+
+int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
+                    int flags, mode_t mode, bool safe)
+{
+    int fd = maybe_do_fake_open(cpu_env, dirfd, pathname, flags, mode, 0, safe);
+    if (fd > -2) {
+        return fd;
+    }
+
     if (safe) {
         return safe_openat(dirfd, path(pathname), flags, mode);
     } else {
@@ -8423,6 +8472,49 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
     }
 }
 
+
+static int do_openat2(CPUArchState *cpu_env, abi_long dirfd,
+                      abi_ptr guest_pathname, abi_ptr guest_open_how,
+                      abi_ulong guest_size)
+{
+    struct open_how_ver0 how = {0};
+    char *pathname;
+    int ret;
+
+    if (guest_size < sizeof(struct target_open_how_ver0)) {
+        return -TARGET_EINVAL;
+    }
+    ret = copy_struct_from_user(&how, sizeof(how), guest_open_how, guest_size);
+    if (ret) {
+        if (ret == -TARGET_E2BIG) {
+            qemu_log_mask(LOG_UNIMP,
+                          "Unimplemented openat2 open_how size: "
+                          TARGET_ABI_FMT_lu "\n", guest_size);
+        }
+        return ret;
+    }
+    pathname = lock_user_string(guest_pathname);
+    if (!pathname) {
+        return -TARGET_EFAULT;
+    }
+
+    how.flags = target_to_host_bitmask(tswap64(how.flags), fcntl_flags_tbl);
+    how.mode = tswap64(how.mode);
+    how.resolve = tswap64(how.resolve);
+    int fd = maybe_do_fake_open(cpu_env, dirfd, pathname, how.flags, how.mode,
+                                how.resolve, true);
+    if (fd > -2) {
+        ret = get_errno(fd);
+    } else {
+        ret = get_errno(safe_openat2(dirfd, pathname, &how,
+                                     sizeof(struct open_how_ver0)));
+    }
+
+    fd_trans_unregister(ret);
+    unlock_user(pathname, guest_pathname, 0);
+    return ret;
+}
+
 ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
 {
     ssize_t ret;
@@ -9195,6 +9287,9 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
         fd_trans_unregister(ret);
         unlock_user(p, arg2, 0);
         return ret;
+    case TARGET_NR_openat2:
+        ret = do_openat2(cpu_env, arg1, arg2, arg3, arg4);
+        return ret;
 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
     case TARGET_NR_name_to_handle_at:
         ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);