From nobody Sun Dec 22 05:15:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1727795765; cv=none; d=zohomail.com; s=zohoarc; b=GdfuaOg2apGrCQDID0H/ZhGw6ofj+OmiFFKIkzDrqA+9mqrWfH4O+W4QoBy8Z/5QScuah1Qg+uaFRVnyMXAiN2hMxLOKChpMHf9Ulj270iY9/haMjihgRxyjJuz94yuz44m/S3FSpIBC0p5D9e6MaLIgESzJXxeSnnrEW8NMIY4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727795765; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KRiyw0at5DGRgBVyJeLHnY8AwyrMo6ngg550Y6llmjo=; b=JScxeZKxlrWZ1vBqrcUWggfNgdk3tGZxxnVawOL3Mg09jJ/1b2C4ln5vAWnQp8ZkEpJiESq5Ch/vmR/7ZXR6TybRQl7kGtdCn7gMMSmw8mZsGFmXC8oTrXQBwhm9YPSo7oDDC6z9Vn1FZPcFOyGqHpLD/rQG7Urm+lFZGPOSWik= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 172779576512659.929243934562464; Tue, 1 Oct 2024 08:16:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sveal-0005rf-Tk; Tue, 01 Oct 2024 11:15: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 1sveaj-0005pB-Pj for qemu-devel@nongnu.org; Tue, 01 Oct 2024 11:15:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sveah-00030x-GF for qemu-devel@nongnu.org; Tue, 01 Oct 2024 11:15:09 -0400 Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-434-dmlzA0EYNfWf25aLYbHFUg-1; Tue, 01 Oct 2024 11:15:05 -0400 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a8a92ab4cdbso433823666b.0 for ; Tue, 01 Oct 2024 08:15:04 -0700 (PDT) Received: from top.fritz.box (p4fdf9fd8.dip0.t-ipconnect.de. [79.223.159.216]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a93c2775d23sm732438166b.4.2024.10.01.08.15.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 08:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727795706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KRiyw0at5DGRgBVyJeLHnY8AwyrMo6ngg550Y6llmjo=; b=ethcAxULcxDTREgBtctSCUMq9Smb8IS/rqGBVQygSGt5eCaYk2tR2WfsSLa10CURIdClNW ive+5OIpO8xkgJrerV5M1hI9fN425AcFHCqhjLSlIwoJUwLAx6MpTb8kQPJSWhj98SbXQt fbkLz5dACfz9KvGVyqJydbnNdxlhkco= X-MC-Unique: dmlzA0EYNfWf25aLYbHFUg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727795703; x=1728400503; 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=KRiyw0at5DGRgBVyJeLHnY8AwyrMo6ngg550Y6llmjo=; b=QWfhhOTJE7v1P3wobNaVMqZpLzcBBGCv3JGJitXSzdjQ8iAfKjzNniLKVFVry/pqDU BQTCnlO5OMRcG9hGi2e+ZFvw0wLnztenLg0QMjWrMMYhUF7WfEfj/9v03M+NM/4SCazU l2RZK1jdeOa/n+UJeh/b0Gmr/romKvvkgLqZ5aQf8J4fZ1tQY4Xy9ZBZhWEQrFerJ62d mTU4cPZJRULTbe2ZtbJut30NzfkPhcFBvmaQJKlYdC8WbvD59f1kYzVci8IzRsRSgjou jfbY6qciM6eyia9c/UpEcV2cSnXBa2/fBtczx2vCCR3O/c445SKK4zINPX/x8CIkHQcz 4juQ== X-Gm-Message-State: AOJu0Yyq0ycpaM3zRBak08AFPTsPkSZk/ptmDGvVYGxdkRUbvjcGMfZd 67WpltUAY4iCKEsrIkbeovcxMJQK5fcCGh3jemmESFnuy3D6fqtm4f1B0jqJLahIZ/GqL3FPkF3 PmITBsq4I5BT17npZUMVTBsTeO/Lbx2LcCF0UQR32+nXetqnxEvwFxt+6km+gy7WMS9DgqbR+wd cuKcYro/pIwF4XWZ97/3+scGrn2Ytg5Z/c X-Received: by 2002:a17:907:6ea0:b0:a8a:85af:7ae8 with SMTP id a640c23a62f3a-a93c48e95e2mr2038445766b.11.1727795703445; Tue, 01 Oct 2024 08:15:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH51X9A3rjOhvPRWXYKQjlNm0hLalqz3h/MK8zbLDjG9LbvohJfluOzxt07+lvwC6+oNNe+mw== X-Received: by 2002:a17:907:6ea0:b0:a8a:85af:7ae8 with SMTP id a640c23a62f3a-a93c48e95e2mr2038441866b.11.1727795702985; Tue, 01 Oct 2024 08:15:02 -0700 (PDT) From: Michael Vogt To: qemu-devel@nongnu.org Cc: Michael Vogt , Richard Henderson , Laurent Vivier , Michael Vogt Subject: [PATCH v9 1/2] linux-user: add openat2 support in linux-user Date: Tue, 1 Oct 2024 17:14:53 +0200 Message-ID: <1c2c8c9db3731ed4c6fd9b10c63637c3e4caf8f5.1727795334.git.mvogt@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=mvogt@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.144, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1727795766006116600 Content-Type: text/plain; charset="utf-8" This commit adds support for the `openat2()` syscall in the `linux-user` userspace emulator. It is implemented by extracting a new helper `maybe_do_fake_open()` out of the exiting `do_guest_openat()` and share that with the new `do_guest_openat2()`. Unfortunately we cannot just make do_guest_openat2() a superset of do_guest_openat() because the openat2() syscall is stricter with the argument checking and will return an error for invalid flags or mode combinations (which open()/openat() will ignore). The implementation is similar to SYSCALL_DEFINE(openat2), i.e. a new `copy_struct_from_user()` is used that works the same as the kernels version to support backwards-compatibility for struct syscall argument. Instead of including openat2.h we create a copy of `open_how` as `open_how_ver0` to ensure that if the structure grows we can log a LOG_UNIMP warning. Note that in this commit using openat2() for a "faked" file in /proc will honor the "resolve" flags for RESOLVE_NO_{MAGIC,SYM}LINKS for path based access to /proc/self/exe (which is the only magic link we support for faked files). Note it will not catch special access via e.g. dirfd. This is not great but it seems similar to the exiting behavior when openat() is called with a dirfd to "/proc". Here too the fake file lookup may not catch the special file because no dirfd is used to determine if the path is in /proc. Signed-off-by: Michael Vogt Buglink: https://github.com/osbuild/bootc-image-builder/issues/619 Reviewed-by: Laurent Vivier --- linux-user/syscall.c | 105 +++++++++++++++++++++++++++++++++++++- linux-user/syscall_defs.h | 13 +++++ 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index a666986189..2febc3bc3f 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -602,6 +602,34 @@ static int check_zeroed_user(abi_long addr, size_t ksi= ze, size_t usize) return 1; } =20 +/* + * 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() + */ +static int +copy_struct_from_user(void *dst, size_t ksize, abi_ptr src, size_t usize) +{ + size_t size =3D MIN(ksize, usize); + size_t rest =3D MAX(ksize, usize) - size; + + /* Deal with trailing bytes. */ + if (usize < ksize) { + memset(dst + size, 0, rest); + } else if (usize > ksize) { + int ret =3D check_zeroed_user(src, ksize, usize); + if (ret <=3D 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 +681,15 @@ safe_syscall3(ssize_t, read, int, fd, void *, buff, si= ze_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) + +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) + #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 +8369,9 @@ static int open_net_route(CPUArchState *cpu_env, int = fd) } #endif =20 -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 =3D NULL; const char *pathname; @@ -8370,6 +8408,12 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd= , const char *fname, } =20 if (is_proc_myself(pathname, "exe")) { + /* Honor openat2 resolve flags */ + if ((openat2_resolve & RESOLVE_NO_MAGICLINKS) || + (openat2_resolve & RESOLVE_NO_SYMLINKS)) { + errno =3D ELOOP; + return -1; + } if (safe) { return safe_openat(dirfd, exec_path, flags, mode); } else { @@ -8416,6 +8460,17 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd= , const char *fname, return fd; } =20 + return -2; +} + +int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname, + int flags, mode_t mode, bool safe) +{ + int fd =3D 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 +8478,49 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd= , const char *fname, } } =20 + +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 =3D {0}; + char *pathname; + int ret; + + if (guest_size < sizeof(struct target_open_how_ver0)) { + return -TARGET_EINVAL; + } + ret =3D copy_struct_from_user(&how, sizeof(how), guest_open_how, guest= _size); + if (ret) { + if (ret =3D=3D -TARGET_E2BIG) { + qemu_log_mask(LOG_UNIMP, + "Unimplemented openat2 open_how size: " + TARGET_ABI_FMT_lu "\n", guest_size); + } + return ret; + } + pathname =3D lock_user_string(guest_pathname); + if (!pathname) { + return -TARGET_EFAULT; + } + + how.flags =3D target_to_host_bitmask(tswap64(how.flags), fcntl_flags_t= bl); + how.mode =3D tswap64(how.mode); + how.resolve =3D tswap64(how.resolve); + int fd =3D maybe_do_fake_open(cpu_env, dirfd, pathname, how.flags, how= .mode, + how.resolve, true); + if (fd > -2) { + ret =3D get_errno(fd); + } else { + ret =3D 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 +9293,9 @@ static abi_long do_syscall1(CPUArchState *cpu_env, in= t num, abi_long arg1, fd_trans_unregister(ret); unlock_user(p, arg2, 0); return ret; + case TARGET_NR_openat2: + ret =3D 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 =3D do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5); diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h index e08d088740..de5091c977 100644 --- a/linux-user/syscall_defs.h +++ b/linux-user/syscall_defs.h @@ -2748,4 +2748,17 @@ struct target_sched_param { abi_int sched_priority; }; =20 +/* from kernel's include/uapi/linux/openat2.h */ +struct target_open_how_ver0 { + abi_ullong flags; + abi_ullong mode; + abi_ullong resolve; +}; +#ifndef RESOLVE_NO_MAGICLINKS +#define RESOLVE_NO_MAGICLINKS 0x02 +#endif +#ifndef RESOLVE_NO_SYMLINKS +#define RESOLVE_NO_SYMLINKS 0x04 +#endif + #endif --=20 2.45.2 From nobody Sun Dec 22 05:15:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1727795809; cv=none; d=zohomail.com; s=zohoarc; b=AtU5PmutkEIHbx3BJdLjpCdOCv7xT24jU9qTjv2G8dupi1taAqhW3g0QJHGRGxrbSE7+Ki/LkIyKCv2Y3LdBvoubyzm9Oj+swllbMr+Igrj0HqYSM4vDHDwRZndavkVHTDHr/Cwwvify/kQHShKf+wP3KRRW8pGeOrRQjEEARmU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727795809; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Z+DDLwciT2U9s6YaMw5ad3NppqiC8fN15Iiq9AYSzTQ=; b=mLWg68H+86H+5RYTw9y+CfwrYxwE2HJwHUHXqCDXfYloC55wSSoHFwNROBeQTPn3UTDOeXHFVhaONFXt4+ygf78SCJy+SIGt8i/pwq+SCkpAi7/b96t4pnl5oSZI9Rkw8+mjQNXfHi7MFfvHlHa6vBatsc2iUaSdxm5dlCXZNvI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1727795809233187.54856782866887; Tue, 1 Oct 2024 08:16:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sveam-0005rj-Mt; Tue, 01 Oct 2024 11:15:12 -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 1sveaj-0005pC-Qe for qemu-devel@nongnu.org; Tue, 01 Oct 2024 11:15:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sveah-000317-Gd for qemu-devel@nongnu.org; Tue, 01 Oct 2024 11:15:09 -0400 Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-207-dmHBveO6NWi2hGNacavDvA-1; Tue, 01 Oct 2024 11:15:05 -0400 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a8711c48990so464977266b.2 for ; Tue, 01 Oct 2024 08:15:05 -0700 (PDT) Received: from top.fritz.box (p4fdf9fd8.dip0.t-ipconnect.de. [79.223.159.216]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a93c2775d23sm732438166b.4.2024.10.01.08.15.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 08:15:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727795706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z+DDLwciT2U9s6YaMw5ad3NppqiC8fN15Iiq9AYSzTQ=; b=a6ybsV6WBzMtvCVCbljUy9IuOy3oLkTwaGbaAbwnlLYhDTaSQ094EnRUXqWkUtAnPSnG9n 1S23fIU9Bc4bxg7XxTACT5xA4n2kd7lAMKA8n1BvbqHb5nkkLcrskx9HIYH62qjg9HB9XW 8MohYvUNROfjj0A6SY8WtJMbtjYBV0w= X-MC-Unique: dmHBveO6NWi2hGNacavDvA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727795704; x=1728400504; 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=Z+DDLwciT2U9s6YaMw5ad3NppqiC8fN15Iiq9AYSzTQ=; b=pgAOftfJFnZxKBy4S9/S9+aiCKw5jyzX3M1w3e72oRIsyxzdiA5ze4NJncu+UacDwp D8r+4sbk0Smh+9YRsjvlTIupP9+9LlgQXDrw+F35kcKX5va8V2hLZK0HSkdF/Hs5pUOY 1pjJGlLSfhywfQ0sUKzQEE/7c58U9xYkjjUcv7+JOFm3yYF9OAE84ldGHTK9V+bEZp+N ogloHIpJf/RKo212Hr03HdOe6OKBiB6HOH2cNypJYh6SnBuMM+BuwhMdlWvWQ5zGIOBF vqTgZpKGm/I8dJ1XIO8BG6d1oMoJPfX7gPLpq8uXf3VF6QcRJnHqU8MrMUxOxtBi6zRW dy9Q== X-Gm-Message-State: AOJu0YyYS50SvntzM39vDt+3UtGRHevnZjCCuR9xyDGJbC5MQYEK2GgK YjcDDQMFbuciNlGZF1lYH+8NJGN/Qaqp+kvAf4w/H0RLEQsWTMjTSv9K9UmtJSyuxqH2oHEwx5S t3yJLQ170QYS2j6q0VlmMZj73mIzEv2hSJ5ChgvuSDuKHKLbidGZNaw+GC+l+3Jtu8hXEdgg7DD oyMsuiOALBTJ6uB5+Y4x8k+WzK/0EWEuQS X-Received: by 2002:a17:907:74b:b0:a8a:53fb:d571 with SMTP id a640c23a62f3a-a93c48e8009mr1917857366b.11.1727795704152; Tue, 01 Oct 2024 08:15:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3AtHV0UP/RLGc9t6tkda11KoxmLU0QNbSKwZB2U4GzkdAXVsq7RFsI8zdkWXlTBkJ8rDq6A== X-Received: by 2002:a17:907:74b:b0:a8a:53fb:d571 with SMTP id a640c23a62f3a-a93c48e8009mr1917853766b.11.1727795703683; Tue, 01 Oct 2024 08:15:03 -0700 (PDT) From: Michael Vogt To: qemu-devel@nongnu.org Cc: Michael Vogt , Richard Henderson , Laurent Vivier , Michael Vogt Subject: [PATCH v9 2/2] linux-user: add strace support for openat2 Date: Tue, 1 Oct 2024 17:14:54 +0200 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=mvogt@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.144, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1727795810276116600 Content-Type: text/plain; charset="utf-8" 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 --- linux-user/qemu.h | 9 ++++++++ linux-user/strace.c | 44 +++++++++++++++++++++++++++++++++++++++ linux-user/strace.list | 3 +++ linux-user/syscall.c | 8 +------ linux-user/syscall_defs.h | 5 +++++ meson.build | 1 + 6 files changed, 63 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); =20 +/* + * 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 usi= ze); + /* Functions for accessing guest memory. The tget and tput functions read/write single values, byteswapping as necessary. The lock_user fun= ction gets a pointer to a contiguous area of guest memory, but does not perfo= rm diff --git a/linux-user/strace.c b/linux-user/strace.c index b4d1098170..350cb707e4 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[] =3D { FLAG_END, }; =20 +UNUSED static const struct flags openat2_resolve_flags[] =3D { +#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[] =3D { #ifdef MS_BIND FLAG_GENERIC(MS_BIND), @@ -3483,6 +3498,35 @@ print_openat(CPUArchState *cpu_env, const struct sys= callname *name, } #endif =20 +#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 =3D {0}; + + print_syscall_prologue(name); + print_at_dirfd(arg0, 0); + print_string(arg1, 0); + if (copy_struct_from_user(&how, sizeof(how), arg2, arg3) =3D=3D 0) { + how.flags =3D tswap64(how.flags); + how.mode =3D tswap64(how.mode); + how.resolve =3D tswap64(how.resolve); + + 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, 0); + } 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 *n= ame, 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/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 ksiz= e, 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 usi= ze) { size_t size =3D MIN(ksize, usize); size_t rest =3D MAX(ksize, usize) - size; @@ -682,11 +681,6 @@ safe_syscall3(ssize_t, write, int, fd, const void *, b= uff, size_t, count) safe_syscall4(int, openat, int, dirfd, const char *, pathname, \ int, flags, mode_t, mode) =20 -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) =20 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 { }; =20 /* 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/meson.build b/meson.build index 10464466ff..77cb5c41d8 100644 --- a/meson.build +++ b/meson.build @@ -2488,6 +2488,7 @@ config_host_data.set('CONFIG_LINUX_MAGIC_H', cc.has_h= eader('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')) --=20 2.45.2