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 1gBp58-0002lJ-M6 for pgsql-hackers@arkaria.postgresql.org; Sun, 14 Oct 2018 22:41:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gBp56-0003yo-Oi for pgsql-hackers@arkaria.postgresql.org; Sun, 14 Oct 2018 22:41:52 +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 1gBp55-0003yh-0u for pgsql-hackers@lists.postgresql.org; Sun, 14 Oct 2018 22:41:52 +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 1gBp51-0004kH-61 for pgsql-hackers@postgresql.org; Sun, 14 Oct 2018 22:41:49 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id C56E321B55; Sun, 14 Oct 2018 18:41:45 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sun, 14 Oct 2018 18:41:45 -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=vaIMQhi5NJdYUXZCIwM2aN10xZ4 2sSjaxGxaBJY/hws=; b=Ie89SczRCXTqFop45OXB0VrlGnZFPkozI+o0OfLPpws rPV5fH+s7hkHfYiznvEHGfYZXECGv0YaUnn/p9W5cLW16uUw1rjL6GlQ4hPfP2lu bCVAuJGyfKldbGuCdok7T4feg4KusiVZjrwJqvwWayf02PAwl+iNVCTfqhHfsFS2 x3fuH9xDWrFds93BMo+7H/qpN4zf97OUOgOt+FvDhZ4izPdHxuBGHPua7J7uuVR4 Q6/gAuQBamp5OhhB2oDTgE9KnP0FCdqiKEvr7atn+C+HGFXNMuIthyk8ylhWH8nQ 4i2ekHFwq/0evYiQxY93myY6yxWoyX3l/BJGxOhhjpQ== 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=vaIMQh i5NJdYUXZCIwM2aN10xZ42sSjaxGxaBJY/hws=; b=gkbIs3lI1dU67y/km56QDj X29x3qQWsereJJo+Ze+o1j0VCdR8ycsn5Rk+VA59muTo6OiTyANGUVqsRfFwdg4Q U328tDH8LWOqvnrhCwkIrI4UWr4eFcC0m6jSeEDVB3t+Mcd9mZHsjYdLkX+Xfmmx oHh1Sc2EqTwSSGsHHeJ8hryeIOVQS9WYKDsm/Db7YBceGcv3/3Bx1/1b9EQqvgtH Gkd1ehU6gHDePwO0NEnrDmBJ3PyVDWMLNv7ajYqxJQ/aczWvvw6LwWk1cvMTu1el Pp1p8OyMWE7tjnMWOEfKJJbxhVUPW2tc3rZMp7W+nHLJVJjVLfV05IRBeIOPytVg == 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 72792E43AC; Sun, 14 Oct 2018 18:41:42 -0400 (EDT) Date: Mon, 15 Oct 2018 07:41:38 +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: <20181014224138.GA15473@paquier.xyz> References: <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> <20181014015902.GI30064@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="DBIVS5p969aUjpLe" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --DBIVS5p969aUjpLe Content-Type: multipart/mixed; boundary="uAKRQypu60I7Lcqm" Content-Disposition: inline --uAKRQypu60I7Lcqm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Oct 14, 2018 at 10:24:55AM -0400, Andrew Dunstan wrote: > [snip] Thanks a lot for the review, Andrew! > This code now seems redundant: >=20 > =A0=A0=A0=A0 if (strcmp(fn, ".") =3D=3D 0 || > =A0=A0=A0=A0 =A0=A0=A0 strcmp(fn, "..") =3D=3D 0) > =A0=A0=A0=A0 =A0=A0=A0 return true; Indeed. I have renamed skipfile() to isRelFileName on the way, which looks better in the context. > I would probably reverse the order of these two tests. It might not make = any > difference, assuming fn is never an empty string, but it seems more logic= al > to me. >=20 > +=A0=A0=A0 /* good to go if only digits */ > +=A0=A0=A0 if (fn[pos] =3D=3D '\0') > +=A0=A0=A0 =A0=A0=A0 return false; > +=A0=A0=A0 /* leave if no digits */ > +=A0=A0=A0 if (pos =3D=3D 0) > +=A0=A0=A0 =A0=A0=A0 return true; No objections to that. Done. > It also looks to me like the check for a segment number doesn't ensure > there is at least one digit, so "123." might pass, but I could be > wrong. In any case, there isn't a test for that, and there probably > should be. You are right here. This name pattern has been failing. Fixed in the attached. I have added "123_" and "123_." as extra patterns to check. What do you think about the updated version attached? -- Michael --uAKRQypu60I7Lcqm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="checksum-whitelist-v3.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..666ab3f21e 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,27 +50,69 @@ 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, -}; - +/* + * isRelFileName + * + * Check if the given file name has a shape authorized for scanning. + */ static bool -skipfile(const char *fn) +isRelFileName(const char *fn) { - const char *const *f; + int pos; =20 - if (strcmp(fn, ".") =3D=3D 0 || - strcmp(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) + ; + /* leave if no digits */ + if (pos =3D=3D 0) + return false; + /* good to go if only digits */ + if (fn[pos] =3D=3D '\0') return true; =20 - for (f =3D skip; *f; f++) - if (strcmp(*f, fn) =3D=3D 0) - return true; - return false; + /* 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 false; + + pos +=3D forkchar + 1; + } + + /* Check for an optional segment number */ + if (fn[pos] =3D=3D '.') + { + int segchar; + + for (segchar =3D 1; isdigit((unsigned char) fn[pos + segchar]); ++segcha= r) + ; + + if (segchar <=3D 1) + return false; + pos +=3D segchar; + } + + /* Now this should be the end */ + if (fn[pos] !=3D '\0') + return false; + return true; } =20 static void @@ -146,7 +189,7 @@ scan_directory(const char *basedir, const char *subdir) char fn[MAXPGPATH]; struct stat st; =20 - if (skipfile(de->d_name)) + if (!isRelFileName(de->d_name)) continue; =20 snprintf(fn, sizeof(fn), "%s/%s", path, de->d_name); 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..8442534e95 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,31 @@ 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.", "foo"; +append_to_file "$pgdata/global/123_", "foo"; +append_to_file "$pgdata/global/123_.", "foo"; +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 +92,33 @@ $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-named relation files with some corrupted +# 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"); +} + +# Authorized relation files filled with corrupted data cause the +# checksum checks to fail. +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"); --uAKRQypu60I7Lcqm-- --DBIVS5p969aUjpLe Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlvDxiIACgkQnvQgOdby QH2tTg/+KRnQLX4aAGgMLZiilXqxCzXaFwaOF+PAYE6pLG5Uf7/PREuDb0T2BIrd gXkPYOUrPhUbBpqjhw17IltS2X7O7slF4bwBZjyZ5cVO0FBB7U6Ll4QIJphsp0bs WRl+uC5+nAJUJvo5ClRq8tdGstX4Ip+OU5WIZeMqzy4fbWcYCLplJgmIsV5x4Kvc w6oJbo8K7R7jP2hhyG4jrxf+6BPENVvNrp+XgpexvZJSvCVEetGbb+iXetxJogi0 4e8GAHy46is/XHry3aILdQRtUDiW89qsMWa09VNxu3p9nwABOu0svHy/h8xzy7tQ xhl6KwtJdgCWknbwD+euslirpHvz0rQrK4PGG/ba8EMaHhC1T9mfMRfywzaPYc+N UkgedDivyJOW/XQSBlOTLr3LK/qim/XQuAR/6wAuuGNwDvN/VJRmCkliTRTawHJk iBCozrqNDPsnzEK0g92/kNvPAdBrX8V2cv4Nw/qm+ke+Y9z1WTTVd9zItOQMAPQx NYGgM7gploWahQF0dGNBUKf49Ajk1jOeNhQOkL67glkksR8zUeQdSlSWoDaBYat0 nrOGuzZG+iykccYIYHYTQ7ZverXgNHz5vIdRuTq6jjYdXy35+TZlG8XZSodBXsjB S904VIamPUi0fsBqPfov1kgKm2tjyzyk0amaqYGk8DB0zzqqF5E= =9LJt -----END PGP SIGNATURE----- --DBIVS5p969aUjpLe--