Skip to content

Commit a021a1d

Browse files
committed
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
1 parent 9394fb8 commit a021a1d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

doc/src/sgml/protocol.sgml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,20 @@ SELCT 1/0;
14671467
and proceed without requesting <acronym>SSL</acronym>.
14681468
</para>
14691469

1470+
<para>
1471+
When <acronym>SSL</acronym> encryption can be performed, the server
1472+
is expected to send only the single <literal>S</literal> byte and then
1473+
wait for the frontend to initiate an <acronym>SSL</acronym> handshake.
1474+
If additional bytes are available to read at this point, it likely
1475+
means that a man-in-the-middle is attempting to perform a
1476+
buffer-stuffing attack
1477+
(<ulink url="https://www.postgresql.org/support/security/CVE-2021-23222/">CVE-2021-23222</ulink>).
1478+
Frontends should be coded either to read exactly one byte from the
1479+
socket before turning the socket over to their SSL library, or to
1480+
treat it as a protocol violation if they find they have read additional
1481+
bytes.
1482+
</para>
1483+
14701484
<para>
14711485
An initial SSLRequest can also be used in a connection that is being
14721486
opened to send a CancelRequest message.

src/interfaces/libpq/fe-connect.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,19 @@ PQconnectPoll(PGconn *conn)
27202720
pollres = pqsecure_open_client(conn);
27212721
if (pollres == PGRES_POLLING_OK)
27222722
{
2723+
/*
2724+
* At this point we should have no data already buffered.
2725+
* If we do, it was received before we performed the SSL
2726+
* handshake, so it wasn't encrypted and indeed may have
2727+
* been injected by a man-in-the-middle.
2728+
*/
2729+
if (conn->inCursor != conn->inEnd)
2730+
{
2731+
appendPQExpBufferStr(&conn->errorMessage,
2732+
libpq_gettext("received unencrypted data after SSL response\n"));
2733+
goto error_return;
2734+
}
2735+
27232736
/* SSL handshake done, ready to send startup packet */
27242737
conn->status = CONNECTION_MADE;
27252738
return PGRES_POLLING_WRITING;

0 commit comments

Comments
 (0)