Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMpsl-00000002E38-1fje for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Oct 2026 16:55:12 +0000 Received: from localhost ([127.0.0.1]:34320 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xMpsk-0000000B0gT-3mFR for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Oct 2026 16:55:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:57630) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMpsk-0000000B0gL-2Goa for pgsql-bugs@lists.postgresql.org; Fri, 30 Oct 2026 16:55:10 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMpsi-00000001j49-1l7P for pgsql-bugs@lists.postgresql.org; Fri, 30 Oct 2026 16:55:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 69UGt0rl2601837; Fri, 30 Oct 2026 12:55:00 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 69UGt0rl2601837 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1793379300; bh=XuOX7fJ3mwL+VlSDv4qFOU3PorTqx41LsW8Poi49FAM=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=n8nwuOG4XKSAqzilnMj1OKRVqAt3CYf8DAVRbpM9uWDLKrqJFGhQB+3+s2bDpxWLf dbgo7WQAxHbTpSlAdYe+PA5JliBlSDeVPGkSdlZKVjg4AEgLRY3xl3Rtq1a6D5pZ7+ OdmMFDbInXxft78zG5YYV+4n0yNgHFKs1j7YwO1M= From: Tom Lane To: Matt Carter cc: Andres Freund , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #19411: libpq 16.x exhibits a memory leak when connections are repeatedly created and destroyed In-reply-to: References: <19411-0440c8897a04b638@postgresql.org> <1702505.1771347709@sss.pgh.pa.us> <1881734.1771354069@sss.pgh.pa.us> <6xylkfh6idnx7ujhfhyoxku3v67ygoeve4yt7z2ytc65vzwvcv@z2sj5jnamhbe> <2114661.1793306838@sss.pgh.pa.us> Comments: In-reply-to Matt Carter message dated "Thu, 29 Oct 2026 21:52:17 -0000" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2601785.1793379253.0@sss.pgh.pa.us> Date: Fri, 30 Oct 2026 12:55:00 -0400 Message-ID: <2601836.1793379300@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2601785.1793379253.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Matt Carter writes: > Tom Lane wrote: >> I propose changing its name altogether, as attached. > Thanks, Tom. I tested your patch against my GSSAPI-authenticated server = (MIT krb5 1.20.1, OpenSSL 1.1). It applies to 18.3 with line offsets and b= uilds without warnings. Using the reproducer from my previous message, the= se are the bytes per connection still allocated after warm-up (450 connect= ions): > 18.3 unpatched 17844 > 18.3 + your patch 108 > 18.3 + your patch, gssencmode=3Ddisable 42 > GSSAPI authentication works as before. Thanks for testing that, but after looking closer, I believe that pg_GSS_continue has even more problems here. It will attempt to acquire credentials whether or not GSS delegation is enabled, contrary to the adjacent comment and unlike what happens in pqsecure_open_gss. And if GSS delegation is enabled, it will set the GSS_C_DELEG_FLAG whether or not it was able to acquire credentials, which seems not very sane and again different from the logic in pqsecure_open_gss. So I think what we really need is more like the attached. This passes our own test suite, but I wonder if you can exercise it in your own environment. > We would benefit from back-patches to 16 and 17: Yeah, this problem was visibly introduced in v16 when we added GSS delegation support, so I anticipate back-patching that far. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="v1-0001-libpq-handle-GSS-credentials-more-sanely.patch"; charset="us-ascii" Content-ID: <2601785.1793379253.2@sss.pgh.pa.us> Content-Description: v1-0001-libpq-handle-GSS-credentials-more-sanely.patch Content-Transfer-Encoding: quoted-printable =46rom e7509d1ec9f0d75e5539340981abf0d607d1fac4 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 30 Oct 2026 12:47:51 -0400 Subject: [PATCH v1] libpq: handle GSS credentials more sanely. pg_GSS_have_cred_cache() always acquires new GSS credentials. If we already had some, they were just leaked due to overwriting conn->gcred, and that's a nontrivial amount of memory (around 16K according to reporter). Unfortunately, pg_GSS_continue() did this each time through, and it is typically called more than once in a GSSAPI connection cycle. So repeated connections using GSS authentication could leak substantial amounts of memory. IMO the source of this problem is really the poorly-designed API of pg_GSS_have_cred_cache(): it's unclear that it always gets new credentials. Let's rename it and remove the unnecessary pointer parameter to make the behavior clearer. To add insult to injury, pg_GSS_continue() shouldn't even be trying to acquire credentials unless GSS delegation is enabled, which it is not by default; so the whole effort is often wasted. Problem evidently introduced by 6633cfb21, so back-patch to v16. Bug: #19411 Reported-by: Matt Carter Diagnosed-by: Matt Carter Author: Tom Lane Discussion: https://postgr.es/m/19411-0440c8897a04b638@postgresql.org Backpatch-through: 16 --- src/interfaces/libpq/fe-auth.c | 10 ++++++---- src/interfaces/libpq/fe-connect.c | 16 +++++++++++----- src/interfaces/libpq/fe-gssapi-common.c | 15 ++++++--------- src/interfaces/libpq/fe-gssapi-common.h | 2 +- src/interfaces/libpq/fe-secure-gssapi.c | 4 ++-- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth= .c index e3bddf91203..4a55964875a 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -102,11 +102,13 @@ pg_GSS_continue(PGconn *conn, int payloadlen) pqTraceOutputMessage(conn, conn->inBuffer + conn->inStart, false); = /* Only try to acquire credentials if GSS delegation isn't disabled. */ - if (!pg_GSS_have_cred_cache(&conn->gcred)) - conn->gcred =3D GSS_C_NO_CREDENTIAL; - if (conn->gssdelegation && conn->gssdelegation[0] =3D=3D '1') - gss_flags |=3D GSS_C_DELEG_FLAG; + { + if (conn->gcred =3D=3D GSS_C_NO_CREDENTIAL) + conn->gcred =3D pg_GSS_get_cred_cache(); + if (conn->gcred !=3D GSS_C_NO_CREDENTIAL) + gss_flags |=3D GSS_C_DELEG_FLAG; + } = maj_stat =3D gss_init_sec_context(&min_stat, conn->gcred, diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-c= onnect.c index 811cb0f2a50..9e17a8f1c6f 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -3281,7 +3281,8 @@ keep_going: /* We will come back to here until = there is } if (conn->gcred =3D=3D GSS_C_NO_CREDENTIAL) { - if (!pg_GSS_have_cred_cache(&conn->gcred)) + conn->gcred =3D pg_GSS_get_cred_cache(); + if (conn->gcred =3D=3D GSS_C_NO_CREDENTIAL) { libpq_append_conn_error(conn, "GSSAPI encryption required but no credential cache"); @@ -4847,15 +4848,16 @@ select_next_encryption_method(PGconn *conn, bool h= ave_valid_connection) if ((remaining_methods & ENC_GSSAPI) !=3D 0) { /* - * If GSSAPI encryption is enabled, then call pg_GSS_have_cred_cache() - * which will return true if we can acquire credentials (and give us a - * handle to use in conn->gcred), and then send a packet to the server + * If GSSAPI encryption is enabled, then call pg_GSS_get_cred_cache() + * to try to acquire credentials, and then send a packet to the server * asking for GSSAPI Encryption (and skip past SSL negotiation and * regular startup below). */ if (!conn->gctx) { - if (!pg_GSS_have_cred_cache(&conn->gcred)) + if (conn->gcred =3D=3D GSS_C_NO_CREDENTIAL) + conn->gcred =3D pg_GSS_get_cred_cache(); + if (conn->gcred =3D=3D GSS_C_NO_CREDENTIAL) { conn->allowed_enc_methods &=3D ~ENC_GSSAPI; remaining_methods &=3D ~ENC_GSSAPI; @@ -5020,6 +5022,10 @@ pqMakeEmptyPGconn(void) conn->altsock =3D PGINVALID_SOCKET; conn->Pfdebug =3D NULL; = +#ifdef ENABLE_GSS + conn->gcred =3D GSS_C_NO_CREDENTIAL; /* let's not assume this is 0 */ +#endif + /* * We try to send at least 8K at a time, which is the usual size of pipe * buffers on Unix systems. That way, when we are sending a large amoun= t diff --git a/src/interfaces/libpq/fe-gssapi-common.c b/src/interfaces/libp= q/fe-gssapi-common.c index e7776fe76e7..c23819a219c 100644 --- a/src/interfaces/libpq/fe-gssapi-common.c +++ b/src/interfaces/libpq/fe-gssapi-common.c @@ -55,10 +55,11 @@ pg_GSS_error(const char *mprefix, PGconn *conn, } = /* - * Check if we can acquire credentials at all (and yield them if so). + * Attempt to acquire credentials, and return the token if successful. + * Return GSS_C_NO_CREDENTIAL if not. */ -bool -pg_GSS_have_cred_cache(gss_cred_id_t *cred_out) +gss_cred_id_t +pg_GSS_get_cred_cache(void) { OM_uint32 major, minor; @@ -67,12 +68,8 @@ pg_GSS_have_cred_cache(gss_cred_id_t *cred_out) major =3D gss_acquire_cred(&minor, GSS_C_NO_NAME, 0, GSS_C_NO_OID_SET, GSS_C_INITIATE, &cred, NULL, NULL); if (major !=3D GSS_S_COMPLETE) - { - *cred_out =3D NULL; - return false; - } - *cred_out =3D cred; - return true; + return GSS_C_NO_CREDENTIAL; + return cred; } = /* diff --git a/src/interfaces/libpq/fe-gssapi-common.h b/src/interfaces/libp= q/fe-gssapi-common.h index f89d9e071e8..769e9f5ae91 100644 --- a/src/interfaces/libpq/fe-gssapi-common.h +++ b/src/interfaces/libpq/fe-gssapi-common.h @@ -20,7 +20,7 @@ = void pg_GSS_error(const char *mprefix, PGconn *conn, OM_uint32 maj_stat, OM_uint32 min_stat); -bool pg_GSS_have_cred_cache(gss_cred_id_t *cred_out); +gss_cred_id_t pg_GSS_get_cred_cache(void); int pg_GSS_load_servicename(PGconn *conn); = #endif diff --git a/src/interfaces/libpq/fe-secure-gssapi.c b/src/interfaces/libp= q/fe-secure-gssapi.c index 5ae10af5678..c43ab0294df 100644 --- a/src/interfaces/libpq/fe-secure-gssapi.c +++ b/src/interfaces/libpq/fe-secure-gssapi.c @@ -640,9 +640,9 @@ pqsecure_open_gss(PGconn *conn) = if (conn->gssdelegation && conn->gssdelegation[0] =3D=3D '1') { - /* Acquire credentials if possible */ + /* Attempt to acquire credentials, if we didn't already */ if (conn->gcred =3D=3D GSS_C_NO_CREDENTIAL) - (void) pg_GSS_have_cred_cache(&conn->gcred); + conn->gcred =3D pg_GSS_get_cred_cache(); = /* * We have credentials and gssdelegation is enabled, so request -- = 2.52.0 ------- =_aaaaaaaaaa0--