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

Commit f39ddd8

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 8b358b4 commit f39ddd8

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/bin/pg_dump/pg_backup_archiver.c

+34-5
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,8 @@ PrintTOCSummary(Archive *AHX)
11041104

11051105
ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
11061106
ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
1107-
AH->archdbname, AH->tocCount, AH->compression);
1107+
replace_line_endings(AH->archdbname),
1108+
AH->tocCount, AH->compression);
11081109

11091110
switch (AH->format)
11101111
{
@@ -1142,10 +1143,37 @@ PrintTOCSummary(Archive *AHX)
11421143
curSection = te->section;
11431144
if (ropt->verbose ||
11441145
(_tocEntryRequired(te, curSection, ropt) & (REQ_SCHEMA | REQ_DATA)) != 0)
1146+
{
1147+
char *sanitized_name;
1148+
char *sanitized_schema;
1149+
char *sanitized_owner;
1150+
1151+
/*
1152+
* As in _printTocEntry(), sanitize strings that might contain
1153+
* newlines, to ensure that each logical output line is in fact
1154+
* one physical output line. This prevents confusion when the
1155+
* file is read by "pg_restore -L". Note that we currently don't
1156+
* bother to quote names, meaning that the name fields aren't
1157+
* automatically parseable. "pg_restore -L" doesn't care because
1158+
* it only examines the dumpId field, but someday we might want to
1159+
* try harder.
1160+
*/
1161+
sanitized_name = replace_line_endings(te->tag);
1162+
if (te->namespace)
1163+
sanitized_schema = replace_line_endings(te->namespace);
1164+
else
1165+
sanitized_schema = pg_strdup("-");
1166+
sanitized_owner = replace_line_endings(te->owner);
1167+
11451168
ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
11461169
te->catalogId.tableoid, te->catalogId.oid,
1147-
te->desc, te->namespace ? te->namespace : "-",
1148-
te->tag, te->owner);
1170+
te->desc, sanitized_schema, sanitized_name,
1171+
sanitized_owner);
1172+
1173+
free(sanitized_name);
1174+
free(sanitized_schema);
1175+
free(sanitized_owner);
1176+
}
11491177
if (ropt->verbose && te->nDeps > 0)
11501178
{
11511179
int i;
@@ -3531,8 +3559,9 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData, bool acl_pass)
35313559
}
35323560

35333561
/*
3534-
* Sanitize a string to be included in an SQL comment, by replacing any
3535-
* newlines with spaces.
3562+
* Sanitize a string to be included in an SQL comment or TOC listing,
3563+
* by replacing any newlines with spaces.
3564+
* The result is a freshly malloc'd string.
35363565
*/
35373566
static char *
35383567
replace_line_endings(const char *str)

0 commit comments

Comments
 (0)