You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudberry.apache.org by dj...@apache.org on 2026/10/22 09:53:30 UTC

(cloudberry) branch main updated (2eb170819e9 -> 1991c4d102a)

This is an automated email from the ASF dual-hosted git repository.

tuhaihe pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git


    from 2eb170819e9 Doc: add Backpatch-through trailer to .gitmessage
     new be0ee2512c3 Require privilege to call gp_log_backend_memory_contexts()
     new f3dda0ec069 Restrict the gpexpand catalog lock and version bump to superusers
     new 1991c4d102a Restrict gp_add_master_standby() to superusers

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../isolation2/expected/gpexpand_catalog_lock.out  | 38 ++++++++++++++++
 .../test/isolation2/sql/gpexpand_catalog_lock.sql  | 25 ++++++++++
 src/backend/catalog/system_functions.sql           | 24 ++++++++++
 src/backend/utils/adt/mcxtfuncs.c                  | 24 ++++++++++
 src/backend/utils/gp/segadmin.c                    | 25 ++++++----
 src/backend/utils/misc/gpexpand.c                  | 18 +++++++-
 .../isolation2/expected/gpexpand_catalog_lock.out  | 38 ++++++++++++++++
 src/test/isolation2/sql/gpexpand_catalog_lock.sql  | 25 ++++++++++
 src/test/regress/expected/gp_log_mem_dispatch.out  | 53 ++++++++++++++++++++++
 src/test/regress/expected/privileges.out           | 26 +++++++++++
 src/test/regress/sql/gp_log_mem_dispatch.sql       | 40 ++++++++++++++++
 src/test/regress/sql/privileges.sql                | 25 ++++++++++
 .../expected/gpexpand_catalog_lock.out             | 38 ++++++++++++++++
 .../sql/gpexpand_catalog_lock.sql                  | 25 ++++++++++
 14 files changed, 414 insertions(+), 10 deletions(-)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


(cloudberry) 01/03: Require privilege to call gp_log_backend_memory_contexts()

Posted by dj...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tuhaihe pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit be0ee2512c3c20e8ea058e5cafdc14ca6e2641c9
Author: Jianghua Yang <yj...@gmail.com>
AuthorDate: Thu Oct 22 10:37:17 2026 +0800

    Require privilege to call gp_log_backend_memory_contexts()
    
    gp_log_backend_memory_contexts(int8[, int8]) was executable by PUBLIC
    and had no privilege check of its own, while the
    pg_log_backend_memory_contexts() it calls on the segments is protected
    only by its EXECUTE privilege, which the direct call bypasses. Any role
    could therefore make the backends of any other session dump their memory
    contexts into the segment logs.
    
    The wrapper now requires EXECUTE on
    pg_log_backend_memory_contexts(integer), on the coordinator and on the
    segments, so it follows the GRANT model of the function it wraps; the
    error names that function and hints at the required GRANT. The check
    takes effect on a binary upgrade. system_functions.sql additionally
    revokes both signatures from PUBLIC for new initdbs.
    
    gp_log_mem_dispatch covers the denial, the upgraded-cluster state
    (EXECUTE on the wrapper alone), the same denial on the segments when
    the wrapper is run there through gp_dist_random(), and a role granted
    both functions.
---
 src/backend/catalog/system_functions.sql          |  9 ++++
 src/backend/utils/adt/mcxtfuncs.c                 | 24 ++++++++++
 src/test/regress/expected/gp_log_mem_dispatch.out | 53 +++++++++++++++++++++++
 src/test/regress/sql/gp_log_mem_dispatch.sql      | 40 +++++++++++++++++
 4 files changed, 126 insertions(+)

diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 5cb5958557f..620101d223a 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -747,6 +747,15 @@ REVOKE EXECUTE ON FUNCTION pg_ls_replslotdir(text) FROM PUBLIC;
 
 REVOKE EXECUTE ON FUNCTION pg_log_backend_memory_contexts(integer) FROM PUBLIC;
 
+-- gp_log_backend_memory_contexts() makes the segments call
+-- pg_log_backend_memory_contexts() for the backends of another session, so it
+-- is revoked from PUBLIC like that function.  It also checks the caller's
+-- EXECUTE privilege on pg_log_backend_memory_contexts() at call time, which
+-- covers clusters that were not initdb'd with this REVOKE.
+REVOKE EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint) FROM PUBLIC;
+
+REVOKE EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint, bigint) FROM PUBLIC;
+
 -- pg_file_write/pg_file_rename/pg_file_unlink have proacl NULL (PUBLIC
 -- EXECUTE) because their _v1_1 bodies were copied in-core from adminpack
 -- without adminpack's matching REVOKE.  genfile.c now also checks for
diff --git a/src/backend/utils/adt/mcxtfuncs.c b/src/backend/utils/adt/mcxtfuncs.c
index 2b474e6ffd3..8f71580084c 100644
--- a/src/backend/utils/adt/mcxtfuncs.c
+++ b/src/backend/utils/adt/mcxtfuncs.c
@@ -20,12 +20,14 @@
 #include "funcapi.h"
 #include "libpq-fe.h"
 #include "libpq-int.h"
+#include "catalog/pg_proc.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
 #include "nodes/pg_list.h"
 #include "pgstat.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "utils/acl.h"
 #include "utils/builtins.h"
 
 #include "cdb/cdbdisp_query.h"
@@ -33,6 +35,8 @@
 #include "cdb/cdbutil.h"
 #include "cdb/cdbvars.h"
 #include "utils/elog.h"
+#include "utils/fmgroids.h"
+#include "utils/lsyscache.h"
 #include "utils/palloc.h"
 
 /* ----------
@@ -229,6 +233,26 @@ gp_log_backend_memory_contexts(PG_FUNCTION_ARGS)
 	int32			resultCount = 0;
 	List			*dispatchedSegments = NIL;
 	List			*returnedSegments = NIL;
+	AclResult		aclresult;
+
+	/*
+	 * The segments end up calling pg_log_backend_memory_contexts() directly
+	 * for every backend of the target session, which bypasses the EXECUTE
+	 * privilege of that function - its only protection: it is revoked from
+	 * PUBLIC by default and can be granted to other roles.  Require the same
+	 * privilege here, on the coordinator and on every segment, so that this
+	 * wrapper cannot be used to signal the backends of other sessions by a
+	 * role that may not call pg_log_backend_memory_contexts() itself.
+	 */
+	aclresult = object_aclcheck(ProcedureRelationId,
+								F_PG_LOG_BACKEND_MEMORY_CONTEXTS, GetUserId(),
+								ACL_EXECUTE);
+	if (aclresult != ACLCHECK_OK)
+		ereport(ERROR,
+				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+				 errmsg("permission denied for function %s",
+						get_func_name(F_PG_LOG_BACKEND_MEMORY_CONTEXTS)),
+				 errhint("gp_log_backend_memory_contexts() requires EXECUTE privilege on pg_log_backend_memory_contexts(integer).")));
 
 	if (Gp_role == GP_ROLE_UTILITY)
 	{
diff --git a/src/test/regress/expected/gp_log_mem_dispatch.out b/src/test/regress/expected/gp_log_mem_dispatch.out
index 0c51340ec16..7a9b74a8aa6 100644
--- a/src/test/regress/expected/gp_log_mem_dispatch.out
+++ b/src/test/regress/expected/gp_log_mem_dispatch.out
@@ -56,3 +56,56 @@ WARNING:  "-3" is not a valid content ID
                               0
 (1 row)
 
+-- gp_log_backend_memory_contexts() follows the GRANT model of the
+-- pg_log_backend_memory_contexts() it calls on the segments: a regular role
+-- is denied (EXECUTE is revoked from PUBLIC); a role granted EXECUTE on the
+-- wrapper alone is still refused by the function itself, which is the state
+-- of a cluster upgraded without initdb, where proacl stays NULL; a role
+-- granted EXECUTE on both functions may log its own session.
+CREATE ROLE regress_log_mem_user;
+SET SESSION AUTHORIZATION regress_log_mem_user;
+SELECT gp_log_backend_memory_contexts(0);
+ERROR:  permission denied for function gp_log_backend_memory_contexts
+SELECT gp_log_backend_memory_contexts(0, 0);
+ERROR:  permission denied for function gp_log_backend_memory_contexts
+RESET SESSION AUTHORIZATION;
+GRANT EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint) TO regress_log_mem_user;
+GRANT EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint, bigint) TO regress_log_mem_user;
+SET SESSION AUTHORIZATION regress_log_mem_user;
+SELECT gp_log_backend_memory_contexts(0);
+ERROR:  permission denied for function pg_log_backend_memory_contexts
+HINT:  gp_log_backend_memory_contexts() requires EXECUTE privilege on pg_log_backend_memory_contexts(integer).
+SELECT gp_log_backend_memory_contexts(0, 0);
+ERROR:  permission denied for function pg_log_backend_memory_contexts
+HINT:  gp_log_backend_memory_contexts() requires EXECUTE privilege on pg_log_backend_memory_contexts(integer).
+-- the check also runs on the segments, where no dispatch happens first; the
+-- segment of the error is not deterministic, so only report its SQLSTATE
+DO $$
+BEGIN
+    PERFORM gp_log_backend_memory_contexts(0) FROM gp_dist_random('gp_id');
+    RAISE NOTICE 'gp_log_backend_memory_contexts() unexpectedly succeeded';
+EXCEPTION WHEN insufficient_privilege THEN
+    RAISE NOTICE 'segment refused with SQLSTATE %', SQLSTATE;
+END;
+$$;
+NOTICE:  segment refused with SQLSTATE 42501
+RESET SESSION AUTHORIZATION;
+GRANT EXECUTE ON FUNCTION pg_log_backend_memory_contexts(integer) TO regress_log_mem_user;
+SET SESSION AUTHORIZATION regress_log_mem_user;
+SELECT gp_log_backend_memory_contexts(current_setting('gp_session_id')::bigint);
+ gp_log_backend_memory_contexts 
+--------------------------------
+                              3
+(1 row)
+
+SELECT gp_log_backend_memory_contexts(current_setting('gp_session_id')::bigint, 0);
+ gp_log_backend_memory_contexts 
+--------------------------------
+                              1
+(1 row)
+
+RESET SESSION AUTHORIZATION;
+REVOKE EXECUTE ON FUNCTION pg_log_backend_memory_contexts(integer) FROM regress_log_mem_user;
+REVOKE EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint) FROM regress_log_mem_user;
+REVOKE EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint, bigint) FROM regress_log_mem_user;
+DROP ROLE regress_log_mem_user;
diff --git a/src/test/regress/sql/gp_log_mem_dispatch.sql b/src/test/regress/sql/gp_log_mem_dispatch.sql
index 0b9c92c56f4..a87f1ce8cd4 100644
--- a/src/test/regress/sql/gp_log_mem_dispatch.sql
+++ b/src/test/regress/sql/gp_log_mem_dispatch.sql
@@ -36,3 +36,43 @@ WITH sessionCTE AS (
     WHERE application_name = 'pg_regress/gp_log_mem_dispatch'
 )
 SELECT gp_log_backend_memory_contexts(sess_id, -3) FROM sessionCTE;
+
+-- gp_log_backend_memory_contexts() follows the GRANT model of the
+-- pg_log_backend_memory_contexts() it calls on the segments: a regular role
+-- is denied (EXECUTE is revoked from PUBLIC); a role granted EXECUTE on the
+-- wrapper alone is still refused by the function itself, which is the state
+-- of a cluster upgraded without initdb, where proacl stays NULL; a role
+-- granted EXECUTE on both functions may log its own session.
+CREATE ROLE regress_log_mem_user;
+SET SESSION AUTHORIZATION regress_log_mem_user;
+SELECT gp_log_backend_memory_contexts(0);
+SELECT gp_log_backend_memory_contexts(0, 0);
+RESET SESSION AUTHORIZATION;
+
+GRANT EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint) TO regress_log_mem_user;
+GRANT EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint, bigint) TO regress_log_mem_user;
+SET SESSION AUTHORIZATION regress_log_mem_user;
+SELECT gp_log_backend_memory_contexts(0);
+SELECT gp_log_backend_memory_contexts(0, 0);
+-- the check also runs on the segments, where no dispatch happens first; the
+-- segment of the error is not deterministic, so only report its SQLSTATE
+DO $$
+BEGIN
+    PERFORM gp_log_backend_memory_contexts(0) FROM gp_dist_random('gp_id');
+    RAISE NOTICE 'gp_log_backend_memory_contexts() unexpectedly succeeded';
+EXCEPTION WHEN insufficient_privilege THEN
+    RAISE NOTICE 'segment refused with SQLSTATE %', SQLSTATE;
+END;
+$$;
+RESET SESSION AUTHORIZATION;
+
+GRANT EXECUTE ON FUNCTION pg_log_backend_memory_contexts(integer) TO regress_log_mem_user;
+SET SESSION AUTHORIZATION regress_log_mem_user;
+SELECT gp_log_backend_memory_contexts(current_setting('gp_session_id')::bigint);
+SELECT gp_log_backend_memory_contexts(current_setting('gp_session_id')::bigint, 0);
+RESET SESSION AUTHORIZATION;
+
+REVOKE EXECUTE ON FUNCTION pg_log_backend_memory_contexts(integer) FROM regress_log_mem_user;
+REVOKE EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint) FROM regress_log_mem_user;
+REVOKE EXECUTE ON FUNCTION gp_log_backend_memory_contexts(bigint, bigint) FROM regress_log_mem_user;
+DROP ROLE regress_log_mem_user;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


(cloudberry) 02/03: Restrict the gpexpand catalog lock and version bump to superusers

