Skip to content

Refuse queued commands in EXEC once the client is logged out - #243

Open
madolson wants to merge 2 commits into
unstablefrom
fix-exec-noauth-after-logout
Open

madolson wants to merge 2 commits into
unstablefrom
fix-exec-noauth-after-logout

Conversation

@madolson

@madolson madolson commented Dec 5, 2026

Copy link
Copy Markdown
Owner

If a client deletes its own ACL user inside a transaction, with MULTI, ACL DELUSER <self>, ..., EXEC, the rest of the EXEC runs as the default user without its password. Deleting the user moves the client to default, unauthenticated, and closes it after the reply, but EXEC keeps going and rechecks each remaining command against default. Scripts and SORT BY/GET check ACLs again while they run, so they get default's permissions, for example writing or copying keys outside the deleted user's key patterns. EXEC now refuses the remaining commands with NOAUTH once the client needs to authenticate again, the same check every command outside a transaction gets. Affects 7.2 and every later release.

AI generated details

Problem

ACLFreeUserAndKillClients() switches every client of the deleted user to DefaultUser with authenticated = 0 and calls freeClientOrCloseLater() (src/acl.c:590-593). ACL LOAD does the same for a client whose user is missing from the new file (src/acl.c:3336-3337). For the client running the command, that only sets close_after_command.

The NOAUTH check lives in processCommand() (src/server.c:4744), which EXEC does not go through for its queued commands. execCommand() only reruns ACLCheckAllPerm(c, ...) (src/multi.c:380), and c->user is now default. A queued command already passed its queue-time check as the original user. EVAL's server.call (through VM_Call, src/module.c:6967) and SORT ... BY/GET (src/sort.c:215) check again at run time, against the client's current user, which is default. The replies are dropped because the client is closing, but writes land. SORT ... GET <pattern> STORE <key> copies keys the user cannot read into a key it can.

The existing tests didn't catch this. "Delete a user that the client is using" in tests/unit/acl.tcl only checks that the connection closes. It doesn't run anything after the delete.

Introduced by ec1aee0 ("ACL: implement DELUSER", 2019) together with the EXEC recheck from f4d3717 (2019), so it is in every Valkey release. The ACL LOAD variant came with valkey-io#3800 (9.2.0, and backported to 8.0.10, 8.1.8, 9.0.5 and 9.1.1).

default has a password Before After
ACL DELUSER <self>, then EVAL writing outside the user's patterns write applied NOAUTH, key unchanged (test)
ACL DELUSER <self>, then SORT ... GET secret* STORE app:out secret copied into app:out NOAUTH, app:out not created (test)
ACL LOAD dropping self, then EVAL writing outside the patterns write applied NOAUTH, key unchanged (test)
ACL DELUSER <self>, then AUTH other ..., then a write as other runs runs (test)
MULTI in the AOF, loaded with requirepass set loads loads (test)

When default is nopass, authRequired() is false and the rest of the transaction still runs as default. That is unchanged. Any new connection gets the same permissions without a password, so the client gains nothing.

Fix

In the EXEC loop, a command that runs after the client needs to authenticate gets NOAUTH, as processCommand() would give it. AUTH and HELLO are exempt there and here, so a queued AUTH still logs the client in. Clients with no user skip the check. The AOF client has user == NULL and authenticated = 0 when requirepass is set, so without the c->user guard every transaction in the AOF would fail to load. The new tests/unit/multi.tcl test covers that. With the guard removed it fails on DEBUG LOADAOF with I/O error reading reply.

Alternative considered

  • Move the deleted user's clients to a user with no permissions instead of default. That fixes EXEC too, but it changes ACL WHOAMI, CLIENT LIST, ACL LOG and module-visible state for every client being closed. The client is unauthenticated, and NOAUTH is the existing answer for that.
  • Stop when the client is marked to close (close_after_reply). Clients get marked to close for reasons that have nothing to do with authentication. Some ACL SETUSER channel changes and ACL LOAD close pub/sub clients without changing their user, so this would refuse commands that are still allowed. The check should match what is actually wrong.

Testing

With src/multi.c reverted to a808a77aa and the tests kept, --loops 5:

[err]: Commands queued after ACL DELUSER of the calling user do not run as the default user in tests/unit/acl.tcl
Expected 'topsecret 0' to be equal to 'changed 1' (context: type eval line 21 cmd {assert_equal {topsecret 0} [list [r GET secret] [r EXISTS app:out]]} proc ::test)
[err]: Commands queued after ACL LOAD removes the calling user do not run as the default user in tests/unit/acl.tcl
Expected 'topsecret' to be equal to 'changed' (context: type eval line 24 cmd {assert_equal topsecret [r GET secret]} proc ::test)
...
Test Summary: 5 passed, 10 failed

This was generated by AI but verified, with love, by a human.

ACL DELUSER of the calling user, or an ACL LOAD that drops it, moves the
client to the default user unauthenticated and closes it after the reply.
Inside MULTI, EXEC kept running the rest of the queued commands, and its
per-command ACL recheck was made against the default user. Commands
queued as the deleted user passed their queue-time check, but scripts
and SORT BY/GET check ACLs again while they run, so they acted with the
default user's permissions, for example writing keys outside the deleted
user's key patterns.

EXEC now replies NOAUTH to the remaining commands once the client needs
to authenticate, the same check processCommand() makes. Clients without
a user, like the AOF client, are not subject to it.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
@madolson madolson added needs-review Staged work waiting on madolson's review approved madolson approved; ready to promote upstream and removed needs-review Staged work waiting on madolson's review labels Dec 5, 2026
tags [list "acl external:skip"] is a single tag, so the block was not skipped and CONFIG SET aclfile failed on the external server.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved madolson approved; ready to promote upstream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant