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 1oadXi-00084W-Qv for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Sep 2022 13:44:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oadXh-0000PI-IL for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Sep 2022 13:44:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oadXh-0000P8-1c for pgsql-hackers@lists.postgresql.org; Tue, 20 Sep 2022 13:44:05 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oadXd-0007Y7-KN for pgsql-hackers@postgresql.org; Tue, 20 Sep 2022 13:44:03 +0000 Received: from [192.168.28.82] (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id B217521C3D40; Tue, 20 Sep 2022 16:43:59 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1663681440; bh=hEQrzCmZK/HxE+z+bDGFj7spObgmX9MDxtx+Virf4H4=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=V1FTx3ApqtojrtzCnBskDcjofS+DXtfzwNpXVCkFToMIWUgJaGXEIc2V/RpD20UGp FS7WJsgeXoPJXXKGejVEJH5DnblLkN+5Mw1P+3LWAs2Og1+EgCUV7ORCyz+JbbGO/S H0kBn1ZPlisWglII7rALHQraTnvdmm472ADrK1i0= Message-ID: <66d83d70-ba5e-11a7-c5b6-a7ecddaf1281@postgrespro.ru> Date: Tue, 20 Sep 2022 16:43:59 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: On login trigger: take three Content-Language: en-US To: Daniel Gustafsson , PostgreSQL Hackers Cc: Ivan Panchenko , Andres Freund , Tom Lane , Greg Nancarrow , Teodor Sigaev , Ibrar Ahmed , vignesh C , Pavel Stehule , Amit Kapila , Masahiko Sawada , a.sokolov@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> <3A419032-B266-4DD8-A6E8-F1EB1048B435@yesql.se> <491AA22E-BA4A-46D0-852E-3784A8CC842E@yesql.se> From: Sergey Shinderuk In-Reply-To: <491AA22E-BA4A-46D0-852E-3784A8CC842E@yesql.se> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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/