pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sergey Shinderuk <s.shinderuk@postgrespro.ru>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Subject: Fix error handling in be_tls_open_server()
Date: Tue, 1 Aug 2023 17:44:13 +0300
Message-ID: <8db5374d-32e0-6abb-d402-40762511eff2@postgrespro.ru> (raw)

Hi,

A static analyzer reported a possible pfree(NULL) in 
be_tls_open_server(). Here is a fix. Also handle an error from 
X509_NAME_print_ex().

AFAICS, the error "SSL certificate's distinguished name contains 
embedded null" could not be reached at all, because XN_FLAG_RFC2253 
passed to X509_NAME_print_ex() ensures that null bytes are escaped.

Best regards,

-- 
Sergey Shinderuk		https://postgrespro.com/

Attachments:

pfree-peer-cn-fix.diff (text/plain, 1.4K) download diff --git a/src/backend/libpq/be-secure-openssl.c b/src/backend/libpq/be-secure-openssl.c index 658b09988d6..31b6a6eacdf 100644 --- a/src/backend/libpq/be-secure-openssl.c +++ b/src/backend/libpq/be-secure-openssl.c @@ -620,8 +620,11 @@ aloop: bio = BIO_new(BIO_s_mem()); if (!bio) { - pfree(port->peer_cn); - port->peer_cn = NULL; + if (port->peer_cn != NULL) + { + pfree(port->peer_cn); + port->peer_cn = NULL; + } return -1; } @@ -632,12 +635,15 @@ aloop: * which make regular expression matching a bit easier. Also note that * it prints the Subject fields in reverse order. */ - X509_NAME_print_ex(bio, x509name, 0, XN_FLAG_RFC2253); - if (BIO_get_mem_ptr(bio, &bio_buf) <= 0) + if (X509_NAME_print_ex(bio, x509name, 0, XN_FLAG_RFC2253) == -1 || + BIO_get_mem_ptr(bio, &bio_buf) <= 0) { BIO_free(bio); - pfree(port->peer_cn); - port->peer_cn = NULL; + if (port->peer_cn != NULL) + { + pfree(port->peer_cn); + port->peer_cn = NULL; + } return -1; } peer_dn = MemoryContextAlloc(TopMemoryContext, bio_buf->length + 1); @@ -651,8 +657,11 @@ aloop: (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL certificate's distinguished name contains embedded null"))); pfree(peer_dn); - pfree(port->peer_cn); - port->peer_cn = NULL; + if (port->peer_cn != NULL) + { + pfree(port->peer_cn); + port->peer_cn = NULL; + } return -1; }

view thread (17+ messages)  latest in thread

Message-ID: <8db5374d-32e0-6abb-d402-40762511eff2@postgrespro.ru>
Permalink:  ../8db5374d-32e0-6abb-d402-40762511eff2@postgrespro.ru/
Also on:    postgresql.org/message-id/8db5374d-32e0-6abb-d402-40762511eff2@postgrespro.ru

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: s.shinderuk@postgrespro.ru
  Subject: Re: Fix error handling in be_tls_open_server()
  In-Reply-To: <8db5374d-32e0-6abb-d402-40762511eff2@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox