Skip to content

Commit b70df45

Browse files
nathan-bossartreshke
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 c11d8da commit b70df45

21 files changed

Lines changed: 426 additions & 11 deletions

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">
@@ -1078,6 +1090,29 @@ PostgreSQL documentation
10781090
</listitem>
10791091
</varlistentry>
10801092

1093+
<varlistentry>
1094+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
1095+
<listitem>
1096+
<para>
1097+
Use the provided string as the <application>psql</application>
1098+
<command>\restrict</command> key in the dump output. This can only be
1099+
specified for plain-text dumps, i.e., when <option>--format</option> is
1100+
set to <literal>plain</literal> or the <option>--format</option> option
1101+
is omitted. If no restrict key is specified,
1102+
<application>pg_dump</application> will generate a random one as
1103+
needed. Keys may contain only alphanumeric characters.
1104+
</para>
1105+
<para>
1106+
This option is primarily intended for testing purposes and other
1107+
scenarios that require repeatable output (e.g., comparing dump files).
1108+
It is not recommended for general use, as a malicious server with
1109+
advance knowledge of the key may be able to inject arbitrary code that
1110+
will be executed on the machine that runs
1111+
<application>psql</application> with the dump output.
1112+
</para>
1113+
</listitem>
1114+
</varlistentry>
1115+
10811116
<varlistentry>
10821117
<term><option>--rows-per-insert=<replaceable class="parameter">nrows</replaceable></option></term>
10831118
<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>
@@ -559,6 +569,26 @@ PostgreSQL documentation
559569
</listitem>
560570
</varlistentry>
561571

572+
<varlistentry>
573+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
574+
<listitem>
575+
<para>
576+
Use the provided string as the <application>psql</application>
577+
<command>\restrict</command> key in the dump output. If no restrict
578+
key is specified, <application>pg_dumpall</application> will generate a
579+
random one as needed. Keys may contain only alphanumeric characters.
580+
</para>
581+
<para>
582+
This option is primarily intended for testing purposes and other
583+
scenarios that require repeatable output (e.g., comparing dump files).
584+
It is not recommended for general use, as a malicious server with
585+
advance knowledge of the key may be able to inject arbitrary code that
586+
will be executed on the machine that runs
587+
<application>psql</application> with the dump output.
588+
</para>
589+
</listitem>
590+
</varlistentry>
591+
562592
<varlistentry>
563593
<term><option>--rows-per-insert=<replaceable class="parameter">nrows</replaceable></option></term>
564594
<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">
@@ -675,6 +687,28 @@ PostgreSQL documentation
675687
</listitem>
676688
</varlistentry>
677689

690+
<varlistentry>
691+
<term><option>--restrict-key=<replaceable class="parameter">restrict_key</replaceable></option></term>
692+
<listitem>
693+
<para>
694+
Use the provided string as the <application>psql</application>
695+
<command>\restrict</command> key in the dump output. This can only be
696+
specified for SQL script output, i.e., when the <option>--file</option>
697+
option is used. If no restrict key is specified,
698+
<application>pg_restore</application> will generate a random one as
699+
needed. Keys may contain only alphanumeric characters.
700+
</para>
701+
<para>
702+
This option is primarily intended for testing purposes and other
703+
scenarios that require repeatable output (e.g., comparing dump files).
704+
It is not recommended for general use, as a malicious server with
705+
advance knowledge of the key may be able to inject arbitrary code that
706+
will be executed on the machine that runs
707+
<application>psql</application> with the dump output.
708+
</para>
709+
</listitem>
710+
</varlistentry>
711+
678712
<varlistentry>
679713
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
680714
<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
@@ -3355,6 +3355,24 @@ lo_import 152801
33553355
</varlistentry>
33563356

33573357

3358+
<varlistentry id="app-psql-meta-command-restrict">
3359+
<term><literal>\restrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3360+
<listitem>
3361+
<para>
3362+
Enter "restricted" mode with the provided key. In this mode, the only
3363+
allowed meta-command is <command>\unrestrict</command>, to exit
3364+
restricted mode. The key may contain only alphanumeric characters.
3365+
</para>
3366+
<para>
3367+
This command is primarily intended for use in plain-text dumps
3368+
generated by <application>pg_dump</application>,
3369+
<application>pg_dumpall</application>, and
3370+
<application>pg_restore</application>, but it may be useful elsewhere.
3371+
</para>
3372+
</listitem>
3373+
</varlistentry>
3374+
3375+
33583376
<varlistentry id="app-psql-meta-command-s">
33593377
<term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
33603378
<listitem>
@@ -3529,6 +3547,24 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
35293547
</varlistentry>
35303548

35313549

3550+
<varlistentry id="app-psql-meta-command-unrestrict">
3551+
<term><literal>\unrestrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3552+
<listitem>
3553+
<para>
3554+
Exit "restricted" mode (i.e., where all other meta-commands are
3555+
blocked), provided the specified key matches the one given to
3556+
<command>\restrict</command> when restricted mode was entered.
3557+
</para>
3558+
<para>
3559+
This command is primarily intended for use in plain-text dumps
3560+
generated by <application>pg_dump</application>,
3561+
<application>pg_dumpall</application>, and
3562+
<application>pg_restore</application>, but it may be useful elsewhere.
3563+
</para>
3564+
</listitem>
3565+
</varlistentry>
3566+
3567+
35323568
<varlistentry id="app-psql-meta-command-unset">
35333569
<term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
35343570

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,
@@ -924,3 +925,40 @@ makeAlterConfigCommand(PGconn *conn, const char *configitem,
924925

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

src/bin/pg_dump/dumputils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,7 @@ extern void makeAlterConfigCommand(PGconn *conn, const char *configitem,
6767
extern char *escape_backslashes(const char *src, bool quotes_too);
6868
extern char *escape_fmtopts_string(const char *src);
6969
extern char *custom_fmtopts_string(const char *src);
70+
extern char *generate_restrict_key(void);
71+
extern bool valid_restrict_key(const char *restrict_key);
7072

7173
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_backup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ typedef struct _restoreOptions
156156
int enable_row_security;
157157
int sequence_data; /* dump sequence data even in schema-only mode */
158158
int binary_upgrade;
159+
160+
char *restrict_key;
159161
} RestoreOptions;
160162

161163
typedef struct _dumpOptions
@@ -202,6 +204,8 @@ typedef struct _dumpOptions
202204

203205
int sequence_data; /* dump sequence data even in schema-only mode */
204206
int do_nothing;
207+
208+
char *restrict_key;
205209
} DumpOptions;
206210

207211

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
206206
dopt->include_everything = ropt->include_everything;
207207
dopt->enable_row_security = ropt->enable_row_security;
208208
dopt->sequence_data = ropt->sequence_data;
209+
dopt->restrict_key = ropt->restrict_key ? pg_strdup(ropt->restrict_key) : NULL;
209210

210211
return dopt;
211212
}
@@ -468,6 +469,17 @@ RestoreArchive(Archive *AHX)
468469

469470
ahprintf(AH, "--\n-- Apache Cloudberry database dump\n--\n\n");
470471

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

754766
ahprintf(AH, "--\n-- Apache Cloudberry database dump complete\n--\n\n");
755767

768+
/*
769+
* If generating plain-text output, exit restricted mode at the very end
770+
* of the script. This is not pro forma; in particular, pg_dumpall
771+
* requires this when transitioning from one database to another.
772+
*/
773+
if (ropt->restrict_key)
774+
ahprintf(AH, "\\unrestrict %s\n\n", ropt->restrict_key);
775+
756776
/*
757777
* Clean up & we're done.
758778
*/
@@ -3301,11 +3321,21 @@ _reconnectToDB(ArchiveHandle *AH, const char *dbname)
33013321
else
33023322
{
33033323
PQExpBufferData connectbuf;
3324+
RestoreOptions *ropt = AH->public.ropt;
3325+
3326+
/*
3327+
* We must temporarily exit restricted mode for \connect, etc.
3328+
* Anything added between this line and the following \restrict must
3329+
* be careful to avoid any possible meta-command injection vectors.
3330+
*/
3331+
ahprintf(AH, "\\unrestrict %s\n", ropt->restrict_key);
33043332

33053333
initPQExpBuffer(&connectbuf);
33063334
appendPsqlMetaConnect(&connectbuf, dbname);
3307-
ahprintf(AH, "%s\n", connectbuf.data);
3335+
ahprintf(AH, "%s", connectbuf.data);
33083336
termPQExpBuffer(&connectbuf);
3337+
3338+
ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key);
33093339
}
33103340

33113341
/*

0 commit comments

Comments
 (0)