From: Michael Paquier <michael@paquier.xyz>
To: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
Cc: Michael Banck <michael.banck@credativ.de>
Cc: Andres Freund <andres@anarazel.de>
Cc: Postgres hackers <pgsql-hackers@postgresql.org>
Cc: Daniel Gustafsson <daniel@yesql.se>
Cc: Magnus Hagander <magnus@hagander.net>
Subject: Re: pgsql: Add TAP tests for pg_verify_checksums
Date: Sun, 14 Oct 2018 10:59:02 +0900
Message-ID: <20181014015902.GI30064@paquier.xyz> (raw)
In-Reply-To: <1bce17ea-08f7-c480-fe69-a5f6467b461f@2ndQuadrant.com>
References: <20181012013918.GA30064@paquier.xyz>
<20181012015319.ezsb2cxb52ulbqgx@alap3.anarazel.de>
<20181012020758.GB30064@paquier.xyz>
<20181012024118.q3jwio7gf52nsrmm@alap3.anarazel.de>
<20181012031158.GD30064@paquier.xyz>
<20181012060543.GE30064@paquier.xyz>
<20181012101447.GA13287@nighthawk.caipicrew.dd-dns.de>
<20181013083040.GF30064@paquier.xyz>
<c6290e25-97c1-5d2c-dc97-846ddb261d4d@2ndQuadrant.com>
<1bce17ea-08f7-c480-fe69-a5f6467b461f@2ndQuadrant.com>
On Sat, Oct 13, 2018 at 05:53:00PM -0400, Andrew Dunstan wrote:
> It occurred to me that a pretty simple fix could just be to blacklist
> everything that didn't start with a digit. The whitelist approach is
> probably preferable... depends how urgent we see this as.
Yeah, possibly, still that's not a correct long-term fix. So attached
is what I think we should do for HEAD and REL_11_STABLE with an approach
using a whitelist. I have added positive and negative tests on top of
the existing TAP tests, as suggested by Michael B, and I made the code
use relpath.h to make sure that we don't miss any fork types.
Any objections to this patch?
--
Michael
Attachments:checksum-whitelist-v2.patch (text/x-diff, 4.9K) downloaddiff --git a/src/bin/pg_verify_checksums/pg_verify_checksums.c b/src/bin/pg_verify_checksums/pg_verify_checksums.cindex 1bc020ab6c..7fb9d656fa 100644--- a/src/bin/pg_verify_checksums/pg_verify_checksums.c+++ b/src/bin/pg_verify_checksums/pg_verify_checksums.c@@ -15,6 +15,7 @@
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
+#include "common/relpath.h"
#include "getopt_long.h"
#include "pg_getopt.h"
#include "storage/bufpage.h"
@@ -49,26 +50,66 @@ usage(void)
printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
}
-static const char *const skip[] = {- "pg_control",- "pg_filenode.map",- "pg_internal.init",- "PG_VERSION",- NULL,-};-
static bool
skipfile(const char *fn)
{
- const char *const *f;+ int pos;+ int savepos;
if (strcmp(fn, ".") == 0 ||
strcmp(fn, "..") == 0)
return true;
- for (f = skip; *f; f++)- if (strcmp(*f, fn) == 0)+ /*----------+ * Only files including data checksums are authorized for scanning. This+ * is guessed based on the file name, by reverse-engineering+ * GetRelationPath so make sure to update both code paths if any updates+ * are done. The following file names are allowed:+ * <digits>+ * <digits>.<segment>+ * <digits>_<forkname>+ * <digits>_<forkname>.<segment>+ *+ * Note that temporary files, beginning with 't', are also skipped.+ *+ *----------+ */++ /* A non-empty string of digits should follow */+ for (pos = 0; isdigit((unsigned char) fn[pos]); ++pos)+ ;+ /* good to go if only digits */+ if (fn[pos] == '\0')+ return false;+ /* leave if no digits */+ if (pos == 0)+ return true;++ /* Authorized fork files can be scanned */+ if (fn[pos] == '_')+ {+ int forkchar = forkname_chars(&fn[pos + 1], NULL);++ if (forkchar <= 0)
return true;
++ pos += forkchar + 1;+ }++ /* Check for an optional segment number */+ if (fn[pos] == '.')+ {+ savepos = pos + 1; /* count dot */+ /* This should use only digits */+ for (pos = savepos; isdigit((unsigned char) fn[pos]); ++pos)+ ;+ if (fn[pos] != '\0')+ return true;+ }++ /* Now this should be the end */+ if (fn[pos] != '\0')+ return true;
return false;
}
diff --git a/src/bin/pg_verify_checksums/t/002_actions.pl b/src/bin/pg_verify_checksums/t/002_actions.plindex dc29da09af..33408ced89 100644--- a/src/bin/pg_verify_checksums/t/002_actions.pl+++ b/src/bin/pg_verify_checksums/t/002_actions.pl@@ -5,7 +5,7 @@ use strict;
use warnings;
use PostgresNode;
use TestLib;
-use Test::More tests => 12;+use Test::More tests => 36;
# Initialize node with checksums enabled.
my $node = get_new_node('node_checksum');
@@ -17,6 +17,28 @@ command_like(['pg_controldata', $pgdata],
qr/Data page checksum version:.*1/,
'checksums enabled in control file');
+# Add set of dummy files with some contents. These should not be scanned+# by the tool.+append_to_file "$pgdata/global/123.12t", "foo";+append_to_file "$pgdata/global/foo", "foo2";+append_to_file "$pgdata/global/t123", "bar";+append_to_file "$pgdata/global/123a", "bar2";+append_to_file "$pgdata/global/.123", "foobar";+append_to_file "$pgdata/global/_fsm", "foobar2";+append_to_file "$pgdata/global/_init", "foobar3";+append_to_file "$pgdata/global/_vm.123", "foohoge";+append_to_file "$pgdata/global/123_vm.123t", "foohoge2";++# Those are correct but empty files, so they should pass through.+append_to_file "$pgdata/global/99999", "";+append_to_file "$pgdata/global/99999.123", "";+append_to_file "$pgdata/global/99999_fsm", "";+append_to_file "$pgdata/global/99999_init", "";+append_to_file "$pgdata/global/99999_vm", "";+append_to_file "$pgdata/global/99999_init.123", "";+append_to_file "$pgdata/global/99999_fsm.123", "";+append_to_file "$pgdata/global/99999_vm.123", "";+
# Checksums pass on a newly-created cluster
command_ok(['pg_verify_checksums', '-D', $pgdata],
"succeeds with offline cluster");
@@ -67,3 +89,30 @@ $node->command_checks_all([ 'pg_verify_checksums', '-D', $pgdata, '-r',
[qr/Bad checksums:.*1/],
[qr/checksum verification failed/],
'fails for corrupted data on single relfilenode');
++# Utility routine to check that pg_verify_checksums is correctly+# able to detect correctly-shaped relation files with some data.+sub fail_corrupt+{+ my $node = shift;+ my $file = shift;+ my $pgdata = $node->data_dir;++ # Create the file with some data in it.+ append_to_file "$pgdata/global/$file", "foo";++ $node->command_checks_all([ 'pg_verify_checksums', '-D', $pgdata],+ 1,+ [qr/^$/],+ [qr/could not read block/],+ "fails for corrupted data in $file");+}++fail_corrupt($node, "99999");+fail_corrupt($node, "99999.123");+fail_corrupt($node, "99999_fsm");+fail_corrupt($node, "99999_init");+fail_corrupt($node, "99999_vm");+fail_corrupt($node, "99999_init.123");+fail_corrupt($node, "99999_fsm.123");+fail_corrupt($node, "99999_vm.123");
signature.asc (application/pgp-signature, 832B) download
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-committers@postgresql.org
Cc: michael@paquier.xyz, andrew.dunstan@2ndquadrant.com, michael.banck@credativ.de, andres@anarazel.de, pgsql-hackers@postgresql.org, daniel@yesql.se, magnus@hagander.net
Subject: Re: pgsql: Add TAP tests for pg_verify_checksums
In-Reply-To: <20181014015902.GI30064@paquier.xyz>
References: <20181012013918.GA30064@paquier.xyz> <20181012015319.ezsb2cxb52ulbqgx@alap3.anarazel.de> <20181012020758.GB30064@paquier.xyz> <20181012024118.q3jwio7gf52nsrmm@alap3.anarazel.de> <20181012031158.GD30064@paquier.xyz> <20181012060543.GE30064@paquier.xyz> <20181012101447.GA13287@nighthawk.caipicrew.dd-dns.de> <20181013083040.GF30064@paquier.xyz> <c6290e25-97c1-5d2c-dc97-846ddb261d4d@2ndQuadrant.com> <1bce17ea-08f7-c480-fe69-a5f6467b461f@2ndQuadrant.com> <20181014015902.GI30064@paquier.xyz>
* 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