From nobody Sun Nov 24 08:23:11 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1725032451; cv=none; d=zohomail.com; s=zohoarc; b=P1T7F/arn3wqw4GClYAHtRzLZIHz/caOMt9HrQFErh0WPwtv6fI3qxHNBfta+yDgzy3e418Hm0b/lxAsQbb1eJyRce2zGUGKdRrYHSoV476IRvcaNfr//F4+DViBCt/CEl522eVHDy3F7bngqSbqN1Uo1qNGgVLOJbKR2MPtJFY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725032451; 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=3dtWBVBnvRzXmNnNzQnWmL83+wdr3RB3HRnVZEGFSZY=; b=a5JBXN1QsE1CxQG/82PahM/e8n3K7suNUNwQ5O1JZL2DFzxpLKlZFLSdj0RaRr5XN9Vs0iyd+2srwnHPB6C4Hj59I/0kF2BpslxiMxQtuihGXzmTbYtbAM3HMfrDET2QCf8PklwD4EItC9et8YMkZ6cfO6aKY7LclC4n9GRM+CQ= 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 172503245185942.806391526759285; Fri, 30 Aug 2024 08:40:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sk3jj-0007CN-EE; Fri, 30 Aug 2024 11:40:31 -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 1sk3jh-0007Bs-UB for qemu-devel@nongnu.org; Fri, 30 Aug 2024 11:40:29 -0400 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sk3jf-0000gs-N2 for qemu-devel@nongnu.org; Fri, 30 Aug 2024 11:40:29 -0400 Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-58ef19aa69dso1843093a12.3 for ; Fri, 30 Aug 2024 08:40:27 -0700 (PDT) Received: from top.fritz.box (p4fd6b0d7.dip0.t-ipconnect.de. [79.214.176.215]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a89891964d8sm227525766b.129.2024.08.30.08.40.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 08:40:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725032426; x=1725637226; 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=3dtWBVBnvRzXmNnNzQnWmL83+wdr3RB3HRnVZEGFSZY=; b=TcsTX84QeJdeuK1zHaiyXnP5j7S7NQfwbkzGAvaH3uly1gGW21GGgwhG+Im+RCncRp 9WzieDn3jjv7jgLqqZeeo5l51F6UNzWA3SXdzll7HEVCoRSfg3krvcLpIyVkbY2bxUio MWSuwRlLs/mVYoJJA3Fv8VQ9UFffCb4wFO2BiGGJ3RhL/TJ5kCaCZ3Jw9NVSYFIIPBTh +p70hvtnM9+A/KfbkRww++Lvr7VoKTr3CMEcGPfAMtEKaz5zVHmGxy7CCaGvXA96BP/a c29pyoe9n7djV9gydYjrC5pmtFqugpkGy4TU1QI/s9LX+d7up8+VvWFB69YiWTzXD5Ns BRiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725032426; x=1725637226; 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=3dtWBVBnvRzXmNnNzQnWmL83+wdr3RB3HRnVZEGFSZY=; b=IjRvlkhxNzKQMStvkb5OuUfWY2CFsgwOIQTIr6xHCQbErNa4c+fA4H8iWG9sHXLSxM 8GkI+LDMmzf9pQ43oFFkMzFtsKRs0wuOfrJv5ym/kO5X8KAWy4m7WuW4Iu9Sms3eKjts 7zvvm/LUgTA83Cvqzl1vINkW7Utd6rEptSWxjiPCuXgFfTdDSTPfgD1LBKX7Poh9XAk0 bJmsdhJ450HiiVq8KahnPtg7U2Kxu6DdzXRVX6A/ZXDrHi6XgUHXfh7J+eg6koeDN86E wZXaRqkdgsKajoZfbYwuxnHA5sKDooPLUWZoiSCAm4SLmG009LiMHaxTYFu0+YFb4jBC HjAw== X-Gm-Message-State: AOJu0YyODtMQVlAlx2QxAM5KvrbJlI8sUVCf40tQNyMuw/qEXqliO19n 0crG2H6MJsThejHzBzqOksxbxZZUKH3HPg5BrJbif9Qc5oJDeK3fIpF6iV/x X-Google-Smtp-Source: AGHT+IEiJwbwWNY04n8hqw1xkzCXrOIJ3b0QBS1OI4zT561StpIzLDnQVCuccf4W7RRcmRJUDm02Kg== X-Received: by 2002:a17:907:97c8:b0:a71:ddb8:9394 with SMTP id a640c23a62f3a-a89b95694c8mr5824666b.40.1725032425317; Fri, 30 Aug 2024 08:40:25 -0700 (PDT) From: Michael Vogt X-Google-Original-From: Michael Vogt To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Michael Vogt Subject: [PATCH v3 1/1] linux-user: add openat2 support in linux-user Date: Fri, 30 Aug 2024 17:39:45 +0200 Message-ID: <20240830153943.5337-4-mvogt@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240830153943.5337-2-mvogt@redhat.com> References: <20240830153943.5337-2-mvogt@redhat.com> 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=2a00:1450:4864:20::52d; envelope-from=michael.vogt@gmail.com; helo=mail-ed1-x52d.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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 @gmail.com) X-ZM-MESSAGEID: 1725032453875116600 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()`. Unfortunatly 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 usef 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 ignore the "resolve" flags. 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 "realpath()" is used to determine if the path is in /proc. Alternatively to ignoring we could simply fail with `-TARGET_ENOSYS` (or similar) if `resolve` flags are passed and we found something that looks like a file in /proc that needs faking. Signed-off-by: Michael Vogt Buglink: https://github.com/osbuild/bootc-image-builder/issues/619 --- linux-user/syscall.c | 116 ++++++++++++++++++++++++++++++++++++-- linux-user/syscall_defs.h | 7 +++ 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 9d5415674d..83c944508b 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) @@ -8334,8 +8371,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, + bool safe, bool *use_returned_fd) { g_autofree char *proc_name =3D NULL; const char *pathname; @@ -8362,6 +8400,7 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd,= const char *fname, #endif { NULL, NULL, NULL } }; + *use_returned_fd =3D true; =20 /* if this is a file from /proc/ filesystem, expand full name */ proc_name =3D realpath(fname, NULL); @@ -8418,13 +8457,77 @@ int do_guest_openat(CPUArchState *cpu_env, int dirf= d, const char *fname, return fd; } =20 + *use_returned_fd =3D false; + return -1; +} + +int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname, + int flags, mode_t mode, bool safe) +{ + bool use_returned_fd; + int fd =3D maybe_do_fake_open(cpu_env, dirfd, fname, flags, mode, safe, + &use_returned_fd); + if (use_returned_fd) { + return fd; + } + if (safe) { - return safe_openat(dirfd, path(pathname), flags, mode); + return safe_openat(dirfd, path(fname), flags, mode); } else { - return openat(dirfd, path(pathname), flags, mode); + return openat(dirfd, path(fname), flags, mode); } } =20 + +static int do_openat2(CPUArchState *cpu_env, abi_long dirfd, + abi_ptr guest_pathname, abi_ptr guest_open_how, + abi_long guest_size) +{ + struct open_how_ver0 how =3D {0}; + 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: %lu\n", + guest_size); + } + return ret; + } + char *pathname =3D lock_user_string(guest_pathname); + if (!pathname) { + return -TARGET_EFAULT; + } + + how.flags =3D target_to_host_bitmask(how.flags, fcntl_flags_tbl); + how.mode =3D tswap64(how.mode); + how.resolve =3D tswap64(how.resolve); + + /* + * Ideally we would pass "how->resolve" flags into this helper too but + * the lookup for files that need faking is based on "realpath()" so + * neither a dirfd for "proc" nor restrictions via "resolve" flags can + * be honored right now. + */ + bool use_returned_fd; + int fd =3D maybe_do_fake_open(cpu_env, dirfd, pathname, how.flags, how= .mode, + true, &use_returned_fd); + if (use_returned_fd) { + return 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; @@ -9197,6 +9300,11 @@ static abi_long do_syscall1(CPUArchState *cpu_env, i= nt num, abi_long arg1, fd_trans_unregister(ret); unlock_user(p, arg2, 0); return ret; +#if defined(TARGET_NR_openat2) + case TARGET_NR_openat2: + ret =3D do_openat2(cpu_env, arg1, arg2, arg3, arg4); + return ret; +#endif #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 a00b617cae..74abcb4613 100644 --- a/linux-user/syscall_defs.h +++ b/linux-user/syscall_defs.h @@ -2754,4 +2754,11 @@ 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; +}; + #endif --=20 2.45.2