Commit ce9ab889 authored by Tom Lane's avatar Tom Lane

Make REPLICATION privilege checks test current user not authenticated user.

The pg_start_backup() and pg_stop_backup() functions checked the privileges
of the initially-authenticated user rather than the current user, which is
wrong.  For example, a user-defined index function could successfully call
these functions when executed by ANALYZE within autovacuum.  This could
allow an attacker with valid but low-privilege database access to interfere
with creation of routine backups.  Reported and fixed by Noah Misch.

Security: CVE-2013-1901
parent 85079078
...@@ -8367,7 +8367,7 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, ...@@ -8367,7 +8367,7 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
backup_started_in_recovery = RecoveryInProgress(); backup_started_in_recovery = RecoveryInProgress();
if (!superuser() && !is_authenticated_user_replication_role()) if (!superuser() && !has_rolreplication(GetUserId()))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser or replication role to run a backup"))); errmsg("must be superuser or replication role to run a backup")));
...@@ -8705,7 +8705,7 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p) ...@@ -8705,7 +8705,7 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
backup_started_in_recovery = RecoveryInProgress(); backup_started_in_recovery = RecoveryInProgress();
if (!superuser() && !is_authenticated_user_replication_role()) if (!superuser() && !has_rolreplication(GetUserId()))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
(errmsg("must be superuser or replication role to run a backup")))); (errmsg("must be superuser or replication role to run a backup"))));
......
...@@ -390,15 +390,15 @@ SetUserIdAndContext(Oid userid, bool sec_def_context) ...@@ -390,15 +390,15 @@ SetUserIdAndContext(Oid userid, bool sec_def_context)
/* /*
* Check if the authenticated user is a replication role * Check whether specified role has explicit REPLICATION privilege
*/ */
bool bool
is_authenticated_user_replication_role(void) has_rolreplication(Oid roleid)
{ {
bool result = false; bool result = false;
HeapTuple utup; HeapTuple utup;
utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(AuthenticatedUserId)); utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
if (HeapTupleIsValid(utup)) if (HeapTupleIsValid(utup))
{ {
result = ((Form_pg_authid) GETSTRUCT(utup))->rolreplication; result = ((Form_pg_authid) GETSTRUCT(utup))->rolreplication;
......
...@@ -726,7 +726,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, ...@@ -726,7 +726,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
{ {
Assert(!bootstrap); Assert(!bootstrap);
if (!superuser() && !is_authenticated_user_replication_role()) if (!superuser() && !has_rolreplication(GetUserId()))
ereport(FATAL, ereport(FATAL,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser or replication role to start walsender"))); errmsg("must be superuser or replication role to start walsender")));
......
...@@ -439,7 +439,7 @@ extern void ValidatePgVersion(const char *path); ...@@ -439,7 +439,7 @@ extern void ValidatePgVersion(const char *path);
extern void process_shared_preload_libraries(void); extern void process_shared_preload_libraries(void);
extern void process_local_preload_libraries(void); extern void process_local_preload_libraries(void);
extern void pg_bindtextdomain(const char *domain); extern void pg_bindtextdomain(const char *domain);
extern bool is_authenticated_user_replication_role(void); extern bool has_rolreplication(Oid roleid);
/* in access/transam/xlog.c */ /* in access/transam/xlog.c */
extern bool BackupInProgress(void); extern bool BackupInProgress(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment