postgresql/src/include
Heikki Linnakangas 57ec87c6b8 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
2015-02-02 17:09:46 +02:00
..
access Fix query-duration memory leak with GIN rescans. 2015-01-30 17:59:17 +01:00
bootstrap Fix off-by-one loop count in MapArrayTypeName, and get rid of static array. 2014-12-16 15:35:36 -05:00
catalog Fix assorted oversights in range selectivity estimation. 2015-01-30 12:31:08 -05:00
commands Improve performance of EXPLAIN with large range tables. 2015-01-15 13:18:16 -05:00
common Add pg_string_endswith as the start of a string helper library in src/common. 2015-01-03 20:54:13 +01:00
datatype pgindent run for 9.4 2014-05-06 12:12:18 -04:00
executor Ensure that RowExprs and whole-row Vars produce the expected column names. 2014-11-10 15:21:14 -05:00
foreign Update copyright for 2014 2014-01-07 16:05:30 -05:00
lib pgindent run for 9.4 2014-05-06 12:12:18 -04:00
libpq Be more careful to not lose sync in the FE/BE protocol. 2015-02-02 17:09:46 +02:00
mb pgindent run for 9.4 2014-05-06 12:12:18 -04:00
nodes Ensure that RowExprs and whole-row Vars produce the expected column names. 2014-11-10 15:21:14 -05:00
optimizer Fix some more problems with nested append relations. 2014-10-01 19:31:18 -04:00
parser pgindent run for 9.4 2014-05-06 12:12:18 -04:00
port pgindent run for 9.4 2014-05-06 12:12:18 -04:00
portability pgindent run for 9.4 2014-05-06 12:12:18 -04:00
postmaster When a background worker exists with code 0, unregister it. 2014-05-07 17:44:42 -04:00
regex pgindent run for 9.4 2014-05-06 12:12:18 -04:00
replication Fix decoding of MULTI_INSERTs when rows other than the last are toasted. 2014-07-06 15:59:53 +02:00
rewrite pgindent run for 9.4 2014-05-06 12:12:18 -04:00
snowball pgindent run for 9.4 2014-05-06 12:12:18 -04:00
storage Don't skip SQL backends in logical decoding for visibility computation. 2014-12-02 23:52:44 +01:00
tcop Be more careful to not lose sync in the FE/BE protocol. 2015-02-02 17:09:46 +02:00
tsearch Allow opclasses to provide tri-valued GIN consistent functions. 2014-03-12 17:51:30 +02:00
utils Improve performance of EXPLAIN with large range tables. 2015-01-15 13:18:16 -05:00
.gitignore Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
c.h pgindent run for 9.4 2014-05-06 12:12:18 -04:00
fmgr.h Redesign API presented by nodeAgg.c for ordered-set and similar aggregates. 2014-07-03 18:25:37 -04:00
funcapi.h pgindent run for 9.4 2014-05-06 12:12:18 -04:00
getaddrinfo.h Fix assorted issues in client host name lookup. 2014-04-02 17:11:24 -04:00
getopt_long.h Centralize getopt-related declarations in a new header file pg_getopt.h. 2014-02-15 14:31:30 -05:00
Makefile Remove maintainer-check target, fold into normal build 2013-10-10 20:11:56 -04:00
miscadmin.h Be more careful to not lose sync in the FE/BE protocol. 2015-02-02 17:09:46 +02:00
pg_config.h.in On Darwin, detect and report a multithreaded postmaster. 2015-01-07 22:36:35 -05:00
pg_config.h.win32 Stamp 9.4.0. 2014-12-15 20:07:34 -05:00
pg_config_ext.h.in Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_ext.h.win32 Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_manual.h Move PG_AUTOCONF_FILENAME definition 2014-12-03 19:58:13 -05:00
pg_getopt.h Fix comment. 2014-05-08 12:42:56 -04:00
pg_trace.h Update copyright for 2014 2014-01-07 16:05:30 -05:00
pgstat.h Save pg_stat_statements statistics file into $PGDATA/pg_stat directory at shutdown. 2014-06-04 12:09:45 +09:00
pgtar.h Update copyright for 2014 2014-01-07 16:05:30 -05:00
pgtime.h Support timezone abbreviations that sometimes change. 2014-10-16 15:22:13 -04:00
port.h Fix libpq's behavior when /etc/passwd isn't readable. 2015-01-11 12:35:47 -05:00
postgres.h pgindent run for 9.4 2014-05-06 12:12:18 -04:00
postgres_ext.h pgindent run for 9.4 2014-05-06 12:12:18 -04:00
postgres_fe.h Update copyright for 2014 2014-01-07 16:05:30 -05:00
rusagestub.h Update copyright for 2014 2014-01-07 16:05:30 -05:00
windowapi.h Update copyright for 2014 2014-01-07 16:05:30 -05:00