pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sergey Shinderuk <s.shinderuk@postgrespro.ru>
To: Daniel Gustafsson <daniel@yesql.se>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Cc: Ivan Panchenko <wao@mail.ru>
Cc: Andres Freund <andres@anarazel.de>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Greg Nancarrow <gregn4422@gmail.com>
Cc: Teodor Sigaev <teodor@sigaev.ru>
Cc: Ibrar Ahmed <ibrar.ahmad@gmail.com>
Cc: vignesh C <vignesh21@gmail.com>
Cc: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Amit Kapila <amit.kapila16@gmail.com>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: a.sokolov@postgrespro.ru
Subject: Re: On login trigger: take three
Date: Tue, 20 Sep 2022 16:43:59 +0300
Message-ID: <66d83d70-ba5e-11a7-c5b6-a7ecddaf1281@postgrespro.ru> (raw)
In-Reply-To: <491AA22E-BA4A-46D0-852E-3784A8CC842E@yesql.se>
References: <2E4E472A-69F8-4EAC-B337-3341AC2F0188@yesql.se>
	<3378A73C-4441-43C7-B8F8-1B3611505524@yesql.se>
	<20220328213125.omlbcyzngqztxnyi@alap3.anarazel.de>
	<1648639288.568040808@f194.i.mail.ru>
	<B4DCB4BA-6103-4D86-8AD4-D62BEBE6837F@yesql.se>
	<fad05f5590d2b133af5eb81b728992e4@postgrespro.ru>
	<3A419032-B266-4DD8-A6E8-F1EB1048B435@yesql.se>
	<491AA22E-BA4A-46D0-852E-3784A8CC842E@yesql.se>

On 02.09.2022 18:36, Daniel Gustafsson wrote:
> This had bitrotted a fair bit, attached is a rebase along with (mostly)
> documentation fixes.  0001 adds a generic GUC for ignoring event triggers and
> 0002 adds the login event with event trigger support, and hooks it up to the
> GUC such that broken triggers wont require single-user mode.  Moving the CF
> entry back to Needs Review.


Hello!

There is a race around setting and clearing of dathasloginevt.

Steps to reproduce:

1. Create a trigger:

   CREATE FUNCTION on_login_proc() RETURNS event_trigger AS $$
   BEGIN
     RAISE NOTICE 'You are welcome!';
   END;
   $$ LANGUAGE plpgsql;

   CREATE EVENT TRIGGER on_login_trigger ON login EXECUTE PROCEDURE 
on_login_proc();

2. Then drop it, but don't start new sessions:

   DROP EVENT TRIGGER on_login_trigger;

3. Create another trigger, but don't commit yet:

   BEGIN;
   CREATE EVENT TRIGGER on_login_trigger ON login EXECUTE PROCEDURE 
on_login_proc();

4. Start a new session. This clears dathasloginevt.

5. Commit the transaction:

   COMMIT;

Now we have a trigger, but it doesn't fire, because dathasloginevt=false.


If two sessions create triggers concurrently, one of them will fail.

Steps:

1. In the first session, start a transaction and create a trigger:

   BEGIN;
   CREATE EVENT TRIGGER on_login_trigger1 ON login EXECUTE PROCEDURE 
on_login_proc();

2. In the second session, create another trigger (this query blocks):

   CREATE EVENT TRIGGER on_login_trigger2 ON login EXECUTE PROCEDURE 
on_login_proc();

3. Commit in the first session:

   COMMIT;

The second session fails:

   postgres=# CREATE EVENT TRIGGER on_login_trigger2 ON login EXECUTE 
PROCEDURE on_login_proc();
   ERROR:  tuple concurrently updated



What else bothers me is that login triggers execute in an environment 
under user control and one has to be very careful. The example trigger 
from the documentation

+DECLARE

+  hour integer = EXTRACT('hour' FROM current_time);

+  rec boolean;

+BEGIN

+-- 1. Forbid logging in between 2AM and 4AM.

+IF hour BETWEEN 2 AND 4 THEN

+  RAISE EXCEPTION 'Login forbidden';

+END IF;


can be bypassed with PGOPTIONS='-c timezone=...'. Probably this is 
nothing new and concerns any SECURITY DEFINER function, but still...


Best regards,

-- 
Sergey Shinderuk		https://postgrespro.com/





view thread (171+ messages)  latest in thread

Message-ID: <66d83d70-ba5e-11a7-c5b6-a7ecddaf1281@postgrespro.ru>
Permalink:  ../66d83d70-ba5e-11a7-c5b6-a7ecddaf1281@postgrespro.ru/
Also on:    postgresql.org/message-id/66d83d70-ba5e-11a7-c5b6-a7ecddaf1281@postgrespro.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: s.shinderuk@postgrespro.ru, daniel@yesql.se, wao@mail.ru, andres@anarazel.de, tgl@sss.pgh.pa.us, gregn4422@gmail.com, teodor@sigaev.ru, ibrar.ahmad@gmail.com, vignesh21@gmail.com, pavel.stehule@gmail.com, amit.kapila16@gmail.com, sawada.mshk@gmail.com, a.sokolov@postgrespro.ru
  Subject: Re: On login trigger: take three
  In-Reply-To: <66d83d70-ba5e-11a7-c5b6-a7ecddaf1281@postgrespro.ru>
  References: <2E4E472A-69F8-4EAC-B337-3341AC2F0188@yesql.se> <3378A73C-4441-43C7-B8F8-1B3611505524@yesql.se> <20220328213125.omlbcyzngqztxnyi@alap3.anarazel.de> <1648639288.568040808@f194.i.mail.ru> <B4DCB4BA-6103-4D86-8AD4-D62BEBE6837F@yesql.se> <fad05f5590d2b133af5eb81b728992e4@postgrespro.ru> <3A419032-B266-4DD8-A6E8-F1EB1048B435@yesql.se> <491AA22E-BA4A-46D0-852E-3784A8CC842E@yesql.se> <66d83d70-ba5e-11a7-c5b6-a7ecddaf1281@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox