From patchwork Fri Aug 11 09:06:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ulfvonbelow X-Patchwork-Id: 15780 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 1FE1927BBEA; Fri, 11 Aug 2023 10:07:09 +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=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,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 A97D327BBE9 for ; Fri, 11 Aug 2023 10:07:07 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qUO6r-0000se-Do; Fri, 11 Aug 2023 05:07:05 -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 1qUO6p-0000sJ-37 for guix-patches@gnu.org; Fri, 11 Aug 2023 05:07:03 -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 1qUO6o-0006QD-Qg for guix-patches@gnu.org; Fri, 11 Aug 2023 05:07:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qUO6o-0005wA-Ee for guix-patches@gnu.org; Fri, 11 Aug 2023 05:07:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#65221] [PATCH 1/2] service: make EXTRA-PORTS work as advertised. References: <20230811090352.3572-1-striness@tilde.club> In-Reply-To: <20230811090352.3572-1-striness@tilde.club> Resent-From: ulfvonbelow Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 11 Aug 2023 09:07:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65221 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 65221@debbugs.gnu.org Received: via spool by 65221-submit@debbugs.gnu.org id=B65221.169174478522759 (code B ref 65221); Fri, 11 Aug 2023 09:07:02 +0000 Received: (at 65221) by debbugs.gnu.org; 11 Aug 2023 09:06:25 +0000 Received: from localhost ([127.0.0.1]:44891 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qUO6C-0005v0-F7 for submit@debbugs.gnu.org; Fri, 11 Aug 2023 05:06:25 -0400 Received: from tilde.club ([142.44.150.184]:33070 ident=postfix) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qUO6A-0005ur-9m for 65221@debbugs.gnu.org; Fri, 11 Aug 2023 05:06:23 -0400 Received: by tilde.club (Postfix, from userid 5378) id CE52D224F4692; Fri, 11 Aug 2023 09:06:21 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 tilde.club CE52D224F4692 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tilde.club; s=mail; t=1691744781; bh=Int3jw4rw+fZql4dVXkEmhtVkM5b/HBzmw/2N5nrKQA=; h=From:To:Subject:Date:From; b=LK8hy63tE3Y7RD7mLPwjk9/aDOQsxvo5uiYs8ZI/CB+capjO0Lj+GzmFEf5ez/AeO r+HwyCiqxo/eu7LIvftq9ajCrIwCiqPY6RXUSUq66FKi7gDrT90kSFUCRJQKThkPpR e4u7zDpUtGOL3xZaJTiGfxzh1qkdbyEpl3/vXvXQ= From: ulfvonbelow Date: Fri, 11 Aug 2023 04:06:14 -0500 Message-Id: <20230811090615.3707-1-striness@tilde.club> X-Mailer: git-send-email 2.40.1 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 EXEC-COMMAND (and, by extension, FORK+EXEC-COMMAND) has several issues: 1. Despite it being documented that "all other file descriptors are closed prior to yielding control to COMMAND", this is not currently the case - only other file descriptors that are already marked as FD_CLOEXEC are closed. For example, if user code happens to have a file descriptor open, for example with call-with-input-file, while EXEC-COMMAND is run, the new process image will inherit that file descriptor. This may cause some resource waste, but more importantly may cause security issues in certain situations. 2. EXTRA-PORTS is only honored when either LOG-PORT or LOG-FILE is passed. I have no idea why this is the case, it isn't documented anywhere, and it isn't intuitive. 3. Even when LOG-PORT or LOG-FILE is passed, EXTRA-PORTS may not work as described, because it copies file descriptor contents in an arbitrary order. For example, suppose that (map fileno EXTRA-PORTS) is (7 6 5 4 3). If the underlying file originally stored in fd N is represented by F(N), it will assign 3 <-- F(7) 4 <-- F(6) 5 <-- F(5) 6 <-- F(6) 7 <-- F(7) In other words, the copying of earlier FDs in EXTRA-PORTS may overwrite later FDs in EXTRA-PORTS. Because the process of properly and safely copying those FDs involves many steps, we've split it, along with marking all file descriptors not being preserved as FD_CLOEXEC, into a separate procedure named PRESERVE-PORTS. * modules/shepherd/service.scm (preserve-ports): new procedure. (exec-command): use it. --- modules/shepherd/service.scm | 119 +++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 41 deletions(-) diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm index 68553d4..ffbd03c 100644 --- a/modules/shepherd/service.scm +++ b/modules/shepherd/service.scm @@ -1434,6 +1434,52 @@ FILE." (list->vector (map (lambda (group) (group:gid (getgr group))) supplementary-groups))) +(define (preserve-ports extra-ports) + "Duplicate the FDs (fd1 fd2 ... fdN) corresponding to the N ports in +EXTRA-PORTS into the FD range (3 4 ... 3+N). This will work regardless of the +numeric values of fd1 ... fdN. Any open file descriptors not in EXTRA-PORTS +and numbered 3 or higher WILL be closed or marked FD_CLOEXEC." + ;; We employ the following strategy: copy FDs as high as possible, in + ;; descending order of FD, so as to avoid clobbering, then copy the high FDs + ;; to low FDs, in the order specified in EXTRA-PORTS. If more than half of + ;; the FD range is included in EXTRA-PORTS, this still won't work, and we + ;; may reach a point where copying low will require us to copy the + ;; still-uncopied FDs high again. This should be sufficiently rare as to + ;; not be a concern. + (let* ((max-fds-count (max-file-descriptors)) + (highest-fd (- max-fds-count 1)) + (extra-fds-count (length extra-ports)) + (preserved-fds-count (+ 3 extra-fds-count)) + (extra-fds (map fileno extra-ports)) + (index+fd (map cons + (iota extra-fds-count) + extra-fds)) + (index+fd-by-fileno (sort index+fd + (lambda (pair1 pair2) + (> (cdr pair1) + (cdr pair2))))) + (index2+fd-by-fileno (map cons + (iota extra-fds-count) + index+fd-by-fileno)) + (index2+fd (sort index2+fd-by-fileno + (lambda (spec1 spec2) + (< (second spec1) (second spec2)))))) + (for-each dup2 + (map cdr index+fd-by-fileno) + (iota extra-fds-count highest-fd -1)) + (for-each (match-lambda + ((by-fileno-index original-index . original-fd) + (dup2 (- highest-fd by-fileno-index) + (+ 3 original-index)))) + index2+fd) + (for-each (lambda (fd) + (catch-system-error + (let ((flags (fcntl fd F_GETFD))) + (when (zero? (logand flags FD_CLOEXEC)) + (fcntl fd F_SETFD (logior FD_CLOEXEC flags)))))) + (iota (- max-fds-count preserved-fds-count) + preserved-fds-count)))) + (define* (exec-command command #:key (user #f) @@ -1479,48 +1525,39 @@ false." (chdir directory) (environ environment-variables) - ;; Close all the file descriptors except stdout and stderr. - (let ((max-fd (max-file-descriptors))) + ;; Redirect stdin. + (catch-system-error (close-fdes 0)) + ;; Make sure file descriptor zero is used, so we don't end up reusing + ;; it for something unrelated, which can confuse some packages. + (dup2 (if input-port + (fileno input-port) + (open-fdes "/dev/null" O_RDONLY)) + 0) - ;; Redirect stdin. - (catch-system-error (close-fdes 0)) - ;; Make sure file descriptor zero is used, so we don't end up reusing - ;; it for something unrelated, which can confuse some packages. - (dup2 (if input-port - (fileno input-port) - (open-fdes "/dev/null" O_RDONLY)) - 0) + (when (or log-port log-file) + (catch #t + (lambda () + ;; Redirect stdout and stderr to use LOG-FILE. + (catch-system-error (close-fdes 1)) + (catch-system-error (close-fdes 2)) + (dup2 (if log-file + (open-fdes log-file (logior O_CREAT O_WRONLY O_APPEND) + #o640) + (fileno log-port)) + 1) + (dup2 1 2)) - (when (or log-port log-file) - (catch #t - (lambda () - ;; Redirect stout and stderr to use LOG-FILE. - (catch-system-error (close-fdes 1)) - (catch-system-error (close-fdes 2)) - (dup2 (if log-file - (open-fdes log-file (logior O_CREAT O_WRONLY O_APPEND) - #o640) - (fileno log-port)) - 1) - (dup2 1 2) - - ;; Make EXTRA-PORTS available starting from file descriptor 3. - ;; This clears their FD_CLOEXEC flag. - (let loop ((fd 3) - (ports extra-ports)) - (match ports - (() #t) - ((port rest ...) - (catch-system-error (close-fdes fd)) - (dup2 (fileno port) fd) - (loop (+ 1 fd) rest))))) - - (lambda (key . args) - (when log-file - (format (current-error-port) - "failed to open log-file ~s:~%" log-file)) - (print-exception (current-error-port) #f key args) - (primitive-exit 1)))) + (lambda (key . args) + (when log-file + (format (current-error-port) + "failed to open log-file ~s:~%" log-file)) + (print-exception (current-error-port) #f key args) + (primitive-exit 1)))) + + ;; Close all the file descriptors except stdout, stderr, and EXTRA-PORTS. + ;; Make EXTRA-PORTS available starting from file descriptor 3. + ;; This clears their FD_CLOEXEC flag. + (preserve-ports extra-ports) ;; setgid must be done *before* setuid, otherwise the user will ;; likely no longer have permissions to setgid. @@ -1558,7 +1595,7 @@ false." (format (current-error-port) "exec of ~s failed: ~a~%" program (strerror (system-error-errno args))) - (primitive-exit 1))))))) + (primitive-exit 1)))))) (define %precious-signals ;; Signals that the shepherd process handles. From patchwork Fri Aug 11 09:06:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ulfvonbelow X-Patchwork-Id: 15781 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 2C90927BBE2; Fri, 11 Aug 2023 10:07:15 +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=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable 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 5E9F327BBE9 for ; Fri, 11 Aug 2023 10:07:12 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qUO6r-0000sy-LH; Fri, 11 Aug 2023 05:07:05 -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 1qUO6p-0000sR-Bc for guix-patches@gnu.org; Fri, 11 Aug 2023 05:07:03 -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 1qUO6p-0006QH-2P for guix-patches@gnu.org; Fri, 11 Aug 2023 05:07:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qUO6o-0005wI-To for guix-patches@gnu.org; Fri, 11 Aug 2023 05:07:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#65221] [PATCH 2/2] service: use PRESERVE-PORTS for redirecting FDs 0-2. Resent-From: ulfvonbelow Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 11 Aug 2023 09:07:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65221 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 65221@debbugs.gnu.org Received: via spool by 65221-submit@debbugs.gnu.org id=B65221.169174479122779 (code B ref 65221); Fri, 11 Aug 2023 09:07:02 +0000 Received: (at 65221) by debbugs.gnu.org; 11 Aug 2023 09:06:31 +0000 Received: from localhost ([127.0.0.1]:44894 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qUO6J-0005vK-3a for submit@debbugs.gnu.org; Fri, 11 Aug 2023 05:06:31 -0400 Received: from tilde.club ([142.44.150.184]:33084 ident=postfix) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qUO6G-0005vC-Ll for 65221@debbugs.gnu.org; Fri, 11 Aug 2023 05:06:29 -0400 Received: by tilde.club (Postfix, from userid 5378) id 68AEC224F468B; Fri, 11 Aug 2023 09:06:28 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 tilde.club 68AEC224F468B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tilde.club; s=mail; t=1691744788; bh=zT+RsBzrdwyHo9JkTvV911XbenRJjrkflyhLBtnqsII=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UkXPcBtjRYG/oW0cImwMh3XsOKQeRLKp4PtyxHFIP+kVZE/ZWU8N5Pz1dMk3o1/8l cuQAmxsHOWy0ZN4WSqtuGgDrQaSGNPN2Xn3oOHZq1uy3rg3p1/xdqZ8sot+nzlyLwe uiaJlxhXj5u9y6m7UevqV5LRrsV/FebvQd9nkb3A= From: ulfvonbelow Date: Fri, 11 Aug 2023 04:06:15 -0500 Message-Id: <20230811090615.3707-2-striness@tilde.club> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230811090615.3707-1-striness@tilde.club> References: <20230811090615.3707-1-striness@tilde.club> 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 There are currently some corner cases in how EXTRA-PORTS works due to it not managing FDs 0, 1, and 2. Specifically, if one were to include a port in EXTRA-PORTS with FD 0, 1, or 2, it would *not* be preserved, and would instead represent the file that EXEC-COMMAND assigned to that file descriptor. To avoid this, it's necessary to call PRESERVE-PORTS *before* redirecting the input, but this could clobber LOG-PORT or INPUT-PORT, so it would become necessary to include LOG-PORT and INPUT-PORT in the call to PRESERVE-PORTS, then do the redirection using the new FD assignment, then close them. This complication can be avoided if we simply let PRESERVE-PORTS itself do the redirection. This also solves other edge cases, like if LOG-PORT has fileno 0 or 1 (previously passing a LOG-PORT of (current-output-port) would cause an error, as the underlying file descriptor would be closed before dup2 was called to copy it), or if INPUT-PORT has fileno 0. To solve this, we modify PRESERVE-PORTS to allow both file descriptors and ports, and to start the range it copies into at 0 instead of 3. We then modify EXEC-COMMAND to explicitly pass the desired standard I/O FDs / ports at the front of the list it passes to PRESERVE-PORTS. * modules/shepherd/service.scm (preserve-ports): Allow elements of EXTRA-PORTS to be either ports or file descriptors. Start the range of FDs being duplicated into at 0 instead of 3. (exec-command): use PRESERVE-PORTS for redirecting FDs 0, 1, and 2. --- modules/shepherd/service.scm | 74 +++++++++++++++++------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm index ffbd03c..5f735fe 100644 --- a/modules/shepherd/service.scm +++ b/modules/shepherd/service.scm @@ -1435,10 +1435,10 @@ FILE." supplementary-groups))) (define (preserve-ports extra-ports) - "Duplicate the FDs (fd1 fd2 ... fdN) corresponding to the N ports in -EXTRA-PORTS into the FD range (3 4 ... 3+N). This will work regardless of the + "Duplicate the FDs (fd0 fd1 ... fdN) corresponding to the N+1 ports or FDs in +EXTRA-PORTS into the FD range (0 1 ... N). This will work regardless of the numeric values of fd1 ... fdN. Any open file descriptors not in EXTRA-PORTS -and numbered 3 or higher WILL be closed or marked FD_CLOEXEC." +WILL be closed or marked FD_CLOEXEC." ;; We employ the following strategy: copy FDs as high as possible, in ;; descending order of FD, so as to avoid clobbering, then copy the high FDs ;; to low FDs, in the order specified in EXTRA-PORTS. If more than half of @@ -1449,8 +1449,9 @@ and numbered 3 or higher WILL be closed or marked FD_CLOEXEC." (let* ((max-fds-count (max-file-descriptors)) (highest-fd (- max-fds-count 1)) (extra-fds-count (length extra-ports)) - (preserved-fds-count (+ 3 extra-fds-count)) - (extra-fds (map fileno extra-ports)) + (extra-fds (map (lambda (x) + (if (port? x) (fileno x) x)) + extra-ports)) (index+fd (map cons (iota extra-fds-count) extra-fds)) @@ -1470,15 +1471,15 @@ and numbered 3 or higher WILL be closed or marked FD_CLOEXEC." (for-each (match-lambda ((by-fileno-index original-index . original-fd) (dup2 (- highest-fd by-fileno-index) - (+ 3 original-index)))) + original-index))) index2+fd) (for-each (lambda (fd) (catch-system-error (let ((flags (fcntl fd F_GETFD))) (when (zero? (logand flags FD_CLOEXEC)) (fcntl fd F_SETFD (logior FD_CLOEXEC flags)))))) - (iota (- max-fds-count preserved-fds-count) - preserved-fds-count)))) + (iota (- max-fds-count extra-fds-count) + extra-fds-count)))) (define* (exec-command command #:key @@ -1525,39 +1526,34 @@ false." (chdir directory) (environ environment-variables) - ;; Redirect stdin. - (catch-system-error (close-fdes 0)) - ;; Make sure file descriptor zero is used, so we don't end up reusing - ;; it for something unrelated, which can confuse some packages. - (dup2 (if input-port - (fileno input-port) - (open-fdes "/dev/null" O_RDONLY)) - 0) - - (when (or log-port log-file) - (catch #t - (lambda () - ;; Redirect stdout and stderr to use LOG-FILE. - (catch-system-error (close-fdes 1)) - (catch-system-error (close-fdes 2)) - (dup2 (if log-file - (open-fdes log-file (logior O_CREAT O_WRONLY O_APPEND) - #o640) - (fileno log-port)) - 1) - (dup2 1 2)) - - (lambda (key . args) - (when log-file - (format (current-error-port) - "failed to open log-file ~s:~%" log-file)) - (print-exception (current-error-port) #f key args) - (primitive-exit 1)))) - - ;; Close all the file descriptors except stdout, stderr, and EXTRA-PORTS. + ;; Close all the file descriptors except stdin, stdout, stderr, and + ;; EXTRA-PORTS. ;; Make EXTRA-PORTS available starting from file descriptor 3. ;; This clears their FD_CLOEXEC flag. - (preserve-ports extra-ports) + (let* ( ;; Make sure file descriptor zero is used, so we don't end up reusing + ;; it for something unrelated, which can confuse some packages. + (stdin (or input-port (open-fdes "/dev/null" O_RDONLY))) + (stdout (catch #t + (lambda () + (or log-port + (and log-file + (open-fdes log-file + (logior O_CREAT O_WRONLY O_APPEND) + #o640)) + 1)) + (lambda (key . args) + (when log-file + (format (current-error-port) + "failed to open log-file ~s:~%" log-file)) + (print-exception (current-error-port) #f key args) + (primitive-exit 1)))) + (stderr (if (or log-port log-file) + stdout + 2))) + (preserve-ports (cons* stdin + stdout + stderr + extra-ports))) ;; setgid must be done *before* setuid, otherwise the user will ;; likely no longer have permissions to setgid.