Skip to content

Commit f1bf970

Browse files
nathan-bossartmyrrc
authored andcommitted
Restrict psql meta-commands in plain-text dumps.
A malicious server could inject psql meta-commands into plain-text dump output (i.e., scripts created with pg_dump --format=plain, pg_dumpall, or pg_restore --file) that are run at restore time on the machine running psql. To fix, introduce a new "restricted" mode in psql that blocks all meta-commands (except for \unrestrict to exit the mode), and teach pg_dump, pg_dumpall, and pg_restore to use this mode in plain-text dumps. While at it, encourage users to only restore dumps generated from trusted servers or to inspect it beforehand, since restoring causes the destination to execute arbitrary code of the source superusers' choice. However, the client running the dump and restore needn't trust the source or destination superusers. Reported-by: Martin Rakhmanov Reported-by: Matthieu Denais <litezeraw@gmail.com> Reported-by: RyotaK <ryotak.mail@gmail.com> Suggested-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Noah Misch <noah@leadboat.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Security: CVE-2025-8714 Backpatch-through: 13
1 parent a3caafd commit f1bf970

File tree

22 files changed

+433
-13
lines changed

22 files changed

+433
-13
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ PostgreSQL documentation
9292
light of the limitations listed below.
9393
</para>
9494

95+
<warning>
96+
<para>
97+
Restoring a dump causes the destination to execute arbitrary code of the
98+
source superusers' choice. Partial dumps and partial restores do not limit
99+
that. If the source superusers are not trusted, the dumped SQL statements
100+
must be inspected before restoring. Non-plain-text dumps can be inspected
101+
by using <application>pg_restore</application>'s <option>--file</option>
102+
option. Note that the client running the dump and restore need not trust
103+
the source or destination superusers.
104+
</para>
105+
</warning>
106+
95107
</refsect1>
96108

97109
<refsect1 id="pg-dump-options">
@@ -1216,6 +1228,29 @@ PostgreSQL documentation
12161228
</listitem>
12171229
</varlistentry>
12181230

1231+
<varlistentry>
1232+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
1233+
<listitem>
1234+
<para>
1235+
Use the provided string as the <application>psql</application>
1236+
<command>\restrict</command> key in the dump output. This can only be
1237+
specified for plain-text dumps, i.e., when <option>--format</option> is
1238+
set to <literal>plain</literal> or the <option>--format</option> option
1239+
is omitted. If no restrict key is specified,
1240+
<application>pg_dump</application> will generate a random one as
1241+
needed. Keys may contain only alphanumeric characters.
1242+
</para>
1243+
<para>
1244+
This option is primarily intended for testing purposes and other
1245+
scenarios that require repeatable output (e.g., comparing dump files).
1246+
It is not recommended for general use, as a malicious server with
1247+
advance knowledge of the key may be able to inject arbitrary code that
1248+
will be executed on the machine that runs
1249+
<application>psql</application> with the dump output.
1250+
</para>
1251+
</listitem>
1252+
</varlistentry>
1253+
12191254
<varlistentry>
12201255
<term><option>--rows-per-insert=<replaceable class="parameter">nrows</replaceable></option></term>
12211256
<listitem>

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ PostgreSQL documentation
6666
linkend="libpq-pgpass"/> for more information.
6767
</para>
6868

69+
<warning>
70+
<para>
71+
Restoring a dump causes the destination to execute arbitrary code of the
72+
source superusers' choice. Partial dumps and partial restores do not limit
73+
that. If the source superusers are not trusted, the dumped SQL statements
74+
must be inspected before restoring. Note that the client running the dump
75+
and restore need not trust the source or destination superusers.
76+
</para>
77+
</warning>
78+
6979
</refsect1>
7080

7181
<refsect1>
@@ -564,6 +574,26 @@ exclude database <replaceable class="parameter">PATTERN</replaceable>
564574
</listitem>
565575
</varlistentry>
566576

577+
<varlistentry>
578+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
579+
<listitem>
580+
<para>
581+
Use the provided string as the <application>psql</application>
582+
<command>\restrict</command> key in the dump output. If no restrict
583+
key is specified, <application>pg_dumpall</application> will generate a
584+
random one as needed. Keys may contain only alphanumeric characters.
585+
</para>
586+
<para>
587+
This option is primarily intended for testing purposes and other
588+
scenarios that require repeatable output (e.g., comparing dump files).
589+
It is not recommended for general use, as a malicious server with
590+
advance knowledge of the key may be able to inject arbitrary code that
591+
will be executed on the machine that runs
592+
<application>psql</application> with the dump output.
593+
</para>
594+
</listitem>
595+
</varlistentry>
596+
567597
<varlistentry>
568598
<term><option>--rows-per-insert=<replaceable class="parameter">nrows</replaceable></option></term>
569599
<listitem>

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ PostgreSQL documentation
6868
<application>pg_restore</application> will not be able to load the data
6969
using <command>COPY</command> statements.
7070
</para>
71+
72+
<warning>
73+
<para>
74+
Restoring a dump causes the destination to execute arbitrary code of the
75+
source superusers' choice. Partial dumps and partial restores do not limit
76+
that. If the source superusers are not trusted, the dumped SQL statements
77+
must be inspected before restoring. Non-plain-text dumps can be inspected
78+
by using <application>pg_restore</application>'s <option>--file</option>
79+
option. Note that the client running the dump and restore need not trust
80+
the source or destination superusers.
81+
</para>
82+
</warning>
7183
</refsect1>
7284

7385
<refsect1 id="app-pgrestore-options">
@@ -765,6 +777,28 @@ PostgreSQL documentation
765777
</listitem>
766778
</varlistentry>
767779

780+
<varlistentry>
781+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
782+
<listitem>
783+
<para>
784+
Use the provided string as the <application>psql</application>
785+
<command>\restrict</command> key in the dump output. This can only be
786+
specified for SQL script output, i.e., when the <option>--file</option>
787+
option is used. If no restrict key is specified,
788+
<application>pg_restore</application> will generate a random one as
789+
needed. Keys may contain only alphanumeric characters.
790+
</para>
791+
<para>
792+
This option is primarily intended for testing purposes and other
793+
scenarios that require repeatable output (e.g., comparing dump files).
794+
It is not recommended for general use, as a malicious server with
795+
advance knowledge of the key may be able to inject arbitrary code that
796+
will be executed on the machine that runs
797+
<application>psql</application> with the dump output.
798+
</para>
799+
</listitem>
800+
</varlistentry>
801+
768802
<varlistentry>
769803
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
770804
<listitem>

doc/src/sgml/ref/pgupgrade.sgml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ PostgreSQL documentation
7070
pg_upgrade supports upgrades from 9.2.X and later to the current
7171
major release of <productname>PostgreSQL</productname>, including snapshot and beta releases.
7272
</para>
73+
74+
<warning>
75+
<para>
76+
Upgrading a cluster causes the destination to execute arbitrary code of the
77+
source superusers' choice. Ensure that the source superusers are trusted
78+
before upgrading.
79+
</para>
80+
</warning>
7381
</refsect1>
7482

7583
<refsect1>

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,6 +3340,24 @@ lo_import 152801
33403340
</varlistentry>
33413341

33423342

3343+
<varlistentry id="app-psql-meta-command-restrict">
3344+
<term><literal>\restrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3345+
<listitem>
3346+
<para>
3347+
Enter "restricted" mode with the provided key. In this mode, the only
3348+
allowed meta-command is <command>\unrestrict</command>, to exit
3349+
restricted mode. The key may contain only alphanumeric characters.
3350+
</para>
3351+
<para>
3352+
This command is primarily intended for use in plain-text dumps
3353+
generated by <application>pg_dump</application>,
3354+
<application>pg_dumpall</application>, and
3355+
<application>pg_restore</application>, but it may be useful elsewhere.
3356+
</para>
3357+
</listitem>
3358+
</varlistentry>
3359+
3360+
33433361
<varlistentry id="app-psql-meta-command-s">
33443362
<term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
33453363
<listitem>
@@ -3514,6 +3532,24 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
35143532
</varlistentry>
35153533

35163534

3535+
<varlistentry id="app-psql-meta-command-unrestrict">
3536+
<term><literal>\unrestrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3537+
<listitem>
3538+
<para>
3539+
Exit "restricted" mode (i.e., where all other meta-commands are
3540+
blocked), provided the specified key matches the one given to
3541+
<command>\restrict</command> when restricted mode was entered.
3542+
</para>
3543+
<para>
3544+
This command is primarily intended for use in plain-text dumps
3545+
generated by <application>pg_dump</application>,
3546+
<application>pg_dumpall</application>, and
3547+
<application>pg_restore</application>, but it may be useful elsewhere.
3548+
</para>
3549+
</listitem>
3550+
</varlistentry>
3551+
3552+
35173553
<varlistentry id="app-psql-meta-command-unset">
35183554
<term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
35193555

src/bin/pg_combinebackup/t/002_compare_backups.pl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
[
172172
'pg_dumpall', '-f',
173173
$dump1, '--no-sync',
174+
'--restrict-key=test',
174175
'--no-unlogged-table-data', '-d',
175176
$pitr1->connstr('postgres'),
176177
],
@@ -179,6 +180,7 @@
179180
[
180181
'pg_dumpall', '-f',
181182
$dump2, '--no-sync',
183+
'--restrict-key=test',
182184
'--no-unlogged-table-data', '-d',
183185
$pitr2->connstr('postgres'),
184186
],

src/bin/pg_dump/dumputils.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "dumputils.h"
2020
#include "fe_utils/string_utils.h"
2121

22+
static const char restrict_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
2223

2324
static bool parseAclItem(const char *item, const char *type,
2425
const char *name, const char *subname, int remoteVersion,
@@ -920,3 +921,40 @@ makeAlterConfigCommand(PGconn *conn, const char *configitem,
920921

921922
pg_free(mine);
922923
}
924+
925+
/*
926+
* Generates a valid restrict key (i.e., an alphanumeric string) for use with
927+
* psql's \restrict and \unrestrict meta-commands. For safety, the value is
928+
* chosen at random.
929+
*/
930+
char *
931+
generate_restrict_key(void)
932+
{
933+
uint8 buf[64];
934+
char *ret = palloc(sizeof(buf));
935+
936+
if (!pg_strong_random(buf, sizeof(buf)))
937+
return NULL;
938+
939+
for (int i = 0; i < sizeof(buf) - 1; i++)
940+
{
941+
uint8 idx = buf[i] % strlen(restrict_chars);
942+
943+
ret[i] = restrict_chars[idx];
944+
}
945+
ret[sizeof(buf) - 1] = '\0';
946+
947+
return ret;
948+
}
949+
950+
/*
951+
* Checks that a given restrict key (intended for use with psql's \restrict and
952+
* \unrestrict meta-commands) contains only alphanumeric characters.
953+
*/
954+
bool
955+
valid_restrict_key(const char *restrict_key)
956+
{
957+
return restrict_key != NULL &&
958+
restrict_key[0] != '\0' &&
959+
strspn(restrict_key, restrict_chars) == strlen(restrict_key);
960+
}

src/bin/pg_dump/dumputils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@ extern void makeAlterConfigCommand(PGconn *conn, const char *configitem,
6464
const char *type2, const char *name2,
6565
PQExpBuffer buf);
6666

67+
extern char *generate_restrict_key(void);
68+
extern bool valid_restrict_key(const char *restrict_key);
69+
6770
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_backup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ typedef struct _restoreOptions
158158
int enable_row_security;
159159
int sequence_data; /* dump sequence data even in schema-only mode */
160160
int binary_upgrade;
161+
162+
char *restrict_key;
161163
} RestoreOptions;
162164

163165
typedef struct _dumpOptions
@@ -205,6 +207,8 @@ typedef struct _dumpOptions
205207

206208
int sequence_data; /* dump sequence data even in schema-only mode */
207209
int do_nothing;
210+
211+
char *restrict_key;
208212
} DumpOptions;
209213

210214
/*

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
188188
dopt->include_everything = ropt->include_everything;
189189
dopt->enable_row_security = ropt->enable_row_security;
190190
dopt->sequence_data = ropt->sequence_data;
191+
dopt->restrict_key = ropt->restrict_key ? pg_strdup(ropt->restrict_key) : NULL;
191192

192193
return dopt;
193194
}
@@ -452,6 +453,17 @@ RestoreArchive(Archive *AHX)
452453

453454
ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
454455

456+
/*
457+
* If generating plain-text output, enter restricted mode to block any
458+
* unexpected psql meta-commands. A malicious source might try to inject
459+
* a variety of things via bogus responses to queries. While we cannot
460+
* prevent such sources from affecting the destination at restore time, we
461+
* can block psql meta-commands so that the client machine that runs psql
462+
* with the dump output remains unaffected.
463+
*/
464+
if (ropt->restrict_key)
465+
ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key);
466+
455467
if (AH->archiveRemoteVersion)
456468
ahprintf(AH, "-- Dumped from database version %s\n",
457469
AH->archiveRemoteVersion);
@@ -792,6 +804,14 @@ RestoreArchive(Archive *AHX)
792804

793805
ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n");
794806

807+
/*
808+
* If generating plain-text output, exit restricted mode at the very end
809+
* of the script. This is not pro forma; in particular, pg_dumpall
810+
* requires this when transitioning from one database to another.
811+
*/
812+
if (ropt->restrict_key)
813+
ahprintf(AH, "\\unrestrict %s\n\n", ropt->restrict_key);
814+
795815
/*
796816
* Clean up & we're done.
797817
*/
@@ -3354,11 +3374,21 @@ _reconnectToDB(ArchiveHandle *AH, const char *dbname)
33543374
else
33553375
{
33563376
PQExpBufferData connectbuf;
3377+
RestoreOptions *ropt = AH->public.ropt;
3378+
3379+
/*
3380+
* We must temporarily exit restricted mode for \connect, etc.
3381+
* Anything added between this line and the following \restrict must
3382+
* be careful to avoid any possible meta-command injection vectors.
3383+
*/
3384+
ahprintf(AH, "\\unrestrict %s\n", ropt->restrict_key);
33573385

33583386
initPQExpBuffer(&connectbuf);
33593387
appendPsqlMetaConnect(&connectbuf, dbname);
3360-
ahprintf(AH, "%s\n", connectbuf.data);
3388+
ahprintf(AH, "%s", connectbuf.data);
33613389
termPQExpBuffer(&connectbuf);
3390+
3391+
ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key);
33623392
}
33633393

33643394
/*

0 commit comments

Comments
 (0)