Re: Allow non-superuser to cancel superuser tasks.
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: Nathan Bossart <nathandbossart@gmail.com>
Cc: Alexander Kukushkin <cyberdemn@gmail.com>, "Andrey M. Borodin" <x4mmm@yandex-team.ru>,
Michael Paquier <michael@paquier.xyz>, Kirill Reshke <reshkekirill@gmail.com>, "Leung,
Anthony" <antholeu@amazon.com>, "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>
Date: 2024-11-22T23:13:16Z
Lists: pgsql-hackers
Hi,
On 2024-11-22 13:21:53 -0600, Nathan Bossart wrote:
> Here is a draft-grade patch for this one. It seems pretty
> straightforward...
Thanks for looking into it quickly.
> diff --git a/src/backend/storage/ipc/signalfuncs.c b/src/backend/storage/ipc/signalfuncs.c
> index aa729a36e3..8e165e9729 100644
> --- a/src/backend/storage/ipc/signalfuncs.c
> +++ b/src/backend/storage/ipc/signalfuncs.c
> @@ -87,10 +87,7 @@ pg_signal_backend(int pid, int sig)
> */
> if (!OidIsValid(proc->roleId) || superuser_arg(proc->roleId))
> {
> - ProcNumber procNumber = GetNumberFromPGProc(proc);
> - PgBackendStatus *procStatus = pgstat_get_beentry_by_proc_number(procNumber);
> -
> - if (procStatus && procStatus->st_backendType == B_AUTOVAC_WORKER)
> + if (pgstat_get_backend_type(pid) == B_AUTOVAC_WORKER)
> {
> if (!has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_AUTOVACUUM_WORKER))
> return SIGNAL_BACKEND_NOAUTOVAC;
Because we already mapped the pid to a ProcNumber, it'd be cheaper to access
the backend status via procnumber.
> +BackendType
> +pgstat_get_backend_type(int pid)
> +{
> + PgBackendStatus *beentry = BackendStatusArray;
> +
> + for (int i = 1; i <= MaxBackends; i++)
> + {
> + volatile PgBackendStatus *vbeentry = beentry;
> + bool found;
> +
> + for (;;)
> + {
> + int before_changecount;
> + int after_changecount;
> +
> + pgstat_begin_read_activity(vbeentry, before_changecount);
> +
> + found = (vbeentry->st_procpid == pid);
> +
> + pgstat_end_read_activity(vbeentry, after_changecount);
We don't need the pgstat_begin_read_activity() protocol when just accessing a
single 4 byte value - we assume in lots of places that can be read in a
non-tearable way.
> + if (pgstat_read_activity_complete(before_changecount,
> + after_changecount))
> + break;
> +
> + CHECK_FOR_INTERRUPTS();
> + }
> +
> + if (found)
> + return beentry->st_backendType;
But if we were to follow it, we'd certainly need to use it here too.
Greetings,
Andres Freund
Commits
-
Look up backend type in pg_signal_backend() more cheaply.
- 61171a632d10 18.0 landed
-
Add tap test for pg_signal_autovacuum role
- d2b74882cab8 18.0 landed
-
Introduce pg_signal_autovacuum_worker.
- ccd38024bc3c 18.0 landed
-
Add a slot synchronization function.
- ddd5f4f54a02 17.0 cited
-
Ban role pg_signal_backend from more superuser backend types.
- 3a9b18b30953 17.0 cited