]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add additional sanity checks when reading a blkreftable.
authorRobert Haas <rhaas@postgresql.org>
Wed, 15 Jul 2026 17:04:32 +0000 (13:04 -0400)
committerRobert Haas <rhaas@postgresql.org>
Wed, 15 Jul 2026 17:13:56 +0000 (13:13 -0400)
Code elsewhere in the system assumes that fork numbers and chunk sizes
are within bounds, so the code that reads those quantities from disk
should validate that they are. Without these additional checks, a
corrupted file can cause us to index off the end of fork number or chunk
entry arrays, potentially resulting in a crash.

Reported-by: oxsignal <awo@kakao.com> (chunk sizes)
Reported-by: Robert Haas <rhaas@postgresql.org> (fork numbers)
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: http://postgr.es/m/CA+TgmoYP8RKoBGosS7C6Fdr-GNCfyz_W1zmK=Tx1Fe0ZvzGh0g@mail.gmail.com
Backpatch-through: 17

src/common/blkreftable.c

index 5c85d6a5c6d427fd07d9be43f73167d88f57cf48..b33687a9f36610425b00623d55a62ce19c7afb84 100644 (file)
@@ -657,6 +657,15 @@ BlockRefTableReaderNextRelation(BlockRefTableReader *reader,
                return false;
        }
 
+       /* Sanity-check the fork number. */
+       if (sentry.forknum < 0 || sentry.forknum > MAX_FORKNUM)
+       {
+               reader->error_callback(reader->error_callback_arg,
+                                                          "file \"%s\" has invalid fork number %d",
+                                                          reader->error_filename, sentry.forknum);
+               return false;
+       }
+
        /*
         * Sanity-check the nchunks value.  In the backend, palloc_array would
         * enforce this anyway (with a more generic error message); but in
@@ -678,6 +687,19 @@ BlockRefTableReaderNextRelation(BlockRefTableReader *reader,
        BlockRefTableRead(reader, reader->chunk_size,
                                          sentry.nchunks * sizeof(uint16));
 
+       /* Sanity-check the chunk sizes. */
+       for (unsigned i = 0; i < sentry.nchunks; ++i)
+       {
+               if (reader->chunk_size[i] > MAX_ENTRIES_PER_CHUNK)
+               {
+                       reader->error_callback(reader->error_callback_arg,
+                                                                  "file \"%s\" chunk %u has invalid size %u",
+                                                                  reader->error_filename, i,
+                                                                  (unsigned) reader->chunk_size[i]);
+                       return false;
+               }
+       }
+
        /* Set up for chunk scan. */
        reader->total_chunks = sentry.nchunks;
        reader->consumed_chunks = 0;