Skip to content

fix(ec2,iam): a tagged create is authorized twice, against ec2:CreateTags (#691) - #705

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/691-createtags-second-pass
Aug 21, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/691-createtags-second-pass

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #691

AWS performs two authorization decisions on a create that carries tags:

If tags are specified in the resource-creating action, Amazon performs additional authorization on the ec2:CreateTags action to verify if users have permissions to create tags. Therefore, 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 that real EC2 refuses — and, in the direction that actually matters for a guardrail, a policy that deliberately withheld ec2:CreateTags to 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:

  • The resources are the ones the create will make, not the ones it reads. A launch is authorized against an AMI, a subnet and a security group; its tags land on an instance and a volume. AWS's own example turns on exactly that: it scopes the tagging grant to instance/* and says "Users cannot tag existing resources, and users cannot tag volumes using the RunInstances request."
  • ec2:CreateAction is set only here, which is what makes AWS's documented policy mean "tag during a create, and not otherwise".
  • It runs after the primary decision succeeds, the order AWS describes — a caller who cannot perform the create hears about the create first.

What changed

  • ec2AuthzCreateTagsPass (ec2_authz.go) walks TagSpecification.N, and for RunInstances the launch template's two tag scopes, returning the scopes a create actually applies tags to — or nil, which is AWS's "The ec2:CreateTags action 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 a ResourceType with no Tag.M.
  • AuthController.checkEC2TagOnCreate (authz.go) evaluates ec2:CreateTags against one arn:aws:ec2:<region>:<account>:<type>/* per distinct declared resource type, through the identity policies and then the boundary.
  • ec2AuthzTemplateData extracted out of mergeTemplate, 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.N never 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 direct CreateTags/DeleteTags carries Tag.N, not TagSpecification.N, so it resolves to nil and is authorized once, as itself.

<type>/* is substrate's reading

The 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 for instance/* alone refuses the volume half, naming volume/* so the caller knows which ARN to add.

TagSpecification ResourceType values 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:CreateTags action 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/* and aws:TagKeys in 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. No aws: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 at main. 13 failing leaf subtests, every one in the "was allowed" direction:

--- FAIL: TestEC2_Authz_TaggedCreateRequiresCreateTags
    --- FAIL: .../a_launch_tagging_its_instance
    --- FAIL: .../a_launch_tagging_its_volumes
    --- FAIL: .../a_volume_created_with_tags
    --- PASS: .../an_untagged_launch
    --- PASS: .../an_untagged_volume
    --- PASS: .../a_tag_specification_carrying_no_tags
--- FAIL: TestEC2_Authz_CreateActionScopesTheTaggingGrant
    --- FAIL: .../tagging_the_volumes_in_the_same_launch_is_not
    --- FAIL: .../the_value_is_the_creating_operation_and_is_case-sensitive
--- FAIL: TestEC2_Authz_CreateActionIsAbsentOutsideACreate/a_tagged_launch
--- FAIL: TestEC2_Authz_TaggingPassSeesTheRequestTags
    --- FAIL: .../the_prescribed_key_with_another_value
    --- FAIL: .../the_prescribed_tag_plus_one_more
--- FAIL: TestEC2_Authz_TemplateSuppliedTagsAreAuthorized
    --- FAIL: .../a_template's_instance_tags_require_ec2:CreateTags
    --- FAIL: .../a_template's_volume_tags_name_the_volume_wildcard
    --- FAIL: .../the_request's_own_tags_replace_the_template's
--- FAIL: TestEC2_Authz_BoundaryAppliesToTheTaggingPass

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 only ec2:RunInstances and now needs the ec2:CreateTags statement too. That is the behaviour change, asserted: the policy it used is one real EC2 refuses.

Live run

substrate server --address :4678 from a scratch directory, real AWS CLI, AWS_MAX_ATTEMPTS=1, torn down with lsof -ti:4678 | xargs kill.

Policy Call Result
ec2:RunInstances only untagged launch i-4a2f3c3eea828d66
the same tagged launch AccessDenied — ec2:CreateTags on …:instance/*
AWS's documented grant on instance/* + ec2:CreateAction=RunInstances tagged launch i-4eb37757e43a1c2d, Name=web applied
the same launch tagging instance and volume AccessDenied on …:volume/*
the same standalone create-tags AccessDenied on …:instance/i-4eb3… — the key is absent
grant conditioned on ec2:CreateAction=CreateVolume tagged launch AccessDenied
the same tagged create-volume vol-dc46668522f44c04
Deny gated on Null:{"ec2:CreateAction":"false"} tagged launch AccessDenied
the same standalone create-tags tagged
ec2:RunInstances only launch from a tagging template AccessDenied on …:instance/*
+ the documented grant the same launch i-e9908b5b4fc4efcd, FromTemplate=yes applied
grant conditioned on aws:RequestTag/purpose=test launch tagging purpose=test i-28ac3c03cbe1958c
the same launch tagging purpose=prod AccessDenied
boundary omitting ec2:CreateTags untagged launch i-bb4c127da6321b4f
the same tagged launch AccessDenied … (blocked by permission boundary)

Mutation testing

Twelve mutants, anchor count asserted == 1 over the whole file, gofmt -w && go vet so a non-compiling mutant is BROKEN rather than a result, full-package go test -race -count=1 ./emulator/ plus go test -C test/e2e ./... with no -run filter, restore verified byte-for-byte.

Mutant Verdict
the second pass is skipped entirely KILLED
the pass runs on every ec2 request, so ec2:CreateAction is set for a direct CreateTags KILLED
the template's scopes are ignored KILLED
the instance scope's precedence gate is dropped KILLED
the volume scope's precedence gate is dropped SURVIVED, then killed
an empty tag scope still records its resource type KILLED
templateTags are not merged into the tagging context SURVIVED, then killed
ec2:CreateAction is never set KILLED
ec2:CreateAction carries ec2:RunInstances rather than RunInstances KILLED
aws:TagKeys is not populated in the tagging pass KILLED
the boundary is not consulted in the tagging pass KILLED
one */* for every scope instead of one ARN per resource type KILLED

Both 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/* or volume/*), 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 only Env=tmpl must permit a launch whose Env=tmpl comes from the template alone; and AWS's ForAllValues:StringEquals over aws:TagKeys ["Name"] must permit a launch whose own volume tag is Name, which it does only if the overridden scope keeps the template's Team key 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:createaction does not match) but global to the evaluator, not specific to this key, and it cuts both ways: a differently-cased Deny is inert, a differently-cased Allow is a false refusal. The caveat is recorded at ec2_createtags_authz_test.go and 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:* or ec2:CreateTags broadly.

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 ./... and go test -C test/e2e ./... all green.

Docs

A new #### A tagged create is authorized twice section 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 the Null construct 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. The Not covered block that named this pass is gone; the DeleteTags sentence that said the ec2:CreateAction difference was unmodelled now says it is, and points here. The multivalued-keys chapter lists ec2:CreateAction among the single-valued keys substrate populates.

…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.
@scttfrdmn scttfrdmn added this to the v0.106.0 milestone Aug 21, 2026
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.64706% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_authz.go 95.83% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit b77f1aa into main Aug 21, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/691-createtags-second-pass branch August 21, 2026 08:02
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,iam: the second ec2:CreateTags authorization pass for a resource-creating action that carries tags

1 participant