Serverless Container Security:
Fargate and Cloud Run Attack Surfaces

No host OS access doesn't mean no attack surface. Serverless containers shift the threat model — abstracting infrastructure while quietly preserving the most dangerous primitives: IAM credentials, network egress, and runtime injection.

Serverless Container Security

The Shifted Threat Model

AWS Fargate and Google Cloud Run are marketed on the promise of no server management — no patching, no SSH, no direct host access. Security teams often interpret this as a smaller attack surface. That interpretation is partially correct and dangerously incomplete.

What serverless containers remove: host-level lateral movement, kernel exploit persistence, and direct node access by the attacker. What they preserve: container escape to cloud API, IAM credential abuse, supply chain compromise via the container image, and runtime injection through the application process itself.

Core Misconception

Serverless doesn't mean "no infrastructure to attack." It means the infrastructure is now owned by the cloud provider — but the container image, the IAM role, the environment variables, and the network configuration are still owned and mis-configured by you.

IMDS and Credential Theft

AWS Fargate tasks run with an ECS task metadata endpoint that exposes temporary IAM credentials. Unlike EC2 IMDSv2 which requires session-oriented token requests, the ECS task metadata endpoint at 169.254.170.2 is reachable from within the container without any special hop restriction.

# From inside a compromised Fargate container
curl http://169.254.170.2/v2/credentials/<credentials_relative_uri>

# Response leaks:
# - AccessKeyId
# - SecretAccessKey
# - Token
# - Expiration

# Attacker then uses these from outside
export AWS_ACCESS_KEY_ID=ASIA...
export AWS_SECRET_ACCESS_KEY=...
export AWS_SESSION_TOKEN=...
aws sts get-caller-identity

The relative URI needed to fetch credentials is exposed via AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable. An attacker who can execute arbitrary code (SSRF, deserialization, command injection) can trivially pivot to AWS API access.

Cloud Run Equivalent

Google Cloud Run services run as a service account. The metadata server at 169.254.169.254 returns service account tokens. Any SSRF vulnerability in the application can fetch http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token and obtain a bearer token for all APIs the service account can access.

IAM Task Role Over-Privilege

ECS Fargate tasks have an execution role (used by ECS to pull images and write logs) and a task role (used by your application code). Engineers frequently attach broad policies to both because the specific permissions needed are "unclear" or because a broader policy "works."

Common Task Role Mistakes

  • Attaching AmazonS3FullAccess when only one bucket needs read access
  • Using arn:aws:iam::*:policy/AdministratorAccess on the execution role
  • Granting secretsmanager:GetSecretValue with resource * instead of specific secret ARNs
  • Allowing iam:PassRole without iam:PassedToService conditions
  • Execution role having ecr:* rather than just pull permissions on the specific repo

Once an attacker has credentials via IMDS theft, the blast radius is determined entirely by the task role's permissions. A task role with S3 full access means an attacker can exfiltrate all buckets in the account. With iam:PassRole they can escalate to admin.

Attack Flow — Fargate Privilege Escalation

1

SSRF vulnerability in Fargate task application code

↓
2

Fetch AWS_CONTAINER_CREDENTIALS_RELATIVE_URI from environment or metadata endpoint

↓
3

Retrieve temporary task role credentials from 169.254.170.2

↓
4

Use credentials from attacker-controlled machine — no container needed

↓
5

If task role has iam:PassRole: create Lambda with admin role, instant privilege escalation

Image Supply Chain Risk

Both Fargate and Cloud Run pull container images from registries. The image is the entire trust boundary — whatever is baked into the image runs with the task's IAM role and network permissions. Supply chain attacks against base images, package dependencies, or CI pipelines that build images are therefore extremely high value.

Critical Risk

Pulling node:18-alpine from Docker Hub directly in production gives Docker Hub — and anyone who can compromise that image — complete control over your Fargate task's AWS credentials. Use ECR private registry, pin exact digests, and scan every image layer before deployment.

# Insecure — floating tag from public registry
FROM node:18-alpine

# Secure — pinned digest from private registry
FROM 123456789.dkr.ecr.us-east-1.amazonaws.com/node-base@sha256:abc123...

# Verify image digest in deployment pipeline
docker manifest inspect 123456789.dkr.ecr.us-east-1.amazonaws.com/myapp:latest \
  --verbose | jq '.Descriptor.digest'

ECR Image Scanning

ECR Enhanced Scanning (powered by Amazon Inspector) identifies OS package CVEs and programming language package vulnerabilities. Enable it on every repository and configure pipeline gates that fail on CRITICAL or HIGH findings before the image tag is promoted to production.

Runtime Process Injection

