mirror of
https://github.com/postgres/postgres.git
synced 2025-08-30 06:01:21 +03:00
Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes, for use in both frontend and backend. At the moment, it's only used in the backend, but the upcoming SCRAM authentication patches need strong random numbers in libpq as well. pg_strong_random() is based on, and replaces, the existing implementation in pgcrypto. It can acquire strong random numbers from a number of sources, depending on what's available: - OpenSSL RAND_bytes(), if built with OpenSSL - On Windows, the native cryptographic functions are used - /dev/urandom Unlike the current pgcrypto function, the source is chosen by configure. That makes it easier to test different implementations, and ensures that we don't accidentally fall back to a less secure implementation, if the primary source fails. All of those methods are quite reliable, it would be pretty surprising for them to fail, so we'd rather find out by failing hard. If no strong random source is available, we fall back to using erand48(), seeded from current timestamp, like PostmasterRandom() was. That isn't cryptographically secure, but allows us to still work on platforms that don't have any of the above stronger sources. Because it's not very secure, the built-in implementation is only used if explicitly requested with --disable-strong-random. This replaces the more complicated Fortuna algorithm we used to have in pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom, so it doesn't seem worth the maintenance effort to keep that. pgcrypto functions that require strong random numbers will be disabled with --disable-strong-random. Original patch by Magnus Hagander, tons of further work by Michael Paquier and me. Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
This commit is contained in:
@@ -164,7 +164,7 @@
|
||||
typedef struct bkend
|
||||
{
|
||||
pid_t pid; /* process id of backend */
|
||||
long cancel_key; /* cancel key for cancels for this backend */
|
||||
int32 cancel_key; /* cancel key for cancels for this backend */
|
||||
int child_slot; /* PMChildSlot for this backend, if any */
|
||||
|
||||
/*
|
||||
@@ -358,13 +358,15 @@ static volatile bool avlauncher_needs_signal = false;
|
||||
static volatile bool StartWorkerNeeded = true;
|
||||
static volatile bool HaveCrashedWorker = false;
|
||||
|
||||
#ifndef HAVE_STRONG_RANDOM
|
||||
/*
|
||||
* State for assigning random salts and cancel keys.
|
||||
* State for assigning cancel keys.
|
||||
* Also, the global MyCancelKey passes the cancel key assigned to a given
|
||||
* backend from the postmaster to that backend (via fork).
|
||||
*/
|
||||
static unsigned int random_seed = 0;
|
||||
static struct timeval random_start_time;
|
||||
#endif
|
||||
|
||||
#ifdef USE_BONJOUR
|
||||
static DNSServiceRef bonjour_sdref = NULL;
|
||||
@@ -403,8 +405,7 @@ static void processCancelRequest(Port *port, void *pkt);
|
||||
static int initMasks(fd_set *rmask);
|
||||
static void report_fork_failure_to_client(Port *port, int errnum);
|
||||
static CAC_state canAcceptConnections(void);
|
||||
static long PostmasterRandom(void);
|
||||
static void RandomSalt(char *salt, int len);
|
||||
static bool RandomCancelKey(int32 *cancel_key);
|
||||
static void signal_child(pid_t pid, int signal);
|
||||
static bool SignalSomeChildren(int signal, int targets);
|
||||
static void TerminateChildren(int signal);
|
||||
@@ -471,7 +472,7 @@ typedef struct
|
||||
InheritableSocket portsocket;
|
||||
char DataDir[MAXPGPATH];
|
||||
pgsocket ListenSocket[MAXLISTEN];
|
||||
long MyCancelKey;
|
||||
int32 MyCancelKey;
|
||||
int MyPMChildSlot;
|
||||
#ifndef WIN32
|
||||
unsigned long UsedShmemSegID;
|
||||
@@ -1292,8 +1293,10 @@ PostmasterMain(int argc, char *argv[])
|
||||
* Remember postmaster startup time
|
||||
*/
|
||||
PgStartTime = GetCurrentTimestamp();
|
||||
/* PostmasterRandom wants its own copy */
|
||||
#ifndef HAVE_STRONG_RANDOM
|
||||
/* RandomCancelKey wants its own copy */
|
||||
gettimeofday(&random_start_time, NULL);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* We're ready to rock and roll...
|
||||
@@ -2344,15 +2347,6 @@ ConnCreate(int serverFd)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Precompute password salt values to use for this connection. It's
|
||||
* slightly annoying to do this long in advance of knowing whether we'll
|
||||
* need 'em or not, but we must do the random() calls before we fork, not
|
||||
* after. Else the postmaster's random sequence won't get advanced, and
|
||||
* all backends would end up using the same salt...
|
||||
*/
|
||||
RandomSalt(port->md5Salt, sizeof(port->md5Salt));
|
||||
|
||||
/*
|
||||
* Allocate GSSAPI specific state struct
|
||||
*/
|
||||
@@ -3905,7 +3899,14 @@ BackendStartup(Port *port)
|
||||
* backend will have its own copy in the forked-off process' value of
|
||||
* MyCancelKey, so that it can transmit the key to the frontend.
|
||||
*/
|
||||
MyCancelKey = PostmasterRandom();
|
||||
if (!RandomCancelKey(&MyCancelKey))
|
||||
{
|
||||
ereport(LOG,
|
||||
(errcode(ERRCODE_OUT_OF_MEMORY),
|
||||
errmsg("could not acquire random number")));
|
||||
return STATUS_ERROR;
|
||||
}
|
||||
|
||||
bn->cancel_key = MyCancelKey;
|
||||
|
||||
/* Pass down canAcceptConnections state */
|
||||
@@ -4218,8 +4219,10 @@ BackendRun(Port *port)
|
||||
* generator state. We have to clobber the static random_seed *and* start
|
||||
* a new random sequence in the random() library function.
|
||||
*/
|
||||
#ifndef HAVE_STRONG_RANDOM
|
||||
random_seed = 0;
|
||||
random_start_time.tv_usec = 0;
|
||||
#endif
|
||||
/* slightly hacky way to convert timestamptz into integers */
|
||||
TimestampDifference(0, port->SessionStartTime, &secs, &usecs);
|
||||
srandom((unsigned int) (MyProcPid ^ (usecs << 12) ^ secs));
|
||||
@@ -5068,63 +5071,42 @@ StartupPacketTimeoutHandler(void)
|
||||
|
||||
|
||||
/*
|
||||
* RandomSalt
|
||||
* Generate a random cancel key.
|
||||
*/
|
||||
static void
|
||||
RandomSalt(char *salt, int len)
|
||||
static bool
|
||||
RandomCancelKey(int32 *cancel_key)
|
||||
{
|
||||
long rand;
|
||||
int i;
|
||||
|
||||
#ifdef HAVE_STRONG_RANDOM
|
||||
return pg_strong_random((char *) cancel_key, sizeof(int32));
|
||||
#else
|
||||
/*
|
||||
* We use % 255, sacrificing one possible byte value, so as to ensure that
|
||||
* all bits of the random() value participate in the result. While at it,
|
||||
* add one to avoid generating any null bytes.
|
||||
* If built with --disable-strong-random, use plain old erand48.
|
||||
*
|
||||
* We cannot use pg_backend_random() in postmaster, because it stores
|
||||
* its state in shared memory.
|
||||
*/
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
rand = PostmasterRandom();
|
||||
salt[i] = (rand % 255) + 1;
|
||||
}
|
||||
}
|
||||
static unsigned short seed[3];
|
||||
|
||||
/*
|
||||
* PostmasterRandom
|
||||
*
|
||||
* Caution: use this only for values needed during connection-request
|
||||
* processing. Otherwise, the intended property of having an unpredictable
|
||||
* delay between random_start_time and random_stop_time will be broken.
|
||||
*/
|
||||
static long
|
||||
PostmasterRandom(void)
|
||||
{
|
||||
/*
|
||||
* Select a random seed at the time of first receiving a request.
|
||||
*/
|
||||
if (random_seed == 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
struct timeval random_stop_time;
|
||||
struct timeval random_stop_time;
|
||||
|
||||
gettimeofday(&random_stop_time, NULL);
|
||||
gettimeofday(&random_stop_time, NULL);
|
||||
|
||||
/*
|
||||
* We are not sure how much precision is in tv_usec, so we swap
|
||||
* the high and low 16 bits of 'random_stop_time' and XOR them
|
||||
* with 'random_start_time'. On the off chance that the result is
|
||||
* 0, we loop until it isn't.
|
||||
*/
|
||||
random_seed = random_start_time.tv_usec ^
|
||||
((random_stop_time.tv_usec << 16) |
|
||||
((random_stop_time.tv_usec >> 16) & 0xffff));
|
||||
}
|
||||
while (random_seed == 0);
|
||||
seed[0] = (unsigned short) random_start_time.tv_usec;
|
||||
seed[1] = (unsigned short) (random_stop_time.tv_usec) ^ (random_start_time.tv_usec >> 16);
|
||||
seed[2] = (unsigned short) (random_stop_time.tv_usec >> 16);
|
||||
|
||||
srandom(random_seed);
|
||||
random_seed = 1;
|
||||
}
|
||||
|
||||
return random();
|
||||
*cancel_key = pg_jrand48(seed);
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -5295,16 +5277,23 @@ StartAutovacuumWorker(void)
|
||||
*/
|
||||
if (canAcceptConnections() == CAC_OK)
|
||||
{
|
||||
/*
|
||||
* Compute the cancel key that will be assigned to this session.
|
||||
* We probably don't need cancel keys for autovac workers, but
|
||||
* we'd better have something random in the field to prevent
|
||||
* unfriendly people from sending cancels to them.
|
||||
*/
|
||||
if (!RandomCancelKey(&MyCancelKey))
|
||||
{
|
||||
ereport(LOG,
|
||||
(errcode(ERRCODE_INTERNAL_ERROR),
|
||||
errmsg("could not acquire random number")));
|
||||
return;
|
||||
}
|
||||
|
||||
bn = (Backend *) malloc(sizeof(Backend));
|
||||
if (bn)
|
||||
{
|
||||
/*
|
||||
* Compute the cancel key that will be assigned to this session.
|
||||
* We probably don't need cancel keys for autovac workers, but
|
||||
* we'd better have something random in the field to prevent
|
||||
* unfriendly people from sending cancels to them.
|
||||
*/
|
||||
MyCancelKey = PostmasterRandom();
|
||||
bn->cancel_key = MyCancelKey;
|
||||
|
||||
/* Autovac workers are not dead_end and need a child slot */
|
||||
@@ -5592,8 +5581,25 @@ bgworker_should_start_now(BgWorkerStartTime start_time)
|
||||
static bool
|
||||
assign_backendlist_entry(RegisteredBgWorker *rw)
|
||||
{
|
||||
Backend *bn = malloc(sizeof(Backend));
|
||||
Backend *bn;
|
||||
|
||||
/*
|
||||
* Compute the cancel key that will be assigned to this session. We
|
||||
* probably don't need cancel keys for background workers, but we'd better
|
||||
* have something random in the field to prevent unfriendly people from
|
||||
* sending cancels to them.
|
||||
*/
|
||||
if (!RandomCancelKey(&MyCancelKey))
|
||||
{
|
||||
ereport(LOG,
|
||||
(errcode(ERRCODE_INTERNAL_ERROR),
|
||||
errmsg("could not acquire random number")));
|
||||
|
||||
rw->rw_crashed_at = GetCurrentTimestamp();
|
||||
return false;
|
||||
}
|
||||
|
||||
bn = malloc(sizeof(Backend));
|
||||
if (bn == NULL)
|
||||
{
|
||||
ereport(LOG,
|
||||
@@ -5610,15 +5616,7 @@ assign_backendlist_entry(RegisteredBgWorker *rw)
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the cancel key that will be assigned to this session. We
|
||||
* probably don't need cancel keys for background workers, but we'd better
|
||||
* have something random in the field to prevent unfriendly people from
|
||||
* sending cancels to them.
|
||||
*/
|
||||
MyCancelKey = PostmasterRandom();
|
||||
bn->cancel_key = MyCancelKey;
|
||||
|
||||
bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
|
||||
bn->bkend_type = BACKEND_TYPE_BGWORKER;
|
||||
bn->dead_end = false;
|
||||
|
Reference in New Issue
Block a user