Skip to content

Commit e65d9c8

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 9ae0f11 commit e65d9c8

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
@@ -1348,6 +1348,20 @@
13481348
and proceed without requesting <acronym>SSL</acronym>.
13491349
</para>
13501350

1351+
<para>
1352+
When <acronym>SSL</acronym> encryption can be performed, the server
1353+
is expected to send only the single <literal>S</literal> byte and then
1354+
wait for the frontend to initiate an <acronym>SSL</acronym> handshake.
1355+
If additional bytes are available to read at this point, it likely
1356+
means that a man-in-the-middle is attempting to perform a
1357+
buffer-stuffing attack
1358+
(<ulink url="https://www.postgresql.org/support/security/CVE-2021-23222/">CVE-2021-23222</ulink>).
1359+
Frontends should be coded either to read exactly one byte from the
1360+
socket before turning the socket over to their SSL library, or to
1361+
treat it as a protocol violation if they find they have read additional
1362+
bytes.
1363+
</para>
1364+
13511365
<para>
13521366
An initial SSLRequest can also be used in a connection that is being
13531367
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
@@ -2719,6 +2719,19 @@ PQconnectPoll(PGconn *conn)
27192719
pollres = pqsecure_open_client(conn);
27202720
if (pollres == PGRES_POLLING_OK)
27212721
{
2722+
/*
2723+
* At this point we should have no data already buffered.
2724+
* If we do, it was received before we performed the SSL
2725+
* handshake, so it wasn't encrypted and indeed may have
2726+
* been injected by a man-in-the-middle.
2727+
*/
2728+
if (conn->inCursor != conn->inEnd)
2729+
{
2730+
appendPQExpBufferStr(&conn->errorMessage,
2731+
libpq_gettext("received unencrypted data after SSL response\n"));
2732+
goto error_return;
2733+
}
2734+
27222735
/* SSL handshake done, ready to send startup packet */
27232736
conn->status = CONNECTION_MADE;
27242737
return PGRES_POLLING_WRITING;

0 commit comments

Comments
 (0)