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.96) (envelope-from ) id 1wbkaD-002oSZ-0j for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jun 2026 19:45:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wbkaC-007pOc-0P for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jun 2026 19:45:24 +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.96) (envelope-from ) id 1wbkaB-007pOU-2g for pgsql-hackers@lists.postgresql.org; Mon, 22 Jun 2026 19:45:23 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wbkaA-00000001dPQ-0qYK for pgsql-hackers@lists.postgresql.org; Mon, 22 Jun 2026 19:45:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 65MJjFBT646742; Mon, 22 Jun 2026 15:45:15 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org cc: Heikki Linnakangas Subject: Fix unsafe coding in ResourceOwnerReleaseAll() MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <646720.1782157460.0@sss.pgh.pa.us> Date: Mon, 22 Jun 2026 15:45:15 -0400 Message-ID: <646741.1782157515@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <646720.1782157460.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable I wondered why the example shown in [1] led to a double-free crash rather than a clean failure. On investigation, the problem is that ResourceOwnerReleaseAll() calls the resource kind's ReleaseResource method before deleting the resource from the owner, rather than afterwards as the code in ResourceOwnerReleaseAllOfKind() does. So if we throw an error inside ReleaseResource, the subsequent abort cleanup comes back and does resource owner cleanup again, and we try to delete the item again. Kaboom. The attached patch converts the bug example into a clean failure, even with the recent bug fix in pgcrypto undone: regression=3D# SELECT encrypt_iv( repeat('A', 1073741308)::bytea, decode('00112233445566778899aabbccddeeff', 'hex'), decode('000102030405060708090a0b0c0d0e0f', 'hex'), 'aes' ); ERROR: invalid memory alloc request size 1073741824 WARNING: AbortTransaction while in ABORT state ERROR: ResourceOwnerForget called for pgcrypto OpenSSL cipher handle afte= r release started regression=3D# = Of course, this might lead to leaking the resource we wished to free. But that's better than crashing, or at least that's the value judgment we made long ago in the original ResourceOwner code. regards, tom lane [1] https://www.postgresql.org/message-id/19527-6e7686960c6dce78%40postgre= sql.org ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="make-ResourceOwnerReleaseAll-safer.patch"; charset="us-ascii" Content-ID: <646720.1782157460.2@sss.pgh.pa.us> Content-Description: make-ResourceOwnerReleaseAll-safer.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/res= owner/resowner.c index 06e1121c5ff..03d2f5a0334 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -347,6 +347,7 @@ ResourceOwnerReleaseAll(ResourceOwner owner, ResourceR= eleasePhase phase, { ResourceElem *items; uint32 nitems; + bool using_arr; = /* * ResourceOwnerSort must've been called already. All the resources are @@ -358,12 +359,14 @@ ResourceOwnerReleaseAll(ResourceOwner owner, Resourc= eReleasePhase phase, { items =3D owner->arr; nitems =3D owner->narr; + using_arr =3D true; } else { Assert(owner->narr =3D=3D 0); items =3D owner->hash; nitems =3D owner->nhash; + using_arr =3D false; } = /* @@ -392,13 +395,20 @@ ResourceOwnerReleaseAll(ResourceOwner owner, Resourc= eReleasePhase phase, elog(WARNING, "resource was not closed: %s", res_str); pfree(res_str); } - kind->ReleaseResource(value); + + /* + * Update stored count to forget the item before calling its + * ReleaseResource method. This avoids double-free crashes in case an + * error gets thrown within ReleaseResource. + */ nitems--; + if (using_arr) + owner->narr =3D nitems; + else + owner->nhash =3D nitems; + + kind->ReleaseResource(value); } - if (owner->nhash =3D=3D 0) - owner->narr =3D nitems; - else - owner->nhash =3D nitems; } = = ------- =_aaaaaaaaaa0--