Repository navigation
fix(ec2,iam): a tagged create is authorized twice, against ec2:CreateTags (#691) - #705
Merged
Merged
Conversation
…Tags (#691) AWS performs a second authorization pass on ec2:CreateTags when a resource-creating action carries tags: "users must also have explicit permissions to use the ec2:CreateTags action." Substrate performed only the first, so a policy granting ec2:RunInstances alone launched a tagged instance real EC2 refuses — and a policy withholding ec2:CreateTags to stop callers writing tags did not stop them. The pass is a second decision, not a second resource: the resources are the ones the create will make (one arn:aws:ec2:<region>:<acct>:<type>/* per declared TagSpecification.N.ResourceType, substrate's reading since they do not exist yet), and the context carries ec2:CreateAction, which is absent outside a create. Tags a launch template supplies count too, under the same per-scope precedence the handler applies. Follow-up #704 records that condition-key names are matched case-sensitively where AWS documents them as case-insensitive — global to the evaluator.
Codecov Report❌ Patch coverage is
📢 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 #691
AWS performs two authorization decisions on a create that carries tags:
Substrate performed only the first. So a policy granting
ec2:RunInstancesalone launched a tagged instance that real EC2 refuses — and, in the direction that actually matters for a guardrail, a policy that deliberately withheldec2:CreateTagsto stop callers writing tags did not stop them.It is a second decision, not a second resource
Different action, different resources, and a context key the create's own decision must not see. That is why it is a separate pass rather than extra ARNs on the primary loop:
instance/*and says "Users cannot tag existing resources, and users cannot tag volumes using theRunInstancesrequest."ec2:CreateActionis set only here, which is what makes AWS's documented policy mean "tag during a create, and not otherwise".What changed
ec2AuthzCreateTagsPass(ec2_authz.go) walksTagSpecification.N, and forRunInstancesthe launch template's two tag scopes, returning the scopes a create actually applies tags to — ornil, which is AWS's "Theec2:CreateTagsaction is only evaluated if tags are applied during the resource-creating action." An untagged create is decided exactly as before, as is a specification naming aResourceTypewith noTag.M.AuthController.checkEC2TagOnCreate(authz.go) evaluatesec2:CreateTagsagainst onearn:aws:ec2:<region>:<account>:<type>/*per distinct declared resource type, through the identity policies and then the boundary.ec2AuthzTemplateDataextracted out ofmergeTemplate, so the resource half and the tag half of one launch's decision cannot resolve different template versions.The pass is deliberately not gated on a list of creating operation names. An operation that does not accept
TagSpecification.Nnever carries one, so the general rule costs nothing — and a create added to substrate later is covered without anyone remembering to extend a list, which is the only direction that cannot produce a false allow. A directCreateTags/DeleteTagscarriesTag.N, notTagSpecification.N, so it resolves toniland is authorized once, as itself.<type>/*is substrate's readingThe resources have no IDs when the decision is made — that is the whole situation — so no concrete ARN exists to authorize against. The wildcard-per-declared-type shape is what all four of AWS's example policies for this key are written against (
*/*,instance/*), and it is the only shape under which its prose about volumes is true: a launch tagging both scopes needs the grant on both types, and one written forinstance/*alone refuses the volume half, namingvolume/*so the caller knows which ARN to add.TagSpecificationResourceTypevalues are ARN resource types (instance,volume,network-interface,natgateway), so there is no mapping table, and a type substrate does not otherwise model is passed through as written. Filtering to a known set would be the one direction that can produce a false allow.Precedence mirrors the handler, per scope
AWS: "The
ec2:CreateTagsaction is also evaluated if tags are provided in a launch template." The template's instance tags are the ones the launch applies only when the request named none of its own (ec2HasUserTags, the same gate the handler uses, so substrate's fleet stamp does not count as the caller naming tags); likewise for volume tags. A request that overrides a scope authorizes the tags it actually sends for that scope — otherwise AWS's approved-keys guardrail would refuse a launch over a tag the launch never applies.aws:RequestTag/*andaws:TagKeysin the second pass are the first pass's map plus whatever the template contributed, so the two decisions cannot disagree about what the request asked for. Noaws:ResourceTag/*is populated: the resources do not exist yet, so a condition about tags already on them is unsatisfiable, and fabricating one would let the tags being applied stand in for tags already present.Fail-before
The new tests use only pre-existing exported API (
CheckAccess), so they compile and run atmain. 13 failing leaf subtests, every one in the "was allowed" direction:The rows that "passed" are the untagged ones, which must keep passing — the gate on tags being present is half the rule.
One pre-existing test changed, legitimately
TestEC2_TagAuthz_RequestTagConditionEvaluates/"a launch's TagSpecification form still resolves"granted onlyec2:RunInstancesand now needs theec2:CreateTagsstatement too. That is the behaviour change, asserted: the policy it used is one real EC2 refuses.Live run
substrate server --address :4678from a scratch directory, real AWS CLI,AWS_MAX_ATTEMPTS=1, torn down withlsof -ti:4678 | xargs kill.ec2:RunInstancesonlyi-4a2f3c3eea828d66AccessDenied—ec2:CreateTagson…:instance/*instance/*+ec2:CreateAction=RunInstancesi-4eb37757e43a1c2d,Name=webappliedAccessDeniedon…:volume/*create-tagsAccessDeniedon…:instance/i-4eb3…— the key is absentec2:CreateAction=CreateVolumeAccessDeniedcreate-volumevol-dc46668522f44c04Denygated onNull:{"ec2:CreateAction":"false"}AccessDeniedcreate-tagsec2:RunInstancesonlyAccessDeniedon…:instance/*i-e9908b5b4fc4efcd,FromTemplate=yesappliedaws:RequestTag/purpose=testpurpose=testi-28ac3c03cbe1958cpurpose=prodAccessDeniedec2:CreateTagsi-bb4c127da6321b4fAccessDenied … (blocked by permission boundary)Mutation testing
Twelve mutants, anchor count asserted
== 1over the whole file,gofmt -w && go vetso a non-compiling mutant is BROKEN rather than a result, full-packagego test -race -count=1 ./emulator/plusgo test -C test/e2e ./...with no-runfilter, restore verified byte-for-byte.ec2:CreateActionis set for a directCreateTagstemplateTagsare not merged into the tagging contextec2:CreateActionis never setec2:CreateActioncarriesec2:RunInstancesrather thanRunInstancesaws:TagKeysis not populated in the tagging pass*/*for every scope instead of one ARN per resource typeBoth survivors were holes in the tests, and both were about a template contributing tags nobody asserted the values of. Every template assertion I had written turned on the pass happening (a denial naming
instance/*orvolume/*), and a denial is reached identically whether the template's tags are in the context or absent from it. Two subtests were added in the allow direction, where the difference is observable: a grant admitting onlyEnv=tmplmust permit a launch whoseEnv=tmplcomes from the template alone; and AWS'sForAllValues:StringEqualsoveraws:TagKeys ["Name"]must permit a launch whose own volume tag isName, which it does only if the overridden scope keeps the template'sTeamkey out of the context. Both mutants re-run: KILLED.Follow-up filed
#704 — substrate matches condition-key names case-sensitively where AWS documents them as case-insensitive. Found here (
ec2:createactiondoes not match) but global to the evaluator, not specific to this key, and it cuts both ways: a differently-casedDenyis inert, a differently-casedAllowis a false refusal. The caveat is recorded atec2_createtags_authz_test.goand in the docs rather than pinned as intended behaviour.Compatibility
A policy granting only the create action will start failing on a tagged create. That is what real AWS requires, and it is the reason the issue exists, but it is visible to any consumer whose IaC tags what it creates — which CDK and Terraform both do by default. The fix is the statement AWS documents:
{"Effect": "Allow", "Action": "ec2:CreateTags", "Resource": "arn:aws:ec2:us-east-1:111122223333:instance/*", "Condition": {"StringEquals": {"ec2:CreateAction": "RunInstances"}}}An untagged create is unaffected, and so is any caller whose policy already grants
ec2:*orec2:CreateTagsbroadly.Verification
gofmt -l .,go build ./...,go vet ./...,golangci-lint run ./...(0 issues),make test(race),make coverage(82.2% total),make docs-reference-check,make docs-versions,npm --prefix docs run build,go vet -C test/e2e ./...andgo test -C test/e2e ./...all green.Docs
A new
#### A tagged create is authorized twicesection in the EC2 authorization chapter: the two-decisions framing, the per-request-type ARN table, why it is the created resources and not the read ones,ec2:CreateAction's value and its absence outside a create (with theNullconstruct that detects that), the template half and its per-scope precedence, what is in the pass's context and what deliberately is not, and the boundary. TheNot coveredblock that named this pass is gone; theDeleteTagssentence that said theec2:CreateActiondifference was unmodelled now says it is, and points here. The multivalued-keys chapter listsec2:CreateActionamong the single-valued keys substrate populates.