diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2015-02-02 17:08:56 +0200 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2015-02-02 17:09:46 +0200 |
commit | 57ec87c6b8dcb5258aae414fbdbeaf6eaf09feb1 (patch) | |
tree | c6351ed1dec62165dc147df890fca1ffdb9a951a /src/backend/tcop/fastpath.c | |
parent | ca84dfa90e9929737bda7393f88b488cb7161147 (diff) | |
download | postgresql-57ec87c6b8dcb5258aae414fbdbeaf6eaf09feb1.tar.gz postgresql-57ec87c6b8dcb5258aae414fbdbeaf6eaf09feb1.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/tcop/fastpath.c')
-rw-r--r-- | src/backend/tcop/fastpath.c | 29 |
1 files changed, 1 insertions, 28 deletions
diff --git a/src/backend/tcop/fastpath.c b/src/backend/tcop/fastpath.c index 9f50c5add58..6614a12f1dd 100644 --- a/src/backend/tcop/fastpath.c +++ b/src/backend/tcop/fastpath.c @@ -75,7 +75,7 @@ static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip, * The caller should already have initialized buf to empty. * ---------------- */ -static int +int GetOldFunctionMessage(StringInfo buf) { int32 ibuf; @@ -281,33 +281,6 @@ HandleFunctionRequest(StringInfo msgBuf) char msec_str[32]; /* - * Read message contents if not already done. - */ - if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3) - { - if (GetOldFunctionMessage(msgBuf)) - { - if (IsTransactionState()) - ereport(COMMERROR, - (errcode(ERRCODE_CONNECTION_FAILURE), - errmsg("unexpected EOF on client connection with an open transaction"))); - else - { - /* - * Can't send DEBUG log messages to client at this point. - * Since we're disconnecting right away, we don't need to - * restore whereToSendOutput. - */ - whereToSendOutput = DestNone; - ereport(DEBUG1, - (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST), - errmsg("unexpected EOF on client connection"))); - } - return EOF; - } - } - - /* * Now that we've eaten the input message, check to see if we actually * want to do the function call or not. It's now safe to ereport(); we * won't lose sync with the frontend. |