Skip to content

Commit 99cbb0b

Browse files
committed
Match pg_user_mappings limits to information_schema.user_mapping_options.
Both views replace the umoptions field with NULL when the user does not meet qualifications to see it. They used different qualifications, and pg_user_mappings documented qualifications did not match its implemented qualifications. Make its documentation and implementation match those of user_mapping_options. One might argue for stronger qualifications, but these have long, documented tenure. pg_user_mappings has always exhibited this problem, so back-patch to 9.2 (all supported versions). Michael Paquier and Feike Steenbergen. Reviewed by Jeff Janes. Reported by Andrew Wheelwright. Security: CVE-2017-7486
1 parent 47864be commit 99cbb0b

File tree

5 files changed

+80
-8
lines changed

5 files changed

+80
-8
lines changed

doc/src/sgml/catalogs.sgml

+5-2
Original file line numberDiff line numberDiff line change
@@ -8769,8 +8769,11 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
87698769
<entry></entry>
87708770
<entry>
87718771
User mapping specific options, as <quote>keyword=value</>
8772-
strings, if the current user is the owner of the foreign
8773-
server, else null
8772+
strings. This column will show as null unless the current user
8773+
is the user being mapped, or the mapping is for
8774+
<literal>PUBLIC</literal> and the current user is the server
8775+
owner, or the current user is a superuser. The intent is
8776+
to protect password information stored as user mapping option.
87748777
</entry>
87758778
</row>
87768779
</tbody>

src/backend/catalog/system_views.sql

+5-5
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,11 @@ CREATE VIEW pg_user_mappings AS
669669
ELSE
670670
A.rolname
671671
END AS usename,
672-
CASE WHEN pg_has_role(S.srvowner, 'USAGE') OR has_server_privilege(S.oid, 'USAGE') THEN
673-
U.umoptions
674-
ELSE
675-
NULL
676-
END AS umoptions
672+
CASE WHEN (U.umuser <> 0 AND A.rolname = current_user)
673+
OR (U.umuser = 0 AND pg_has_role(S.srvowner, 'USAGE'))
674+
OR (SELECT rolsuper FROM pg_authid WHERE rolname = current_user)
675+
THEN U.umoptions
676+
ELSE NULL END AS umoptions
677677
FROM pg_user_mapping U
678678
LEFT JOIN pg_authid A ON (A.oid = U.umuser) JOIN
679679
pg_foreign_server S ON (U.umserver = S.oid);

src/test/regress/expected/foreign_data.out

+54
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,61 @@ WARNING: no privileges were granted for "s9"
11591159
CREATE USER MAPPING FOR current_user SERVER s9;
11601160
DROP SERVER s9 CASCADE; -- ERROR
11611161
ERROR: must be owner of foreign server s9
1162+
-- Check visibility of user mapping data
1163+
SET ROLE regress_test_role;
1164+
CREATE SERVER s10 FOREIGN DATA WRAPPER foo;
1165+
CREATE USER MAPPING FOR public SERVER s10 OPTIONS (user 'secret');
1166+
GRANT USAGE ON FOREIGN SERVER s10 TO unprivileged_role;
1167+
-- owner of server can see option fields
1168+
\deu+
1169+
List of user mappings
1170+
Server | User name | FDW Options
1171+
--------+-------------------+-------------------
1172+
s10 | public | ("user" 'secret')
1173+
s4 | foreign_data_user |
1174+
s5 | regress_test_role | (modified '1')
1175+
s6 | regress_test_role |
1176+
s8 | foreign_data_user |
1177+
s8 | public |
1178+
s9 | unprivileged_role |
1179+
t1 | public | (modified '1')
1180+
(8 rows)
1181+
1182+
RESET ROLE;
1183+
-- superuser can see option fields
1184+
\deu+
1185+
List of user mappings
1186+
Server | User name | FDW Options
1187+
--------+-------------------+---------------------
1188+
s10 | public | ("user" 'secret')
1189+
s4 | foreign_data_user |
1190+
s5 | regress_test_role | (modified '1')
1191+
s6 | regress_test_role |
1192+
s8 | foreign_data_user | (password 'public')
1193+
s8 | public |
1194+
s9 | unprivileged_role |
1195+
t1 | public | (modified '1')
1196+
(8 rows)
1197+
1198+
-- unprivileged user cannot see option fields
1199+
SET ROLE unprivileged_role;
1200+
\deu+
1201+
List of user mappings
1202+
Server | User name | FDW Options
1203+
--------+-------------------+-------------
1204+
s10 | public |
1205+
s4 | foreign_data_user |
1206+
s5 | regress_test_role |
1207+
s6 | regress_test_role |
1208+
s8 | foreign_data_user |
1209+
s8 | public |
1210+
s9 | unprivileged_role |
1211+
t1 | public |
1212+
(8 rows)
1213+
11621214
RESET ROLE;
1215+
DROP SERVER s10 CASCADE;
1216+
NOTICE: drop cascades to user mapping for public on server s10
11631217
-- DROP FOREIGN TABLE
11641218
DROP FOREIGN TABLE no_table; -- ERROR
11651219
ERROR: foreign table "no_table" does not exist

src/test/regress/expected/rules.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ SELECT viewname, definition FROM pg_views WHERE schemaname <> 'information_schem
13221322
pg_timezone_abbrevs | SELECT pg_timezone_abbrevs.abbrev, pg_timezone_abbrevs.utc_offset, pg_timezone_abbrevs.is_dst FROM pg_timezone_abbrevs() pg_timezone_abbrevs(abbrev, utc_offset, is_dst);
13231323
pg_timezone_names | SELECT pg_timezone_names.name, pg_timezone_names.abbrev, pg_timezone_names.utc_offset, pg_timezone_names.is_dst FROM pg_timezone_names() pg_timezone_names(name, abbrev, utc_offset, is_dst);
13241324
pg_user | SELECT pg_shadow.usename, pg_shadow.usesysid, pg_shadow.usecreatedb, pg_shadow.usesuper, pg_shadow.usecatupd, pg_shadow.userepl, '********'::text AS passwd, pg_shadow.valuntil, pg_shadow.useconfig FROM pg_shadow;
1325-
pg_user_mappings | SELECT u.oid AS umid, s.oid AS srvid, s.srvname, u.umuser, CASE WHEN (u.umuser = (0)::oid) THEN 'public'::name ELSE a.rolname END AS usename, CASE WHEN (pg_has_role(s.srvowner, 'USAGE'::text) OR has_server_privilege(s.oid, 'USAGE'::text)) THEN u.umoptions ELSE NULL::text[] END AS umoptions FROM ((pg_user_mapping u LEFT JOIN pg_authid a ON ((a.oid = u.umuser))) JOIN pg_foreign_server s ON ((u.umserver = s.oid)));
1325+
pg_user_mappings | SELECT u.oid AS umid, s.oid AS srvid, s.srvname, u.umuser, CASE WHEN (u.umuser = (0)::oid) THEN 'public'::name ELSE a.rolname END AS usename, CASE WHEN ((((u.umuser <> (0)::oid) AND (a.rolname = "current_user"())) OR ((u.umuser = (0)::oid) AND pg_has_role(s.srvowner, 'USAGE'::text))) OR (SELECT pg_authid.rolsuper FROM pg_authid WHERE (pg_authid.rolname = "current_user"()))) THEN u.umoptions ELSE NULL::text[] END AS umoptions FROM ((pg_user_mapping u LEFT JOIN pg_authid a ON ((a.oid = u.umuser))) JOIN pg_foreign_server s ON ((u.umserver = s.oid)));
13261326
pg_views | SELECT n.nspname AS schemaname, c.relname AS viewname, pg_get_userbyid(c.relowner) AS viewowner, pg_get_viewdef(c.oid) AS definition FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char");
13271327
rtest_v1 | SELECT rtest_t1.a, rtest_t1.b FROM rtest_t1;
13281328
rtest_vcomp | SELECT x.part, (x.size * y.factor) AS size_in_cm FROM rtest_comp x, rtest_unitfact y WHERE (x.unit = y.unit);

src/test/regress/sql/foreign_data.sql

+15
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,22 @@ ALTER SERVER s9 VERSION '1.2'; -- ERROR
469469
GRANT USAGE ON FOREIGN SERVER s9 TO regress_test_role; -- WARNING
470470
CREATE USER MAPPING FOR current_user SERVER s9;
471471
DROP SERVER s9 CASCADE; -- ERROR
472+
473+
-- Check visibility of user mapping data
474+
SET ROLE regress_test_role;
475+
CREATE SERVER s10 FOREIGN DATA WRAPPER foo;
476+
CREATE USER MAPPING FOR public SERVER s10 OPTIONS (user 'secret');
477+
GRANT USAGE ON FOREIGN SERVER s10 TO unprivileged_role;
478+
-- owner of server can see option fields
479+
\deu+
480+
RESET ROLE;
481+
-- superuser can see option fields
482+
\deu+
483+
-- unprivileged user cannot see option fields
484+
SET ROLE unprivileged_role;
485+
\deu+
472486
RESET ROLE;
487+
DROP SERVER s10 CASCADE;
473488

474489
-- DROP FOREIGN TABLE
475490
DROP FOREIGN TABLE no_table; -- ERROR

0 commit comments

Comments
 (0)