From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id didsKjF9bGOiYAEAbAwnHQ (envelope-from ) for ; Thu, 10 Nov 2022 05:25:21 +0100 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id OAa2KTF9bGMuIwAAauVa8A (envelope-from ) for ; Thu, 10 Nov 2022 05:25:21 +0100 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 74E7220C0F for ; Thu, 10 Nov 2022 05:25:17 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1osz7h-0004Ai-99; Wed, 09 Nov 2022 23:25:05 -0500 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 1osz7f-0004AE-Fr for guix-patches@gnu.org; Wed, 09 Nov 2022 23:25:03 -0500 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 1osz7f-0006zf-4u for guix-patches@gnu.org; Wed, 09 Nov 2022 23:25:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1osz7e-00054L-OH for guix-patches@gnu.org; Wed, 09 Nov 2022 23:25:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#59161] [PATCH v2 2/4] 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, 10 Nov 2022 04:25:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 59161 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 59161@debbugs.gnu.org Cc: Maxim Cournoyer X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.166805425619384 (code B ref -1); Thu, 10 Nov 2022 04:25:02 +0000 Received: (at submit) by debbugs.gnu.org; 10 Nov 2022 04:24:16 +0000 Received: from localhost ([127.0.0.1]:41551 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1osz6t-00052Y-8Y for submit@debbugs.gnu.org; Wed, 09 Nov 2022 23:24:15 -0500 Received: from lists.gnu.org ([209.51.188.17]:52086) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1osz6r-00052P-Id for submit@debbugs.gnu.org; Wed, 09 Nov 2022 23:24:14 -0500 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 1osz6p-00046n-JJ for guix-patches@gnu.org; Wed, 09 Nov 2022 23:24:13 -0500 Received: from mail-qt1-x829.google.com ([2607:f8b0:4864:20::829]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1osz6n-0006mG-Tk for guix-patches@gnu.org; Wed, 09 Nov 2022 23:24:11 -0500 Received: by mail-qt1-x829.google.com with SMTP id h21so403703qtu.2 for ; Wed, 09 Nov 2022 20:24:09 -0800 (PST) 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=zrkjGIBtzayhHZQrYnnG/gG6ymHR3J6x5PRzXc/KVf4=; b=D0MycE7m4wtArYzszwaInPlmUkdFGQ2Yj/DGY4R/RWLun8TcZRVVYiqi3SnQx6KxLE ldSqwNXsMTfUQGUDWw8jrbFRR39fumxyEwbls6FZfrHAXO2yQhbeS1AaaFTWeP/0+Kpl co5h0AYhIkRmB3CGBKER6OpNPBRMUfArI3nImKBiqs39k4dRSb4ZrJkOh24IUXh+Vgc+ 2aEisOXJy5wU33On5fxQbsrBl1gMCDEKqA0lfsPTVIB/ud0We6tIkbgNVWShIHCpWIFJ kT/ZfC8XZSLA1Qg+cjz6Gvx4B5BaZjRdazenG8bkWBdaf0WcRt7ZSveA9yeBp5SL9ZFl I20g== 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=zrkjGIBtzayhHZQrYnnG/gG6ymHR3J6x5PRzXc/KVf4=; b=XuCFf0LA/ptdc7nr1991hooGTN4z5kq8wXgV7w24s5mETmKlIRPpA1ShIndzoxZ7uk vUhQO4PlLK8O/ZJt5N0WoDBR1i7kiwnTG6RaeIRfXOU8ocNw2wSrijlQlNDdSI8wHly5 mFzv2s2pvlHCZd97sZRolAW1by7PEZxz55xnhgTFDzFxhY2MFGo4eqwY5ksNHsmVLjxh bawQSaEijhN8KS7MV5L6McQS5Nl2/56pS0OTWNCD7Fa7BaW5FJ0yAoy5u63WXIOu0dI0 Xf0zC2foJke//TWOD8s3o8dF6/sKLlSd953iZanHEiIMEVOE5Ux31e8Gjbm+1b5VNgdf BaQQ== X-Gm-Message-State: ACrzQf1r2cKujfI5vayEcfez778VeSbVslwUGTmGjehpKAinVPFkQJ6e NzwzcayhqySzc+J7ZCAiNepnb/d+4ZI= X-Google-Smtp-Source: AMsMyM50jm9G7m0e3TdiaPj94AR1WbWsy6oyId3V1n86R/Pqxt76Z/kduwncSqYwJ6GNwDx1vopaLQ== X-Received: by 2002:ac8:6b45:0:b0:3a5:6416:a499 with SMTP id x5-20020ac86b45000000b003a56416a499mr24696824qts.263.1668054248667; Wed, 09 Nov 2022 20:24:08 -0800 (PST) Received: from localhost.localdomain ([2607:fad8:4:3::1001]) by smtp.gmail.com with ESMTPSA id r1-20020ac85e81000000b0039cc7ebf46bsm10515105qtx.93.2022.11.09.20.24.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 20:24:08 -0800 (PST) From: Maxim Cournoyer Date: Wed, 9 Nov 2022 23:23:49 -0500 Message-Id: <20221110042351.829-2-maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221110042351.829-1-maxim.cournoyer@gmail.com> References: <87h6z7omzy.fsf_-_@gnu.org> <20221110042351.829-1-maxim.cournoyer@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::829; envelope-from=maxim.cournoyer@gmail.com; helo=mail-qt1-x829.google.com 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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1668054317; 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=zrkjGIBtzayhHZQrYnnG/gG6ymHR3J6x5PRzXc/KVf4=; b=RB/Z1kwGR1opse4ponystgwYvoWZl7Pyudg2+8E1UASxxP/yiDt072K9EvioJDli5S/eoj I1PKy4SRx3ZZXx7xTwvT0LuyjNNCNGAX1wHfiLRuM/jIcEqG0CQ/Yg4TkcdN9xzPfINEKL U2jjJeftuCdZrNdqXG7iwKx8wgBuw1ueLaAbeGnVnC/+ipO5xbgGfq5kJv3CEVfeodfYhy t0f7BKu7Dgrg5B1djLj069xaWJaUTiaxUIQVlMKYWZriwZ0Jn6lHvlHWoUeiTDlN29T8SL acFsVVkgZ5YmdAFIRJToRS3TJs5t4AiApRC1ulZ/wIOYUcv58xn7TxzOsUL5yg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1668054317; a=rsa-sha256; cv=none; b=XyIi+5tvZtsFdqjY8N5d1JwtspiwEWP96MJL+VyQ/TR3eCg+wqdqkbWHHasoydQ1wHicxj D30hm5348eEpNj12muTW94n0MwtROTTbfiiYyHcUxqMdfK/rOEWCuTV+2cW8iM4614No0b XzZDA/F1ZCaWVsJeqnR9oBg0YFEQLRebevnuNS4mu9Ou7Cl21p0mYGXVWuKAZDymnUrpRn lp1n3/Ez1y8zZYiFhtVsp8N2rmXX2j04w/wfALoTRk8kdoJn7zGQHNjdcn2ZtdHgwVUWMl v29Tphr17KbJ8tHAXOk3RoTmkGo+BRvK2QEJxggN4aSeVN5svlhdR+tTKYb6Mw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=D0MycE7m; 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: 6.78 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=D0MycE7m; 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: 74E7220C0F X-Spam-Score: 6.78 X-Migadu-Scanner: scn0.migadu.com X-TUID: XAhKzsKvWNpD * 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..33a9616c0d 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 relatively + ;; to the link location. + (unless (if (string-prefix? "/" old) + (file-exists? old) + (with-directory-excursion (dirname new*) + (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