Posted by dj...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tuhaihe pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit f3dda0ec069574680b35c52c65b5abb07cd0a3a7
Author: Jianghua Yang <yj...@gmail.com>
AuthorDate: Thu Oct 22 10:37:31 2026 +0800

    Restrict the gpexpand catalog lock and version bump to superusers
    
    gp_expand_lock_catalog() and gp_expand_bump_version() had proacl = NULL
    (PUBLIC EXECUTE) and no privilege check of their own, although both exist
    only for gpexpand:
    
    - gp_expand_lock_catalog() takes the gpexpand catalog lock in exclusive
      mode; while a session holds it, every catalog change in the cluster
      fails with "gpexpand in progress".
    - gp_expand_bump_version() increments the gpexpand version in shared
      memory; every session that holds temporary tables is disconnected on
      its next catalog change, and all other sessions rebuild their gangs.
    
    Any role could therefore block all catalog changes, for superusers too,
    for as long as it kept a transaction open, or disconnect other sessions
    at will.
    
    Both functions now require a superuser and raise
    ERRCODE_INSUFFICIENT_PRIVILEGE otherwise. The check takes effect on a
    binary upgrade; system_functions.sql additionally revokes both functions
    from PUBLIC for new initdbs.
    
    The isolation2 test gpexpand_catalog_lock checks both layers: a regular
    role is denied at the privilege layer, and with EXECUTE granted (the
    state of a cluster upgraded without initdb, where proacl stays NULL) by
    the functions themselves. The singlenode and pax_storage copies of the
    test are kept in sync.
    
    gpexpand and gpshrink, the in-tree callers, run as the superuser and are
    unaffected.
---
 .../isolation2/expected/gpexpand_catalog_lock.out  | 38 ++++++++++++++++++++++
 .../test/isolation2/sql/gpexpand_catalog_lock.sql  | 25 ++++++++++++++
 src/backend/catalog/system_functions.sql           |  7 ++++
 src/backend/utils/misc/gpexpand.c                  | 18 +++++++++-
 .../isolation2/expected/gpexpand_catalog_lock.out  | 38 ++++++++++++++++++++++
 src/test/isolation2/sql/gpexpand_catalog_lock.sql  | 25 ++++++++++++++
 .../expected/gpexpand_catalog_lock.out             | 38 ++++++++++++++++++++++
 .../sql/gpexpand_catalog_lock.sql                  | 25 ++++++++++++++
 8 files changed, 213 insertions(+), 1 deletion(-)

diff --git a/contrib/pax_storage/src/test/isolation2/expected/gpexpand_catalog_lock.out b/contrib/pax_storage/src/test/isolation2/expected/gpexpand_catalog_lock.out
index b060a23cfcc..bd60837b224 100644
--- a/contrib/pax_storage/src/test/isolation2/expected/gpexpand_catalog_lock.out
+++ b/contrib/pax_storage/src/test/isolation2/expected/gpexpand_catalog_lock.out
@@ -247,3 +247,41 @@ ROLLBACK
 
 10: end;
 END
+
+--
+-- the catalog lock and the version bump are reserved to superusers
+--
+
+create role regress_gpexpand_user;
+CREATE
+
+-- a regular role is denied at the privilege layer ...
+40: set session authorization regress_gpexpand_user;
+SET
+40: select gp_expand_lock_catalog();
+ERROR:  permission denied for function gp_expand_lock_catalog
+40: select gp_expand_bump_version();
+ERROR:  permission denied for function gp_expand_bump_version
+40: reset session authorization;
+RESET
+
+-- ... and, with EXECUTE granted as on a cluster upgraded without initdb,
+-- by the functions themselves
+grant execute on function gp_expand_lock_catalog() to regress_gpexpand_user;
+GRANT
+grant execute on function gp_expand_bump_version() to regress_gpexpand_user;
+GRANT
+40: set session authorization regress_gpexpand_user;
+SET
+40: select gp_expand_lock_catalog();
+ERROR:  must be superuser to lock the catalog for gpexpand
+40: select gp_expand_bump_version();
+ERROR:  must be superuser to bump the gpexpand version
+40q: ... <quitting>
+
+revoke execute on function gp_expand_lock_catalog() from regress_gpexpand_user;
+REVOKE
+revoke execute on function gp_expand_bump_version() from regress_gpexpand_user;
+REVOKE
+drop role regress_gpexpand_user;
+DROP
diff --git a/contrib/pax_storage/src/test/isolation2/sql/gpexpand_catalog_lock.sql b/contrib/pax_storage/src/test/isolation2/sql/gpexpand_catalog_lock.sql
index 321343ec454..601696936d3 100644
--- a/contrib/pax_storage/src/test/isolation2/sql/gpexpand_catalog_lock.sql
+++ b/contrib/pax_storage/src/test/isolation2/sql/gpexpand_catalog_lock.sql
@@ -152,3 +152,28 @@ create table t30 (c1 int, c2 int) distributed by (c1);
 30: rollback;
 
 10: end;
