Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b251379

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 a88fe25 commit b251379

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
@@ -403,6 +403,7 @@ static void processCancelRequest(Port *port, void *pkt);
403403
static int initMasks(fd_set *rmask);
404404
static void report_fork_failure_to_client(Port *port, int errnum);
405405
static CAC_state canAcceptConnections(void);
406+
static long PostmasterRandom(void);
406407
static void RandomSalt(char *md5Salt);
407408
static void signal_child(pid_t pid, int signal);
408409
static bool SignalSomeChildren(int signal, int targets);
@@ -574,6 +575,16 @@ PostmasterMain(int argc, char *argv[])
574575
*/
575576
umask(S_IRWXG | S_IRWXO);
576577

578+
/*
579+
* Initialize random(3) so we don't get the same values in every run.
580+
*
581+
* Note: the seed is pretty predictable from externally-visible facts such
582+
* as postmaster start time, so avoid using random() for security-critical
583+
* random values during postmaster startup. At the time of first
584+
* connection, PostmasterRandom will select a hopefully-more-random seed.
585+
*/
586+
srandom((unsigned int) (MyProcPid ^ MyStartTime));
587+
577588
/*
578589
* By default, palloc() requests in the postmaster will be allocated in
579590
* the PostmasterContext, which is space that can be recycled by backends.
@@ -5101,8 +5112,12 @@ RandomSalt(char *md5Salt)
51015112

51025113
/*
51035114
* PostmasterRandom
5115+
*
5116+
* Caution: use this only for values needed during connection-request
5117+
* processing. Otherwise, the intended property of having an unpredictable
5118+
* delay between random_start_time and random_stop_time will be broken.
51045119
*/
5105-
long
5120+
static long
51065121
PostmasterRandom(void)
51075122
{
51085123
/*

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)