Serverless containers still run real Linux processes. If the application has a command injection, deserialization, or arbitrary code execution vulnerability, an attacker can execute commands within the container context — accessing environment variables, calling the IMDS endpoint, and making network requests.

What Runtime Security Looks Like

AWS Fargate supports Falco-based runtime threat detection through Amazon GuardDuty Container Runtime Monitoring. Enabling this allows detection of reverse shell spawning, unusual file writes, and privilege escalation attempts within Fargate tasks — visibility you'd normally only get from host-level agents.

# Fargate task definition with read-only root filesystem
{
  "containerDefinitions": [{
    "name": "api",
    "image": "123456789.dkr.ecr.us-east-1.amazonaws.com/api@sha256:...",
    "readonlyRootFilesystem": true,
    "mountPoints": [{
      "sourceVolume": "tmp-vol",
      "containerPath": "/tmp",
      "readOnly": false
    }],
    "user": "1000:1000",
    "privileged": false,
    "linuxParameters": {
      "capabilities": {
        "drop": ["ALL"]
      }
    }
  }]
}

Unrestricted Network Egress

By default, Fargate tasks placed in a VPC with a route to the internet have unrestricted outbound access. Cloud Run services can reach the public internet unless VPC Service Controls restrict them. An attacker with code execution can exfiltrate data, establish reverse shells, and contact C2 infrastructure.

Egress Control Options

  • Fargate: Place tasks in private subnets, route all traffic through inspection-capable NAT or firewall (AWS Network Firewall, third-party NGFW). Use VPC Endpoints for AWS API calls — prevents credential theft from reaching external IMDS-like endpoints.
  • Cloud Run: Use VPC connector with all-traffic egress, route through Cloud NAT with restricted IP ranges. Restrict IAM permissions to least privilege.
  • Both: Apply security group or firewall rules that allow only the specific ports and destinations the application requires. Deny everything else by default.

Secrets in Environment Variables

Environment variables are the default mechanism for passing configuration to containers. They're also persisted in task definitions, CloudTrail logs, and sometimes application error dumps. Database passwords, API keys, and signing secrets should never be passed as plaintext environment variables.

# BAD — secret in plaintext env var (visible in task definition, logs)
environment:
  - name: DB_PASSWORD
    value: "super-secret-password"

# GOOD — reference from Secrets Manager
secrets:
  - name: DB_PASSWORD
    valueFrom: "arn:aws:secretsmanager:us-east-1:123:secret:prod/db-password-AbCdEf"

# Cloud Run — reference from Secret Manager
env:
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: db-password
        key: latest
Task Definition History

AWS ECS task definitions are versioned and immutable. If you ever put a plaintext secret in a task definition, it remains in that task definition revision forever — accessible to anyone with ecs:DescribeTaskDefinition permission. Rotate the secret and audit who has that IAM permission.

Defences and Hardening

Serverless containers require a security model that operates at the image and IAM layer rather than the host layer. The following controls form a comprehensive baseline:

Serverless Container Security Baseline

  • IAM least privilege: Scope task roles to specific resources and actions. Use aws:RequestedRegion and resource ARN conditions everywhere.
  • Image provenance: Private ECR/Artifact Registry only. Pin images to SHA256 digest. Require signed images via AWS Signer or Cosign.
  • Read-only root filesystem: Set readonlyRootFilesystem: true. Mount only /tmp as writable.
  • Non-root user: Run as UID 1000+ with no capability additions. Drop ALL capabilities.
  • Secrets management: All secrets from Secrets Manager or Parameter Store via the secrets injection mechanism — never plaintext env vars.
  • Network egress: Private subnets + VPC endpoints for all AWS APIs. Egress-only internet access via inspecting firewall.
  • Runtime threat detection: Enable GuardDuty Container Runtime Monitoring (Fargate) or Cloud Run threat detection.
  • Vulnerability scanning: ECR Enhanced Scanning or Artifact Registry scanning. Block on CRITICAL/HIGH before promotion.
  • IMDS protection: Where possible, restrict SSRF vectors at the application level. WAF rules blocking requests to 169.254.*.

"Serverless containers don't eliminate infrastructure risk. They transfer host risk to the cloud provider while concentrating application, IAM, and image risk onto the development team."

The security conversation around serverless containers often stalls at "the provider handles infrastructure security." That framing is correct but incomplete. The provider secures the hypervisor and host. The development team still owns the IAM configuration, the container image, the application code, the network egress policy, and the secrets management strategy — which is where the most exploited vulnerabilities actually live.

AquilaX scans container images and IaC definitions to identify over-privileged IAM roles, plaintext secrets in task definitions, missing hardening flags, and vulnerable base image components — giving teams the signal needed to address the attack surface that serverless containers actually expose.