From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id ANJBGd1CU2G2PAEAgWs5BA (envelope-from ) for ; Tue, 28 Sep 2021 18:29:17 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id 2E7sFN1CU2FnPgAAbx9fmQ (envelope-from ) for ; Tue, 28 Sep 2021 16:29:17 +0000 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 94BEB13AED for ; Tue, 28 Sep 2021 18:29:16 +0200 (CEST) Received: from localhost ([::1]:48164 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVFyl-0005C6-N0 for larch@yhetil.org; Tue, 28 Sep 2021 12:29:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35966) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVFyZ-00059z-UC for guix-patches@gnu.org; Tue, 28 Sep 2021 12:29:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:35259) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mVFyZ-0000Fe-9M for guix-patches@gnu.org; Tue, 28 Sep 2021 12:29:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mVFyZ-0005eu-6W; Tue, 28 Sep 2021 12:29:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#50814] [PATCH 5/5] guix: git-authenticate: Fix authenticate-repository. Resent-From: Attila Lendvai Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 28 Sep 2021 16:29:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50814 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 50814@debbugs.gnu.org Cc: Attila Lendvai Received: via spool by 50814-submit@debbugs.gnu.org id=B50814.163284648521642 (code B ref 50814); Tue, 28 Sep 2021 16:29:03 +0000 Received: (at 50814) by debbugs.gnu.org; 28 Sep 2021 16:28:05 +0000 Received: from localhost ([127.0.0.1]:46800 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mVFxc-0005cy-B6 for submit@debbugs.gnu.org; Tue, 28 Sep 2021 12:28:04 -0400 Received: from mail-ed1-f41.google.com ([209.85.208.41]:36655) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mVFxY-0005cD-GS for 50814@debbugs.gnu.org; Tue, 28 Sep 2021 12:28:02 -0400 Received: by mail-ed1-f41.google.com with SMTP id y35so34379300ede.3 for <50814@debbugs.gnu.org>; Tue, 28 Sep 2021 09:28:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K3gV4fd7UNLEYp1WYaFYa5vcbIbwtvEpYeiR+CxFaIY=; b=qGwXbsdTK0YeIShJIQJvxxn2CJZovHDAOTE9jz9xGn2aJNVA0gb6zR0isBV7/ug+Q6 NPpMJp3MaUa8FuqixBQm8/wpNsWVNafDPwEHsSbv6JN0Xt6z+N41p+W9klnDJviqem9E kpmeVNDIVKzO/GYjlpZnvTpT5W25BmR6ISgyZ+c+Pz4Bgqfx6VEGUyoLHkSUcyEQ8WNC qdqtbFC7pYMr4aOQcShpgRy/ojJiURfLH3LBhxkXOrm3uwxIUD020mPIYTx42Ox/I4cY aNLoRaFoQUP7qJKPTFcYXeTlElSh8uTjdVlp4CySnTXTSHCs2dlGcyI9wo3yduHYc8lE 2+0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=K3gV4fd7UNLEYp1WYaFYa5vcbIbwtvEpYeiR+CxFaIY=; b=Ju3S+yEzdHL90bDfZoK1W6/atcnDiFNJ9UrMl5zK1nOjtyhNQscKkLv20wH80G0RCz Qk238JNMw+gsGNcv09P8hnKIrl4Se5K8XWZM5B4pNI6u7csaC65UnpqjLph1H/LqsZNx JxO5I4b1DHmw/4+ib4zjZFMzYaNTcBnFLUJq+T9LnBMfgvjAsja2w//UUHrmqzc3v6m0 FRG1Jl+omlz2FwdhGatlNzX6FCp09/7J8lTjltJdM8/lQf/NR2Wh547kqa4krauykMTc 4RbreXU7sILPVtdE87xMMgZtTYijhSien48xRn8mi9VrWmkiySrdTcStvY3vPUpklZN5 HHLA== X-Gm-Message-State: AOAM532CtXHXJrB7UGVoRwETc9TvEhVGIckrhpNWgFYAF17JrVH3ZrO+ xwjKayMypGBlnox6P1AO+azIadq6CpY= X-Google-Smtp-Source: ABdhPJzXlVtjKkjrYVmpU/HVspvBWphxDTyKjx1wi5PCfOfV9O/YA8ssXLqYcX/ZWEN/iYVkAed+IA== X-Received: by 2002:aa7:cd41:: with SMTP id v1mr8494871edw.393.1632846473009; Tue, 28 Sep 2021 09:27:53 -0700 (PDT) Received: from lelap.lan (catv-213-222-131-28.catv.broadband.hu. [213.222.131.28]) by smtp.gmail.com with ESMTPSA id f4sm3147720ejq.125.2021.09.28.09.27.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 09:27:52 -0700 (PDT) From: Attila Lendvai Date: Tue, 28 Sep 2021 18:24:08 +0200 Message-Id: <20210928162406.27205-5-attila@lendvai.name> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210928162406.27205-1-attila@lendvai.name> References: <20210928162406.27205-1-attila@lendvai.name> MIME-Version: 1.0 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" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1632846556; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: 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=K3gV4fd7UNLEYp1WYaFYa5vcbIbwtvEpYeiR+CxFaIY=; b=aB9tI/g2rH0xIrVMKijSKruC7lrbDcEJjBd4IWrRLucfs08FjhJLdn+JuOBv/2ZWIUeSRC 8GujdP1VGobinsCGRlgjlJj28mTRY9q7QQ1i5oJBNMQm7hUdSL0d3gdZEPKFd4DzE6YWMh t5BDXfBHgoQV7g57O4bNwv0T18FVn1AujSEciFsaZYogefiQB1ewr1CNtDinrjujkyk29O C18LBdlBZpDKUxAHwtdZ3uFKUx/BmrcfHtqSjO6gHMCuF7eL0mJi+LTuyRpiZf5gNu94t+ wMLCs3avYqMRNSymZP7yaQ2Od8GYnnwOxCQroBwmSO8beJaoWf3qOn1TE0x+kw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1632846556; a=rsa-sha256; cv=none; b=BcFfi/ksWRpF+P+IvQw+x5lPEPNhy/n/c1gX7p5qUOocTv+sU2DYW+o8AwpPyOy6OY86lT devvhtl8i+bzhkOGfGwSq/u6vCf1chA/H4uFxxe/fk/Zd1sXlvIb9aLyFGRqEfooslkqaw cWU2g+K6HlGncNFwJoDWDPOzOnr6mPmvdhPszsUdZiNhdICM3xDaVmr+AoegZlLnKQyidt mNuCEFgyPx6OOJpSN/4Hd5mkDax9GvX9aQpacc85ThSsGyPSAtaWgZ51ONQ1QKkKYa3+hg dCADIA89V98gPG5GRHKY8kceX42K27iukldcBUDUoMi0BUs1ngePATDJHXJneg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=qGwXbsdT; dmarc=none; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: 3.60 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=qGwXbsdT; dmarc=none; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 94BEB13AED X-Spam-Score: 3.60 X-Migadu-Scanner: scn0.migadu.com X-TUID: sIdfnpPzszH0 Always verify the channel introduction commit, so that no commit can slip through that was signed with a different key. Always update the cache, because it affects the behavior of later calls. Signal a continuable compound-condition (with type &warning included) when a channel introduction commit doesn't also update the '.guix-authentications' file. * guix/git-authenticate.scm (authenticate-commit): Reword and extend the error message to point to the relevant part of the manual. (authenticate-repository): Eliminate optimizations to make the code path less dependent on the input. Always trust the intro-commit itself. Always call verify-introductory-commit. (verify-introductory-commit): Check if the commit contains the key that was used to sign it, and issue a warning otherwise. This is to avoid the confusion caused by only the *second* commit yielding an error, because intro-commits are always trusted. (authenticate-commit): Clarify error message. (authorized-keys-at-commit): Factored out to the toplevel from commit-authorized-keys. --- An example output with this patch: $ ./pre-inst-env guix pull --allow-downgrades Updating channel 'guix' from Git repository at '/path/guix'... guix pull: warning: moving channel 'guix' from 26a979105a58e99c6e0fbb51cb1500dfa2bc2cec to unrelated commit 17fc5e35699d2219e6fae1f0583bb8c2ec3deb25 guix pull: warning: initial commit 17fc5e35699d2219e6fae1f0583bb8c2ec3deb25 does not add the key it is signed with (2E4F C7F5 07AB F022 36D3 D51F 31EE D3BE 74EC 3A1F) to the '.guix-authorizations' file. Authenticating channel 'guix', commits 17fc5e3 to 17fc5e3 (0 new commits)... [...] guix/channels.scm | 4 +- guix/git-authenticate.scm | 156 ++++++++++++++++++++++---------------- 2 files changed, 94 insertions(+), 66 deletions(-) diff --git a/guix/channels.scm b/guix/channels.scm index e4e0428eb5..b84064537f 100644 --- a/guix/channels.scm +++ b/guix/channels.scm @@ -347,8 +347,8 @@ commits)...~%") (progress-reporter/bar (length commits))) (define authentic-commits - ;; Consider the currently-used commit of CHANNEL as authentic so - ;; authentication can skip it and all its closure. + ;; Optimization: consider the currently-used commit of CHANNEL as + ;; authentic, so that authentication can skip it and all its closure. (match (find (lambda (candidate) (eq? (channel-name candidate) (channel-name channel))) (current-channels)) diff --git a/guix/git-authenticate.scm b/guix/git-authenticate.scm index ab3fcd8b2f..b2821a45ad 100644 --- a/guix/git-authenticate.scm +++ b/guix/git-authenticate.scm @@ -30,6 +30,7 @@ #:select (cache-directory with-atomic-file-output)) #:use-module ((guix build utils) #:select (mkdir-p)) + #:use-module (guix diagnostics) #:use-module (guix progress) #:use-module (srfi srfi-1) #:use-module (srfi srfi-11) @@ -37,7 +38,10 @@ #:use-module (srfi srfi-34) #:use-module (srfi srfi-35) #:use-module (rnrs bytevectors) + #:use-module ((rnrs exceptions) + #:select (raise-continuable)) #:use-module (rnrs io ports) + #:use-module (ice-9 exceptions) #:use-module (ice-9 match) #:autoload (ice-9 pretty-print) (pretty-print) #:export (read-authorizations @@ -159,11 +163,10 @@ return a list of authorized fingerprints." (string-downcase (string-filter char-set:graphic fingerprint)))) fingerprints)))) -(define* (commit-authorized-keys repository commit - #:optional (default-authorizations '())) - "Return the list of OpenPGP fingerprints authorized to sign COMMIT, based on -authorizations listed in its parent commits. If one of the parent commits -does not specify anything, fall back to DEFAULT-AUTHORIZATIONS." +(define (authorized-keys-at-commit repository commit default-authorizations) + "Return the list of authorized key fingerprints from the '.guix-authorizations' +file at the given commit." + (define (parents-have-authorizations-file? commit) ;; Return true if at least one of the parents of COMMIT has the ;; '.guix-authorizations' file. @@ -185,28 +188,35 @@ does not specify anything, fall back to DEFAULT-AUTHORIZATIONS." to remove '.guix-authorizations' file") (oid->string (commit-id commit))))))) - (define (commit-authorizations commit) - (catch 'git-error - (lambda () - (let* ((tree (commit-tree commit)) - (entry (tree-entry-bypath tree ".guix-authorizations")) - (blob (blob-lookup repository (tree-entry-id entry)))) - (read-authorizations - (open-bytevector-input-port (blob-content blob))))) - (lambda (key error) - (if (= (git-error-code error) GIT_ENOTFOUND) - (begin - ;; Prevent removal of '.guix-authorizations' since it would make - ;; it trivial to force a fallback to DEFAULT-AUTHORIZATIONS. - (assert-parents-lack-authorizations commit) - default-authorizations) - (throw key error))))) + (catch 'git-error + (lambda () + (let* ((tree (commit-tree commit)) + (entry (tree-entry-bypath tree ".guix-authorizations")) + (blob (blob-lookup repository (tree-entry-id entry)))) + (read-authorizations + (open-bytevector-input-port (blob-content blob))))) + (lambda (key error) + (if (= (git-error-code error) GIT_ENOTFOUND) + (begin + ;; Prevent removal of '.guix-authorizations' since it would make + ;; it trivial to force a fallback to DEFAULT-AUTHORIZATIONS. + (assert-parents-lack-authorizations commit) + default-authorizations) + (throw key error))))) +(define* (commit-authorized-keys repository commit + #:optional (default-authorizations '())) + "Return the list of OpenPGP fingerprints authorized to sign COMMIT, based on +authorizations listed in its parent commits. If one of the parent commits +does not specify anything, fall back to DEFAULT-AUTHORIZATIONS." (match (commit-parents commit) (() default-authorizations) (parents (apply lset-intersection bytevector=? - (map commit-authorizations parents))))) + (map (lambda (commit) + (authorized-keys-at-commit repository commit + default-authorizations)) + parents))))) (define* (authenticate-commit repository commit keyring #:key (default-authorizations '())) @@ -236,8 +246,8 @@ not specify anything, fall back to DEFAULT-AUTHORIZATIONS." (condition (&unauthorized-commit-error (commit id) (signing-key signing-key))) - (formatted-message (G_ "commit ~a not signed by an authorized \ -key: ~a") + (formatted-message (G_ "commit ~a is signed by an unauthorized \ +key: ~a\nSee info guix \"Specifying Channel Authorizations\".") (oid->string id) (openpgp-format-fingerprint (openpgp-public-key-fingerprint @@ -356,7 +366,8 @@ authenticated (only COMMIT-ID is written to cache, though)." (base64-encode (sha256 (string->utf8 (repository-directory repository)))))) -(define (verify-introductory-commit repository keyring commit expected-signer) +(define (verify-introductory-commit repository commit expected-signer keyring + authorizations) "Look up COMMIT in REPOSITORY, and raise an exception if it is not signed by EXPECTED-SIGNER." (define actual-signer @@ -364,13 +375,26 @@ EXPECTED-SIGNER." (commit-signing-key repository (commit-id commit) keyring))) (unless (bytevector=? expected-signer actual-signer) - (raise (formatted-message (G_ "initial commit ~a is signed by '~a' \ + (raise (make-compound-condition + (condition (&unauthorized-commit-error (commit (commit-id commit)) + (signing-key actual-signer))) + (formatted-message (G_ "initial commit ~a is signed by '~a' \ instead of '~a'") - (oid->string (commit-id commit)) - (openpgp-format-fingerprint actual-signer) - (openpgp-format-fingerprint expected-signer))))) - -(define* (authenticate-repository repository start signer + (oid->string (commit-id commit)) + (openpgp-format-fingerprint actual-signer) + (openpgp-format-fingerprint expected-signer))))) + (unless (member actual-signer + (authorized-keys-at-commit repository commit authorizations) + bytevector=?) + (raise-continuable + (make-compound-condition + (condition (&warning)) + (formatted-message (G_ "initial commit ~a does not add \ +the key it is signed with (~a) to the '.guix-authorizations' file.") + (oid->string (commit-id commit)) + (openpgp-format-fingerprint actual-signer)))))) + +(define* (authenticate-repository repository intro-commit-hash intro-signer #:key (keyring-reference "keyring") (cache-key (repository-cache-key repository)) @@ -380,11 +404,12 @@ instead of '~a'") (historical-authorizations '()) (make-reporter (const progress-reporter/silent))) - "Authenticate REPOSITORY up to commit END, an OID. Authentication starts -with commit START, an OID, which must be signed by SIGNER; an exception is -raised if that is not the case. Commits listed in AUTHENTIC-COMMITS and their -closure are considered authentic. Return an alist mapping OpenPGP public keys -to the number of commits signed by that key that have been traversed. + "Authenticate REPOSITORY up to commit END, an OID. Authentication starts with +commit INTRO-COMMIT-HASH, an OID, which must be signed by INTRO-SIGNER; an +exception is raised if that is not the case. Commits listed in +AUTHENTIC-COMMITS and their closure are considered authentic. Return an +alist mapping OpenPGP public keys to the number of commits signed by that +key that have been traversed. The OpenPGP keyring is loaded from KEYRING-REFERENCE in REPOSITORY, where KEYRING-REFERENCE is the name of a branch. The list of authenticated commits @@ -393,8 +418,10 @@ is cached in the authentication cache under CACHE-KEY. HISTORICAL-AUTHORIZATIONS must be a list of OpenPGP fingerprints (bytevectors) denoting the authorized keys for commits whose parent lack the '.guix-authorizations' file." - (define start-commit - (commit-lookup repository start)) + + (define intro-commit + (commit-lookup repository intro-commit-hash)) + (define end-commit (commit-lookup repository end)) @@ -404,36 +431,37 @@ denoting the authorized keys for commits whose parent lack the (define authenticated-commits ;; Previously-authenticated commits that don't need to be checked again. (filter-map (lambda (id) + ;; We need to tolerate when cached commits disappear due to + ;; --allow-downgrades. (false-if-git-not-found (commit-lookup repository (string->oid id)))) (append (previously-authenticated-commits cache-key) - authentic-commits))) + authentic-commits + ;; The intro commit is unconditionally trusted. + (list (oid->string intro-commit-hash))))) (define commits ;; Commits to authenticate, excluding the closure of ;; AUTHENTICATED-COMMITS. - (commit-difference end-commit start-commit - authenticated-commits)) - - ;; When COMMITS is empty, it's because END-COMMIT is in the closure of - ;; START-COMMIT and/or AUTHENTICATED-COMMITS, in which case it's known to - ;; be authentic already. - (if (null? commits) - '() - (let ((reporter (make-reporter start-commit end-commit commits))) - ;; If it's our first time, verify START-COMMIT's signature. - (when (null? authenticated-commits) - (verify-introductory-commit repository keyring - start-commit signer)) - - (let ((stats (call-with-progress-reporter reporter - (lambda (report) - (authenticate-commits repository commits - #:keyring keyring - #:default-authorizations - historical-authorizations - #:report-progress report))))) - (cache-authenticated-commit cache-key - (oid->string (commit-id end-commit))) - - stats)))) + (commit-difference end-commit intro-commit + authenticated-commits)) + + (verify-introductory-commit repository intro-commit + intro-signer keyring + historical-authorizations) + + (let* ((reporter (make-reporter intro-commit end-commit commits)) + (stats (call-with-progress-reporter reporter + (lambda (report) + (authenticate-commits repository commits + #:keyring keyring + #:default-authorizations + historical-authorizations + #:report-progress report))))) + ;; Note that this will make the then current end commit of any channel, + ;; that has been used/trusted in the past with a channel introduction, + ;; remain trusted until the cache is cleared. + (cache-authenticated-commit cache-key + (oid->string (commit-id end-commit))) + + stats)) -- 2.33.0