Skip to content

Commit e92ed93

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 7c0a78f commit e92ed93

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/backend/libpq/pqcomm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,18 @@ pq_getstring(StringInfo s)
11831183
}
11841184
}
11851185

1186+
/* --------------------------------
1187+
* pq_buffer_has_data - is any buffered data available to read?
1188+
*
1189+
* This will *not* attempt to read more data.
1190+
* --------------------------------
1191+
*/
1192+
bool
1193+
pq_buffer_has_data(void)
1194+
{
1195+
return (PqRecvPointer < PqRecvLength);
1196+
}
1197+
11861198

11871199
/* --------------------------------
11881200
* pq_startmsgread - begin reading a message from the client.

src/backend/postmaster/postmaster.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,18 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
20492049
return STATUS_ERROR;
20502050
#endif
20512051

2052+
/*
2053+
* At this point we should have no data already buffered. If we do,
2054+
* it was received before we performed the SSL handshake, so it wasn't
2055+
* encrypted and indeed may have been injected by a man-in-the-middle.
2056+
* We report this case to the client.
2057+
*/
2058+
if (pq_buffer_has_data())
2059+
ereport(FATAL,
2060+
(errcode(ERRCODE_PROTOCOL_VIOLATION),
2061+
errmsg("received unencrypted data after SSL request"),
2062+
errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
2063+
20522064
/*
20532065
* regular startup packet, cancel, etc packet should follow, but not
20542066
* another SSL negotiation request, and a GSS request should only
@@ -2081,6 +2093,18 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
20812093
return STATUS_ERROR;
20822094
#endif
20832095

2096+
/*
2097+
* At this point we should have no data already buffered. If we do,
2098+
* it was received before we performed the GSS handshake, so it wasn't
2099+
* encrypted and indeed may have been injected by a man-in-the-middle.
2100+
* We report this case to the client.
2101+
*/
2102+
if (pq_buffer_has_data())
2103+
ereport(FATAL,
2104+
(errcode(ERRCODE_PROTOCOL_VIOLATION),
2105+
errmsg("received unencrypted data after GSSAPI encryption request"),
2106+
errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
2107+
20842108
/*
20852109
* regular startup packet, cancel, etc packet should follow, but not
20862110
* another GSS negotiation request, and an SSL request should only

src/include/libpq/libpq.h

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

7778
/*

0 commit comments

Comments
 (0)