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

Commit 49a91b8

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 6fa51c7 commit 49a91b8

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/backend/postmaster/postmaster.c

+16-1
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 *salt, int len);
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.
@@ -5099,8 +5110,12 @@ RandomSalt(char *salt, int len)
50995110

51005111
/*
51015112
* PostmasterRandom
5113+
*
5114+
* Caution: use this only for values needed during connection-request
5115+
* processing. Otherwise, the intended property of having an unpredictable
5116+
* delay between random_start_time and random_stop_time will be broken.
51025117
*/
5103-
long
5118+
static long
51045119
PostmasterRandom(void)
51055120
{
51065121
/*

src/backend/storage/ipc/dsm.c

+1-2
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"
@@ -182,7 +181,7 @@ dsm_postmaster_startup(PGShmemHeader *shim)
182181
{
183182
Assert(dsm_control_address == NULL);
184183
Assert(dsm_control_mapped_size == 0);
185-
dsm_control_handle = (dsm_handle) PostmasterRandom();
184+
dsm_control_handle = random();
186185
if (dsm_control_handle == 0)
187186
continue;
188187
if (dsm_impl_op(DSM_OP_CREATE, dsm_control_handle, segsize,

src/include/postmaster/postmaster.h

-1
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)