Skip to content

Commit 93528f7

Browse files
committed
Avoid using PostmasterRandom() for DSM control segment ID.
Commits 470d886 et al intended to fix the problem that the postmaster selected the same "random" DSM control segment ID on every start. But using PostmasterRandom() for that destroys the intended property that the delay between random_start_time and random_stop_time will be unpredictable. (Said delay is probably already more predictable than we could wish, but that doesn't mean that reducing it by a couple orders of magnitude is OK.) Revert the previous patch and add a comment warning against misuse of PostmasterRandom. Fix the original problem by calling srandom() early in PostmasterMain, using a low-security seed that will later be overwritten by PostmasterRandom. Discussion: <20789.1474390434@sss.pgh.pa.us>
1 parent c359178 commit 93528f7

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ static void processCancelRequest(Port *port, void *pkt);
399399
static int initMasks(fd_set *rmask);
400400
static void report_fork_failure_to_client(Port *port, int errnum);
401401
static CAC_state canAcceptConnections(void);
402+
static long PostmasterRandom(void);
402403
static void RandomSalt(char *md5Salt);
403404
static void signal_child(pid_t pid, int signal);
404405
static bool SignalSomeChildren(int signal, int targets);
@@ -568,6 +569,16 @@ PostmasterMain(int argc, char *argv[])
568569
*/
569570
umask(S_IRWXG | S_IRWXO);
570571

572+
/*
573+
* Initialize random(3) so we don't get the same values in every run.
574+
*
575+
* Note: the seed is pretty predictable from externally-visible facts such
576+
* as postmaster start time, so avoid using random() for security-critical
577+
* random values during postmaster startup. At the time of first
578+
* connection, PostmasterRandom will select a hopefully-more-random seed.
579+
*/
580+
srandom((unsigned int) (MyProcPid ^ MyStartTime));
581+
571582
/*
572583
* By default, palloc() requests in the postmaster will be allocated in
573584
* the PostmasterContext, which is space that can be recycled by backends.
@@ -5075,8 +5086,12 @@ RandomSalt(char *md5Salt)
50755086

50765087
/*
50775088
* PostmasterRandom
5089+
*
5090+
* Caution: use this only for values needed during connection-request
5091+
* processing. Otherwise, the intended property of having an unpredictable
5092+
* delay between random_start_time and random_stop_time will be broken.
50785093
*/
5079-
long
5094+
static long
50805095
PostmasterRandom(void)
50815096
{
50825097
/*

src/backend/storage/ipc/dsm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
#include "lib/ilist.h"
3838
#include "miscadmin.h"
39-
#include "postmaster/postmaster.h"
4039
#include "storage/dsm.h"
4140
#include "storage/ipc.h"
4241
#include "storage/lwlock.h"
@@ -180,7 +179,7 @@ dsm_postmaster_startup(PGShmemHeader *shim)
180179
{
181180
Assert(dsm_control_address == NULL);
182181
Assert(dsm_control_mapped_size == 0);
183-
dsm_control_handle = (dsm_handle) PostmasterRandom();
182+
dsm_control_handle = random();
184183
if (dsm_control_handle == 0)
185184
continue;
186185
if (dsm_impl_op(DSM_OP_CREATE, dsm_control_handle, segsize,

src/include/postmaster/postmaster.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ extern const char *progname;
4848

4949
extern void PostmasterMain(int argc, char *argv[]) pg_attribute_noreturn();
5050
extern void ClosePostmasterPorts(bool am_syslogger);
51-
extern long PostmasterRandom(void);
5251

5352
extern int MaxLivePostmasterChildren(void);
5453

0 commit comments

Comments
 (0)