pgsql: Replace PostmasterRandom() with a stronger source, second attemp

From: Heikki Linnakangas <heikki(dot)linnakangas(at)iki(dot)fi>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Replace PostmasterRandom() with a stronger source, second attemp
Date: 2016-12-05 11:44:37
Message-ID: E1cDrhF-0002Wp-PR@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/fe0a0b5993dfe24e4b3bcf52fa64ff41a444b8f1

Modified Files
--------------
configure | 109 +++++
configure.in | 52 +++
contrib/pgcrypto/Makefile | 2 +-
contrib/pgcrypto/expected/pgp-compression_1.out | 42 ++
contrib/pgcrypto/expected/pgp-decrypt_1.out | 424 +++++++++++++++++++
contrib/pgcrypto/expected/pgp-encrypt_1.out | 161 +++++++
contrib/pgcrypto/expected/pgp-pubkey-encrypt_1.out | 62 +++
contrib/pgcrypto/fortuna.c | 463 ---------------------
contrib/pgcrypto/fortuna.h | 38 --
contrib/pgcrypto/internal.c | 63 ---
contrib/pgcrypto/openssl.c | 46 --
contrib/pgcrypto/pgcrypto.c | 27 +-
contrib/pgcrypto/pgp-encrypt.c | 22 +-
contrib/pgcrypto/pgp-mpi-internal.c | 10 +-
contrib/pgcrypto/pgp-pgsql.c | 95 +----
contrib/pgcrypto/pgp-pubenc.c | 26 +-
contrib/pgcrypto/pgp-s2k.c | 15 +-
contrib/pgcrypto/px-crypt.c | 7 +-
contrib/pgcrypto/px.c | 34 +-
contrib/pgcrypto/px.h | 7 +-
contrib/pgcrypto/random.c | 247 -----------
doc/src/sgml/installation.sgml | 17 +
src/Makefile.global.in | 1 +
src/backend/libpq/auth.c | 62 ++-
src/backend/libpq/crypt.c | 10 +-
src/backend/postmaster/postmaster.c | 146 ++++---
src/backend/storage/ipc/ipci.c | 3 +
src/backend/storage/lmgr/lwlocknames.txt | 1 +
src/backend/utils/init/globals.c | 2 +-
src/backend/utils/misc/Makefile | 5 +-
src/backend/utils/misc/backend_random.c | 158 +++++++
src/include/libpq/crypt.h | 2 +-
src/include/libpq/libpq-be.h | 1 -
src/include/miscadmin.h | 2 +-
src/include/pg_config.h.in | 12 +
src/include/pg_config.h.win32 | 12 +
src/include/port.h | 6 +
src/include/utils/backend_random.h | 19 +
src/port/Makefile | 4 +
src/port/erand48.c | 7 +
src/port/pg_strong_random.c | 149 +++++++
src/tools/msvc/Mkvcbuild.pm | 5 +-
42 files changed, 1472 insertions(+), 1104 deletions(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2016-12-05 11:46:20 pgsql: Fix typo in docs.
Previous Message Fujii Masao 2016-12-05 11:32:20 pgsql: Fix incorrect output from gin_desc().