Repository navigation
Scope the in-account reaper's ssm:SendCommand grant - #829
Merged
Merged
Conversation
pkg/reaperiam.ScanSelfPolicyDocument — the inline policy `spawn reaper deploy` writes — granted ssm:SendCommand on Resource "*" with no condition, and the reaper invokes DocumentName "AWS-RunShellScript". That is arbitrary shell as root on anything SSM-managed in the account, which strictly dominates the ec2:TerminateInstances the same document tag-gates with considerable care. The correct shape already existed in the cross-account template; the scan-self copy never got it. SendCommand is now split three ways: scoped to arn:aws:ec2:*:*:instance/* with ssm:resourceTag/spawn:managed, a separate unconditioned grant on the one document the reaper names (a document carries no tag, so conditioning it would deny every call), and GetCommandInvocation left on "*" because it takes no resource ARN. ManagedSSMTagCondition is new and deliberately separate from the ec2: one: a condition key belongs to the service of the ACTION, so reusing ManagedTagCondition here yields a statement IAM accepts and never authorizes (#652/#665). Two gates, both watched failing against compiling reverts: TestEveryDestructiveActionIsContained (the existing gates assert that what the reaper calls is granted, which is blind to a grant being too broad) and TestSSMActionsUseSSMConditionKeys. The first caught a mistake while being written — it listed ec2:DeleteTags, which the reaper does not hold, and refused to let the list claim coverage it lacked. Also drops a stale comment claiming neither the template nor the cross-account role granted these actions. Refs GHSA-2hfh-q6jv-qgjp Closes #818
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #818. Advisory: GHSA-2hfh-q6jv-qgjp (draft; publishes on merge).
The defect
pkg/reaperiam.ScanSelfPolicyDocumentis the inline policyspawn reaper deploywrites onto the in-account reaper's role. It granted:
{"Effect":"Allow","Action":["ssm:SendCommand","ssm:GetCommandInvocation"],"Resource":"*"}and the reaper invokes
DocumentName: "AWS-RunShellScript". So the role could runarbitrary shell as root on any SSM-managed instance in the account — which strictly
dominates the
ec2:TerminateInstancesthe same document tag-gates withconsiderable care. Terminating a non-spawn instance was blocked; taking it over was not.
The fix
The correct shape already existed in
deployment/cloudformation/ttl-reaper-cross-account-role.yaml; the scan-self copynever received it.
SendCommandis now three statements:arn:aws:ec2:*:*:instance/*, conditioned onssm:resourceTag/spawn:managedspawn:managedtag, soconditioning this statement too would deny every call.
SendCommandauthorizesagainst the instance and the document separately, which is why the split is required
rather than stylistic.
GetCommandInvocationon*, because it takes no resource-level ARNManagedSSMTagConditionis new and deliberately not a reuse ofManagedTagCondition:a condition key belongs to the service of the action, so
ec2:ResourceTagon anssm:call is absent from the request context and the statement becomes a silentimplicit deny (#652/#665).
No deployed role needs remediation — correcting my own flag on #818
I flagged this as having a live deploy consequence, unlike the
cmd/onboard.gobatch,on the reasoning that
spawn reaper deploywrites this policy so any already-deployedreaper keeps the broad grant. That was wrong for these accounts. Checked both:
ssm:SendCommandspawn-ttl-reaper-production-TTLReaperFunctionRole-ZJ84YZ2dCPeispawn-ttl-reaper-ec2arn:aws:ec2:*:435415984226:instance/*+ssm:resourceTag/spawn:managed, plus the separate document statementNo self-scan reaper has ever been deployed in either account, so the over-broad grant
existed only in the generator. Anyone who has run
spawn reaper deployelsewhereshould re-run it; there is nothing to remediate here.
Gates
Two, in
pkg/reaperiam/drift_test.go:TestEveryDestructiveActionIsContained— every destructive action must be boundedby a condition or by a resource narrower than
*. The package's existing coveragegates assert that everything the reaper calls is granted, which is structurally
blind to a grant being too broad; that is why this sat. Action-keyed rather than
statement-keyed, so a new statement inherits the requirement (rule 4).
fsx:DeleteFileSystemis the one recorded exception, with --fsx-lifecycle ephemeral did not reap the filesystem when the instance terminated (1.2 TiB left AVAILABLE and billing) #613 behind it.TestSSMActionsUseSSMConditionKeys— fails anssm:statement conditioned on anec2:key. The trap this package documents twice and had never asserted.Both watched failing against reverts that compile (rule 1):
The containment gate caught a mistake of mine while being written: I listed
ec2:DeleteTags, which the reaper does not hold, and its own "if it is not granted,drop it" check refused to let the list claim coverage it did not have.
Verification
make check(all 9 nested modules),make check-docs, changelog gate,scripts/lambda-deploy-census.sh,semgrep scan --config=auto --error .,Tier 0 e2e with
-count=1— all clean. No live AWS change in this PR.