From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Newsgroups: gmane.comp.gnu.guix.patches,gmane.lisp.guile.devel Subject: [bug#56867] [PATCH] download: Do not wrap TLS port on GnuTLS >= 3.7.7. Date: Mon, 1 Aug 2022 11:07:49 +0200 Message-ID: <20220801090749.11655-1-ludo@gnu.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39027"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= , guile-devel@gnu.org To: 56867@debbugs.gnu.org Original-X-From: guix-patches-bounces+gcggp-guix-patches=m.gmane-mx.org@gnu.org Mon Aug 01 11:09:44 2022 Return-path: Envelope-to: gcggp-guix-patches@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oIRQl-000A50-VW for gcggp-guix-patches@m.gmane-mx.org; Mon, 01 Aug 2022 11:09:44 +0200 Original-Received: from localhost ([::1]:60578 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIRQk-0003oa-R6 for gcggp-guix-patches@m.gmane-mx.org; Mon, 01 Aug 2022 05:09:42 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37078) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIRQ6-0003Dm-Oq for guix-patches@gnu.org; Mon, 01 Aug 2022 05:09:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:49506) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIRQ6-0007rL-Fe for guix-patches@gnu.org; Mon, 01 Aug 2022 05:09:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oIRQ6-0001MC-8T for guix-patches@gnu.org; Mon, 01 Aug 2022 05:09:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 01 Aug 2022 09:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 56867 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch X-Debbugs-Original-To: guix-patches@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16593448905116 (code B ref -1); Mon, 01 Aug 2022 09:09:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 1 Aug 2022 09:08:10 +0000 Original-Received: from localhost ([127.0.0.1]:39255 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIRPG-0001KS-0O for submit@debbugs.gnu.org; Mon, 01 Aug 2022 05:08:10 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:45622) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIRPD-0001KG-Gz for submit@debbugs.gnu.org; Mon, 01 Aug 2022 05:08:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36778) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIRPA-0002NF-4g; Mon, 01 Aug 2022 05:08:05 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:45638) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIRP9-0007hv-N2; Mon, 01 Aug 2022 05:08:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to: references; bh=HCoMK1K2ppAmZLSTpV80QUzxKhuvaQ+EdXehIf+i8C8=; b=gqxEA3cKoYZ014 +mvcpT1iSU9Z5bGFT4ns5yB8BtuKoUE3LtNXkf7bSFus9t++vTBrcvJLI5tp5AWsAArWG/7LF8Q3K Cwc+/8O+wC39iVEIHkKFPi0flQ05MsbAFh4l6ezH+MBQYO4fTWBn4LE1Sg3ja4gw1Vf5jX35TxmD+ F7BEOMXc0vaGXlpKHKFoKytdvFBS5TT/1L8GzVdEFdjPhI9sUzfe/jVyvvHtUKANnxgkT9Rzk8KxG PeRKj0Tz2r4itS1May2oEO3sfXxsf39ra9HGQE3HkxRBB+k75k2UCoOOmhjjSbcrQRsUZXXTbtb+3 85bid4gTzWza6oQVt4zw==; Original-Received: from [193.50.110.235] (port=43010 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIRP8-0007G3-Ia; Mon, 01 Aug 2022 05:08:03 -0400 X-Mailer: git-send-email 2.37.1 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+gcggp-guix-patches=m.gmane-mx.org@gnu.org Original-Sender: "Guix-patches" Xref: news.gmane.io gmane.comp.gnu.guix.patches:28316 gmane.lisp.guile.devel:21272 Archived-At: The custom input/output port wrapping the TLS session record port would introduce overhead, and it would also prevent its uses in a non-blocking context--e.g., with Fibers. The port close mechanism added in GnuTLS 3.7.7 allows us to get rid of that wrapper. * guix/build/download.scm (wrap-record-port-for-gnutls<3.7.7): New procedure, with code formerly in 'tls-wrap'. (tls-wrap): Check for 'set-session-record-port-close!' and use it when available; otherwise call 'wrap-record-port-for-gnutls<3.7.7'. --- guix/build/download.scm | 102 +++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 48 deletions(-) Hello! I'll land a similar change in Guile's (web client) module afterwards if there are no objections. Ludo'. diff --git a/guix/build/download.scm b/guix/build/download.scm index 41583e8143..de094890b3 100644 --- a/guix/build/download.scm +++ b/guix/build/download.scm @@ -245,6 +245,54 @@ (define (print-tls-certificate-error port key args default-printer) (set-exception-printer! 'tls-certificate-error print-tls-certificate-error) +(define (wrap-record-port-for-gnutls<3.7.7 record port) + "Return a port that wraps RECORD to ensure that closing it also closes PORT, +the actual socket port, and its file descriptor. Make sure it does not +introduce extra buffering (custom ports are buffered by default as of Guile +3.0.5). + +This wrapper is unnecessary with GnuTLS >= 3.7.7, which can automatically +close SESSION's file descriptor when RECORD is closed." + (define (read! bv start count) + (define read + (catch 'gnutls-error + (lambda () + (get-bytevector-n! record bv start count)) + (lambda (key err proc . rest) + ;; When responding to "Connection: close" requests, some servers + ;; close the connection abruptly after sending the response body, + ;; without doing a proper TLS connection termination. Treat it as + ;; EOF. This is fixed in GnuTLS 3.7.7. + (if (eq? err error/premature-termination) + the-eof-object + (apply throw key err proc rest))))) + + (if (eof-object? read) + 0 + read)) + (define (write! bv start count) + (put-bytevector record bv start count) + (force-output record) + count) + (define (get-position) + (port-position record)) + (define (set-position! new-position) + (set-port-position! record new-position)) + (define (close) + (unless (port-closed? port) + (close-port port)) + (unless (port-closed? record) + (close-port record))) + + (define (unbuffered port) + (setvbuf port 'none) + port) + + (unbuffered + (make-custom-binary-input/output-port "gnutls wrapped port" read! write! + get-position set-position! + close))) + (define* (tls-wrap port server #:key (verify-certificate? #t)) "Return PORT wrapped in a TLS connection to SERVER. SERVER must be a DNS host name without trailing dot." @@ -317,55 +365,13 @@ (define (log level str) (apply throw args)))) (let ((record (session-record-port session))) - (define (read! bv start count) - (define read - (catch 'gnutls-error - (lambda () - (get-bytevector-n! record bv start count)) - (lambda (key err proc . rest) - ;; When responding to "Connection: close" requests, some - ;; servers close the connection abruptly after sending the - ;; response body, without doing a proper TLS connection - ;; termination. Treat it as EOF. - (if (eq? err error/premature-termination) - the-eof-object - (apply throw key err proc rest))))) - - (if (eof-object? read) - 0 - read)) - (define (write! bv start count) - (put-bytevector record bv start count) - (force-output record) - count) - (define (get-position) - (port-position record)) - (define (set-position! new-position) - (set-port-position! record new-position)) - (define (close) - (unless (port-closed? port) - (close-port port)) - (unless (port-closed? record) - (close-port record))) - - (define (unbuffered port) - (setvbuf port 'none) - port) - (setvbuf record 'block) - - ;; Return a port that wraps RECORD to ensure that closing it also - ;; closes PORT, the actual socket port, and its file descriptor. - ;; Make sure it does not introduce extra buffering (custom ports - ;; are buffered by default as of Guile 3.0.5). - ;; XXX: This wrapper would be unnecessary if GnuTLS could - ;; automatically close SESSION's file descriptor when RECORD is - ;; closed, but that doesn't seem to be possible currently (as of - ;; 3.6.9). - (unbuffered - (make-custom-binary-input/output-port "gnutls wrapped port" read! write! - get-position set-position! - close))))) + (if (module-defined? (resolve-interface '(gnutls)) + 'set-session-record-port-close!) ;GnuTLS >= 3.7.7 + (let ((close-wrapped-port (lambda (_) (close-port port)))) + (set-session-record-port-close! record close-wrapped-port) + record) + (wrap-record-port-for-gnutls<3.7.7 record port))))) (define (ensure-uri uri-or-string) ;XXX: copied from (web http) (cond base-commit: ab59155c5a38dda7efaceb47c7528578fcf0def4 -- 2.37.1