summaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/pg_backup_archiver.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2018-02-26 10:18:21 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2018-02-26 10:18:21 -0500
commit3d2aed664ee8271fd6c721ed0aa10168cda112ea (patch)
tree39be30feff26cda6a1fd262bc634d5114bf6f03b /src/bin/pg_dump/pg_backup_archiver.c
parent3bf05e096b9f8375e640c5d7996aa57efd7f240c (diff)
Avoid using unsafe search_path settings during dump and restore.
Historically, pg_dump has "set search_path = foo, pg_catalog" when dumping an object in schema "foo", and has also caused that setting to be used while restoring the object. This is problematic because functions and operators in schema "foo" could capture references meant to refer to pg_catalog entries, both in the queries issued by pg_dump and those issued during the subsequent restore run. That could result in dump/restore misbehavior, or in privilege escalation if a nefarious user installs trojan-horse functions or operators. This patch changes pg_dump so that it does not change the search_path dynamically. The emitted restore script sets the search_path to what was used at dump time, and then leaves it alone thereafter. Created objects are placed in the correct schema, regardless of the active search_path, by dint of schema-qualifying their names in the CREATE commands, as well as in subsequent ALTER and ALTER-like commands. Since this change requires a change in the behavior of pg_restore when processing an archive file made according to this new convention, bump the archive file version number; old versions of pg_restore will therefore refuse to process files made with new versions of pg_dump. Security: CVE-2018-1058
Diffstat (limited to 'src/bin/pg_dump/pg_backup_archiver.c')
-rw-r--r--src/bin/pg_dump/pg_backup_archiver.c53
1 files changed, 42 insertions, 11 deletions
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index a4deb53e3a8..fc233a608f3 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -70,6 +70,7 @@ static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
static void _selectTablespace(ArchiveHandle *AH, const char *tablespace);
static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te);
static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te);
+static void processSearchPathEntry(ArchiveHandle *AH, TocEntry *te);
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH);
static RestorePass _tocEntryRestorePass(TocEntry *te);
static bool _tocEntryIsACL(TocEntry *te);
@@ -900,7 +901,9 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel)
ahprintf(AH, "TRUNCATE TABLE %s%s;\n\n",
(PQserverVersion(AH->connection) >= 80400 ?
"ONLY " : ""),
- fmtId(te->tag));
+ fmtQualifiedId(PQserverVersion(AH->connection),
+ te->namespace,
+ te->tag));
}
/*
@@ -987,10 +990,10 @@ _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te)
/*
* Disable them.
*/
- _selectOutputSchema(AH, te->namespace);
-
ahprintf(AH, "ALTER TABLE %s DISABLE TRIGGER ALL;\n\n",
- fmtId(te->tag));
+ fmtQualifiedId(PQserverVersion(AH->connection),
+ te->namespace,
+ te->tag));
}
static void
@@ -1015,10 +1018,10 @@ _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te)
/*
* Enable them.
*/
- _selectOutputSchema(AH, te->namespace);
-
ahprintf(AH, "ALTER TABLE %s ENABLE TRIGGER ALL;\n\n",
- fmtId(te->tag));
+ fmtQualifiedId(PQserverVersion(AH->connection),
+ te->namespace,
+ te->tag));
}
/*
@@ -2711,6 +2714,8 @@ ReadToc(ArchiveHandle *AH)
processEncodingEntry(AH, te);
else if (strcmp(te->desc, "STDSTRINGS") == 0)
processStdStringsEntry(AH, te);
+ else if (strcmp(te->desc, "SEARCHPATH") == 0)
+ processSearchPathEntry(AH, te);
}
}
@@ -2759,6 +2764,16 @@ processStdStringsEntry(ArchiveHandle *AH, TocEntry *te)
}
static void
+processSearchPathEntry(ArchiveHandle *AH, TocEntry *te)
+{
+ /*
+ * te->defn should contain a command to set search_path. We just copy it
+ * verbatim for use later.
+ */
+ AH->public.searchpath = pg_strdup(te->defn);
+}
+
+static void
StrictNamesCheck(RestoreOptions *ropt)
{
const char *missing_name;
@@ -2814,9 +2829,10 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
teReqs res = REQ_SCHEMA | REQ_DATA;
RestoreOptions *ropt = AH->public.ropt;
- /* ENCODING and STDSTRINGS items are treated specially */
+ /* These items are treated specially */
if (strcmp(te->desc, "ENCODING") == 0 ||
- strcmp(te->desc, "STDSTRINGS") == 0)
+ strcmp(te->desc, "STDSTRINGS") == 0 ||
+ strcmp(te->desc, "SEARCHPATH") == 0)
return REQ_SPECIAL;
/*
@@ -3117,6 +3133,10 @@ _doSetFixedOutputState(ArchiveHandle *AH)
if (ropt && ropt->use_role)
ahprintf(AH, "SET ROLE %s;\n", fmtId(ropt->use_role));
+ /* Select the dump-time search_path */
+ if (AH->public.searchpath)
+ ahprintf(AH, "%s", AH->public.searchpath);
+
/* Make sure function checking is disabled */
ahprintf(AH, "SET check_function_bodies = false;\n");
@@ -3321,6 +3341,15 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
{
PQExpBuffer qry;
+ /*
+ * If there was a SEARCHPATH TOC entry, we're supposed to just stay with
+ * that search_path rather than switching to entry-specific paths.
+ * Otherwise, it's an old archive that will not restore correctly unless
+ * we set the search_path as it's expecting.
+ */
+ if (AH->public.searchpath)
+ return;
+
if (!schemaName || *schemaName == '\0' ||
(AH->currSchema && strcmp(AH->currSchema, schemaName) == 0))
return; /* no need to do anything */
@@ -3453,8 +3482,10 @@ _getObjectDescription(PQExpBuffer buf, TocEntry *te, ArchiveHandle *AH)
strcmp(type, "SUBSCRIPTION") == 0 ||
strcmp(type, "USER MAPPING") == 0)
{
- /* We already know that search_path was set properly */
- appendPQExpBuffer(buf, "%s %s", type, fmtId(te->tag));
+ appendPQExpBuffer(buf, "%s ", type);
+ if (te->namespace && *te->namespace)
+ appendPQExpBuffer(buf, "%s.", fmtId(te->namespace));
+ appendPQExpBufferStr(buf, fmtId(te->tag));
return;
}