From patchwork Mon Aug 1 09:07:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 41077 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 9E3C727BBEA; Mon, 1 Aug 2022 10:10:00 +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 D690027BBE9 for ; Mon, 1 Aug 2022 10:09:59 +0100 (BST) Received: from localhost ([::1]:60578 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIRR1-0003oa-0K for patchwork@mira.cbaines.net; Mon, 01 Aug 2022 05:09:59 -0400 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 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 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 Subject: [bug#56867] [PATCH] download: Do not wrap TLS port on GnuTLS >= 3.7.7. 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 To: 56867@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= , guile-devel@gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.16593448905116 (code B ref -1); Mon, 01 Aug 2022 09:09:02 +0000 Received: (at submit) by debbugs.gnu.org; 1 Aug 2022 09:08:10 +0000 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 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 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 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==; 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 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Mon, 1 Aug 2022 11:07:49 +0200 Message-Id: <20220801090749.11655-1-ludo@gnu.org> X-Mailer: git-send-email 2.37.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" X-getmail-retrieved-from-mailbox: Patches 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'. base-commit: ab59155c5a38dda7efaceb47c7528578fcf0def4 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