Skip to content

Commit 6b48f06

Browse files
committed
Prevent running pg_resetwal/pg_resetxlog against wrong-version data dirs.
pg_resetwal (formerly pg_resetxlog) doesn't insist on finding a matching version number in pg_control, and that seems like an important thing to preserve since recovering from corrupt pg_control is a prime reason to need to run it. However, that means you can try to run it against a data directory of a different major version, which is at best useless and at worst disastrous. So as to provide some protection against that type of pilot error, inspect PG_VERSION at startup and refuse to do anything if it doesn't match. PG_VERSION is read-only after initdb, so it's unlikely to get corrupted, and even if it were corrupted it would be easy to fix by hand. This hazard has been there all along, so back-patch to all supported branches. Michael Paquier, with some kibitzing by me Discussion: https://postgr.es/m/[email protected]
1 parent a653606 commit 6b48f06

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

doc/src/sgml/ref/pg_resetxlog.sgml

+5
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ PostgreSQL documentation
204204
<command>pg_resetxlog</command> to run. But before you do
205205
so, make doubly certain that there is no server process still alive.
206206
</para>
207+
208+
<para>
209+
<command>pg_resetxlog</command> works only with servers of the same
210+
major version.
211+
</para>
207212
</refsect1>
208213

209214
</refentry>

src/bin/pg_resetxlog/pg_resetxlog.c

+69-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static const char *progname;
6969
static char *restrict_env;
7070
#endif
7171

72+
static void CheckDataVersion(void);
7273
static bool ReadControlFile(void);
7374
static void GuessControlValues(void);
7475
static void PrintControlValues(bool guessed);
@@ -275,6 +276,9 @@ main(int argc, char *argv[])
275276
exit(1);
276277
}
277278

279+
/* Check that data directory matches our server version */
280+
CheckDataVersion();
281+
278282
/*
279283
* Check for a postmaster lock file --- if there is one, refuse to
280284
* proceed, on grounds we might be interfering with a live installation.
@@ -405,6 +409,70 @@ pg_strdup(const char *str)
405409
}
406410
#endif
407411

412+
/*
413+
* Look at the version string stored in PG_VERSION and decide if this utility
414+
* can be run safely or not.
415+
*
416+
* We don't want to inject pg_control and WAL files that are for a different
417+
* major version; that can't do anything good. Note that we don't treat
418+
* mismatching version info in pg_control as a reason to bail out, because
419+
* recovering from a corrupted pg_control is one of the main reasons for this
420+
* program to exist at all. However, PG_VERSION is unlikely to get corrupted,
421+
* and if it were it would be easy to fix by hand. So let's make this check
422+
* to prevent simple user errors.
423+
*/
424+
static void
425+
CheckDataVersion(void)
426+
{
427+
const char *ver_file = "PG_VERSION";
428+
FILE *ver_fd;
429+
char rawline[64];
430+
int len;
431+
432+
if ((ver_fd = fopen(ver_file, "r")) == NULL)
433+
{
434+
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
435+
progname, ver_file, strerror(errno));
436+
exit(1);
437+
}
438+
439+
/* version number has to be the first line read */
440+
if (!fgets(rawline, sizeof(rawline), ver_fd))
441+
{
442+
if (!ferror(ver_fd))
443+
{
444+
fprintf(stderr, _("%s: unexpected empty file \"%s\"\n"),
445+
progname, ver_file);
446+
}
447+
else
448+
{
449+
fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
450+
progname, ver_file, strerror(errno));
451+
}
452+
exit(1);
453+
}
454+
455+
/* remove trailing newline, handling Windows newlines as well */
456+
len = strlen(rawline);
457+
if (len > 0 && rawline[len - 1] == '\n')
458+
{
459+
rawline[--len] = '\0';
460+
if (len > 0 && rawline[len - 1] == '\r')
461+
rawline[--len] = '\0';
462+
}
463+
464+
if (strcmp(rawline, PG_MAJORVERSION) != 0)
465+
{
466+
fprintf(stderr, _("%s: data directory is of wrong version\n"
467+
"File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".\n"),
468+
progname, ver_file, rawline, PG_MAJORVERSION);
469+
exit(1);
470+
}
471+
472+
fclose(ver_fd);
473+
}
474+
475+
408476
/*
409477
* Try to read the existing pg_control file.
410478
*
@@ -474,7 +542,7 @@ ReadControlFile(void)
474542
}
475543

476544
/* Looks like it's a mess. */
477-
fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
545+
fprintf(stderr, _("%s: pg_control exists but is broken or wrong version; ignoring it\n"),
478546
progname);
479547
return false;
480548
}

0 commit comments

Comments
 (0)