From patchwork Thu Oct 27 03:50:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 44148 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 9E82527BBEB; Thu, 27 Oct 2022 04:54:03 +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=-3.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,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 6F94727BBEA for ; Thu, 27 Oct 2022 04:54:02 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ontwJ-0003IT-M1; Wed, 26 Oct 2022 23:52:20 -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 1ontw5-0002ir-S9 for guix-patches@gnu.org; Wed, 26 Oct 2022 23:52:08 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ontw2-0001Qb-OB for guix-patches@gnu.org; Wed, 26 Oct 2022 23:52:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ontw2-00029h-Jh for guix-patches@gnu.org; Wed, 26 Oct 2022 23:52:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#58812] [PATCH 3/5] install: Validate symlink target in evaluate-populate-directive. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 27 Oct 2022 03:52:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58812 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 58812@debbugs.gnu.org Cc: Maxim Cournoyer Received: via spool by 58812-submit@debbugs.gnu.org id=B58812.16668426938208 (code B ref 58812); Thu, 27 Oct 2022 03:52:02 +0000 Received: (at 58812) by debbugs.gnu.org; 27 Oct 2022 03:51:33 +0000 Received: from localhost ([127.0.0.1]:55705 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ontvY-00028K-UQ for submit@debbugs.gnu.org; Wed, 26 Oct 2022 23:51:33 -0400 Received: from mail-qt1-f169.google.com ([209.85.160.169]:45775) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ontvX-00027y-Et for 58812@debbugs.gnu.org; Wed, 26 Oct 2022 23:51:32 -0400 Received: by mail-qt1-f169.google.com with SMTP id x3so301684qtj.12 for <58812@debbugs.gnu.org>; Wed, 26 Oct 2022 20:51:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lBkcrXstesL/RYcBdJo1quwLH1W3JOZ/ebtnOaJ5hnA=; b=HbVKAMNnEv1jYGZg/E3/OmgRuheAVsklxR0zKZw5pvwV/WTRHugqNghRWBxafc4Gdt 8f0weOchKjwLUu8hyTi2BTbE2j+nRqZHG/1NLdwFkpnoQF7w6lDnOE55E5E6diQA8xuR KRyzvIIXjqNoPBnzyJKbLNB5nsrzgqzmHof5jDbhRl0y+IuDyEXW27pwLKtw+armSiL5 dH5RbxZaJ2utBGPzsiXPgTCGfW3C2mLT7cL4adMNa51ZLwxfJEwV9aYrLnyAudRmrTgn KdvHBZX2vAUn1hoH3FBwsi240cqWrWmuFf2GgaltftNM6gT42FIwk29MPr8chVsu9j/w Xkmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lBkcrXstesL/RYcBdJo1quwLH1W3JOZ/ebtnOaJ5hnA=; b=pl+2yK4eARYNOwLUg1V/N1/rNr2qIKtUUs3KnfBTas3kEFpg5UGjahmGMXY8MAFvC6 F0r+GIVn9Ijbc4MYkNiMU6gcn1PBN0RV6PXDtp1Pqg8t0toBEBJoWi1+DWJRjWVnwfIE McXkPAKX0vWYNYs/seCifU8xRlxPH1RMzDxugx/7SIbSUClnSA+eOvpTVPDCoqTN9SBv yuHSkhX7uRZHCMaafvdsBJ3rFWM5+BkB+BwILqU2NBc6tJISi0x+9YQW5zkaHCOPzD6/ teVnonxGNGq5xfLagw3cSGW2sJvJAzctURBL7ftdyI4nJiSoHPKGPCTKFpP+UU3U/KLu i3bA== X-Gm-Message-State: ACrzQf3Iqj78y1NhUTv+6vD9LJ+7lW4C6E/uSB5pmPAmNiGcTknoOcsY zW4hIztaxdCAZgRBc+hbababW3Lf128= X-Google-Smtp-Source: AMsMyM5kviHnp/nHdY7YhkXb4eQqfKvE6v2znuY6182uuRdSEfMdy8spS1iEjqrZz1VpSCzS/Jw1yQ== X-Received: by 2002:a05:622a:512:b0:39a:d23a:431a with SMTP id l18-20020a05622a051200b0039ad23a431amr39392656qtx.370.1666842685640; Wed, 26 Oct 2022 20:51:25 -0700 (PDT) Received: from localhost.localdomain (dsl-152-21.b2b2c.ca. [66.158.152.21]) by smtp.gmail.com with ESMTPSA id x1-20020a05620a448100b006ec771d8f89sm255488qkp.112.2022.10.26.20.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Oct 2022 20:51:25 -0700 (PDT) From: Maxim Cournoyer Date: Wed, 26 Oct 2022 23:50:58 -0400 Message-Id: <20221027035100.28852-3-maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221027035100.28852-1-maxim.cournoyer@gmail.com> References: <20221027035100.28852-1-maxim.cournoyer@gmail.com> 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: , Sender: "Guix-patches" Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/build/install.scm (evaluate-populate-directive): By default, error when the target of a symlink doesn't exist. Always ensure TARGET ends with "/". (populate-root-file-system): Call evaluate-populate-directive with #:error-on-dangling-symlink #t and add comment. --- gnu/build/install.scm | 60 ++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/gnu/build/install.scm b/gnu/build/install.scm index f5c8407b89..15cc29b2c8 100644 --- a/gnu/build/install.scm +++ b/gnu/build/install.scm @@ -1,6 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès ;;; Copyright © 2016 Chris Marusich +;;; Copyright © 2022 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -56,19 +57,24 @@ (define (install-boot-config bootcfg bootcfg-location mount-point) (define* (evaluate-populate-directive directive target #:key (default-gid 0) - (default-uid 0)) + (default-uid 0) + (error-on-dangling-symlink? #t)) "Evaluate DIRECTIVE, an sexp describing a file or directory to create under directory TARGET. DEFAULT-UID and DEFAULT-GID are the default UID and GID in the context of the caller. If the directive matches those defaults then, -'chown' won't be run." +'chown' won't be run. When ERROR-ON-DANGLING-SYMLINK? is true, abort with an +error when a dangling symlink would be created." + (define target* (if (string-suffix? "/" target) + target + (string-append target "/"))) (let loop ((directive directive)) (catch 'system-error (lambda () (match directive (('directory name) - (mkdir-p (string-append target name))) + (mkdir-p (string-append target* name))) (('directory name uid gid) - (let ((dir (string-append target name))) + (let ((dir (string-append target* name))) (mkdir-p dir) ;; If called from a context without "root" permissions, "chown" ;; to root will fail. In that case, do not try to run "chown" @@ -78,27 +84,38 @@ (define* (evaluate-populate-directive directive target (chown dir uid gid)))) (('directory name uid gid mode) (loop `(directory ,name ,uid ,gid)) - (chmod (string-append target name) mode)) + (chmod (string-append target* name) mode)) (('file name) - (call-with-output-file (string-append target name) + (call-with-output-file (string-append target* name) (const #t))) (('file name (? string? content)) - (call-with-output-file (string-append target name) + (call-with-output-file (string-append target* name) (lambda (port) (display content port)))) ((new '-> old) - (let try () - (catch 'system-error - (lambda () - (symlink old (string-append target new))) - (lambda args - ;; When doing 'guix system init' on the current '/', some - ;; symlinks may already exists. Override them. - (if (= EEXIST (system-error-errno args)) - (begin - (delete-file (string-append target new)) - (try)) - (apply throw args)))))))) + (let ((new* (string-append target* new))) + (let try () + (catch 'system-error + (lambda () + (when error-on-dangling-symlink? + ;; When the symbolic link points to a relative path, + ;; checking if its target exists must be done relative to + ;; the link location. + (with-directory-excursion (if (string-prefix? "/" old) + (getcwd) + (dirname new*)) ;relative + (unless (file-exists? old) + (error (format #f "symlink `~a' points to nonexistent \ +file `~a'" new* old))))) + (symlink old new*)) + (lambda args + ;; When doing 'guix system init' on the current '/', some + ;; symlinks may already exists. Override them. + (if (= EEXIST (system-error-errno args)) + (begin + (delete-file new*) + (try)) + (apply throw args))))))))) (lambda args ;; Usually we can only get here when installing to an existing root, ;; as with 'guix system init foo.scm /'. @@ -142,7 +159,10 @@ (define* (populate-root-file-system system target includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM. EXTRAS is a list of directives appended to the built-in directives to populate TARGET." - (for-each (cut evaluate-populate-directive <> target) + ;; It's expected that some symbolic link targets do not exist yet, so do not + ;; error on dangling links. + (for-each (cut evaluate-populate-directive <> target + #:error-on-dangling-symlink? #f) (append (directives (%store-directory)) extras)) ;; Add system generation 1.