Skip to content

Commit c0b7f29

Browse files
committed
Added Read-only Containers section
1 parent 7841f93 commit c0b7f29

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

manuscript/markdown/main/chapter2.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Docker leverages the Linux (kernel) namespaces which provide an isolated workspa
141141

142142
Also keep in mind that, by default, the user in the container, unless otherwise specified, is root, the same root user as on the host system.
143143

144-
{#vps-identify-risks-docker-docker-host-engine-and-containers-namespaces-mnt-labelling}
144+
{#docker-host-engine-and-containers-namespaces-mnt-labelling}
145145
Labelling systems such as [Linux Security Modules (LSM)](#docker-host-engine-and-containers-linux-security-modules) require that the contents of a volume mounted into a container be [labelled](https://docs.docker.com/engine/admin/volumes/volumes/#create-and-manage-volumes). This can be done by adding the `z` (as seen in above example) or `Z` suffix to the volume mount. The `z` suffix instructs Docker to share the mounted volume with other containers, and in so doing, Docker applies a shared content label. Alternatively, if you provide the `Z` suffix, Docker applies a private unshared label, which means only the current container can use the mounted volume. Further details can be found at the [dockervolumes documentation](https://docs.docker.com/engine/admin/volumes/volumes/). This is something to keep in mind if you are using LSM, and have a process inside your container that is unable to use the mounted data.
146146
`--volumes-from` allows you to specify a data volume from another container.
147147

@@ -806,7 +806,7 @@ Docker has [disabled about 44 system calls](https://docs.docker.com/engine/secur
806806

807807
If you are looking to attack the Linux kernel via its APIs from a Docker container, you still have plenty of surface area here to play with.
808808

809-
## Seccomp (Countermeasures) {#docker-hardening-docker-host-engine-and-containers-seccomp-countermeasures}
809+
## SecComp (Countermeasures) {#docker-hardening-docker-host-engine-and-containers-seccomp-countermeasures}
810810

811811
First, you need to make sure your Docker instance was built with Seccomp. Using the recommended command from the CIS Docker Benchmark:
812812

@@ -828,3 +828,25 @@ To add system calls to the list of syscalls you want to block for your container
828828

829829
{linenos=off, lang=Bash}
830830
docker run --rm -it --security-opt seccomp=/path/to/seccomp/profile.json hello-world
831+
832+
## Read-only Containers
833+
834+
In order to set up read-only hosts, physical or virtual, there is a lot of work to be done, and in some cases, it becomes challenging to stop an Operating System writing to some files. I discussed this in depth in the subsections "Partitioning on OS Installation" and "Lock Down the
835+
Mounting of Partitions" in the VPS chapter of my book: Fascicle 1 of [Holistic Info-Sec for Web Developers](https://f1.holisticinfosecforwebdevelopers.com/) In contrast, running Docker containers as read-only is trivial.
836+
837+
Running a container with the `--read-only` flag stops writes to the container.
838+
839+
This can sometimes be a little to constraining, as your application may need to write some temporary data locally. You could volume mount a host directory into your container, but this would obviously expose that temporary data to the host, and also other containers that may mount the same host directory. To stop other containers sharing your mounted volume, you would have to employ [labeling](#docker-host-engine-and-containers-namespaces-mnt-labelling) with the likes of LSM and apply the `Z` suffix at volume mount time.
840+
841+
A better, easier and simpler solution would be to apply the [`--tmpfs`](https://docs.docker.com/engine/reference/commandline/run/#mount-tmpfs-tmpfs) flag to one or more directories. `--tmpfs` allows the creation of tmpfs (appearing as a mounted file system, but stored in volatile memory) mounts on any local directory, which solves the problem of not being able to write to read-only containers.
842+
843+
If an existing directory is specified with the `--tmpfs` option, you will experience similar behaviour to that of mounting an empty directory onto an existing one. The directory is initially empty, any additions or modifications to the directories contents will not persist past container stop.
844+
845+
The following is an example of running a container as read-only with a writeable tmpfs `/tmp` directory:
846+
847+
{linenos=off, lang=Bash}
848+
docker run -it --rm --read-only --tmpfs /tmp --name=my-read-only-container ubuntu
849+
850+
The default mount flags with `--tmpfs` are the same as the Linux default `mount` flags, if you do not specify any `mount` flags the following will be used:
851+
`rw,noexec,nosuid,nodev,size=65536k`
852+

0 commit comments

Comments
 (0)