From patchwork Sun Jul 21 09:08:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivian Wang X-Patchwork-Id: 13738004 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 90FF3C3DA5D for ; Sun, 21 Jul 2024 13:22:34 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sVWVk-0000yI-By; Sun, 21 Jul 2024 09:22:00 -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 1sVSZP-0002Ij-Jg for qemu-devel@nongnu.org; Sun, 21 Jul 2024 05:09:31 -0400 Received: from kuriko.dram.page ([65.108.252.55]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sVSZM-0002Q7-Ex for qemu-devel@nongnu.org; Sun, 21 Jul 2024 05:09:31 -0400 From: Vivian Wang DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dram.page; s=mail; t=1721552964; bh=NeRawDYGirUd6fjzt3B5LrPbIDPJUX8InLiktOIoU7g=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GQpy6TW4BW/jpK02FOhBT6Hh1LeAot1xNhHK1Uia1OnTSdcGK5qg9FHux6zeFGX+e nY9UpOHFzPZtcndQVKhdFzrpgH8fs2p1ewab8pMl0bZYb6q9wwiJxndrFqBSIEu5LK NI7op7FENsgrtNfexpqKWPY0DceJKiiquLuV55/U= To: qemu-devel@nongnu.org Cc: Vivian Wang , Richard Henderson , Laurent Vivier Subject: [PATCH 1/2] util/getauxval: Ensure setting errno if not found Date: Sun, 21 Jul 2024 17:08:16 +0800 Message-ID: <20240721090817.120888-2-uwu@dram.page> In-Reply-To: <20240721090817.120888-1-uwu@dram.page> References: <20240721090817.120888-1-uwu@dram.page> MIME-Version: 1.0 Received-SPF: pass client-ip=65.108.252.55; envelope-from=uwu@dram.page; helo=kuriko.dram.page 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sun, 21 Jul 2024 09:21:56 -0400 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sometimes zero is a valid value for getauxval (e.g. AT_EXECFD). Make sure that we can distinguish between a valid zero value and a not found entry by setting errno. Ignore getauxval from sys/auxv.h on glibc < 2.19 because it does not set errno. Signed-off-by: Vivian Wang --- util/getauxval.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/util/getauxval.c b/util/getauxval.c index b124107d61..f1008bdc59 100644 --- a/util/getauxval.c +++ b/util/getauxval.c @@ -24,7 +24,13 @@ #include "qemu/osdep.h" -#ifdef CONFIG_GETAUXVAL +/* If glibc < 2.19, getauxval can't be used because it does not set errno if + entry is not found. */ +#if defined(CONFIG_GETAUXVAL) && \ + (!defined(__GLIBC__) \ + || __GLIBC__ > 2 \ + || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19)) + /* Don't inline this in qemu/osdep.h, because pulling in for the system declaration of getauxval pulls in the system , which conflicts with qemu's version. */ @@ -95,6 +101,7 @@ unsigned long qemu_getauxval(unsigned long type) } } + errno = ENOENT; return 0; } @@ -104,7 +111,9 @@ unsigned long qemu_getauxval(unsigned long type) unsigned long qemu_getauxval(unsigned long type) { unsigned long aux = 0; - elf_aux_info(type, &aux, sizeof(aux)); + int ret = elf_aux_info(type, &aux, sizeof(aux)); + if (ret != 0) + errno = ret; return aux; } @@ -112,6 +121,7 @@ unsigned long qemu_getauxval(unsigned long type) unsigned long qemu_getauxval(unsigned long type) { + errno = ENOSYS; return 0; } From patchwork Sun Jul 21 09:08:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivian Wang X-Patchwork-Id: 13738005 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 92D57C3DA63 for ; Sun, 21 Jul 2024 13:22:34 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sVWVh-0000r4-Vw; Sun, 21 Jul 2024 09:21:58 -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 1sVSZO-0002Gu-3J for qemu-devel@nongnu.org; Sun, 21 Jul 2024 05:09:30 -0400 Received: from kuriko.dram.page ([65.108.252.55]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sVSZM-0002QF-FP for qemu-devel@nongnu.org; Sun, 21 Jul 2024 05:09:29 -0400 From: Vivian Wang DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dram.page; s=mail; t=1721552966; bh=gK/jIAxVKlERV1qkgDSRBBE0fYvUoxt+qhPcUqdUVgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=L8pqtadHCcegDS+Gd8+hfVC+G27C3rkaYxCnzUSPgQm3v86DQoKLfad96BR2q0fd6 tGDlnebCH86vwvGEi0IaRaxPwksfPQMRKlZEOkZAJLgQsYZqr7wXeuRiCnXT3NzeGq yMvUBKOnmzo5Zr2EISDtK5h5jcKREvAu5hgUIbVE= To: qemu-devel@nongnu.org Cc: Vivian Wang , Richard Henderson , Laurent Vivier Subject: [PATCH 2/2] linux-user/main: Check errno when getting AT_EXECFD Date: Sun, 21 Jul 2024 17:08:17 +0800 Message-ID: <20240721090817.120888-3-uwu@dram.page> In-Reply-To: <20240721090817.120888-1-uwu@dram.page> References: <20240721090817.120888-1-uwu@dram.page> MIME-Version: 1.0 Received-SPF: pass client-ip=65.108.252.55; envelope-from=uwu@dram.page; helo=kuriko.dram.page 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sun, 21 Jul 2024 09:21:56 -0400 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org It's possible for AT_EXECFD to end up with a valid value of 0. Check errno when using qemu_getauxval instead of return value to handle this case. Not handling this case leads to a confusing condition where the executable ends up as fd 0, i.e. stdin. Signed-off-by: Vivian Wang Fixes: 0b959cf5e4cc ("linux-user: Use qemu_getauxval for AT_EXECFD") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2448 Reviewed-by: Richard Henderson --- linux-user/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/linux-user/main.c b/linux-user/main.c index 7d3cf45fa9..8143a0d4b0 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -755,8 +755,9 @@ int main(int argc, char **argv, char **envp) /* * Manage binfmt-misc open-binary flag */ + errno = 0; execfd = qemu_getauxval(AT_EXECFD); - if (execfd == 0) { + if (errno != 0) { execfd = open(exec_path, O_RDONLY); if (execfd < 0) { printf("Error while loading %s: %s\n", exec_path, strerror(errno));