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

sql: ensures that users can't access the DB after DROP USER #20718

Closed
jordanlewis opened this issue Dec 14, 2017 · 10 comments · Fixed by #104215
Closed

sql: ensures that users can't access the DB after DROP USER #20718

jordanlewis opened this issue Dec 14, 2017 · 10 comments · Fixed by #104215
Assignees
Labels
A-authentication Pertains to authn subsystems A-cc-enablement Pertains to current CC production issues or short-term projects A-sql-execution Relating to SQL execution. A-sql-privileges SQL privilege handling and permission checks. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-server-triaged-202105

Comments

@jordanlewis
Copy link
Member

jordanlewis commented Dec 14, 2017

In both secure and insecure modes, a connected user is permitted to stay connected and run queries even after the user has been DROPped.

This is counterintuitive and possibly a security risk.

Edit 2023-03-29 from @rafiss:

Note that PG does not end the SQL sessions of a user that got dropped, so it's not clear if we need that behavior. the thing PG does that we should do is check that the user exists whenever we check privileges.

Mitigation

a deactivated/deleted user can be "ejected" as follows:

  1. by manually running CANCEL SESSIONS with a suitable SELECT clause that selects only that user's session.
  2. by deleting the relevant entries from system.web_sessions.

Jira issue: CRDB-5913

Epic CRDB-26131

@mberhault
Copy link
Contributor

@vivek for triage.

Users can't be dropped when they still have privileges. And privilege checks should fail for all queries (transaction, time-travelling queries, etc...) (if not, that's a bug).
Cancelling a session on user drop probably involves some fairly big feedback. Maybe checking for user existence if privilege checks fail?

@jordanlewis
Copy link
Member Author

There are queries that could consume a lot of server resources that don't touch tables, like SELECT generate_series. Additionally, every open session consumes resources. See this forum post for an example: https://forum.cockroachlabs.com/t/deleting-existing-sessions/1216/4

It would be helpful for this user to have sessions for deleted users get terminated.

@mberhault
Copy link
Contributor

I agree this is wanted. I'm just saying turning error deletion into a session termination is not a trivial thing. But then again, I don't know anything about how we hook sessions into things.

@bdarnell
Copy link
Contributor

There's an RFC for session cancellation: #17252.

@dianasaur323
Copy link
Contributor

Session cancellation is marked for 2.0 in Airtable. @vivekmenezes mentioning punting on the decision on how would implement it until Jan.

@vivekmenezes
Copy link
Contributor

pinged the RFC

@knz knz added A-sql-privileges SQL privilege handling and permission checks. A-sql-execution Relating to SQL execution. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) labels May 9, 2018
@tbg tbg added this to the 2.2 milestone Sep 27, 2018
@petermattis petermattis removed this from the 2.2 milestone Oct 5, 2018
@asubiotto
Copy link
Contributor

cc @aaron-crl

@yuzefovich
Copy link
Member

Looks like this would fall onto SQL Features team, so I'll move the issue in their project.

@rafiss
Copy link
Collaborator

rafiss commented Jun 30, 2021

although this seems like a reasonable idea, I tested with Postgres and observed that neither DROP USER nor ALTER USER NOLOGIN cause an existing session for that user to be terminated.

so i'll consider this "blocked" until we hear from product or our customers that this issue is important to address. i'm worried about accidentally breaking compatibility if we implement this without being sure.

@knz knz added A-authentication Pertains to authn subsystems A-cc-enablement Pertains to current CC production issues or short-term projects labels Jul 29, 2021
@knz knz changed the title sql: make DROP USER and ALTER USER NOLOGIN kill all existing sessions sql: ensures that users can't access the DB after DROP USER Mar 29, 2023
@exalate-issue-sync exalate-issue-sync bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed T-server-and-security DB Server & Security labels Jun 1, 2023
@craig craig bot closed this as completed in 618a893 Jun 7, 2023
craig bot pushed a commit that referenced this issue Oct 10, 2023
111662: opt: add lock operator r=DrewKimball a=michae2

**execbuilder: change error message to match PostgreSQL**

Release note (sql change): Change the following error message:

```
statement error cannot execute FOR UPDATE in a read-only transaction
```

to this to match PostgreSQL:

```
statement error cannot execute SELECT FOR UPDATE in a read-only transaction
```

**opt: add lock operator**

Add a new implementation of `SELECT FOR UPDATE` and `SELECT FOR SHARE`
statements. Instead of locking during the initial row fetch, this new
implementation constructs a `Lock` operator on the top of the query plan
which performs the locking phase using a locking semi-join lookup.

During optbuilder we build plans with both `Lock` operators and
initial-row-fetch locking. During execbuilder we decide which
implementation to use based on the isolation level and whether
`optimizer_use_lock_op_for_serializable` is set. If the new
implementation is chosen, `Lock` operators become locking
semi-LookupJoins.

In some cases these new plans will have superfluous lookup joins. A
future PR will optimize away some of these superfluous lookup joins.

Fixes: #57031, #75457

Epic: CRDB-25322

Release note (sql change): Add a new session variable,
`optimizer_use_lock_op_for_serializable`, which when set enables a new
implementation of `SELECT FOR UPDATE`. This new implementation of
`SELECT FOR UPDATE` acquires row locks *after* any joins and filtering,
and always acquires row locks on the primary index of the table being
locked. This more closely matches `SELECT FOR UPDATE` behavior in
PostgreSQL, but at the cost of more round trips from gateway node to
replica leaseholder.

Under read committed isolation (and other isolation levels weaker than
serializable) we will always use this new implementation of `SELECT FOR
UPDATE` regardless of the value of
`optimizer_use_lock_op_for_serializable` to ensure correctness.

111934: sql: cache role existence checks to fix perf regression r=rafiss a=rafiss

In 12ec26f, we started to check for the existence of a role whenever a privilege for the public role was checked. This can hapen multiple times during some pg_catalog queries, so it introduced a performance regression.

Now, the role existence is cached so we avoid the penalty. The existence of a role is cached for the duration of the entire transaction, and also gets inherited by any internal executor used to implement a command run by the user's transaction.

No release note since this is new in 23.2.

informs #20718
fixes #112102
Release note: None

Co-authored-by: Michael Erickson <[email protected]>
Co-authored-by: Rafi Shamim <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-authentication Pertains to authn subsystems A-cc-enablement Pertains to current CC production issues or short-term projects A-sql-execution Relating to SQL execution. A-sql-privileges SQL privilege handling and permission checks. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-server-triaged-202105
Projects
None yet
Development

Successfully merging a pull request may close this issue.