pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-hackers@lists.postgresql.org
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Subject: Fix unsafe coding in ResourceOwnerReleaseAll()
Date: Mon, 22 Jun 2026 15:45:15 -0400
Message-ID: <646741.1782157515@sss.pgh.pa.us> (raw)

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=# 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 after release started
regression=# 

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%40postgresql.org

Attachments:

make-ResourceOwnerReleaseAll-safer.patch (text/x-diff, 1.4K) download diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/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, ResourceReleasePhase 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, ResourceReleasePhase phase, { items = owner->arr; nitems = owner->narr; + using_arr = true; } else { Assert(owner->narr == 0); items = owner->hash; nitems = owner->nhash; + using_arr = false; } /* @@ -392,13 +395,20 @@ ResourceOwnerReleaseAll(ResourceOwner owner, ResourceReleasePhase 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 = nitems; + else + owner->nhash = nitems; + + kind->ReleaseResource(value); } - if (owner->nhash == 0) - owner->narr = nitems; - else - owner->nhash = nitems; } =

view thread (7+ messages)  latest in thread

Message-ID: <646741.1782157515@sss.pgh.pa.us>
Permalink:  ../646741.1782157515@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/646741.1782157515@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, hlinnaka@iki.fi
  Subject: Re: Fix unsafe coding in ResourceOwnerReleaseAll()
  In-Reply-To: <646741.1782157515@sss.pgh.pa.us>
  References: <646741.1782157515@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox