pgpool2.git / summary / log / commit / refs

commit    650c660a89df7ca4f5247dbc61e6a65ea63fc78b
Author:   Tatsuo Ishii <ishii@postgresql.org>
Date:     Fri Sep 09 02:24:54 2016 +0000

    Do not use random() while generating MD5 salt.
    
    random() should not be used in security related applications.  To
    replace random(), import PostmasterRandom() from PostgreSQL.  Also
    store current time at the start up of Pgpool-II main process for later
    use.
    
    Per Coverity CID 1362583.


src/auth/pool_auth.c | 49 ++++++++++++++++++++++++++++++++++++---- src/main/pgpool_main.c | 4 ++++ src/main/pool_globals.c | 1 + src/query_cache/pool_memqcache.c | 2 +- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/auth/pool_auth.c b/src/auth/pool_auth.c index 5c585b014..ca503ae51 100644 --- a/src/auth/pool_auth.c +++ b/src/auth/pool_auth.c @@ -55,6 +55,8 @@ static int read_password_packet(POOL_CONNECTION *frontend, int protoMajor, char static int send_password_packet(POOL_CONNECTION *backend, int protoMajor, char *password); static int send_auth_ok(POOL_CONNECTION *frontend, int protoMajor); +static long PostmasterRandom(void); + /* * After sending the start up packet to the backend, do the * authentication against backend. if success return 0 otherwise non @@ -1287,14 +1289,51 @@ int pool_read_int(POOL_CONNECTION_POOL *cp) */ void pool_random_salt(char *md5Salt) { - srandom(time(NULL)); - long rand = random(); + long rand = PostmasterRandom(); md5Salt[0] = (rand % 255) + 1; - rand = random(); + rand = PostmasterRandom(); md5Salt[1] = (rand % 255) + 1; - rand = random(); + rand = PostmasterRandom(); md5Salt[2] = (rand % 255) + 1; - rand = random(); + rand = PostmasterRandom(); md5Salt[3] = (rand % 255) + 1; } + +/* + * PostmasterRandom + */ +static long +PostmasterRandom(void) +{ + extern struct timeval random_start_time; + static unsigned int random_seed = 0; + + /* + * Select a random seed at the time of first receiving a request. + */ + if (random_seed == 0) + { + do + { + struct timeval random_stop_time; + + 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); + + srandom(random_seed); + } + + return random(); +} diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c index 5b695a859..ecbf18f54 100644 --- a/src/main/pgpool_main.c +++ b/src/main/pgpool_main.c @@ -136,6 +136,7 @@ static void initialize_backend_status_from_watchdog(void); static struct sockaddr_un un_addr; /* unix domain socket path */ static struct sockaddr_un pcp_un_addr; /* unix domain socket path for PCP */ ProcessInfo *process_info = NULL; /* Per child info table on shmem */ +struct timeval random_start_time; /* * Private copy of backend status @@ -196,6 +197,9 @@ int PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) MemoryContext MainLoopMemoryContext; sigjmp_buf local_sigjmp_buf; + /* For PostmasterRandom */ + gettimeofday(&random_start_time, NULL); + /* Set the process type variable */ processType = PT_MAIN; processState = INITIALIZING; diff --git a/src/main/pool_globals.c b/src/main/pool_globals.c index bcf06a6ee..c87dff47c 100644 --- a/src/main/pool_globals.c +++ b/src/main/pool_globals.c @@ -25,3 +25,4 @@ pid_t mypid; /* pgpool parent process id */ ProcessType processType; ProcessState processState; +random_start_time; diff --git a/src/query_cache/pool_memqcache.c b/src/query_cache/pool_memqcache.c index a0d183150..338d9b8eb 100644 --- a/src/query_cache/pool_memqcache.c +++ b/src/query_cache/pool_memqcache.c @@ -3,7 +3,7 @@ * pgpool: a language independent connection pool server for PostgreSQL * written by Tatsuo Ishii * - * Copyright (c) 2003-2014 PgPool Global Development Group + * Copyright (c) 2003-2016 PgPool Global Development Group * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby [parent: 3fe15b7cbca1]