Repository navigation
fix(ec2,iam): a launch is authorized against every resource it names (#662) - #676
Merged
Merged
Conversation
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.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
8 tasks
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.
buildResourceARN'sec2arm derived its resource ARN fromreq.Params["InstanceId.1"], a parameter noRunInstancesrequest 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
"*""andresourceMatcheslets"*"satisfy any statement". It does not.resourceMatches(emulator/iam_eval.go:407-425) short-circuits only onresource == "", and callsglobMatch(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 ownResourcebegins with*.That makes the defect two-sided, and it is the false allow that decides the severity:
Allowon"*"beside an ARN-scopedDenyignored theDenyentirely. A policy written to keep workloads out of a private or shared subnet — allowec2:RunInstanceson*, deny it onsubnet/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.Allownaming 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-Allowpolicy 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.buildResourceARNsgained one gated arm beside the Organizations one;len(multi) > 0is 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.
arn:aws:ec2:{region}::image/{ami}), the format the reference gives, because an AMI is shareable. It has to stay empty:arnAccountIDreads an empty account as "no account", not "this account", so an account-scoped ARN would not match the policy AWS documents.instance/*andnetwork-interface/*for the resources whose IDs do not exist at decision time — the shape AWS's own least-privilege examples write. Includinginstance/*is what makes a policy omittinginstancea 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 toinstance/i-*matches on AWS and not here.tagsmember, so anaws:ResourceTagcondition 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.CheckAccessruns before the handler (server.go:695).resolveLaunchTemplatewas extracted to the free functionec2LookupLaunchTemplateso the decision and the handler cannot drift. AWS states this rule only inCreateFleet'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 withAccessDeniedinstead of theInvalidLaunchTemplateId.NotFoundit earns."*", which would widen the policy the caller wrote. A request naming none of them returnsniland 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 forRunInstances, 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 reachesCheckAccessat 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 viaPutUserPolicy+CreateAccessKeyand a static-credentials config, because the built-inAKIA…key resolves to no principal and an unenforced caller would make every assertion pass regardless of policy. Asserts the genericsmithy.APIErrorwithErrorCode() == "AccessDenied"(EC2's XML protocol has noExceptionsuffix), that the message names the missing ARN, and that the refusal launched nothing. Before the fix it failed withgot <nil>— the guardrail was inert.make test(race) andmake 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.:4678as the IAM user's own access key: withAllow ec2:*on*plus aDenyon one subnet, the launch into that subnet is refused namingarn:aws:ec2:us-east-1:123456789012:subnet/subnet-6239…, a launch into another succeeds, anddescribe-instancesshows 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 tosubnet/*(3); tag maps merged into a union (the false-allow — killed by the mistagging subtests);breakafter 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:
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.CreateImagesubtest ofSingleResourceOperationsAreUnchanged, i.e. only because one non-RunInstancesEC2 operation happens to carry anImageId. Sufficient, but resting on one subtest's parameter choice.|| trueon the preceding early-return condition, becausego vet'sunreachableanalyzer rejects an insertedreturn. Behaviourally the same early return.Closes #662