Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gBVgi-00085D-JI for pgsql-hackers@arkaria.postgresql.org; Sun, 14 Oct 2018 01:59:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gBVgg-0006eI-6H for pgsql-hackers@arkaria.postgresql.org; Sun, 14 Oct 2018 01:59:22 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gBVge-0006e1-DJ for pgsql-hackers@lists.postgresql.org; Sun, 14 Oct 2018 01:59:22 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gBVgV-0000Ey-Hs for pgsql-hackers@postgresql.org; Sun, 14 Oct 2018 01:59:19 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 631D921B84; Sat, 13 Oct 2018 21:59:09 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sat, 13 Oct 2018 21:59:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=Mc4yyMeyyOdlAFZzC/YvQBMHU+6 OESLlr9Ims3Oi5RY=; b=EgoxCvz5i7UncP8ZoKHfnr9DBd1e/GfANQNiGgZJnH2 2YbdpBrAB2f8NxX9IehgbfGsXq+yR0nWpoO3RmTVgE+MV5MFQ+pYzKfx9YvyK3qb WhXrVjemU5/+ecI2TV2KDGyyC4Vo4dtvtuKsT25PEcAegSKhrgZ1XdUFXZvXIRFj ff91/L+FRaSWDbmykvrkSgS7pAmY9tDXqNJnygidNOGFZDAvvU5HWWQ8b+jB9l2+ wcA6RbYOWEHUQfGDTKqBRH0046RE7kI/OpUYkkD9wsKHWQzB4MUQTUMcqvEjQb6X SrtbKrs2bxRZjlG8E4DkAQr3l4f9lDqrXCMVxCDWXsw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=Mc4yyM eyyOdlAFZzC/YvQBMHU+6OESLlr9Ims3Oi5RY=; b=K51AU29AW3ZNLqeNMh65Yw pQkM6sMVLh1LJYzoZXwLfddd3Es5L5qsX/HUJWkhlbDBkdq/I0k0bnN2ViYQLj5l 270zRv58HdT1ieiK9NHBXYd2TwsdVToZUq1WGc++xh3nD8dqYxZaQ7Qs8249+UED w/0DohZFmyEx4RXnhyVdCxxJX11vGPHe0/5kJmuyU3pFO417nhqPCK2S5qut9ATe JiiGzoVmuoQKTD/EAm43QkUP6eNhfmoNydyBdU1WogCrQUtjBswOWZPAti1UqplG yT4uNEO52PbesMcc3R+Y9yoMXmzchWsSNPTj21ayFXP4Iskojx04kPVqU6jbvnyg == X-ME-Sender: X-ME-Proxy: Received: from paquier.xyz (c137162.net61215.cablenet.ne.jp [61.215.137.162]) by mail.messagingengine.com (Postfix) with ESMTPA id A203BE4240; Sat, 13 Oct 2018 21:59:05 -0400 (EDT) Date: Sun, 14 Oct 2018 10:59:02 +0900 From: Michael Paquier To: Andrew Dunstan Cc: Michael Banck , Andres Freund , Postgres hackers , Daniel Gustafsson , Magnus Hagander Subject: Re: pgsql: Add TAP tests for pg_verify_checksums Message-ID: <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> <1bce17ea-08f7-c480-fe69-a5f6467b461f@2ndQuadrant.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="yklP1rR72f9kjNtc" Content-Disposition: inline In-Reply-To: <1bce17ea-08f7-c480-fe69-a5f6467b461f@2ndQuadrant.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --yklP1rR72f9kjNtc Content-Type: multipart/mixed; boundary="DXIF1lRUlMsbZ3S1" Content-Disposition: inline --DXIF1lRUlMsbZ3S1 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --DXIF1lRUlMsbZ3S1 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="checksum-whitelist-v2.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_verify_checksums/pg_verify_checksums.c b/src/bin/pg= _verify_checksums/pg_verify_checksums.c index 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 @@ =20 #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 .\n")); } =20 -static const char *const skip[] =3D { - "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; =20 if (strcmp(fn, ".") =3D=3D 0 || strcmp(fn, "..") =3D=3D 0) return true; =20 - for (f =3D skip; *f; f++) - if (strcmp(*f, fn) =3D=3D 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: + * + * . + * _ + * _. + * + * Note that temporary files, beginning with 't', are also skipped. + * + *---------- + */ + + /* A non-empty string of digits should follow */ + for (pos =3D 0; isdigit((unsigned char) fn[pos]); ++pos) + ; + /* good to go if only digits */ + if (fn[pos] =3D=3D '\0') + return false; + /* leave if no digits */ + if (pos =3D=3D 0) + return true; + + /* Authorized fork files can be scanned */ + if (fn[pos] =3D=3D '_') + { + int forkchar =3D forkname_chars(&fn[pos + 1], NULL); + + if (forkchar <=3D 0) return true; + + pos +=3D forkchar + 1; + } + + /* Check for an optional segment number */ + if (fn[pos] =3D=3D '.') + { + savepos =3D pos + 1; /* count dot */ + /* This should use only digits */ + for (pos =3D savepos; isdigit((unsigned char) fn[pos]); ++pos) + ; + if (fn[pos] !=3D '\0') + return true; + } + + /* Now this should be the end */ + if (fn[pos] !=3D '\0') + return true; return false; } =20 diff --git a/src/bin/pg_verify_checksums/t/002_actions.pl b/src/bin/pg_veri= fy_checksums/t/002_actions.pl index 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 =3D> 12; +use Test::More tests =3D> 36; =20 # Initialize node with checksums enabled. my $node =3D 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'); =20 +# 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 =3D shift; + my $file =3D shift; + my $pgdata =3D $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"); --DXIF1lRUlMsbZ3S1-- --yklP1rR72f9kjNtc Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlvCouYACgkQnvQgOdby QH1M2Q//UCq4VArSJxVWsWthyQsx7FdqvYfhQQp41e4U9Q+NPO7iMqUA1GOvwd6+ nzmYfo8xPMnd4f8RW97L7ICVMuDWRVZy/lOo+72V6LoJPhV3x+N3LHhHf9uCOe7/ 5BPOEc6QZkaIxR6xA5IxVCeZWu6c2WXYs7WQEYNEUTcyNE77VzKc5fChTadJgfaB KU0aKyaQscAS96dB4N58kv5KAFSPldjD96dFGp/ozkS2gpnef+e3ji1TYrVzdwi7 t/uSU5ZLQi0OsCc+H6311K+sGXxzIz7X5wYBsJcJKVJUnSk/VGbdthBHQmHXcHIV 2kG2nZENkZtzThiQkr+Xse8nu7CMeEG2T0BynMjh/X7+aJ1dW6Q3UheQEQKqQPfW J8mX502l8lQVEGeVtzLEogpk2KHmooz+cs9Ofl/4kb6wiNCx+jrwJnuTXCIQQReP BUqQtxXEn4veYINwq/eJ1hh/GZBfZlSV6Lwbi3y18YFXHG2ZMRkyFK3EGgufXhkb 4sb7bz8PHpVlFS4jTZ4YQGoxJzd9b8uXpRM/Zx+E8zcoUwYqcPgyYZNXtkAxd6bE TBnsoSncuX42VqkJizmm1XQzVIBlL4hr43q/N0CPdxRDFHaEi9vxwZZb5K9iLXde THnEq9RcvV5Oj3HzLPNr8R2m13d94ddF1j6I/PUA6gXgbxKnFCw= =RjDd -----END PGP SIGNATURE----- --yklP1rR72f9kjNtc--