diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2021-04-28 15:50:42 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2021-04-28 15:50:46 -0400 |
commit | 9626325da5e8e23ff90091bc96535495d350f06e (patch) | |
tree | 3efd2c81eed57367390df8a737ad76b89cabc4ee /src/backend/tcop/postgres.c | |
parent | d6b8d29419df0efad57f95c80b871745d1b55da6 (diff) | |
download | postgresql-9626325da5e8e23ff90091bc96535495d350f06e.tar.gz postgresql-9626325da5e8e23ff90091bc96535495d350f06e.zip |
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
Diffstat (limited to 'src/backend/tcop/postgres.c')
-rw-r--r-- | src/backend/tcop/postgres.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 1216a2b397b..2d6d145ecc0 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -343,6 +343,7 @@ static int SocketBackend(StringInfo inBuf) { int qtype; + int maxmsglen; /* * Get message type code from the frontend. @@ -375,7 +376,9 @@ SocketBackend(StringInfo inBuf) /* * Validate message type code before trying to read body; if we have lost * sync, better to say "command unknown" than to run out of memory because - * we used garbage as a length word. + * we used garbage as a length word. We can also select a type-dependent + * limit on what a sane length word could be. (The limit could be chosen + * more granularly, but it's not clear it's worth fussing over.) * * This also gives us a place to set the doing_extended_query_message flag * as soon as possible. @@ -383,28 +386,37 @@ SocketBackend(StringInfo inBuf) switch (qtype) { case 'Q': /* simple query */ + maxmsglen = PQ_LARGE_MESSAGE_LIMIT; doing_extended_query_message = false; break; case 'F': /* fastpath function call */ + maxmsglen = PQ_LARGE_MESSAGE_LIMIT; doing_extended_query_message = false; break; case 'X': /* terminate */ + maxmsglen = PQ_SMALL_MESSAGE_LIMIT; doing_extended_query_message = false; ignore_till_sync = false; break; case 'B': /* bind */ + case 'P': /* parse */ + maxmsglen = PQ_LARGE_MESSAGE_LIMIT; + doing_extended_query_message = true; + break; + case 'C': /* close */ case 'D': /* describe */ case 'E': /* execute */ case 'H': /* flush */ - case 'P': /* parse */ + maxmsglen = PQ_SMALL_MESSAGE_LIMIT; doing_extended_query_message = true; break; case 'S': /* sync */ + maxmsglen = PQ_SMALL_MESSAGE_LIMIT; /* stop any active skip-till-Sync */ ignore_till_sync = false; /* mark not-extended, so that a new error doesn't begin skip */ @@ -412,8 +424,13 @@ SocketBackend(StringInfo inBuf) break; case 'd': /* copy data */ + maxmsglen = PQ_LARGE_MESSAGE_LIMIT; + doing_extended_query_message = false; + break; + case 'c': /* copy done */ case 'f': /* copy fail */ + maxmsglen = PQ_SMALL_MESSAGE_LIMIT; doing_extended_query_message = false; break; @@ -427,6 +444,7 @@ SocketBackend(StringInfo inBuf) ereport(FATAL, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("invalid frontend message type %d", qtype))); + maxmsglen = 0; /* keep compiler quiet */ break; } @@ -435,7 +453,7 @@ SocketBackend(StringInfo inBuf) * after the type code; we can read the message contents independently of * the type. */ - if (pq_getmessage(inBuf, 0)) + if (pq_getmessage(inBuf, maxmsglen)) return EOF; /* suitable message already logged */ RESUME_CANCEL_INTERRUPTS(); |