Content-Length: 19189 | pFad | https://postgr.es/c/9dc85806d

git.postgresql.org Git - postgresql.git/commitdiff
Avoid potential pfree on NULL on OpenSSL errors
authorDaniel Gustafsson <dgustafsson@postgresql.org>
Fri, 22 Sep 2023 09:18:25 +0000 (11:18 +0200)
committerDaniel Gustafsson <dgustafsson@postgresql.org>
Fri, 22 Sep 2023 09:18:25 +0000 (11:18 +0200)
Guard against the pointer being NULL before pfreeing upon an error
returned from OpenSSL.  Also handle errors from X509_NAME_print_ex
which also can return -1 on memory allocation errors.

Backpatch down to v15 where the code was added.

Author: Sergey Shinderuk <s.shinderuk@postgrespro.ru>
Discussion: https://postgr.es/m/8db5374d-32e0-6abb-d402-40762511eff2@postgrespro.ru
Backpatch-through: v15

src/backend/libpq/be-secure-openssl.c

index 8f9b81c71af52a42900bded1b10e37db8f5fa95c..f5c5ed210e22c214d0e64d21599e8ba50fec7c44 100644 (file)
@@ -615,8 +615,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;
        }
 
@@ -627,12 +630,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);
@@ -646,8 +652,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;
        }
 








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://postgr.es/c/9dc85806d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy