Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ed6f1b

Browse files
committed
pg_dump: avoid unsafe function calls in getPolicies().
getPolicies() had the same disease I fixed in other places incommite3fcbbd, i.e., it was calling pg_get_expr() forexpressions on tables that we don't necessarily have lock on.To fix, restrict the query to only collect interesting rows,rather than doing the filtering on the client side.Back-patch of commit3e6e86a. That's been in v15/HEAD long enoughto have some confidence about it, so now let's fix the problem inolder branches.Discussion:https://postgr.es/m/2273648.1634764485@sss.pgh.pa.usDiscussion:https://postgr.es/m/7d7eb6128f40401d81b3b7a898b6b4de@W2012-02.nidsa.locDiscussion:https://postgr.es/m/45c93d57-9973-248e-d2df-e02ca9af48d4@darold.net
1 parent344b784 commit1ed6f1b

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

‎src/bin/pg_dump/pg_dump.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,7 @@ void
35493549
getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
35503550
{
35513551
PQExpBuffer query;
3552+
PQExpBuffer tbloids;
35523553
PGresult *res;
35533554
PolicyInfo *polinfo;
35543555
inti_oid;
@@ -3564,15 +3565,17 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
35643565
j,
35653566
ntups;
35663567

3568+
/* No policies before 9.5 */
35673569
if (fout->remoteVersion < 90500)
35683570
return;
35693571

35703572
query = createPQExpBuffer();
3573+
tbloids = createPQExpBuffer();
35713574

35723575
/*
3573-
* First, check which tables have RLS enabled. We represent RLS being
3574-
* enabled on a table by creating a PolicyInfo object with null polname.
3576+
* Identify tables of interest, and check which ones have RLS enabled.
35753577
*/
3578+
appendPQExpBufferChar(tbloids, '{');
35763579
for (i = 0; i < numTables; i++)
35773580
{
35783581
TableInfo *tbinfo = &tblinfo[i];
@@ -3581,9 +3584,23 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
35813584
if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
35823585
continue;
35833586

3587+
/* It can't have RLS or policies if it's not a table */
3588+
if (tbinfo->relkind != RELKIND_RELATION &&
3589+
tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
3590+
continue;
3591+
3592+
/* Add it to the list of table OIDs to be probed below */
3593+
if (tbloids->len > 1)/* do we have more than the '{'? */
3594+
appendPQExpBufferChar(tbloids, ',');
3595+
appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
3596+
3597+
/* Is RLS enabled? (That's separate from whether it has policies) */
35843598
if (tbinfo->rowsec)
35853599
{
35863600
/*
3601+
* We represent RLS being enabled on a table by creating a
3602+
* PolicyInfo object with null polname.
3603+
*
35873604
* Note: use tableoid 0 so that this object won't be mistaken for
35883605
* something that pg_depend entries apply to.
35893606
*/
@@ -3603,15 +3620,18 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
36033620
polinfo->polwithcheck = NULL;
36043621
}
36053622
}
3623+
appendPQExpBufferChar(tbloids, '}');
36063624

36073625
/*
3608-
* Now, read all RLS policies, and create PolicyInfo objects for all those
3609-
* that are of interest.
3626+
* Now, read all RLS policies belonging to the tables of interest, and
3627+
* create PolicyInfo objects for them. (Note that we must filter the
3628+
* results server-side not locally, because we dare not apply pg_get_expr
3629+
* to tables we don't have lock on.)
36103630
*/
36113631
pg_log_info("reading row-level security policies");
36123632

36133633
printfPQExpBuffer(query,
3614-
"SELECT oid, tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
3634+
"SELECTpol.oid,pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
36153635
if (fout->remoteVersion >= 100000)
36163636
appendPQExpBuffer(query, "pol.polpermissive, ");
36173637
else
@@ -3621,7 +3641,9 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
36213641
" pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
36223642
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
36233643
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3624-
"FROM pg_catalog.pg_policy pol");
3644+
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
3645+
"JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)",
3646+
tbloids->data);
36253647

36263648
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
36273649

@@ -3645,13 +3667,6 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
36453667
Oidpolrelid = atooid(PQgetvalue(res, j, i_polrelid));
36463668
TableInfo *tbinfo = findTableByOid(polrelid);
36473669

3648-
/*
3649-
* Ignore row security on tables not to be dumped. (This will
3650-
* result in some harmless wasted slots in polinfo[].)
3651-
*/
3652-
if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
3653-
continue;
3654-
36553670
polinfo[j].dobj.objType = DO_POLICY;
36563671
polinfo[j].dobj.catId.tableoid =
36573672
atooid(PQgetvalue(res, j, i_tableoid));
@@ -3686,6 +3701,7 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
36863701
PQclear(res);
36873702

36883703
destroyPQExpBuffer(query);
3704+
destroyPQExpBuffer(tbloids);
36893705
}
36903706

36913707
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp