From patchwork Sun Oct 20 21:22:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reepca Russelstein X-Patchwork-Id: 32278 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 9A84A27BBEC; Mon, 21 Oct 2024 05:41:05 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FROM_SUSPICIOUS_NTLD,MAILING_LIST_MULTI,PDS_OTHER_BAD_TLD, 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 6EA8927BBE2 for ; Mon, 21 Oct 2024 05:41:03 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t2kDh-0003FH-9v; Mon, 21 Oct 2024 00:40:41 -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 1t2kDd-0003EZ-8m for guix-patches@gnu.org; Mon, 21 Oct 2024 00:40:37 -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 1t2kDc-0006pR-W5 for guix-patches@gnu.org; Mon, 21 Oct 2024 00:40:37 -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:Date:From:To:Subject; bh=Jv1Q3ggmq4d6C+AuYMjgVyp8DXl5ypr+PB0KXqv5tQw=; b=nwPeWmKxX3Dzr90Jiu9OQHJFqtX6pp+KzoKq+0osRZH5lyNWr/7njqz4Sea9aUccEXIsanusE9k9c0Knw8JUfMWSyc5qX+GrjZvBifqM5CEuac+m76OEGZruvwF/D4g9rZVP8vzZwI2fsQ5UJ/gAahy/mpkH6gNzKycz+kWznArQoEvA1mKiQRhmGG95NNquqvFMAU4F3Gr5GolKq3iUa7SsM3szpb52xnpbwo1EtAZ8KYnsTGq1Ae4pS1LAvyBdSwYbpm66Dj0EutsWSDA0op5OmEx01kt5ih0U+S8TEu/0Z+HHiTUmzAVPFZEHvWeUMn7tCmbDwP0kMjhA1t2XYQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1t2kE2-0001wN-JX for guix-patches@gnu.org; Mon, 21 Oct 2024 00:41:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#73923] Vulnerability allowing takeover of build users Resent-From: Reepca Russelstein Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 21 Oct 2024 04:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 73923 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: 73923@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.17294856126991 (code B ref -1); Mon, 21 Oct 2024 04:41:02 +0000 Received: (at submit) by debbugs.gnu.org; 21 Oct 2024 04:40:12 +0000 Received: from localhost ([127.0.0.1]:49654 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1t2kDC-0001oe-AW for submit@debbugs.gnu.org; Mon, 21 Oct 2024 00:40:11 -0400 Received: from lists.gnu.org ([209.51.188.17]:39164) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1t2dOP-00065F-Cj for submit@debbugs.gnu.org; Sun, 20 Oct 2024 17:23:18 -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 1t2dNz-00029b-3Q for guix-patches@gnu.org; Sun, 20 Oct 2024 17:22:51 -0400 Received: from mailout.russelstein.xyz ([2605:6400:20:11e::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t2dNw-0002Ww-77 for guix-patches@gnu.org; Sun, 20 Oct 2024 17:22:50 -0400 DKIM-Signature: v=1; a=ed25519-sha256; q=dns/txt; c=relaxed/relaxed; d=russelstein.xyz; s=ed25519; h=Content-Type:MIME-Version:Message-ID:Date: Subject:To:From:Sender:Reply-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-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Jv1Q3ggmq4d6C+AuYMjgVyp8DXl5ypr+PB0KXqv5tQw=; b=sZT/NfV76GML3ye/LHIRo8LuQE Blwi3GQ5q7CODbGGn8yFtcPXvoOgKl9R7/WP6lqqtAgwQfI+QqWQkY8/1nAA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=russelstein.xyz; s=rsa; h=Content-Type:MIME-Version:Message-ID:Date:Subject :To:From:Sender:Reply-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-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Jv1Q3ggmq4d6C+AuYMjgVyp8DXl5ypr+PB0KXqv5tQw=; b=Atc+PExF//LZSWoME8n9dxpECu 891MdEZaRvmKtqeX4lPz9ey+cM2Jy+zbPrFfj7GKpJJn6Ka4XKQ23PKp+YvzygjtmlP6mXZq1oREK DwsMNJWxJpR1sntbGqTen+gn2wLkE23DElr8NA7spyMJbWxPNo1ZyYxZhnry87b13+/lNc4NgGlnY kfsbrfROqh0xry6DbF5QgMhTtuYxTfUvrOe5Gb7iPNxjomQMRvCRNIW7gzOYFmsJMVPIanhSRDLn9 L8vkSZYxfUI7FBSW2QY+jIi7B/QR+Y9QVKS5TXmBNn0JwWKS7XQBEWsf3TgVx60wm9nO4Ruh9/Y7Y 1xwMFnlyOiCnR11+BVxOXQnha3FTZG3Cy/VMENorE3arxuZYDrCpW+zQQpHfJdpNRcFBT5C0iG5/g 8EeFXuw4EWPjQNi2wAJdT1ppuFNBMRoJa+zmsi0xMmWGSbyTQtU/nh6TOo0OJjBJ5xo0iMqQwMI8Q olhNEewpqU2VDvzhhP5PdGGGI9pyWNoB2IzLqEjn8gds9cgpITnbwUXdPqKaoKhbf3YonUc4qQ+4P PqDxEcwkFt1aPWDnZCtdSPuB7Hh7iEciruR2YZ3rjXjoxlTUyCwBAuO1OCvMQYw524CFeOiMdcWcV SorzIms/e0Si4M6bA0bOFdDGSPN9aAn5FnHNaFOf4=; Received: by russelstein.xyz with esmtpsa (TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.98) (envelope-from ) id 1t2dNr-000000001Aj-1J4R for guix-patches@gnu.org; Sun, 20 Oct 2024 16:22:44 -0500 Date: Sun, 20 Oct 2024 16:22:00 -0500 Message-ID: <871q0albbb.fsf@russelstein.xyz> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Received-SPF: pass client-ip=2605:6400:20:11e::1; envelope-from=reepca@russelstein.xyz; helo=mailout.russelstein.xyz 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: Mon, 21 Oct 2024 00:40:08 -0400 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: , Reply-to: Reepca Russelstein X-ACL-Warn: , Reepca Russelstein via Guix-patches X-Patchwork-Original-From: Reepca Russelstein via Guix-patches via From: Reepca Russelstein 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 For a very long time, guix-daemon has helpfully made the outputs of failed derivation builds available at the same location they were at in the build container (see https://git.savannah.gnu.org/cgit/guix.git/tree/nix/libstore/build.cc?id=e951a375a01262dfd470ee343baf7c41dbc6ff58#n1371). This has proven quite useful for debugging of various packages, but unfortunately it is implemented by a plain "rename" of the top-level store items from the chroot's store to the real store. This does not change the permissions or ownership of these files, which allows a setuid / setgid binary created by a malicious build to become exposed to the rest of the users, who can then use it to gain control over that build user. They can exploit this control to overwrite the output of any builds run by that user using /proc/PID/fd and SIGSTOP. Also, there is a window of time for /successful/ build outputs between when they are moved from the chroot and when their permissions are canonicalized, which likewise allows for setuid / setgid binaries to be exposed to other users (see https://git.savannah.gnu.org/cgit/guix.git/tree/nix/libstore/build.cc?id=e951a375a01262dfd470ee343baf7c41dbc6ff58#n2343). The first patch fixes the former, the second patch fixes the latter. We then need to update the guix package to use these new commits, which I leave to whoever applies this to do since my local repository is in a rather unclean state and a fresh work tree may take some time to be ready to run 'make update-guix-package'. From d096d653cc69118e05f49247ab312d0096b16656 Mon Sep 17 00:00:00 2001 Message-ID: In-Reply-To: References: From: Reepca Russelstein Date: Sun, 20 Oct 2024 15:39:02 -0500 Subject: [PATCH 2/2] nix: build: sanitize successful build outputs prior to exposing them. There is currently a window of time between when the build outputs are exposed and when their metadata is canonicalized. * nix/libstore/build.cc (DerivationGoal::registerOutputs): wait until after metadata canonicalization to move successful build outputs to the store. Change-Id: Ia995136f3f965eaf7b0e1d92af964b816f3fb276 --- nix/libstore/build.cc | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc index 67ebfe2f14..43a8a37184 100644 --- a/nix/libstore/build.cc +++ b/nix/libstore/build.cc @@ -2369,15 +2369,6 @@ void DerivationGoal::registerOutputs() Path actualPath = path; if (useChroot) { actualPath = chrootRootDir + path; - if (pathExists(actualPath)) { - /* Move output paths from the chroot to the store. */ - if (buildMode == bmRepair) - replaceValidPath(path, actualPath); - else - if (buildMode != bmCheck && rename(actualPath.c_str(), path.c_str()) == -1) - throw SysError(format("moving build output `%1%' from the chroot to the store") % path); - } - if (buildMode != bmCheck) actualPath = path; } else { Path redirected = redirectedOutputs[path]; if (buildMode == bmRepair @@ -2463,6 +2454,20 @@ void DerivationGoal::registerOutputs() canonicalisePathMetaData(actualPath, buildUser.enabled() && !rewritten ? buildUser.getUID() : -1, inodesSeen); + if (useChroot) { + if (pathExists(actualPath)) { + /* Now that output paths have been canonicalized (in particular + there are no setuid files left), move them outside of the + chroot and to the store. */ + if (buildMode == bmRepair) + replaceValidPath(path, actualPath); + else + if (buildMode != bmCheck && rename(actualPath.c_str(), path.c_str()) == -1) + throw SysError(format("moving build output `%1%' from the chroot to the store") % path); + } + if (buildMode != bmCheck) actualPath = path; + } + /* For this output path, find the references to other paths contained in it. Compute the SHA-256 NAR hash at the same time. The hash is stored in the database so that we can -- 2.45.2