Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: check privileges after data sources in memo staleness check #102405

Merged
merged 1 commit into from
Apr 28, 2023

Conversation

DrewKimball
Copy link
Collaborator

@DrewKimball DrewKimball commented Apr 27, 2023

Previously, the opt.Metadata staleness check would check the access privileges for a data source immediately after checking that the data source still resolves as it originally did. This meant that the privilege check for one data source could occur before checking staleness of other data sources. Before #96045 this wasn't a problem, since these checks would happen in the order in which the data sources were resolved. But after #96045, the data sources were stored in a map instead of a slice, which made the order in which the checks are performed nondeterministic.

This change in behavior can cause queries to return insufficient privilege errors even when the current user has access privileges on all data sources referenced by the query. This is because the query may implicitly reference a table by ID if there is a foreign key relation from an explicitly referenced data source. If the database is changed, this ID reference will still resolve the same, but if the user is changed to one without privileges on that table, the staleness check will result in an error. This wasn't a problem before because the referencing table would always be checked first, causing the staleness check to finish without an error.

This patch fixes the bug by moving the privilege checks until after all data source resolution checks have succeeded.

Fixes #102375

Release note (bug fix): Fixed a bug introduced in versions 22.1.19, 22.2.8, and pre-release versions of 23.1 that could cause queries to return spurious insufficient privilege errors. For the bug to occur, two databases would need to have duplicate tables each with a foreign key reference to another table. The error would then occur if the same SQL string was executed against both databases concurrently by users that have privileges over only one of the tables.

@DrewKimball DrewKimball added backport-22.1.x backport-23.1.x Flags PRs that need to be backported to 23.1 labels Apr 27, 2023
@DrewKimball DrewKimball requested a review from a team as a code owner April 27, 2023 07:06
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@DrewKimball
Copy link
Collaborator Author

DrewKimball commented Apr 27, 2023

I couldn't figure out how to make a logic test reproduce this error even with --stress and --race, but I've verified using the manual reproduction from #96045 #102375 that the problem is fixed.

@DrewKimball DrewKimball requested a review from rytaft April 27, 2023 07:18
@rytaft
Copy link
Collaborator

rytaft commented Apr 27, 2023

I think you have the wrong issue number, since #96045 is the PR you merged earlier.

@rytaft
Copy link
Collaborator

rytaft commented Apr 27, 2023

Maybe you meant #102375?

@rytaft
Copy link
Collaborator

rytaft commented Apr 27, 2023

Here's a logictest you can use that reproduces the issue for me:

statement ok
CREATE DATABASE testdb1;
USE testdb1;
CREATE TABLE ab (a INT PRIMARY KEY, b INT);
INSERT INTO ab VALUES (1, 1);
CREATE TABLE xy (x INT, y INT, FOREIGN KEY (x) REFERENCES ab(a));
GRANT ALL ON xy TO testuser;
GRANT ALL ON ab TO testuser;
ALTER ROLE testuser WITH LOGIN;

statement ok
CREATE DATABASE testdb2;
USE testdb2;
CREATE TABLE ab (a INT PRIMARY KEY, b INT);
INSERT INTO ab VALUES (1, 1);
CREATE TABLE xy (x INT, y INT, FOREIGN KEY (x) REFERENCES ab(a));
CREATE USER testuser2;
GRANT ALL ON xy TO testuser2;
GRANT ALL ON ab TO testuser2;
ALTER ROLE testuser2 WITH LOGIN;

user testuser

statement ok
USE testdb1

statement ok
INSERT into xy VALUES(1, 1)

user testuser2

statement ok
USE testdb2

statement ok
INSERT into xy VALUES(1, 1)

statement ok
INSERT into xy VALUES(1, 1)

user testuser

statement ok
USE testdb1

statement ok
INSERT into xy VALUES(1, 1)

@DrewKimball
Copy link
Collaborator Author

Maybe you meant #102375?

Yes, thanks for catching that.

@DrewKimball
Copy link
Collaborator Author

Here's a logictest you can use that reproduces the issue for me:

Huh, tried a similar one and didn't get anything, nice find!

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: thanks for the quick fix!

Reviewed 2 of 2 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @cucaroach)

@rytaft
Copy link
Collaborator

rytaft commented Apr 27, 2023

pkg/sql/logictest/testdata/logic_test/schema line 1321 at r2 (raw file):

REVOKE ALL ON testdb2.xy FROM testuser2;
REVOKE ALL ON testdb2.ab FROM testuser2;
DROP USER testuser;

nit: testuser seems to exist by default so not sure you need to drop it

Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @cucaroach and @rytaft)


pkg/sql/logictest/testdata/logic_test/schema line 1321 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: testuser seems to exist by default so not sure you need to drop it

Good point, Done.

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @cucaroach)

Previously, the `opt.Metadata` staleness check would check the access
privileges for a data source immediately after checking that the data
source still resolves as it originally did. This meant that the privilege
check for one data source could occur before checking staleness of other
data sources. Before cockroachdb#96045 this wasn't a problem, since these checks
would happen in the order in which the data sources were resolved. But
after cockroachdb#96045, the data sources were stored in a map instead of a slice,
which made the order in which the checks are performed nondeterministic.

This change in behavior can cause queries to return insufficient privilege
errors even when the current user has access privileges on all data sources
referenced by the query. This is because the query may *implicitly*
reference a table by ID if there is a foreign key relation from an explicitly
referenced data source. If the database is changed, this ID reference will
still resolve the same, but if the user is changed to one without privileges
on that table, the staleness check will result in an error. This wasn't a
problem before because the referencing table would always be checked first,
causing the staleness check to finish without an error.

This patch fixes the bug by moving the privilege checks until after all
data source resolution checks have succeeded.

Fixes cockroachdb#102375

Release note (bug fix): Fixed a bug introduced in versions 22.1.19,
22.2.8, and pre-release versions of 23.1 that could cause queries to
return spurious insufficient privilege errors. For the bug to occur,
two databases would need to have duplicate tables each with a foreign key
reference to another table. The error would then occur if the same SQL
string was executed against both databases concurrently by users that have
privileges over only one of the tables.
@DrewKimball
Copy link
Collaborator Author

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Apr 28, 2023

Build succeeded:

@blathers-crl
Copy link

blathers-crl bot commented Apr 28, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 9381f84 to blathers/backport-release-22.1-102405: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.1.x failed. See errors above.


error creating merge commit from 9381f84 to blathers/backport-release-22.2-102405: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2.x failed. See errors above.


error creating merge commit from 9381f84 to blathers/backport-release-23.1.0-102405: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.1.0 failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

opt: check privileges after data sources in memo stale check
3 participants