postgres.git / summary / log / commit / refs
commit 7d47e41238004b6b8bce076d4bb76d858257b58d
Author: Etsuro Fujita <efujita@postgresql.org>
Date: Sat Sep 26 11:35:00 2026 +0000
First released in: 19.0
Fix privilege handling in postgres_fdw statistics import.
When ANALYZE imports statistics from a remote server, it fetched/stored
the statistics while still running as the user executing ANALYZE, not as
the foreign table's owner. This is inconsistent with the sampling path,
and it causes a security issue: a role that owns a foreign table but has
no access to the underlying remote data (eg, no access privileges on the
remote server) could obtain that data by having a privileged user run
ANALYZE on the table; the imported statistics like most_common_vals then
expose sampled values from the remote data, as shown in the reproducer
on the discussion thread.
To fix, switch to the foreign table owner's userid in analyze_rel()
before calling the ImportForeignStatistics() routine, mirroring the
identity used by the sampling path. This not only makes the privilege
handling consistent between the sampling/import paths, but also prevents
statistics import from disclosing data the table owner couldn't
otherwise obtain.
Oversight in commit 28972b6fc.
Reported-by: Fujii Masao <masao.fujii@gmail.com>
Reported-by: Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
Reported-by: Noah Misch <noah@leadboat.com>
Author: Noah Misch <noah@leadboat.com>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Reviewed-by: Etsuro Fujita <etsuro.fujita@gmail.com>
Discussion: https://postgr.es/m/CAPmGK16jVk+i2KMkkgR9ajoPdaUAinvcspkk5Bc6urbo2xYTMQ@mail.gmail.com
Backpatch-through: 19
contrib/postgres_fdw/postgres_fdw.c | 9 ++++-----
src/backend/commands/analyze.c | 30 ++++++++++++++++++++++++++----
2 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index fbe01fdde85..42a704344b8 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -5832,11 +5832,10 @@ fetch_remote_statistics(Relation relation,
}
/*
- * Get connection to the foreign server. Connection manager will
- * establish new connection if necessary.
- *
- * Note that unlike the sampling case, we only query pg_class and
- * pg_stats, so we do the remote access as the current user.
+ * Get the connection to use. We do the remote access as the table's
+ * owner. Note that unlike AnalyzeForeignTable(), the core code would
+ * already have switched us to the table's owner, before we are called
+ * from ImportForeignStatistics().
*/
user = GetUserMapping(GetUserId(), table->serverid);
conn = GetConnection(user, false, NULL);
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 4fea106bafd..d0498b14da1 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -228,10 +228,32 @@ analyze_rel(Oid relid, RangeVar *relation,
fdwroutine = GetFdwRoutineForRelation(onerel, false);
- if (fdwroutine->ImportForeignStatistics != NULL &&
- fdwroutine->ImportForeignStatistics(onerel, va_cols, elevel))
- stats_imported = true;
- else
+ if (fdwroutine->ImportForeignStatistics != NULL)
+ {
+ Oid save_userid;
+ int save_sec_context;
+ int save_nestlevel;
+
+ /*
+ * Switch to the table owner's userid, as in the sampling path.
+ * Also lock down security-restricted operations and arrange to
+ * make GUC variable changes local to this command.
+ */
+ GetUserIdAndSecContext(&save_userid, &save_sec_context);
+ SetUserIdAndSecContext(onerel->rd_rel->relowner,
+ save_sec_context | SECURITY_RESTRICTED_OPERATION);
+ save_nestlevel = NewGUCNestLevel();
+ RestrictSearchPath();
+
+ stats_imported = fdwroutine->ImportForeignStatistics(onerel,
+ va_cols,
+ elevel);
+
+ AtEOXact_GUC(false, save_nestlevel);
+ SetUserIdAndSecContext(save_userid, save_sec_context);
+ }
+
+ if (!stats_imported)
{
bool ok = false;