Docker, start to ship · Chapter 23 of 26

Shrink the attack surface

By the end of this chapter you can run a container whose filesystem cannot be written, with only the root powers its app needs, and with no way for anything inside to gain more. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production habits
  • 5 tasks
  • Docker
  • In the all labs plan

The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.

Why you need this

Ravi found a file in Bunty's menu container, chai-23-menu:

$ docker exec chai-23-menu cat /usr/local/bin/miner.txt
mining bitcoin, brb

It was Ravi's own test, but a real attacker with a shell in there could write anywhere in the container and use every power Docker gave it. Deleting the file fixes nothing: the next attacker writes it again. Every container so far ran with Docker's defaults, a writable filesystem and a set of root's powers, and those are decided when a container is created.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Run chai-23-menu with a read-only filesystem

    Run the menu in a container whose files cannot be changed.

    1. Delete Bunty's chai-23-menu.

    2. Run a new chai-23-menu from nginx:alpine, in the background. Use the same port (the lab's 8023 to the container's 80) and the same read-only mount of the site folder at /usr/share/nginx/html.

    3. Make its root filesystem read-only.

    4. If nginx stops at once, read its logs. Give it a memory folder (tmpfs) at each folder it must write to.

    5. Run curl localhost:8023.

    To pass: chai-23-menu is running from nginx, with a read-only root filesystem. localhost:8023 shows the Tapri menu page.

  2. Task 2

    Keep only the four capabilities nginx needs

    Take away every power of root that nginx does not need.

    1. Delete chai-23-menu, and run it again with all the options from task 1.

    2. This time, drop every capability.

    3. Add back only CHOWN, SETUID, SETGID and NET_BIND_SERVICE.

    4. Run curl localhost:8023.

    To pass: chai-23-menu is running and keeps no capability other than those four. It still has a read-only root filesystem and shows the menu on port 8023.

  3. Task 3

    Stop processes in it from getting new powers

    Stop every process in the container from getting powers it did not start with.

    1. Delete chai-23-menu, and run it again with all the options from tasks 1 and 2.

    2. This time, also add the no-new-privileges security option.

    3. Inspect the container to see its security options.

    To pass: chai-23-menu is running with no-new-privileges. Its read-only filesystem and its four capabilities stay.

  4. Task 4

    Play the attacker against the new container

    Play the attacker: try to write a file into the new container.

    1. Finish tasks 1 to 3 first.

    2. With docker exec chai-23-menu, try to write the file /usr/local/bin/miner.txt inside the container. Use touch, cp or tee, or a shell with echo and >.

    3. The write must fail with a read-only message.

    To pass: a write with docker exec chai-23-menu failed. You typed it in the terminal, in the chapter folder, after the container you have now was started. The file /usr/local/bin/miner.txt is not in the container. If you replace the container later, try the write again.

  5. Task 5 · interview question

    Interview: why a smaller base image is safer

    Interview question: why is a smaller base image safer?

    1. Think: what does a bigger image have that a smaller one does not?

    2. Think: why does that matter for security?

    3. Write one or two sentences in your own words in answer.txt, in the chapter folder.

    To pass: answer.txt says what a smaller image has less of, and why that is safer.

What you will learn

  • --read-only and --tmpfs: a filesystem nobody can write to, with memory folders only where the app must write.
  • --cap-drop and --cap-add: take root's powers away, and give back only the ones the app needs.
  • --security-opt no-new-privileges: nothing inside can gain more powers than it started with.

More in Production habits

Build "Shrink the attack surface" yourself

Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.