8000 Back-patch "Only quote libpq connection string values that need quoti… · s-monk/postgres@a19edcd · GitHub
[go: up one dir, main page]

Skip to content

Commit a19edcd

Browse files
committed
Back-patch "Only quote libpq connection string values that need quoting."
Back-patch commit 2953cd6 and certain runPgDump() bits of 3dee636 to 9.2 and 9.1. This synchronizes their doConnStrQuoting() implementations with later releases. Subsequent security patches will modify that function. Security: CVE-2016-5424
1 parent 4837155 commit a19edcd

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/bin/pg_dump/pg_dumpall.c

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ dumpDatabases(PGconn *conn)
16561656
static int
16571657
runPgDump(const char *dbname)
16581658
{
1659-
PQExpBuffer connstr = createPQExpBuffer();
1659+
PQExpBuffer connstrbuf = createPQExpBuffer();
16601660
PQExpBuffer cmd = createPQExpBuffer();
16611661
int ret;
16621662

@@ -1678,11 +1678,10 @@ runPgDump(const char *dbname)
16781678
* database name as is, but if it contains any = characters, it would
16791679
* incorrectly treat it as a connection string.
16801680
*/
1681-
appendPQExpBuffer(connstr, "dbname='");
1682-
doConnStrQuoting(connstr, dbname);
1683-
appendPQExpBuffer(connstr, "'");
1681+
appendPQExpBufferStr(connstrbuf, "dbname=");
1682+
doConnStrQuoting(connstrbuf, dbname);
16841683

1685-
doShellQuoting(cmd, connstr->data);
1684+
doShellQuoting(cmd, connstrbuf->data);
16861685

16871686
appendPQExpBuffer(cmd, "%s", SYSTEMQUOTE);
16881687

@@ -1695,7 +1694,7 @@ runPgDump(const char *dbname)
16951694
ret = system(cmd->data);
16961695

16971696
destroyPQExpBuffer(cmd);
1698-
destroyPQExpBuffer(connstr);
1697+
destroyPQExpBuffer(connstrbuf);
16991698

17001699
return ret;
17011700
}
@@ -1943,15 +1942,40 @@ dumpTimestamp(char *msg)
19431942
static void
19441943
doConnStrQuoting(PQExpBuffer buf, const char *str)
19451944
{
1946-
while (*str)
1945+
const char *s;
1946+
bool needquotes;
1947+
1948+
/*
1949+
* If the string consists entirely of plain ASCII characters, no need to
1950+
* quote it. This is quite conservative, but better safe than sorry.
1951+
*/
1952+
needquotes = false;
1953+
for (s = str; *s; s++)
1954+
{
1955+
if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
1956+
(*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
1957+
{
1958+
needquotes = true;
1959+
break;
1960+
}
1961+
}
1962+
1963+
if (needquotes)
19471964
{
1948-
/* ' and \ must be escaped by to \' and \\ */
1949-
if (*str == '\'' || *str == '\\')
1950-
appendPQExpBufferChar(buf, '\\');
1965+
appendPQExpBufferChar(buf, '\'');
1966+
while (*str)
1967+
{
1968+
/* ' and \ must be escaped by to \' and \\ */
1969+
if (*str == '\'' || *str == '\\')
1970+
appendPQExpBufferChar(buf, '\\');
19511971

1952-
appendPQExpBufferChar(buf, *str);
1953-
str++;
1972+
appendPQExpBufferChar(buf, *str);
1973+
str++;
1974+
}
1975+
appendPQExpBufferChar(buf, '\'');
19541976
}
1977+
else
1978+
appendPQExpBufferStr(buf, str);
19551979
}
19561980

19571981
/*

0 commit comments

Comments
 (0)
0