From unknown Sun Sep 14 21:52:24 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#79415 <79415@debbugs.gnu.org> To: bug#79415 <79415@debbugs.gnu.org> Subject: Status: [PATCH] Fix tmpfile close on execute Reply-To: bug#79415 <79415@debbugs.gnu.org> Date: Mon, 15 Sep 2025 04:52:24 +0000 retitle 79415 [PATCH] Fix tmpfile close on execute reassign 79415 guile submitter 79415 odion@efficios.com severity 79415 normal tag 79415 patch thanks From debbugs-submit-bounces@debbugs.gnu.org Tue Sep 09 13:35:25 2025 Received: (at submit) by debbugs.gnu.org; 9 Sep 2025 17:35:26 +0000 Received: from localhost ([127.0.0.1]:60474 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uw2FZ-0006W5-95 for submit@debbugs.gnu.org; Tue, 09 Sep 2025 13:35:25 -0400 Received: from lists.gnu.org ([2001:470:142::17]:43624) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uw2FU-0006VW-KU for submit@debbugs.gnu.org; Tue, 09 Sep 2025 13:35:21 -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 1uw2FN-0005kB-HV for bug-guile@gnu.org; Tue, 09 Sep 2025 13:35:14 -0400 Received: from smtpout.efficios.com ([158.69.130.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uw2FI-0004uV-0C for bug-guile@gnu.org; Tue, 09 Sep 2025 13:35:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1757439301; bh=s8WB9ofKqqFjBmCCYCjWDNCqWgHCKEX3efPn5VPK7Wc=; h=From:To:Cc:Subject:Date:From; b=x7waYZo7GYgAtnix4BXmoMrO0ay9KEL3NaOj2TYqo5gfHyO8vH7Do4uD5iBy3/J0C w4brFIfCyAKhQL1cEjJuMBskFZ7YNfymx4FO3A2Cj1zBPglj9yGocSArcSFAekObn8 B/fprl7J8fHCGqRLKdPaRMxitx3fJMGl708ZwtxhMPkO+2CvEEhmaFF0dNpaeRORTn tHt/mb0l+OSpuO5zpnC5VgqLhkJt8ih+UDez4QG2m57rfqmOzq/VhVmJcNrIN7LEwG h7fwr2d3gtqdZb253O0sOuZRQDI6Qqbe5mgwl4IlOkNWjaLQgjiQVM4q7dRlaNkrHC 7KfVudrhuHQHw== Received: from laura.localdomain (199-193-172-8.cpe.axion.ca [199.193.172.8]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4cLrYd6MHSz9QL; Tue, 09 Sep 2025 13:35:01 -0400 (EDT) From: odion@efficios.com To: bug-guile@gnu.org Subject: [PATCH] Fix tmpfile close on execute Date: Tue, 9 Sep 2025 13:35:00 -0400 Message-ID: <20250909173500.16429-1-odion@efficios.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=158.69.130.18; envelope-from=odion@efficios.com; helo=smtpout.efficios.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 1.0 (+) X-Debbugs-Envelope-To: submit Cc: Olivier Dion X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.0 (/) From: Olivier Dion Guile duplicates the underlying file descriptor stored in a FILE pointer returned by tmpfile(3). This is done to manage the file descriptor with Guile's ports abstraction instead of using the C FILE pointer abstraction. However, the dup(2) system call does not copy the file descriptor flags (O_CLOEXEC), which is important for respecting the semantic expected from tmpfile(3) in that the temporary file will not leak to child processes. Fix this by using the fcntl(2) system call using the `F_DUPFD_CLOEXEC' command. * libguile/posix.c (scm_tmpfile): Use fcntl(2) instead of dup(2) on none MINGW32 systems. --- libguile/posix.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libguile/posix.c b/libguile/posix.c index c8bbb0f83..fc9a116ed 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1825,7 +1825,19 @@ SCM_DEFINE (scm_tmpfile, "tmpfile", 0, 0, 0, SCM_SYSERROR; #ifndef __MINGW32__ - fd = dup (fileno (rv)); + /* tmpfile(3) says: + [...] the file will be automatically deleted when it is closed + or the program terminates. + + That last part can only be done if the `O_CLOEXEC' file descriptor + flag is set. + + However, dup(2) does not copy the file descriptor flags to the new + file descriptor. fcntl(2) with the `F_DUPFD_CLOEXEC' command + essentially to the same thing as dup(2) but will set the + `O_CLOEXEC' flag on the new file descriptor, thus preserving the + expected behavior. */ + fd = fcntl (fileno (rv), F_DUPFD_CLOEXEC, 0); fclose (rv); #else fd = fileno (rv); -- 2.50.1