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 1scgFB-009b1e-13 for pgsql-hackers@arkaria.postgresql.org; Sat, 10 Aug 2024 07:10:29 +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 1scgF9-007abn-ED for pgsql-hackers@arkaria.postgresql.org; Sat, 10 Aug 2024 07:10:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scgF9-007abf-4I for pgsql-hackers@lists.postgresql.org; Sat, 10 Aug 2024 07:10:27 +0000 Received: from fout1-smtp.messagingengine.com ([103.168.172.144]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scgF2-0045vK-Bc for pgsql-hackers@postgresql.org; Sat, 10 Aug 2024 07:10:26 +0000 Received: from compute8.internal (compute8.nyi.internal [10.202.2.227]) by mailfout.nyi.internal (Postfix) with ESMTP id 740CD138FFDE for ; Sat, 10 Aug 2024 03:10:18 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute8.internal (MEProxy); Sat, 10 Aug 2024 03:10:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eisentraut.org; h=cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm2; t=1723273818; x=1723360218; bh=6f2NS2ysx14KrvPOfcMYPDowBSWrrTtR gXNOr3Gw3ic=; b=4hTFieFHyjZ/BO9s/pskvDhqtmtfUKodhEm4JJ+QORcS22ou 8J1VK2w10ps62jNG9ZRJG0gouJDn2XiN9cwnSL+Bdjddz2ZcJWqTTEKAmyhXdujf aJPVkiHL+q+27o7ZGYbazlmORsUHC5GfiMZdI8mFujv21bKUUY3RUa7nU9PkXtUn Lo19AiUiOwGLVZ6zo/b9HU+82uQXxqxD95hFXFguZnZOXAJXSFeWjLyLPnwg0Bmw edDXeRSlmFqcT+3QeKC80ve+fnBEuJUhEHdP+lTB8mnq2Zv5uFerLYsdB847rdDV SQm47h0FSAppvKJg/3SUr6Fz3Qw8bdQ0TvfCrg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1723273818; x=1723360218; bh=6f2NS2ysx14KrvPOfcMYPDowBSWrrTtRgXN Or3Gw3ic=; b=nzkn6IOJXnvd5vT8FvQVJVzy3XID3kOyxlp9C6xN/gooZaj7wdV khQYDSqI4Yl+/elrpWmHlvaR3afA3E9CgwqztmjghgCODw2hrB5fV62rhzuTD4d+ +x19LjCbfcaCyBk0+c1tOliQKLXVF2GfMSP4TCozeHVPl5otJlm5FNKe0XY09UyP nkJ19YeRrJPTzll70MpbF+5krGPg9dGvnE9mdH2RUA6wAKTzPhomsr5eREVtioz/ UYIGKh/sHlaptCDUw4WHakzRiVH+ewD9HU7HHleuMrpML/vpUhxb8uQqmzex2tjZ v095vaftZC2uxkKbdTZ0lMelnJYtb58nsdw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrleehgdduudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgg gfhffuvfesmhdtreertddvjeenucfhrhhomheprfgvthgvrhcugfhishgvnhhtrhgruhht uceophgvthgvrhesvghishgvnhhtrhgruhhtrdhorhhgqeenucggtffrrghtthgvrhhnpe ehteekudegtdehtedttefhudekjefgudeiheeiteffuedtteffkeekgfelvdffleenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpvghtvghrse gvihhsvghnthhrrghuthdrohhrghdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhht phhouhhtpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphhoshhtghhrvghsqh hlrdhorhhg X-ME-Proxy: Feedback-ID: ie0a040ee:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 10 Aug 2024 03:10:17 -0400 (EDT) Content-Type: multipart/mixed; boundary="------------Lyfny47cEBP0nHu0v7MolpSh" Message-ID: Date: Sat, 10 Aug 2024 09:10:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US From: Peter Eisentraut Subject: libpq minor TOCTOU violation To: pgsql-hackers List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------Lyfny47cEBP0nHu0v7MolpSh Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit I was playing with a static analyzer security scanner and it flagged a time-of-check-time-of-use violation in libpq. I was going to propose a fix for this on -hackers, since you probably can't do anything interesting with this, but then I figured I'd better check here first. libpq checks the permissions of the password file before opening it and rejects it if the permissions are more permissive than 0600. It does this in two separate steps, first stat(), then fopen(), which is what triggers the static analyzer. The standard fix for this kind of thing is to open the file first and then use fstat() on the file handle for the permission check. Note that libpq doesn't check who the owner of the file is or what directory it is in. The location of the password file can be changed from its default via libpq connection settings. So it seems to me that if the location of the password file were a world-writable directory, an "attacker" could supply a dummy file with 0600 permissions for the stat() call and then swap it out for a world-readable file with passwords for the fopen() call, both files owned by the attacker. And so they could have the libpq application try out passwords on their behalf. Obviously, this requires a number of unlikely circumstances, including the ability to repeatedly exploit the gap between the stat() and fopen() call. But it seems formally incorrect, so it seems good to fix it, at least to make the code a better example. Thoughts? --------------Lyfny47cEBP0nHu0v7MolpSh Content-Type: text/plain; charset=UTF-8; name="0001-libpq-Fix-minor-TOCTOU-violation.patch" Content-Disposition: attachment; filename="0001-libpq-Fix-minor-TOCTOU-violation.patch" Content-Transfer-Encoding: base64 RnJvbSBlYTY4YmNlZmVjYjc5Y2ZiNGVlMjcxYzgyNjg1MjI4Y2M1MDg0Mzg1IE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBQZXRlciBFaXNlbnRyYXV0IDxwZXRlckBlaXNlbnRy YXV0Lm9yZz4KRGF0ZTogU2F0LCAxMCBBdWcgMjAyNCAwODo0NjozMiArMDIwMApTdWJqZWN0 OiBbUEFUQ0hdIGxpYnBxOiBGaXggbWlub3IgVE9DVE9VIHZpb2xhdGlvbgoKbGlicHEgY2hl Y2tzIHRoZSBwZXJtaXNzaW9ucyBvZiB0aGUgcGFzc3dvcmQgZmlsZSBiZWZvcmUgb3Blbmlu ZyBpdC4KVGhlIHdheSB0aGlzIGlzIGRvbmUgaW4gdHdvIHNlcGFyYXRlIG9wZXJhdGlvbnMs IGEgc3RhdGljIGFuYWx5emVyCndvdWxkIGZsYWcgYXMgYSB0aW1lLW9mLWNoZWNrLXRpbWUt b2YtdXNlIHZpb2xhdGlvbi4gIEluIHByYWN0aWNlLCB5b3UKY2FuJ3QgZG8gYW55dGhpbmcg d2l0aCB0aGF0LCBidXQgaXQgc3RpbGwgc2VlbXMgYmV0dGVyIHN0eWxlIHRvIGZpeAppdC4K ClRvIGZpeCBpdCwgb3BlbiB0aGUgZmlsZSBmaXJzdCBhbmQgdGhlbiBjaGVjayB0aGUgcGVy bWlzc2lvbnMgb24gdGhlCm9wZW5lZCBmaWxlIGhhbmRsZS4KLS0tCiBzcmMvaW50ZXJmYWNl cy9saWJwcS9mZS1jb25uZWN0LmMgfCAxMCArKysrKy0tLS0tCiAxIGZpbGUgY2hhbm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