From patchwork Mon Mar 17 17:02:49 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: 40286 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 51CF327BBEA; Mon, 17 Mar 2025 17:05:45 +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 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 07B6F27BBE2 for ; Mon, 17 Mar 2025 17:05:45 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tuDts-0005AN-5G; Mon, 17 Mar 2025 13:05: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 1tuDtn-00057f-T1 for guix-patches@gnu.org; Mon, 17 Mar 2025 13:05: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 1tuDtn-0005h6-FX for guix-patches@gnu.org; Mon, 17 Mar 2025 13:05:11 -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=alB18FQ02RJgQ2t9PmgVFEVxyGnlHGMnOSmTnM+uGrabDUsdFg7+SvTlJc3tKmWEwLl2C65Mq3DGcnqWWDPdN2F9+s712D2UmrFFV74orsC90/kBbOkH/hbC343Og4NLePfJIc56/SjC9a2PePCkr+/E2yFMaE+JmCLDnBb1tY1ljrOFYS8AvnyomqXQ4eOTl0eHmvb2xNqdzd2JkojMfcmQ/dnQahXI3gMjW8xFBTSLerCLJVUEtT3eWavggGnqkUB4mdz8qFYAL+0rBRSgnIhLYp8nEr4vz9Mb9asiS8Yh7WEx+o+E5s3iMsyJwLD71znRFbdjdxYbTSIxSNCH9Q==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tuDtm-0002pP-0e for guix-patches@gnu.org; Mon, 17 Mar 2025 13:05:10 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#75810] [PATCH v6 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: Mon, 17 Mar 2025 17:05:09 +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.174223105410215 (code B ref 75810); Mon, 17 Mar 2025 17:05:09 +0000 Received: (at 75810) by debbugs.gnu.org; 17 Mar 2025 17:04:14 +0000 Received: from localhost ([127.0.0.1]:60648 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tuDsp-0002eL-Tq for submit@debbugs.gnu.org; Mon, 17 Mar 2025 13:04:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34942) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tuDsb-0002aO-Cu for 75810@debbugs.gnu.org; Mon, 17 Mar 2025 13:03:59 -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 1tuDsV-00057a-4w; Mon, 17 Mar 2025 13:03:51 -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=DQ54cPzuL/mSP1qf4akB qryPUChROs9LsSf6L0HjxSTMFy2e2XtMHrTs4/51jMpyt2nB/LJf3SN2UtEfiZS1fsSeIpKEakTcs p2uUHyga9KK0AdBmb7ubLWMD0dQFeyOdxwStuarfiq37UWdd0rUh9+eKlILtZd6fwDYy7ggrG+azu gZPvo2siO9d0Hs27mFZm73z8TJeGPzLhiC2xQrm+Go+iwBu1cpCLF/mdlSYxnIOgYN9E6Z7uuluoe r7X9FmKsRu/QOsxYxfuqJuedmIMzHHDmWi53nTf8w8ugmD048JD0wPKkCmK5hx6IXLKRYN7t+DmJS fXdkCGj663DujA==; From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Mon, 17 Mar 2025 18:02:49 +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