Skip to content

ECS with readonlyRootFilesystem and /tmp dir

0

Hi,

I have an issue that is making me pull hairs. I am trying to enable readonlyRootFilesystem on our ECS tasks for a Security Control but doing this it appears the /tmp dir becomes read-only and our node user cannot write to it.

We have a super simple task definition (sorry for the formatting):

Resources:
  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: !Ref Name
      NetworkMode: awsvpc
      Memory: 1024
      Cpu: 512
      Volumes:
        - Name: tmp-volume
      RequiresCompatibilities:
        - FARGATE
      ExecutionRoleArn: !Ref ExecutionRole
      TaskRoleArn: !Ref TaskRole
      ContainerDefinitions:
        - Name: !Ref Name
          Image: !Ref Image
          readonlyRootFilesystem: true
          MountPoints:
            - SourceVolume: tmp-volume
              ContainerPath: /tmp
              ReadOnly: false

In our Dockerfile we do this at the end to ensure the Docker app does not run as root:

WORKDIR /app
USER appuser
CMD ["node dist/main"]

Do you have suggestions for this issue? One alternative perhaps is to change the TMPDIR to /app/tmp and just don't mount any volume at all and leave it be.

asked a year ago1.4K views

4 Answers
0

When you set readonlyRootFilesystem: true in your ECS task definition, the container's entire root filesystem becomes read-only, including the /tmp directory. This is a security best practice to prevent unauthorized modifications to the container's filesystem.

Your approach of mounting a volume at /tmp is correct, but there are a few issues with your current configuration:

  1. You've defined a volume named tmp-volume, but you haven't specified what type of volume it is. For Fargate tasks, you need to define it properly.

  2. The volume needs to be writable by your appuser. In multi-container tasks with different users, Amazon ECS automatically manages group permissions to allow specified users to write to volumes, but you need to ensure your user parameter is properly configured.

Here are some solutions:

  1. Complete your volume definition by specifying it as an ephemeral storage volume:
Volumes:
- Name: tmp-volume
DockerVolumeConfiguration:
Scope: task
Autoprovision: true
Driver: local
  1. Ensure your container definition includes the user parameter that matches the user ID of your appuser in the container image.

  2. Your alternative approach of changing TMPDIR to /app/tmp could also work if your application allows for this configuration and the /app directory is writable by your appuser. This would avoid the need for a separate volume mount.

  3. Another option is to use an environment variable to redirect temporary files to a writable location:

Environment:
- Name: TMPDIR
Value: /app/tmp

Remember to create the /app/tmp directory in your Dockerfile and ensure it has the proper permissions for your appuser.
Sources
Amazon ECS task definition parameters for Amazon EC2 - Amazon Elastic Container Service
Amazon ECS task definition parameters for Amazon ECS Managed Instances - Amazon Elastic Container Service
Container can't write to Amazon EBS volume - Amazon Elastic Container Service
Amazon ECS task definition parameters for Fargate - Amazon Elastic Container Service
ecs-containers-readonly-access - AWS Config

answered a year ago

  • I actually tried just now removing the Volume and MountPoint parameters entirely from my Task Definition and instead do this in my Dockerfile to use /app/tmp as the application tmpdir. And it works up until we again enable readonlyRootFilesystem. Then the app starts raising errors like: Uncaught exception EROFS: read-only file system, open '/app/tmp/c1.wav'

    RUN mkdir -p /app/tmp && chown -R appuser:appuser /app/tmp ENV TMPDIR=/app/tmp RUN chown -R appuser:appuser /app USER appuser WORKDIR /app CMD ["node dist/main"]

0

Ok, I made some changes. But still when ECS mount /app/tmp it is owned by root and the appuser cannot write to it. I am lost here.

  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: !Ref Name
      NetworkMode: awsvpc
      Memory: 2048
      Cpu: 1024
      Volumes:
        - Name: tmp-volume
      RequiresCompatibilities:
        - FARGATE
      ExecutionRoleArn: !Ref ExecutionRole
      TaskRoleArn: !Ref TaskRole
      ContainerDefinitions:
        - Name: !Ref Name
          Image: !Ref Image
          User: "appuser:appuser"
          #readonlyRootFilesystem: true
          MountPoints:
            - SourceVolume: tmp-volume
              ContainerPath: /app/tmp
              ReadOnly: false

Here's the Dockerfile

RUN useradd -m appuser

WORKDIR /app

RUN mkdir -p /app/tmp && chown -R appuser:appuser /app/tmp
ENV TMPDIR=/app/tmp

VOLUME ["/app/tmp"]

RUN chown -R appuser:appuser /app

USER appuser

CMD ["node dist/main"]

answered a year ago

  • Hi, I am facing the same issue. Were you able to find a solution for this

0

Perhaps I need to add this to my Dockerfile, which I have not, if I am to go with using /app/tmp as the default os tmpdir via TMPDIR environmental variable.

VOLUME ["/app/tmp"]

answered a year ago

0

There is a very similar question I answered the other day - please take a look. And, yes, you do need to update dockerfile. Refer: https://repost.aws/questions/QU_3NaRXH0RKewjMYc-hWJTw

EXPERT

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.