Re: [PATCH] pageinspect: validate line pointers before using them

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] pageinspect: validate line pointers before using them
Date: 2026-09-01 00:13:32
Message-ID: CAGRkXqQ+NvzXW+2AjkT1ACR-W+y-cDoy+mzLXriMAV2ovVRA5g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I went looking at the other access methods and found a worse case than the
one I first reported, so here is a two-patch series.

0001 is what I sent before, unchanged: GiST, B-tree and hash page_items()
follow a line pointer after only checking ItemIdIsValid(), which just tests
that the pointer isn't NULL -- and it never is.

0002 is the new part. Nothing checks the page header at all, and pd_lower
is what PageGetMaxOffsetNumber() is computed from, so two bytes are enough
to walk off the end of the line pointer array.

That includes heap_page_items() crash on a corrupt page:
CREATE TABLE t AS SELECT i FROM generate_series(1,200) i;
-- pd_lower is at offset 12
SELECT heap_page_items(set_byte(set_byte(get_raw_page('t',0),12,255),13,255));
==ERROR: AddressSanitizer: heap-buffer-overflow READ of size 4
#0 heap_page_items heapfuncs.c:188

So 0002 adds verify_page_header(), using the same checks PageIsVerified()
makes on the header. I didn't call PageIsVerified() itself because it also
verifies the checksum, and there is no block number for a raw bytea; happy
to factor those checks into a shared helper instead, if that's preferred.
I also kept it out of get_page_from_raw(), so page_header() can still show
you have a damaged header.
While I was there, BRIN and GIN turned out to have the same problem as
0001, so 0002 fixes those too.

Thanks,
Shihao

Attachment Content-Type Size
0002-pageinspect-validate-page-headers-and-BRIN-and-GIN-p.patch application/octet-stream 11.9 KB
0001-pageinspect-validate-line-pointers-before-using-them.patch application/octet-stream 10.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2026-09-01 00:14:50 Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
Previous Message Zsolt Parragi 2026-08-31 23:47:29 Re: Routed ON CONFLICT inserts broken by partition-local deferrable unique constraints in 19 and master