Skip to content

Commit 046c2c8

Browse files
committed
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
1 parent 9b6194e commit 046c2c8

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/backend/libpq/pqcomm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,18 @@ pq_getstring(StringInfo s)
11761176
}
11771177
}
11781178

1179+
/* --------------------------------
1180+
* pq_buffer_has_data - is any buffered data available to read?
1181+
*
1182+
* This will *not* attempt to read more data.
1183+
* --------------------------------
1184+
*/
1185+
bool
1186+
pq_buffer_has_data(void)
1187+
{
1188+
return (PqRecvPointer < PqRecvLength);
1189+
}
1190+
11791191

11801192
/* --------------------------------
11811193
* pq_startmsgread - begin reading a message from the client.

src/backend/postmaster/postmaster.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,19 @@ ProcessStartupPacket(Port *port, bool SSLdone)
20252025
if (SSLok == 'S' && secure_open_server(port) == -1)
20262026
return STATUS_ERROR;
20272027
#endif
2028+
2029+
/*
2030+
* At this point we should have no data already buffered. If we do,
2031+
* it was received before we performed the SSL handshake, so it wasn't
2032+
* encrypted and indeed may have been injected by a man-in-the-middle.
2033+
* We report this case to the client.
2034+
*/
2035+
if (pq_buffer_has_data())
2036+
ereport(FATAL,
2037+
(errcode(ERRCODE_PROTOCOL_VIOLATION),
2038+
errmsg("received unencrypted data after SSL request"),
2039+
errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
2040+
20282041
/* regular startup packet, cancel, etc packet should follow... */
20292042
/* but not another SSL negotiation request */
20302043
return ProcessStartupPacket(port, true);

src/include/libpq/libpq.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ extern int pq_getmessage(StringInfo s, int maxlen);
7171
extern int pq_getbyte(void);
7272
extern int pq_peekbyte(void);
7373
extern int pq_getbyte_if_available(unsigned char *c);
74+
extern bool pq_buffer_has_data(void);
7475
extern int pq_putbytes(const char *s, size_t len);
7576

7677
/*

0 commit comments

Comments
 (0)