From patchwork Thu Dec 7 13:43:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gnau X-Patchwork-Id: 1873199 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=JsBlMf0c; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SmFvJ5QnFz23mf for ; Fri, 8 Dec 2023 00:47:24 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type:List-Help: Reply-To:List-Archive:List-Unsubscribe:List-Subscribe:From:List-Post:List-Id: Message-ID:MIME-Version:Date:Subject:To:Cc:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=UQreWsNK0FmtCcSZ81eZNhuM7ZNRoXsG+ApG4mwss9s=; b=JsBlMf0c8sTZRnNibIEMsaMSqa jckmL+56n6NyvwdHlegZwLwbvetAUlOmzX9weYYZ14NmfpsH5QJe22/KKtULOa9Pa7+adoszb0gY0 EU3fwyNZlTali5VyUbyhCBDR9l8SQhuWxBtUy4vfNrOYDrJK+Dfa7o9K2GQ3rC6QbPgX1/aVcSu06 2+ut8tjDCSbjXhlxWfQwLRS/4sKHSmic99/MH6kHO1++uV3mzutynyxMh7ZAfjs25oT/WshD2Lx/8 m1y2VDFRJnF9Q99WWHD7XUDI+SVdmSPTklx2u+F+jKLER8jbBicao4O1gLdTinmX4catsXVIb8Pvm 6rDEGo7Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rBEgD-00Cvyl-0i; Thu, 07 Dec 2023 13:44:41 +0000 To: openwrt-devel@lists.openwrt.org, John Crispin Subject: [PATCH procd] state: set_stdio: chdir back to / in case of failure Date: Thu, 7 Dec 2023 14:43:45 +0100 MIME-Version: 1.0 Message-ID: List-Id: OpenWrt Development List List-Post: X-Patchwork-Original-From: Andreas Gnau via openwrt-devel From: Andreas Gnau Precedence: list X-Mailman-Version: 2.1.34 X-BeenThere: openwrt-devel@lists.openwrt.org List-Subscribe: , List-Unsubscribe: , List-Archive: Reply-To: Andreas Gnau List-Help: Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org The sender domain has a DMARC Reject/Quarantine policy which disallows sending mailing list messages using the original "From" header. To mitigate this problem, the original message has been wrapped automatically by the mailing list software. set_stdio chdirs to /dev/ to facilitate easy freopen of the console device name given by the tty parameter. Make sure to chdir back to / done in all cases, even in the error path. This keeps the function free from unintended side effects. Before this commit, in case of an error, the working directory would remain /dev/ which would break sysupgrade because the rest of the code would rely on the current working directory to be unchanged, which is not an unreasonable expectation to make. Fixing this fixes an issue where sysupgrade would fail, when /dev/console does not exist or cannot be opened, which can happen for example when setting console= on kernel cmdline. Closes: https://github.com/openwrt/openwrt/issues/6005 Fixes: 91da63d3d3fd ("properly handle return codes") Signed-off-by: Andreas Gnau --- state.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/state.c b/state.c index fb81248fd7e7..30e3569c8af9 100644 --- a/state.c +++ b/state.c @@ -48,11 +48,12 @@ static void set_stdio(const char* tty) if (chdir("/dev") || !freopen(tty, "r", stdin) || !freopen(tty, "w", stdout) || - !freopen(tty, "w", stderr) || - chdir("/")) + !freopen(tty, "w", stderr)) ERROR("failed to set stdio: %m\n"); else fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK); + if (chdir("/")) + ERROR("failed to change dir to /: %m\n"); } static void set_console(void)