Acquire ControlFileLock in relevant SQL functions.
authorThomas Munro
Sun, 15 Oct 2023 21:43:47 +0000 (10:43 +1300)
committerThomas Munro
Sun, 15 Oct 2023 21:45:18 +0000 (10:45 +1300)
Commit dc7d70ea added functions that read the control file, but didn't
acquire ControlFileLock.  With unlucky timing, file systems that have
weak interlocking like ext4 and ntfs could expose partially overwritten
contents, and the checksum would fail.

Back-patch to all supported releases.

Reviewed-by: David Steele
Reviewed-by: Anton A. Melnikov
Reviewed-by: Michael Paquier
Discussion: https://postgr.es/m/20221123014224.xisi44byq3cf5psi%40awork3.anarazel.de

src/backend/utils/misc/pg_controldata.c

index 781f8b8758097da122d5c4d3689c113fb8406d8e..fe52cffbf40eb5ed6c3b08732968229896339f32 100644 (file)
@@ -24,6 +24,7 @@
 #include "common/controldata_utils.h"
 #include "funcapi.h"
 #include "miscadmin.h"
+#include "storage/lwlock.h"
 #include "utils/builtins.h"
 #include "utils/pg_lsn.h"
 #include "utils/timestamp.h"
@@ -54,7 +55,9 @@ pg_control_system(PG_FUNCTION_ARGS)
    tupdesc = BlessTupleDesc(tupdesc);
 
    /* read the control file */
+   LWLockAcquire(ControlFileLock, LW_SHARED);
    ControlFile = get_controlfile(DataDir, &crc_ok);
+   LWLockRelease(ControlFileLock);
    if (!crc_ok)
        ereport(ERROR,
                (errmsg("calculated CRC checksum does not match value stored in file")));
@@ -132,7 +135,9 @@ pg_control_checkpoint(PG_FUNCTION_ARGS)
    tupdesc = BlessTupleDesc(tupdesc);
 
    /* Read the control file. */
+   LWLockAcquire(ControlFileLock, LW_SHARED);
    ControlFile = get_controlfile(DataDir, &crc_ok);
+   LWLockRelease(ControlFileLock);
    if (!crc_ok)
        ereport(ERROR,
                (errmsg("calculated CRC checksum does not match value stored in file")));
@@ -235,7 +240,9 @@ pg_control_recovery(PG_FUNCTION_ARGS)
    tupdesc = BlessTupleDesc(tupdesc);
 
    /* read the control file */
+   LWLockAcquire(ControlFileLock, LW_SHARED);
    ControlFile = get_controlfile(DataDir, &crc_ok);
+   LWLockRelease(ControlFileLock);
    if (!crc_ok)
        ereport(ERROR,
                (errmsg("calculated CRC checksum does not match value stored in file")));
@@ -300,7 +307,9 @@ pg_control_init(PG_FUNCTION_ARGS)
    tupdesc = BlessTupleDesc(tupdesc);
 
    /* read the control file */
+   LWLockAcquire(ControlFileLock, LW_SHARED);
    ControlFile = get_controlfile(DataDir, &crc_ok);
+   LWLockRelease(ControlFileLock);
    if (!crc_ok)
        ereport(ERROR,
                (errmsg("calculated CRC checksum does not match value stored in file")));