Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab35b9d

Browse files
committed
Harden pmsignal.c against clobbered shared memory.
The postmaster is not supposed to do anything that depends fundamentally on shared memory contents, because that creates the risk that a backend crash that trashes shared memory will take the postmaster down with it, preventing automatic recovery. In commit 969d7cd I lost sight of this principle and coded AssignPostmasterChildSlot() in such a way that it could fail or even crash if the shared PMSignalState structure became corrupted. Remarkably, we've not seen field reports of such crashes; but I managed to induce one while testing the recent changes around palloc chunk headers. To fix, make a semi-duplicative state array inside the postmaster so that we need consult only local state while choosing a "child slot" for a new backend. Ensure that other postmaster-executed routines in pmsignal.c don't have critical dependencies on the shared state, either. Corruption of PMSignalState might now lead ReleasePostmasterChildSlot() to conclude that backend X failed, when actually backend Y was the one that trashed things. But that doesn't matter, because we'll force a cluster-wide reset regardless. Back-patch to all supported branches, since this is an old bug. Discussion: https://postgr.es/m/[email protected]
1 parent 23e2a06 commit ab35b9d

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/backend/storage/ipc/pmsignal.c

+43-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "replication/walsender.h"
2323
#include "storage/pmsignal.h"
2424
#include "storage/shmem.h"
25+
#include "utils/memutils.h"
2526

2627

2728
/*
@@ -65,12 +66,20 @@ struct PMSignalData
6566
sig_atomic_t PMSignalFlags[NUM_PMSIGNALS];
6667
/* per-child-process flags */
6768
int num_child_flags; /* # of entries in PMChildFlags[] */
68-
int next_child_flag; /* next slot to try to assign */
6969
sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER];
7070
};
7171

72+
/* PMSignalState pointer is valid in both postmaster and child processes */
7273
NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
7374

75+
/*
76+
* These static variables are valid only in the postmaster. We keep a
77+
* duplicative private array so that we can trust its state even if some
78+
* failing child has clobbered the PMSignalData struct in shared memory.
79+
*/
80+
static int num_child_inuse; /* # of entries in PMChildInUse[] */
81+
static int next_child_inuse; /* next slot to try to assign */
82+
static bool *PMChildInUse; /* true if i'th flag slot is assigned */
7483

7584
/*
7685
* PMSignalShmemSize
@@ -102,7 +111,25 @@ PMSignalShmemInit(void)
102111
if (!found)
103112
{
104113
MemSet(PMSignalState, 0, PMSignalShmemSize());
105-
PMSignalState->num_child_flags = MaxLivePostmasterChildren();
114+
num_child_inuse = MaxLivePostmasterChildren();
115+
PMSignalState->num_child_flags = num_child_inuse;
116+
117+
/*
118+
* Also allocate postmaster's private PMChildInUse[] array. We
119+
* might've already done that in a previous shared-memory creation
120+
* cycle, in which case free the old array to avoid a leak. (Do it
121+
* like this to support the possibility that MaxLivePostmasterChildren
122+
* changed.) In a standalone backend, we do not need this.
123+
*/
124+
if (PostmasterContext != NULL)
125+
{
126+
if (PMChildInUse)
127+
pfree(PMChildInUse);
128+
PMChildInUse = (bool *)
129+
MemoryContextAllocZero(PostmasterContext,
130+
num_child_inuse * sizeof(bool));
131+
}
132+
next_child_inuse = 0;
106133
}
107134
}
108135

@@ -150,21 +177,24 @@ CheckPostmasterSignal(PMSignalReason reason)
150177
int
151178
AssignPostmasterChildSlot(void)
152179
{
153-
int slot = PMSignalState->next_child_flag;
180+
int slot = next_child_inuse;
154181
int n;
155182

156183
/*
157-
* Scan for a free slot. We track the last slot assigned so as not to
158-
* waste time repeatedly rescanning low-numbered slots.
184+
* Scan for a free slot. Notice that we trust nothing about the contents
185+
* of PMSignalState, but use only postmaster-local data for this decision.
186+
* We track the last slot assigned so as not to waste time repeatedly
187+
* rescanning low-numbered slots.
159188
*/
160-
for (n = PMSignalState->num_child_flags; n > 0; n--)
189+
for (n = num_child_inuse; n > 0; n--)
161190
{
162191
if (--slot < 0)
163-
slot = PMSignalState->num_child_flags - 1;
164-
if (PMSignalState->PMChildFlags[slot] == PM_CHILD_UNUSED)
192+
slot = num_child_inuse - 1;
193+
if (!PMChildInUse[slot])
165194
{
195+
PMChildInUse[slot] = true;
166196
PMSignalState->PMChildFlags[slot] = PM_CHILD_ASSIGNED;
167-
PMSignalState->next_child_flag = slot;
197+
next_child_inuse = slot;
168198
return slot + 1;
169199
}
170200
}
@@ -186,7 +216,7 @@ ReleasePostmasterChildSlot(int slot)
186216
{
187217
bool result;
188218

189-
Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
219+
Assert(slot > 0 && slot <= num_child_inuse);
190220
slot--;
191221

192222
/*
@@ -196,17 +226,18 @@ ReleasePostmasterChildSlot(int slot)
196226
*/
197227
result = (PMSignalState->PMChildFlags[slot] == PM_CHILD_ASSIGNED);
198228
PMSignalState->PMChildFlags[slot] = PM_CHILD_UNUSED;
229+
PMChildInUse[slot] = false;
199230
return result;
200231
}
201232

202233
/*
203234
* IsPostmasterChildWalSender - check if given slot is in use by a
204-
* walsender process.
235+
* walsender process. This is called only by the postmaster.
205236
*/
206237
bool
207238
IsPostmasterChildWalSender(int slot)
208239
{
209-
Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
240+
Assert(slot > 0 && slot <= num_child_inuse);
210241
slot--;
211242

212243
if (PMSignalState->PMChildFlags[slot] == PM_CHILD_WALSENDER)

0 commit comments

Comments
 (0)