[packages/postgresql/POSTGRESQL_9_3] Rel 2. Fix TLS 1.3 with ssl_renegotiation_limit, fix build

arekm arekm at pld-linux.org
Fri Nov 13 15:11:47 CET 2026


commit 9b37e96156b8ace87d172da54cf51615b29a7bfb
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Fri Nov 13 15:11:11 2026 +0100

    Rel 2. Fix TLS 1.3 with ssl_renegotiation_limit, fix build
    
    Fixes CVE-2024-10977, CVE-2021-23222, CVE-2021-23214.

 postgresql-CVE-2021-23214.patch     | 98 +++++++++++++++++++++++++++++++++++++
 postgresql-CVE-2021-23222.patch     | 89 +++++++++++++++++++++++++++++++++
 postgresql-CVE-2024-10977.patch     | 74 ++++++++++++++++++++++++++++
 postgresql-libxml2-2.13-tests.patch | 34 +++++++++++++
 postgresql-openldap25.patch         | 24 +++++++++
 postgresql-tls13-key-update.patch   | 28 +++++++++++
 postgresql.spec                     | 20 +++++++-
 7 files changed, 365 insertions(+), 2 deletions(-)
---
diff --git a/postgresql.spec b/postgresql.spec
index bc3dff6..309d14f 100644
--- a/postgresql.spec
+++ b/postgresql.spec
@@ -33,10 +33,10 @@ Summary(uk.UTF-8):	PostgreSQL - система керування базами 
 Summary(zh_CN.UTF-8):	PostgreSQL 客户端程序和库文件
 Name:		postgresql
 Version:	%{mver}.25
-Release:	1
+Release:	2
 License:	BSD
 Group:		Applications/Databases
-Source0:	http://ftp.postgresql.org/pub/source/v%{version}/%{name}-%{version}.tar.bz2
+Source0:	https://ftp.postgresql.org/pub/source/v%{version}/%{name}-%{version}.tar.bz2
 # Source0-md5:	8a7c673ece44f6e3cd124676b704cc48
 Source1:	%{name}.init
 Source2:	pgsql-Database-HOWTO-html.tar.gz
@@ -54,6 +54,13 @@ Patch3:		%{name}-ac_version.patch
 Patch4:		%{name}-disable_horology_test.patch
 Patch5:		%{name}-heimdal.patch
 Patch6:		build.patch
+Patch7:		%{name}-openldap25.patch
+Patch8:		%{name}-libxml2-2.13-tests.patch
+Patch9:		%{name}-tls13-key-update.patch
+# Debian ELTS postgresql-9.4 backports
+Patch10:	%{name}-CVE-2021-23214.patch
+Patch11:	%{name}-CVE-2021-23222.patch
+Patch12:	%{name}-CVE-2024-10977.patch
 URL:		http://www.postgresql.org/
 BuildRequires:	autoconf
 BuildRequires:	automake
@@ -110,6 +117,9 @@ BuildRoot:	%{tmpdir}/%{name}-%{version}-root-%(id -u -n)
 
 %define		_ulibdir	/usr/lib
 
+# 9.3 sources predate C23 (typedef char bool)
+%define		specflags	-std=gnu17
+
 # omitted contribs:
 # dummy_seclabel, pg_test_fsync, spi, test_parser, worker_spi - examples/tests
 # tsearch2 - old module for compatibility only
@@ -785,6 +795,12 @@ Różne moduły dołączone do PostgreSQL-a.
 %patch -P4 -p1
 %patch -P5 -p1
 %patch -P6 -p1
+%patch -P7 -p1
+%patch -P8 -p1
+%patch -P9 -p1
+%patch -P10 -p1
+%patch -P11 -p1
+%patch -P12 -p1
 
 # force rebuild of bison/flex files
 find src -name \*.l -o -name \*.y | xargs touch
