1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-30 06:01:21 +03:00

Restrict accesses to non-system views and foreign tables during pg_dump.

When pg_dump retrieves the list of database objects and performs the
data dump, there was possibility that objects are replaced with others
of the same name, such as views, and access them. This vulnerability
could result in code execution with superuser privileges during the
pg_dump process.

This issue can arise when dumping data of sequences, foreign
tables (only 13 or later), or tables registered with a WHERE clause in
the extension configuration table.

To address this, pg_dump now utilizes the newly introduced
restrict_nonsystem_relation_kind GUC parameter to restrict the
accesses to non-system views and foreign tables during the dump
process. This new GUC parameter is added to back branches too, but
these changes do not require cluster recreation.

Back-patch to all supported branches.

Reviewed-by: Noah Misch
Security: CVE-2024-7348
Backpatch-through: 12
This commit is contained in:
Masahiko Sawada
2024-08-05 06:05:25 -07:00
parent 8b57eb67e8
commit e81e53a0c1
14 changed files with 254 additions and 1 deletions

View File

@@ -23,6 +23,7 @@
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/rel.h"
@@ -322,6 +323,15 @@ GetFdwRoutine(Oid fdwhandler)
Datum datum;
FdwRoutine *routine;
/* Check if the access to foreign tables is restricted */
if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_FOREIGN_TABLE) != 0))
{
/* there must not be built-in FDW handler */
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("access to non-system foreign table is restricted")));
}
datum = OidFunctionCall0(fdwhandler);
routine = (FdwRoutine *) DatumGetPointer(datum);

View File

@@ -40,6 +40,7 @@
#include "parser/parse_clause.h"
#include "parser/parsetree.h"
#include "partitioning/partprune.h"
#include "tcop/tcopprot.h"
#include "utils/lsyscache.h"
@@ -7111,7 +7112,19 @@ make_modifytable(PlannerInfo *root, Plan *subplan,
Assert(rte->rtekind == RTE_RELATION);
Assert(operation != CMD_MERGE);
if (rte->relkind == RELKIND_FOREIGN_TABLE)
{
/* Check if the access to foreign tables is restricted */
if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_FOREIGN_TABLE) != 0))
{
/* there must not be built-in foreign tables */
Assert(rte->relid >= FirstNormalObjectId);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("access to non-system foreign table is restricted")));
}
fdwroutine = GetFdwRoutineByRelId(rte->relid);
}
else
fdwroutine = NULL;
}

View File

@@ -47,6 +47,7 @@
#include "rewrite/rewriteManip.h"
#include "statistics/statistics.h"
#include "storage/bufmgr.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/partcache.h"
@@ -465,6 +466,17 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
/* Grab foreign-table info using the relcache, while we have it */
if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
{
/* Check if the access to foreign tables is restricted */
if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_FOREIGN_TABLE) != 0))
{
/* there must not be built-in foreign tables */
Assert(RelationGetRelid(relation) >= FirstNormalObjectId);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("access to non-system foreign table is restricted")));
}
rel->serverid = GetForeignServerIdByRelId(RelationGetRelid(relation));
rel->fdwroutine = GetFdwRoutineForRelation(relation, true);
}

View File

@@ -41,6 +41,7 @@
#include "rewrite/rewriteManip.h"
#include "rewrite/rewriteSearchCycle.h"
#include "rewrite/rowsecurity.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
@@ -1734,6 +1735,14 @@ ApplyRetrieveRule(Query *parsetree,
if (rule->qual != NULL)
elog(ERROR, "cannot handle qualified ON SELECT rule");
/* Check if the expansion of non-system views are restricted */
if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_VIEW) != 0 &&
RelationGetRelid(relation) >= FirstNormalObjectId))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("access to non-system view \"%s\" is restricted",
RelationGetRelationName(relation))));
if (rt_index == parsetree->resultRelation)
{
/*
@@ -3128,6 +3137,14 @@ rewriteTargetView(Query *parsetree, Relation view)
}
}
/* Check if the expansion of non-system views are restricted */
if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_VIEW) != 0 &&
RelationGetRelid(view) >= FirstNormalObjectId))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("access to non-system view \"%s\" is restricted",
RelationGetRelationName(view))));
/*
* For INSERT/UPDATE the modified columns must all be updatable.
*/

View File

@@ -81,6 +81,7 @@
#include "utils/snapmgr.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"
#include "utils/varlena.h"
/* ----------------
* global variables
@@ -105,6 +106,9 @@ int PostAuthDelay = 0;
/* Time between checks that the client is still connected. */
int client_connection_check_interval = 0;
/* flags for non-system relation kinds to restrict use */
int restrict_nonsystem_relation_kind;
/* ----------------
* private typedefs etc
* ----------------
@@ -3601,6 +3605,66 @@ assign_max_stack_depth(int newval, void *extra)
max_stack_depth_bytes = newval_bytes;
}
/*
* GUC check_hook for restrict_nonsystem_relation_kind
*/
bool
check_restrict_nonsystem_relation_kind(char **newval, void **extra, GucSource source)
{
char *rawstring;
List *elemlist;
ListCell *l;
int flags = 0;
/* Need a modifiable copy of string */
rawstring = pstrdup(*newval);
if (!SplitIdentifierString(rawstring, ',', &elemlist))
{
/* syntax error in list */
GUC_check_errdetail("List syntax is invalid.");
pfree(rawstring);
list_free(elemlist);
return false;
}
foreach(l, elemlist)
{
char *tok = (char *) lfirst(l);
if (pg_strcasecmp(tok, "view") == 0)
flags |= RESTRICT_RELKIND_VIEW;
else if (pg_strcasecmp(tok, "foreign-table") == 0)
flags |= RESTRICT_RELKIND_FOREIGN_TABLE;
else
{
GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
pfree(rawstring);
list_free(elemlist);
return false;
}
}
pfree(rawstring);
list_free(elemlist);
/* Save the flags in *extra, for use by the assign function */
*extra = malloc(sizeof(int));
*((int *) *extra) = flags;
return true;
}
/*
* GUC assign_hook for restrict_nonsystem_relation_kind
*/
void
assign_restrict_nonsystem_relation_kind(const char *newval, void *extra)
{
int *flags = (int *) extra;
restrict_nonsystem_relation_kind = *flags;
}
/*
* set_debug_options --- apply "-d N" command line option

View File

@@ -716,6 +716,7 @@ static char *recovery_target_string;
static char *recovery_target_xid_string;
static char *recovery_target_name_string;
static char *recovery_target_lsn_string;
static char *restrict_nonsystem_relation_kind_string;
/* should be static, but commands/variable.c needs to get at this */
@@ -4711,6 +4712,17 @@ static struct config_string ConfigureNamesString[] =
check_backtrace_functions, assign_backtrace_functions, NULL
},
{
{"restrict_nonsystem_relation_kind", PGC_USERSET, CLIENT_CONN_STATEMENT,
gettext_noop("Sets relation kinds of non-system relation to restrict use"),
NULL,
GUC_LIST_INPUT | GUC_NOT_IN_SAMPLE
},
&restrict_nonsystem_relation_kind_string,
"",
check_restrict_nonsystem_relation_kind, assign_restrict_nonsystem_relation_kind, NULL
},
/* End-of-list marker */
{
{NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL