From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id GCaLO2qY52Kf9gAAbAwnHQ (envelope-from ) for ; Mon, 01 Aug 2022 11:10:03 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id QFWPOmqY52LnagEAG6o9tA (envelope-from ) for ; Mon, 01 Aug 2022 11:10:02 +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 7697EF49D for ; Mon, 1 Aug 2022 11:10:02 +0200 (CEST) Received: from localhost ([::1]:60578 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIRR3-0003oa-6L for larch@yhetil.org; Mon, 01 Aug 2022 05:10:01 -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 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" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1659345002; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=HCoMK1K2ppAmZLSTpV80QUzxKhuvaQ+EdXehIf+i8C8=; b=FGoTt1bAx2CoBy+TgIAADQUHyZ6hrrlVWrESYbXoCLThhpS5s8h2rAz2vQm4sHf1+d+Gyj KDK+q3if2cnQJWfvGKFqQX3qsZxwUMe7sViU3m7knPo0ipkBMWjOynW6APm2vyjc+42HaW 9k+ttyeVapHGbqwnNJzf5Nc6RxU0CRACdqKHn9T6ggo7aRWXm2tiAUACMtqmQGnZIevKHW 2/P1c6aLD4UOF0NaQMIRwlDA/tfqt3WKNbp01U+8Debvo1DDvkpRzlqWOxVaSEm9uq3zTh bVc+mKCV1uyyImCxjBhCKjduzIxdziW0cN/d1+j2RD1+RoBWjlIcgeiYaPBfog== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1659345002; a=rsa-sha256; cv=none; b=Vm844rOPlk4LlB8KpoF7N6xiYrlu0TRTm7tfMjUf4IlNje6VUa2vOF0EsJeIxo5HLPnPDb L0zk2ygN07bviuBPouvLUDFnSsVpkusM74AM5MseFWSCYCUF7lOpANb2vXJimgYTvHaLNo 8hLJCaJGuOB1HHqqRW496LacTStY3dGaXGf0PQtsiAadt8VbP8bR3LVR8i4i3kCH2cbRtL 2q58sUag0hPtH1SuuzQK95XRomUyfc2VvhqYm7Rc/sOkCUDKbeoWA86gY+dVIF51WXWj9y HxVFvufdFISYtelFfdhce+JLUfMECpLQ9SXWAOnKvjI/rjzVZYnoOcc0N+QMvA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=gqxEA3cK; dmarc=pass (policy=none) header.from=gnu.org; 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" X-Migadu-Spam-Score: -1.62 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=gqxEA3cK; dmarc=pass (policy=none) header.from=gnu.org; 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" X-Migadu-Queue-Id: 7697EF49D X-Spam-Score: -1.62 X-Migadu-Scanner: scn0.migadu.com X-TUID: utBKy9+R0Zhn 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