Skip to content

Commit 0c0a95c

Browse files
committed
Sanitize newlines in object names in "pg_restore -l" output.
Commits 89e0bac et al replaced newlines with spaces in object names printed in SQL comments, but we neglected to consider that the same names are also printed by "pg_restore -l", and a newline would render the output unparseable by "pg_restore -L". Apply the same replacement in "-l" output. Since "pg_restore -L" doesn't actually examine any object names, only the dump ID field that starts each line, this is enough to fix things for its purposes. The previous fix was treated as a security issue, and we might have done that here as well, except that the issue was reported publicly to start with. Anyway it's hard to see how this could be exploited for SQL injection; "pg_restore -L" doesn't do much with the file except parse it for leading integers. Per bug #14587 from Milos Urbanek. Back-patch to all supported versions. Discussion: https://postgr.es/m/20170310155318.1425.30483@wrigleys.postgresql.org
1 parent af47191 commit 0c0a95c

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,8 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
895895

896896
ahprintf(AH, ";\n; Archive created at %s", ctime(&AH->createDate));
897897
ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
898-
AH->archdbname, AH->tocCount, AH->compression);
898+
replace_line_endings(AH->archdbname),
899+
AH->tocCount, AH->compression);
899900

900901
switch (AH->format)
901902
{
@@ -932,10 +933,37 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
932933
curSection = te->section;
933934
if (ropt->verbose ||
934935
(_tocEntryRequired(te, curSection, ropt) & (REQ_SCHEMA | REQ_DATA)) != 0)
936+
{
937+
char *sanitized_name;
938+
char *sanitized_schema;
939+
char *sanitized_owner;
940+
941+
/*
942+
* As in _printTocEntry(), sanitize strings that might contain
943+
* newlines, to ensure that each logical output line is in fact
944+
* one physical output line. This prevents confusion when the
945+
* file is read by "pg_restore -L". Note that we currently don't
946+
* bother to quote names, meaning that the name fields aren't
947+
* automatically parseable. "pg_restore -L" doesn't care because
948+
* it only examines the dumpId field, but someday we might want to
949+
* try harder.
950+
*/
951+
sanitized_name = replace_line_endings(te->tag);
952+
if (te->namespace)
953+
sanitized_schema = replace_line_endings(te->namespace);
954+
else
955+
sanitized_schema = pg_strdup("-");
956+
sanitized_owner = replace_line_endings(te->owner);
957+
935958
ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
936959
te->catalogId.tableoid, te->catalogId.oid,
937-
te->desc, te->namespace ? te->namespace : "-",
938-
te->tag, te->owner);
960+
te->desc, sanitized_schema, sanitized_name,
961+
sanitized_owner);
962+
963+
free(sanitized_name);
964+
free(sanitized_schema);
965+
free(sanitized_owner);
966+
}
939967
if (ropt->verbose && te->nDeps > 0)
940968
{
941969
int i;
@@ -3217,8 +3245,9 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
32173245
}
32183246

32193247
/*
3220-
* Sanitize a string to be included in an SQL comment, by replacing any
3221-
* newlines with spaces.
3248+
* Sanitize a string to be included in an SQL comment or TOC listing,
3249+
* by replacing any newlines with spaces.
3250+
* The result is a freshly malloc'd string.
32223251
*/
32233252
static char *
32243253
replace_line_endings(const char *str)

0 commit comments

Comments
 (0)