Skip to content

fix(ec2,iam): a launch is authorized against every resource it names (#662) - #676

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/662-runinstances-authz-resources
Aug 20, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/662-runinstances-authz-resources

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

buildResourceARN's ec2 arm derived its resource ARN from req.Params["InstanceId.1"], a parameter no RunInstances request carries, so every launch was authorized against the literal string "*". The Service Authorization Reference marks five required resource types for the action — image, instance, network-interface, security-group, subnet — and evaluates the caller's policies against all of them.

The mechanism, corrected

#662 states the resource resolves to "*" "and resourceMatches lets "*" satisfy any statement". It does not. resourceMatches (emulator/iam_eval.go:407-425) short-circuits only on resource == "", and calls globMatch(stmtEntry, requestResource) — the statement is the pattern and the request's resource is the value (iam_eval.go:519-560). So "*" as a request resource matches only a statement whose own Resource begins with *.

That makes the defect two-sided, and it is the false allow that decides the severity:

  • A broad Allow on "*" beside an ARN-scoped Deny ignored the Deny entirely. A policy written to keep workloads out of a private or shared subnet — allow ec2:RunInstances on *, deny it on subnet/subnet-private — read as a privilege boundary and was inert. This is the same class as Organizations: MoveAccount does not authorize against SourceParentId/DestinationParentId, only AccountId #660, which shipped alone as v0.102.0.
  • A least-privilege Allow naming the five documented ARNs denied every launch, because none of those patterns matches the value *. This is the error a consumer hits first, and it is what makes the broad-Allow policy look like the safe configuration.

What changed

emulator/ec2_authz.go (new) resolves every resource a launch names and returns them as []authzResource, reusing the multi-resource machinery #660 built: all-must-allow, a fresh condition context per resource, the permission boundary evaluated inside the loop, and the first failure naming the ARN. buildResourceARNs gained one gated arm beside the Organizations one; len(multi) > 0 is what keeps every other EC2 operation on the single-resource path.

Fixed order — image, subnet, security groups, network interfaces, instance — so the denial message is deterministic and replay-stable, with request-named resources first because those are the ARNs a caller can act on.

  • The AMI's ARN carries no account field (arn:aws:ec2:{region}::image/{ami}), the format the reference gives, because an AMI is shareable. It has to stay empty: arnAccountID reads an empty account as "no account", not "this account", so an account-scoped ARN would not match the policy AWS documents.
  • instance/* and network-interface/* for the resources whose IDs do not exist at decision time — the shape AWS's own least-privilege examples write. Including instance/* is what makes a policy omitting instance a denial, the answer real AWS gives. An interface the request brings by ID is named instead. The cost is a documented limit: a policy scoped to instance/i-* matches on AWS and not here.
  • Each ARN carries only its own tags, read through one reader over the shared tags member, so an aws:ResourceTag condition written about the subnet cannot be satisfied by a tag on the AMI. An unreadable record yields no tags, never a partial map — an absent condition key denies, whereas a fabricated one would let a storage fault grant access.
  • Launch-template resources are authorized as if the request had named them, under the same field-by-field, all-or-nothing precedence the handler uses, resolved independently because CheckAccess runs before the handler (server.go:695). resolveLaunchTemplate was extracted to the free function ec2LookupLaunchTemplate so the decision and the handler cannot drift. AWS states this rule only in CreateFleet's description: "Resource-level permissions for this action do not include the resources specified in a launch template. To specify resource-level permissions for resources specified in a launch template, you must include the resources in the RunInstances action statement." A template that cannot be read contributes nothing rather than answering a missing template with AccessDenied instead of the InvalidLaunchTemplateId.NotFound it earns.
  • An omitted resource is skipped, not resolved to "*", which would widen the policy the caller wrote. A request naming none of them returns nil and is decided exactly as before.

Deliberately not covered (stated in docs/services.md, not left to be discovered)

volume (asterisked only inside the reference's scenario rows, never on the action's own resource list — requiring it would refuse a policy naming exactly the five documented types); the launch-template ARN (not marked required for RunInstances, same reasoning); the EC2 condition keys; a launch whose subnet and security group come from the default VPC, resolved after the decision (#673); and the fleet path, which never reaches CheckAccess at all (ec2_fleet.go:505-510).

Verification

  • emulator/ec2_authz_test.go — 12 tests. Every behavioural one confirmed to fail before the change (probe injected after the operation gate); the two that passed before are exactly the ones pinning unchanged behaviour, which is correct by design.
  • test/e2e/journey_ec2_runinstances_authz_test.go — a real IAM user via PutUserPolicy + CreateAccessKey and a static-credentials config, because the built-in AKIA… key resolves to no principal and an unenforced caller would make every assertion pass regardless of policy. Asserts the generic smithy.APIError with ErrorCode() == "AccessDenied" (EC2's XML protocol has no Exception suffix), that the message names the missing ARN, and that the refusal launched nothing. Before the fix it failed with got <nil> — the guardrail was inert.
  • make test (race) and make coverage: emulator 82.8%, total 82.1%. gofmt -l, go build ./..., go vet ./..., golangci-lint run ./... (0 issues), make docs-reference docs-reference-check docs-versions, npm --prefix docs run build, go test -C test/e2e ./... — all clean.
  • Live run against a built binary on :4678 as the IAM user's own access key: with Allow ec2:* on * plus a Deny on one subnet, the launch into that subnet is refused naming arn:aws:ec2:us-east-1:123456789012:subnet/subnet-6239…, a launch into another succeeds, and describe-instances shows exactly one instance — the refusal wrote nothing. A policy naming the five required ARNs launches; the same policy minus the security-group ARN is refused naming that ARN.

Mutation testing — 9 mutants, 9 killed, 0 survivors

Resolver returns nil (the bug itself, 11 tests); AMI dropped (5); absent subnet resolved to subnet/* (3); tag maps merged into a union (the false-allow — killed by the mistagging subtests); break after the first resource (9 EC2 + 4 Organizations tests); boundary evaluated against only the first resource (#660's own mutation survivor, now killed twice over); image ARN gains the account field (5); template fallback skipped; operation gate deleted.

Three kill margins worth recording rather than reporting clean:

  • The tag-union mutant — the most dangerous of the set — is killed by exactly one test, TestEC2_Authz_ResourceTagsPairWithTheirOwnResource. A union map still yields the right five ARNs and differs only when a condition names a tag a different resource carries, so that test's per-resource mistagging subtests are the only oracle for it.
  • The gate-deleted mutant is killed only by the CreateImage subtest of SingleResourceOperationsAreUnchanged, i.e. only because one non-RunInstances EC2 operation happens to carry an ImageId. Sufficient, but resting on one subtest's parameter choice.
  • Two mutants had to be re-expressed as || true on the preceding early-return condition, because go vet's unreachable analyzer rejects an inserted return. Behaviourally the same early return.

Closes #662

buildResourceARN's ec2 arm derived its ARN from InstanceId.1, a parameter
no RunInstances request carries, so every launch was decided against the
literal string "*". The Service Authorization Reference marks five
required resource types for the action and evaluates the caller's
policies against all of them.
@scttfrdmn
scttfrdmn merged commit 2039f22 into main Aug 20, 2026
15 checks passed
@scttfrdmn
scttfrdmn deleted the fix/662-runinstances-authz-resources branch August 20, 2026 19:36
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.94118% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_authz.go 92.30% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

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.

ec2:RunInstances authorizes against the instance ARN alone, so an image/subnet/security-group-scoped policy is not enforced

1 participant