From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id KIpRAX9Z8GWwEAAAqHPOHw:P1 (envelope-from ) for ; Tue, 12 Mar 2024 14:32:47 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id KIpRAX9Z8GWwEAAAqHPOHw (envelope-from ) for ; Tue, 12 Mar 2024 14:32:47 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b="JFZl/t5V"; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gnu.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1710250366; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:resent-cc:resent-from:resent-sender: resent-message-id:in-reply-to:in-reply-to:references:references: list-id:list-help:list-unsubscribe:list-subscribe:list-post: dkim-signature; bh=ApG/gHDiTE1k3aAxBiJqo9n/srVBwLyCkr8lEWPgc+U=; b=c/r4qhi45+5pU+s+aiujfuHZ5JdZiCDq+ntxtJXD32xCxK8XsK19KQbMSdumOPiZN/DlcW PJq8lifeACYOD0VG59V6WG/vWkKIsO/jQ9F21c3o4oIwB/Oq60YcIEdIiV40xVZ5VupRqS FfYA4nvVgkwfP8LgEznvj/KptFHYoznkNFBn6cIxvRikJeyxLdD3Jl+pie5U2xGuR/idsa WSHxMTnmL/kbYZPxkhqPBNOGlvy8TKTBwfeY1IY3y1hNy/98xV8tw0NJtVaJL7xebuo+w/ bP6n1AqCXCzFWgY32u4MkK3GDgJGC5xA3PANnCqlTT/sUwYMT0aB4GnvJNcSWg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b="JFZl/t5V"; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gnu.org ARC-Seal: i=1; s=key1; d=yhetil.org; t=1710250366; a=rsa-sha256; cv=none; b=CcU7rVKfwPjncBltYeT/TvnzhF+7IWGP26N5r6l73S8B2SdldjQ5JJmDue3jqvSUitoRgv Z1DYKwJqWILeMZuM/PkfPuCsSxJWTr3P0LqT+JXRwxb9j122HDqSgt6dYABflf6FjxyJZ1 n3deMcFfufJo3XZD7q0x8y7Y+FUaL+7xyJKgVZPq6GoWIGQRUqWiOvzAWkO3LTU2SWgmoF BKbhvrI2GE0tV3XVcTLz5NB5ecNuloz+eInAt/XpSbg0F5hbjbv+WZDefoFTqXcxzRiaVi RVMwojtyjXa8l5Fmaf1SFL8SI6SHP3Op6z4azkBRJvGCi9zZGs4BsFTJTeJyDQ== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id BDA8762A26 for ; Tue, 12 Mar 2024 14:32:46 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rk2Ep-0007mn-Ab; Tue, 12 Mar 2024 09:32:15 -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 1rk2E4-0007dR-0k for guix-patches@gnu.org; Tue, 12 Mar 2024 09:31:48 -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 1rk2E3-0001r4-Oe for guix-patches@gnu.org; Tue, 12 Mar 2024 09:31:27 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rk2Ec-0005xg-5m for guix-patches@gnu.org; Tue, 12 Mar 2024 09:32:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#69728] [PATCH security] daemon: Protect against FD escape when building fixed-output derivations (CVE-2024-27297). Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 12 Mar 2024 13:32:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69728 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch security To: 69728@debbugs.gnu.org Cc: Picnoir , =?UTF-8?Q?Th=C3=A9ophane?= Hufschmitt , guix-security@gnu.org Received: via spool by 69728-submit@debbugs.gnu.org id=B69728.171025031122900 (code B ref 69728); Tue, 12 Mar 2024 13:32:02 +0000 Received: (at 69728) by debbugs.gnu.org; 12 Mar 2024 13:31:51 +0000 Received: from localhost ([127.0.0.1]:42083 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rk2ER-0005xH-0V for submit@debbugs.gnu.org; Tue, 12 Mar 2024 09:31:51 -0400 Received: from eggs.gnu.org ([209.51.188.92]:37266) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rk2EO-0005x4-5l for 69728@debbugs.gnu.org; Tue, 12 Mar 2024 09:31:49 -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 1rk2Di-0001n0-Bp; Tue, 12 Mar 2024 09:31:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:References:In-Reply-To:Subject:To: From; bh=ApG/gHDiTE1k3aAxBiJqo9n/srVBwLyCkr8lEWPgc+U=; b=JFZl/t5Vs1DXkerv0xqF KHOBGrygJPO1XFkcYKiLo52Pk7fFXB8szXqxdoNZfwWNTbW1rFKKGxb0LVlhakNZeVhvg4R8UsI9N X0QG0oWQZiPl8u5bq0xdyJGybOqSX4EUVnE0l2xH5NCNrO77leB6I/y/eNU9riOyStag+Q5VPTpgf mS6HSUsaNKKihcSluQN5TJ6Dt+dCYI4eWD+7tq/995f+rbKnqnhIEoU9TYMFMGUrucHG2h4FYqT+c vaR64BWbvo6VAXMsQ4uMhqBPyxF5j0AkxrGX/oqvEQKlodDxyzOhFxJHT7QpMzpyac7QZ+M7Tc1KZ R22Hb6onELQ1wg==; From: Ludovic =?UTF-8?Q?Court=C3=A8s?= In-Reply-To: <87frwwo1mo.fsf@gnu.org> ("Ludovic =?UTF-8?Q?Court=C3=A8s?="'s message of "Mon, 11 Mar 2024 23:16:31 +0100") References: <87frwwo1mo.fsf@gnu.org> Date: Tue, 12 Mar 2024 14:31:00 +0100 Message-ID: <87ttlblgq3.fsf_-_@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" 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+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -6.19 X-Spam-Score: -6.19 X-Migadu-Queue-Id: BDA8762A26 X-Migadu-Scanner: mx13.migadu.com X-TUID: BNRWcz4ikuj7 --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello, Ludovic Court=C3=A8s skribis: > Pushed (with a slightly different commit message) as > 8f4ffb3fae133bb21d7991e97c2f19a7108b1143. > > Updated the =E2=80=98guix=E2=80=99 package in b8954a7faeccae11c32add7cd0f= 408d139af3a43: > Guix System users can now reconfigure! > > Added a news entry in 4003c60abf7a6e59e47cc2deb9eef2f104ebb994. It turns out that the previous fix was incomplete due to a mistake of mine. I pushed ff1251de0bc327ec478fc66a562430fbf35aef42 to address that (patch attached for clarity). Commit 30a8de0bcdadfb55cbcaa34760527c1b767808c7 updates the =E2=80=98guix= =E2=80=99 package again. Now is the time to reconfigure. I=E2=80=99ll send a reproducer in a separa= te message. Apologies for the mishap. Ludo=E2=80=99. --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable commit ff1251de0bc327ec478fc66a562430fbf35aef42 Author: Ludovic Court=C3=A8s Date: Tue Mar 12 11:53:35 2024 +0100 daemon: Address shortcoming in previous security fix for CVE-2024-27297. =20=20=20=20 This is a followup to 8f4ffb3fae133bb21d7991e97c2f19a7108b1143. =20=20=20=20 Commit 8f4ffb3fae133bb21d7991e97c2f19a7108b1143 fell short in two ways: (1) it didn=E2=80=99t have any effet for fixed-output derivations performed in a chroot, which is the case for all of them except those using =E2=80=9Cbuiltin:download=E2=80=9D and =E2=80=9Cbuiltin:git-downl= oad=E2=80=9D, and (2) it did not preserve ownership when copying, leading to =E2=80=9Csuspicious ownersh= ip or permission [=E2=80=A6] rejecting this build output=E2=80=9D errors. =20=20=20=20 * nix/libstore/build.cc (DerivationGoal::buildDone): Account for =E2=80=98chrootRootDir=E2=80=99 when copying =E2=80=98drv.outputs=E2=80= =99. * nix/libutil/util.cc (copyFileRecursively): Add =E2=80=98fchown=E2=80= =99 and =E2=80=98fchownat=E2=80=99 calls to preserve file ownership; this is necessary for chrooted fixed-output derivation builds. * nix/libutil/util.hh: Update comment. =20=20=20=20 Change-Id: Ib59f040e98fed59d1af81d724b874b592cbef156 diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc index e2adee118b..d23c0944a4 100644 --- a/nix/libstore/build.cc +++ b/nix/libstore/build.cc @@ -1387,13 +1387,14 @@ void DerivationGoal::buildDone() make sure that there's no stale file descriptor pointing to= it (CVE-2024-27297). */ foreach (DerivationOutputs::iterator, i, drv.outputs) { - if (pathExists(i->second.path)) { - Path pivot =3D i->second.path + ".tmp"; - copyFileRecursively(i->second.path, pivot, true); - int err =3D rename(pivot.c_str(), i->second.path.c_str()); + Path output =3D chrootRootDir + i->second.path; + if (pathExists(output)) { + Path pivot =3D output + ".tmp"; + copyFileRecursively(output, pivot, true); + int err =3D rename(pivot.c_str(), output.c_str()); if (err !=3D 0) throw SysError(format("renaming `%1%' to `%2%'") - % pivot % i->second.path); + % pivot % output); } } } diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc index 493f06f357..578d657293 100644 --- a/nix/libutil/util.cc +++ b/nix/libutil/util.cc @@ -422,6 +422,7 @@ static void copyFileRecursively(int sourceroot, const P= ath &source, if (destinationFd =3D=3D -1) throw SysError(format("opening `%1%'") % sou= rce); =20 copyFile(sourceFd, destinationFd); + fchown(destinationFd, st.st_uid, st.st_gid); } else if (S_ISLNK(st.st_mode)) { char target[st.st_size + 1]; ssize_t result =3D readlinkat(sourceroot, source.c_str(), target, st.st_s= ize); @@ -430,6 +431,8 @@ static void copyFileRecursively(int sourceroot, const P= ath &source, int err =3D symlinkat(target, destinationroot, destination.c_str()); if (err !=3D 0) throw SysError(format("creating symlink `%1%'") % destination); + fchownat(destinationroot, destination.c_str(), + st.st_uid, st.st_gid, AT_SYMLINK_NOFOLLOW); } else if (S_ISDIR(st.st_mode)) { int err =3D mkdirat(destinationroot, destination.c_str(), 0755); if (err !=3D 0) @@ -455,6 +458,7 @@ static void copyFileRecursively(int sourceroot, const P= ath &source, for (auto & i : readDirectory(sourceFd)) copyFileRecursively((int)sourceFd, i.name, (int)destinationFd, i.name, deleteSource); + fchown(destinationFd, st.st_uid, st.st_gid); } else throw Error(format("refusing to copy irregular file `%1%'") % s= ource); =20 if (deleteSource) diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh index 058f5f8446..377aac0684 100644 --- a/nix/libutil/util.hh +++ b/nix/libutil/util.hh @@ -102,9 +102,10 @@ void deletePath(const Path & path); void deletePath(const Path & path, unsigned long long & bytesFreed, size_t linkThreshold =3D 1); =20 -/* Copy SOURCE to DESTINATION, recursively. Throw if SOURCE contains a fi= le - that is not a regular file, symlink, or directory. When DELETESOURCE is - true, delete source files once they have been copied. */ +/* Copy SOURCE to DESTINATION, recursively, preserving ownership. Throw if + SOURCE contains a file that is not a regular file, symlink, or director= y. + When DELETESOURCE is true, delete source files once they have been + copied. */ void copyFileRecursively(const Path &source, const Path &destination, bool deleteSource =3D false); =20 --=-=-=--