Skip to content

Commit 7b88529

Browse files
committed
Fix per-session activation of ALTER {ROLE|DATABASE} SET role.
After commit 5a2fed9, the catalog state resulting from these commands ceased to affect sessions. Restore the longstanding behavior, which is like beginning the session with a SET ROLE command. If cherry-picking the CVE-2024-10978 fixes, default to including this, too. (This fixes an unintended side effect of fixing CVE-2024-10978.) Back-patch to v12, like that commit. The release team decided to include v12, despite the original intent to halt v12 commits earlier this week. Tom Lane and Noah Misch. Reported by Etienne LAFARGE. Discussion: https://postgr.es/m/CADOZwSb0UsEr4_UTFXC5k7=fyyK8uKXekucd+-uuGjJsGBfxgw@mail.gmail.com
1 parent e5ed873 commit 7b88529

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

src/backend/utils/init/miscinit.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,25 @@ InitializeSessionUserId(const char *rolename, Oid roleid, bool bypass_login_chec
824824
{
825825
SetAuthenticatedUserId(roleid);
826826

827-
/* Set SessionUserId and related variables via the GUC mechanisms */
827+
/*
828+
* Set SessionUserId and related variables, including "role", via the
829+
* GUC mechanisms.
830+
*
831+
* Note: ideally we would use PGC_S_DYNAMIC_DEFAULT here, so that
832+
* session_authorization could subsequently be changed from
833+
* pg_db_role_setting entries. Instead, session_authorization in
834+
* pg_db_role_setting has no effect. Changing that would require
835+
* solving two problems:
836+
*
837+
* 1. If pg_db_role_setting has values for both session_authorization
838+
* and role, we could not be sure which order those would be applied
839+
* in, and it would matter.
840+
*
841+
* 2. Sites may have years-old session_authorization entries. There's
842+
* not been any particular reason to remove them. Ending the dormancy
843+
* of those entries could seriously change application behavior, so
844+
* only a major release should do that.
845+
*/
828846
SetConfigOption("session_authorization", rname,
829847
PGC_BACKEND, PGC_S_OVERRIDE);
830848
}

src/backend/utils/misc/guc.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -4099,6 +4099,12 @@ set_config_with_handle(const char *name, config_handle *handle,
40994099
* expect that if "role" isn't supposed to be default, it
41004100
* has been or will be set by a separate reload action.
41014101
*
4102+
* Also, for the call from InitializeSessionUserId with
4103+
* source == PGC_S_OVERRIDE, use PGC_S_DYNAMIC_DEFAULT for
4104+
* "role"'s source, so that it's still possible to set
4105+
* "role" from pg_db_role_setting entries. (See notes in
4106+
* InitializeSessionUserId before changing this.)
4107+
*
41024108
* A fine point: for RESET session_authorization, we do
41034109
* "RESET role" not "SET ROLE NONE" (by passing down NULL
41044110
* rather than "none" for the value). This would have the
@@ -4111,7 +4117,9 @@ set_config_with_handle(const char *name, config_handle *handle,
41114117
(void) set_config_with_handle("role", NULL,
41124118
value ? "none" : NULL,
41134119
orig_context,
4114-
orig_source,
4120+
(orig_source == PGC_S_OVERRIDE)
4121+
? PGC_S_DYNAMIC_DEFAULT
4122+
: orig_source,
41154123
orig_srole,
41164124
action,
41174125
true,

src/test/modules/unsafe_tests/Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# src/test/modules/unsafe_tests/Makefile
22

3-
REGRESS = rolenames alter_system_table guc_privs
3+
REGRESS = rolenames setconfig alter_system_table guc_privs
4+
REGRESS_OPTS = \
5+
--create-role=regress_authenticated_user_sr \
6+
--create-role=regress_authenticated_user_ssa
47

58
# the whole point of these tests is to not run installcheck
69
NO_INSTALLCHECK = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- This is borderline unsafe in that an additional login-capable user exists
2+
-- during the test run. Under installcheck, a too-permissive pg_hba.conf
3+
-- might allow unwanted logins as regress_authenticated_user_ssa.
4+
ALTER USER regress_authenticated_user_ssa superuser;
5+
CREATE ROLE regress_session_user;
6+
CREATE ROLE regress_current_user;
7+
GRANT regress_current_user TO regress_authenticated_user_sr;
8+
GRANT regress_session_user TO regress_authenticated_user_ssa;
9+
ALTER ROLE regress_authenticated_user_ssa
10+
SET session_authorization = regress_session_user;
11+
ALTER ROLE regress_authenticated_user_sr SET ROLE = regress_current_user;
12+
\c - regress_authenticated_user_sr
13+
SELECT current_user, session_user;
14+
current_user | session_user
15+
----------------------+-------------------------------
16+
regress_current_user | regress_authenticated_user_sr
17+
(1 row)
18+
19+
-- The longstanding historical behavior is that session_authorization in
20+
-- setconfig has no effect. Hence, session_user remains
21+
-- regress_authenticated_user_ssa. See comment in InitializeSessionUserId().
22+
\c - regress_authenticated_user_ssa
23+
SELECT current_user, session_user;
24+
current_user | session_user
25+
--------------------------------+--------------------------------
26+
regress_authenticated_user_ssa | regress_authenticated_user_ssa
27+
(1 row)
28+
29+
RESET SESSION AUTHORIZATION;
30+
DROP USER regress_session_user;
31+
DROP USER regress_current_user;

src/test/modules/unsafe_tests/meson.build

+3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ tests += {
77
'regress': {
88
'sql': [
99
'rolenames',
10+
'setconfig',
1011
'alter_system_table',
1112
'guc_privs',
1213
],
14+
'regress_args': ['--create-role=regress_authenticated_user_sr',
15+
'--create-role=regress_authenticated_user_ssa'],
1316
'runningcheck': false,
1417
},
1518
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- This is borderline unsafe in that an additional login-capable user exists
2+
-- during the test run. Under installcheck, a too-permissive pg_hba.conf
3+
-- might allow unwanted logins as regress_authenticated_user_ssa.
4+
5+
ALTER USER regress_authenticated_user_ssa superuser;
6+
CREATE ROLE regress_session_user;
7+
CREATE ROLE regress_current_user;
8+
GRANT regress_current_user TO regress_authenticated_user_sr;
9+
GRANT regress_session_user TO regress_authenticated_user_ssa;
10+
ALTER ROLE regress_authenticated_user_ssa
11+
SET session_authorization = regress_session_user;
12+
ALTER ROLE regress_authenticated_user_sr SET ROLE = regress_current_user;
13+
14+
\c - regress_authenticated_user_sr
15+
SELECT current_user, session_user;
16+
17+
-- The longstanding historical behavior is that session_authorization in
18+
-- setconfig has no effect. Hence, session_user remains
19+
-- regress_authenticated_user_ssa. See comment in InitializeSessionUserId().
20+
\c - regress_authenticated_user_ssa
21+
SELECT current_user, session_user;
22+
RESET SESSION AUTHORIZATION;
23+
DROP USER regress_session_user;
24+
DROP USER regress_current_user;

0 commit comments

Comments
 (0)