From patchwork Tue Oct 8 18:51:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 833616 Delivered-To: patch@linaro.org Received: by 2002:adf:a1d9:0:b0:367:895a:4699 with SMTP id v25csp450510wrv; Tue, 8 Oct 2024 11:54:16 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUv0Dqq5yK7kW2nJrnJZIGuapJ5RHCjgk1tVYb/zjZAB0Cxq7qc2jWaNgelm/GDSJAaD11Mmg==@linaro.org X-Google-Smtp-Source: AGHT+IER/kmEN11ZRCoPbRuX8NInoPprQhKLS2pU2M59kVDiv2zrwUt+04FEm4w/KxUFmIyZl4G4 X-Received: by 2002:a05:620a:1910:b0:7a9:c160:c80b with SMTP id af79cd13be357-7ae6f432b34mr2683170485a.8.1728413655985; Tue, 08 Oct 2024 11:54:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1728413655; cv=none; d=google.com; s=arc-20240605; b=T7GNnZZ1x4H4J/LyyFVJp55cd3Mv6NFdxAvW3hHDwXnACwlW+gJs+U7CWyFgjrfWi1 I1Vkuv7tIlnjGkOIJ+w0VhOTVQsySTaSBURB3JmeB12GKdKQ7DNvFiuPAoiPEifY86ro Nt27hjXCjY6rGenOrZCfYD/tVyo1wJtFeDGODUI/SjnRH9wpsnd4Cqtgll6qS2gEyJqt S1c6stYs86SDGO0MgUYXfzI5SULywZZ39b7gbOJ1siV5rf8yTQ+D8AIhdrR02Rp/tWZ7 FuUkH8lIf0LAYoAd8vLkETo7qG4nsLZJ2bUgsbNX945f5DA6iEvapB0qufeY+hvuzx/T Nwwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8nnulTAtUcmZmZfuGcf5eSLVvnbBtACxcyEvOVNzWDI=; fh=bfzdhCcU3ZfQ17YMkDaytQs6HssTokv83KlNoAueAKU=; b=FClY39rMCRtv9N8nzZEQvW2/GPztzTauClRTl7LJ5Jp21rBk/HZ6HP+gJy/NGtKR12 3J4dicEP5byOOQv/HAINu6aglTv2lFvrINMPk+hEnK5Z4OSdxEVlLUB+hmA0KXYON5aX SvaRKvDFVpbMxCVuu8oDWmiVblcAUfNS//aXAkcRJOAfMmXVkIhjP0kNWZTQh0BvJLCP Xs404gzrIfakwS+qasCYh1DaYbuL1TWM4iyESTvdwuJ1I+a1i2e7VYl1jruJfFzPYelt xB3gZks6YhqDmrXnRrArfBJvT9Ofu7RB9R/icIUXqfhqu5856rj1X+JV93TsTTF8rdoC bgXA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HOWkmtFX; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7ae75764cd9si902301185a.455.2024.10.08.11.54.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Oct 2024 11:54:15 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HOWkmtFX; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1syFJb-0007Tx-0Z; Tue, 08 Oct 2024 14:52:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1syFJP-0007Qn-OG for qemu-devel@nongnu.org; Tue, 08 Oct 2024 14:52:00 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1syFJK-0002SM-32 for qemu-devel@nongnu.org; Tue, 08 Oct 2024 14:51:57 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-71e0cd1f3b6so1567576b3a.0 for ; Tue, 08 Oct 2024 11:51:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1728413507; x=1729018307; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8nnulTAtUcmZmZfuGcf5eSLVvnbBtACxcyEvOVNzWDI=; b=HOWkmtFX5bWk+yQqu2HuS8tpUHsY0ETwB274GglsmoYDdncRCxTPc5Rczi6yVhATzE ARdxfVWmjC0za36gseFT7aB05hhXxH4r2IdvC3vnT8yjnelVxvNCXJoClLF+FxwlBpk2 3aJDpLVBYDQhkMmj6kS4XjJ16j3EqSbyN9YQxvzeZok3F3CATnXxEYgX3B6SZQjHr+oN YvHrynosqkD1J1zI/ugYuBtJdd05E4xO/rOlVwUGjypwQjG3rLeFY+RaBwBWSzWYnNdc 3JC5Y5VLS3WVmok75veLcc8KIjMfokxrmSRhodYqouW5r2Qd3MR90JtBuZelTqWRGDid gPgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728413507; x=1729018307; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8nnulTAtUcmZmZfuGcf5eSLVvnbBtACxcyEvOVNzWDI=; b=suaXGOox/eZ+P7AbiMxWlxLcD6czEmNmS/7zhkZ2qCyMdMm6V7MA6bYcwqZwSSYs1j XSm88zGj+VCVAvArMWikVsSU+6uTPkGWgh5ZfVr+Rlr7rNAlOcBNuHpLKP6TsT+x2bZH BpfdFVERe+Zpi0DsyAkxQ0bDCaqCAo3OdztDUenLx+xSxwFI6KXuWMId4eoeGC1yRJel 55lCHHS7/WplD0zOGwou1oInQa/K0nExuK6zGPu2+9xhkZDgqtpbMlo6CkPt/23l0OPY Wa228rDz1pPRVIrj8FjkwSjpFDmoZVP26HHviQnd2IjNz9W3x0J1qlNeLH/fA/vXGuTz bUwA== X-Gm-Message-State: AOJu0Yw4b8xD1yKVtce+dkFp1FF/d6FEQxTUr2os5iTkoOQwoVYX4QbH Zuo/naQGjKLR9uAsGWl54dHIdLfmnRxDDD96tf3Rm/AWCxoHAd6xGFK21voVdPK54ZKODP2MSA8 3 X-Received: by 2002:a05:6a21:1192:b0:1d8:a354:1acd with SMTP id adf61e73a8af0-1d8a35421f0mr141040637.25.1728413507435; Tue, 08 Oct 2024 11:51:47 -0700 (PDT) Received: from stoup.. (174-21-81-121.tukw.qwest.net. [174.21.81.121]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7e9f6821c32sm7186818a12.33.2024.10.08.11.51.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 11:51:46 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Michael Vogt , Laurent Vivier Subject: [PULL 04/14] linux-user: add strace support for openat2 Date: Tue, 8 Oct 2024 11:51:31 -0700 Message-ID: <20241008185141.20057-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241008185141.20057-1-richard.henderson@linaro.org> References: <20241008185141.20057-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42d; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Michael Vogt This commit adds support for the `openat2()` to `QEMU_STRACE`. It will use the `openat2.h` header if available to create user readable flags for the `resolve` argument but does not require the header otherwise. It also makes `copy_struct_from_user()` available via `qemu.h` and `open_how_ver0` via `syscall_defs.h` so that strace.c can use them. Signed-off-by: Michael Vogt Reviewed-by: Laurent Vivier Message-ID: [rth: Add braces around the expanded how structure, like strace(3)] Signed-off-by: Richard Henderson --- linux-user/qemu.h | 9 ++++++++ linux-user/syscall_defs.h | 5 +++++ linux-user/strace.c | 47 +++++++++++++++++++++++++++++++++++++++ linux-user/syscall.c | 8 +------ linux-user/strace.list | 3 +++ meson.build | 1 + 6 files changed, 66 insertions(+), 7 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 2e90a97175..98ad848ab2 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -313,6 +313,15 @@ static inline bool access_ok(CPUState *cpu, int type, int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len); int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len); +/* + * copy_struct_from_user() 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); + /* Functions for accessing guest memory. The tget and tput functions read/write single values, byteswapping as necessary. The lock_user function gets a pointer to a contiguous area of guest memory, but does not perform diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h index de5091c977..0ade83745e 100644 --- a/linux-user/syscall_defs.h +++ b/linux-user/syscall_defs.h @@ -2749,6 +2749,11 @@ struct target_sched_param { }; /* from kernel's include/uapi/linux/openat2.h */ +struct open_how_ver0 { + __u64 flags; + __u64 mode; + __u64 resolve; +}; struct target_open_how_ver0 { abi_ullong flags; abi_ullong mode; diff --git a/linux-user/strace.c b/linux-user/strace.c index b4d1098170..d3cdd09dc1 100644 --- a/linux-user/strace.c +++ b/linux-user/strace.c @@ -13,6 +13,9 @@ #include #include #include +#ifdef HAVE_OPENAT2_H +#include +#endif #include #include "qemu.h" #include "user-internals.h" @@ -1063,6 +1066,18 @@ UNUSED static const struct flags open_flags[] = { FLAG_END, }; +UNUSED static const struct flags openat2_resolve_flags[] = { +#ifdef HAVE_OPENAT2_H + FLAG_GENERIC(RESOLVE_NO_XDEV), + FLAG_GENERIC(RESOLVE_NO_MAGICLINKS), + FLAG_GENERIC(RESOLVE_NO_SYMLINKS), + FLAG_GENERIC(RESOLVE_BENEATH), + FLAG_GENERIC(RESOLVE_IN_ROOT), + FLAG_GENERIC(RESOLVE_CACHED), +#endif + FLAG_END, +}; + UNUSED static const struct flags mount_flags[] = { #ifdef MS_BIND FLAG_GENERIC(MS_BIND), @@ -3483,6 +3498,38 @@ print_openat(CPUArchState *cpu_env, const struct syscallname *name, } #endif +#ifdef TARGET_NR_openat2 +static void +print_openat2(CPUArchState *cpu_env, const struct syscallname *name, + abi_long arg0, abi_long arg1, abi_long arg2, + abi_long arg3, abi_long arg4, abi_long arg5) +{ + struct open_how_ver0 how; + + print_syscall_prologue(name); + print_at_dirfd(arg0, 0); + print_string(arg1, 0); + + if ((abi_ulong)arg3 >= sizeof(struct target_open_how_ver0) && + copy_struct_from_user(&how, sizeof(how), arg2, arg3) == 0) { + how.flags = tswap64(how.flags); + how.mode = tswap64(how.mode); + how.resolve = tswap64(how.resolve); + qemu_log("{"); + print_open_flags(how.flags, 0); + if (how.flags & TARGET_O_CREAT) { + print_file_mode(how.mode, 0); + } + print_flags(openat2_resolve_flags, how.resolve, 1); + qemu_log("},"); + } else { + print_pointer(arg2, 0); + } + print_raw_param(TARGET_ABI_FMT_lu, arg3, 1); + print_syscall_epilogue(name); +} +#endif + #ifdef TARGET_NR_pidfd_send_signal static void print_pidfd_send_signal(CPUArchState *cpu_env, const struct syscallname *name, diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 2febc3bc3f..1354e75694 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -608,8 +608,7 @@ static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize) * * Similar to kernels uaccess.h:copy_struct_from_user() */ -static int -copy_struct_from_user(void *dst, size_t ksize, abi_ptr src, size_t usize) +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; @@ -682,11 +681,6 @@ 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) -struct open_how_ver0 { - __u64 flags; - __u64 mode; - __u64 resolve; -}; safe_syscall4(int, openat2, int, dirfd, const char *, pathname, \ const struct open_how_ver0 *, how, size_t, size) diff --git a/linux-user/strace.list b/linux-user/strace.list index dfd4237d14..ef658224fc 100644 --- a/linux-user/strace.list +++ b/linux-user/strace.list @@ -715,6 +715,9 @@ #ifdef TARGET_NR_openat { TARGET_NR_openat, "openat" , NULL, print_openat, NULL }, #endif +#ifdef TARGET_NR_openat2 +{ TARGET_NR_openat2, "openat2" , NULL, print_openat2, NULL }, +#endif #ifdef TARGET_NR_osf_adjtime { TARGET_NR_osf_adjtime, "osf_adjtime" , NULL, NULL, NULL }, #endif diff --git a/meson.build b/meson.build index 33954b3eba..4ea1984fc5 100644 --- a/meson.build +++ b/meson.build @@ -2481,6 +2481,7 @@ config_host_data.set('CONFIG_LINUX_MAGIC_H', cc.has_header('linux/magic.h')) config_host_data.set('CONFIG_VALGRIND_H', cc.has_header('valgrind/valgrind.h')) config_host_data.set('HAVE_BTRFS_H', cc.has_header('linux/btrfs.h')) config_host_data.set('HAVE_DRM_H', cc.has_header('libdrm/drm.h')) +config_host_data.set('HAVE_OPENAT2_H', cc.has_header('linux/openat2.h')) config_host_data.set('HAVE_PTY_H', cc.has_header('pty.h')) config_host_data.set('HAVE_SYS_DISK_H', cc.has_header('sys/disk.h')) config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h'))