postgres.git / summary / log / commit / refs

commit    df9e6d7c7be5b178bf2c04f74d2f5dd1f7ec77d0
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Fri Oct 30 17:56:10 2026 +0000
First released in: 16.16, 17.12, 18.7, 19.0

    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 its 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.
    
    Problems evidently introduced by 6633cfb21, so back-patch to v16.
    
    Bug: #19411
    Reported-by: Matt Carter <Matt.Carter@twosigma.com>
    Diagnosed-by: Matt Carter <Matt.Carter@twosigma.com>
    Author: Tom Lane <tgl@sss.pgh.pa.us>
    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 3b25d8afda4..d8a5d863706 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -95,11 +95,13 @@ pg_GSS_continue(PGconn *conn, int payloadlen) } /* Only try to acquire credentials if GSS delegation isn't disabled. */ - if (!pg_GSS_have_cred_cache(&conn->gcred)) - conn->gcred = GSS_C_NO_CREDENTIAL; - if (conn->gssdelegation && conn->gssdelegation[0] == '1') - gss_flags |= GSS_C_DELEG_FLAG; + { + if (conn->gcred == GSS_C_NO_CREDENTIAL) + conn->gcred = pg_GSS_get_cred_cache(); + if (conn->gcred != GSS_C_NO_CREDENTIAL) + gss_flags |= GSS_C_DELEG_FLAG; + } maj_stat = gss_init_sec_context(&min_stat, conn->gcred, diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 5d390c1d8f3..625ac451b07 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -2953,7 +2953,8 @@ keep_going: /* We will come back to here until there is } if (conn->gcred == GSS_C_NO_CREDENTIAL) { - if (!pg_GSS_have_cred_cache(&conn->gcred)) + conn->gcred = pg_GSS_get_cred_cache(); + if (conn->gcred == GSS_C_NO_CREDENTIAL) { libpq_append_conn_error(conn, "GSSAPI encryption required but no credential cache"); @@ -4416,15 +4417,16 @@ select_next_encryption_method(PGconn *conn, bool have_valid_connection) if ((remaining_methods & ENC_GSSAPI) != 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 == GSS_C_NO_CREDENTIAL) + conn->gcred = pg_GSS_get_cred_cache(); + if (conn->gcred == GSS_C_NO_CREDENTIAL) { conn->allowed_enc_methods &= ~ENC_GSSAPI; remaining_methods &= ~ENC_GSSAPI; @@ -4588,6 +4590,10 @@ pqMakeEmptyPGconn(void) conn->sock = PGINVALID_SOCKET; conn->Pfdebug = NULL; +#ifdef ENABLE_GSS + conn->gcred = 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 amount diff --git a/src/interfaces/libpq/fe-gssapi-common.c b/src/interfaces/libpq/fe-gssapi-common.c index 29d8fea6290..fd16cf0d612 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 = gss_acquire_cred(&minor, GSS_C_NO_NAME, 0, GSS_C_NO_OID_SET, GSS_C_INITIATE, &cred, NULL, NULL); if (major != GSS_S_COMPLETE) - { - *cred_out = NULL; - return false; - } - *cred_out = cred; - return true; + return GSS_C_NO_CREDENTIAL; + return cred; } /* diff --git a/src/interfaces/libpq/fe-gssapi-common.h b/src/interfaces/libpq/fe-gssapi-common.h index e3504811eb5..627faac27df 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/libpq/fe-secure-gssapi.c index 9bb3872d0a1..94e49fd66de 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] == '1') { - /* Acquire credentials if possible */ + /* Attempt to acquire credentials, if we didn't already */ if (conn->gcred == GSS_C_NO_CREDENTIAL) - (void) pg_GSS_have_cred_cache(&conn->gcred); + conn->gcred = pg_GSS_get_cred_cache(); /* * We have credentials and gssdelegation is enabled, so request [parent: 2acade963cb1]