From nobody Thu May 9 06:15:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 165911001823229.157862166427776; Fri, 29 Jul 2022 08:53:38 -0700 (PDT) Received: from localhost ([::1]:56228 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oHSIy-00082q-49 for importer@patchew.org; Fri, 29 Jul 2022 11:53:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51720) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oHSFR-0007BD-Qn for qemu-devel@nongnu.org; Fri, 29 Jul 2022 11:49:57 -0400 Received: from donkey.codingfarm.de ([2a01:4f8:190:12cf::d:1]:36502) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oHSFQ-0007pt-0R for qemu-devel@nongnu.org; Fri, 29 Jul 2022 11:49:57 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by donkey.codingfarm.de (Postfix) with ESMTPSA id 93EBC8F551; Fri, 29 Jul 2022 17:49:51 +0200 (CEST) Received: by zebra.codingfarm.de (Postfix, from userid 1000) id 45F6940A5A; Fri, 29 Jul 2022 17:49:51 +0200 (CEST) From: =?UTF-8?q?Rainer=20M=C3=BCller?= To: qemu-devel@nongnu.org Cc: Richard Henderson , =?UTF-8?q?Rainer=20M=C3=BCller?= , Laurent Vivier Subject: [PATCH v2] linux-user: Use memfd for open syscall emulation Date: Fri, 29 Jul 2022 17:49:51 +0200 Message-Id: <20220729154951.76268-1-raimue@codingfarm.de> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220725162811.87985-1-raimue@codingfarm.de> References: <20220725162811.87985-1-raimue@codingfarm.de> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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: none client-ip=2a01:4f8:190:12cf::d:1; envelope-from=raimue@zebra.codingfarm.de; helo=donkey.codingfarm.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, NO_DNS_FOR_FROM=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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" X-ZM-MESSAGEID: 1659110020128100001 For certain paths in /proc, the open syscall is intercepted and the returned file descriptor points to a temporary file with emulated contents. If TMPDIR is not accessible or writable for the current user (for example in a read-only mounted chroot or container) tools such as ps from procps may fail unexpectedly. Trying to read one of these paths such as /proc/self/stat would return an error such as ENOENT or EROFS. To relax the requirement on a writable TMPDIR, use memfd_create() instead to create an anonymous file and return its file descriptor. Signed-off-by: Rainer M=C3=BCller Reviewed-by: Richard Henderson --- v2: no more #ifdefs, use stub from util/memfd.c with ENOSYS fallback, tested with 'strace -e fault=3Dmemfd_create' --- linux-user/syscall.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 991b85e6b4..7b55726f25 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8269,16 +8269,22 @@ static int do_openat(CPUArchState *cpu_env, int dir= fd, const char *pathname, int char filename[PATH_MAX]; int fd, r; =20 - /* create temporary file to map stat to */ - tmpdir =3D getenv("TMPDIR"); - if (!tmpdir) - tmpdir =3D "/tmp"; - snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir= ); - fd =3D mkstemp(filename); + fd =3D memfd_create("qemu-open", 0); if (fd < 0) { - return fd; + if (errno !=3D ENOSYS) { + return fd; + } + /* create temporary file to map stat to */ + tmpdir =3D getenv("TMPDIR"); + if (!tmpdir) + tmpdir =3D "/tmp"; + snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tm= pdir); + fd =3D mkstemp(filename); + if (fd < 0) { + return fd; + } + unlink(filename); } - unlink(filename); =20 if ((r =3D fake_open->fill(cpu_env, fd))) { int e =3D errno; --=20 2.25.1