From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id kLxROdoAWmNZ6QAAbAwnHQ (envelope-from ) for ; Thu, 27 Oct 2022 05:54:03 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id ADssOdoAWmPl0wAA9RJhRA (envelope-from ) for ; Thu, 27 Oct 2022 05:54:02 +0200 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 655742ED0 for ; Thu, 27 Oct 2022 05:54:02 +0200 (CEST) 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 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1666842842; 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: content-transfer-encoding:content-transfer-encoding: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=lBkcrXstesL/RYcBdJo1quwLH1W3JOZ/ebtnOaJ5hnA=; b=miwbj/jlftb0YWMwS2VHO4gAqYrju/pQShHhx6pSK05vTS9LPme8UMjOgH5K8Mkcfv60eD K42ezdOL9M4ZkzpYtt4wIDuM5H4WNkFD1l8NPm78SSiClIb4TeG+4Ngy1Cu/RwvDqXdaCW 6k2ALA9GcLHG40u9WYitRDxStlYKqMvIu22QCYS997B57+IVkmvYmDZWSQ0cbWYtBtuQIU 14sR/vnPfmeFVaE1kjPqQW6126h0LkKq70PhmoI6jtkVgYUmqRJl3KtMFSGwGzI57knGCN zFOjI2bwukGnmpwhKslXhUZ+mx5TQv9PqCcofloq+9zFS4+s1Ohu/Wp66kpPAg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1666842842; a=rsa-sha256; cv=none; b=uH7uswHFyholq8JzJNq1cjBiWgBzBT9P2NkgEe9uy6EdTGcteud0kbq28vnHbEh3d5YFgo iWfhoirbGea4HBWwTOovPqGcO8NgsJNIUgElmIRXSYbPfPniET0GQh+CsmBwRX9ohf4afl eFAdJE6X6Xenvcx87/SUV2Yc+FyqtL8FkJoH0UyeWYqAKp3WCiyM0jf9ae50AO+GnOGJyl mhDq8bF3IpM1lAB+tetKgmsNnxqg/OAoAmYRigLxr10vB+mST5KJeqKuzNDqcATSMP2oM8 sKsYmHPdCJ0dQm48oRRh7NzjTugcYBImYnI6jG289ZAVQnYno3KIsUVzSxEDQg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=HbVKAMNn; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); 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" X-Migadu-Spam-Score: 7.58 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=HbVKAMNn; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); 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" X-Migadu-Queue-Id: 655742ED0 X-Spam-Score: 7.58 X-Migadu-Scanner: scn1.migadu.com X-TUID: Vt5UYxZXX9wY * 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. -- 2.37.3