diff --git a/postgresql-CVE-2021-23214.patch b/postgresql-CVE-2021-23214.patch
new file mode 100644
index 0000000..3f8424d
--- /dev/null
+++ b/postgresql-CVE-2021-23214.patch
@@ -0,0 +1,98 @@
+From: Debian PostgreSQL Maintainers <team+postgresql at tracker.debian.org>
+Date: Tue, 27 Jun 2023 18:51:15 +0000
+Subject: CVE-2021-23214
+
+Origin: htthttp://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff_plain;h=046c2c846b741a12e7fd61d8d86bf324a20e3dfc
+Reviewed-by: Sylvain Beucler <beuc at debian.org>
+Last-Update: 2021-11-17
+
+X-Git-Tag: REL9_6_24~3
+X-Git-Url: http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff_plain;h=046c2c846b741a12e7fd61d8d86bf324a20e3dfc
+
+Reject extraneous data after SSL or GSS encryption handshake.
+
+The server collects up to a bufferload of data whenever it reads data
+from the client socket.  When SSL or GSS encryption is requested
+during startup, any additional data received with the initial
+request message remained in the buffer, and would be treated as
+already-decrypted data once the encryption handshake completed.
+Thus, a man-in-the-middle with the ability to inject data into the
+TCP connection could stuff some cleartext data into the start of
+a supposedly encryption-protected database session.
+
+This could be abused to send faked SQL commands to the server,
+although that would only work if the server did not demand any
+authentication data.  (However, a server relying on SSL certificate
+authentication might well not do so.)
+
+To fix, throw a protocol-violation error if the internal buffer
+is not empty after the encryption handshake.
+
+Our thanks to Jacob Champion for reporting this problem.
+
+Security: CVE-2021-23214
+---
+ src/backend/libpq/pqcomm.c          | 12 ++++++++++++
+ src/backend/postmaster/postmaster.c | 13 +++++++++++++
+ src/include/libpq/libpq.h           |  1 +
+ 3 files changed, 26 insertions(+)
+
+diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
+index 2fe8699..ecedd59 100644
+--- a/src/backend/libpq/pqcomm.c
++++ b/src/backend/libpq/pqcomm.c
+@@ -1139,6 +1139,18 @@ pq_startmsgread(void)
+ 	PqCommReadingMsg = true;
+ }
+ 
++/* --------------------------------
++ *		pq_buffer_has_data		- is any buffered data available to read?
++ *
++ * This will *not* attempt to read more data.
++ * --------------------------------
++ */
++bool
++pq_buffer_has_data(void)
++{
++	return (PqRecvPointer < PqRecvLength);
++}
++
+ 
+ /* --------------------------------
+  *		pq_endmsgread	- finish reading message.
+diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
+index aab072c..dccb6f4 100644
+--- a/src/backend/postmaster/postmaster.c
++++ b/src/backend/postmaster/postmaster.c
+@@ -1985,6 +1985,19 @@ retry1:
+ 		if (SSLok == 'S' && secure_open_server(port) == -1)
+ 			return STATUS_ERROR;
+ #endif
++
++		/*
++		 * At this point we should have no data already buffered.  If we do,
++		 * it was received before we performed the SSL handshake, so it wasn't
++		 * encrypted and indeed may have been injected by a man-in-the-middle.
++		 * We report this case to the client.
++		 */
++		if (pq_buffer_has_data())
++			ereport(FATAL,
++					(errcode(ERRCODE_PROTOCOL_VIOLATION),
++					 errmsg("received unencrypted data after SSL request"),
++					 errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
++
+ 		/* regular startup packet, cancel, etc packet should follow... */
+ 		/* but not another SSL negotiation request */
+ 		return ProcessStartupPacket(port, true);
+diff --git a/src/include/libpq/libpq.h b/src/include/libpq/libpq.h
+index d634fd2..ad546cd 100644
+--- a/src/include/libpq/libpq.h
++++ b/src/include/libpq/libpq.h
+@@ -62,6 +62,7 @@ extern int	pq_getmessage(StringInfo s, int maxlen);
+ extern int	pq_getbyte(void);
+ extern int	pq_peekbyte(void);
+ extern int	pq_getbyte_if_available(unsigned char *c);
++extern bool pq_buffer_has_data(void);
+ extern int	pq_putbytes(const char *s, size_t len);
+ extern int	pq_flush(void);
+ extern int	pq_flush_if_writable(void);
diff --git a/postgresql-CVE-2021-23222.patch b/postgresql-CVE-2021-23222.patch
new file mode 100644
index 0000000..c395af8
--- /dev/null
+++ b/postgresql-CVE-2021-23222.patch
@@ -0,0 +1,89 @@
+From: Debian PostgreSQL Maintainers <team+postgresql at tracker.debian.org>
+Date: Tue, 27 Jun 2023 18:51:15 +0000
+Subject: CVE-2021-23222
+
+Origin: http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff_plain;h=d83cdfdca9d918bbbd6bb209139b94c954da7228
+Reviewed-by: Sylvain Beucler <beuc at debian.org>
+Last-Update: 2021-11-17
+
+X-Git-Tag: REL9_6_24~2
+X-Git-Url: http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff_plain;h=d83cdfdca9d918bbbd6bb209139b94c954da7228
+
+libpq: reject extraneous data after SSL or GSS encryption handshake.
+
+libpq collects up to a bufferload of data whenever it reads data from
+the socket.  When SSL or GSS encryption is requested during startup,
+any additional data received with the server's yes-or-no reply
+remained in the buffer, and would be treated as already-decrypted data
+once the encryption handshake completed.  Thus, a man-in-the-middle
+with the ability to inject data into the TCP connection could stuff
+some cleartext data into the start of a supposedly encryption-protected
+database session.
+
+This could probably be abused to inject faked responses to the
+client's first few queries, although other details of libpq's behavior
+make that harder than it sounds.  A different line of attack is to
+exfiltrate the client's password, or other sensitive data that might
+be sent early in the session.  That has been shown to be possible with
+a server vulnerable to CVE-2021-23214.
+
+To fix, throw a protocol-violation error if the internal buffer
+is not empty after the encryption handshake.
+
+Our thanks to Jacob Champion for reporting this problem.
+
+Security: CVE-2021-23222
+---
+ doc/src/sgml/protocol.sgml        | 14 ++++++++++++++
+ src/interfaces/libpq/fe-connect.c | 13 +++++++++++++
+ 2 files changed, 27 insertions(+)
+
+diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml
+index 4111570..9400e36 100644
+--- a/doc/src/sgml/protocol.sgml
++++ b/doc/src/sgml/protocol.sgml
+@@ -1307,6 +1307,20 @@
+     and proceed without requesting <acronym>SSL</acronym>.
+    </para>
+ 
++   <para>
++    When <acronym>SSL</acronym> encryption can be performed, the server
++    is expected to send only the single <literal>S</literal> byte and then
++    wait for the frontend to initiate an <acronym>SSL</acronym> handshake.
++    If additional bytes are available to read at this point, it likely
++    means that a man-in-the-middle is attempting to perform a
++    buffer-stuffing attack
++    (<ulink url="https://www.postgresql.org/support/security/CVE-2021-23222/">CVE-2021-23222</ulink>).
++    Frontends should be coded either to read exactly one byte from the
++    socket before turning the socket over to their SSL library, or to
++    treat it as a protocol violation if they find they have read additional
++    bytes.
++   </para>
++
+    <para>
+     An initial SSLRequest can also be used in a connection that is being
+     opened to send a CancelRequest message.
+diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
+index 27cadd0..dce3d00 100644
+--- a/src/interfaces/libpq/fe-connect.c
++++ b/src/interfaces/libpq/fe-connect.c
+@@ -2371,6 +2371,19 @@ keep_going:						/* We will come back to here until there is
+ 				pollres = pqsecure_open_client(conn);
+ 				if (pollres == PGRES_POLLING_OK)
+ 				{
++					/*
++					 * At this point we should have no data already buffered.
++					 * If we do, it was received before we performed the SSL
++					 * handshake, so it wasn't encrypted and indeed may have
++					 * been injected by a man-in-the-middle.
++					 */
++					if (conn->inCursor != conn->inEnd)
++					{
++						appendPQExpBufferStr(&conn->errorMessage,
++											 libpq_gettext("received unencrypted data after SSL response\n"));
++						goto error_return;
++					}
++
+ 					/* SSL handshake done, ready to send startup packet */
+ 					conn->status = CONNECTION_MADE;
+ 					return PGRES_POLLING_WRITING;
diff --git a/postgresql-CVE-2024-10977.patch b/postgresql-CVE-2024-10977.patch
new file mode 100644
index 0000000..15cd737
--- /dev/null
+++ b/postgresql-CVE-2024-10977.patch
@@ -0,0 +1,74 @@
+From 2a951ef0aace58026c31b9a88aeeda19c9af4205 Mon Sep 17 00:00:00 2001
+From: Michael Paquier <michael at paquier.xyz>
+Date: Mon, 11 Nov 2024 10:20:03 +0900
+Subject: [PATCH] libpq: Bail out during SSL/GSS negotiation errors
+
+This commit changes libpq so that errors reported by the backend during
+the protocol negotiation for SSL and GSS are discarded by the client, as
+these may include bytes that could be consumed by the client and write
+arbitrary bytes to a client's terminal.
+
+A failure with the SSL negotiation now leads to an error immediately
+reported, without a retry on any other methods allowed, like a fallback
+to a plaintext connection.
+
+A failure with GSS discards the error message received, and we allow a
+fallback as it may be possible that the error is caused by a connection
+attempt with a pre-11 server, GSS encryption having been introduced in
+v12.  This was a problem only with v17 and newer versions; older
+versions discard the error message already in this case, assuming a
+failure caused by a lack of support for GSS encryption.
+
+Author: Jacob Champion
+Reviewed-by: Peter Eisentraut, Heikki Linnakangas, Michael Paquier
+Security: CVE-2024-10977
+Backpatch-through: 12
+
+Origin: backport, https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=2a951ef0aace58026c31b9a88aeeda19c9af4205
+---
+ doc/src/sgml/protocol.sgml        |    8 ++++----
+ src/interfaces/libpq/fe-connect.c |   15 ++++++---------
+ 2 files changed, 10 insertions(+), 13 deletions(-)
+
+--- a/doc/src/sgml/protocol.sgml
++++ b/doc/src/sgml/protocol.sgml
+@@ -1298,10 +1298,10 @@
+ 
+    <para>
+     The frontend should also be prepared to handle an ErrorMessage
+-    response to SSLRequest from the server.  This would only occur if
+-    the server predates the addition of <acronym>SSL</acronym> support
+-    to <productname>PostgreSQL</>.  (Such servers are now very ancient,
+-    and likely do not exist in the wild anymore.)
++    response to SSLRequest from the server. The frontend should not display
++    this error message to the user/application, since the server has not been
++    authenticated
++    (<ulink url="https://www.postgresql.org/support/security/CVE-2024-10977/">CVE-2024-10977</ulink>).
+     In this case the connection must
+     be closed, but the frontend might choose to open a fresh connection
+     and proceed without requesting <acronym>SSL</acronym>.
+--- a/src/interfaces/libpq/fe-connect.c
++++ b/src/interfaces/libpq/fe-connect.c
+@@ -2345,16 +2345,13 @@
+ 					{
+ 						/*
+ 						 * Server failure of some sort, such as failure to
+-						 * fork a backend process.  We need to process and
+-						 * report the error message, which might be formatted
+-						 * according to either protocol 2 or protocol 3.
+-						 * Rather than duplicate the code for that, we flip
+-						 * into AWAITING_RESPONSE state and let the code there
+-						 * deal with it.  Note we have *not* consumed the "E"
+-						 * byte here.
++						 * fork a backend process.  Don't bother retrieving
++						 * the error message; we should not trust it as the
++						 * server has not been authenticated yet.
+ 						 */
+-						conn->status = CONNECTION_AWAITING_RESPONSE;
+-						goto keep_going;
++						appendPQExpBuffer(&conn->errorMessage,
++										  libpq_gettext("server sent an error response during SSL exchange\n"));
++						goto error_return;
+ 					}
+ 					else
+ 					{
diff --git a/postgresql-libxml2-2.13-tests.patch b/postgresql-libxml2-2.13-tests.patch
new file mode 100644
index 0000000..f1f5365
--- /dev/null
+++ b/postgresql-libxml2-2.13-tests.patch
@@ -0,0 +1,34 @@
+libxml2 >= 2.13 no longer adds a second "chunk is not well balanced" error
+after the first one; the error raised is unchanged.
+
+diff -urNp postgresql-9.3.25.orig/src/test/regress/expected/xml.out postgresql-9.3.25/src/test/regress/expected/xml.out
+--- postgresql-9.3.25.orig/src/test/regress/expected/xml.out
++++ postgresql-9.3.25/src/test/regress/expected/xml.out
+@@ -211,17 +211,11 @@ ERROR:  invalid XML content
+ DETAIL:  line 1: xmlParseEntityRef: no name
+ <invalidentity>&</invalidentity>
+                 ^
+-line 1: chunk is not well balanced
+-<invalidentity>&</invalidentity>
+-                                ^
+ SELECT xmlparse(content '<undefinedentity>&idontexist;</undefinedentity>');
+ ERROR:  invalid XML content
+ DETAIL:  line 1: Entity 'idontexist' not defined
+ <undefinedentity>&idontexist;</undefinedentity>
+                              ^
+-line 1: chunk is not well balanced
+-<undefinedentity>&idontexist;</undefinedentity>
+-                                               ^
+ SELECT xmlparse(content '<invalidns xmlns=''<''/>');
+          xmlparse          
+ ---------------------------
+@@ -242,9 +236,6 @@ DETAIL:  line 1: Entity 'idontexist' not
+ line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
+ <twoerrors>&idontexist;</unbalanced>
+                                     ^
+-line 1: chunk is not well balanced
+-<twoerrors>&idontexist;</unbalanced>
+-                                    ^
+ SELECT xmlparse(content '<nosuchprefix:tag/>');
+       xmlparse       
+ ---------------------
diff --git a/postgresql-openldap25.patch b/postgresql-openldap25.patch
new file mode 100644
index 0000000..c58534e
--- /dev/null
+++ b/postgresql-openldap25.patch
@@ -0,0 +1,24 @@
+OpenLDAP 2.5+ merged libldap_r into libldap; configure aborts without it.
+Backport of upstream 3c612d9f6 (REL9_6_STABLE) / 53c38a086 (master).
+
+diff -urNp postgresql-9.3.25.orig/configure.in postgresql-9.3.25/configure.in
+--- postgresql-9.3.25.orig/configure.in
++++ postgresql-9.3.25/configure.in
+@@ -1636,11 +1636,13 @@ if test "$with_ldap" = yes ; then
+ 		 [$EXTRA_LDAP_LIBS])
+     LDAP_LIBS_BE="-lldap $EXTRA_LDAP_LIBS"
+     if test "$enable_thread_safety" = yes; then
+-      # on some platforms ldap_r fails to link without PTHREAD_LIBS
+-      AC_CHECK_LIB(ldap_r, ldap_simple_bind, [],
+-		   [AC_MSG_ERROR([library 'ldap_r' is required for LDAP])],
++      # Use ldap_r for FE if available, else assume ldap is thread-safe.
++      # On some platforms ldap_r fails to link without PTHREAD_LIBS.
++      LIBS="$_LIBS"
++      AC_CHECK_LIB(ldap_r, ldap_bind,
++		   [LDAP_LIBS_FE="-lldap_r $EXTRA_LDAP_LIBS"],
++		   [LDAP_LIBS_FE="-lldap $EXTRA_LDAP_LIBS"],
+ 		   [$PTHREAD_CFLAGS $PTHREAD_LIBS $EXTRA_LDAP_LIBS])
+-      LDAP_LIBS_FE="-lldap_r $EXTRA_LDAP_LIBS"
+     else
+       LDAP_LIBS_FE="-lldap $EXTRA_LDAP_LIBS"
+     fi
diff --git a/postgresql-tls13-key-update.patch b/postgresql-tls13-key-update.patch
new file mode 100644
index 0000000..b10778f
--- /dev/null
+++ b/postgresql-tls13-key-update.patch
@@ -0,0 +1,28 @@
+TLS 1.3 has no renegotiation: with ssl_renegotiation_limit > 0 every TLS 1.3
+connection is killed once the limit is reached.  Use a key update instead.
+
+diff -urNp postgresql-9.3.25.orig/src/backend/libpq/be-secure.c postgresql-9.3.25/src/backend/libpq/be-secure.c
+--- postgresql-9.3.25.orig/src/backend/libpq/be-secure.c
++++ postgresql-9.3.25/src/backend/libpq/be-secure.c
+@@ -339,6 +339,21 @@ secure_write(Port *port, void *ptr, size
+ 		 * progress.  Request the renegotiation 1kB before the limit has
+ 		 * actually expired.
+ 		 */
++#ifdef TLS1_3_VERSION
++		if (ssl_renegotiation_limit &&
++			port->count > (ssl_renegotiation_limit - 1) * 1024L &&
++			SSL_version(port->ssl) >= TLS1_3_VERSION)
++		{
++			/*
++			 * TLS 1.3 has no renegotiation; a key update renews the traffic
++			 * keys in both directions.  If one is still pending, retry on
++			 * the next write.
++			 */
++			if (SSL_key_update(port->ssl, SSL_KEY_UPDATE_REQUESTED) == 1)
++				port->count = 0;
++		}
++		else
++#endif
+ 		if (ssl_renegotiation_limit && !in_ssl_renegotiation &&
+ 			port->count > (ssl_renegotiation_limit - 1) * 1024L)
+ 		{
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/postgresql.git/commitdiff/9b37e96156b8ace87d172da54cf51615b29a7bfb



More information about the pld-cvs-commit mailing list