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

Commit4203842

Browse files
committed
Use pg_strong_random() to select each server process's random seed.
Previously we just set the seed based on process ID and start timestamp.Both those values are directly available within the session, and canbe found out or guessed by other users too, making the session's seriesof random(3) values fairly predictable. Up to now, our backend-internaluses of random(3) haven't seemed security-critical, but commit88bdbd3added one that potentially is: when using log_statement_sample_rate, auser might be able to predict which of his SQL statements will get logged.To improve this situation, upgrade the per-process seed initializationmethod to use pg_strong_random() if available, greatly reducing thepredictability of the initial seed value. This adds a few tens ofmicroseconds to process start time, but since backend startup time isat least a couple of milliseconds, that seems an acceptable price.This means that pg_strong_random() needs to be able to run withoutreliance on any backend infrastructure, since it will be invokedbefore any of that is up. It was safe for that already, but adjustcomments and #include commands to make it clearer.Discussion:https://postgr.es/m/3859.1545849900@sss.pgh.pa.us
1 parent6645ad6 commit4203842

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

‎src/backend/postmaster/postmaster.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,11 +2520,13 @@ ClosePostmasterPorts(bool am_syslogger)
25202520
/*
25212521
* InitProcessGlobals -- set MyProcPid, MyStartTime[stamp], random seeds
25222522
*
2523-
* Called early in every backend.
2523+
* Called early inthe postmaster andevery backend.
25242524
*/
25252525
void
25262526
InitProcessGlobals(void)
25272527
{
2528+
unsignedintrseed;
2529+
25282530
MyProcPid=getpid();
25292531
MyStartTimestamp=GetCurrentTimestamp();
25302532
MyStartTime=timestamptz_to_time_t(MyStartTimestamp);
@@ -2539,15 +2541,30 @@ InitProcessGlobals(void)
25392541
#endif
25402542

25412543
/*
2542-
* Set a different seed for random() in every backend. Since PIDs and
2543-
* timestamps tend to change more frequently in their least significant
2544-
* bits, shift the timestamp left to allow a larger total number of seeds
2545-
* in a given time period. Since that would leave only 20 bits of the
2546-
* timestamp that cycle every ~1 second, also mix in some higher bits.
2544+
* Set a different seed for random() in every process. We want something
2545+
* unpredictable, so if possible, use high-quality random bits for the
2546+
* seed. Otherwise, fall back to a seed based on timestamp and PID.
2547+
*
2548+
* Note we can't use pg_backend_random here, since this is used in the
2549+
* postmaster, and even in a backend we might not be attached to shared
2550+
* memory yet.
25472551
*/
2548-
srandom(((uint64)MyProcPid) ^
2552+
#ifdefHAVE_STRONG_RANDOM
2553+
if (!pg_strong_random(&rseed,sizeof(rseed)))
2554+
#endif
2555+
{
2556+
/*
2557+
* Since PIDs and timestamps tend to change more frequently in their
2558+
* least significant bits, shift the timestamp left to allow a larger
2559+
* total number of seeds in a given time period. Since that would
2560+
* leave only 20 bits of the timestamp that cycle every ~1 second,
2561+
* also mix in some higher bits.
2562+
*/
2563+
rseed= ((uint64)MyProcPid) ^
25492564
((uint64)MyStartTimestamp <<12) ^
2550-
((uint64)MyStartTimestamp >>20));
2565+
((uint64)MyStartTimestamp >>20);
2566+
}
2567+
srandom(rseed);
25512568
}
25522569

25532570

‎src/port/pg_strong_random.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* Our definition of "strong" is that it's suitable for generating random
77
* salts and query cancellation keys, during authentication.
88
*
9+
* Note: this code is run quite early in postmaster and backend startup;
10+
* therefore, even when built for backend, it cannot rely on backend
11+
* infrastructure such as elog() or palloc().
12+
*
913
* Copyright (c) 1996-2018, PostgreSQL Global Development Group
1014
*
1115
* IDENTIFICATION
@@ -14,11 +18,7 @@
1418
*-------------------------------------------------------------------------
1519
*/
1620

17-
#ifndefFRONTEND
18-
#include"postgres.h"
19-
#else
20-
#include"postgres_fe.h"
21-
#endif
21+
#include"c.h"
2222

2323
#include<fcntl.h>
2424
#include<unistd.h>
@@ -44,7 +44,7 @@ static HCRYPTPROV hProvider = 0;
4444
* Read (random) bytes from a file.
4545
*/
4646
staticbool
47-
random_from_file(char*filename,void*buf,size_tlen)
47+
random_from_file(constchar*filename,void*buf,size_tlen)
4848
{
4949
intf;
5050
char*p=buf;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp