From patchwork Tue Jan 23 16:48:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 59346 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 DFFE027BBEA; Tue, 23 Jan 2024 16:49:21 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 6C3FA27BBE2 for ; Tue, 23 Jan 2024 16:49:20 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rSJxL-0003uo-9G; Tue, 23 Jan 2024 11:48:59 -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 1rSJxJ-0003tu-MJ for guix-patches@gnu.org; Tue, 23 Jan 2024 11:48:57 -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 1rSJxJ-0004NQ-DU for guix-patches@gnu.org; Tue, 23 Jan 2024 11:48:57 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rSJxO-00048j-3w for guix-patches@gnu.org; Tue, 23 Jan 2024 11:49:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#68677] [PATCH 1/6] services: secret-service: Make the endpoint configurable. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 23 Jan 2024 16:49:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68677 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 68677@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 68677-submit@debbugs.gnu.org id=B68677.170602853115817 (code B ref 68677); Tue, 23 Jan 2024 16:49:02 +0000 Received: (at 68677) by debbugs.gnu.org; 23 Jan 2024 16:48:51 +0000 Received: from localhost ([127.0.0.1]:43872 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rSJxC-00046z-27 for submit@debbugs.gnu.org; Tue, 23 Jan 2024 11:48:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54786) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rSJx9-000468-Hf for 68677@debbugs.gnu.org; Tue, 23 Jan 2024 11:48:48 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rSJwy-0004IU-UY; Tue, 23 Jan 2024 11:48:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=dgYi8/JjBUTr0ADRIHljhEgQOylTJpQYcKZo9APNDsQ=; b=UD7p5umH/hHxKWfKj5oW zcFwfNt+/FrE6y1stRsAZd8pU/UtztzEq4apkPwQecAVtzPQ+J3dr2KL2h9goy10CdvzC2QDeEH7w iEDFFA38d8wq8P/f8Owxx5TzeOnf+HsjYQ5bj+PRCsHkM4oD0Z+a5hV7KRHv+xL3ClhIRR78+jGze BiC2afYV/RI+ZhrKV+FpUubGVeLxZseArNRWU76uOMz64b9gVe+aJwJh/ksiGVtwhlF/l5PQ4UXbB 9o90xh5Xhr8hWF5RmR5AJOcHsVFAeil0aZVqVCoHmSUjeo8F1m8vL+dS12Rb2D3UH6+C42Pb/N+TJ uQoN//eifjfKPA==; From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 23 Jan 2024 17:48:16 +0100 Message-ID: <4eb616e06f6ecc291e3f144728c6010d051acdfb.1706027375.git.ludo@gnu.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 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 Until now, the secret service had a hard-coded TCP endpoint on port 1004. This change lets users specify arbitrary socket addresses. * gnu/build/secret-service.scm (socket-address->string): New procedure, taken from Shepherd. (secret-service-send-secrets): Replace ‘port’ by ‘address’ and adjust accordingly. (secret-service-receive-secrets): Likewise. * gnu/services/virtualization.scm (secret-service-shepherd-services): Likewise. (secret-service-operating-system): Add optional ‘address’ parameter and honor it. Adjust ‘start’ method accordingly. Change-Id: I87a9514f1c170dca756ce76083d7182c6ebf6578 --- gnu/build/secret-service.scm | 62 +++++++++++++++++++++------------ gnu/services/virtualization.scm | 40 ++++++++++++--------- 2 files changed, 63 insertions(+), 39 deletions(-) diff --git a/gnu/build/secret-service.scm b/gnu/build/secret-service.scm index e13fd4eef3..0226c64032 100644 --- a/gnu/build/secret-service.scm +++ b/gnu/build/secret-service.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2020-2022 Ludovic Courtès +;;; Copyright © 2020-2023 Ludovic Courtès ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen ;;; ;;; This file is part of GNU Guix. @@ -93,13 +93,28 @@ (define (wait-for-readable-fd port timeout) ('readable #t) ('timeout #f))))))) -(define* (secret-service-send-secrets port secret-root +(define (socket-address->string address) + "Return a human-readable representation of ADDRESS, an object as returned by +'make-socket-address'." + (let ((family (sockaddr:fam address))) + (cond ((= AF_INET family) + (string-append (inet-ntop AF_INET (sockaddr:addr address)) + ":" (number->string (sockaddr:port address)))) + ((= AF_INET6 family) + (string-append "[" (inet-ntop AF_INET6 (sockaddr:addr address)) "]" + ":" (number->string (sockaddr:port address)))) + ((= AF_UNIX family) + (sockaddr:path address)) + (else + (object->string address))))) + +(define* (secret-service-send-secrets address secret-root #:key (retry 60) (handshake-timeout 180)) - "Copy all files under SECRET-ROOT using TCP to secret-service listening at -local PORT. If connect fails, sleep 1s and retry RETRY times; once connected, -wait for at most HANDSHAKE-TIMEOUT seconds for handshake to complete. Return -#f on failure." + "Copy all files under SECRET-ROOT by connecting to secret-service listening +at ADDRESS, an address as returned by 'make-socket-address'. If connection +fails, sleep 1s and retry RETRY times; once connected, wait for at most +HANDSHAKE-TIMEOUT seconds for handshake to complete. Return #f on failure." (define (file->file+size+mode file-name) (let ((stat (stat file-name)) (target (substring file-name (string-length secret-root)))) @@ -118,9 +133,9 @@ (define* (secret-service-send-secrets port secret-root (dump-port input sock)))) files))) - (log "sending secrets to ~a~%" port) + (log "sending secrets to ~a~%" (socket-address->string address)) + (let ((sock (socket AF_INET (logior SOCK_CLOEXEC SOCK_STREAM) 0)) - (addr (make-socket-address AF_INET INADDR_LOOPBACK port)) (sleep (if (resolve-module '(fibers) #f) (module-ref (resolve-interface '(fibers)) 'sleep) sleep))) @@ -129,7 +144,7 @@ (define* (secret-service-send-secrets port secret-root ;; forward port inside the guest. (let loop ((retry retry)) (catch 'system-error - (cute connect sock addr) + (cute connect sock address) (lambda (key . args) (when (zero? retry) (apply throw key args)) @@ -147,7 +162,8 @@ (define* (secret-service-send-secrets port secret-root (('secret-service-server ('version version ...)) (log "sending files from ~s...~%" secret-root) (send-files sock) - (log "done sending files to port ~a~%" port) + (log "done sending files to ~a~%" + (socket-address->string address)) (close-port sock) secret-root) (x @@ -155,7 +171,8 @@ (define* (secret-service-send-secrets port secret-root (close-port sock) #f)) (begin ;timeout - (log "timeout while sending files to ~a~%" port) + (log "timeout while sending files to ~a~%" + (socket-address->string address)) (close-port sock) #f)))) @@ -168,19 +185,20 @@ (define (delete-file* file) (unless (= ENOENT (system-error-errno args)) (apply throw args))))) -(define (secret-service-receive-secrets port) - "Listen to local PORT and wait for a secret service client to send secrets. -Write them to the file system. Return the list of files installed on success, -and #f otherwise." +(define (secret-service-receive-secrets address) + "Listen to ADDRESS, an address returned by 'make-socket-address', and wait +for a secret service client to send secrets. Write them to the file system. +Return the list of files installed on success, and #f otherwise." - (define (wait-for-client port) - ;; Wait for a TCP connection on PORT. Note: We cannot use the - ;; virtio-serial ports, which would be safer, because they are - ;; (presumably) unsupported on GNU/Hurd. + (define (wait-for-client address) + ;; Wait for a connection on ADDRESS. Note: virtio-serial ports are safer + ;; than TCP connections but they are (presumably) unsupported on GNU/Hurd. (let ((sock (socket AF_INET (logior SOCK_CLOEXEC SOCK_STREAM) 0))) - (bind sock AF_INET INADDR_ANY port) + (bind sock address) (listen sock 1) - (log "waiting for secrets on port ~a...~%" port) + (log "waiting for secrets on ~a...~%" + (socket-address->string address)) + (match (select (list sock) '() '() 60) (((_) () ()) (match (accept sock) @@ -244,7 +262,7 @@ (define (secret-service-receive-secrets port) (log "invalid secrets received~%") #f))) - (let* ((port (wait-for-client port)) + (let* ((port (wait-for-client address)) (result (and=> port read-secrets))) (when port (close-port port)) diff --git a/gnu/services/virtualization.scm b/gnu/services/virtualization.scm index f0f0ab3bf1..5b8566f600 100644 --- a/gnu/services/virtualization.scm +++ b/gnu/services/virtualization.scm @@ -996,7 +996,7 @@ (define qemu-guest-agent-service-type ;;; Secrets for guest VMs. ;;; -(define (secret-service-shepherd-services port) +(define (secret-service-shepherd-services address) "Return a Shepherd service that fetches sensitive material at local PORT, over TCP. Reboot upon failure." ;; This is a Shepherd service, rather than an activation snippet, to make @@ -1018,7 +1018,7 @@ (define (secret-service-shepherd-services port) "receiving secrets from the host...~%") (force-output (current-error-port)) - (let ((sent (secret-service-receive-secrets #$port))) + (let ((sent (secret-service-receive-secrets #$address))) (unless sent (sleep 3) (reboot)))))) @@ -1039,9 +1039,13 @@ (define secret-service-type boot time. This service is meant to be used by virtual machines (VMs) that can only be accessed by their host."))) -(define (secret-service-operating-system os) +(define* (secret-service-operating-system os + #:optional + (address + #~(make-socket-address + AF_INET INADDR_ANY 1004))) "Return an operating system based on OS that includes the secret-service, -that will be listening to receive secret keys on port 1004, TCP." +that will be listening to receive secret keys on ADDRESS." (operating-system (inherit os) (services @@ -1049,7 +1053,7 @@ (define (secret-service-operating-system os) ;; activation: that requires entropy and thus takes time during boot, and ;; those keys are going to be overwritten by secrets received from the ;; host anyway. - (cons (service secret-service-type 1004) + (cons (service secret-service-type address) (modify-services (operating-system-user-services os) (openssh-service-type config => (openssh-configuration @@ -1243,24 +1247,26 @@ (define (hurd-vm-shepherd-service config) (source-module-closure '((gnu build secret-service) (guix build utils))) #~(lambda () - (let ((pid (fork+exec-command #$vm-command - #:user "childhurd" - ;; XXX TODO: use "childhurd" after - ;; updating Shepherd - #:group "kvm" - #:environment-variables - ;; QEMU tries to write to /var/tmp - ;; by default. - '("TMPDIR=/tmp"))) - (port #$(hurd-vm-port config %hurd-vm-secrets-port)) - (root #$(hurd-vm-configuration-secret-root config))) + (let* ((pid (fork+exec-command #$vm-command + #:user "childhurd" + ;; XXX TODO: use "childhurd" after + ;; updating Shepherd + #:group "kvm" + #:environment-variables + ;; QEMU tries to write to /var/tmp + ;; by default. + '("TMPDIR=/tmp"))) + (port #$(hurd-vm-port config %hurd-vm-secrets-port)) + (root #$(hurd-vm-configuration-secret-root config)) + (address (make-socket-address AF_INET INADDR_LOOPBACK + port))) (catch #t (lambda _ ;; XXX: 'secret-service-send-secrets' won't complete until ;; the guest has booted and its secret service server is ;; running, which could take 20+ seconds during which PID 1 ;; is stuck waiting. - (if (secret-service-send-secrets port root) + (if (secret-service-send-secrets address root) pid (begin (kill (- pid) SIGTERM)