Skip to content

Commit

Permalink
[#14419] YSQL: import Ban role pg_signal_backend from more superuser …
Browse files Browse the repository at this point in the history
…backend types.

Summary:
Import upstream postgres commit e082734c8e78e6622a0422e612a870278721e83f from REL_11_STABLE.
This is needed as a mitigation for CVE-2023-5870.

Original commit message:
```
    Ban role pg_signal_backend from more superuser backend types.

    Documentation says it cannot signal "a backend owned by a superuser".
    On the contrary, it could signal background workers, including the
    logical replication launcher.  It could signal autovacuum workers and
    the autovacuum launcher.  Block all that.  Signaling autovacuum workers
    and those two launchers doesn't stall progress beyond what one could
    achieve other ways.  If a cluster uses a non-core extension with a
    background worker that does not auto-restart, this could create a denial
    of service with respect to that background worker.  A background worker
    with bugs in its code for responding to terminations or cancellations
    could experience those bugs at a time the pg_signal_backend member
    chooses.  Back-patch to v11 (all supported versions).

    Reviewed by Jelte Fennema-Nio.  Reported by Hemanth Sandrana and
    Mahendrakar Srinivasarao.

    Security: CVE-2023-5870
```
Jira: DB-3834

Test Plan:
  ./yb_build.sh --java-test org.yb.pgsql.TestPgRegressAuthorization#testPgRegressAuthorization

Reviewers: jason, skumar

Reviewed By: jason

Subscribers: jason, yql

Differential Revision: https://phorge.dev.yugabyte.com/D32400
  • Loading branch information
Devansh Saxena committed Jul 11, 2024
1 parent 15c8cfb commit 0a73bdd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/postgres/src/backend/utils/adt/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,13 @@ pg_signal_backend(int pid, int sig)
return SIGNAL_BACKEND_ERROR;
}

/* Only allow superusers to signal superuser-owned backends. */
if (superuser_arg(proc->roleId) && !superuser())
/*
* Only allow superusers to signal superuser-owned backends. Any process
* not advertising a role might have the importance of a superuser-owned
* backend, so treat it that way.
*/
if ((!OidIsValid(proc->roleId) || superuser_arg(proc->roleId)) &&
!superuser())
return SIGNAL_BACKEND_NOSUPERUSER;

/* Users can signal backends they have role membership in. */
Expand Down
18 changes: 18 additions & 0 deletions src/postgres/src/test/regress/expected/privileges.out
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,24 @@ SELECT * FROM pg_largeobject LIMIT 0;
SET SESSION AUTHORIZATION regress_priv_user1;
SELECT * FROM pg_largeobject LIMIT 0; -- to be denied
ERROR: permission denied for table pg_largeobject
-- pg_signal_backend can't signal superusers
RESET SESSION AUTHORIZATION;
BEGIN;
CREATE OR REPLACE FUNCTION terminate_nothrow(pid int) RETURNS bool
LANGUAGE plpgsql SECURITY DEFINER SET client_min_messages = error AS $$
BEGIN
RETURN pg_terminate_backend($1);
EXCEPTION WHEN OTHERS THEN
RETURN false;
END$$;
ALTER FUNCTION terminate_nothrow OWNER TO pg_signal_backend;
SELECT backend_type FROM pg_stat_activity
WHERE CASE WHEN COALESCE(usesysid, 10) = 10 THEN terminate_nothrow(pid) END;
backend_type
--------------
(0 rows)

ROLLBACK;
-- test default ACLs
\c -
CREATE SCHEMA testns;
Expand Down
18 changes: 18 additions & 0 deletions src/postgres/src/test/regress/expected/yb_pg_privileges.out
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,24 @@ SELECT * FROM pg_largeobject LIMIT 0;
SET SESSION AUTHORIZATION regress_priv_user1;
SELECT * FROM pg_largeobject LIMIT 0; -- to be denied
ERROR: permission denied for table pg_largeobject
-- pg_signal_backend can't signal superusers
RESET SESSION AUTHORIZATION;
BEGIN;
CREATE OR REPLACE FUNCTION terminate_nothrow(pid int) RETURNS bool
LANGUAGE plpgsql SECURITY DEFINER SET client_min_messages = error AS $$
BEGIN
RETURN pg_terminate_backend($1);
EXCEPTION WHEN OTHERS THEN
RETURN false;
END$$;
ALTER FUNCTION terminate_nothrow OWNER TO pg_signal_backend;
SELECT backend_type FROM pg_stat_activity
WHERE CASE WHEN COALESCE(usesysid, 10) = 10 THEN terminate_nothrow(pid) END;
backend_type
--------------
(0 rows)

ROLLBACK;
-- test default ACLs
\c -
CREATE SCHEMA testns;
Expand Down
15 changes: 15 additions & 0 deletions src/postgres/src/test/regress/sql/privileges.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,21 @@ SELECT * FROM pg_largeobject LIMIT 0;
SET SESSION AUTHORIZATION regress_priv_user1;
SELECT * FROM pg_largeobject LIMIT 0; -- to be denied

-- pg_signal_backend can't signal superusers
RESET SESSION AUTHORIZATION;
BEGIN;
CREATE OR REPLACE FUNCTION terminate_nothrow(pid int) RETURNS bool
LANGUAGE plpgsql SECURITY DEFINER SET client_min_messages = error AS $$
BEGIN
RETURN pg_terminate_backend($1);
EXCEPTION WHEN OTHERS THEN
RETURN false;
END$$;
ALTER FUNCTION terminate_nothrow OWNER TO pg_signal_backend;
SELECT backend_type FROM pg_stat_activity
WHERE CASE WHEN COALESCE(usesysid, 10) = 10 THEN terminate_nothrow(pid) END;
ROLLBACK;

-- test default ACLs
\c -

Expand Down
15 changes: 15 additions & 0 deletions src/postgres/src/test/regress/sql/yb_pg_privileges.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,21 @@ SELECT * FROM pg_largeobject LIMIT 0;
SET SESSION AUTHORIZATION regress_priv_user1;
SELECT * FROM pg_largeobject LIMIT 0; -- to be denied

-- pg_signal_backend can't signal superusers
RESET SESSION AUTHORIZATION;
BEGIN;
CREATE OR REPLACE FUNCTION terminate_nothrow(pid int) RETURNS bool
LANGUAGE plpgsql SECURITY DEFINER SET client_min_messages = error AS $$
BEGIN
RETURN pg_terminate_backend($1);
EXCEPTION WHEN OTHERS THEN
RETURN false;
END$$;
ALTER FUNCTION terminate_nothrow OWNER TO pg_signal_backend;
SELECT backend_type FROM pg_stat_activity
WHERE CASE WHEN COALESCE(usesysid, 10) = 10 THEN terminate_nothrow(pid) END;
ROLLBACK;

-- test default ACLs
\c -

Expand Down

0 comments on commit 0a73bdd

Please sign in to comment.