Skip to content

Commit f1634c9

Browse files
committed
Acquire ControlFileLock in relevant SQL functions.
Commit dc7d70e 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 <david@pgmasters.net> Reviewed-by: Anton A. Melnikov <aamelnikov@inbox.ru> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/20221123014224.xisi44byq3cf5psi%40awork3.anarazel.de
1 parent 2759924 commit f1634c9

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/backend/utils/misc/pg_controldata.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "common/controldata_utils.h"
2424
#include "funcapi.h"
2525
#include "miscadmin.h"
26+
#include "storage/lwlock.h"
2627
#include "utils/builtins.h"
2728
#include "utils/pg_lsn.h"
2829
#include "utils/timestamp.h"
@@ -53,7 +54,9 @@ pg_control_system(PG_FUNCTION_ARGS)
5354
tupdesc = BlessTupleDesc(tupdesc);
5455

5556
/* read the control file */
57+
LWLockAcquire(ControlFileLock, LW_SHARED);
5658
ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
59+
LWLockRelease(ControlFileLock);
5760
if (!crc_ok)
5861
ereport(ERROR,
5962
(errmsg("calculated CRC checksum does not match value stored in file")));
@@ -131,7 +134,9 @@ pg_control_checkpoint(PG_FUNCTION_ARGS)
131134
tupdesc = BlessTupleDesc(tupdesc);
132135

133136
/* Read the control file. */
137+
LWLockAcquire(ControlFileLock, LW_SHARED);
134138
ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
139+
LWLockRelease(ControlFileLock);
135140
if (!crc_ok)
136141
ereport(ERROR,
137142
(errmsg("calculated CRC checksum does not match value stored in file")));
@@ -235,7 +240,9 @@ pg_control_recovery(PG_FUNCTION_ARGS)
235240
tupdesc = BlessTupleDesc(tupdesc);
236241

237242
/* read the control file */
243+
LWLockAcquire(ControlFileLock, LW_SHARED);
238244
ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
245+
LWLockRelease(ControlFileLock);
239246
if (!crc_ok)
240247
ereport(ERROR,
241248
(errmsg("calculated CRC checksum does not match value stored in file")));
@@ -302,7 +309,9 @@ pg_control_init(PG_FUNCTION_ARGS)
302309
tupdesc = BlessTupleDesc(tupdesc);
303310

304311
/* read the control file */
312+
LWLockAcquire(ControlFileLock, LW_SHARED);
305313
ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
314+
LWLockRelease(ControlFileLock);
306315
if (!crc_ok)
307316
ereport(ERROR,
308317
(errmsg("calculated CRC checksum does not match value stored in file")));

0 commit comments

Comments
 (0)