Skip to content

Commit

Permalink
Add overflow protection for block-related data in WAL records
Browse files Browse the repository at this point in the history
XLogRecordBlockHeader, the header holding the information for the data
related to a block, tracks the length of the data appended to the WAL
record with data_length (uint16).  This limitation in size was not
enforced by the public routine in charge of registering the data
assembled later to form the WAL record inserted, XLogRegisterBufData().
Incorrectly used, it could lead to the generation of records with some
of its data overflowed.  This commit adds some safeguards to prevent
that for the block data, complaining immediately if attempting to add to
a record block information with a size larger than UINT16_MAX, which is
the limit implied by the internal logic.

Note that this also adjusts XLogRegisterData() and XLogRegisterBufData()
so as the length of the WAL record data given by the caller is unsigned,
matching with what gets stored in XLogRecData->len.

Extracted from a larger patch by the same author.  The original patch
includes more protections when assembling a record in full that will be
looked at separately later.

Author: Matthias van de Meent
Reviewed-by: Andres Freund, Heikki Linnakangas, Michael Paquier, David
Zhang
Discussion: https://postgr.es/m/CAEze2WgGiw+LZt+vHf8tWqB_6VxeLsMeoAuod0N=ij1q17n5pw@mail.gmail.com
  • Loading branch information
michaelpq committed Jul 27, 2022
1 parent 70988b7 commit ffd1b6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/backend/access/transam/xloginsert.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ XLogRegisterBlock(uint8 block_id, RelFileLocator *rlocator, ForkNumber forknum,
* XLogRecGetData().
*/
void
XLogRegisterData(char *data, int len)
XLogRegisterData(char *data, uint32 len)
{
XLogRecData *rdata;

Expand Down Expand Up @@ -386,7 +386,7 @@ XLogRegisterData(char *data, int len)
* limited)
*/
void
XLogRegisterBufData(uint8 block_id, char *data, int len)
XLogRegisterBufData(uint8 block_id, char *data, uint32 len)
{
registered_buffer *regbuf;
XLogRecData *rdata;
Expand All @@ -399,8 +399,16 @@ XLogRegisterBufData(uint8 block_id, char *data, int len)
elog(ERROR, "no block with id %d registered with WAL insertion",
block_id);

if (num_rdatas >= max_rdatas)
/*
* Check against max_rdatas and ensure we do not register more data per
* buffer than can be handled by the physical data format; i.e. that
* regbuf->rdata_len does not grow beyond what
* XLogRecordBlockHeader->data_length can hold.
*/
if (num_rdatas >= max_rdatas ||
regbuf->rdata_len + len > UINT16_MAX)
elog(ERROR, "too much WAL data");

rdata = &rdatas[num_rdatas++];

rdata->data = data;
Expand Down Expand Up @@ -756,12 +764,18 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,

if (needs_data)
{
/*
* When copying to XLogRecordBlockHeader, the length is narrowed
* to an uint16. Double-check that it is still correct.
*/
Assert(regbuf->rdata_len <= UINT16_MAX);

/*
* Link the caller-supplied rdata chain for this buffer to the
* overall list.
*/
bkpb.fork_flags |= BKPBLOCK_HAS_DATA;
bkpb.data_length = regbuf->rdata_len;
bkpb.data_length = (uint16) regbuf->rdata_len;
total_len += regbuf->rdata_len;

rdt_datas_last->next = regbuf->rdata_head;
Expand Down
4 changes: 2 additions & 2 deletions src/include/access/xloginsert.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ extern void XLogBeginInsert(void);
extern void XLogSetRecordFlags(uint8 flags);
extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info);
extern void XLogEnsureRecordSpace(int max_block_id, int ndatas);
extern void XLogRegisterData(char *data, int len);
extern void XLogRegisterData(char *data, uint32 len);
extern void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags);
extern void XLogRegisterBlock(uint8 block_id, RelFileLocator *rlocator,
ForkNumber forknum, BlockNumber blknum, char *page,
uint8 flags);
extern void XLogRegisterBufData(uint8 block_id, char *data, int len);
extern void XLogRegisterBufData(uint8 block_id, char *data, uint32 len);
extern void XLogResetInsertion(void);
extern bool XLogCheckBufferNeedsBackup(Buffer buffer);

Expand Down

0 comments on commit ffd1b6b

Please sign in to comment.