Skip to content

Commit 64a7106

Browse files
committed
Empty search_path in logical replication apply worker and walsender.
This is like CVE-2018-1058 commit 582edc3. Today, a malicious user of a publisher or subscriber database can invoke arbitrary SQL functions under an identity running replication, often a superuser. This fix may cause "does not exist" or "no schema has been selected to create in" errors in a replication process. After upgrading, consider watching server logs for these errors. Objects accruing schema qualification in the wake of the earlier commit are unlikely to need further correction. Back-patch to v10, which introduced logical replication. Security: CVE-2020-14349
1 parent d4d0ec9 commit 64a7106

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "pqexpbuffer.h"
2424
#include "access/xlog.h"
2525
#include "catalog/pg_type.h"
26+
#include "common/connect.h"
2627
#include "funcapi.h"
2728
#include "mb/pg_wchar.h"
2829
#include "miscadmin.h"
@@ -211,6 +212,22 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
211212
return NULL;
212213
}
213214

215+
if (logical)
216+
{
217+
PGresult *res;
218+
219+
res = libpqrcv_PQexec(conn->streamConn,
220+
ALWAYS_SECURE_SEARCH_PATH_SQL);
221+
if (PQresultStatus(res) != PGRES_TUPLES_OK)
222+
{
223+
PQclear(res);
224+
ereport(ERROR,
225+
(errmsg("could not clear search path: %s",
226+
pchomp(PQerrorMessage(conn->streamConn)))));
227+
}
228+
PQclear(res);
229+
}
230+
214231
conn->logical = logical;
215232

216233
return conn;

src/backend/replication/logical/worker.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,12 @@ ApplyWorkerMain(Datum main_arg)
16431643
MyLogicalRepWorker->userid,
16441644
0);
16451645

1646+
/*
1647+
* Set always-secure search path, so malicious users can't redirect user
1648+
* code (e.g. pg_index.indexprs).
1649+
*/
1650+
SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
1651+
16461652
/* Load the subscription into persistent memory context. */
16471653
ApplyContext = AllocSetContextCreate(TopMemoryContext,
16481654
"ApplyContext",

src/test/subscription/t/001_rep_changes.pl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
$node_subscriber->start;
1717

1818
# Create some preexisting content on publisher
19+
$node_publisher->safe_psql(
20+
'postgres',
21+
"CREATE FUNCTION public.pg_get_replica_identity_index(int)
22+
RETURNS regclass LANGUAGE sql AS 'SELECT 1/0'"); # shall not call
1923
$node_publisher->safe_psql('postgres',
2024
"CREATE TABLE tab_notrep AS SELECT generate_series(1,10) AS a");
2125
$node_publisher->safe_psql('postgres',

0 commit comments

Comments
 (0)