Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can β€” learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company πŸ‘‡

fix(khaos): the containment check that refused every container with a volume

CarterPerez-dev10069f0450a1
diff --git a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse.go b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse.go
index 7ddbb752..e7214056 100644
--- a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse.go
+++ b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse.go
@@ -283,7 +283,7 @@ func checkSocketReachable(t Target, _ Guard) *Refusal {
return refuse(
PredicateSocketReachable,
t,
- "the container bind-mounts "+source+", which "+reason+
+ "the container mounts "+source+", which "+reason+
", and access to the Docker daemon is equivalent to root on this machine",
"remove that bind mount, or target a service that cannot reach the daemon",
)
@@ -328,7 +328,13 @@ func socketPaths() []string {
func socketReachableFrom(source string) (string, bool) {
resolved := resolvePath(source)
- if overlaps(resolved, daemonStatePath) {
+ // Containment is DIRECTIONAL. A mount that contains the daemon's state
+ // directory exposes it; one the daemon handed out from inside it does not.
+ // Every named and anonymous volume lives at
+ // /var/lib/docker/volumes/<name>/_data, so a symmetric test refuses every
+ // container with a volume - which is every database, every cache and every
+ // queue - while blaming the operator for a bind mount they never wrote.
+ if !managedVolume(resolved) && overlaps(resolved, daemonStatePath) {
return "exposes the Docker daemon state directory", true
}
@@ -349,6 +355,33 @@ func socketReachableFrom(source string) (string, bool) {
return "", false
}
+// managedVolume reports whether this path is ONE volume's data directory under
+// the daemon's volume root. The `_data` component is required, so the volume
+// root itself, the volume metadata beside it, and every other subtree of
+// /var/lib/docker stay refused: those expose every volume on the host, or
+// another container's filesystem, rather than the one volume the daemon handed
+// to this container.
+//
+// A volume created with `--opt device=/var/run` resolves to the device path and
+// never reaches this check, so the narrow allowance cannot be used to smuggle
+// the socket in.
+func managedVolume(resolved string) bool {
+ if !isAncestor(daemonVolumePath, resolved) {
+ return false
+ }
+
+ for _, element := range strings.Split(
+ strings.TrimPrefix(resolved, daemonVolumePath+pathSeparator),
+ pathSeparator,
+ ) {
+ if element == volumeDataDir {
+ return true
+ }
+ }
+
+ return false
+}
+
func resolvePath(path string) string {
if path == "" {
return path
diff --git a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse_test.go b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse_test.go
index c1e7c750..b576a3d3 100644
--- a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse_test.go
+++ b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/refuse_test.go
@@ -297,6 +297,96 @@ func TestSocketIsRefusedThroughEveryReachablePath(t *testing.T) {
}
}
+// TestDaemonStateContainmentIsDirectional is the control for the one narrow
+// allowance inside /var/lib/docker. The symmetric version of this test refused
+// every container holding a Docker-managed volume, which is every database,
+// cache and queue khaos could ever target, and the refusal told the operator to
+// remove a bind mount they never wrote.
+//
+// Every row in the refused list must stay refused: the allowance is one
+// volume's own data directory and nothing else.
+func TestDaemonStateContainmentIsDirectional(t *testing.T) {
+ refused := []string{
+ "/var/lib/docker",
+ "/var/lib/docker/",
+ "/var/lib/docker/containers",
+ "/var/lib/docker/containers/abc123",
+ "/var/lib/docker/overlay2",
+ "/var/lib/docker/image",
+ "/var/lib/docker/volumes",
+ "/var/lib/docker/volumes/metadata.db",
+ "/var/lib",
+ "/var",
+ "/",
+ }
+
+ for _, source := range refused {
+ t.Run("refused"+source, func(t *testing.T) {
+ target, guard := acceptable()
+ target.BindSources = []string{source}
+
+ refusal := Refuse(target, guard)
+ if refusal == nil ||
+ refusal.Predicate != PredicateSocketReachable {
+ t.Fatalf(
+ "a mount of %q reaches daemon state and was not refused",
+ source,
+ )
+ }
+ })
+ }
+
+ allowed := []string{
+ "/var/lib/docker/volumes/pgdata/_data",
+ "/var/lib/docker/volumes/pgdata/_data/pg_wal",
+ "/var/lib/docker/volumes/019fed45e77b712556a8beb2e4bc7347d/_data",
+ }
+
+ for _, source := range allowed {
+ t.Run("allowed"+source, func(t *testing.T) {
+ target, guard := acceptable()
+ target.BindSources = []string{source}
+
+ if refusal := Refuse(target, guard); refusal != nil {
+ t.Fatalf(
+ "%q is one volume's own data directory, which the daemon hands to the container itself, and it was refused by %s",
+ source,
+ refusal.Predicate,
+ )
+ }
+ })
+ }
+}
+
+// TestTheVolumeAllowanceCannotSmuggleTheSocket pins the reason the allowance is
+// safe: it is applied to the RESOLVED path, so a volume whose local driver
+// points at a sensitive host directory is judged by where it actually lands.
+func TestTheVolumeAllowanceCannotSmuggleTheSocket(t *testing.T) {
+ smuggled := []string{
+ "/var/run",
+ "/run",
+ DockerSocketPath,
+ "/var/lib/docker/volumes/../containers",
+ "/var/lib/docker/volumes/../../docker",
+ }
+
+ for _, source := range smuggled {
+ t.Run(source, func(t *testing.T) {
+ target, guard := acceptable()
+ target.BindSources = []string{source}
+
+ refusal := Refuse(target, guard)
+ if refusal == nil ||
+ refusal.Predicate != PredicateSocketReachable {
+ t.Fatalf(
+ "%q reaches the daemon and was not refused",
+ source,
+ )
+ }
+ })
+ }
+}
+
func TestOrdinaryBindMountsAreAllowed(t *testing.T) {
harmless := []string{
"/srv/app/config",
diff --git a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/target.go b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/target.go
index f9e99f19..414afa40 100644
--- a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/target.go
+++ b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/target.go
@@ -35,6 +35,8 @@ const (
DockerSocketPath = "/var/run/docker.sock"
runSocketPath = "/run/docker.sock"
daemonStatePath = "/var/lib/docker"
+ daemonVolumePath = "/var/lib/docker/volumes"
+ volumeDataDir = "_data"
socketBaseName = "docker.sock"
)