+
+--
+-- the catalog lock and the version bump are reserved to superusers
+--
+
+create role regress_gpexpand_user;
+
+-- a regular role is denied at the privilege layer ...
+40: set session authorization regress_gpexpand_user;
+40: select gp_expand_lock_catalog();
+40: select gp_expand_bump_version();
+40: reset session authorization;
+
+-- ... and, with EXECUTE granted as on a cluster upgraded without initdb,
+-- by the functions themselves
+grant execute on function gp_expand_lock_catalog() to regress_gpexpand_user;
+grant execute on function gp_expand_bump_version() to regress_gpexpand_user;
+40: set session authorization regress_gpexpand_user;
+40: select gp_expand_lock_catalog();
+40: select gp_expand_bump_version();
+40q:
+
+revoke execute on function gp_expand_lock_catalog() from regress_gpexpand_user;
+revoke execute on function gp_expand_bump_version() from regress_gpexpand_user;
+drop role regress_gpexpand_user;
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 620101d223a..0beaeac06d1 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -774,6 +774,13 @@ REVOKE EXECUTE ON FUNCTION pg_file_unlink(text) FROM public;
 -- read-side functions above (pg_read_server_files) rather than write.
 REVOKE EXECUTE ON FUNCTION pg_logdir_ls() FROM public;
 
+-- The catalog lock and the version bump used by gpexpand are reserved to
+-- superusers.  The functions enforce that themselves as well; this is defense
+-- in depth for new initdbs.
+REVOKE EXECUTE ON FUNCTION gp_expand_lock_catalog() FROM public;
+
+REVOKE EXECUTE ON FUNCTION gp_expand_bump_version() FROM public;
+
 
 --
 -- We also set up some things as accessible to standard roles.
diff --git a/src/backend/utils/misc/gpexpand.c b/src/backend/utils/misc/gpexpand.c
index 1c9b02a6778..5d63f4e3c45 100644
--- a/src/backend/utils/misc/gpexpand.c
+++ b/src/backend/utils/misc/gpexpand.c
@@ -26,6 +26,7 @@
 #include "catalog/pg_statistic.h"
 #include "cdb/cdbutil.h"
 #include "cdb/cdbvars.h"
+#include "miscadmin.h"
 #include "postmaster/fts.h"
 #include "storage/lock.h"
 #include "utils/builtins.h"
@@ -79,10 +80,18 @@ GetGpExpandVersion(void)
  * a gpexpand version change also prevent concurrent changes to catalog
  * during gpexpand (see gp_expand_lock_catalog)
  *
+ * Only superusers may call it: a version change makes every other session
+ * rebuild its view of the cluster and disconnects the sessions that hold
+ * temporary tables.
  */
 Datum
 gp_expand_bump_version(PG_FUNCTION_ARGS)
 {
+	if (!superuser())
+		ereport(ERROR,
+				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+				 errmsg("must be superuser to bump the gpexpand version")));
+
 	*gp_expand_version += 1;
 	PG_RETURN_VOID();
 }
@@ -90,11 +99,18 @@ gp_expand_bump_version(PG_FUNCTION_ARGS)
 /*
  * Lock the catalog lock in exclusive mode.
  *
- * This should only be called by gpexpand.
+ * This should only be called by gpexpand, and is therefore restricted to
+ * superusers: while the lock is held, every catalog change in the cluster
+ * fails (see gp_expand_protect_catalog_changes).
  */
 Datum
 gp_expand_lock_catalog(PG_FUNCTION_ARGS)
 {
+	if (!superuser())
+		ereport(ERROR,
+				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+				 errmsg("must be superuser to lock the catalog for gpexpand")));
+
 	(void) LockAcquire(&gp_expand_locktag, AccessExclusiveLock, false, false);
 
 	PG_RETURN_VOID();
diff --git a/src/test/isolation2/expected/gpexpand_catalog_lock.out b/src/test/isolation2/expected/gpexpand_catalog_lock.out
index b060a23cfcc..bd60837b224 100644
--- a/src/test/isolation2/expected/gpexpand_catalog_lock.out
+++ b/src/test/isolation2/expected/gpexpand_catalog_lock.out
@@ -247,3 +247,41 @@ ROLLBACK
 
 10: end;
 END
