Skip to content

Commit d8f5acb

Browse files
committed
Fix potential stack overflow in incremental backup.
The user can set RELSEG_SIZE to a high number at compile time, so we can't use it to control the size of an array on the stack: it could be many gigabytes in size. On closer inspection, we don't really need that intermediate array anyway. Let's just write directly into the output array, and then perform the absolute->relative adjustment in place. This fixes new code from commit dc21234. Reviewed-by: Robert Haas <robertmhaas@gmail.com> Discussion: https://postgr.es/m/CA%2BhUKG%2B2hZ0sBztPW4mkLfng0qfkNtAHFUfxOMLizJ0BPmi5%2Bg%40mail.gmail.com
1 parent f56a9de commit d8f5acb

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/backend/backup/basebackup_incremental.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ GetIncrementalFilePath(Oid dboid, Oid spcoid, RelFileNumber relfilenumber,
726726
* How should we back up a particular file as part of an incremental backup?
727727
*
728728
* If the return value is BACK_UP_FILE_FULLY, caller should back up the whole
729-
* file just as if this were not an incremental backup.
729+
* file just as if this were not an incremental backup. The contents of the
730+
* relative_block_numbers array is unspecified in this case.
730731
*
731732
* If the return value is BACK_UP_FILE_INCREMENTALLY, caller should include
732733
* an incremental file in the backup instead of the entire file. On return,
@@ -745,7 +746,6 @@ GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path,
745746
BlockNumber *relative_block_numbers,
746747
unsigned *truncation_block_length)
747748
{
748-
BlockNumber absolute_block_numbers[RELSEG_SIZE];
749749
BlockNumber limit_block;
750750
BlockNumber start_blkno;
751751
BlockNumber stop_blkno;
@@ -872,8 +872,13 @@ GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path,
872872
errcode(ERRCODE_INTERNAL_ERROR),
873873
errmsg_internal("overflow computing block number bounds for segment %u with size %zu",
874874
segno, size));
875+
876+
/*
877+
* This will write *absolute* block numbers into the output array, but
878+
* we'll transpose them below.
879+
*/
875880
nblocks = BlockRefTableEntryGetBlocks(brtentry, start_blkno, stop_blkno,
876-
absolute_block_numbers, RELSEG_SIZE);
881+
relative_block_numbers, RELSEG_SIZE);
877882
Assert(nblocks <= RELSEG_SIZE);
878883

879884
/*
@@ -892,19 +897,22 @@ GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path,
892897
return BACK_UP_FILE_FULLY;
893898

894899
/*
895-
* Looks like we can send an incremental file, so sort the absolute the
896-
* block numbers and then transpose absolute block numbers to relative
897-
* block numbers.
900+
* Looks like we can send an incremental file, so sort the block numbers
901+
* and then transpose them from absolute block numbers to relative block
902+
* numbers if necessary.
898903
*
899904
* NB: If the block reference table was using the bitmap representation
900905
* for a given chunk, the block numbers in that chunk will already be
901906
* sorted, but when the array-of-offsets representation is used, we can
902907
* receive block numbers here out of order.
903908
*/
904-
qsort(absolute_block_numbers, nblocks, sizeof(BlockNumber),
909+
qsort(relative_block_numbers, nblocks, sizeof(BlockNumber),
905910
compare_block_numbers);
906-
for (i = 0; i < nblocks; ++i)
907-
relative_block_numbers[i] = absolute_block_numbers[i] - start_blkno;
911+
if (start_blkno != 0)
912+
{
913+
for (i = 0; i < nblocks; ++i)
914+
relative_block_numbers[i] -= start_blkno;
915+
}
908916
*num_blocks_required = nblocks;
909917

910918
/*

0 commit comments

Comments
 (0)