TL;DR
- On each workspace’s EC2 host, the host’s management processes and members’ agents share one OS. Only the management agent and the SSM agent should get role credentials from IMDS.
- Requiring IMDSv2 and a hop limit of 1 does not stop processes on the same host. As AWS documentation suggests, access is allowed by the socket owner’s UID.
- Packets from another network namespace skip the host’s output hook and have no visible UID. A forward hook rejects anything addressed to IMDS.
Agent processes can reach 169.254.169.254 too
Each workspace gets one EC2 instance, and agents run on it. The host runs:
- the host management agent (its own UID)
- the SSM agent (root)
- members’ agents and the commands they start, each under the member’s UID
The instance role exists so the management agent can operate AWS. A command run by an agent should not get those credentials with two lines:
TOKEN=$(curl -sX PUT http://169.254.169.254/latest/api/token \
-H "X-aws-ec2-metadata-token-ttl-seconds: 60")
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/iam/security-credentials/
What IMDSv2 and the hop limit cover
The instance requires IMDSv2 (http_tokens = "required") and sets the PUT response hop limit to 1.
The hop limit is the TTL of the PUT response that carries the token. With 1, it does not reach anything one more routing hop away. AWS documentation notes that with a hop limit of 1, containers may get no response.
Processes on the same host cross no hop, though. Neither IMDSv2 nor the hop limit tells host processes apart. Turning IMDS off would break the management agent and the SSM agent.
Allow by socket UID
AWS documentation shows how to restrict IMDS with the iptables owner module. It also says allow rules are safer than deny rules, because software added later is not allowed by accident.
I wrote the same with nftables. The output hook sends IMDS traffic to a dedicated chain that passes only root and the management agent’s UID.
chain metadata {
meta skuid { 0, 1500 } accept
reject with icmpx type admin-prohibited
}
chain output {
type filter hook output priority -150; policy accept;
ip daddr 169.254.169.254 jump metadata
ip6 daddr fd00:ec2::254 jump metadata
}
The UID numbers are examples. The rules go in at the very start of host startup, before any service. destroy table and the definition are one file passed to nft -f -, so old and new rules never mix. If applying them fails, the disk is not mounted and no service starts.
Changing HTTP headers or how the address is written does not change the socket owner’s UID. A connection from a member’s UID fails immediately with ICMP admin-prohibited.
Packets from another network namespace have no UID
The UID rule has a gap. The output hook only sees packets sent by sockets on the host.
A process in a container or in a network namespace made with ip netns sends packets inside its own namespace. When they enter the host through a veth, the host is forwarding them. They pass the forward hook, not the output hook. nftables meta skuid only matches packets tied to a host socket.
With a hop limit of 1, the token response does not make it down this path today. But that depends on a single instance setting. AWS documentation also suggests raising the hop limit to 2 when containers need IMDS. The moment it is raised, this path opens.
And when no response arrives, the client waits for its timeout.
Reject by destination in the forward hook
Forwarded packets carry no UID. So anything addressed to IMDS is rejected, whoever sent it.
chain forward {
type filter hook forward priority -150; policy accept;
ip daddr 169.254.169.254 reject with icmpx type admin-prohibited
ip6 daddr fd00:ec2::254 reject with icmpx type admin-prohibited
}
The management agent and the SSM agent run in the host namespace. No legitimate user reaches IMDS through a forwarded path, so rejecting all of it costs nothing.
The rule lives in the host namespace. Becoming root inside another namespace does not let a process rewrite the host’s forward hook. Because it is reject, clients fail at once instead of waiting for a timeout.
If something like the region is needed, pass it as an environment variable. AWS documentation recommends the same for containers.
Protecting IMDS is safer when it does not rest on one layer. IMDSv2 and the hop limit are instance settings, and the UID rule covers host processes. The forward hook covers the packets neither of them sees.