Subject: Re: Clarification on Role Access Rights to Table Indexes

From: Ayush Vatsa <[email-masked]>
Date: Sat, 8 Mar 2025 20:34:40 +0530

> I'm wondering whether setting missing_ok to true is correct here.  IIUC we
> should have an AccessShareLock on the index, but I don't know if that's
> enough protection.

Since we are already opening the relation with rel = relation_open(relOid,
AccessShareLock);,
if relOid does not exist, it will throw an error. If it does exist, we
acquire an AccessShareLock,
preventing it from being dropped.

By the time we reach IndexGetRelation(), we can be confident that relOid
exists and is
protected by the lock. Given this, it makes sense to keep missing_ok = false
here.

Let me know if you agree or if you see any scenario where
missing_ok = true would be preferable—I can update the condition
accordingly.

Thanks!
Ayush Vatsa
SDE AWS

---

From: Nathan Bossart <[email-masked]>
Date: Sat, 8 Mar 2025 15:08:02 -0600

On Sat, Mar 08, 2025 at 08:34:40PM +0530, Ayush Vatsa wrote:
> missing_ok = true would be preferable-I can update the condition
> accordingly.

Right, we will have a lock on the index, but my concern is that we won't
have a lock on its table.  I was specifically concerned that a concurrent
DROP TABLE could cause IndexGetRelation() to fail, i.e., emit a gross
"cache lookup failed" error.  From a quick test and skim of the relevant
code, I think your patch is fine, though.  IndexGetRelation() retrieves the
table OID from pg_index, so the OID should definitely be valid.  And IIUC
DROP TABLE first acquires a lock on the table and its dependent objects
(e.g., indexes) before any actual deletions, so AFAICT there's no problem
with using it in pg_class_aclcheck() and get_rel_name(), either.

-- 
nathan

---

From: Ayush Vatsa <[email-masked]>
Date: Sun, 9 Mar 2025 03:01:41 +0530

> From a quick test and skim of the relevant
> code, I think your patch is fine, though
Thanks for reviewing.

> And IIUC
True, I have also verified that from [1], hence I think we are safe here.
Maybe we can move ahead with the patch if we can see no other concerns.

[1]
https://github.com/postgres/postgres/blob/master/src/backend/catalog/dependency.c#L398-L430

Thanks,
Ayush Vatsa
SDE AWS

---

From: Nathan Bossart <[email-masked]>
Date: Sat, 8 Mar 2025 15:57:25 -0600

> Maybe we can move ahead with the patch if we can see no other concerns.

I think we should allow some time in case others want to review the patch.
I do see a concern upthread about increased deadlock risk [0], but your
patch doesn't lock the table, but unless I'm wrong [1] (which is always
possible), it doesn't need to lock it.

Anyway, here is a tidied up patch.

[0] https://postgr.es/m/1246906.1739896202%40sss.pgh.pa.us
[1] https://postgr.es/m/Z8yxsm9ZWVkHlPbV%40nathan

-- 
nathan

---

From: Tom Lane <[email-masked]>
Date: Sat, 08 Mar 2025 17:17:40 -0500

> possible), it doesn't need to lock it.

It bothers me a bit that this proposes to do something as complicated
as pg_class_aclcheck on a table we have no lock on.  As you say, the
lock we hold on the index would prevent DROP TABLE, but that doesn't
mean we won't have any issues with other DDL on the table.  Still,
taking a lock would be bad because of the deadlock hazard, and I
think the potential for conflicts with concurrent DDL is nonzero in
a lot of other places.  So I don't have any concrete reason to object.

ReindexIndex() faces this same problem and solves it with some
very complex code that manages to get the table's lock first.
But I see that it's also doing pg_class_aclcheck on a table
it hasn't locked yet, so I don't think that adopting its approach
would do anything useful for us here.

			regards, tom lane

---

From: Nathan Bossart <[email-masked]>
Date: Sat, 8 Mar 2025 19:35:18 -0600

> would do anything useful for us here.

I noticed that amcheck's bt_index_check_internal() handles this problem,
and I think that approach could be adapted here:

	relkind = get_rel_relkind(relOid);
	if (relkind == RELKIND_INDEX || relkind == RELKIND_PARTITIONED_INDEX)
	{
		permOid = IndexGetRelation(relOid, true);
		if (OidIsValid(permOid))
			LockRelationOid(permOid, AccessShareLock);
		else
			fail = true;
	}
	else
		permOid = relOid;

	rel = relation_open(relOid, AccessShareLock);
	if (fail ||
		(permOid != relOid && permOid != IndexGetRelation(relOid, false)))
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_TABLE),
				 errmsg("could not find parent table of index \"%s\"",
						RelationGetRelationName(rel))));

	aclresult = pg_class_aclcheck(permOid, GetUserId(), ACL_SELECT);
	if (aclresult != ACLCHECK_OK)
		aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));

	if (permOid != relOid)
		UnlockRelationOid(permOid, AccessShareLock);

stats_lock_check_privileges() does something similar, but it's not as
cautious about the "heapid != IndexGetRelation(indrelid, false)" race
condition.  Maybe RangeVarCallbackForReindexIndex() should be smarter about
this, too.  That being said, this is a fair amount of complexity to handle
something that is in theory extremely rare...

-- 
nathan

---

From: Tom Lane <[email-masked]>
Date: Sun, 09 Mar 2025 11:48:05 -0400

> ...
> condition.

Egad, we've already got three inconsistent implementations of this
functionality?  I think the first step must be to unify them into
a common implementation, if at all possible.

			regards, tom lane

---

From: Nathan Bossart <[email-masked]>
Date: Mon, 10 Mar 2025 10:15:19 -0500

> a common implementation, if at all possible.

Agreed.  I worry that trying to unify each bespoke implementation into a
single function might result in an unwieldy mess, but I'll give it a
shot...

-- 
nathan

---

From: vignesh C <[email-masked]>
Date: Sun, 16 Mar 2025 18:30:23 +0530

> Anyway, here is a tidied up patch.

I noticed that Tom Lane's comment from [1] is not addressed. I'm
changing the commitfest entry status to Waiting on Author, Please
address them and update the status to Needs Review.
[1] - https://www.postgresql.org/message-id/279947.1741535285%40sss.pgh.pa.us

Regards,
Vignesh

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 24 Sep 2025 10:58:25 -0500

> single function might result in an unwieldy mess, but I'll give it a

I tried to unify these, but each one seems to be just different enough to
make it not worth the trouble.  Instead, I took a look at each
implementation:

* amcheck's amcheck_lock_relation_and_check() seems correct to me.

* stats_lock_check_privileges() appears to be missing the second
IndexGetRelation() check after locking the table and index, so I added
that in 0001.  Since this code is new to v18, I proposed to back-patch 0001
there.

* RangeVarCallbackForReindexIndex() was checking privileges on the table
before locking it, so I reversed it in 0002.  Interestingly, this caused
test errors because LockRelationOid() checks for invalidation messages, so
the pg_class_aclcheck() call started failing with unhelpful errors due to
concurrently dropped relations.  To deal with that, I switched to
pg_class_aclcheck_ext() so that we can handle missing relations.
Furthermore, I noticed that this callback seems to assume that as long as
the index does not change between calls, its table won't, either.  That's
probably always true in practice, but even if it's completely true, I see
no reason to rely on it.  So, I simplified the code to unconditionally
unlock any previously-locked table and to lock whatever IndexGetRelation()
returns.  This could probably be back-patched, but in the absence of any
reports or any reproducible bugs, I don't think we should.

* 0003 fixes pg_prewarm's privilege checks by following a similar pattern.
This probably ought to get back-patched to all supported versions.

-- 
nathan

---

From: Tom Lane <[email-masked]>
Date: Wed, 24 Sep 2025 12:13:34 -0400

Nathan Bossart <[email-masked]> writes:
> before locking it, so I reversed it in 0002.

Don't we do that intentionally, to make sure someone can't cause DOS
on a table they have no privileges on?

			regards, tom lane

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 24 Sep 2025 11:52:09 -0500

> on a table they have no privileges on?

Ah, right.  I switched it back in v4.

-- 
nathan

---

From: Nathan Bossart <[email-masked]>
Date: Thu, 2 Oct 2025 12:37:44 -0500

> Ah, right.  I switched it back in v4.

Unless more feedback materializes, I'm planning to commit these sometime
next week.

-- 
nathan

---

From: Jeff Davis <[email-masked]>
Date: Wed, 08 Oct 2025 20:06:04 -0700

> Ah, right.  I switched it back in v4.

v4-0001 looks good to me.

Just to make sure I understand: the actual problem would only happen
with OID wraparound, right?

Regards,
	Jeff Davis

---

From: Jeff Davis <[email-masked]>
Date: Wed, 08 Oct 2025 20:28:01 -0700

> v4-0001 looks good to me.

Actually, now I'm unsure. v4-0001 is taking a lock on the table before
checking privileges, whereas v4-0002 is going to some effort to avoid
that. Is that because the latter is taking a ShareLock?

Regards,
	Jeff Davis

---

From: Nathan Bossart <[email-masked]>
Date: Thu, 9 Oct 2025 10:39:32 -0500

> that. Is that because the latter is taking a ShareLock?

I was confused by this, too.  We seem to go to great lengths to avoid
taking a lock before checking permissions in RangeVarGetRelidExtended(),
but in pg_prewarm() and this stats code, we are taking the lock first.
pg_prewarm() can't use RangeVarGetRelid because you give it the OID, but
I'm not seeing why stat_utils.c can't use it.  We should probably fix this.
I wouldn't be surprised if there are other examples.

-- 
nathan

---

From: Nathan Bossart <[email-masked]>
Date: Thu, 9 Oct 2025 16:18:03 -0500

> I wouldn't be surprised if there are other examples.

I spent some time trying to change pg_prewarm() to check permissions before
locking and came up with the attached.  There are certainly issues with the
patch, but this at least demonstrates the complexity required.  I'm tempted
to say that this is more trouble than it's worth, but it does feel a little
weird to leave it as-is.

There's a similar pattern in get_rel_from_relname() in dblink.c, which also
seems to only be used with an AccessShareLock (like pg_prewarm).  My best
guess from reading lots of code, commit messages, and old e-mails in the
archives is that the original check-privileges-before-locking work was
never completed.

I'm currently leaning towards continuing with v4 of the patch set.  0001
and 0003 are a little weird in that a concurrent change could lead to a
"could not find parent table" ERROR, but IIUC that is an extremely remote
possibility.

-- 
nathan

---

From: Nathan Bossart <[email-masked]>
Date: Fri, 10 Oct 2025 11:26:08 -0500

> archives is that the original check-privileges-before-locking work was

I added an 0004 that changes dblink to use RangeVarGetRelidExtended().

> "could not find parent table" ERROR, but IIUC that is an extremely remote

After sleeping on it, I still think this is the right call.  In any case,
I've spent way too much time on this stuff, so I plan to commit the
attached soon.

-- 
nathan

---

From: Jeff Davis <[email-masked]>
Date: Fri, 10 Oct 2025 11:31:03 -0700

> > archives is that the original check-privileges-before-locking work

Interesting, thank you for the analysis.

> I've spent way too much time on this stuff, so I plan to commit the

I'm OK with that. v5-0001 is an improvement over the current situation.

Regards,
	Jeff Davis

---

From: Jeff Davis <[email-masked]>
Date: Mon, 13 Oct 2025 11:16:36 -0700

> on a table they have no privileges on?

Is this only a problem for strong locks (ShareLock or greater)?

Strong locks are a problem when you have a pattern like a long running
query that holds an AccessShareLock, and then an unprivileged user
requests an AccessExclusiveLock, forcing other queries to queue up
behind it, and the queue doesn't clear until the long running query
finishes.

But weaker locks don't seem to have that problem, right?

Regards,
	Jeff Davis

---

From: Nathan Bossart <[email-masked]>
Date: Mon, 13 Oct 2025 14:30:48 -0500

On Fri, Oct 10, 2025 at 11:31:03AM -0700, Jeff Davis wrote:
>> After sleeping on it, I still think this is the right call.  In any
>> case, I've spent way too much time on this stuff, so I plan to commit
>> the attached soon.
> 

Okay, I lied.  I spent even more time on these patches and came up with the
attached.  Here's a summary of what's going on:

* 0001 moves the code for stats clearing/setting to use
RangeVarGetRelidExtended().  The existing code looks up the relation, locks
it, and then checks permissions.  There's no guarantee that the relation
you looked up didn't concurrently change before locking, and locking before
privilege checks is troublesome from a DOS perspective.  One downside of
using RangeVarGetRelidExtended() is that we can't use AccessShareLock for
regular indexes, but I'm not sure that's really a problem since we're
already using ShareUpdateExclusiveLock for everything else.  The
RangeVarGetRelidExtended() callback is similar to the one modified by 0002.
This should be back-patched to v18.

