Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5a9167c

Browse files
committed
Run REFRESH MATERIALIZED VIEW CONCURRENTLY in right security context
The internal commands in REFRESH MATERIALIZED VIEW CONCURRENTLY are correctly executed in SECURITY_RESTRICTED_OPERATION mode, except for creating the temporary "diff" table, because you cannot create temporary tables in SRO mode. But creating the temporary "diff" table is a pretty complex CTAS command that selects from another temporary table created earlier in the command. If you can cajole that CTAS command to execute code defined by the table owner, the table owner can run code with the privileges of the user running the REFRESH command. The proof-of-concept reported to the security team relied on CREATE RULE to convert the internally-built temp table to a view. That's not possible since commit b23cd18, and I was not able to find a different way to turn the SELECT on the temp table into code execution, so as far as I know this is only exploitable in v15 and below. That's a fiddly assumption though, so apply this patch to master and all stable versions. Thanks to Pedro Gallegos for the report. Security: CVE-2023-5869 Reviewed-by: Noah Misch
1 parent dafbfed commit 5a9167c

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/backend/commands/matview.c

+26-7
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,35 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
656656
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1))));
657657
}
658658

659+
/*
660+
* Create the temporary "diff" table.
661+
*
662+
* Temporarily switch out of the SECURITY_RESTRICTED_OPERATION context,
663+
* because you cannot create temp tables in SRO context. For extra
664+
* paranoia, add the composite type column only after switching back to
665+
* SRO context.
666+
*/
659667
SetUserIdAndSecContext(relowner,
660668
save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
669+
resetStringInfo(&querybuf);
670+
appendStringInfo(&querybuf,
671+
"CREATE TEMP TABLE %s (tid pg_catalog.tid)",
672+
diffname);
673+
if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
674+
elog(ERROR, "SPI_exec failed: %s", querybuf.data);
675+
SetUserIdAndSecContext(relowner,
676+
save_sec_context | SECURITY_RESTRICTED_OPERATION);
677+
resetStringInfo(&querybuf);
678+
appendStringInfo(&querybuf,
679+
"ALTER TABLE %s ADD COLUMN newdata %s",
680+
diffname, tempname);
681+
if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
682+
elog(ERROR, "SPI_exec failed: %s", querybuf.data);
661683

662-
/* Start building the query for creating the diff table. */
684+
/* Start building the query for populating the diff table. */
663685
resetStringInfo(&querybuf);
664686
appendStringInfo(&querybuf,
665-
"CREATE TEMP TABLE %s AS "
687+
"INSERT INTO %s "
666688
"SELECT mv.ctid AS tid, newdata.*::%s AS newdata "
667689
"FROM %s mv FULL JOIN %s newdata ON (",
668690
diffname, tempname, matviewname, tempname);
@@ -788,13 +810,10 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
788810
"WHERE newdata.* IS NULL OR mv.* IS NULL "
789811
"ORDER BY tid");
790812

791-
/* Create the temporary "diff" table. */
792-
if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
813+
/* Populate the temporary "diff" table. */
814+
if (SPI_exec(querybuf.data, 0) != SPI_OK_INSERT)
793815
elog(ERROR, "SPI_exec failed: %s", querybuf.data);
794816

795-
SetUserIdAndSecContext(relowner,
796-
save_sec_context | SECURITY_RESTRICTED_OPERATION);
797-
798817
/*
799818
* We have no further use for data from the "full-data" temp table, but we
800819
* must keep it around because its type is referenced from the diff table.

0 commit comments

Comments
 (0)