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 1l3we8-0005go-0Q for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Jan 2021 07:50:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l3we6-0002nU-OL for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Jan 2021 07:50:46 +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 1l3we6-0002nN-He for pgsql-hackers@lists.postgresql.org; Mon, 25 Jan 2021 07:50:46 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l3we3-00006W-Cd for pgsql-hackers@lists.postgresql.org; Mon, 25 Jan 2021 07:50:45 +0000 Received: from hnk.local (p2388216-ipbf1027funabasi.chiba.ocn.ne.jp [118.6.6.216]) by oss.nttdata.com (Postfix) with ESMTPSA id 7582062771; Mon, 25 Jan 2021 16:50:39 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: [PATCH] postgres_fdw connection caching - cause remote sessions linger till the local session exit To: Bharath Rupireddy Cc: Zhihong Yu , Alexey Kondratov , "Hou, Zhijie" , PostgreSQL Hackers References: <0c53c3da-9bfe-82f0-1190-5f029e92a816@oss.nttdata.com> <394e56d4-5b8c-57cf-52e2-ea331436882d@oss.nttdata.com> <0e61fbcc-fca2-18f8-758c-4ed61c664fd7@oss.nttdata.com> <153fba15-1e9d-c2f7-2b4d-3fafa8d4621b@oss.nttdata.com> <9f6a69cd-3e4c-1703-a829-331dd9b660a0@oss.nttdata.com> <646119f3-fd27-7fd7-5377-b72310af5d21@oss.nttdata.com> <62431587-ed3a-f333-51a9-967ccf2a78db@oss.nttdata.com> From: Fujii Masao Message-ID: <1c643738-6e52-565d-0261-740d490b550e@oss.nttdata.com> Date: Mon, 25 Jan 2021 16:50:39 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2021/01/23 13:40, Bharath Rupireddy wrote: > On Fri, Jan 22, 2021 at 6:43 PM Fujii Masao wrote: >>>> Please review the v16 patch set further. >>> >>> Thanks! Will review that later. >> >> + /* >> + * For the given server, if we closed connection or it is still in >> + * use, then no need of scanning the cache further. We do this >> + * because the cache can not have multiple cache entries for a >> + * single foreign server. >> + */ >> >> On second thought, ISTM that single foreign server can have multiple cache >> entries. For example, >> >> CREATE ROLE foo1 SUPERUSER; >> CREATE ROLE foo2 SUPERUSER; >> CREATE EXTENSION postgres_fdw; >> CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw OPTIONS (port '5432'); >> CREATE USER MAPPING FOR foo1 SERVER loopback OPTIONS (user 'postgres'); >> CREATE USER MAPPING FOR foo2 SERVER loopback OPTIONS (user 'postgres'); >> CREATE TABLE t (i int); >> CREATE FOREIGN TABLE ft (i int) SERVER loopback OPTIONS (table_name 't'); >> SET SESSION AUTHORIZATION foo1; >> SELECT * FROM ft; >> SET SESSION AUTHORIZATION foo2; >> SELECT * FROM ft; >> >> >> Then you can see there are multiple open connections for the same server >> as follows. So we need to scan all the entries even when the serverid is >> specified. >> >> SELECT * FROM postgres_fdw_get_connections(); >> >> server_name | valid >> -------------+------- >> loopback | t >> loopback | t >> (2 rows) > > This is a great finding. Thanks a lot. I will remove > hash_seq_term(&scan); in disconnect_cached_connections and add this as > a test case for postgres_fdw_get_connections function, just to show > there can be multiple connections with a single server name. > >> This means that user (even non-superuser) can disconnect the connection >> established by another user (superuser), by using postgres_fdw_disconnect_all(). >> Is this really OK? > > Yeah, connections can be discarded by non-super users using > postgres_fdw_disconnect_all and postgres_fdw_disconnect. Given the > fact that a non-super user requires a password to access foreign > tables [1], IMO a non-super user changing something related to a super > user makes no sense at all. If okay, we can have a check in > disconnect_cached_connections something like below: Also like pg_terminate_backend(), we should disallow non-superuser to disconnect the connections established by other non-superuser if the requesting user is not a member of the other? Or that's overkill because the target to discard is just a connection and it can be established again if necessary? For now I'm thinking that it might better to add the restriction like pg_terminate_backend() at first and relax that later if possible. But I'd like hear more opinions about this. > > +static bool > +disconnect_cached_connections(Oid serverid) > +{ > + HASH_SEQ_STATUS scan; > + ConnCacheEntry *entry; > + bool all = !OidIsValid(serverid); > + bool result = false; > + > + if (!superuser()) > + ereport(ERROR, > + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), > + errmsg("must be superuser to discard open connections"))); > + > + if (!ConnectionHash) > > Having said that, it looks like dblink_disconnect doesn't perform > superuser checks. Also non-superuser (set by SET ROLE or SET SESSION AUTHORIZATION) seems to be able to run SQL using the dblink connection established by superuser. If we didn't think that this is a problem, we also might not need to care about issue even for postgres_fdw. Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION