Skip to content

Scope the in-account reaper's ssm:SendCommand grant - #829

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/818-reaper-ssm-scope
Oct 11, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/818-reaper-ssm-scope

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #818. Advisory: GHSA-2hfh-q6jv-qgjp (draft; publishes on merge).

The defect

pkg/reaperiam.ScanSelfPolicyDocument is the inline policy spawn reaper deploy
writes 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 run
arbitrary shell as root on any SSM-managed instance in the account — which strictly
dominates the ec2:TerminateInstances the same document tag-gates with
considerable 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 copy
never received it. SendCommand is now three statements:

  • the instance, arn:aws:ec2:*:*:instance/*, conditioned on ssm:resourceTag/spawn:managed
  • the document, unconditioned — a document carries no spawn:managed tag, so
    conditioning this statement too would deny every call. SendCommand authorizes
    against the instance and the document separately, which is why the split is required
    rather than stylistic.
  • GetCommandInvocation on *, because it takes no resource-level ARN

ManagedSSMTagCondition is new and deliberately not a reuse of ManagedTagCondition:
a condition key belongs to the service of the action, so ec2:ResourceTag on an
ssm: call is absent from the request context and the statement becomes a silent
implicit 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.go batch,
on the reasoning that spawn reaper deploy writes this policy so any already-deployed
reaper keeps the broad grant. That was wrong for these accounts. Checked both:

role account ssm:SendCommand
spawn-ttl-reaper-production-TTLReaperFunctionRole-ZJ84YZ2dCPei 966362334030 absent entirely across all 4 inline policies — the SAM template's block is conditional and unset
spawn-ttl-reaper-ec2 435415984226 already correctly scoped — arn:aws:ec2:*:435415984226:instance/* + ssm:resourceTag/spawn:managed, plus the separate document statement

No 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 deploy elsewhere
should re-run it; there is nothing to remediate here.

Gates

Two, in pkg/reaperiam/drift_test.go:

  • TestEveryDestructiveActionIsContained — every destructive action must be bounded
    by a condition or by a resource narrower than *. The package's existing coverage
    gates 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:DeleteFileSystem is 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 an ssm: statement conditioned on an
    ec2: key. The trap this package documents twice and had never asserted.

Both watched failing against reverts that compile (rule 1):

revert 1  Resource "*", no condition
          -> ssm:SendCommand is granted on Resource "*" with no condition.
revert 2  ManagedTagCondition (ec2: key) on the ssm: statement
          -> conditioned on "ec2:ResourceTag/spawn:managed" ... silent implicit DENY (#665)

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.

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

codecov Bot commented Oct 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 13ca7a2 into main Oct 11, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/818-reaper-ssm-scope branch October 11, 2026 17:38
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.

Scope the in-account reaper's SSM grant the way the cross-account template already does

1 participant