diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2015-02-02 17:08:45 +0200 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2015-02-02 17:09:53 +0200 |
commit | 2b3a8b20c2da9f39ffecae25ab7c66974fbc0d3b (patch) | |
tree | f0294b5a71c2b3c847446f4145d5d2a3247de8bb /src/backend/libpq/pqcomm.c | |
parent | 59b919822ab060f721e235964d19b55a19c815f0 (diff) | |
download | postgresql-2b3a8b20c2da9f39ffecae25ab7c66974fbc0d3b.tar.gz postgresql-2b3a8b20c2da9f39ffecae25ab7c66974fbc0d3b.zip |
Be more careful to not lose sync in the FE/BE protocol.
If any error occurred while we were in the middle of reading a protocol
message from the client, we could lose sync, and incorrectly try to
interpret a part of another message as a new protocol message. That will
usually lead to an "invalid frontend message" error that terminates the
connection. However, this is a security issue because an attacker might
be able to deliberately cause an error, inject a Query message in what's
supposed to be just user data, and have the server execute it.
We were quite careful to not have CHECK_FOR_INTERRUPTS() calls or other
operations that could ereport(ERROR) in the middle of processing a message,
but a query cancel interrupt or statement timeout could nevertheless cause
it to happen. Also, the V2 fastpath and COPY handling were not so careful.
It's very difficult to recover in the V2 COPY protocol, so we will just
terminate the connection on error. In practice, that's what happened
previously anyway, as we lost protocol sync.
To fix, add a new variable in pqcomm.c, PqCommReadingMsg, that is set
whenever we're in the middle of reading a message. When it's set, we cannot
safely ERROR out and continue running, because we might've read only part
of a message. PqCommReadingMsg acts somewhat similarly to critical sections
in that if an error occurs while it's set, the error handler will force the
connection to be terminated, as if the error was FATAL. It's not
implemented by promoting ERROR to FATAL in elog.c, like ERROR is promoted
to PANIC in critical sections, because we want to be able to use
PG_TRY/CATCH to recover and regain protocol sync. pq_getmessage() takes
advantage of that to prevent an OOM error from terminating the connection.
To prevent unnecessary connection terminations, add a holdoff mechanism
similar to HOLD/RESUME_INTERRUPTS() that can be used hold off query cancel
interrupts, but still allow die interrupts. The rules on which interrupts
are processed when are now a bit more complicated, so refactor
ProcessInterrupts() and the calls to it in signal handlers so that the
signal handlers always call it if ImmediateInterruptOK is set, and
ProcessInterrupts() can decide to not do anything if the other conditions
are not met.
Reported by Emil Lenngren. Patch reviewed by Noah Misch and Andres Freund.
Backpatch to all supported versions.
Security: CVE-2015-0244
Diffstat (limited to 'src/backend/libpq/pqcomm.c')
-rw-r--r-- | src/backend/libpq/pqcomm.c | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index e3efac34ce4..254fd8285b0 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -127,8 +127,9 @@ static int PqRecvLength; /* End of data available in PqRecvBuffer */ /* * Message status */ -static bool PqCommBusy; -static bool DoingCopyOut; +static bool PqCommBusy; /* busy sending data to the client */ +static bool PqCommReadingMsg; /* in the middle of reading a message */ +static bool DoingCopyOut; /* in old-protocol COPY OUT processing */ /* Internal functions */ @@ -177,6 +178,7 @@ pq_init(void) PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize); PqSendPointer = PqSendStart = PqRecvPointer = PqRecvLength = 0; PqCommBusy = false; + PqCommReadingMsg = false; DoingCopyOut = false; on_proc_exit(socket_close, 0); } @@ -916,6 +918,8 @@ pq_recvbuf(void) int pq_getbyte(void) { + Assert(PqCommReadingMsg); + while (PqRecvPointer >= PqRecvLength) { if (pq_recvbuf()) /* If nothing in buffer, then recv some */ @@ -954,6 +958,8 @@ pq_getbyte_if_available(unsigned char *c) { int r; + Assert(PqCommReadingMsg); + if (PqRecvPointer < PqRecvLength) { *c = PqRecvBuffer[PqRecvPointer++]; @@ -1006,6 +1012,8 @@ pq_getbytes(char *s, size_t len) { size_t amount; + Assert(PqCommReadingMsg); + while (len > 0) { while (PqRecvPointer >= PqRecvLength) @@ -1038,6 +1046,8 @@ pq_discardbytes(size_t len) { size_t amount; + Assert(PqCommReadingMsg); + while (len > 0) { while (PqRecvPointer >= PqRecvLength) @@ -1074,6 +1084,8 @@ pq_getstring(StringInfo s) { int i; + Assert(PqCommReadingMsg); + resetStringInfo(s); /* Read until we get the terminating '\0' */ @@ -1106,6 +1118,58 @@ pq_getstring(StringInfo s) /* -------------------------------- + * pq_startmsgread - begin reading a message from the client. + * + * This must be called before any of the pq_get* functions. + * -------------------------------- + */ +void +pq_startmsgread(void) +{ + /* + * There shouldn't be a read active already, but let's check just to be + * sure. + */ + if (PqCommReadingMsg) + ereport(FATAL, + (errcode(ERRCODE_PROTOCOL_VIOLATION), + errmsg("terminating connection because protocol sync was lost"))); + + PqCommReadingMsg = true; +} + + +/* -------------------------------- + * pq_endmsgread - finish reading message. + * + * This must be called after reading a V2 protocol message with + * pq_getstring() and friends, to indicate that we have read the whole + * message. In V3 protocol, pq_getmessage() does this implicitly. + * -------------------------------- + */ +void +pq_endmsgread(void) +{ + Assert(PqCommReadingMsg); + + PqCommReadingMsg = false; +} + +/* -------------------------------- + * pq_is_reading_msg - are we currently reading a message? + * + * This is used in error recovery at the outer idle loop to detect if we have + * lost protocol sync, and need to terminate the connection. pq_startmsgread() + * will check for that too, but it's nicer to detect it earlier. + * -------------------------------- + */ +bool +pq_is_reading_msg(void) +{ + return PqCommReadingMsg; +} + +/* -------------------------------- * pq_getmessage - get a message with length word from connection * * The return value is placed in an expansible StringInfo, which has @@ -1126,6 +1190,8 @@ pq_getmessage(StringInfo s, int maxlen) { int32 len; + Assert(PqCommReadingMsg); + resetStringInfo(s); /* Read message length word */ @@ -1167,6 +1233,9 @@ pq_getmessage(StringInfo s, int maxlen) ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("incomplete message from client"))); + + /* we discarded the rest of the message so we're back in sync. */ + PqCommReadingMsg = false; PG_RE_THROW(); } PG_END_TRY(); @@ -1184,6 +1253,9 @@ pq_getmessage(StringInfo s, int maxlen) s->data[len] = '\0'; } + /* finished reading the message. */ + PqCommReadingMsg = false; + return 0; } |