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 KGbGLbchbWO1mwAAbAwnHQ (envelope-from ) for ; Thu, 10 Nov 2022 17:07:19 +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 mGTfLbchbWOnWQAAauVa8A (envelope-from ) for ; Thu, 10 Nov 2022 17:07:19 +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 4E3E49142 for ; Thu, 10 Nov 2022 17:07:19 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otA58-0002xn-JZ; Thu, 10 Nov 2022 11:07:11 -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 1otA51-0002u1-AS for guix-patches@gnu.org; Thu, 10 Nov 2022 11:07: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 1otA51-0003CE-2U for guix-patches@gnu.org; Thu, 10 Nov 2022 11:07:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1otA50-0000Yc-Tj for guix-patches@gnu.org; Thu, 10 Nov 2022 11:07:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#58812] [PATCH v3 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 16:07: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.16680963772033 (code B ref 58812); Thu, 10 Nov 2022 16:07:02 +0000 Received: (at 58812) by debbugs.gnu.org; 10 Nov 2022 16:06:17 +0000 Received: from localhost ([127.0.0.1]:44045 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otA4H-0000Wi-BZ for submit@debbugs.gnu.org; Thu, 10 Nov 2022 11:06:17 -0500 Received: from mail-qt1-f181.google.com ([209.85.160.181]:34561) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otA4F-0000WC-Ke for 58812@debbugs.gnu.org; Thu, 10 Nov 2022 11:06:16 -0500 Received: by mail-qt1-f181.google.com with SMTP id e15so1174921qts.1 for <58812@debbugs.gnu.org>; Thu, 10 Nov 2022 08:06:15 -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=ppiVJj8OjcamSEDw8VrTouNF+kWeQAzWkkdGuBuszLhux9+uPX4aVuv00hEbB+uQDp 7RoU09nD8SSGgYNpXW2H3SUQuEOFxcTkGkTHBtP9jjs4lwUrutzScGGjYuGUKUVBfoBr pqVb5PY661Gu2y9OA7LrsRviltzjQosOFTxL1eL52bsKaLBGA1CPcBVlOOz3te8a1z9x AkSMszeOp8qAtn0aqmKjOgnlCgCamDblqcUA9Y045Z3o+ZbYU8h6e24YeUbkqFp88e9y wn/zZ8sArdEsewsvt05kXhe5JGRii9fksAL9efEzmiKxzNow8YTlTGn+cHMhhvR1JBfC DKlg== 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=dUu9DA/erGVzPgB0VlUfsVThCfbFT5sObQdmk78adJ4npbhLRr0y6gHXrIw7GDEUoz xci7y9xz9xPt8bD60sTXwG1xAbZAY0eYCwLGRaSrUIeVTWv5rNIXusDfd0TShutgDpNJ MwStE1iSgb/MjfClB0UNC/QEP/Zuwi5R7WtvEo4hJpSdH5yzhNJvmx1El6U5BBQ1p38B AFuHtblRyPclAatGixbMOC9GJ66O6j2AYevhiklFWm6P3Phf6kwDw7Jja5Mcz2cHoQoP vcKhGPRp3bapGgCiZHp9mWibVnGJ3HZB1kTwUC04TlPh+10d2qg+3C4TiQJUVYUYgmkj umTw== X-Gm-Message-State: ANoB5pkVjwVcFkplAdREbvlVIEGPlKB5fRGoVBy9EVRoI10xXTCY5g2S 9oaDy0ZMOJ2pMZFT+DwC1U46EzpAVKI= X-Google-Smtp-Source: AA0mqf43uru6AFvMrE0DdVpO4DVqAkfu1kQYydlnoemQPib17jc5v8xzUekZAo3BKQ2/kR0owG8tzA== X-Received: by 2002:ac8:7755:0:b0:3a5:925a:b33a with SMTP id g21-20020ac87755000000b003a5925ab33amr15592955qtu.69.1668096368522; Thu, 10 Nov 2022 08:06:08 -0800 (PST) Received: from localhost.localdomain ([2607:fad8:4:3::1001]) by smtp.gmail.com with ESMTPSA id t18-20020a37ea12000000b006fafc111b12sm6344916qkj.83.2022.11.10.08.06.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 08:06:08 -0800 (PST) From: Maxim Cournoyer Date: Thu, 10 Nov 2022 11:05:48 -0500 Message-Id: <20221110160550.4366-2-maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221110160550.4366-1-maxim.cournoyer@gmail.com> References: <87sfiqhp1u.fsf@gnu.org> <20221110160550.4366-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: , 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=1668096439; 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=DKpVzsR5z6Xw09vH0zcMc5x06f+1+YdTgG1vKBN9TZvlWxlpOPOPMWk/LlyqgHoNPOOwQP h6FDPGTvt00wLlB2P5QEPNK1smklAQbARAglDxGEhpXyGaFo/UW164ijT/+Zt8ekjFPpNm oUE1LVeHBFl8AK1PzSk+OiR0zwoJY3KBZUxcJYdGkQFlHAvIGnseIVZXvpL9on5XNQ+jYr avlF6Alq/fdOfgB/XFLx0n4c4TXcOkNU+pIzbnQzHtmRqcLbcwXe3EKSJ9DJnV+4jCwgCx QROHwHSQkCaDM7fYnPhWmFZrmGiMtHSCFMyxmkqK41EbJo5YeG2FGxrEoJVt7A== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1668096439; a=rsa-sha256; cv=none; b=ckROYXD8fBBMnwikHeiRO6V70EgOqsAfW2s4SgTfX3Rk9fWw2jWi0hWRAzNpTrIe+dzHaW AYpXtk+w4EIa0umftSd/uBbzWWd6SXH4Sy9wAFi6+YLkyS7j8fAPzE/hff6g7m0QonODBm r6/7c89qJOrLxk+aEreoaPRbgM5WQyloySD6KErWAHUkVrvSg1dw7IDRq3oYK5Ma4LoIb7 3gk7UxPguSntOhTHUsi0eflP5/rYze2hdoF0o+R2CipIDEhLIYQmD+WFmnKg1vSuIw5qiw WLXdMaPInBMQhbt+QmTv3XCm03KNtlOg65lQs8EI74BcLDpf21bsmvuAWkw6VQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=ppiVJj8O; 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.48 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=ppiVJj8O; 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: 4E3E49142 X-Spam-Score: 7.48 X-Migadu-Scanner: scn1.migadu.com X-TUID: sF92NksoAyGd * 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