+
+--
+-- the catalog lock and the version bump are reserved to superusers
+--
+
+create role regress_gpexpand_user;
+CREATE
+
+-- a regular role is denied at the privilege layer ...
+40: set session authorization regress_gpexpand_user;
+SET
+40: select gp_expand_lock_catalog();
+ERROR:  permission denied for function gp_expand_lock_catalog
+40: select gp_expand_bump_version();
+ERROR:  permission denied for function gp_expand_bump_version
+40: reset session authorization;
+RESET
+
+-- ... and, with EXECUTE granted as on a cluster upgraded without initdb,
+-- by the functions themselves
+grant execute on function gp_expand_lock_catalog() to regress_gpexpand_user;
+GRANT
+grant execute on function gp_expand_bump_version() to regress_gpexpand_user;
+GRANT
+40: set session authorization regress_gpexpand_user;
+SET
+40: select gp_expand_lock_catalog();
+ERROR:  must be superuser to lock the catalog for gpexpand
+40: select gp_expand_bump_version();
+ERROR:  must be superuser to bump the gpexpand version
+40q: ... <quitting>
+
+revoke execute on function gp_expand_lock_catalog() from regress_gpexpand_user;
+REVOKE
+revoke execute on function gp_expand_bump_version() from regress_gpexpand_user;
+REVOKE
+drop role regress_gpexpand_user;
+DROP
diff --git a/src/test/isolation2/sql/gpexpand_catalog_lock.sql b/src/test/isolation2/sql/gpexpand_catalog_lock.sql
index 321343ec454..601696936d3 100644
--- a/src/test/isolation2/sql/gpexpand_catalog_lock.sql
+++ b/src/test/isolation2/sql/gpexpand_catalog_lock.sql
@@ -152,3 +152,28 @@ create table t30 (c1 int, c2 int) distributed by (c1);
 30: rollback;
 
 10: end;
+
+--
+-- the catalog lock and the version bump are reserved to superusers
+--
+
+create role regress_gpexpand_user;
+
+-- a regular role is denied at the privilege layer ...
+40: set session authorization regress_gpexpand_user;
+40: select gp_expand_lock_catalog();
+40: select gp_expand_bump_version();
+40: reset session authorization;
+
+-- ... and, with EXECUTE granted as on a cluster upgraded without initdb,
+-- by the functions themselves
+grant execute on function gp_expand_lock_catalog() to regress_gpexpand_user;
+grant execute on function gp_expand_bump_version() to regress_gpexpand_user;
+40: set session authorization regress_gpexpand_user;
+40: select gp_expand_lock_catalog();
+40: select gp_expand_bump_version();
+40q:
+
+revoke execute on function gp_expand_lock_catalog() from regress_gpexpand_user;
+revoke execute on function gp_expand_bump_version() from regress_gpexpand_user;
+drop role regress_gpexpand_user;
diff --git a/src/test/singlenode_isolation2/expected/gpexpand_catalog_lock.out b/src/test/singlenode_isolation2/expected/gpexpand_catalog_lock.out
index b060a23cfcc..bd60837b224 100644
--- a/src/test/singlenode_isolation2/expected/gpexpand_catalog_lock.out
+++ b/src/test/singlenode_isolation2/expected/gpexpand_catalog_lock.out
@@ -247,3 +247,41 @@ ROLLBACK
 
 10: end;
 END
+
+--
+-- the catalog lock and the version bump are reserved to superusers
+--
+
+create role regress_gpexpand_user;
+CREATE
+
+-- a regular role is denied at the privilege layer ...
+40: set session authorization regress_gpexpand_user;
+SET
+40: select gp_expand_lock_catalog();
+ERROR:  permission denied for function gp_expand_lock_catalog
+40: select gp_expand_bump_version();
+ERROR:  permission denied for function gp_expand_bump_version
+40: reset session authorization;
+RESET
+
+-- ... and, with EXECUTE granted as on a cluster upgraded without initdb,
+-- by the functions themselves
+grant execute on function gp_expand_lock_catalog() to regress_gpexpand_user;
+GRANT
+grant execute on function gp_expand_bump_version() to regress_gpexpand_user;
+GRANT
+40: set session authorization regress_gpexpand_user;
+SET
+40: select gp_expand_lock_catalog();
+ERROR:  must be superuser to lock the catalog for gpexpand
+40: select gp_expand_bump_version();
+ERROR:  must be superuser to bump the gpexpand version
+40q: ... <quitting>
+
+revoke execute on function gp_expand_lock_catalog() from regress_gpexpand_user;
+REVOKE
+revoke execute on function gp_expand_bump_version() from regress_gpexpand_user;
+REVOKE
+drop role regress_gpexpand_user;
+DROP
diff --git a/src/test/singlenode_isolation2/sql/gpexpand_catalog_lock.sql b/src/test/singlenode_isolation2/sql/gpexpand_catalog_lock.sql
index 321343ec454..601696936d3 100644
--- a/src/test/singlenode_isolation2/sql/gpexpand_catalog_lock.sql
+++ b/src/test/singlenode_isolation2/sql/gpexpand_catalog_lock.sql
@@ -152,3 +152,28 @@ create table t30 (c1 int, c2 int) distributed by (c1);
 30: rollback;
 
 10: end;
