Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jIoiK-0005b3-VN for pgsql-bugs@arkaria.postgresql.org; Mon, 30 Mar 2020 07:20:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jIoiI-0000E6-5B for pgsql-bugs@arkaria.postgresql.org; Mon, 30 Mar 2020 07:20:02 +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 1jIoiH-0000Di-VG for pgsql-bugs@lists.postgresql.org; Mon, 30 Mar 2020 07:20:01 +0000 Received: from [64.147.123.25] (helo=wout2-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jIoiE-0002cq-RR for pgsql-bugs@lists.postgresql.org; Mon, 30 Mar 2020 07:20:00 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 507DA73E; Mon, 30 Mar 2020 03:19:47 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 30 Mar 2020 03:19:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=fbo0GW3p5MhxBHbRknOLsA94v9Q FwkguxSWgcH5GIXE=; b=JEtWL3erthZ1G9ROByaneI/Iviv3jJiKVqjWgiTWNXO DEi/eAYDtSKwYKk1MoAZmQsGC29AX4X74nHYGC0pdwbEVFvF2IwEaxZOA6lpcCHc iIQybcPqDlzloCb4mgSRvtafOqNhAT3TlGigRDblz2AT+YFJ9+Nr2u7WgH03ICpj qd0ca1/t822ZEZBLBgw7+RMNmchOyaMiVmc9hSMvOwW1HLZ9iluan1MDlUwfm7Eh k7BgoULZfRQgLZOfLW8EzlILw1pcD8j0LWHfDopyCNBDdbSkgy2o6Xp3BE04q51d DR/xovOASkl3bPxJI5YxeBTZGxsy88JUaIahBXAeLeA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=fbo0GW 3p5MhxBHbRknOLsA94v9QFwkguxSWgcH5GIXE=; b=HinDa/MOqhkq1omeAxW+Mi n/8t59tNdRzbCnxtZz1PS0DFhkSgYn0H879CyIOgUr0n8Juad4sKXqnioECvSK/e MpvzU8DtIECgFZRdrLaZxXv3LYybbN/9gaPaMPlWzn469bT8AboTP6Q3T/wDCek4 KMlQaBLiNEWOnuNTGpWSafkslNvKI5ubP4m6YL9ntbR2MdixSPGJf2+gdZykntLV ME0tVflL4F+8C7A5KgtomHKvkCewwbNKvtaBDO+5h+Ix6/kVeIqXpsVM8C0OgDQQ 1Uq9/Pnmw5/t7IABn6fTA1FGye64DI+WzKGAEGLDSXTUSVThof7JvoZGk8Xk1Raw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudeigedguddvtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdefhedmnecujfgurhepfffhvffukfhfgggtuggjsehgtderredt tddvnecuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesph grqhhuihgvrhdrgiihiieqnecukfhppeduudekrddvgedurddvhedurddvgeeinecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlh esphgrqhhuihgvrhdrgiihii X-ME-Proxy: Received: from paquier.xyz (mp76f1fbf6.ap.nuro.jp [118.241.251.246]) by mail.messagingengine.com (Postfix) with ESMTPA id 6934F328005A; Mon, 30 Mar 2020 03:19:45 -0400 (EDT) Date: Mon, 30 Mar 2020 16:19:37 +0900 From: Michael Paquier To: hghwng@gmail.com, pgsql-bugs@lists.postgresql.org Cc: Alvaro Herrera Subject: Re: BUG #16330: psql accesses null pointer in connect.c:do_connect Message-ID: <20200330071937.GC43995@paquier.xyz> References: <16330-b34835d83619e25d@postgresql.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="9crTWz/Z+Zyzu20v" Content-Disposition: inline In-Reply-To: <16330-b34835d83619e25d@postgresql.org> X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --9crTWz/Z+Zyzu20v Content-Type: multipart/mixed; boundary="uxuisgdDHaNETlh8" Content-Disposition: inline --uxuisgdDHaNETlh8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 30, 2020 at 02:20:48AM +0000, PG Bug reporting form wrote: > If the connection to postmaster is closed, then trying to re-connect to > another one leads to SIGSEGV. >=20 > REPRODUCE: > $ psql > -> \conninfo > You are connected to database "hugh" as user "hugh" via socket in > "/run/postgresql" at port "5432". > *shut down server with commands like "systemctl stop postgresql"* > -> \conninfo=20 > You are currently not connected to a database. > -> \c a b c d > [1] 984978 segmentation fault (core dumped) psql Indeed. Reproduced the problem here on HEAD and REL_12_STABLE, though you need to execute a command after stopping the server to let psql know that you are not connected to a database. > ANALYSIS: > PQhost(o_conn) returns NULL, and strcmp(host, NULL) raises SIGSEGV. Yes, the problem comes from this commit (added Alvaro in CC): commit: 6e5f8d489acccdc50a35a1b7db8e72b5ad579253 author: Alvaro Herrera date: Mon, 19 Nov 2018 14:34:12 -0300 psql: Show IP address in \conninfo And more particularly this bit that ignores the possibility of PQhost() being NULL: - if (!user && reuse_previous) - user =3D PQuser(o_conn); - if (!host && reuse_previous) - host =3D PQhost(o_conn); - if (!port && reuse_previous) - port =3D PQport(o_conn); + if (reuse_previous) + { + if (!user) + user =3D PQuser(o_conn); + if (host && strcmp(host, PQhost(o_conn)) =3D=3D 0) + { + /* + * if we are targetting the same host, reuse its hostaddr for + * consistency + */ + hostaddr =3D PQhostaddr(o_conn); A fix like the attached should be sufficient as if we know that PQhost() is NULL for the old connection we cannot use the old hostaddr. Alvaro, what do you think? -- Michael --uxuisgdDHaNETlh8 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="psql-crash-fix.patch" diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index abb18a19c2..8108a76a77 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2990,7 +2990,8 @@ do_connect(enum trivalue reuse_previous_specification, { if (!user) user = PQuser(o_conn); - if (host && strcmp(host, PQhost(o_conn)) == 0 && + if (host && PQhost(o_conn) && + strcmp(host, PQhost(o_conn)) == 0 && has_hostaddr(o_conn)) { hostaddr = PQhostaddr(o_conn); --uxuisgdDHaNETlh8-- --9crTWz/Z+Zyzu20v Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAl6BnYkACgkQnvQgOdby QH3frQ//b/70lDiAr9yW8wNd7XJrUMQW72jfDljmHxnBFlQ/dBd1tuwBSA70JSur USCTva6YkwQD/OP+0RzIBa2SS+rBittukI0L8reKy0TkwEBQwB3lVXGF7VkoeGmc 2nriyMIN88yuAlHuQddX1pY1thX23QRDggm1XVx4/+lyM3jPIeMDTwcbF/hCUx1T FBW0vLY8uPEeEJ9aeVbvYlv+rRy4sp5vW5npOH9v6oAr3IkTZtt/t1TmtCuWqaNI f6m7sYuaIAy0ZoGwN+fj1C1D2GZZSTcWJNJoVJokeyYC6AGSaUQqF938ZzGweIw8 KnHxKXWZfhxhvYAusBpY1jm03JcTldZD91uOsRbB08KQidGnhUKLq6UTeRcBxu/x MH9Gqk022LS6poQRqVoxq6sNPd+cJlQZdsq/mFfSGjce7uCVNNV0FaeqdVXPJy2D ufp+xz06EGElkJHKu4h3r9CiJyID5YNl2kVQnhvZRSyKUP4iK6tPQClUXToOGxuN EYHgu1U8LmG1G+Iz1LK2weSUkZNSVQE5QTw5LLvOmwNdbaV43bh5tUopfrvNxdN5 4pIuSxXOiewn0O2GBqndNmNls2e6Evq+PRCU4SkTAKTvTzEMsDaYFq1DIKKVG93u 5aSxICww+VdSqzSgcSG8PWRlFBcG1+OtIMY1ti02OREQVnpOVrg= =riRw -----END PGP SIGNATURE----- --9crTWz/Z+Zyzu20v--