Skip to content

fix: a change read the condition of every conditional grant its actor held on the path, of any role, twice - #150

Merged
Michael McQuade (giraffesyo) merged 8 commits into
canaryfrom
fix/apply-condition-reads
Nov 15, 2026
Merged

Michael McQuade (giraffesyo) merged 8 commits into
canaryfrom
fix/apply-condition-reads

Conversation

@giraffesyo

@giraffesyo Michael McQuade (giraffesyo) commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • A change read the condition of every conditional grant its actor held on the path, of any role, twice.
    • The problem: authoritiesIn looked up the condition of every conditional grant the actor held on the scope's path before anything decided which mattered, and the locked phase ran it again and looked them all up again; Grantable did the same. Authorize reads only those of the roles giving the permission it checks. An actor holding 20,000 conditional cluster.login grants on a cluster, beside an unconditional cluster.admin, had each Apply granting one role read about 121,200 pages of rbac_grants (0.4 s), where Authorize read about 600. Through the API anyone's grants could put that on everyone: Import of 250 custom roles × 4 path scopes × 20 conditions to the tenant's members, within every bound, made every member's Apply on the cluster read 121,370 pages (373 ms), where Authorize read 8. Every caller of authoritiesIn had the cost: Apply, Grantable, SetRole, SetDeny, ChangeMembers and SetMemberships through their authority over groups.
    • The fix: an authority reads a grant's condition only once a decision needs it, and only for grants whose role gives the permission that decision asks about (the type's :grant power, a permission the change hands on), as a check does (conditionReads). Conditions read in a call are kept by grant id for the rest of it, so the read under the locks takes them from there: a grant's condition never changes in place, as Apply and Import replace a grant only under an equal condition and a grant made again has a new id. A grant whose condition wasn't read never changes a decision: its role gives nothing the change asks, or a grant read already gives it for good. Grants made until a time are decided together where the actor's grants can't tell their ends apart (decidedAs), so under a condition, the unread grants of roles giving what deciding the role can ask about (what it and the roles it lists give) that end at or after the grant are read first. Standing in unread by their ends instead, 64 grants of a role giving nothing asked, ending between those of a change's 64 grants, split it into 64 decisions, each walking every role the role lists: 211 ms where canary 371f67f, reading their conditions, took 30 ms. Those are read for all of a change's grants at once, for each role until the soonest it is granted until: read for each grant in turn, a change granting 64, each until a time before the one before it, beside 64 grants of a role giving a permission it asks ending between theirs, read their conditions in 64 statements, 44 ms where canary took 26 ms. The cheapest-first order applies to the grants whose conditions were read. A read that fails leaves those grants met by nothing and returns its error: where that left the actor no power to change grants there, Apply returns it rather than a refusal, and Grantable rather than no roles.

No migration, exported name or stored value changes.

Testing

  • Pages of rbac_grants read by the measured Apply (the run after its plans are built), the actor holding n conditional grants of cluster.login and 1, 50 or 0 of cluster.admin giving the power:
Case Plan Before After Authorize
n = 500 custom 3,115 / 3,115 / 3,085 114 / 261 / 77 32 / 179 / 24
n = 500 generic 3,063 / 3,062 / 3,055 66 / 212 / 55 20 / 167 / 16
n = 5,000 custom 30,380 / 30,380 / 30,349 379 / 526 / 341 163 / 310 / 155
n = 5,000 generic 30,327 / 30,327 / 30,318 330 / 477 / 318 151 / 298 / 148
n = 20,000 custom 121,254 / 121,250 / 121,219 1,253 / 1,396 / 1,211 599 / 746 / 591
n = 20,000 generic 121,237 / 121,205 / 121,221 1,208 / 1,347 / 1,193 587 / 734 / 584
the members' 20,000 custom 121,233 1,225 12
Import of 20,000 to the members custom 121,370 1,358 8

Conditions read per Apply went from 2n to 1, 50 or 0.

  • TestAChangeReadsTheConditionsOfOnlyTheGrantsItDecidesBy (new), under force_custom_plan and force_generic_plan. Without the fix: under force_custom_plan, Apply read 12259 pages of grants and 4000 conditions (generic: 12228 and 4000).
  • TestAChangeReadsNoConditionOfTheMembersGrantsGivingNothingItAsks (new). Without the fix: under force_custom_plan, Apply read 12157 pages of grants and 4000 conditions (generic: 12142 and 4000).
  • TestAChangeReadsTheConditionsItDecidesByOnce (new). Without the fix: under force_custom_plan, Apply read 12204 pages of grants, Authorize 6072, and the two 6000 conditions (generic: 12159).
  • TestAChangeDecidesARoleOnceBesideUnreadGrantsEndingBetweenItsGrants (new): an actor holding the power under a condition and 16 unread conditional grants of a role giving nothing asked, ending between those of a change granting a role listing 8 others to 16 under that condition. Without reading them first: granting 16, each until its own time, checked 288 roles, where granting one checked 18. Measured with 64 grants of a role listing 63 that list each other: 64 such grants, 28 ms, 128 role checks (before 211 ms, 8,064; canary 30 ms, 128); 20,000, 298 ms and 3,747 pages (before 403 ms; canary 452 ms and 123,753); where those grants give a permission the decision reads, 20,000 are read once, 326 ms and 63,750 pages (canary 478 ms and 123,751, read twice).
  • TestAChangeReadsTheConditionsOfGrantsEndingBetweenItsGrantsAtOnce (new): an actor holding the power under a condition and 16 unread conditional grants of a role giving cluster:view, ending between those of a change granting cluster.login to 16 under that condition, each until a time before the one before it. Without reading them for all grants at once: granting 16, each until a time before the one before it, read conditions in 16 statements, where granting one read them in 2. Measured with 64 such grants: 26 ms (before 44 ms; canary 26 ms); with 2,000, ending four minutes apart: 48 ms (before 72 ms; canary 55 ms).
  • TestAChangeWhoseConditionsCantBeReadReturnsWhy (new): the actor holding cluster.admin only under a condition, its context canceled once its grants are read. Without the check: Apply: rbac: cluster:grant denied and Grantable: <nil>, 0 roles; canary 371f67f returned context canceled for both.
  • TestEveryChangeWhoseConditionsCantBeReadReturnsWhy (new): DeleteRole, SetDeny, SetGroup and ChangeMembers by an actor holding the power only through conditions, and Apply and Grantable by one with power of its own beside a conditional cluster.admin, its context canceled once its grants are read, return context canceled, and OnRefused isn't told. Without the checks: rbac: tenant:define-roles denied, cluster:deny denied, tenant:groups denied, group:members denied, each told to OnRefused; and cluster:grant denied, and Grantable returning no error.
  • Decisions unchanged, conditional shapes: a randomized harness of conditional grants (nine conditions, one reading the subject) of :grant powers, listing roles and roles handed on, to users and groups, some expiring, a group membership ending; random Apply with conditions, ends and revokes, SetRole with GrantableRoles, SetEligibility and Activate with approvers, SetDeny with exemptions and conditions, ChangeMembers under a conditional GroupMembers grant, DeleteDeny and DeleteRole, and Grantable of four actors on a cluster and a workspace after every step: 300 seeds × 80 steps on this branch and on canary 371f67f, 24,000 steps (4,051 accepted, 17,223 refused, 2,471 invalid) and 192,000 Grantable answers alike, but for the order of two refusals one ChangeMembers joins, which varies from run to run on canary too; and 150 more seeds × 80 steps once the unread grants ending at or after a grant are read first, 12,000 steps and 96,000 answers, alike. Run under -race too, with eight goroutines calling Apply and Grantable on one Authorizer and on WithTx copies at once, and eight granting 16 each, until times before the one before, on one Authorizer.
  • Decisions unchanged, grants ending between a change's: a randomized harness of actors holding 8 to 23 conditional, expiring grants of 14 roles (:grant powers, roles listing others, custom roles listing custom roles redefined between steps), some through groups whose memberships end, under a deny exempting a group, each step an Apply of 2 to 11 grants under one condition, each until a time at, a microsecond either side of, or five minutes either side of the end of one of the actor's grants: 100 seeds × 120 steps on this branch and on canary 371f67f, about 19,000 grants decided alike, about 5,000 of them allowed.
  • Decisions unchanged: a round 20 audit's randomized change-sequence harness (random Apply with grants, expiring grants and revokes by ID in five spellings, SetRole, DeleteRole, SetEligibility, Activate, Deactivate, SetDeny and DeleteExpired by four actors under random denies, a constraint and a listing role), 80 seeds × 120 steps on this branch and on canary 371f67f: all 9,600 steps decided alike, 3,187 accepted and 6,413 refused with the same errors, and every refused change left no rows, history or events; and again on 332fa6c, 9,600 steps alike.
  • The package's change, authority, condition, listing, eligibility, deny, group and role tests: passed (927 s).
  • make tidy-check lint vulncheck and GODEBUG=fips140=only go test -race -shuffle=on -cover -timeout 60m ./... against PostgreSQL 18 on 55432, on 332fa6c: passed (2469.646s, coverage: 94.9% of statements).

Checklist

  • make check passes
  • No exported name, stored value or table was renamed (they are contract once released)

🤖 Generated with pw code

…ed the change, and Grantable listed nothing, rather than return the error
… gives for good, and a failed read returns its error
… into a decision for each beside unread conditional grants ending between theirs
… actor's power alone through them or beside its own
… conditions of the actor's grants ending between theirs in a statement for each
@giraffesyo
Michael McQuade (giraffesyo) marked this pull request as ready for review November 15, 2026 01:28
@giraffesyo
Michael McQuade (giraffesyo) merged commit 9872532 into canary Nov 15, 2026
9 checks passed
@giraffesyo
Michael McQuade (giraffesyo) deleted the fix/apply-condition-reads branch November 15, 2026 02:15
@github-actions github-actions Bot mentioned this pull request Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant