Skip to content

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

Closed

Description

@scttfrdmn

What

ec2:RunInstances names five required resource types. Verified against the Service
Authorization Reference markdown source
(https://docs.aws.amazon.com/service-authorization/latest/reference/list_ec2.md, the
[RunInstances] action block, asterisked entries):

  • image*
  • instance*
  • network-interface*
  • security-group*
  • subnet*

Substrate builds one ARN for EC2, and only from InstanceId.1
(emulator/authz.go, the case "ec2": arm of buildResourceARN). A RunInstances
request has no InstanceId.1 — the instance does not exist yet — so the resource
resolves to "*", and resourceMatches lets "*" satisfy any statement. Every
RunInstances request is therefore authorized against a wildcard.

Why it matters

This is the same defect class as #660, in its most commonly written form. The canonical
least-privilege launch policy scopes the launch to an approved AMI, a subnet and a
security group:

{"Effect":"Allow","Action":"ec2:RunInstances","Resource":[
  "arn:aws:ec2:us-east-1::image/ami-approved",
  "arn:aws:ec2:us-east-1:123456789012:subnet/subnet-private",
  "arn:aws:ec2:us-east-1:123456789012:security-group/sg-restricted",
  "arn:aws:ec2:us-east-1:123456789012:instance/*",
  "arn:aws:ec2:us-east-1:123456789012:network-interface/*"
]}

Under substrate, a launch using a different AMI, subnet or security group is allowed,
because none of those ARNs reaches the decision. A consumer testing that their guardrail
holds gets a green run from a policy that does not hold — the false-allow direction, and
the one a privilege boundary must never fail in.

RunInstances is also the operation AWS uses to document the rule
(reference_policies_elements_condition_operators.html): "The entire statement is
evaluated against every resource that is required
to launch the instance." So unlike
Organizations MoveAccount, this one has an explicit published statement to implement
against.

The machinery already exists

#660 (v0.102.0) generalized CheckAccess to take []authzResource — (ARN, tags) pairs,
all of which must allow, each ARN matched against its own tags — with
orgAuthzMoveAccountResources as its only caller. This issue is its second caller:
an ec2AuthzRunInstancesResources that reads ImageId, SubnetId,
SecurityGroupId.N / NetworkInterface.N.* out of the query params and returns a pair
per named resource, skipping absent members (never "*", which would widen the caller's
policy) and pairing each ARN with the tags on the resource it names.

Acceptance criteria

  • A RunInstances request is authorized against the AMI, subnet and security groups
    it names, in addition to the instance and network-interface ARNs, and a policy
    omitting any of them is denied with the failing ARN in the message.
  • Absent members are skipped, not resolved to "*". A request naming no AMI behaves
    as it does today rather than silently widening.
  • SecurityGroupId.N and the launch-template / NetworkInterface.N.Groups.N forms
    are both read — a policy must not be bypassable by moving the security group from
    one request shape to the other.
  • Each ARN is matched against its own tags, so an aws:ResourceTag condition
    about the subnet cannot be satisfied by a tag on the AMI.
  • DescribeInstances, TerminateInstances and every other EC2 operation keep
    today's single-resource decision — this must not become a service-wide change.
  • The permission boundary is checked against every resource, not just the first
    (the survivor mutation testing found in Organizations: MoveAccount does not authorize against SourceParentId/DestinationParentId, only AccountId #660).
  • Tests fail before the change; an e2e journey drives it through the SDK.
  • docs/services.md records which EC2 operations name several resources.

Not in scope

Whether ec2:CreateTags on launch (the TagSpecifications path) needs the same
treatment. Worth a look, but it has its own asterisk set and belongs in its own issue.

Activity

  1. modified the milestones: v0.103.0, v0.104.0 on Aug 16, 2026
  2. scttfrdmn commented on Aug 20, 2026

    @scttfrdmn
    OwnerAuthor

    Moved to v0.104.0.

    This rewrites the same runInstancesWithTags parsing that #666 is changing in v0.103.0 — #666 adds BlockDeviceMapping.N.* parsing and materializes volumes in the per-instance loop. Landing this on top of that, rather than beside it, keeps two PRs from editing the same function for unrelated reasons.

    Nothing in the analysis changes: buildResourceARN's case "ec2": still resolves a RunInstances request to "*" (the request carries no InstanceId.1), so the five resource types AWS marks required — image*, instance*, network-interface*, security-group*, subnet* — are not enforced, and a wildcard satisfies any statement. The multi-resource machinery that landed in v0.102.0 (#660) is what this will build on.

  3. scttfrdmn commented on Aug 20, 2026

    @scttfrdmn
    OwnerAuthor

    Correction: the mechanism, and the false allow this actually produces

    The issue states the resource resolves to "*" "and resourceMatches lets "*" satisfy any statement." That is not what the code does.

    resourceMatches (emulator/iam_eval.go:407-425) short-circuits only on resource == "", not on "*", and calls globMatch(stmtEntry, requestResource) — the statement's Resource entry is the pattern and the request resource is the value (iam_eval.go:519-560). So a request resource of "*" matches only a statement whose own Resource entry begins with *.

    The consequence: today's defect is two-sided.

    • The least-privilege policy quoted above — the one naming the five required ARNs — denies every launch, because none of those entries is a pattern matching the literal "*". That is a false deny.
    • The genuine false allow is a broad Allow ec2:RunInstances on "*" beside an ARN-scoped Deny. The ARN-scoped Deny never matches, so a guardrail written to fence off a particular subnet, AMI or security group is silently inert.

    That second shape is the same one #660's TestOrganizations_Authz_MoveAccountParentScopedDenyBlocks pins, and it is the fail-before/pass-after test for this issue.

    The fix and its severity are unchanged; only which test demonstrates the false allow changes.

  4. scttfrdmn commented on Aug 20, 2026

    @scttfrdmn
    OwnerAuthor

    Shipped in v0.104.0 (#676).

    A launch now resolves all five resources the Service Authorization Reference marks required — image, subnet, each security group, each network interface brought by ID, and the instance — and every one is evaluated against the caller's policies with the permission boundary applied to each, each ARN matched against its own tags. The denial names the first resource the policies do not allow, in a fixed order.

    Both directions verified live against a built binary as a real IAM user's own access key: Allow ec2:* on * plus a Deny on one subnet refuses the launch into that subnet naming its ARN and permits a launch into another, with DescribeInstances confirming the refusal wrote nothing; and a policy naming exactly the five required ARNs launches, while the same policy minus the security-group ARN is refused naming that ARN. Nine mutants, nine killed — including the boundary-outside-the-loop mutation that survived #660's harness, now killed through both callers.

    Three gaps this work scoped out are filed and on v0.105.0: #673 (the default VPC's subnet and security group are resolved after the authorization decision), #674 (ec2:CreateTags on the TagSpecifications path), and the fleet path, which never reaches CheckAccess at all — documented in docs/services.md rather than filed, since it is a property of how fleets launch internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions