Skip to content

Commit d267bdc

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 c326d7f commit d267bdc

File tree

21 files changed

+427
-11
lines changed

21 files changed

+427
-11
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">
@@ -1031,6 +1043,29 @@ PostgreSQL documentation
10311043
</listitem>
10321044
</varlistentry>
10331045

1046+
<varlistentry>
1047+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
1048+
<listitem>
1049+
<para>
1050+
Use the provided string as the <application>psql</application>
1051+
<command>\restrict</command> key in the dump output. This can only be
1052+
specified for plain-text dumps, i.e., when <option>--format</option> is
1053+
set to <literal>plain</literal> or the <option>--format</option> option
1054+
is omitted. If no restrict key is specified,
1055+
<application>pg_dump</application> will generate a random one as
1056+
needed. Keys may contain only alphanumeric characters.
1057+
</para>
1058+
<para>
1059+
This option is primarily intended for testing purposes and other
1060+
scenarios that require repeatable output (e.g., comparing dump files).
1061+
It is not recommended for general use, as a malicious server with
1062+
advance knowledge of the key may be able to inject arbitrary code that
1063+
will be executed on the machine that runs
1064+
<application>psql</application> with the dump output.
1065+
</para>
1066+
</listitem>
1067+
</varlistentry>
1068+
10341069
<varlistentry>
10351070
<term><option>--rows-per-insert=<replaceable class="parameter">nrows</replaceable></option></term>
10361071
<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>
@@ -533,6 +543,26 @@ PostgreSQL documentation
533543
</listitem>
534544
</varlistentry>
535545

546+
<varlistentry>
547+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
548+
<listitem>
549+
<para>
550+
Use the provided string as the <application>psql</application>
551+
<command>\restrict</command> key in the dump output. If no restrict
552+
key is specified, <application>pg_dumpall</application> will generate a
553+
random one as needed. Keys may contain only alphanumeric characters.
554+
</para>
555+
<para>
556+
This option is primarily intended for testing purposes and other
557+
scenarios that require repeatable output (e.g., comparing dump files).
558+
It is not recommended for general use, as a malicious server with
559+
advance knowledge of the key may be able to inject arbitrary code that
560+
will be executed on the machine that runs
561+
<application>psql</application> with the dump output.
562+
</para>
563+
</listitem>
564+
</varlistentry>
565+
536566
<varlistentry>
537567
<term><option>--rows-per-insert=<replaceable class="parameter">nrows</replaceable></option></term>
538568
<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">
@@ -685,6 +697,28 @@ PostgreSQL documentation
685697
</listitem>
686698
</varlistentry>
687699

700+
<varlistentry>
701+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
702+
<listitem>
703+
<para>
704+
Use the provided string as the <application>psql</application>
705+
<command>\restrict</command> key in the dump output. This can only be
706+
specified for SQL script output, i.e., when the <option>--file</option>
707+
option is used. If no restrict key is specified,
708+
<application>pg_restore</application> will generate a random one as
709+
needed. Keys may contain only alphanumeric characters.
710+
</para>
711+
<para>
712+
This option is primarily intended for testing purposes and other
713+
scenarios that require repeatable output (e.g., comparing dump files).
714+
It is not recommended for general use, as a malicious server with
715+
advance knowledge of the key may be able to inject arbitrary code that
716+
will be executed on the machine that runs
717+
<application>psql</application> with the dump output.
718+
</para>
719+
</listitem>
720+
</varlistentry>
721+
688722
<varlistentry>
689723
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
690724
<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
@@ -3244,6 +3244,24 @@ lo_import 152801
32443244
</varlistentry>
32453245

32463246

3247+
<varlistentry>
3248+
<term><literal>\restrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3249+
<listitem>
3250+
<para>
3251+
Enter "restricted" mode with the provided key. In this mode, the only
3252+
allowed meta-command is <command>\unrestrict</command>, to exit
3253+
restricted mode. The key may contain only alphanumeric characters.
3254+
</para>
3255+
<para>
3256+
This command is primarily intended for use in plain-text dumps
3257+
generated by <application>pg_dump</application>,
3258+
<application>pg_dumpall</application>, and
3259+
<application>pg_restore</application>, but it may be useful elsewhere.
3260+
</para>
3261+
</listitem>
3262+
</varlistentry>
3263+
3264+
32473265
<varlistentry>
32483266
<term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
32493267
<listitem>
@@ -3418,6 +3436,24 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
34183436
</varlistentry>
34193437

34203438

3439+
<varlistentry>
3440+
<term><literal>\unrestrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3441+
<listitem>
3442+
<para>
3443+
Exit "restricted" mode (i.e., where all other meta-commands are
3444+
blocked), provided the specified key matches the one given to
3445+
<command>\restrict</command> when restricted mode was entered.
3446+
</para>
3447+
<para>
3448+
This command is primarily intended for use in plain-text dumps
3449+
generated by <application>pg_dump</application>,
3450+
<application>pg_dumpall</application>, and
3451+
<application>pg_restore</application>, but it may be useful elsewhere.
3452+
</para>
3453+
</listitem>
3454+
</varlistentry>
3455+
3456+
34213457
<varlistentry>
34223458
<term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
34233459

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
@@ -153,6 +153,8 @@ typedef struct _restoreOptions
153153
int enable_row_security;
154154
int sequence_data; /* dump sequence data even in schema-only mode */
155155
int binary_upgrade;
156+
157+
char *restrict_key;
156158
} RestoreOptions;
157159

158160
typedef struct _dumpOptions
@@ -200,6 +202,8 @@ typedef struct _dumpOptions
200202

201203
int sequence_data; /* dump sequence data even in schema-only mode */
202204
int do_nothing;
205+
206+
char *restrict_key;
203207
} DumpOptions;
204208

205209
/*

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
209209
dopt->include_everything = ropt->include_everything;
210210
dopt->enable_row_security = ropt->enable_row_security;
211211
dopt->sequence_data = ropt->sequence_data;
212+
dopt->restrict_key = ropt->restrict_key ? pg_strdup(ropt->restrict_key) : NULL;
212213

213214
return dopt;
214215
}
@@ -465,6 +466,17 @@ RestoreArchive(Archive *AHX)
465466

466467
ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
467468

469+
/*
470+
* If generating plain-text output, enter restricted mode to block any
471+
* unexpected psql meta-commands. A malicious source might try to inject
472+
* a variety of things via bogus responses to queries. While we cannot
473+
* prevent such sources from affecting the destination at restore time, we
474+
* can block psql meta-commands so that the client machine that runs psql
475+
* with the dump output remains unaffected.
476+
*/
477+
if (ropt->restrict_key)
478+
ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key);
479+
468480
if (AH->archiveRemoteVersion)
469481
ahprintf(AH, "-- Dumped from database version %s\n",
470482
AH->archiveRemoteVersion);
@@ -741,6 +753,14 @@ RestoreArchive(Archive *AHX)
741753

742754
ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n");
743755

756+
/*
757+
* If generating plain-text output, exit restricted mode at the very end
758+
* of the script. This is not pro forma; in particular, pg_dumpall
759+
* requires this when transitioning from one database to another.
760+
*/
761+
if (ropt->restrict_key)
762+
ahprintf(AH, "\\unrestrict %s\n\n", ropt->restrict_key);
763+
744764
/*
745765
* Clean up & we're done.
746766
*/
@@ -3253,11 +3273,21 @@ _reconnectToDB(ArchiveHandle *AH, const char *dbname)
32533273
else
32543274
{
32553275
PQExpBufferData connectbuf;
3276+
RestoreOptions *ropt = AH->public.ropt;
3277+
3278+
/*
3279+
* We must temporarily exit restricted mode for \connect, etc.
3280+
* Anything added between this line and the following \restrict must
3281+
* be careful to avoid any possible meta-command injection vectors.
3282+
*/
3283+
ahprintf(AH, "\\unrestrict %s\n", ropt->restrict_key);
32563284

32573285
initPQExpBuffer(&connectbuf);
32583286
appendPsqlMetaConnect(&connectbuf, dbname);
3259-
ahprintf(AH, "%s\n", connectbuf.data);
3287+
ahprintf(AH, "%s", connectbuf.data);
32603288
termPQExpBuffer(&connectbuf);
3289+
3290+
ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key);
32613291
}
32623292

32633293
/*

0 commit comments

Comments
 (0)