From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:700:3204::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id yHQGEiAnoGWlogAAkFu2QA (envelope-from ) for ; Thu, 11 Jan 2024 18:36:32 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id uIDXDSAnoGVhKAAAe85BDQ (envelope-from ) for ; Thu, 11 Jan 2024 18:36:32 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=wolfsden.cz header.s=mail header.b=x4YhaYR4; dkim=fail ("headers rsa verify failed") header.d=wolfsden.cz header.s=mail header.b=x4YhaYR4; 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"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=wolfsden.cz (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1704994413; 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=DuXXQtM2QPGaGwJomdKIFHH6J8YKdRoalG4j5BkcCek=; b=akum5PCR41RYtIhTRclca0RFOWTLkBqec5ASXh7jBSsD0IN8w8mAfF2XgiWI/X4RZH94Ai feKRbqy0WHOF+iUIvPAEbMzMGFd7dAxmVLU4LNwclRxb6QtBhGCzzguaDoq1KtYX61gUEs gxajjboKcG4rkZ7da2w174XNEPzL+88CxiZkhQZVZoRVftXqo3c1+Z2/qwlebvKe71kKrH jxp8dA5FKYP21TD/626bwvvrCBBZlTRixARgSO510iaMOVZQ8tGc1IkI7t1KDKr2XP53st 34ZGg1mocC5SehgRqrMDH9swoJx8OLv09LfgVoWJ4pPBoZiiu27WP7SDfhEnFQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=wolfsden.cz header.s=mail header.b=x4YhaYR4; dkim=fail ("headers rsa verify failed") header.d=wolfsden.cz header.s=mail header.b=x4YhaYR4; 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"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=wolfsden.cz (policy=none) ARC-Seal: i=1; s=key1; d=yhetil.org; t=1704994413; a=rsa-sha256; cv=none; b=SEeUAhDlfQVNaIc/W70QE5h7BN4RkhfaoXuBUIjEUYQL1JgQge5yygb/2xBLF6QdwwmXgZ dfAzCyyYiSF9+bQ3xLMBBTGxPz6TXPXfwIigNirEQmchxdL6hRgCMM6oRyUuk3goq/6vND C0Vmccx9QhNo/Fr3aWKK2vMP/Lj6wEpP36ZWny7Ro0o3MTnWbxEIDN7lSJXpJkIhGytM9W bbFJK9v0Tnp3BH2T6UuC83/j9eqZSp2lJuVpMHspMuWoec7Z7DRM43EPd6pgE8Xn4riwII i9U/PEoY4UmiF3J3mmSgMU3Ltehnu9Ayf0SgsUEC2aO10nYfPHXhDJxCZXKZTw== 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 9A8F5DFB7 for ; Thu, 11 Jan 2024 18:33:33 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNyvV-00046n-IE; Thu, 11 Jan 2024 12:33:09 -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 1rNyvT-00044a-Gq for guix-patches@gnu.org; Thu, 11 Jan 2024 12:33:07 -0500 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 1rNyvT-0001gx-72 for guix-patches@gnu.org; Thu, 11 Jan 2024 12:33:07 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rNyvQ-0007i7-Ku for guix-patches@gnu.org; Thu, 11 Jan 2024 12:33:04 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#65002] [PATCH 1/6] mapped-devices: Allow unlocking by a key file. References: In-Reply-To: Resent-From: Tomas Volf <~@wolfsden.cz> Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 11 Jan 2024 17:33:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65002 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 65002@debbugs.gnu.org Cc: Tomas Volf Received: via spool by 65002-submit@debbugs.gnu.org id=B65002.170499436329576 (code B ref 65002); Thu, 11 Jan 2024 17:33:04 +0000 Received: (at 65002) by debbugs.gnu.org; 11 Jan 2024 17:32:43 +0000 Received: from localhost ([127.0.0.1]:33752 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rNyv4-0007gs-6J for submit@debbugs.gnu.org; Thu, 11 Jan 2024 12:32:42 -0500 Received: from wolfsden.cz ([37.205.8.62]:45618) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <~@wolfsden.cz>) id 1rNyuu-0007fg-UX for 65002@debbugs.gnu.org; Thu, 11 Jan 2024 12:32:37 -0500 Received: by wolfsden.cz (Postfix, from userid 104) id E0E632507AC; Thu, 11 Jan 2024 17:32:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wolfsden.cz; s=mail; t=1704994352; bh=lHhs3cBVhmZ9vJfZow/R++juhXY33XOvQVgyIOmAY+E=; h=From:To:Cc:Subject:Date; b=x4YhaYR4fc5DmzpHWgQ8b33oLDjuXrgzkMJeSVGWo7aMwCZyrGnp6cEbVFghOQA2w At03KRTv4UD3WAuNNbrOUUji+NTmH1w1KxbtEiIC7ug1L+mDP11jJcRL9asGkeFYJl v7zsxz/pZSQUXI04OEEPvf/thHxR2/zHRpAhtO4SC5uRLP0eB3v6HTKSkF62Aov4b2 moelnFQ/8bf7pTogtHXZYHgTL3ovLcOx1SBX+KDSf7mrVv7F/JvsoJVXOB8Zmu5BBe h7MvZ52+N8HKsmxCtFxmhPsY6uHrOFC9pKFsPmn5GyViXAMOjKS9txiOo4MRjU+5Dl asSoUPdxTlGvDsKx2p6mzQdL62v3/mDMe97Ma2NDx71NYqDnN+01Di2ayzIZfnh4Iy cuEyLASQcuYnjJB9HQaF6tNI+eC30Aa9HIwAkYaKdM7JI9qhqy3uJL/QT+o9av2qpH L5yDAkfOjP6awZ5t4HkFBINxfKYq8s+mV6le89Ilw6lqviAJ8dZefz1HUkMGQv6flO /M61C+jkE5mrdgeTboXsmEYOOTcAnJ5WG9rcG2pwGF1G7CiwpnN9bm6RYASmEAv2rL jrlsPBs+guZO1Tx022zR0l3ONAcCTyzJ6hQn0aQb+rZHQrfe3PaBUBiRNTVYsr2ZAT Rtkus+n0rp6Tgem22bXO2/KA= Received: from localhost (unknown [193.32.127.158]) by wolfsden.cz (Postfix) with ESMTPSA id 4722724FBC8; Thu, 11 Jan 2024 17:32:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wolfsden.cz; s=mail; t=1704994352; bh=lHhs3cBVhmZ9vJfZow/R++juhXY33XOvQVgyIOmAY+E=; h=From:To:Cc:Subject:Date; b=x4YhaYR4fc5DmzpHWgQ8b33oLDjuXrgzkMJeSVGWo7aMwCZyrGnp6cEbVFghOQA2w At03KRTv4UD3WAuNNbrOUUji+NTmH1w1KxbtEiIC7ug1L+mDP11jJcRL9asGkeFYJl v7zsxz/pZSQUXI04OEEPvf/thHxR2/zHRpAhtO4SC5uRLP0eB3v6HTKSkF62Aov4b2 moelnFQ/8bf7pTogtHXZYHgTL3ovLcOx1SBX+KDSf7mrVv7F/JvsoJVXOB8Zmu5BBe h7MvZ52+N8HKsmxCtFxmhPsY6uHrOFC9pKFsPmn5GyViXAMOjKS9txiOo4MRjU+5Dl asSoUPdxTlGvDsKx2p6mzQdL62v3/mDMe97Ma2NDx71NYqDnN+01Di2ayzIZfnh4Iy cuEyLASQcuYnjJB9HQaF6tNI+eC30Aa9HIwAkYaKdM7JI9qhqy3uJL/QT+o9av2qpH L5yDAkfOjP6awZ5t4HkFBINxfKYq8s+mV6le89Ilw6lqviAJ8dZefz1HUkMGQv6flO /M61C+jkE5mrdgeTboXsmEYOOTcAnJ5WG9rcG2pwGF1G7CiwpnN9bm6RYASmEAv2rL jrlsPBs+guZO1Tx022zR0l3ONAcCTyzJ6hQn0aQb+rZHQrfe3PaBUBiRNTVYsr2ZAT Rtkus+n0rp6Tgem22bXO2/KA= From: Tomas Volf <~@wolfsden.cz> Date: Thu, 11 Jan 2024 18:32:11 +0100 Message-ID: X-Mailer: git-send-email 2.41.0 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 X-Migadu-Scanner: mx10.migadu.com X-Migadu-Spam-Score: 3.69 X-Spam-Score: 3.69 X-Migadu-Queue-Id: 9A8F5DFB7 X-TUID: tNrHjYR7907V From: Tomas Volf Requiring the user to input their password in order to unlock a device is not always reasonable, so having an option to unlock the device using a key file is a nice quality of life change. * gnu/system/mapped-devices.scm (open-luks-device): Add #:key-file argument. (luks-device-mapping-with-options): New procedure. * doc/guix.texi (Mapped Devices): Describe the new procedure. Change-Id: I1de4e045f8c2c11f9a94f1656e839c785b0c11c4 --- doc/guix.texi | 25 +++++++++++++ gnu/system/mapped-devices.scm | 67 ++++++++++++++++++++++------------- 2 files changed, 67 insertions(+), 25 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 395545bed7..b1202f2182 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -123,6 +123,7 @@ Copyright @copyright{} 2023 Thomas Ieong@* Copyright @copyright{} 2023 Saku Laesvuori@* Copyright @copyright{} 2023 Graham James Addis@* +Copyright @copyright{} 2023 Tomas Volf@* Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or @@ -17992,6 +17993,30 @@ Mapped Devices @code{dm-crypt} Linux kernel module. @end defvar +@deffn {Procedure} luks-device-mapping-with-options [#:key-file] +Return a @code{luks-device-mapping} object, which defines LUKS block +device encryption using the @command{cryptsetup} command from the +package with the same name. It relies on the @code{dm-crypt} Linux +kernel module. + +If @code{key-file} is provided, unlocking is first attempted using that +key file. This has an advantage of not requiring a password entry, so +it can be used (for example) to unlock RAID arrays automatically on +boot. If key file unlock fails, password unlock is attempted as well. +Key file is not stored in the store and needs to be available at the +given location at the time of the unlock attempt. + +@lisp +;; Following definition would be equivalent to running: +;; cryptsetup open --key-file /crypto.key /dev/sdb1 data +(mapped-device + (source "/dev/sdb1) + (target "data) + (type (luks-device-mapping-with-options + #:key-file "/crypto.key"))) +@end lisp +@end deffn + @defvar raid-device-mapping This defines a RAID device, which is assembled using the @code{mdadm} command from the package with the same name. It requires a Linux kernel diff --git a/gnu/system/mapped-devices.scm b/gnu/system/mapped-devices.scm index e6b8970c12..c19a818453 100644 --- a/gnu/system/mapped-devices.scm +++ b/gnu/system/mapped-devices.scm @@ -2,6 +2,7 @@ ;;; Copyright © 2014-2022 Ludovic Courtès ;;; Copyright © 2016 Andreas Enge ;;; Copyright © 2017, 2018 Mark H Weaver +;;; Copyright © 2024 Tomas Volf <~@wolfsden.cz> ;;; ;;; This file is part of GNU Guix. ;;; @@ -64,6 +65,7 @@ (define-module (gnu system mapped-devices) check-device-initrd-modules ;XXX: needs a better place luks-device-mapping + luks-device-mapping-with-options raid-device-mapping lvm-device-mapping)) @@ -188,7 +190,7 @@ (define (check-device-initrd-modules device linux-modules location) ;;; Common device mappings. ;;; -(define (open-luks-device source targets) +(define* (open-luks-device source targets #:key key-file) "Return a gexp that maps SOURCE to TARGET as a LUKS device, using 'cryptsetup'." (with-imported-modules (source-module-closure @@ -198,7 +200,8 @@ (define (open-luks-device source targets) ((target) #~(let ((source #$(if (uuid? source) (uuid-bytevector source) - source))) + source)) + (keyfile #$key-file)) ;; XXX: 'use-modules' should be at the top level. (use-modules (rnrs bytevectors) ;bytevector? ((gnu build file-systems) @@ -215,29 +218,35 @@ (define (open-luks-device source targets) ;; 'cryptsetup open' requires standard input to be a tty to allow ;; for interaction but shepherd sets standard input to /dev/null; ;; thus, explicitly request a tty. - (zero? (system*/tty - #$(file-append cryptsetup-static "/sbin/cryptsetup") - "open" "--type" "luks" - - ;; Note: We cannot use the "UUID=source" syntax here - ;; because 'cryptsetup' implements it by searching the - ;; udev-populated /dev/disk/by-id directory but udev may - ;; be unavailable at the time we run this. - (if (bytevector? source) - (or (let loop ((tries-left 10)) - (and (positive? tries-left) - (or (find-partition-by-luks-uuid source) - ;; If the underlying partition is - ;; not found, try again after - ;; waiting a second, up to ten - ;; times. FIXME: This should be - ;; dealt with in a more robust way. - (begin (sleep 1) - (loop (- tries-left 1)))))) - (error "LUKS partition not found" source)) - source) - - #$target))))))) + (let ((partition + ;; Note: We cannot use the "UUID=source" syntax here + ;; because 'cryptsetup' implements it by searching the + ;; udev-populated /dev/disk/by-id directory but udev may + ;; be unavailable at the time we run this. + (if (bytevector? source) + (or (let loop ((tries-left 10)) + (and (positive? tries-left) + (or (find-partition-by-luks-uuid source) + ;; If the underlying partition is + ;; not found, try again after + ;; waiting a second, up to ten + ;; times. FIXME: This should be + ;; dealt with in a more robust way. + (begin (sleep 1) + (loop (- tries-left 1)))))) + (error "LUKS partition not found" source)) + source))) + ;; We want to fallback to the password unlock if the keyfile fails. + (or (and keyfile + (zero? (system*/tty + #$(file-append cryptsetup-static "/sbin/cryptsetup") + "open" "--type" "luks" + "--key-file" keyfile + partition #$target))) + (zero? (system*/tty + #$(file-append cryptsetup-static "/sbin/cryptsetup") + "open" "--type" "luks" + partition #$target))))))))) (define (close-luks-device source targets) "Return a gexp that closes TARGET, a LUKS device." @@ -276,6 +285,14 @@ (define luks-device-mapping (close close-luks-device) (check check-luks-device))) +(define* (luks-device-mapping-with-options #:key key-file) + "Return a luks-device-mapping object with open modified to pass the arguments +into the open-luks-device procedure." + (mapped-device-kind + (inherit luks-device-mapping) + (open (λ (source targets) (open-luks-device source targets + #:key-file key-file))))) + (define (open-raid-device sources targets) "Return a gexp that assembles SOURCES (a list of devices) to the RAID device TARGET (e.g., \"/dev/md0\"), using 'mdadm'." base-commit: 5c0f77f4241c9beac0c82deae946bfdc70b49ff0 -- 2.41.0