From: Markus Wanner <markus.wanner@2ndquadrant.com>
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Markus Wanner <markus.wanner@2ndquadrant.com>
To: pgsql-bugs@postgresql.org, Robert Haas <robertmhaas@gmail.com>
Subject: Re: invalid alloc size error possible in shm_mq
Date: Wed, 9 Sep 2020 14:37:17 +0200
Message-ID: <d0761291-3ac5-3b3c-c6cd-0f89be3de2b9@2ndquadrant.com> (raw)
In-Reply-To: <3e428657-26ce-ed05-a7cf-7f2a546c2672@2ndquadrant.com>
References: <3bb363e7-ac04-0ac4-9fe8-db1148755bfa@2ndquadrant.com>
<3e428657-26ce-ed05-a7cf-7f2a546c2672@2ndquadrant.com>
On 8/25/20 12:00 PM, Peter Eisentraut wrote:
> I wonder if the assertion is appropriate or whether it should be a full
> error check.
Good point. Originally, it used to be an error. With the patch (but
w/o assertions enabled) it could result in a buffer overrun. Not good.
I changed the patch to add an error (instead of just an assert) when
asked to read a message larger than MaxAllocSize. So this patch
essentially corrects handling of messages in size between MaxAllocSize/2
and MaxAllocSize.
> Is anything on the sending side ensuring that the maximum
> size is kept? All the size variables are Size/size_t so could be much
> larger than MaxAllocSize.
In this v2 of the patch, I added a check that errors out on the sender
side as well (for trying to send a message larger than MaxAllocSize).
I'd still recommend to back-patch this.
--
Markus Wanner
Senior PostgreSQL Developer
2ndQuadrant - PostgreSQL Solutions for the Enterprise
https://www.2ndQuadrant.com/Attachments:shm_mq_inv_allocation_fix_v2.diff (text/x-patch, 1.8K) downloaddiff --git a/src/backend/storage/ipc/shm_mq.c b/src/backend/storage/ipc/shm_mq.cindex 6f40fb165d..262faecf13 100644--- a/src/backend/storage/ipc/shm_mq.c+++ b/src/backend/storage/ipc/shm_mq.c@@ -24,6 +24,7 @@
#include "storage/procsignal.h"
#include "storage/shm_mq.h"
#include "storage/spin.h"
+#include "utils/memutils.h"
/*
* This structure represents the actual queue, stored in shared memory.
@@ -360,6 +361,13 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait)
for (i = 0; i < iovcnt; ++i)
nbytes += iov[i].len;
+ /* Prevent writing messages overwhelming the receiver. */+ if (nbytes > MaxAllocSize)+ ereport(ERROR,+ (errcode(ERRCODE_INTERNAL_ERROR),+ errmsg("cannot send a message of size %zu via shared memory queue",+ nbytes)));+
/* Try to write, or finish writing, the length word into the buffer. */
while (!mqh->mqh_length_word_complete)
{
@@ -675,6 +683,17 @@ shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
}
nbytes = mqh->mqh_expected_bytes;
+ /*+ * Should be disallowed on the sending side already, but better check+ * and error out on the receiver side as well, rather than trying to+ * read a prohibitively large message.+ */+ if (nbytes > MaxAllocSize)+ ereport(ERROR,+ (errcode(ERRCODE_INTERNAL_ERROR),+ errmsg("invalid message size %zu in shared memory queue",+ nbytes)));+
if (mqh->mqh_partial_bytes == 0)
{
/*
@@ -703,8 +722,13 @@ shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
{
Size newbuflen = Max(mqh->mqh_buflen, MQH_INITIAL_BUFSIZE);
+ /*+ * Double the buffer size until the payload fits, but limit to+ * MaxAllocSize.+ */
while (newbuflen < nbytes)
newbuflen *= 2;
+ newbuflen = Min(newbuflen, MaxAllocSize);
if (mqh->mqh_buffer != NULL)
{
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: markus.wanner@2ndquadrant.com, peter.eisentraut@2ndquadrant.com, robertmhaas@gmail.com
Subject: Re: invalid alloc size error possible in shm_mq
In-Reply-To: <d0761291-3ac5-3b3c-c6cd-0f89be3de2b9@2ndquadrant.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox