Match pg_user_mappings limits to information_schema.user_mapping_options.
authorNoah Misch
Mon, 8 May 2017 14:24:24 +0000 (07:24 -0700)
committerNoah Misch
Mon, 8 May 2017 14:24:27 +0000 (07:24 -0700)
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

doc/src/sgml/catalogs.sgml
src/backend/catalog/system_views.sql
src/test/regress/expected/foreign_data.out
src/test/regress/expected/rules.out
src/test/regress/sql/foreign_data.sql

index ba2e4463ea2bc123bb4ddfb70d3aab8f9b6bc4e0..23cb262753414704a734e20c057270aadb45f2d3 100644 (file)
@@ -9990,8 +9990,11 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
       
       
        User mapping specific options, as keyword=value
-       strings, if the current user is the owner of the foreign
-       server, else null
+       strings.  This column will show as null unless the current user
+       is the user being mapped, or the mapping is for
+       PUBLIC and the current user is the server
+       owner, or the current user is a superuser.  The intent is
+       to protect password information stored as user mapping option.
       
      
     
index c0bd6fa96b750a07d5a2e970fc4dbb3f93243e85..9f59f9d17ba8e58a17e496e16c98773f78b298ee 100644 (file)
@@ -775,11 +775,11 @@ CREATE VIEW pg_user_mappings AS
         ELSE
             A.rolname
         END AS usename,
-        CASE WHEN pg_has_role(S.srvowner, 'USAGE') OR has_server_privilege(S.oid, 'USAGE') THEN
-            U.umoptions
-        ELSE
-            NULL
-        END AS umoptions
+        CASE WHEN (U.umuser <> 0 AND A.rolname = current_user)
+                    OR (U.umuser = 0 AND pg_has_role(S.srvowner, 'USAGE'))
+                    OR (SELECT rolsuper FROM pg_authid WHERE rolname = current_user)
+                    THEN U.umoptions
+                 ELSE NULL 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);
index 660840cd9033c7e277527ed1ffd50163037f50a1..03d11d908ac5dd46a4eb21b0d8d3937c4285c1cc 100644 (file)
@@ -1194,7 +1194,61 @@ WARNING:  no privileges were granted for "s9"
 CREATE USER MAPPING FOR current_user SERVER s9;
 DROP SERVER s9 CASCADE;                                         -- ERROR
 ERROR:  must be owner of foreign server s9
+-- Check visibility of user mapping data
+SET ROLE regress_test_role;
+CREATE SERVER s10 FOREIGN DATA WRAPPER foo;
+CREATE USER MAPPING FOR public SERVER s10 OPTIONS (user 'secret');
+GRANT USAGE ON FOREIGN SERVER s10 TO unprivileged_role;
+-- owner of server can see option fields
+\deu+
+             List of user mappings
+ Server |     User name     |    FDW Options    
+--------+-------------------+-------------------
+ s10    | public            | ("user" 'secret')
+ s4     | foreign_data_user | 
+ s5     | regress_test_role | (modified '1')
+ s6     | regress_test_role | 
+ s8     | foreign_data_user | 
+ s8     | public            | 
+ s9     | unprivileged_role | 
+ t1     | public            | (modified '1')
+(8 rows)
+
+RESET ROLE;
+-- superuser can see option fields
+\deu+
+              List of user mappings
+ Server |     User name     |     FDW Options     
+--------+-------------------+---------------------
+ s10    | public            | ("user" 'secret')
+ s4     | foreign_data_user | 
+ s5     | regress_test_role | (modified '1')
+ s6     | regress_test_role | 
+ s8     | foreign_data_user | (password 'public')
+ s8     | public            | 
+ s9     | unprivileged_role | 
+ t1     | public            | (modified '1')
+(8 rows)
+
+-- unprivileged user cannot see option fields
+SET ROLE unprivileged_role;
+\deu+
+          List of user mappings
+ Server |     User name     | FDW Options 
+--------+-------------------+-------------
+ s10    | public            | 
+ s4     | foreign_data_user | 
+ s5     | regress_test_role | 
+ s6     | regress_test_role | 
+ s8     | foreign_data_user | 
+ s8     | public            | 
+ s9     | unprivileged_role | 
+ t1     | public            | 
+(8 rows)
+
 RESET ROLE;
+DROP SERVER s10 CASCADE;
+NOTICE:  drop cascades to user mapping for public on server s10
 -- Triggers
 CREATE FUNCTION dummy_trigger() RETURNS TRIGGER AS $$
   BEGIN
index 899ab445dbb25541ca5f63da7d87115074f16d4a..e5bd422a302789f8834607d90357e9458cfe6a63 100644 (file)
@@ -2102,7 +2102,9 @@ pg_user_mappings| SELECT u.oid AS umid,
             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
+            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
index 384e155fcb08c48d9d685f0bf211d5144bb29962..b78ae2d0402dccf890cfa128a01a2f05bf6cbed8 100644 (file)
@@ -490,7 +490,22 @@ ALTER SERVER s9 VERSION '1.2';                                  -- ERROR
 GRANT USAGE ON FOREIGN SERVER s9 TO regress_test_role;          -- WARNING
 CREATE USER MAPPING FOR current_user SERVER s9;
 DROP SERVER s9 CASCADE;                                         -- ERROR
+
+-- Check visibility of user mapping data
+SET ROLE regress_test_role;
+CREATE SERVER s10 FOREIGN DATA WRAPPER foo;
+CREATE USER MAPPING FOR public SERVER s10 OPTIONS (user 'secret');
+GRANT USAGE ON FOREIGN SERVER s10 TO unprivileged_role;
+-- owner of server can see option fields
+\deu+
+RESET ROLE;
+-- superuser can see option fields
+\deu+
+-- unprivileged user cannot see option fields
+SET ROLE unprivileged_role;
+\deu+
 RESET ROLE;
+DROP SERVER s10 CASCADE;
 
 -- Triggers
 CREATE FUNCTION dummy_trigger() RETURNS TRIGGER AS $$