From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:403:478a::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id CINCBYfT32T48QAASxT56A (envelope-from ) for ; Fri, 18 Aug 2023 22:24:39 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:478a::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id DB/2BIfT32SEPQAA9RJhRA (envelope-from ) for ; Fri, 18 Aug 2023 22:24:39 +0200 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 D231C51AE8 for ; Fri, 18 Aug 2023 22:24:38 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=tilde.club header.s=mail header.b=GCuxMvwc; 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=tilde.club (policy=none) ARC-Seal: i=1; s=key1; d=yhetil.org; t=1692390278; a=rsa-sha256; cv=none; b=eVWBnkcGb0ECxSuo1VU2g5Q10PG/vysOZrOUm7OykjHwxgmkdAdbnC+R1kogsG604wv/7r wpB7H6iXigOYnpvAfwsNR3ZGNliOYSq2AuoMQ+xGpDi/jx8YE0Q/HfmlDjejP117fWka6j inZZatRg33ZOLZgbcEgkTUo7B2NUuHy1b2huIettSJG7s+BcdED5O2fwNTTvifkhAfgBvb yy4FzgGUZ3I1u6BPC2qoTouQ5rE5I03n0Q/br7OMhiEFxdykujkPWWHXDoHCE9TBKp+81p UpUiThXJPDTLHpJZKrA6vKOhNQSDEw3WpaXDAGxADzcqKdOt1YWOuW6U3RxyEA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=tilde.club header.s=mail header.b=GCuxMvwc; 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=tilde.club (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1692390278; 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=WPsxISdHn+cJswj6Vk0Z/b1ovbtRQsFeIMjvKBgYlmM=; b=WYpR8KewdAjYXu2p2CGcRtbCSc7rPlfMn2rGdSCY68nyJ654wZG/Xq5JGSQhYDWSXdy7u2 Cb1LyjO0qJNFbP1CHxeI6iW2YDRpZhmpYwV9M5XdDMxCAXlWkiRVPMAqY6yD/tRq4hq9B8 ZQr4ZRpa+B6V+CdFSf6pV0S3KqGEuNmlPSlG5wN4wZJpsQt4lUOJCgoWrMNKLRpDe7Urrg VW2z0u96FXA4IZUtI27br6yP/KEWHzwfMGRCdBNDrB3FUQk3C3DtHBD4yEwgglmJo8il9q JY4d9Se3MPDN5I5tG0NFV6HJLtefkMKY2N8a7TtznzR4YEYYMx4jXfc3lp234w== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qX60p-0008Rk-CU; Fri, 18 Aug 2023 16:24:03 -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 1qX60n-0008Qt-Uj for guix-patches@gnu.org; Fri, 18 Aug 2023 16:24:01 -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 1qX60n-0008Fd-Mk for guix-patches@gnu.org; Fri, 18 Aug 2023 16:24:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qX60o-0006f6-8z for guix-patches@gnu.org; Fri, 18 Aug 2023 16:24:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#65221] [PATCH 2/6] service: don't let earlier ports clobber later ones in EXTRA-PORTS. Resent-From: ulfvonbelow Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 18 Aug 2023 20:24: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 Cc: ulfvonbelow Received: via spool by 65221-submit@debbugs.gnu.org id=B65221.169239019125491 (code B ref 65221); Fri, 18 Aug 2023 20:24:02 +0000 Received: (at 65221) by debbugs.gnu.org; 18 Aug 2023 20:23:11 +0000 Received: from localhost ([127.0.0.1]:48884 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qX5zy-0006d4-JG for submit@debbugs.gnu.org; Fri, 18 Aug 2023 16:23:11 -0400 Received: from tilde.club ([2607:5300:204:4340::114]:42128 ident=postfix) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qX5zx-0006cx-5I for 65221@debbugs.gnu.org; Fri, 18 Aug 2023 16:23:09 -0400 Received: by tilde.club (Postfix, from userid 5378) id 2C5C82250B9BD; Fri, 18 Aug 2023 20:23:08 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 tilde.club 2C5C82250B9BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tilde.club; s=mail; t=1692390188; bh=yVbrYz9EJGJTmXLpYaMy+ky6PEx2qQ28zUuECdILUpU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GCuxMvwcbX7UAaYBTSJkbjretG+IqSNk8X4esm2PPmDkTbYMdwKykibyqXcE3BOLx xthTZh0BlZA1wF8mPTDRwfhgRyB/kismThypwkNPyXgNXDuYsBzDZFXFuZdjWTXKtX zh47eJ7EFikXFF7A4Ue5pyvLmwsXQQ8WOlq4RDyE= From: ulfvonbelow Date: Fri, 18 Aug 2023 15:22:35 -0500 Message-Id: <20230818202239.21177-2-striness@tilde.club> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230818202239.21177-1-striness@tilde.club> References: <20230818202239.21177-1-striness@tilde.club> 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-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Scanner: mx0.migadu.com X-Spam-Score: -0.59 X-Migadu-Queue-Id: D231C51AE8 X-Migadu-Spam-Score: -0.59 X-TUID: 59vPUWyM1Z1J In some situations, 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 some complexity, we've split it into a separate procedure named PRESERVE-PORTS. * modules/shepherd/service.scm (reconfigure-ports): new procedure. (exec-command): use it. --- modules/shepherd/service.scm | 154 +++++++++++++++++++++++++---------- 1 file changed, 113 insertions(+), 41 deletions(-) diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm index 68553d4..68993ac 100644 --- a/modules/shepherd/service.scm +++ b/modules/shepherd/service.scm @@ -1434,6 +1434,88 @@ FILE." (list->vector (map (lambda (group) (group:gid (getgr group))) supplementary-groups))) +(define* (reconfigure-fds ports-or-fds #:optional (base 0)) + "Duplicate the FDs (fd0 fd1 ... fdN) corresponding to the N ports or FDs in +EXTRA-PORTS into the FD range (0 1 ... N), clearing their FD_CLOEXEC flag at +the same time. This will work regardless of the numeric values of fd1 +... fdN. File descriptors outside of the range 0..N will not be affected. +This may fail if there are zero unused file descriptors." + ;; If we view each FD as a node, and fd n at index k of FDS as an edge from + ;; fd n to fd k, then we have a rather special type of graph. Because of + ;; how the edges must be specified, it has the property that no node can + ;; have more than one parent, like a tree, but unlike a tree it is possible + ;; to have cycles (combined with the prior restriction, this means a given + ;; node can be part of at most one cycle). I don't know of a good name for + ;; that kind of graph - maybe "rootless tree"? Anyway, our approach is to, + ;; for each unique FD in FDS, do a traversal that both finds the cyclic + ;; path, if it exists, and sets every FD that isn't part of the cycle, then + ;; finally resolve the cycle using a temporary fd. + + (define fds (map (lambda (x) + (if (port? x) (fileno x) x)) + ports-or-fds)) + (define max-fd (apply max 0 fds)) + (define fd->targets + (let ((vec (make-vector (+ 1 max-fd) '()))) + (for-each (lambda (source-fd dest-fd) + (vector-set! vec source-fd + (cons dest-fd + (vector-ref vec source-fd)))) + fds + (iota (length fds) base)) + vec)) + (define visited (make-vector (+ 1 max-fd) #f)) + + (define (rotate-fds! fds) + ;; (fdval1 fdval2 fdval3) --> (fdval3 fdval1 fdval2) + (match (reverse fds) + ((fd0) + ;; Clear close-on-exec flag as if it were dup2'ed. + (fcntl fd0 F_SETFD 0)) + ((fd0 . (and rest (fd1 . _))) + (let ((tmp-fd (dup->fdes fd0))) + (let loop ((fds rest) + (prev fd0)) + (match fds + ((fd . rest) + (dup2 fd prev) + (loop rest fd)) + (() + (dup2 tmp-fd prev) + (close-fdes tmp-fd)))))))) + + (define (top-visit fd) + (let ((cycle (visit fd fd))) + (when cycle + (rotate-fds! cycle)))) + + (define (visit fd cycle-start-fd) + (if (vector-ref visited fd) + #f + (begin + (vector-set! visited fd #t) + (let loop ((targets (vector-ref fd->targets fd)) + (cycle-tail #f)) + (match targets + ((target . rest) + (cond + ((= target cycle-start-fd) + (loop rest (list fd))) + ((> target max-fd) ;; Has no targets, no need to visit. + (dup2 fd target) + (loop rest cycle-tail)) + (else + (let ((maybe-cycle-tail (visit target cycle-start-fd))) + (if maybe-cycle-tail + (loop rest (cons fd maybe-cycle-tail)) + (begin + (dup2 fd target) + (loop rest cycle-tail))))))) + (() + cycle-tail)))))) + + (for-each top-visit fds)) + (define* (exec-command command #:key (user #f) @@ -1479,48 +1561,38 @@ 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) + + ;; Make EXTRA-PORTS available starting from file descriptor 3. + ;; This clears their FD_CLOEXEC flag. + (reconfigure-fds extra-ports 3)) - (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)))) ;; setgid must be done *before* setuid, otherwise the user will ;; likely no longer have permissions to setgid. @@ -1558,7 +1630,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. -- 2.40.1