pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mark Dilger <mark.dilger@enterprisedb.com>
To: Greg Stark <stark@mit.edu>
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Aleksander Alekseev <aleksander@timescale.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Extending amcheck to check toast size and compression
Date: Wed, 3 Nov 2021 15:56:41 -0700
Message-ID: <D3B84852-D981-4396-ACF1-C1DBAAC417B7@enterprisedb.com> (raw)
In-Reply-To: <8C8A33DF-EEA5-45F7-91A7-5E0EA79208CC@enterprisedb.com>
References: <C108F673-BF11-41A0-8436-535E998F7A74@enterprisedb.com>
	<484213DD-9F6E-440F-885F-0B31C600430F@enterprisedb.com>
	<162083278007.16755.9306912342451528285.pgcf@coridan.postgresql.org>
	<8729a485-e138-2e18-0d66-74f15ac0c3cc@iki.fi>
	<29751EFD-91D8-4F2F-8416-030AD067F260@enterprisedb.com>
	<CAM-w4HNAk+U=Lo0mxj6MwW7HB+T=NCgDv+HRWZS_aUsbwVCzDQ@mail.gmail.com>
	<DE60E400-914B-47CD-9A01-1A82ABEE3FCA@enterprisedb.com>
	<CAM-w4HNZTNu8rkYiv91kKuRUZnwuD5F6nQA_E8F2CJknYP3crA@mail.gmail.com>
	<8C8A33DF-EEA5-45F7-91A7-5E0EA79208CC@enterprisedb.com>



> On Oct 20, 2021, at 12:06 PM, Mark Dilger <mark.dilger@enterprisedb.com> wrote:
> 
> Ok.  How about:

Done that way.


—
Mark Dilger
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

Attachments:

v3-0001-Adding-more-toast-pointer-checks-to-amcheck.patch (application/octet-stream, 2.7K) download From af838f90d3752d6b1379f1c4d092d27fb6d58b89 Mon Sep 17 00:00:00 2001 From: Mark Dilger <mark.dilger@enterprisedb.com> Date: Tue, 4 May 2021 08:46:56 -0700 Subject: [PATCH v2] Adding more toast pointer checks to amcheck Expanding the checks of toasted attributes to complain if the rawsize is overlarge. For compressed attributes, complaining if compression appears to have expanded the attribute or if the compression method is invalid. --- contrib/amcheck/verify_heapam.c | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c index 36c1b791a2..2b5dcfb7c1 100644 --- a/contrib/amcheck/verify_heapam.c +++ b/contrib/amcheck/verify_heapam.c @@ -30,6 +30,9 @@ PG_FUNCTION_INFO_V1(verify_heapam); /* The number of columns in tuples returned by verify_heapam */ #define HEAPCHECK_RELATION_COLS 4 +/* The largest valid toast va_rawsize */ +#define VARLENA_SIZE_LIMIT 0x3FFFFFFF + /* * Despite the name, we use this for reporting problems with both XIDs and * MXIDs. @@ -1384,6 +1387,49 @@ check_tuple_attribute(HeapCheckContext *ctx) */ VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr); + /* Toasted attributes too large to be untoasted should never be stored */ + if (toast_pointer.va_rawsize > VARLENA_SIZE_LIMIT) + report_corruption(ctx, + psprintf("toast value %u rawsize %u exceeds limit %u", + toast_pointer.va_valueid, + toast_pointer.va_rawsize, + VARLENA_SIZE_LIMIT)); + + if (VARATT_IS_COMPRESSED(&toast_pointer)) + { + ToastCompressionId cmid; + bool valid = false; + + /* Compression should never expand the attribute */ + if (VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) > toast_pointer.va_rawsize - VARHDRSZ) + report_corruption(ctx, + psprintf("toast value %u external size %u exceeds maximum expected for rawsize %u", + toast_pointer.va_valueid, + VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer), + toast_pointer.va_rawsize)); + + /* Compressed attributes should have a valid compression method */ + cmid = TOAST_COMPRESS_METHOD(&toast_pointer); + switch (cmid) + { + /* List of all valid compression method IDs */ + case TOAST_PGLZ_COMPRESSION_ID: + case TOAST_LZ4_COMPRESSION_ID: + valid = true; + break; + + /* Recognized but invalid compression method ID */ + case TOAST_INVALID_COMPRESSION_ID: + break; + + /* Intentionally no default here */ + } + if (!valid) + report_corruption(ctx, + psprintf("toast value %u has invalid compression method id %d", + toast_pointer.va_valueid, cmid)); + } + /* The tuple header better claim to contain toasted values */ if (!(infomask & HEAP_HASEXTERNAL)) { -- 2.21.1 (Apple Git-122.3) =

view thread (18+ messages)  latest in thread

Message-ID: <D3B84852-D981-4396-ACF1-C1DBAAC417B7@enterprisedb.com>
Permalink:  ../D3B84852-D981-4396-ACF1-C1DBAAC417B7@enterprisedb.com/
Also on:    postgresql.org/message-id/D3B84852-D981-4396-ACF1-C1DBAAC417B7@enterprisedb.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: mark.dilger@enterprisedb.com, stark@mit.edu, hlinnaka@iki.fi, aleksander@timescale.com
  Subject: Re: Extending amcheck to check toast size and compression
  In-Reply-To: <D3B84852-D981-4396-ACF1-C1DBAAC417B7@enterprisedb.com>
  References: <C108F673-BF11-41A0-8436-535E998F7A74@enterprisedb.com> <484213DD-9F6E-440F-885F-0B31C600430F@enterprisedb.com> <162083278007.16755.9306912342451528285.pgcf@coridan.postgresql.org> <8729a485-e138-2e18-0d66-74f15ac0c3cc@iki.fi> <29751EFD-91D8-4F2F-8416-030AD067F260@enterprisedb.com> <CAM-w4HNAk+U=Lo0mxj6MwW7HB+T=NCgDv+HRWZS_aUsbwVCzDQ@mail.gmail.com> <DE60E400-914B-47CD-9A01-1A82ABEE3FCA@enterprisedb.com> <CAM-w4HNZTNu8rkYiv91kKuRUZnwuD5F6nQA_E8F2CJknYP3crA@mail.gmail.com> <8C8A33DF-EEA5-45F7-91A7-5E0EA79208CC@enterprisedb.com> <D3B84852-D981-4396-ACF1-C1DBAAC417B7@enterprisedb.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox