Skip to content

Commit 61d4d14

Browse files
committed
Move permissions check from do_pg_start_backup to pg_start_backup
And the same for do_pg_stop_backup. The code in do_pg_* is not allowed to access the catalogs. For manual base backups, the permissions check can be handled in the calling function, and for streaming base backups only users with the required permissions can get past the authentication step in the first place. Reported by Antonin Houska, diagnosed by Andres Freund
1 parent 2edf3e8 commit 61d4d14

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/backend/access/transam/xlog.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9412,6 +9412,9 @@ issue_xlog_fsync(int fd, uint32 log, uint32 seg)
94129412
*
94139413
* Every successfully started non-exclusive backup must be stopped by calling
94149414
* do_pg_stop_backup() or do_pg_abort_backup().
9415+
*
9416+
* It is the responsibility of the caller of this function to verify the
9417+
* permissions of the calling user!
94159418
*/
94169419
XLogRecPtr
94179420
do_pg_start_backup(const char *backupidstr, bool fast, char **labelfile)
@@ -9431,11 +9434,6 @@ do_pg_start_backup(const char *backupidstr, bool fast, char **labelfile)
94319434

94329435
backup_started_in_recovery = RecoveryInProgress();
94339436

9434-
if (!superuser() && !has_rolreplication(GetUserId()))
9435-
ereport(ERROR,
9436-
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
9437-
errmsg("must be superuser or replication role to run a backup")));
9438-
94399437
/*
94409438
* Currently only non-exclusive backup can be taken during recovery.
94419439
*/
@@ -9731,6 +9729,9 @@ pg_start_backup_callback(int code, Datum arg)
97319729
97329730
* If labelfile is NULL, this stops an exclusive backup. Otherwise this stops
97339731
* the non-exclusive backup specified by 'labelfile'.
9732+
*
9733+
* It is the responsibility of the caller of this function to verify the
9734+
* permissions of the calling user!
97349735
*/
97359736
XLogRecPtr
97369737
do_pg_stop_backup(char *labelfile, bool waitforarchive)
@@ -9761,11 +9762,6 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive)
97619762

97629763
backup_started_in_recovery = RecoveryInProgress();
97639764

9764-
if (!superuser() && !has_rolreplication(GetUserId()))
9765-
ereport(ERROR,
9766-
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
9767-
(errmsg("must be superuser or replication role to run a backup"))));
9768-
97699765
/*
97709766
* Currently only non-exclusive backup can be taken during recovery.
97719767
*/

src/backend/access/transam/xlogfuncs.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ pg_start_backup(PG_FUNCTION_ARGS)
5454

5555
backupidstr = text_to_cstring(backupid);
5656

57+
if (!superuser() && !has_rolreplication(GetUserId()))
58+
ereport(ERROR,
59+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
60+
errmsg("must be superuser or replication role to run a backup")));
61+
5762
startpoint = do_pg_start_backup(backupidstr, fast, NULL);
5863

5964
snprintf(startxlogstr, sizeof(startxlogstr), "%X/%X",
@@ -80,6 +85,11 @@ pg_stop_backup(PG_FUNCTION_ARGS)
8085
XLogRecPtr stoppoint;
8186
char stopxlogstr[MAXFNAMELEN];
8287

88+
if (!superuser() && !has_rolreplication(GetUserId()))
89+
ereport(ERROR,
90+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
91+
(errmsg("must be superuser or replication role to run a backup"))));
92+
8393
stoppoint = do_pg_stop_backup(NULL, true);
8494

8595
snprintf(stopxlogstr, sizeof(stopxlogstr), "%X/%X",

0 commit comments

Comments
 (0)