Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5770172

Browse files
committed
Document security implications of search_path and the public schema.
The ability to create like-named objects in different schemas opens up the potential for users to change the behavior of other users' queries, maliciously or accidentally. When you connect to a PostgreSQL server, you should remove from your search_path any schema for which a user other than yourself or superusers holds the CREATE privilege. If you do not, other users holding CREATE privilege can redefine the behavior of your commands, causing them to perform arbitrary SQL statements under your identity. "SET search_path = ..." and "SELECT pg_catalog.set_config(...)" are not vulnerable to such hijacking, so one can use either as the first command of a session. As special exceptions, the following client applications behave as documented regardless of search_path settings and schema privileges: clusterdb createdb createlang createuser dropdb droplang dropuser ecpg (not programs it generates) initdb oid2name pg_archivecleanup pg_basebackup pg_config pg_controldata pg_ctl pg_dump pg_dumpall pg_isready pg_receivewal pg_recvlogical pg_resetwal pg_restore pg_rewind pg_standby pg_test_fsync pg_test_timing pg_upgrade pg_waldump reindexdb vacuumdb vacuumlo. Not included are core client programs that run user-specified SQL commands, namely psql and pgbench. PostgreSQL encourages non-core client applications to do likewise. Document this in the context of libpq connections, psql connections, dblink connections, ECPG connections, extension packaging, and schema usage patterns. The principal defense for applications is "SELECT pg_catalog.set_config('search_path', '', false)", and the principal defense for databases is "REVOKE CREATE ON SCHEMA public FROM PUBLIC". Either one is sufficient to prevent attack. After a REVOKE, consider auditing the public schema for objects named like pg_catalog objects. Authors of SECURITY DEFINER functions use some of the same defenses, and the CREATE FUNCTION reference page already covered them thoroughly. This is a good opportunity to audit SECURITY DEFINER functions for robust security practice. Back-patch to 9.3 (all supported versions). Reviewed by Michael Paquier and Jonathan S. Katz. Reported by Arseniy Sharoglazov. Security: CVE-2018-1058
1 parent 582edc3 commit 5770172

19 files changed

+369
-100
lines changed

doc/src/sgml/config.sgml

+7-3
Original file line numberDiff line numberDiff line change
@@ -6329,6 +6329,13 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
63296329
setting, either globally or per-user.
63306330
</para>
63316331

6332+
<para>
6333+
For more information on schema handling, see
6334+
<xref linkend="ddl-schemas"/>. In particular, the default
6335+
configuration is suitable only when the database has a single user or
6336+
a few mutually-trusting users.
6337+
</para>
6338+
63326339
<para>
63336340
The current effective value of the search path can be examined
63346341
via the <acronym>SQL</acronym> function
@@ -6340,9 +6347,6 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
63406347
appearing in <varname>search_path</varname> were resolved.
63416348
</para>
63426349

6343-
<para>
6344-
For more information on schema handling, see <xref linkend="ddl-schemas"/>.
6345-
</para>
63466350
</listitem>
63476351
</varlistentry>
63486352

doc/src/sgml/contrib.sgml

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ CREATE EXTENSION <replaceable>module_name</replaceable>;
7575
choice. To do that, add <literal>SCHEMA
7676
<replaceable>schema_name</replaceable></literal> to the <command>CREATE EXTENSION</command>
7777
command. By default, the objects will be placed in your current creation
78-
target schema, typically <literal>public</literal>.
78+
target schema, which in turn defaults to <literal>public</literal>.
7979
</para>
8080

8181
<para>

doc/src/sgml/dblink.sgml

+25-11
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ dblink_connect(text connname, text connstr) returns text
8383
<listitem>
8484
<para><application>libpq</application>-style connection info string, for example
8585
<literal>hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres
86-
password=mypasswd</literal>.
86+
password=mypasswd options=-csearch_path=</literal>.
8787
For details see <xref linkend="libpq-connstring"/>.
8888
Alternatively, the name of a foreign server.
8989
</para>
@@ -104,6 +104,17 @@ dblink_connect(text connname, text connstr) returns text
104104
<refsect1>
105105
<title>Notes</title>
106106

107+
<para>
108+
If untrusted users have access to a database that has not adopted a
109+
<link linkend="ddl-schemas-patterns">secure schema usage pattern</link>,
110+
begin each session by removing publicly-writable schemas from
111+
<varname>search_path</varname>. One could, for example,
112+
add <literal>options=-csearch_path=</literal> to
113+
<parameter>connstr</parameter>. This consideration is not specific
114+
to <filename>dblink</filename>; it applies to every interface for
115+
executing arbitrary SQL commands.
116+
</para>
117+
107118
<para>
108119
Only superusers may use <function>dblink_connect</function> to create
109120
non-password-authenticated connections. If non-superusers need this
@@ -121,13 +132,13 @@ dblink_connect(text connname, text connstr) returns text
121132
<title>Examples</title>
122133

