Skip to content

Commit 3f9132e

Browse files
committed
Be more wary of corrupt data in pageinspect's heap_page_items().
The original intent in heap_page_items() was to return nulls, not throw an error or crash, if an item was sufficiently corrupt that we couldn't safely extract data from it. However, commit d6061f8 utterly missed that memo, and not only put in an un-length-checked copy of the tuple's data section, but also managed to break the check on sane nulls-bitmap length. Either mistake could possibly lead to a SIGSEGV crash if the tuple is corrupt. Bug: #18896 Reported-by: Dmitry Kovalenko <d.kovalenko@postgrespro.ru> Author: Dmitry Kovalenko <d.kovalenko@postgrespro.ru> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/18896-add267b8e06663e3@postgresql.org Backpatch-through: 13
1 parent 6a3e578 commit 3f9132e

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

contrib/pageinspect/heapfuncs.c

+27-18
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,8 @@ heap_page_items(PG_FUNCTION_ARGS)
210210
lp_offset + lp_len <= raw_page_size)
211211
{
212212
HeapTupleHeader tuphdr;
213-
bytea *tuple_data_bytea;
214-
int tuple_data_len;
215213

216214
/* Extract information from the tuple header */
217-
218215
tuphdr = (HeapTupleHeader) PageGetItem(page, id);
219216

220217
values[4] = UInt32GetDatum(HeapTupleHeaderGetRawXmin(tuphdr));
@@ -226,31 +223,32 @@ heap_page_items(PG_FUNCTION_ARGS)
226223
values[9] = UInt32GetDatum(tuphdr->t_infomask);
227224
values[10] = UInt8GetDatum(tuphdr->t_hoff);
228225

229-
/* Copy raw tuple data into bytea attribute */
230-
tuple_data_len = lp_len - tuphdr->t_hoff;
231-
tuple_data_bytea = (bytea *) palloc(tuple_data_len + VARHDRSZ);
232-
SET_VARSIZE(tuple_data_bytea, tuple_data_len + VARHDRSZ);
233-
memcpy(VARDATA(tuple_data_bytea), (char *) tuphdr + tuphdr->t_hoff,
234-
tuple_data_len);
235-
values[13] = PointerGetDatum(tuple_data_bytea);
236-
237226
/*
238227
* We already checked that the item is completely within the raw
239228
* page passed to us, with the length given in the line pointer.
240-
* Let's check that t_hoff doesn't point over lp_len, before using
241-
* it to access t_bits and oid.
229+
* But t_hoff could be out of range, so check it before relying on
230+
* it to fetch additional info.
242231
*/
243232
if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
244233
tuphdr->t_hoff <= lp_len &&
245234
tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
246235
{
236+
int tuple_data_len;
237+
bytea *tuple_data_bytea;
238+
239+
/* Copy null bitmask and OID, if present */
247240
if (tuphdr->t_infomask & HEAP_HASNULL)
248241
{
249-
int bits_len;
250-
251-
bits_len =
252-
BITMAPLEN(HeapTupleHeaderGetNatts(tuphdr)) * BITS_PER_BYTE;
253-
values[11] = CStringGetTextDatum(bits_to_text(tuphdr->t_bits, bits_len));
242+
int bitmaplen;
243+
244+
bitmaplen = BITMAPLEN(HeapTupleHeaderGetNatts(tuphdr));
245+
/* better range-check the attribute count, too */
246+
if (bitmaplen <= tuphdr->t_hoff - SizeofHeapTupleHeader)
247+
values[11] =
248+
CStringGetTextDatum(bits_to_text(tuphdr->t_bits,
249+
bitmaplen * BITS_PER_BYTE));
250+
else
251+
nulls[11] = true;
254252
}
255253
else
256254
nulls[11] = true;
@@ -259,11 +257,22 @@ heap_page_items(PG_FUNCTION_ARGS)
259257
values[12] = HeapTupleHeaderGetOidOld(tuphdr);
260258
else
261259
nulls[12] = true;
260+
261+
/* Copy raw tuple data into bytea attribute */
262+
tuple_data_len = lp_len - tuphdr->t_hoff;
263+
tuple_data_bytea = (bytea *) palloc(tuple_data_len + VARHDRSZ);
264+
SET_VARSIZE(tuple_data_bytea, tuple_data_len + VARHDRSZ);
265+
if (tuple_data_len > 0)
266+
memcpy(VARDATA(tuple_data_bytea),
267+
(char *) tuphdr + tuphdr->t_hoff,
268+
tuple_data_len);
269+
values[13] = PointerGetDatum(tuple_data_bytea);
262270
}
263271
else
264272
{
265273
nulls[11] = true;
266274
nulls[12] = true;
275+
nulls[13] = true;
267276
}
268277
}
269278
else

0 commit comments

Comments
 (0)