* 0002 fixes the RangeVarGetRelidExtended() callback for REINDEX INDEX to
handle unlikely scenarios involving OID reuse (e.g., lookup returns the
same index OID for a different table).  I did confirm there was a bug here
by concurrently re-creating an index with the same OID for a heap with a
different OID (via the pg_upgrade support functions).  In previous versions
of this patch, I tried to fix this by unconditionally unlocking the heap at
the beginning of the callback, but upon further inspection, I noticed that
creates deadlock hazards because we might've already locked the index.  (We
need to lock the heap first.)  In v6, I've just added an ERROR for these
extremely unlikely scenarios.  I've also replaced all early returns in this
function with ERRORs (except for the invalid relId case).  AFAICT the extra
checks are unecessary, and even if they were necessary, I think they break
some of the code related to heap locking in subtle ways.  Some callbacks do
these extra checks, and others do not, and AFAIK there haven't been any
reported problems either way.  0002 should be back-patched to v13, but it
will look a little different on v16 and newer, i.e., before MAINTAIN was
added.

* 0003 fixes the privilege checks in pg_prewarm by using a similar approach
to amcheck_lock_relation_and_check().  This seems correct to me, but it
does add more locking.  This should be back-patched to v13.

* 0004 is a small patch to teach dblink to use RangeVarGetRelidExtended().
I believe this code predates that function.  I don't intend to back-patch
this one.

-- 
nathan

---

From: Tom Lane <[email-masked]>
Date: Mon, 13 Oct 2025 17:21:07 -0400

> But weaker locks don't seem to have that problem, right?

I don't think so.  Even AccessShareLock is enough to block another
session trying to acquire AccessExclusiveLock, and then not only
have you DoS'd that session, but everything else trying to access
the table will queue up behind the AccessExclusiveLock request.
So it's only not-a-problem if nothing anywhere in the system wants
non-sharable locks.

			regards, tom lane

---

From: Jeff Davis <[email-masked]>
Date: Mon, 13 Oct 2025 15:11:38 -0700

> So it's only not-a-problem if nothing anywhere in the system wants

I tried imagining how that could be a problem, but couldn't come up
with anything. If the privilege check is right after the lock, then
either:

(a) The malicious AccessShareLock is granted, then is quickly released
when the privilege check fails and the transaction aborts; or

(b) The malicious AccessShareLock is queued behind a legitimate
AccessExclusiveLock, in which case every other lock would be queued up
as well. As soon as the AccessExclusiveLock is released, the
AccessShareLock would be granted, but quickly released when the
privilege check fails.

For it to be a problem, the malicious lock needs to be strong enough to
conflict with a lock level weaker than itself, i.e. ShareLock or
stronger.

I'm not sure we save anything by being lazier for weaker lock levels,
so perhaps the point is irrelevant. But if I'm missing something,
please let me know.

Regards,
	Jeff Davis

---

From: Jeff Davis <[email-masked]>
Date: Mon, 13 Oct 2025 19:23:36 -0700

> This should be back-patched to v18.

We tried to match the locking behavior for analyze. Originally, that's
because we were using in-place updates, which required those specific
kinds of locks. Now that the in-place code is gone, then I think it's
OK to use ShareUpdateExclusiveLock for indexes, too, but it is a
notable difference in behavior.

Including Corey in case he has comments.

As for the patch itself, it looks good to me. Stylistically I might
have kept the "index_oid" variable, which makes some of the tests a bit
clearer, but I don't have a strong opinion.

The unlikely scenarios are a bit confusing. I'd probably error for
either case. Also, the error message on the second scenario is wrong if
the previous lookup was a table, I think.

> function with ERRORs (except for the invalid relId case).

+1 for throwing errors when we have race conditions combined with name
reuse. Looks fine to me.

> does add more locking.  This should be back-patched to v13.

IIUC this is locking before the privilege check. Is there a reason why
we think this is OK here (and in amcheck_lock_relation_and_check()) but
not for the stats?

> I believe this code predates that function.  I don't intend to back-

Looks good.

Regards,
	Jeff Davis

