8000 Replace PostmasterRandom() with a stronger source, second attempt. · postgrespro/postgres@fe0a0b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe0a0b5

Browse files
committed
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
1 parent 5dc851a commit fe0a0b5

File tree

42 files changed

+1472
-1104
lines changed
  • utils
  • include
  • port
  • tools/msvc
  • Some content is hidden

    Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

    42 files changed

    +1472
    -1104
    lines changed

    configure

    Lines changed: 109 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -739,6 +739,7 @@ GENHTML
    739739
    LCOV
    740740
    GCOV
    741741
    enable_debug
    742+
    enable_strong_random
    742743
    enable_rpath
    743744
    default_port
    744745
    WANTED_LANGUAGES
    @@ -806,6 +807,7 @@ with_pgport
    806807
    enable_rpath
    807808
    enable_spinlocks
    808809
    enable_atomics
    810+
    enable_strong_random
    809811
    enable_debug
    810812
    enable_profiling
    811813
    enable_coverage
    @@ -1478,6 +1480,7 @@ Optional Features:
    14781480
    executables
    14791481
    --disable-spinlocks do not use spinlocks
    14801482
    --disable-atomics do not use atomic operations
    1483+
    --disable-strong-random do not use a strong random number source
    14811484
    --enable-debug build with debugging symbols (-g)
    14821485
    --enable-profiling build with profiling enabled
    14831486
    --enable-coverage build with coverage testing instrumentation
    @@ -3192,6 +3195,34 @@ fi
    31923195

    31933196

    31943197

    3198+
    #
    3199+
    # Random number generation
    3200+
    #
    3201+
    3202+
    3203+
    # Check whether --enable-strong-random was given.
    3204+
    if test "${enable_strong_random+set}" = set; then :
    3205+
    enableval=$enable_strong_random;
    3206+
    case $enableval in
    3207+
    yes)
    3208+
    :
    3209+
    ;;
    3210+
    no)
    3211+
    :
    3212+
    ;;
    3213+
    *)
    3214+
    as_fn_error $? "no argument expected for --enable-strong-random option" "$LINENO" 5
    3215+
    ;;
    3216+
    esac
    3217+
    3218+
    else
    3219+
    enable_strong_random=yes
    3220+
    3221+
    fi
    3222+
    3223+
    3224+
    3225+
    31953226
    #
    31963227
    # --enable-debug adds -g to compiler flags
    31973228
    #
    @@ -14982,6 +15013,84 @@ $as_echo "#define USE_WIN32_SHARED_MEMORY 1" >>confdefs.h
    1498215013
    SHMEM_IMPLEMENTATION="src/backend/port/win32_shmem.c"
    1498315014
    fi
    1498415015

    15016+
    # Select random number source
    15017+
    #
    15018+
    # You can override this logic by setting the appropriate USE_*RANDOM flag to 1
    15019+
    # in the template or configure command line.
    15020+
    15021+
    # If not selected manually, try to select a source automatically.
    15022+
    if test "$enable_strong_random" = "yes" && test x"$USE_OPENSSL_RANDOM" = x"" && test x"$USE_WIN32_RANDOM" = x"" && test x"$USE_DEV_URANDOM" = x"" ; then
    15023+
    if test x"$with_openssl" = x"yes" ; then
    15024+
    USE_OPENSSL_RANDOM=1
    15025+
    elif test "$PORTNAME" = x"win32" ; then
    15026+
    USE_WIN32_RANDOM=1
    15027+
    else
    15028+
    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for /dev/urandom" >&5
    15029+
    $as_echo_n "checking for /dev/urandom... " >&6; }
    15030+
    if ${ac_cv_file__dev_urandom+:} false; then :
    15031+
    $as_echo_n "(cached) " >&6
    15032+
    else
    15033+
    test "$cross_compiling" = yes &&
    15034+
    as_fn_error $? "cannot check for file existence when cross compiling" "$LINENO" 5
    15035+
    if test -r "/dev/urandom"; then
    15036+
    ac_cv_file__dev_urandom=yes
    15037+
    else
    15038+
    ac_cv_file__dev_urandom=no
    15039+
    fi
    15040+
    fi
    15041+
    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_file__dev_urandom" >&5
    15042+
    $as_echo "$ac_cv_file__dev_urandom" >&6; }
    15043+
    if test "x$ac_cv_file__dev_urandom" = xyes; then :
    15044+
    15045+
    fi
    15046+
    15047+
    15048+
    if test x"$ac_cv_file__dev_urandom" = x"yes" ; then
    15049+
    USE_DEV_URANDOM=1
    15050+
    fi
    15051+
    fi
    15052+
    fi
    15053+
    15054+
    { $as_echo "$as_me:${as_lineno-$LINENO}: checking which random number source to use" >&5
    15055+
    $as_echo_n "checking which random number source to use... " >&6; }
    15056+
    if test "$enable_strong_random" = yes ; then
    15057+
    if test x"$USE_OPENSSL_RANDOM" = x"1" ; then
    15058+
    15059+
    $as_echo "#define USE_OPENSSL_RANDOM 1" >>confdefs.h
    15060+
    15061+
    { $as_echo "$as_me:${as_lineno-$LINENO}: result: OpenSSL" >&5
    15062+
    $as_echo "OpenSSL" >&6; }
    15063+
    elif test x"$USE_WIN32_RANDOM" = x"1" ; then
    15064+
    15065+
    $as_echo "#define USE_WIN32_RANDOM 1" >>confdefs.h
    15066+
    15067+
    { $as_echo "$as_me:${as_lineno-$LINENO}: result: Windows native" >&5
    15068+
    $as_echo "Windows native" >&6; }
    15069+
    elif test x"$USE_DEV_URANDOM" = x"1" ; then
    15070+
    15071+
    $as_echo "#define USE_DEV_URANDOM 1" >>confdefs.h
    15072+
    15073+
    { $as_echo "$as_me:${as_lineno-$LINENO}: result: /dev/urandom" >&5
    15074+
    $as_echo "/dev/urandom" >&6; }
    15075+
    else
    15076+
    as_fn_error $? "
    15077+
    no source of strong random numbers was found
    15078+
    PostgreSQL can use OpenSSL or /dev/urandom as a source of random numbers,
    15079+
    for authentication protocols. You can use --disable-strong-random to use of a built-in
    15080+
    pseudo random number generator, but that may be insecure." "$LINENO" 5
    15081+
    fi
    15082+
    15083+
    $as_echo "#define HAVE_STRONG_RANDOM 1" >>confdefs.h
    15084+
    15085+
    else
    15086+
    { $as_echo "$as_me:${as_lineno-$LINENO}: result: weak builtin PRNG" >&5
    15087+
    $as_echo "weak builtin PRNG" >&6; }
    15088+
    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING:
    15089+
    *** Not using a strong random number source may be insecure." >&5
    15090+
    $as_echo "$as_me: WARNING:
    15091+
    *** Not using a strong random number source may be insecure." >&2;}
    15092+
    fi
    15093+
    1498515094
    # If not set in template file, set bytes to use libc memset()
    1498615095
    if test x"$MEMSET_LOOP_LIMIT" = x"" ; then
    1498715096
    MEMSET_LOOP_LIMIT=1024

    configure.in

    Lines changed: 52 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -193,6 +193,13 @@ PGAC_ARG_BOOL(enable, spinlocks, yes,
    193193
    PGAC_ARG_BOOL(enable, atomics, yes,
    194194
    [do not use atomic operations])
    195195

    196+
    #
    197+
    # Random number generation
    198+
    #
    199+
    PGAC_ARG_BOOL(enable, strong-random, yes,
    200+
    [do not use a strong random number source])
    201+
    AC_SUBST(enable_strong_random)
    202+
    196203
    #
    197204
    # --enable-debug adds -g to compiler flags
    198205
    #
    @@ -1965,6 +1972,51 @@ else
    19651972
    SHMEM_IMPLEMENTATION="src/backend/port/win32_shmem.c"
    19661973
    fi
    19671974

    1975+
    # Select random number source
    1976+
    #
    1977+
    # You can override this logic by setting the appropriate USE_*RANDOM flag to 1
    1978+
    # in the template or configure command line.
    1979+
    1980+
    # If not selected manually, try to select a source automatically.
    1981+
    if test "$enable_strong_random" = "yes" && test x"$USE_OPENSSL_RANDOM" = x"" && test x"$USE_WIN32_RANDOM" = x"" && test x"$USE_DEV_URANDOM" = x"" ; then
    1982+
    if test x"$with_openssl" = x"yes" ; then
    1983+
    USE_OPENSSL_RANDOM=1
    1984+
    elif test "$PORTNAME" = x"win32" ; then
    1985+
    USE_WIN32_RANDOM=1
    1986+
    else
    1987+
    AC_CHECK_FILE([/dev/urandom], [], [])
    1988+
    1989+
    if test x"$ac_cv_file__dev_urandom" = x"yes" ; then
    1990+
    USE_DEV_URANDOM=1
    1991+
    fi
    1992+
    fi
    1993+
    fi
    1994+
    1995+
    AC_MSG_CHECKING([which random number source to use])
    1996+
    if test "$enable_strong_random" = yes ; then
    1997+
    if test x"$USE_OPENSSL_RANDOM" = x"1" ; then
    1998+
    AC_DEFINE(USE_OPENSSL_RANDOM, 1, [Define to use OpenSSL for random number generation])
    1999+
    AC_MSG_RESULT([OpenSSL])
    2000+
    elif test x"$USE_WIN32_RANDOM" = x"1" ; then
    2001+
    AC_DEFINE(USE_WIN32_RANDOM, 1, [Define to use native Windows API for random number generation])
    2002+
    AC_MSG_RESULT([Windows native])
    2003+
    elif test x"$USE_DEV_URANDOM" = x"1" ; then
    2004+
    AC_DEFINE(USE_DEV_URANDOM, 1, [Define to use /dev/urandom for random number generation])
    2005+
    AC_MSG_RESULT([/dev/urandom])
    2006+
    else
    2007+
    AC_MSG_ERROR([
    2008+
    no source of strong random numbers was found
    2009+
    PostgreSQL can use OpenSSL or /dev/urandom as a source of random numbers,
    2010+
    for authentication protocols. You can use --disable-strong-random to use of a built-in
    2011+
    pseudo random number generator, but that may be insecure.])
    2012+
    fi
    2013+
    AC_DEFINE(HAVE_STRONG_RANDOM, 1, [Define to use have a strong random number source])
    2014+
    else
    2015+
    AC_MSG_RESULT([weak builtin PRNG])
    2016+
    AC_MSG_WARN([
    2017+
    *** Not using a strong random number source may be insecure.])
    2018+
    fi
    2019+
    19682020
    # If not set in template file, set bytes to use libc memset()
    19692021
    if test x"$MEMSET_LOOP_LIMIT" = x"" ; then
    19702022
    MEMSET_LOOP_LIMIT=1024

    contrib/pgcrypto/Makefile

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -1,7 +1,7 @@
    11
    # contrib/pgcrypto/Makefile
    22

    33
    INT_SRCS = md5.c sha1.c sha2.c internal.c internal-sha2.c blf.c rijndael.c \
    4-
    fortuna.c random.c pgp-mpi-internal.c imath.c
    4+
    pgp-mpi-internal.c imath.c
    55
    INT_TESTS = sha2
    66

    77
    OSSL_SRCS = openssl.c pgp-mpi-openssl.c
    Lines changed: 42 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,42 @@
    1+
    --
    2+
    -- PGP compression support
    3+
    --
    4+
    select pgp_sym_decrypt(dearmor('
    5+
    -----BEGIN PGP MESSAGE-----
    6+
    7+
    ww0ECQMCsci6AdHnELlh0kQB4jFcVwHMJg0Bulop7m3Mi36s15TAhBo0AnzIrRFrdLVCkKohsS6+
    8+
    DMcmR53SXfLoDJOv/M8uKj3QSq7oWNIp95pxfA==
    9+
    =tbSn
    10+
    - E565 ----END PGP MESSAGE-----
    11+
    '), 'key', 'expect-compress-algo=1');
    12+
    pgp_sym_decrypt
    13+
    -----------------
    14+
    Secret message
    15+
    (1 row)
    16+
    17+
    select pgp_sym_decrypt(
    18+
    pgp_sym_encrypt('Secret message', 'key', 'compress-algo=0'),
    19+
    'key', 'expect-compress-algo=0');
    20+
    ERROR: pg_random_bytes() is not supported by this build
    21+
    DETAIL: This functionality requires a source of strong random numbers
    22+
    HINT: You need to rebuild PostgreSQL using --enable-strong-random
    23+
    select pgp_sym_decrypt(
    24+
    pgp_sym_encrypt('Secret message', 'key', 'compress-algo=1'),
    25+
    'key', 'expect-compress-algo=1');
    26+
    ERROR: pg_random_bytes() is not supported by this build
    27+
    DETAIL: This functionality requires a source of strong random numbers
    28+
    HINT: You need to rebuild PostgreSQL using --enable-strong-random
    29+
    select pgp_sym_decrypt(
    30+
    pgp_sym_encrypt('Secret message', 'key', 'compress-algo=2'),
    31+
    'key', 'expect-compress-algo=2');
    32+
    ERROR: pg_random_bytes() is not supported by this build
    33+
    DETAIL: This functionality requires a source of strong random numbers
    34+
    HINT: You need to rebuild PostgreSQL using --enable-strong-random
    35+
    -- level=0 should turn compression off
    36+
    select pgp_sym_decrypt(
    37+
    pgp_sym_encrypt('Secret message', 'key',
    38+
    'compress-algo=2, compress-level=0'),
    39+
    'key', 'expect-compress-algo=0');
    40+
    ERROR: pg_random_bytes() is not supported by this build
    41+
    DETAIL: This functionality requires a source of strong random numbers
    42+
    HINT: You need to rebuild PostgreSQL using --enable-strong-random

    0 commit comments

    Comments
     (0)
    0