From patchwork Mon May 5 23:43:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Scott Tankard X-Patchwork-Id: 42342 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 82B6027BC4B; Tue, 6 May 2025 04:44:41 +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=0.0 required=5.0 tests=none autolearn=unavailable version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 3A21D27BC49 for ; Tue, 6 May 2025 04:44:41 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uC9Dy-0000I4-FG; Mon, 05 May 2025 23:44:06 -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 1uC9Dx-0000HV-81 for guix-patches@gnu.org; Mon, 05 May 2025 23:44:05 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uC9Dw-0005Jr-Th for guix-patches@gnu.org; Mon, 05 May 2025 23:44:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=Date:From:MIME-Version:To:In-Reply-To:References:Subject; bh=4dn3M+zPIAvLzOPTIy74nkNML49rk6v6EkHIST7VnfE=; b=ET3zcRkk+nCbTOny3VTtkdZZwKRC7+hEU0x8iWOaByL/e2cTX7lJ1lzypcglj3yir15Bq9An/rKKOeUm361b2IBgywvwPV6u3LVuNoz+h6j3Oe49v4SawNzKshcG2nt2nbSk+K4++bkZ48WO6oCLFIpCxz2X0Bm+psxLm36tG1mlMITfwuZMmAxo9LyAf13gXXz2p0DVIxa2/NO6T50KRr2EAMThcEZU7lNTkxeUeqfq1JPowfTF9PX7pKjq6S35AMAn4YiThK0koUnj5OCwbIx1eRtVzMaKXCb67KjvRvYxvgwnlxxNl7nqqZVtUIVCecClRU9mpMpOwV3Ea/x9aQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1uC9Dw-0005jk-Ie for guix-patches@gnu.org; Mon, 05 May 2025 23:44:04 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#77667] guix-install.sh: Check fingerprint of downloaded PGP keys before importing References: In-Reply-To: Resent-From: Scott Tankard Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 06 May 2025 03:44:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 77667 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: 77667@debbugs.gnu.org Received: via spool by 77667-submit@debbugs.gnu.org id=B77667.174650298421874 (code B ref 77667); Tue, 06 May 2025 03:44:04 +0000 Received: (at 77667) by debbugs.gnu.org; 6 May 2025 03:43:04 +0000 Received: from localhost ([127.0.0.1]:45301 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uC9Cx-0005gh-2l for submit@debbugs.gnu.org; Mon, 05 May 2025 23:43:04 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]:61947) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1uC5TY-0001OL-0c for 77667@debbugs.gnu.org; Mon, 05 May 2025 19:43:57 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-3031354f134so3980650a91.3 for <77667@debbugs.gnu.org>; Mon, 05 May 2025 16:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1746488629; x=1747093429; darn=debbugs.gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=uT3tSDfhwI1Uhy3NF+qSRM63XIuPihtT9+hFULSO3O0=; b=R/1JlaimD7qCtcFCnLy+NLNWXrlvdKX+htkL0tLzMP/lkA98wsjA2uOPlNWg0x2AuI d6GDpIea7pDpdwe1qe/oLpTlYm+msOPYPKjh6Mf217Qp1tD25nxagAAJiunA8r0OcjOE LEacuQf97VduQAU9qP0kvEJaSOOXxUpxWF+U+jxXBU6CHT1ylJAL+jA0bWzK36GjgyXy bw6AMiqgpPszmHCrltELm/OmD2JH1E0dgHGPAx5FKt2ag8RoJsxHjwv750dcCj7bRH6S oUlC5daitlsCeONDy0qvfGzIBx3Xtn/79Ss5e7jkFEugqh3m/ufP0fTcmULlrFJI8mR6 kfiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746488629; x=1747093429; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=uT3tSDfhwI1Uhy3NF+qSRM63XIuPihtT9+hFULSO3O0=; b=mGZ4L3k9Ac4Npv8KT+Z2kOevaRK5nNohvl/LMUzkiqlTh5MLnCgSidI0HvgO8j4kvu Os4yTmyLUHyYxZUNpR0IVzH/n4/4J5B3dhUSz2xUpyEnsYjMS0QmTJxgcxnl1Db5XEL2 Dr/oyBknzaQUX/eV+RULJY1japyW2W5uJU7aiiqW8Mqhsl/uM1k2Eqe0xR4A9JQDdy+J 8QcAyEqzpuGkMKX06h7MxopZLaqbufGh5hDNrBLjHmhOTpV9QOrqnBlmiz0h3UrJy9Gh RvThw8JViLO7sxB4Tln2l+rbK8gSnhLStptO0FuvIPDeZZnCqQ1AJIxZr+9heW/VbpPb DDrg== X-Gm-Message-State: AOJu0YxD6F/F58bGvCejm4xCMRHww3jXkWxLUbSHG3kuXiSlNvQFZ3lH iEygqVaGGSqeEuWXK3eGv63Hk3fc6Y2MTKQpGrCAJr0v0jhDEhgYTXzpvgZ8gGt1iJ2rCkd1eYx ik8ZcQwoZxRQmkKMVGFeHLxNFyRyPUw== X-Gm-Gg: ASbGncuUCYCJpmy1yc4W+8EWQjRAHpPMSdAkBa9ibJS9nX4JoIlU5GOAwnX5dhJMxgJ EDiAA2wwG0haUu/6LGrCxKKmdMBwfL/c6KCZlN4tH7GqxW7WhSmK7NsagpImS8+OESZdoTmqrPy FTTl/VTsuEJJ4vBUWJvZJDXg== X-Google-Smtp-Source: AGHT+IHAV6ZFtrUNG+GYwSuXPlr3CRLvJkxeKRohn/nka8Xb8sGihDBItpovBzJn8OO40DvUUmPqVohV/XscZ2iOGNE= X-Received: by 2002:a17:90b:2c8e:b0:305:5f32:d9f5 with SMTP id 98e67ed59e1d1-30a6196c3ebmr15502089a91.7.1746488629017; Mon, 05 May 2025 16:43:49 -0700 (PDT) MIME-Version: 1.0 From: Scott Tankard Date: Mon, 5 May 2025 16:43:37 -0700 X-Gm-Features: ATxdqUH6U7wp3wKKaIefQyB_GSaAc5SPyFhcxpkZIX-2YHLdCyA3I-5YITV2RDQ Message-ID: X-Mailman-Approved-At: Mon, 05 May 2025 23:42:59 -0400 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+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches The prior "patch" I sent was in fact a diff; this one is formatted with `git format-patch`. Content is almost exactly the same as the prior diff. From 03bacc6e90063a3d75365354f037dbb943b1a98e Mon Sep 17 00:00:00 2001 From: user <> Date: Mon, 5 May 2025 16:29:31 -0700 Subject: [PATCH] guix-install.sh: Check fingerprint of downloaded PGP keys before importing --- etc/guix-install.sh | 64 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/etc/guix-install.sh b/etc/guix-install.sh index b5d833c..224fa50 100755 --- a/etc/guix-install.sh +++ b/etc/guix-install.sh @@ -105,6 +105,12 @@ declare -A GPG_SIGNING_KEYS GPG_SIGNING_KEYS[15145]=3CE464558A84FDC69DB40CFB090B11993D9AEBB5 # ludo GPG_SIGNING_KEYS[127547]=27D586A4F8900854329FF09F1260E46482E63562 # maxim +# TEST_FAIL_FETCH_KEY=no +# if [ "${TEST_FAIL_FETCH_KEY}" = 'yes' ]; then +# GPG_SIGNING_KEYS[15145]=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +# GPG_SIGNING_KEYS[127547]=BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB +# fi + # ------------------------------------------------------------------------------ #+UTILITIES @@ -189,12 +195,49 @@ add_init_sys_require() fi } +get_gpg_fpr_of_keyfile() +{ + local keyfile="$1" + local tmp_dir + tmp_dir="$(mktemp -t -d guix.gnupg.tmphomedir.XXXXXX)" + gpg -qq --homedir "$tmp_dir" --dry-run --no-keyring \ + --with-colons --show-keys "$keyfile" \ + | grep '^pub:' -A 1 | tail -n 1 \ + | grep -oE '^([^:]*:){10}' | grep -oE '[^:]*:$' | tr -d ':' + # awk -F: '/^pub:/ { getline; print $10 }' + + # Explanation: Get the line immediately after the line that starts + # with `pub:`. Then get the 10th field from that line, with colons as + # field separators. +} + +# Check that a keyfile has expected fingerprint and import it to GPG keyring +# only if so. If keyfile path is given as '-', it reads from STDIN. Returns 0 +# or 1 depending in its outcome, so it can be used in condition tests. +import_key() +{ + local keyfile="$1" + local fpr="$2" + local tmp_dir + + [ "$keyfile" = "-" ] && keyfile=/dev/stdin + tmp_dir="$(mktemp -t -d guix.gpg.XXXXXX)" + cp "$keyfile" "$tmp_dir/key.gpg" + # Copying to a temp dir ensures no unprivileged process can tamper + # with it between the time we check the fingerprint and gpg-import it. + if [ "$fpr" = "$(get_gpg_fpr_of_keyfile "$tmp_dir/key.gpg")" ]; then + gpg --import "$tmp_dir/key.gpg" && return 0 + fi + return 1 +} + chk_gpg_keyring() { # Check whether the Guix release signing public key is present. _debug "--- [ ${FUNCNAME[0]} ] ---" local user_id local gpg_key_id local exit_flag + local tmp_dir for user_id in "${!GPG_SIGNING_KEYS[@]}"; do gpg_key_id=${GPG_SIGNING_KEYS[$user_id]} @@ -209,17 +252,19 @@ Would you like me to fetch it for you?"; then # Use a reasonable time-out here so users don't report silent # ‘freezes’ when Savannah goes out to lunch, as has happened. if wget "https://sv.gnu.org/people/viewgpg.php?user_id=$user_id" \ - --timeout=30 --no-verbose -O- | gpg --import -; then + --timeout=30 --no-verbose -O - | import_key - "$gpg_key_id"; then continue fi fi - # If we reach this point, the key is (still) missing. Report further - # missing keys, if any, but then abort the installation. + # If we reach this point, the key is (still) missing. Report further + # missing keys, if any, but then abort the installation. _err "Missing OpenPGP public key ($gpg_key_id). Fetch it with this command: - wget \"https://sv.gnu.org/people/viewgpg.php?user_id=$user_id\" -O - | \ -sudo -i gpg --import -" +wget 'https://sv.gnu.org/people/viewgpg.php?user_id=${user_id}' -O - | \ +sudo sh $0 import_key - '$gpg_key_id' + +" exit_flag=yes done if [ "$exit_flag" = yes ]; then @@ -1030,14 +1075,15 @@ main_uninstall() main() { - # expect no parameters - # or '--uninstall' if [ 0 -eq $# ]; then main_install else - local uninstall_flag="$1" - if [ '--uninstall' = "${uninstall_flag}" ]; then + local _flag="$1" + if [ "${_flag}" = '--uninstall' ]; then main_uninstall + elif [ "${_flag}" = "import_key" ]; then + import_key "$2" "$3" && exit 0 + exit 1 else echo "unsupported parameters: $*" exit 1 -- 2.45.0