---

From: Nathan Bossart <[email-masked]>
Date: Tue, 14 Oct 2025 11:05:32 -0500

Thanks for reviewing.

> the previous lookup was a table, I think.

Yeah, I think that's a better idea.

> we think this is OK here (and in amcheck_lock_relation_and_check()) but

For amcheck, AFAICT there aren't actually any ACL checks within the code
because the function is restricted to superuser by default.  For
pg_prewarm, I don't know.  You do have to install the extension before
using it, but once installed, it's available to everyone by default.  My
guess is that it just hasn't been a problem in the field.

Regardless, fixing the lock-before-privilege-checks behavior doesn't strike
me as a bug, so I think we ought to proceed with something like 0003 for
back-patching purposes and then to rework it further for v19.  Does that
sound okay to you?

>> * 0004 is a small patch to teach dblink to use
>> RangeVarGetRelidExtended().  I believe this code predates that
>> function.  I don't intend to back-patch this one.
> 
> Looks good.

I'm going to go commit this one now to get it out of the way.

-- 
nathan

---

From: Tom Lane <[email-masked]>
Date: Tue, 14 Oct 2025 12:30:26 -0400

> conflict with a lock level weaker than itself, i.e. ShareLock or

Robert might remember better, but I think the assumption behind
the current design of RangeVarGetRelidExtended is that it's not
okay to take a lock in the first place if you don't have the
privilege to do so.  Your analysis here supposes that it's okay
to take a lock without privileges so long as you can't block someone
else for very long, where "very long" is not tightly defined but
hopefully isn't controllable by the malicious user.  So that's
moving the goalposts somewhat, but you might get people to sign
onto it with more careful analysis of the worst-case delay.
(The thing I'd worry about is whether it's possible to block
execution of the privilege check, or even just make it slow.)

Given that definition, I think you're right that it's possible to
identify cases where lock-then-check can't cause meaningful DoS.
RangeVarGetRelidExtended has to cope with the general case, so
that's not an argument for simplifying it, but we might not need
equivalent complexity everywhere.

			regards, tom lane

---

From: Jeff Davis <[email-masked]>
Date: Tue, 14 Oct 2025 10:01:37 -0700

On Tue, 2025-10-14 at 11:05 -0500, Nathan Bossart wrote:
> For

If we start with an OID, what's the right way to do these kinds of
checks? Could we do an ACL check, then lock it, then do an ACL check
again to catch OID wraparound?

Last-minute suggestions on 0003:

  * Add a comment around the privOid check to explain that, if the
object is an index, we must check the privileges on the table instead.

  * Clarify in the comment that the race against index drop/recreation
involves OID wraparound.

+1 to the patch and backpatch.

As a separate thought, I'm wondering if we should do more to enforce
the idea that we check the privileges and owner of an index's table,
and never the index itself. That's for another discussion, though.

> back-patching purposes and then to rework it further for v19.  Does

According to the current rules[1], it does seem to technically be a
bug, but as far as I can tell, not one of much consequence.

Regards,
	Jeff Davis

[1]
https://www.postgresql.org/message-id/[email-masked]

---

From: Nathan Bossart <[email-masked]>
Date: Tue, 14 Oct 2025 16:32:55 -0500

I've committed 0004.  Here is an updated patch set.

-- 
nathan

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 15 Oct 2025 10:39:17 -0500

> again to catch OID wraparound?

I tried something like this upthread [0].  My feeling was that this was a
lot of complexity for not a lot of gain.  Perhaps it's still worth doing,
though.

[0] https://postgr.es/m/aOgmi6avE6qMw_6t%40nathan

-- 
nathan

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 15 Oct 2025 12:54:21 -0500

I just pushed 0001, and longfin and sifaka have very quickly reminded me
that we don't require C11 on v18.  Will fix shortly...

-- 
nathan

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 15 Oct 2025 16:38:05 -0500

I've committed everything except for 0003, which I probably won't get to
until Friday.  Note that I decided against back-patching 0002 because of
the presumed rarity of and lack of reports for the bug.

-- 
nathan

---

From: Nathan Bossart <[email-masked]>
Date: Fri, 17 Oct 2025 11:56:04 -0500

> the presumed rarity of and lack of reports for the bug.

Everything tracked in this thread is now committed.

-- 
nathan