+
+--
+-- the catalog lock and the version bump are reserved to superusers
+--
+
+create role regress_gpexpand_user;
+
+-- a regular role is denied at the privilege layer ...
+40: set session authorization regress_gpexpand_user;
+40: select gp_expand_lock_catalog();
+40: select gp_expand_bump_version();
+40: reset session authorization;
+
+-- ... and, with EXECUTE granted as on a cluster upgraded without initdb,
+-- by the functions themselves
+grant execute on function gp_expand_lock_catalog() to regress_gpexpand_user;
+grant execute on function gp_expand_bump_version() to regress_gpexpand_user;
+40: set session authorization regress_gpexpand_user;
+40: select gp_expand_lock_catalog();
+40: select gp_expand_bump_version();
+40q:
+
+revoke execute on function gp_expand_lock_catalog() from regress_gpexpand_user;
+revoke execute on function gp_expand_bump_version() from regress_gpexpand_user;
+drop role regress_gpexpand_user;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


(cloudberry) 03/03: Restrict gp_add_master_standby() to superusers

Posted by dj...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tuhaihe pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 1991c4d102a5118270f3ffb45eba21c4d00a6e81
Author: Jianghua Yang <yj...@gmail.com>
AuthorDate: Thu Oct 22 10:37:44 2026 +0800

    Restrict gp_add_master_standby() to superusers
    
    gp_add_master_standby() was the only segadmin function whose
    mirroring_sanity_check() flags did not include SUPERUSER, and like the
    others it had proacl = NULL (PUBLIC EXECUTE). From a utility-mode
    connection, a plain role could therefore insert a standby row into
    gp_segment_configuration on a cluster that has no standby, after which
    gpinitstandby refuses to add a real one until a superuser removes the
    bogus row.
    
    The function now passes SUPERUSER to mirroring_sanity_check(), like
    gp_remove_master_standby() and the segment functions, and runs that
    check before it validates its arguments. mirroring_sanity_check() checks
    the privilege before the mode and location checks, with
    ERRCODE_INSUFFICIENT_PRIVILEGE, so an unprivileged caller is refused
    without learning anything else. The check takes effect on a binary
    upgrade; system_functions.sql additionally revokes both signatures from
    PUBLIC for new initdbs.
    
    The privileges regression test checks both layers: a regular role is
    denied at the privilege layer, and with EXECUTE granted (the state of a
    cluster upgraded without initdb, where proacl stays NULL) by the
    function itself, also when the arguments are NULL.
    
    gpinitstandby, the in-tree caller, runs as the superuser and is
    unaffected.
---
 src/backend/catalog/system_functions.sql |  8 ++++++++
 src/backend/utils/gp/segadmin.c          | 25 ++++++++++++++++---------
 src/test/regress/expected/privileges.out | 26 ++++++++++++++++++++++++++
 src/test/regress/sql/privileges.sql      | 25 +++++++++++++++++++++++++
 4 files changed, 75 insertions(+), 9 deletions(-)

diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 0beaeac06d1..aa00caa681d 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -781,6 +781,14 @@ REVOKE EXECUTE ON FUNCTION gp_expand_lock_catalog() FROM public;
 
 REVOKE EXECUTE ON FUNCTION gp_expand_bump_version() FROM public;
 
+-- Adding a standby coordinator is reserved to superusers, like every other
+-- change to the segment configuration made by the management tools.  The
+-- function enforces that itself as well; this is defense in depth for new
+-- initdbs.
+REVOKE EXECUTE ON FUNCTION gp_add_master_standby(text, text, text) FROM public;
+
+REVOKE EXECUTE ON FUNCTION gp_add_master_standby(text, text, text, integer) FROM public;
+
 
 --
 -- We also set up some things as accessible to standard roles.
diff --git a/src/backend/utils/gp/segadmin.c b/src/backend/utils/gp/segadmin.c
index e8b9b309cb6..947e695a725 100644
--- a/src/backend/utils/gp/segadmin.c
+++ b/src/backend/utils/gp/segadmin.c
@@ -128,6 +128,19 @@ get_maxcontentid()
 static void
 mirroring_sanity_check(int flags, const char *func)
 {
+	/*
+	 * Check the privilege first: a role that may not call the function at all
+	 * is refused before it learns anything about the mode or the role of this
+	 * server.
+	 */
+	if ((flags & SUPERUSER) == SUPERUSER)
+	{
+		if (!superuser())
+			ereport(ERROR,
+					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+					 errmsg("%s can only be run by a superuser", func)));
+	}
+
 	if ((flags & MASTER_ONLY) == MASTER_ONLY)
 	{
 		if (GpIdentity.dbid == UNINITIALIZED_GP_IDENTITY_VALUE)
@@ -149,12 +162,6 @@ mirroring_sanity_check(int flags, const char *func)
 			elog(ERROR, "%s must be run in single-user mode", func);
 	}
 
-	if ((flags & SUPERUSER) == SUPERUSER)
-	{
-		if (!superuser())
-			elog(ERROR, "%s can only be run by a superuser", func);
-	}
-
 	if ((flags & SEGMENT_ONLY) == SEGMENT_ONLY)
 	{
 		if (GpIdentity.dbid == UNINITIALIZED_GP_IDENTITY_VALUE)
@@ -589,6 +596,9 @@ gp_add_master_standby(PG_FUNCTION_ARGS)
 	GpSegConfigEntry	*config;
 	Relation	gprel;
 
+	mirroring_sanity_check(MASTER_ONLY | UTILITY_MODE | SUPERUSER,
+						   "gp_add_master_standby");
+
 	if (PG_ARGISNULL(0))
 		elog(ERROR, "host name cannot be NULL");
 	if (PG_ARGISNULL(1))
@@ -596,9 +606,6 @@ gp_add_master_standby(PG_FUNCTION_ARGS)
 	if (PG_ARGISNULL(2))
 		elog(ERROR, "datadir cannot be NULL");
 
-	mirroring_sanity_check(MASTER_ONLY | UTILITY_MODE,
-						   "gp_add_master_standby");
-
 	/* Check if the system is ok */
 	if (standby_exists())
 		elog(ERROR, "only a single master standby may be defined");
diff --git a/src/test/regress/expected/privileges.out b/src/test/regress/expected/privileges.out
index b26f447b99e..6136cbb617f 100644
--- a/src/test/regress/expected/privileges.out
+++ b/src/test/regress/expected/privileges.out
@@ -3320,3 +3320,29 @@ DROP SCHEMA regress_roleoption;
 DROP ROLE regress_roleoption_protagonist;
 DROP ROLE regress_roleoption_donor;
 DROP ROLE regress_roleoption_recipient;
+-- gp_add_master_standby() is reserved to superusers: a regular role is
+-- denied at the privilege layer, and with EXECUTE granted - the state of a
+-- cluster upgraded without initdb, where proacl stays NULL - by the function
+-- itself.
+CREATE ROLE regress_standby_user;
+SET SESSION AUTHORIZATION regress_standby_user;
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent');
+ERROR:  permission denied for function gp_add_master_standby
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent', 5432);
+ERROR:  permission denied for function gp_add_master_standby
+RESET SESSION AUTHORIZATION;
+GRANT EXECUTE ON FUNCTION gp_add_master_standby(text, text, text) TO regress_standby_user;
+GRANT EXECUTE ON FUNCTION gp_add_master_standby(text, text, text, integer) TO regress_standby_user;
+SET SESSION AUTHORIZATION regress_standby_user;
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent');
+ERROR:  gp_add_master_standby can only be run by a superuser
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent', 5432);
+ERROR:  gp_add_master_standby can only be run by a superuser
+-- the privilege is checked before the arguments
+SELECT gp_add_master_standby(NULL, NULL, NULL);
+ERROR:  gp_add_master_standby can only be run by a superuser
+RESET SESSION AUTHORIZATION;
+-- clean up
+REVOKE EXECUTE ON FUNCTION gp_add_master_standby(text, text, text) FROM regress_standby_user;
+REVOKE EXECUTE ON FUNCTION gp_add_master_standby(text, text, text, integer) FROM regress_standby_user;
+DROP ROLE regress_standby_user;
diff --git a/src/test/regress/sql/privileges.sql b/src/test/regress/sql/privileges.sql
index 89a01ed421e..3af2501b542 100644
--- a/src/test/regress/sql/privileges.sql
+++ b/src/test/regress/sql/privileges.sql
@@ -2127,3 +2127,28 @@ DROP SCHEMA regress_roleoption;
 DROP ROLE regress_roleoption_protagonist;
 DROP ROLE regress_roleoption_donor;
 DROP ROLE regress_roleoption_recipient;
+
+-- gp_add_master_standby() is reserved to superusers: a regular role is
+-- denied at the privilege layer, and with EXECUTE granted - the state of a
+-- cluster upgraded without initdb, where proacl stays NULL - by the function
+-- itself.
+CREATE ROLE regress_standby_user;
+
+SET SESSION AUTHORIZATION regress_standby_user;
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent');
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent', 5432);
+RESET SESSION AUTHORIZATION;
+
+GRANT EXECUTE ON FUNCTION gp_add_master_standby(text, text, text) TO regress_standby_user;
+GRANT EXECUTE ON FUNCTION gp_add_master_standby(text, text, text, integer) TO regress_standby_user;
+SET SESSION AUTHORIZATION regress_standby_user;
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent');
+SELECT gp_add_master_standby('localhost', 'localhost', '/nonexistent', 5432);
+-- the privilege is checked before the arguments
+SELECT gp_add_master_standby(NULL, NULL, NULL);
+RESET SESSION AUTHORIZATION;
+
+-- clean up
+REVOKE EXECUTE ON FUNCTION gp_add_master_standby(text, text, text) FROM regress_standby_user;
+REVOKE EXECUTE ON FUNCTION gp_add_master_standby(text, text, text, integer) FROM regress_standby_user;
+DROP ROLE regress_standby_user;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org