Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oYAZU-0003i6-Oz for pgsql-general@arkaria.postgresql.org; Tue, 13 Sep 2022 18:23:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oYAZS-0006Y9-D9 for pgsql-general@arkaria.postgresql.org; Tue, 13 Sep 2022 18:23:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oYAZR-0006Xy-UW for pgsql-general@lists.postgresql.org; Tue, 13 Sep 2022 18:23:42 +0000 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oYAZK-00064K-5r for pgsql-general@lists.postgresql.org; Tue, 13 Sep 2022 18:23:40 +0000 Received: by mail-pg1-x52a.google.com with SMTP id s206so12115621pgs.3 for ; Tue, 13 Sep 2022 11:23:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date; bh=GpB2ynaNaVbdx68v6/vC2b5vMN+cZhYe60zXX9yxa9E=; b=Dl7EVdZJ8M1V1CQfLX7n9vTr5USFVw+Dp3019E+7EnkbGe38XFnGN8026VpAEDL/ut VWg8t+1TjFuJOKDjNzI/z2q/nj7qim1U+63Ien+VfoYrQo/a1N5MyXXo1Cz/cIvLZX2O YDJSUdOj7EI4MmiM6Xkk7gVmgmc81RokZxt3g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date; bh=GpB2ynaNaVbdx68v6/vC2b5vMN+cZhYe60zXX9yxa9E=; b=VpmRQ5lAVRL8lr/zkZwCOW0aj7aq0jW1vUVSEiXYVYp/rx1wvkyEecUtnVId7j2bi7 UPH6rqaP3fV+A+W7GJv2K96uMgMU+QTsgqw958vt8kqoaipYB+muW6rozjOpnkqUseB3 LZzXy2gvC76p9p1C52r9pLzdCwUfbKb9AuRv77Ziqx8tgeyKqe88dlrNepJCwLgI0Scf o2FAKYegT+c/EBbOuMrh3qj6F8Vq/SczFEW/rCPv1PVL+InnkUTOFFOjsmg0j0ZCfpxx YAl6QIKs0YC6lXPOF3OPJbLBlhGvJIIYnM+DiTFa6819DO+JnqkHXrjlELL9dUgqM0RC sdbA== X-Gm-Message-State: ACgBeo3AJE8yjZ7ZQspNuorg9NKU9VKA/Acf5F/eeXVWDwFhxNXWx7j8 m/hy+Fu+TMPAzBTj3H4WP/IxNQ== X-Google-Smtp-Source: AA6agR65FDtCTiLKX/YC7TImaHM7GpqKjYf+5N81J3pe2DJGzbv7uQfxyJFd83Bfj1F+w395JgjShw== X-Received: by 2002:a05:6a00:1781:b0:53a:8572:4453 with SMTP id s1-20020a056a00178100b0053a85724453mr34230634pfg.76.1663093411725; Tue, 13 Sep 2022 11:23:31 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:f8e9:3bf8:af26:c8ff]) by smtp.gmail.com with ESMTPSA id j6-20020a170903024600b00177e590df96sm8773298plh.118.2022.09.13.11.23.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Sep 2022 11:23:31 -0700 (PDT) From: Bryn Llewellyn Message-Id: <0CAFB5D3-8EB2-4EE1-A50B-7CBFCB7D558D@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_EF21678F-CEEE-46A6-880C-A989E2085103" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Is it possible to stop sessions killing eachother when they all authorize as the same role? Date: Tue, 13 Sep 2022 11:23:29 -0700 In-Reply-To: <1883844.1663044542@sss.pgh.pa.us> Cc: "David G. Johnston" , pgsql-general list To: Tom Lane PostgreSQL References: <10F360BB-3149-45E6-BFFE-10B9AE31F1A6@yugabyte.com> <2F078227-5EEF-4553-89FF-046A6AF12F25@yugabyte.com> <1883844.1663044542@sss.pgh.pa.us> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_EF21678F-CEEE-46A6-880C-A989E2085103 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > tgl@sss.pgh.pa.us wrote: >=20 >> bryn@yugabyte.com wrote: >>=20 >> My non-superuser normalrole with direct login, "u1", is *still* able = to invoke pg_terminate_backend() and kill other "u1" sessions=E2=80=94even= after this (as a super-user): >=20 > Really? I did this in 14.5: >=20 > revoke execute on function pg_terminate_backend from public; > select proacl from pg_proc where proname =3D 'pg_terminate_backend'; >=20 > proacl =20 > ----------------------- > {postgres=3DX/postgres} >=20 > (as expected, the superuser's own execute permission is all that = remains) >=20 > create user joe; > \c - joe > select pg_terminate_backend(42); >=20 > ERROR: permission denied for function pg_terminate_backend >=20 >> It very much looks as if what I have described was deemed to be a bug = (after that behavior had survived from at least version 11) and that = it's now been fixed! >=20 > No, it very much looks like pilot error. But you've not shown us = exactly what your test consisted of, so it's hard to say just where it = went off the rails. I'll be delighted to learn what error I've been making and how to avoid = it in future. I copied my testcase at the end. I also tried it in an old = PG Version 11 that I have in an Ubuntu VM. I got the same outcome there = as I do in Version 14.5 on macOS. (I had to make s small change because, = back then, the signature was just "pg_terminate_backend(int)"=E2=80=94with= out the second "bigint" formal argument.) There must be some-or-other non-standard setting in my environment that = results in the behavior that I see and that other's don't. Notice that = following this: -- Self-document what seems to be the default. grant execute on function pg_terminate_backend(int, bigint) to public; select proacl from pg_proc where proname =3D 'pg_terminate_backend'; I see this: proacl =20 ----------------------------------- {Bllewell=3DX/Bllewell,=3DX/Bllewell} "Bllewell" owns the "postgres" database, the templates, and all the = schemas like "pg_catalog" and "information_schema" that come with a = freshly-created database. Then, later, following this: -- Hardening attempt. revoke execute on function pg_catalog.pg_terminate_backend(int, bigint) = from public; revoke execute on function pg_catalog.pg_terminate_backend(int, bigint) = from r1; select proacl from pg_proc where proname =3D 'pg_terminate_backend'; I see this: proacl =20 ----------------------- {Bllewell=3DX/Bllewell} It seems to be a strange way to report the fact that *any* superuser = inevitably is unstoppable while I have two of these: "Bllewell" and = "postgres"=E2=80=94both of which came with the installation. This anyway annoys me. Is it inevitable on macOS? If not, would it help = to remove my present installation without trace and to make a new one = from scratch? Notice, though, that my Ubuntu installation has no = superuser that matches the OS owner of the installation. There, it's = just "postgres". = --------------------------------------------------------------------------= ------ \c postgres postgres set client_min_messages =3D warning; ------------------------------------------------------------ -- Setup drop schema if exists pulic; drop database if exists play; drop role if exists r1; -- Self-document what seems to be the default. grant execute on function pg_terminate_backend(int, bigint) to public; -- Check the starting state. select proacl from pg_proc where proname =3D 'pg_terminate_backend'; select rolname, rolsuper, rolcanlogin::text from pg_roles where rolname !~ '^pg_' order by rolname; select datname from pg_database where not datistemplate; select count(*) as "nof. ordinary schemas" from pg_namespace where not ( nspname ~ '^pg_' or nspname =3D 'information_schema'); /* RESULTS ------- proacl =20 ----------------------------------- {Bllewell=3DX/Bllewell,=3DX/Bllewell} rolname | rolsuper | rolcanlogin=20 ----------+----------+------------- Bllewell | t | false postgres | t | true datname =20 ---------- postgres nof. ordinary schemas=20 ----------------------- 0 */; -- Setup create database play owner postgres; revoke all on database play from public; create role r1 with login password 'p'; grant connect on database play to r1; ------------------------------------------------------------ -- RED SESSION \c play r1 -- Reports one row. select datname, usename, pid from pg_stat_activity where backend_type =3D 'client backend' order by 1, 2, 3; ------------------------------------------------------------ -- BLUE SESSION \c play r1 -- Reports two rows. select datname, usename, pid from pg_stat_activity where backend_type =3D 'client backend' order by 1, 2, 3; -- No error do $body$ declare p int not null :=3D 0; begin for p in ( select pid from pg_stat_activity where backend_type =3D 'client backend' and pid <> pg_backend_pid()) loop perform pg_terminate_backend(p); end loop; end; $body$; -- Now reports just one row. select datname, usename, pid from pg_stat_activity where backend_type =3D 'client backend' order by 1, 2, 3; ------------------------------------------------------------ -- RED SESSION (don't re-connect) -- Causes FATAL: terminating connection due to administrator command select 1; ------------------------------------------------------------ \c postgres postgres -- Hardening attempt. revoke execute on function pg_catalog.pg_terminate_backend(int, bigint) = from public; revoke execute on function pg_catalog.pg_terminate_backend(int, bigint) = from r1; =E2=80=94 Shows "{Bllewell=3DX/Bllewell}" select proacl from pg_proc where proname =3D 'pg_terminate_backend'; ------------------------------------------------------------ -- BLUE SESSION \c play r1 -- Reports one row. select datname, usename, pid from pg_stat_activity where backend_type =3D 'client backend' order by 1, 2, 3; ------------------------------------------------------------ -- RED SESSION \c play r1 -- Reports two rows. select datname, usename, pid from pg_stat_activity where backend_type =3D 'client backend' order by 1, 2, 3; -- No error do $body$ declare p int not null :=3D 0; begin for p in ( select pid from pg_stat_activity where backend_type =3D 'client backend' and pid <> pg_backend_pid()) loop perform pg_terminate_backend(p); end loop; end; $body$; -- Now reports just one row. select datname, usename, pid from pg_stat_activity where backend_type =3D 'client backend' order by 1, 2, 3; ------------------------------------------------------------ -- BLUE SESSION (don't re-connect) -- Causes FATAL: terminating connection due to administrator command select 1; --Apple-Mail=_EF21678F-CEEE-46A6-880C-A989E2085103 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
tgl@sss.pgh.pa.us wrote:

bryn@yugabyte.com = wrote:

My  non-superuser normalrole = with direct login, "u1", is *still* able to invoke = pg_terminate_backend() and kill other "u1" sessions=E2=80=94even after = this (as a super-user):

Really? I did this in 14.5:

revoke execute on function = pg_terminate_backend from public;
select proacl from = pg_proc where proname =3D 'pg_terminate_backend';

       proacl =         
-----------------------{postgres=3DX/postgres}

(as = expected, the superuser's own execute permission is all that remains)

create user joe;
\c - joe
select pg_terminate_backend(42);

ERROR:  permission denied for function = pg_terminate_backend

It very much looks as if what I have described = was deemed to be a bug (after that behavior had survived from at least = version 11) and that it's now been fixed!

No, it very much = looks like pilot error. But you've not shown us exactly what = your test consisted of, so it's hard to say just where it went off = the rails.

I'll be delighted to learn what error = I've been making and how to avoid it in future. I copied my testcase at = the end. I also tried it in an old PG Version 11 that I have in an = Ubuntu VM. I got the same outcome there as I do in Version 14.5 on = macOS. (I had to make s small change because, back then, the signature = was just "pg_terminate_backend(int)"=E2=80=94without the second "bigint" = formal argument.)

There must be some-or-other non-standard setting in my = environment that results in the behavior that I see and that other's = don't. Notice that following this:

-- Self-document what seems to be the = default.
grant execute on function = pg_terminate_backend(int, bigint) to public;
select proacl = from pg_proc where proname =3D 'pg_terminate_backend';

I see = this:

        =       proacl          =      
-----------------------------------
 {Bllewell=3DX/Bllewell,=3DX/Bllewell}

"Bllewell" owns the "postgres" database, the templates, and = all the schemas like "pg_catalog" and "information_schema" that come = with a freshly-created database. Then, later, following this:

-- Hardening attempt.
revoke = execute on function pg_catalog.pg_terminate_backend(int, bigint) from = public;
revoke execute on function = pg_catalog.pg_terminate_backend(int, bigint) from r1;
select= proacl from pg_proc where proname =3D 'pg_terminate_backend';


I see this:

        proacl      =    
-----------------------
 {Bllewell=3DX/Bllewell}

It seems to be a strange = way to report the fact that *any* superuser inevitably is unstoppable = while I have two of these: "Bllewell" and "postgres"=E2=80=94both of = which came with the installation.

This anyway annoys me. Is it inevitable = on macOS? If not, would it help to remove my present installation = without trace and to make a new one from scratch? Notice, though, that = my Ubuntu installation has no superuser that matches the OS owner of the = installation. There, it's just "postgres".

--------------------------------------------------------------------------------

\c postgres postgres
set = client_min_messages =3D warning;
-------------------------------------------------------------- Setup
drop schema if exists pulic;
drop database if exists play;
drop role if = exists r1;

-- Self-document what seems to = be the default.
grant execute on function = pg_terminate_backend(int, bigint) to public;

-- Check the starting state.
select proacl from pg_proc where = proname =3D 'pg_terminate_backend';

select rolname, = rolsuper, rolcanlogin::text
from pg_roles
where rolname !~ '^pg_'
order by = rolname;

select datname
from pg_database
where not = datistemplate;

select count(*) as "nof. ordinary = schemas"
from pg_namespace
where not (
  nspname= ~ '^pg_' or
  nspname =3D 'information_schema');

/*
 RESULTS
 
-------
              proacl =               
-----------------------------------
 {Bllewell=3DX/Bllewell,=3DX/Bllewell}

 rolname  | rolsuper | = rolcanlogin 
----------+----------+-------------
 Bllewell | t    =     | false
 postgres | t        | = true

 datname  
----------
 postgres

 nof. ordinary = schemas 
-----------------------
          =            0
*/;

-- = Setup
create database play owner postgres;
revoke all on database play from = public;
create role r1 with login password 'p';
grant connect on = database play to r1;
------------------------------------------------------------
-- RED = SESSION
\c play r1

-- Reports one row.
select datname, = usename, pid
from pg_stat_activity
where backend_type =3D 'client = backend'
order by 1, 2, 3;
------------------------------------------------------------
-- BLUE = SESSION
\c play r1

-- Reports two rows.
select datname, = usename, pid
from pg_stat_activity
where backend_type =3D 'client = backend'
order by 1, 2, 3;

-- No error
do = $body$
declare
  p int not null :=3D 0;
begin
  for p in (
  =   select pid
    from pg_stat_activity
  =   where backend_type =3D  'client backend'
  =   and   pid        =   <> pg_backend_pid())
  loop
  =   perform pg_terminate_backend(p);
  end loop;
end;
$body$;

-- = Now reports just one row.
select datname, usename, pid
from pg_stat_activity
where = backend_type =3D 'client backend'
order by 1, 2, 3;
------------------------------------------------------------
-- RED = SESSION (don't re-connect)

-- Causes FATAL: terminating = connection due to administrator command
select 1;
------------------------------------------------------------
\c = postgres postgres

-- Hardening attempt.
revoke execute on = function pg_catalog.pg_terminate_backend(int, bigint) from = public;
revoke execute on function = pg_catalog.pg_terminate_backend(int, bigint) from r1;

=E2=80=94 Shows = "{Bllewell=3DX/Bllewell}"
select proacl from pg_proc where proname =3D = 'pg_terminate_backend';
------------------------------------------------------------
-- BLUE = SESSION
\c play r1

-- Reports one row.
select datname, = usename, pid
from pg_stat_activity
where backend_type =3D 'client = backend'
order by 1, 2, 3;
------------------------------------------------------------
-- RED = SESSION
\c play r1

-- Reports two rows.
select datname, = usename, pid
from pg_stat_activity
where backend_type =3D 'client = backend'
order by 1, 2, 3;

-- No error
do = $body$
declare
  p int not null :=3D 0;
begin
  for p in (
  =   select pid
    from pg_stat_activity
  =   where backend_type =3D  'client backend'
  =   and   pid        =   <> pg_backend_pid())
  loop
  =   perform pg_terminate_backend(p);
  end loop;
end;
$body$;

-- = Now reports just one row.
select datname, usename, pid
from pg_stat_activity
where = backend_type =3D 'client backend'
order by 1, 2, 3;
------------------------------------------------------------
-- BLUE = SESSION (don't re-connect)

-- Causes FATAL: terminating = connection due to administrator command
select 1;

= --Apple-Mail=_EF21678F-CEEE-46A6-880C-A989E2085103--