123134
<screen>
124-
SELECT dblink_connect('dbname=postgres');
135+
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
125136
dblink_connect
126137
----------------
127138
OK
128139
(1 row)
129140

130-
SELECT dblink_connect('myconn', 'dbname=postgres');
141+
SELECT dblink_connect('myconn', 'dbname=postgres options=-csearch_path=');
131142
dblink_connect
132143
----------------
133144
OK
@@ -416,7 +427,8 @@ dblink(text sql [, bool fail_on_error]) returns setof record
416427

417428
<programlisting>
418429
SELECT *
419-
FROM dblink('dbname=mydb', 'select proname, prosrc from pg_proc')
430+
FROM dblink('dbname=mydb options=-csearch_path=',
431+
'select proname, prosrc from pg_proc')
420432
AS t1(proname name, prosrc text)
421433
WHERE proname LIKE 'bytea%';
422434
</programlisting>
@@ -450,7 +462,8 @@ SELECT *
450462
<programlisting>
451463
CREATE VIEW myremote_pg_proc AS
452464
SELECT *
453-
FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
465+
FROM dblink('dbname=postgres options=-csearch_path=',
466+
'select proname, prosrc from pg_proc')
454467
AS t1(proname name, prosrc text);
455468

456469
SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';
@@ -461,7 +474,8 @@ SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';
461474
<title>Examples</title>
462475

463476
<screen>
464-
SELECT * FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
477+
SELECT * FROM dblink('dbname=postgres options=-csearch_path=',
478+
'select proname, prosrc from pg_proc')
465479
AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
466480
proname | prosrc
467481
------------+------------
@@ -479,7 +493,7 @@ SELECT * FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
479493
byteaout | byteaout
480494
(12 rows)
481495

482-
SELECT dblink_connect('dbname=postgres');
496+
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
483497
dblink_connect
484498
----------------
485499
OK
@@ -503,7 +517,7 @@ SELECT * FROM dblink('select proname, prosrc from pg_proc')
503517
byteaout | byteaout
504518
(12 rows)
505519

506-
SELECT dblink_connect('myconn', 'dbname=regression');
520+
SELECT dblink_connect('myconn', 'dbname=regression options=-csearch_path=');
507521
dblink_connect
508522
----------------
509523
OK
@@ -778,7 +792,7 @@ dblink_open(text connname, text cursorname, text sql [, bool fail_on_error]) ret
778792
<title>Examples</title>
779793

780794
<screen>
781-
SELECT dblink_connect('dbname=postgres');
795+
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
782796
dblink_connect
783797
----------------
784798
OK
@@ -899,7 +913,7 @@ dblink_fetch(text connname, text cursorname, int howmany [, bool fail_on_error])
899913
<title>Examples</title>
900914

901915
<screen>
902-
SELECT dblink_connect('dbname=postgres');
916+
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
903917
dblink_connect
904918
----------------
905919
OK
@@ -1036,7 +1050,7 @@ dblink_close(text connname, text cursorname [, bool fail_on_error]) returns text
10361050
<title>Examples</title>
10371051

10381052
<screen>
1039-
SELECT dblink_connect('dbname=postgres');
1053+
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
10401054
dblink_connect
10411055
----------------
10421056
OK

doc/src/sgml/ddl.sgml

+67-27
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,20 @@ CREATE TABLE public.products ( ... );
21722172
in other schemas in the database.
21732173
</para>
21742174

2175+
<para>
2176+
The ability to create like-named objects in different schemas complicates
2177+
writing a query that references precisely the same objects every time. It
2178+
also opens up the potential for users to change the behavior of other
2179+
users' queries, maliciously or accidentally. Due to the prevalence of
2180+
unqualified names in queries and their use
2181+
in <productname>PostgreSQL</productname> internals, adding a schema
2182+
to <varname>search_path</varname> effectively trusts all users having
2183+
<literal>CREATE</literal> privilege on that schema. When you run an
2184+
ordinary query, a malicious user able to create objects in a schema of
2185+
your search path can take control and execute arbitrary SQL functions as
2186+
though you executed them.
2187+
</para>
2188+
21752189
<indexterm>
21762190
<primary>schema</primary>
21772191
<secondary>current</secondary>
@@ -2288,8 +2302,9 @@ SELECT 3 OPERATOR(pg_catalog.+) 4;
22882302
the schema
22892303
<literal>public</literal>. This allows all users that are able to
22902304
connect to a given database to create objects in its
2291-
<literal>public</literal> schema. If you do
2292-
not want to allow that, you can revoke that privilege:
2305+
<literal>public</literal> schema.
2306+
Some <link linkend="ddl-schemas-patterns">usage patterns</link> call for
2307+
revoking that privilege:
22932308
<programlisting>
22942309
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
22952310
</programlisting>
@@ -2339,50 +2354,75 @@ REVOKE CREATE ON SCHEMA public FROM PUBLIC;
23392354
<title>Usage Patterns</title>
23402355

