From patchwork Fri Feb 21 13:05:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 38905 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 4CA2827BBEA; Fri, 21 Feb 2025 13:08:43 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 05E4927BBE2 for ; Fri, 21 Feb 2025 13:08:43 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tlSlE-000578-Ov; Fri, 21 Feb 2025 08:08:08 -0500 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 1tlSlC-000568-CZ for guix-patches@gnu.org; Fri, 21 Feb 2025 08:08:06 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tlSlB-0004fr-PF for guix-patches@gnu.org; Fri, 21 Feb 2025 08:08:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=VRbaQkpHuujQuVs9fnXsFpQvamzXpEoyOQmUI3kKkJU=; b=uUxWA3FZU3r0o6LSsbL8KKGM8fDRBlWaAWkz5ZVj0Xf9we+arsuAfqo6imQWMui0DOVUw0+JO/gAimnSIPzpdmuvgAeEDZ4EynpG24B9oG22/mCUIaPbL1thTIxZS/cPQM8qBTUktadVAt7tsjbo1O4thGT25T24IJTBo5uXb01J+uz/yziPHrln0i9JZtenF79yWXU1L47iB6as07QPf4d9cwCfPv/hVD5LyFuMw6/aMqxY6U7i+TIDD6ZLY8lem8pUdKlMKLTEv29zsUxHtSWSxnIxmxnw8PK9OxFF7fJCz9CySec2WJ+30Y8yrk6M/VQ+yONXGyPM2ZxkBci4Yw==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tlSlB-0005dH-BA for guix-patches@gnu.org; Fri, 21 Feb 2025 08:08:05 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#75810] [PATCH v3 04/11] daemon: Remount root directory as read-only. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 21 Feb 2025 13:08:05 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 75810 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 75810@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 75810-submit@debbugs.gnu.org id=B75810.174014326721437 (code B ref 75810); Fri, 21 Feb 2025 13:08:05 +0000 Received: (at 75810) by debbugs.gnu.org; 21 Feb 2025 13:07:47 +0000 Received: from localhost ([127.0.0.1]:56675 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tlSkq-0005Z5-3e for submit@debbugs.gnu.org; Fri, 21 Feb 2025 08:07:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48282) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tlSjr-0005No-NE for 75810@debbugs.gnu.org; Fri, 21 Feb 2025 08:06:49 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tlSjm-0004Yp-7F; Fri, 21 Feb 2025 08:06:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=VRbaQkpHuujQuVs9fnXsFpQvamzXpEoyOQmUI3kKkJU=; b=dKDJ0U0xZVmzw0RV3sTS HqTzTYj+DkTY3554MBgb90Yq2uCTFaKBNOmy1Fvk4IFlekW2tmzco+7ErZiynwIXeWLoxDVLUg1Zh 9iPDaaDy761EzYvHA95e3787leu9S33EnVUh8ReMbUVn6GK6Trm8dv/Q6hP7VUl0+qkvJlJ8kazp2 5XaxreDXZagCkWMUdW7ZljxXoEaZJVHLVsnYNIi3IL/9Syedc2DTLQQQQMA+eeAl3m4jClq/Xpe3s krjZGkfyhqf1VEjrgYL0TQXnF4u+qzLDsXZiZomdqyova8+HqPjLsvkAecFYuVFeRJrMySvjtRfOM NtFhRhNYhuWtfg==; From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Fri, 21 Feb 2025 14:05:52 +0100 Message-ID: <6272a7109a276d4ffa5cdd8b218b3233aaade5f5.1740142328.git.ludo@gnu.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * nix/libstore/build.cc (DerivationGoal::runChild): Bind-mount the store and /tmp under ‘chrootRootDir’ to themselves as read-write. Remount / as read-only. Change-Id: I79565094c8ec8448401897c720aad75304fd1948 --- nix/libstore/build.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc index 6244c99e751..c87f4f767c5 100644 --- a/nix/libstore/build.cc +++ b/nix/libstore/build.cc @@ -2078,6 +2078,18 @@ void DerivationGoal::runChild() for (auto & i : ss) dirsInChroot[i] = i; + /* Make new mounts for the store and for /tmp. That way, when + 'chrootRootDir' is made read-only below, these two mounts will + remain writable (the store needs to be writable so derivation + outputs can be written to it, and /tmp is writable by + convention). */ + auto chrootStoreDir = chrootRootDir + settings.nixStore; + if (mount(chrootStoreDir.c_str(), chrootStoreDir.c_str(), 0, MS_BIND, 0) == -1) + throw SysError(format("read-write mount of store '%1%' failed") % chrootStoreDir); + auto chrootTmpDir = chrootRootDir + "/tmp"; + if (mount(chrootTmpDir.c_str(), chrootTmpDir.c_str(), 0, MS_BIND, 0) == -1) + throw SysError(format("read-write mount of temporary directory '%1%' failed") % chrootTmpDir); + /* Bind-mount all the directories from the "host" filesystem that we want in the chroot environment. */ @@ -2151,6 +2163,10 @@ void DerivationGoal::runChild() if (rmdir("real-root") == -1) throw SysError("cannot remove real-root directory"); + + /* Remount root as read-only. */ + if (mount("/", "/", 0, MS_BIND | MS_REMOUNT | MS_RDONLY, 0) == -1) + throw SysError(format("read-only remount of build root '%1%' failed") % chrootRootDir); } #endif