Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8f6ce7f

Browse files
committed
Guard against rare RAND_bytes() failures in pg_strong_random().
When built using OpenSSL, pg_strong_random() uses RAND_bytes() togenerate the random number. On very rare occasions that can fail, ifits PRNG has not been seeded with enough data. Additionally, once itdoes fail, all subsequent calls will also fail until more seed data isadded. Since this is required during backend startup, this can resultin all new backends failing to start until a postmaster restart.Guard against that by checking the state of OpenSSL's PRNG usingRAND_status(), and if necessary (very rarely), seeding it usingRAND_poll().Back-patch to v10, where pg_strong_random() was introduced.Dean Rasheed and Michael Paquier.Discussion:https://postgr.es/m/CAEZATCXMtxbzSAvyKKk5uCRf9pNt4UV%2BF_5v%3DgLfJUuPxU4Ytg%40mail.gmail.com
1 parentf2b1316 commit8f6ce7f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎src/port/pg_strong_random.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ pg_strong_random(void *buf, size_t len)
103103
* When built with OpenSSL, use OpenSSL's RAND_bytes function.
104104
*/
105105
#if defined(USE_OPENSSL_RANDOM)
106+
inti;
107+
108+
/*
109+
* Check that OpenSSL's CSPRNG has been sufficiently seeded, and if not
110+
* add more seed data using RAND_poll(). With some older versions of
111+
* OpenSSL, it may be necessary to call RAND_poll() a number of times.
112+
*/
113+
#defineNUM_RAND_POLL_RETRIES 8
114+
115+
for (i=0;i<NUM_RAND_POLL_RETRIES;i++)
116+
{
117+
if (RAND_status()==1)
118+
{
119+
/* The CSPRNG is sufficiently seeded */
120+
break;
121+
}
122+
123+
if (RAND_poll()==0)
124+
{
125+
/*
126+
* RAND_poll() failed to generate any seed data, which means that
127+
* RAND_bytes() will probably fail. For now, just fall through
128+
* and let that happen. XXX: maybe we could seed it some other
129+
* way.
130+
*/
131+
break;
132+
}
133+
}
134+
106135
if (RAND_bytes(buf,len)==1)
107136
return true;
108137
return false;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp