Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qqQ5Q-00A0Jx-W1 for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Oct 2023 03:40:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qqQ5O-00CMcP-TY for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Oct 2023 03:40:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qqQ5N-00CMP4-Vi for pgsql-hackers@lists.postgresql.org; Wed, 11 Oct 2023 03:40:39 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qqQ5L-0013US-Kv for pgsql-hackers@lists.postgresql.org; Wed, 11 Oct 2023 03:40:37 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 95DDD5C02AB; Tue, 10 Oct 2023 23:40:33 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 10 Oct 2023 23:40:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm2; t=1696995633; x=1697082033; bh=iQ Fo5HhBxLczr1oLkp4L+m5IfVI021gv3E9PXk+lqV0=; b=Je1+1UuAlXqOLAJVGG k8zi7h3pBcUGWxMxpF2G/BW9LITrRe6VT0u3xT9kMx6DnMhqPOpxKpDJMjL/d5mK hGneJc7AtiZV3kqWpFOZI62NS5eFC+Dv4h4bnB28rraY945Ju3VwBpSersrCrkDt UeVyXKHI5qHMKDTcC8DfZp7vu+wTLBSoMW1n+mD03ayjqGL6XkX0L36RQhqpc9mm imETPFwQXZ4dsBca7pK8qZoBFSbA7batYtfCATk5WSp+DQDZ+kvaXmqxxqwnwrq1 nsYm7tvvXo+QzAt39BXMSQ0tepm6eEXqnYUxjr9aQxshUxAqs/2mfPAdarwsz5La Xt0A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; t=1696995633; x=1697082033; bh=iQFo5HhBxLczr 1oLkp4L+m5IfVI021gv3E9PXk+lqV0=; b=MlLz4FvXdYB73/id6X5pKJDcmtPrO XeIdKyHZO+N6eon6mA647k4WnCiifVFlszegScTqMJIwm4jbXBgXtdAeuJ25cdm5 jKYtkm4+rTjnZcvOXoz9nwY09BQiy1X9NUK/pkS4Nm9EPZhMZ1wimiF6zHDsZpLU dkLIlDgdfkgF75oY5lVzJ5qHbrwMPhOV9SkJb/h4dUMN2gFqJqwh6/FlHfjDtYb6 fVLAL9wMs5gt6Z/lAoSFSZCGPMym2WrSXve1xfPatyQm7IWF+x1vtqkcHK8NWX37 XzP+Mks4sp0Z+LeJYGEPXM93Z9V/dzpQGNn068zmIyQ094RzBuEY3lcMg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrheejgdefvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculdejtddmnecujfgurhepfffhvfevuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpeetleeifedufffhhfdtteelgeeg geffhfekueevteeigfduudevudetgfegiedvjeenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghquhhivghrrdighiii X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 10 Oct 2023 23:40:31 -0400 (EDT) Date: Wed, 11 Oct 2023 12:40:28 +0900 From: Michael Paquier To: "Drouvot, Bertrand" Cc: Bharath Rupireddy , PostgreSQL Hackers Subject: Re: Add a new BGWORKER_BYPASS_ROLELOGINCHECK flag Message-ID: References: <26e7cd6a-6dd1-4b08-975b-62603eeb6f3f@gmail.com> <06124373-5fdd-4925-9424-9d036d481f3b@gmail.com> <590ec36a-13cb-4253-934b-b6a220d1026e@gmail.com> <2b530754-56c0-4272-a326-11d30a0bca55@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="lQxR8Z3b5yIxlxsv" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --lQxR8Z3b5yIxlxsv Content-Type: multipart/mixed; boundary="NCpCPdh8mcjW1d9K" Content-Disposition: inline --NCpCPdh8mcjW1d9K Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Oct 11, 2023 at 08:26:42AM +0900, Michael Paquier wrote: > On Tue, Oct 10, 2023 at 09:12:49AM +0200, Drouvot, Bertrand wrote: > > On 10/10/23 7:58 AM, Michael Paquier wrote: > >> I was looking at v8 just before you sent this v9, and still got > >> annoyed by the extra boolean argument added to InitPostgres(). > >=20 > > Arf, I did not look at it as I had in mind to look at it once > > this one is in. >=20 > No problem. I'm OK to do it. Applied 0001 for now. > I am not sure that this is necessary in the code paths of > BackgroundWorkerInitializeConnectionByOid() and > BackgroundWorkerInitializeConnection() as datallowconn is handled a > few lines down. /* flags for InitPostgres() */ #define INIT_PG_LOAD_SESSION_LIBS 0x0001 #define INIT_PG_OVERRIDE_ALLOW_CONNS 0x0002 +#define INIT_PG_BYPASS_ROLE_LOGIN 0x0004 In 0002, I am not sure that this is the best name for this new flag. There is consistency with the bgworker part, for sure, but shouldn't we name that OVERRIDE_ROLE_LOGIN instead in miscadmin.h? -- Michael --NCpCPdh8mcjW1d9K Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v11-0002-Allow-background-workers-to-bypass-login-check.patch" Content-Transfer-Encoding: quoted-printable =46rom 550019a0ec84fcde8588bb9c2ae4ab08ad4d5fa5 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Tue, 10 Oct 2023 14:56:36 +0900 Subject: [PATCH v11] Allow background workers to bypass login check This adds a new flag value (BGWORKER_BYPASS_ROLELOGINCHECK) to the flags being used in BackgroundWorkerInitializeConnection() and BackgroundWorkerInitializeConnectionByOid(). This flag allows the background workers to bypass the login check done in InitializeSessionUserId(). --- src/include/miscadmin.h | 4 ++- src/include/postmaster/bgworker.h | 6 ++-- src/backend/postmaster/postmaster.c | 6 ++++ src/backend/utils/init/miscinit.c | 4 +-- src/backend/utils/init/postinit.c | 6 ++-- .../modules/worker_spi/t/001_worker_spi.pl | 31 +++++++++++++++++++ src/test/modules/worker_spi/worker_spi.c | 2 ++ doc/src/sgml/bgworker.sgml | 4 ++- 8 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index 1cc3712c0f..e280b8192d 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -364,7 +364,8 @@ extern bool InSecurityRestrictedOperation(void); extern bool InNoForceRLSOperation(void); extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context); extern void SetUserIdAndContext(Oid userid, bool sec_def_context); -extern void InitializeSessionUserId(const char *rolename, Oid roleid); +extern void InitializeSessionUserId(const char *rolename, Oid roleid, + bool bypass_login_check); extern void InitializeSessionUserIdStandalone(void); extern void SetSessionAuthorization(Oid userid, bool is_superuser); extern Oid GetCurrentRoleId(void); @@ -466,6 +467,7 @@ extern PGDLLIMPORT AuxProcType MyAuxProcType; /* flags for InitPostgres() */ #define INIT_PG_LOAD_SESSION_LIBS 0x0001 #define INIT_PG_OVERRIDE_ALLOW_CONNS 0x0002 +#define INIT_PG_BYPASS_ROLE_LOGIN 0x0004 extern void pg_split_opts(char **argv, int *argcp, const char *optstr); extern void InitializeMaxBackends(void); extern void InitPostgres(const char *in_dbname, Oid dboid, diff --git a/src/include/postmaster/bgworker.h b/src/include/postmaster/bgw= orker.h index 7815507e3d..d7a5c1a946 100644 --- a/src/include/postmaster/bgworker.h +++ b/src/include/postmaster/bgworker.h @@ -150,9 +150,11 @@ extern void BackgroundWorkerInitializeConnectionByOid(= Oid dboid, Oid useroid, ui * Flags to BackgroundWorkerInitializeConnection et al * * - * Allow bypassing datallowconn restrictions when connecting to database + * Allow bypassing datallowconn restrictions and login check when connecti= ng + * to database */ -#define BGWORKER_BYPASS_ALLOWCONN 1 +#define BGWORKER_BYPASS_ALLOWCONN 0x0001 +#define BGWORKER_BYPASS_ROLELOGINCHECK 0x0002 =20 =20 /* Block/unblock signals in a background worker process */ diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/p= ostmaster.c index 3d7fec995a..b1cabbd0d6 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -5567,6 +5567,9 @@ BackgroundWorkerInitializeConnection(const char *dbna= me, const char *username, u /* ignore datallowconn? */ if (flags & BGWORKER_BYPASS_ALLOWCONN) init_flags |=3D INIT_PG_OVERRIDE_ALLOW_CONNS; + /* bypass login check? */ + if (flags & BGWORKER_BYPASS_ROLELOGINCHECK) + init_flags |=3D INIT_PG_BYPASS_ROLE_LOGIN; =20 /* XXX is this the right errcode? */ if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)) @@ -5598,6 +5601,9 @@ BackgroundWorkerInitializeConnectionByOid(Oid dboid, = Oid useroid, uint32 flags) /* ignore datallowconn? */ if (flags & BGWORKER_BYPASS_ALLOWCONN) init_flags |=3D INIT_PG_OVERRIDE_ALLOW_CONNS; + /* bypass login check? */ + if (flags & BGWORKER_BYPASS_ROLELOGINCHECK) + init_flags |=3D INIT_PG_BYPASS_ROLE_LOGIN; =20 /* XXX is this the right errcode? */ if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)) diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/mis= cinit.c index 1e671c560c..182d666852 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -725,7 +725,7 @@ has_rolreplication(Oid roleid) * Initialize user identity during normal backend startup */ void -InitializeSessionUserId(const char *rolename, Oid roleid) +InitializeSessionUserId(const char *rolename, Oid roleid, bool bypass_logi= n_check) { HeapTuple roleTup; Form_pg_authid rform; @@ -789,7 +789,7 @@ InitializeSessionUserId(const char *rolename, Oid rolei= d) /* * Is role allowed to login at all? */ - if (!rform->rolcanlogin) + if (!bypass_login_check && !rform->rolcanlogin) ereport(FATAL, (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), errmsg("role \"%s\" is not permitted to log in", diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/pos= tinit.c index 449541e942..c5b91bd4af 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -684,6 +684,7 @@ BaseInit(void) * flags: * - INIT_PG_LOAD_SESSION_LIBS to honor [session|local]_preload_librarie= s. * - INIT_PG_OVERRIDE_ALLOW_CONNS to connect despite !datallowconn. + * - INIT_PG_BYPASS_ROLE_LOGIN to bypass role login check. * out_dbname: optional output parameter, see below; pass NULL if not used * * The database can be specified by name, using the in_dbname parameter, o= r by @@ -901,7 +902,8 @@ InitPostgres(const char *in_dbname, Oid dboid, } else { - InitializeSessionUserId(username, useroid); + InitializeSessionUserId(username, useroid, + (flags & INIT_PG_BYPASS_ROLE_LOGIN) !=3D 0); am_superuser =3D superuser(); } } @@ -910,7 +912,7 @@ InitPostgres(const char *in_dbname, Oid dboid, /* normal multiuser case */ Assert(MyProcPort !=3D NULL); PerformAuthentication(MyProcPort); - InitializeSessionUserId(username, useroid); + InitializeSessionUserId(username, useroid, false); /* ensure that auth_method is actually valid, aka authn_id is not NULL */ if (MyClientConnectionInfo.authn_id) InitializeSystemUser(MyClientConnectionInfo.authn_id, diff --git a/src/test/modules/worker_spi/t/001_worker_spi.pl b/src/test/mod= ules/worker_spi/t/001_worker_spi.pl index 178962eddb..b5ac4bcaa2 100644 --- a/src/test/modules/worker_spi/t/001_worker_spi.pl +++ b/src/test/modules/worker_spi/t/001_worker_spi.pl @@ -131,4 +131,35 @@ ok( $node->poll_query_until( qq[noconndb|myrole|WorkerSpiMain]), 'dynamic bgworker with BYPASS_ALLOWCONN started'); =20 +# Check BGWORKER_BYPASS_ROLELOGINCHECK. +# First create a role without login access. +$node->safe_psql( + 'postgres', qq[ + CREATE ROLE nologrole WITH NOLOGIN; + GRANT CREATE ON DATABASE mydb TO nologrole; +]); +my $nologrole_id =3D $node->safe_psql('mydb', + "SELECT oid FROM pg_roles where rolname =3D 'nologrole';"); +$log_offset =3D -s $node->logfile; + +# bgworker cannot be launched with login restriction. +$node->psql('postgres', + qq[SELECT worker_spi_launch(13, $mydb_id, $nologrole_id);]); +$node->wait_for_log(qr/role "nologrole" is not permitted to log in/, + $log_offset); + +# bgworker bypasses the login restriction, and can be launched. +$log_offset =3D -s $node->logfile; +my $worker5_pid =3D $node->safe_psql('mydb', + qq(SELECT worker_spi_launch(13, $mydb_id, $nologrole_id, '{"ROLELOGINCHEC= K"}');) +); +ok( $node->poll_query_until( + 'mydb', + qq[SELECT datname, usename, wait_event FROM pg_stat_activity + WHERE backend_type =3D 'worker_spi dynamic' AND + pid =3D $worker5_pid;], + 'mydb|nologrole|WorkerSpiMain'), + 'dynamic bgworker with BYPASS_ROLELOGINCHECK launched' +) or die "Timed out while waiting for dynamic bgworkers to be launched"; + done_testing(); diff --git a/src/test/modules/worker_spi/worker_spi.c b/src/test/modules/wo= rker_spi/worker_spi.c index 1c619d4b18..c26ffe1766 100644 --- a/src/test/modules/worker_spi/worker_spi.c +++ b/src/test/modules/worker_spi/worker_spi.c @@ -452,6 +452,8 @@ worker_spi_launch(PG_FUNCTION_ARGS) =20 if (strcmp(optname, "ALLOWCONN") =3D=3D 0) flags |=3D BGWORKER_BYPASS_ALLOWCONN; + else if (strcmp(optname, "ROLELOGINCHECK") =3D=3D 0) + flags |=3D BGWORKER_BYPASS_ROLELOGINCHECK; else ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), diff --git a/doc/src/sgml/bgworker.sgml b/doc/src/sgml/bgworker.sgml index 9ad1146ba0..8b61cf1c55 100644 --- a/doc/src/sgml/bgworker.sgml +++ b/doc/src/sgml/bgworker.sgml @@ -200,7 +200,9 @@ typedef struct BackgroundWorker InvalidOid, the process will run as the superuser cr= eated during initdb. If BGWORKER_BYPASS_ALLOWCONN= is specified as flags it is possible to bypass the r= estriction - to connect to databases not allowing user connections. + to connect to databases not allowing user connections. If BGWO= RKER_BYPASS_ROLELOGINCHECK + is specified as flags it is possible to bypass the l= ogin check for the + role used to connect to databases. A background worker can only call one of these two functions, and only once. It is not possible to switch databases. --=20 2.42.0 --NCpCPdh8mcjW1d9K-- --lQxR8Z3b5yIxlxsv Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmUmGSwACgkQnvQgOdby QH2q8Q/9HKf0zdGW2p5BkF+L14XiXEenAZ7NNZrBTQeQSErHX4jFPxWDo4VlTmMu y3i3+dXksaqiv+RNQ6DpxIXlZUCyMmx7I1J01lZM7Ag9yZJp3GUqcK2kCq8VJ/7k 0wsyoEfgo8qKvUE/JI1MbMKuhQWeb6wnge4oKRFTiSzf4Z2OlXQXSqT0qA+FoRVo uo8CysLb8d7f/ZbcBmp03Pm583KhBCdDe75DBfifQJC6qHc/Xp3VNjb+Y69MMv0j G6Lw+BAjoBF00NmwJhxvXgknzP9Mfp47CkkIJYMicYxX/1aNIjfJmHi6tlhqCwni 1pnMEmsfV9xCDByTQ8uZJIa2uq1GkfZ6AdRzAYfouDNGIPwi+H9Uf5UXGSUKA+Ub qv1H+VKeMw/2Cgv49sTKk95bgt5gmsE75Xcg6p6cgoDJBwDAiV3feLK/ObmWqG2P 2Wpl2U7EDhX4IpLxcxOz3oNqXHzelV85fH2xJw6U8vhhyjPzkbUnx3xKSYIckwwC 1xKnzRFwnBvyW1yDE7cqwyuvJrq9/2x9wwp1ygcjWIUPwhZ7vf/xEnTYZkLIqK2Y 1vXgtLh4k0uX/prruO04iLM/uDdweWCecQDtNl9nhGEXdYq2NksKj93iCqSkHlh4 sbfpkFPTZw1i0sOegEeQn/ySP2rUAark7KzcRsXVmraXTsAGnUE= =3Piu -----END PGP SIGNATURE----- --lQxR8Z3b5yIxlxsv--