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 1pJK6x-0003Lx-6s for pgsql-hackers@arkaria.postgresql.org; Sat, 21 Jan 2023 20:05:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pJK6u-0002S3-DP for pgsql-hackers@arkaria.postgresql.org; Sat, 21 Jan 2023 20:05:08 +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 1pJK6s-0002Ru-Si for pgsql-hackers@lists.postgresql.org; Sat, 21 Jan 2023 20:05:08 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pJK6k-0004Kj-2X for pgsql-hackers@postgresql.org; Sat, 21 Jan 2023 20:05:06 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id A6F075C007B; Sat, 21 Jan 2023 15:04:55 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sat, 21 Jan 2023 15:04:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc: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=fm3; t=1674331495; x=1674417895; bh=ml5dMJDiQa Wgl10AbI0r284Mhbp7Y2zP9xGMrNVP5P0=; b=LjcwNzSJ/Ckbyw8ETE1CpBZdm5 PGa4pj7mSbDS0csFpw9U60TQzqYbvIVAcIfrIL6+bf8JMX+heAc0EZchYoxH54Ft /FUioh0wBGFTNplUj2N0+5Gx4i/1RKisKgXxG/cHIlTq2xK76J5VsnO2azgMNhIl Lx23D39onaTucGbcgMzRu+a3+fCvvWS7VXeemPw4MoATxPj+/RTcDfsKQhK+/yF6 U+DJAFEicMuZKfAlffOLjqFUVzr6ImZj1ZL06aQHXXhvlKqgN/uWNyRW0rAAVfM4 L++d2IeKXfHR5O5EaRGFWNJG4G008GOaNcgo6pSQjMZHCTXUMD7jXLGIHGEQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc: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= fm3; t=1674331495; x=1674417895; bh=ml5dMJDiQaWgl10AbI0r284Mhbp7 Y2zP9xGMrNVP5P0=; b=NLxTH/MrAD4It7n4jn8/GSaWVg3+3MAztHgTiDJYqCcb zROTNBToWk5SPl3XD1IgDhZpO3lQbO376504iPbBWOEPmK8z7BlBxjXA2lN8rK53 ArqN5M4ZLK+giGWtva4BH7XdIIzfliJx8sMFCRSm14SZ4iJXtU0TGJc7dT10/w2K k/f054CKrMh4KX2L7dIa79oLdkgIQre0Ei1FUxnX4Re6SiqpUobNS+JOeQlMyrUt o4w3mvmJ1KJdNS9U77kXsmWEXS3LGkSbd7Ya9pBaXteRk7+MG+hLhH41ODv3hYZE QZVEiF0L+qauLEhROuJqW4BiCH3fsDYXPtTRCJzy1Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedruddugedguddutdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeetnhgu rhgvshcuhfhrvghunhguuceorghnughrvghssegrnhgrrhgriigvlhdruggvqeenucggtf frrghtthgvrhhnpedvffefvefhteevffegieetfefhtddvffejvefhueetgeeludehteev udeitedtudenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpegrnhgurhgvshesrghnrghrrgiivghlrdguvg X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 21 Jan 2023 15:04:55 -0500 (EST) Date: Sat, 21 Jan 2023 12:04:53 -0800 From: Andres Freund To: Noah Misch Cc: pgsql-hackers@postgresql.org Subject: Re: libpqrcv_connect() leaks PGconn Message-ID: <20230121200453.ba7eftju7hwaf3cd@awork3.anarazel.de> References: <20230121011237.q52apbvlarfv6jm6@awork3.anarazel.de> <20230121025037.mildbmgylv4ybp4a@awork3.anarazel.de> <20230121161642.GD1356297@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230121161642.GD1356297@rfd.leadboat.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2023-01-21 08:16:42 -0800, Noah Misch wrote: > On Fri, Jan 20, 2023 at 06:50:37PM -0800, Andres Freund wrote: > > On 2023-01-20 17:12:37 -0800, Andres Freund wrote: > > > We have code like this in libpqrcv_connect(): > > It's bit worse than I earlier thought: We use walrv_connect() during CREATE > > SUBSCRIPTION. One can easily exhaust file descriptors right now. So I think > > we need to fix this. > > > > I also noticed the following in libpqrcv_connect, added in 11da97024abb: > > > The attached patch fixes both issues. > > Looks good. I'm not worried about a superuser hosing their own session via > CREATE SUBSCRIPTION failures in a loop. At the same time, this fix is plenty > safe to back-patch. Yea, I'm not worried about it from a security perspective and more from a usability perspective (but even there not terribly). File descriptors that leaked, particularly when not reserved (AcquireExternalFD() etc), can lead to weird problems down the line. And I think it's not that rare to need a few attempts at getting the connection string, permissions, etc right. Thanks for looking at the fix! > > I seems we don't have any tests for creating a subscription that fails during > > connection establishment? That doesn't seem optimal - I guess there may have > > been concern around portability of the error messages? > > Perhaps. We have various (non-subscription) tests using "\set VERBOSITY > sqlstate" for that problem. If even the sqlstate varies, a DO block is the > next level of error swallowing. That's a good trick I need to remember. And the errcode for an invalid connection string luckily differs from the one for a not working one. I think found an even easier way - port=-1 is rejected during PQconnectPoll() and will never even open a socket. That'd make it reasonable for the test to happen in subscription.sql, instead of a tap test, I think (faster, easier to maintain). It may be that we'll one day move that error into the PQconninfoParse() phase, but I don't think we need to worry about it now. Any reason not to go for that? If not, I'll add a test for an invalid conninfo and a non-working connection string to subscription.sql. Greetings, Andres Freund