From patchwork Thu Mar 20 20:54:39 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: 40522 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 093A827BBEA; Thu, 20 Mar 2025 20:58:00 +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 C49E527BBE2 for ; Thu, 20 Mar 2025 20:57:59 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tvMx2-0002Zu-6o; Thu, 20 Mar 2025 16:57:16 -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 1tvMwu-0002YG-Kg for guix-patches@gnu.org; Thu, 20 Mar 2025 16:57:12 -0400 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 1tvMwt-0001VQ-Ua for guix-patches@gnu.org; Thu, 20 Mar 2025 16:57:08 -0400 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=3sOVW5n0d1cSpQd8PLRrsmflCadSWuH5XwPCIe/iTyM=; b=g8GWsPX8REOLwevhoyXgEAR58eOmdtPzKiKh0vx+S87tJh4PBYLrkBmBbOw6+2nK8rMFyKWZReLHDy1Sk0Qwk0PwJDr1sgeZBCYTFBnNFQz0cwAHEZbZPQFkR5bCsVuPEntPlixIgwWU7GcVoffEIIv5s8nCaKPJONTibVHx+3xzQ4Jlz5u7uLgkYUA5aCeGIITHwAmAgJb4b+8LMQvqvG/mJlGJ7MCbXRTDFoY9xL1Aw1BDFx9zOA91lCy8NtJcLrSrKTkBFXei1crV9d+VN/ADJbXs0kSV7+pD9tEkihc+MIbdYf2C4OYXvtXFWhjUBuBwb0K2sTEHXvKb8E4spA==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tvMwt-0006Yj-Ls for guix-patches@gnu.org; Thu, 20 Mar 2025 16:57:07 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#75810] [PATCH v7 06/16] 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: Thu, 20 Mar 2025 20:57:07 +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.174250420325026 (code B ref 75810); Thu, 20 Mar 2025 20:57:07 +0000 Received: (at 75810) by debbugs.gnu.org; 20 Mar 2025 20:56:43 +0000 Received: from localhost ([127.0.0.1]:59178 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tvMwT-0006VR-R7 for submit@debbugs.gnu.org; Thu, 20 Mar 2025 16:56:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41196) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tvMw7-0006Qz-0i for 75810@debbugs.gnu.org; Thu, 20 Mar 2025 16:56:21 -0400 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 1tvMw1-0001GI-3V; Thu, 20 Mar 2025 16:56:13 -0400 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=3sOVW5n0d1cSpQd8PLRrsmflCadSWuH5XwPCIe/iTyM=; b=OvaQ5z0ANTbpsqF0NS2e vxv3e4Hl+Dl7rnyiGZQdX5Mql0ZLeIDoofdRmK4wSYA8CjNK2EXcmGr/d+CtOoD1qZSFt4gm7TsMA x2qdnWfmnViM1JDle3Cg4ldkssc1X02YEIVLkSLMs92oYcIRx9t075qXyvTP845N7pMWBw7kD2/h/ k4/VdhzLC2okKLqBm29fglZVMyX2Be3z/Z/lfRG+AiUJ/DoKx3kR4yPG17UlHrfldMztHG+Lpshff qBIodCntT+y8Ple/knLyzuvDGh95JOd1s8UMMC17WKOjcen+olgdoBoj+1Z8xOO7g5Ng6Vu4VACUG 3wFIz1pi9XhcUQ==; From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Thu, 20 Mar 2025 21:54:39 +0100 Message-ID: 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 3861a1ffd9..c8b778362a 100644 --- a/nix/libstore/build.cc +++ b/nix/libstore/build.cc @@ -2091,6 +2091,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. */ @@ -2164,6 +2176,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