23412356
<para>
2342-
Schemas can be used to organize your data in many ways. There are
2343-
a few usage patterns that are recommended and are easily supported by
2344-
the default configuration:
2357+
Schemas can be used to organize your data in many ways. There are a few
2358+
usage patterns easily supported by the default configuration, only one of
2359+
which suffices when database users mistrust other database users:
23452360
<itemizedlist>
23462361
<listitem>
2362+
<!-- "DROP SCHEMA public" is inferior to this REVOKE, because pg_dump
2363+
doesn't preserve that DROP. -->
23472364
<para>
2348-
If you do not create any schemas then all users access the
2349-
public schema implicitly. This simulates the situation where
2350-
schemas are not available at all. This setup is mainly
2351-
recommended when there is only a single user or a few cooperating
2352-
users in a database. This setup also allows smooth transition
2353-
from the non-schema-aware world.
2365+
Constrain ordinary users to user-private schemas. To implement this,
2366+
issue <literal>REVOKE CREATE ON SCHEMA public FROM PUBLIC</literal>,
2367+
and create a schema for each user with the same name as that user. If
2368+
affected users had logged in before this, consider auditing the public
2369+
schema for objects named like objects in
2370+
schema <literal>pg_catalog</literal>. Recall that the default search
2371+
path starts with <literal>$user</literal>, which resolves to the user
2372+
name. Therefore, if each user has a separate schema, they access their
2373+
own schemas by default.
23542374
</para>
23552375
</listitem>
23562376

23572377
<listitem>
23582378
<para>
2359-
You can create a schema for each user with the same name as
2360-
that user. Recall that the default search path starts with
2361-
<literal>$user</literal>, which resolves to the user name.
2362-
Therefore, if each user has a separate schema, they access their
2363-
own schemas by default.
2379+
Remove the public schema from each user's default search path
2380+
using <literal>ALTER ROLE <replaceable>user</replaceable> SET
2381+
search_path = "$user"</literal>. Everyone retains the ability to
2382+
create objects in the public schema, but only qualified names will
2383+
choose those objects. A user holding the <literal>CREATEROLE</literal>
2384+
privilege can undo this setting and issue arbitrary queries under the
2385+
identity of users relying on the setting. If you
2386+
grant <literal>CREATEROLE</literal> to users not warranting this
2387+
almost-superuser ability, use the first pattern instead.
23642388
</para>
2389+
</listitem>
23652390

2391+
<listitem>
23662392
<para>
2367-
If you use this setup then you might also want to revoke access
2368-
to the public schema (or drop it altogether), so users are
2369-
truly constrained to their own schemas.
2393+
Remove the public schema from <varname>search_path</varname> in
2394+
<link linkend="config-setting-configuration-file"><filename>postgresql.conf</filename></link>.
2395+
The ensuing user experience matches the previous pattern. In addition
2396+
to that pattern's implications for <literal>CREATEROLE</literal>, this
2397+
trusts database owners the same way. If you assign
2398+
the <literal>CREATEROLE</literal>
2399+
privilege, <literal>CREATEDB</literal> privilege or individual database
2400+
ownership to users not warranting almost-superuser access, use the
2401+
first pattern instead.
23702402
</para>
23712403
</listitem>
23722404

23732405
<listitem>
23742406
<para>
2375-
To install shared applications (tables to be used by everyone,
2376-
additional functions provided by third parties, etc.), put them
2377-
into separate schemas. Remember to grant appropriate
2378-
privileges to allow the other users to access them. Users can
2379-
then refer to these additional objects by qualifying the names
2380-
with a schema name, or they can put the additional schemas into
2381-
their search path, as they choose.
2407+
Keep the default. All users access the public schema implicitly. This
2408+
simulates the situation where schemas are not available at all, giving
2409+
a smooth transition from the non-schema-aware world. However, any user
2410+
can issue arbitrary queries under the identity of any user not electing
2411+
to protect itself individually. This pattern is acceptable only when
2412+
the database has a single user or a few mutually-trusting users.
23822413
</para>
23832414
</listitem>
23842415
</itemizedlist>
23852416
</para>
2417+
2418+
<para>
2419+
For any pattern, to install shared applications (tables to be used by
2420+
everyone, additional functions provided by third parties, etc.), put them
2421+
into separate schemas. Remember to grant appropriate privileges to allow
2422+
the other users to access them. Users can then refer to these additional
2423+
objects by qualifying the names with a schema name, or they can put the
2424+
additional schemas into their search path, as they choose.
2425+
</para>
23862426
</sect2>
23872427

23882428
<sect2 id="ddl-schemas-portability">
@@ -2405,7 +2445,7 @@ REVOKE CREATE ON SCHEMA public FROM PUBLIC;
24052445
<para>
24062446
Also, there is no concept of a <literal>public</literal> schema in the
24072447
SQL standard. For maximum conformance to the standard, you should
2408-
not use (perhaps even remove) the <literal>public</literal> schema.
2448+
not use the <literal>public</literal> schema.
24092449
</para>
24102450

24112451
<para>

0 commit comments

Comments
 (0)