pgsql: Add heuristic incoming-message-size limits in the server.

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Add heuristic incoming-message-size limits in the server.
Дата
Msg-id E1lbqD3-0008Q1-Aj@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Add heuristic incoming-message-size limits in the server.

We had a report of confusing server behavior caused by a client bug
that sent junk to the server: the server thought the junk was a
very long message length and waited patiently for data that would
never come.  We can reduce the risk of that by being less trusting
about message lengths.

For a long time, libpq has had a heuristic rule that it wouldn't
believe large message size words, except for a small number of
message types that are expected to be (potentially) long.  This
provides some defense against loss of message-boundary sync and
other corrupted-data cases.  The server does something similar,
except that up to now it only limited the lengths of messages
received during the connection authentication phase.  Let's
do the same as in libpq and put restrictions on the allowed
length of all messages, while distinguishing between message
types that are expected to be long and those that aren't.

I used a limit of 10000 bytes for non-long messages.  (libpq's
corresponding limit is 30000 bytes, but given the asymmetry of
the FE/BE protocol, there's no good reason why the numbers should
be the same.)  Experimentation suggests that this is at least a
factor of 10, maybe a factor of 100, more than we really need;
but plenty of daylight seems desirable to avoid false positives.
In any case we can adjust the limit based on beta-test results.

For long messages, set a limit of MaxAllocSize - 1, which is the
most that we can absorb into the StringInfo buffer that the message
is collected in.  This just serves to make sure that a bogus message
size is reported as such, rather than as a confusing gripe about
not being able to enlarge a string buffer.

While at it, make sure that non-mainline code paths (such as
COPY FROM STDIN) are as paranoid as SocketBackend is, and validate
the message type code before believing the message length.
This provides an additional guard against getting stuck on corrupted
input.

Discussion: https://postgr.es/m/2003757.1619373089@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/9626325da5e8e23ff90091bc96535495d350f06e

Modified Files
--------------
src/backend/commands/copyfromparse.c | 31 +++++++++++++++++++++++++------
src/backend/libpq/auth.c             |  3 ++-
src/backend/libpq/pqcomm.c           |  5 ++---
src/backend/replication/walsender.c  | 29 +++++++++++++++++++++++------
src/backend/tcop/postgres.c          | 24 +++++++++++++++++++++---
src/include/libpq/libpq.h            |  9 +++++++++
6 files changed, 82 insertions(+), 19 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Alvaro Herrera
Дата:
Сообщение: pgsql: Allow a partdesc-omitting-partitions to be cached
Следующее
От: Michael Paquier
Дата:
Сообщение: pgsql: doc: Fix description of target_session_attrs=prefer-standby