1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Remove configure switch --disable-strong-random

This removes a portion of infrastructure introduced by fe0a0b5 to allow
compilation of Postgres in environments where no strong random source is
available, meaning that there is no linking to OpenSSL and no
/dev/urandom (Windows having its own CryptoAPI).  No systems shipped
this century lack /dev/urandom, and the buildfarm is actually not
testing this switch at all, so just remove it.  This simplifies
particularly some backend code which included a fallback implementation
using shared memory, and removes a set of alternate regression output
files from pgcrypto.

Author: Michael Paquier
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/20181230063219.GG608@paquier.xyz
This commit is contained in:
Michael Paquier
2019-01-01 20:05:51 +09:00
parent d880b208e5
commit 1707a0d2aa
32 changed files with 60 additions and 1183 deletions

View File

@ -203,7 +203,6 @@ enable_dtrace = @enable_dtrace@
enable_coverage = @enable_coverage@
enable_tap_tests = @enable_tap_tests@
enable_thread_safety = @enable_thread_safety@
enable_strong_random = @enable_strong_random@
python_includespec = @python_includespec@
python_libdir = @python_libdir@

View File

@ -65,7 +65,6 @@
#include "storage/reinit.h"
#include "storage/smgr.h"
#include "storage/spin.h"
#include "utils/backend_random.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
@ -5132,7 +5131,7 @@ BootStrapXLOG(void)
* a genuine-looking password challenge for the non-existent user, in lieu
* of an actual stored password.
*/
if (!pg_backend_random(mock_auth_nonce, MOCK_AUTH_NONCE_LEN))
if (!pg_strong_random(mock_auth_nonce, MOCK_AUTH_NONCE_LEN))
ereport(PANIC,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not generate secret authorization token")));

View File

@ -102,7 +102,6 @@
#include "libpq/crypt.h"
#include "libpq/scram.h"
#include "miscadmin.h"
#include "utils/backend_random.h"
#include "utils/builtins.h"
#include "utils/timestamp.h"
@ -468,7 +467,7 @@ pg_be_scram_build_verifier(const char *password)
password = (const char *) prep_password;
/* Generate random salt */
if (!pg_backend_random(saltbuf, SCRAM_DEFAULT_SALT_LEN))
if (!pg_strong_random(saltbuf, SCRAM_DEFAULT_SALT_LEN))
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not generate random salt")));
@ -1123,7 +1122,7 @@ build_server_first_message(scram_state *state)
char raw_nonce[SCRAM_RAW_NONCE_LEN];
int encoded_len;
if (!pg_backend_random(raw_nonce, SCRAM_RAW_NONCE_LEN))
if (!pg_strong_random(raw_nonce, SCRAM_RAW_NONCE_LEN))
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not generate random nonce")));

View File

@ -36,7 +36,6 @@
#include "port/pg_bswap.h"
#include "replication/walsender.h"
#include "storage/ipc.h"
#include "utils/backend_random.h"
#include "utils/timestamp.h"
@ -835,7 +834,7 @@ CheckMD5Auth(Port *port, char *shadow_pass, char **logdetail)
errmsg("MD5 authentication is not supported when \"db_user_namespace\" is enabled")));
/* include the salt to use for computing the response */
if (!pg_backend_random(md5Salt, 4))
if (!pg_strong_random(md5Salt, 4))
{
ereport(LOG,
(errmsg("could not generate random MD5 salt")));
@ -3036,7 +3035,7 @@ PerformRadiusTransaction(const char *server, const char *secret, const char *por
/* Construct RADIUS packet */
packet->code = RADIUS_ACCESS_REQUEST;
packet->length = RADIUS_HEADER_LENGTH;
if (!pg_backend_random((char *) packet->vector, RADIUS_VECTOR_LENGTH))
if (!pg_strong_random(packet->vector, RADIUS_VECTOR_LENGTH))
{
ereport(LOG,
(errmsg("could not generate random encryption vector")));

View File

@ -367,16 +367,6 @@ static volatile sig_atomic_t WalReceiverRequested = false;
static volatile bool StartWorkerNeeded = true;
static volatile bool HaveCrashedWorker = false;
#ifndef HAVE_STRONG_RANDOM
/*
* 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_SSL
/* Set when and if SSL has been initialized properly */
static bool LoadedSSL = false;
@ -1361,10 +1351,6 @@ PostmasterMain(int argc, char *argv[])
* Remember postmaster startup time
*/
PgStartTime = GetCurrentTimestamp();
#ifndef HAVE_STRONG_RANDOM
/* RandomCancelKey wants its own copy */
gettimeofday(&random_start_time, NULL);
#endif
/*
* Report postmaster status in the postmaster.pid file, to allow pg_ctl to
@ -2531,27 +2517,12 @@ InitProcessGlobals(void)
MyStartTimestamp = GetCurrentTimestamp();
MyStartTime = timestamptz_to_time_t(MyStartTimestamp);
/*
* Don't want backend to be able to see the postmaster random number
* generator state. We have to clobber the static random_seed.
*/
#ifndef HAVE_STRONG_RANDOM
random_seed = 0;
random_start_time.tv_usec = 0;
#endif
/*
* Set a different seed for random() in every process. We want something
* unpredictable, so if possible, use high-quality random bits for the
* seed. Otherwise, fall back to a seed based on timestamp and PID.
*
* Note we can't use pg_backend_random here, since this is used in the
* postmaster, and even in a backend we might not be attached to shared
* memory yet.
*/
#ifdef HAVE_STRONG_RANDOM
if (!pg_strong_random(&rseed, sizeof(rseed)))
#endif
{
/*
* Since PIDs and timestamps tend to change more frequently in their
@ -5256,38 +5227,7 @@ StartupPacketTimeoutHandler(void)
static bool
RandomCancelKey(int32 *cancel_key)
{
#ifdef HAVE_STRONG_RANDOM
return pg_strong_random((char *) cancel_key, sizeof(int32));
#else
/*
* 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.
*/
static unsigned short seed[3];
/*
* Select a random seed at the time of first receiving a request.
*/
if (random_seed == 0)
{
struct timeval random_stop_time;
gettimeofday(&random_stop_time, NULL);
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);
random_seed = 1;
}
*cancel_key = pg_jrand48(seed);
return true;
#endif
return pg_strong_random(cancel_key, sizeof(int32));
}
/*

View File

@ -44,7 +44,6 @@
#include "storage/procsignal.h"
#include "storage/sinvaladt.h"
#include "storage/spin.h"
#include "utils/backend_random.h"
#include "utils/snapmgr.h"
@ -149,7 +148,6 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
size = add_size(size, BTreeShmemSize());
size = add_size(size, SyncScanShmemSize());
size = add_size(size, AsyncShmemSize());
size = add_size(size, BackendRandomShmemSize());
#ifdef EXEC_BACKEND
size = add_size(size, ShmemBackendArraySize());
#endif
@ -269,7 +267,6 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
BTreeShmemInit();
SyncScanShmemInit();
AsyncShmemInit();
BackendRandomShmemInit();
#ifdef EXEC_BACKEND

View File

@ -47,6 +47,5 @@ CommitTsLock 39
ReplicationOriginLock 40
MultiXactTruncationLock 41
OldSnapshotTimeMapLock 42
BackendRandomLock 43
LogicalRepWorkerLock 44
CLogTruncationLock 45
LogicalRepWorkerLock 43
CLogTruncationLock 44

View File

@ -24,7 +24,6 @@
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/backend_random.h"
#include "utils/float.h"
#include "utils/fmgrprotos.h"
#include "utils/sortsupport.h"
@ -2393,7 +2392,7 @@ drandom(PG_FUNCTION_ARGS)
* Should that fail for some reason, we fall back on a lower-quality
* seed based on current time and PID.
*/
if (!pg_backend_random((char *) drandom_seed, sizeof(drandom_seed)))
if (!pg_strong_random(drandom_seed, sizeof(drandom_seed)))
{
TimestampTz now = GetCurrentTimestamp();
uint64 iseed;

View File

@ -14,9 +14,9 @@ include $(top_builddir)/src/Makefile.global
override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS)
OBJS = backend_random.o guc.o help_config.o pg_config.o pg_controldata.o \
pg_rusage.o ps_status.o queryenvironment.o rls.o sampling.o \
superuser.o timeout.o tzparser.o
OBJS = guc.o help_config.o pg_config.o pg_controldata.o pg_rusage.o \
ps_status.o queryenvironment.o rls.o sampling.o superuser.o \
timeout.o tzparser.o
# This location might depend on the installation directories. Therefore
# we can't substitute it into pg_config.h.

View File

@ -1,158 +0,0 @@
/*-------------------------------------------------------------------------
*
* backend_random.c
* Backend random number generation routine.
*
* pg_backend_random() function fills a buffer with random bytes. Normally,
* it is just a thin wrapper around pg_strong_random(), but when compiled
* with --disable-strong-random, we provide a built-in implementation.
*
* This function is used for generating nonces in authentication, and for
* random salt generation in pgcrypto. The built-in implementation is not
* cryptographically strong, but if the user asked for it, we'll go ahead
* and use it anyway.
*
* The built-in implementation uses the standard erand48 algorithm, with
* a seed shared between all backends.
*
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/utils/misc/backend_random.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <sys/time.h>
#include "miscadmin.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/backend_random.h"
#include "utils/timestamp.h"
#ifdef HAVE_STRONG_RANDOM
Size
BackendRandomShmemSize(void)
{
return 0;
}
void
BackendRandomShmemInit(void)
{
/* do nothing */
}
bool
pg_backend_random(char *dst, int len)
{
/* should not be called in postmaster */
Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
return pg_strong_random(dst, len);
}
#else
/*
* Seed for the PRNG, stored in shared memory.
*
* Protected by BackendRandomLock.
*/
typedef struct
{
bool initialized;
unsigned short seed[3];
} BackendRandomShmemStruct;
static BackendRandomShmemStruct * BackendRandomShmem;
Size
BackendRandomShmemSize(void)
{
return sizeof(BackendRandomShmemStruct);
}
void
BackendRandomShmemInit(void)
{
bool found;
BackendRandomShmem = (BackendRandomShmemStruct *)
ShmemInitStruct("Backend PRNG state",
BackendRandomShmemSize(),
&found);
if (!IsUnderPostmaster)
{
Assert(!found);
BackendRandomShmem->initialized = false;
}
else
Assert(found);
}
bool
pg_backend_random(char *dst, int len)
{
int i;
char *end = dst + len;
/* should not be called in postmaster */
Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
LWLockAcquire(BackendRandomLock, LW_EXCLUSIVE);
/*
* Seed the PRNG on the first use.
*/
if (!BackendRandomShmem->initialized)
{
struct timeval now;
gettimeofday(&now, NULL);
BackendRandomShmem->seed[0] = now.tv_sec;
BackendRandomShmem->seed[1] = (unsigned short) (now.tv_usec);
BackendRandomShmem->seed[2] = (unsigned short) (now.tv_usec >> 16);
/*
* Mix in the cancel key, generated by the postmaster. This adds what
* little entropy the postmaster had to the seed.
*/
BackendRandomShmem->seed[0] ^= (MyCancelKey);
BackendRandomShmem->seed[1] ^= (MyCancelKey >> 16);
BackendRandomShmem->initialized = true;
}
for (i = 0; dst < end; i++)
{
uint32 r;
int j;
/*
* pg_jrand48 returns a 32-bit integer. Fill the next 4 bytes from it.
*/
r = (uint32) pg_jrand48(BackendRandomShmem->seed);
for (j = 0; j < 4 && dst < end; j++)
{
*(dst++) = (char) (r & 0xFF);
r >>= 8;
}
}
LWLockRelease(BackendRandomLock);
return true;
}
#endif /* HAVE_STRONG_RANDOM */

View File

@ -4844,13 +4844,9 @@ set_random_seed(const char *seed)
else if (strcmp(seed, "rand") == 0)
{
/* use some "strong" random source */
#ifdef HAVE_STRONG_RANDOM
if (!pg_strong_random(&iseed, sizeof(iseed)))
#endif
{
fprintf(stderr,
"cannot seed random from a strong source, none available: "
"use \"time\" or an unsigned integer value.\n");
fprintf(stderr, "could not generate random seed.\n");
return false;
}
}

View File

@ -552,9 +552,6 @@
/* Define to 1 if you have the `strnlen' function. */
#undef HAVE_STRNLEN
/* Define to use have a strong random number source */
#undef HAVE_STRONG_RANDOM
/* Define to 1 if you have the `strsignal' function. */
#undef HAVE_STRSIGNAL

View File

@ -412,9 +412,6 @@
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
/* Define to use have a strong random number source */
#define HAVE_STRONG_RANDOM 1
/* Define to 1 if you have the `strsignal' function. */
/* #undef HAVE_STRSIGNAL */

View File

@ -498,9 +498,12 @@ extern char *inet_net_ntop(int af, const void *src, int bits,
char *dst, size_t size);
/* port/pg_strong_random.c */
#ifdef HAVE_STRONG_RANDOM
extern bool pg_strong_random(void *buf, size_t len);
#endif
/*
* pg_backend_random used to be a wrapper for pg_strong_random before
* Postgres 12 for the backend code.
*/
#define pg_backend_random pg_strong_random
/* port/pgcheckdir.c */
extern int pg_check_dir(const char *dir);

View File

@ -1,19 +0,0 @@
/*-------------------------------------------------------------------------
*
* backend_random.h
* Declarations for backend random number generation
*
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
*
* src/include/utils/backend_random.h
*
*-------------------------------------------------------------------------
*/
#ifndef BACKEND_RANDOM_H
#define BACKEND_RANDOM_H
extern Size BackendRandomShmemSize(void);
extern void BackendRandomShmemInit(void);
extern bool pg_backend_random(char *dst, int len);
#endif /* BACKEND_RANDOM_H */

View File

@ -19,11 +19,6 @@
#include "common/scram-common.h"
#include "fe-auth.h"
/* These are needed for getpid(), in the fallback implementation */
#ifndef HAVE_STRONG_RANDOM
#include <sys/types.h>
#include <unistd.h>
#endif
/*
* Status of exchange messages used for SCRAM authentication via the
@ -72,7 +67,6 @@ static bool verify_server_signature(fe_scram_state *state);
static void calculate_client_proof(fe_scram_state *state,
const char *client_final_message_without_proof,
uint8 *result);
static bool pg_frontend_random(char *dst, int len);
/*
* Initialize SCRAM exchange status.
@ -320,7 +314,7 @@ build_client_first_message(fe_scram_state *state)
* Generate a "raw" nonce. This is converted to ASCII-printable form by
* base64-encoding it.
*/
if (!pg_frontend_random(raw_nonce, SCRAM_RAW_NONCE_LEN))
if (!pg_strong_random(raw_nonce, SCRAM_RAW_NONCE_LEN))
{
printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not generate nonce\n"));
@ -764,7 +758,7 @@ pg_fe_scram_build_verifier(const char *password)
password = (const char *) prep_password;
/* Generate a random salt */
if (!pg_frontend_random(saltbuf, SCRAM_DEFAULT_SALT_LEN))
if (!pg_strong_random(saltbuf, SCRAM_DEFAULT_SALT_LEN))
{
if (prep_password)
free(prep_password);
@ -779,55 +773,3 @@ pg_fe_scram_build_verifier(const char *password)
return result;
}
/*
* Random number generator.
*/
static bool
pg_frontend_random(char *dst, int len)
{
#ifdef HAVE_STRONG_RANDOM
return pg_strong_random(dst, len);
#else
int i;
char *end = dst + len;
static unsigned short seed[3];
static int mypid = 0;
pglock_thread();
if (mypid != getpid())
{
struct timeval now;
gettimeofday(&now, NULL);
seed[0] = now.tv_sec ^ getpid();
seed[1] = (unsigned short) (now.tv_usec);
seed[2] = (unsigned short) (now.tv_usec >> 16);
}
for (i = 0; dst < end; i++)
{
uint32 r;
int j;
/*
* pg_jrand48 returns a 32-bit integer. Fill the next 4 bytes from
* it.
*/
r = (uint32) pg_jrand48(seed);
for (j = 0; j < 4 && dst < end; j++)
{
*(dst++) = (char) (r & 0xFF);
r >>= 8;
}
}
pgunlock_thread();
return true;
#endif
}

View File

@ -37,14 +37,10 @@ LIBS += $(PTHREAD_LIBS)
OBJS = $(LIBOBJS) $(PG_CRC32C_OBJS) chklocale.o erand48.o inet_net_ntop.o \
noblock.o path.o pgcheckdir.o pgmkdirp.o pgsleep.o \
pgstrcasecmp.o pgstrsignal.o pqsignal.o \
pg_strong_random.o pgstrcasecmp.o pgstrsignal.o pqsignal.o \
qsort.o qsort_arg.o quotes.o snprintf.o sprompt.o strerror.o \
tar.o thread.o
ifeq ($(enable_strong_random), yes)
OBJS += pg_strong_random.o
endif
# libpgport.a, libpgport_shlib.a, and libpgport_srv.a contain the same files
# foo.o, foo_shlib.o, and foo_srv.o are all built from foo.c
OBJS_SHLIB = $(OBJS:%.o=%_shlib.o)