diff --git a/internal/imagecache/bundle_linux.go b/internal/imagecache/bundle_linux.go index 78dbafba8e..9d35ed7bb8 100644 --- a/internal/imagecache/bundle_linux.go +++ b/internal/imagecache/bundle_linux.go @@ -114,6 +114,9 @@ func SetupBundleRootfs(bundlePath string) error { // bundle-local mount point, for the OCI spec to bind into the container. func setupImageVolumes(bundlePath string, volumes []ImageVolumeOverlay) error { for _, vol := range volumes { + if err := validateImageVolumeName(vol.Name); err != nil { + return err + } for _, layerDir := range vol.Layers { if err := FinalizeLayer(layerDir); err != nil { return fmt.Errorf("while finalizing layer %q of image volume %q: %w", layerDir, vol.Name, err) diff --git a/internal/imagecache/spec.go b/internal/imagecache/spec.go index a4ed15180c..7fe3ef2540 100644 --- a/internal/imagecache/spec.go +++ b/internal/imagecache/spec.go @@ -31,6 +31,25 @@ func ImageVolumeMountPath(bundlePath, volumeName string) string { return filepath.Join(bundlePath, "volumes", volumeName) } +// validateImageVolumeName rejects any volume name that would not land under +// the bundle's volumes/ directory as a directory of its own. +// +// The worker composes a mount point by joining the name onto that directory +// and then creates, unmounts and mounts over the result, as root. A name +// carrying a separator or a ".." element would place those operations +// somewhere else on the host, and "." or an empty name would place them on +// volumes/ itself, over the mount points of every other volume in the bundle. +// +// API validation already constrains the name to a DNS label, so this is the +// second line: the worker reads the name back from the bundle's +// rootfs-overlay.json rather than from the request it was validated in. +func validateImageVolumeName(name string) error { + if name == "." || !filepath.IsLocal(name) || filepath.Base(name) != name { + return fmt.Errorf("image volume name %q is not a single path element", name) + } + return nil +} + // OverlaySpecFileName is the file atelet writes into each container bundle // describing how to compose the bundle's rootfs from cached layers. Its // absence means the bundle's rootfs is a plain directory (e.g. one prepared by diff --git a/internal/imagecache/spec_test.go b/internal/imagecache/spec_test.go index 5e86d3f5c3..fbc0cb770a 100644 --- a/internal/imagecache/spec_test.go +++ b/internal/imagecache/spec_test.go @@ -23,6 +23,67 @@ import ( "testing" ) +// TestValidateImageVolumeName pins which names setupImageVolumes will build a +// mount point from. The table is the decision; the assertion under it is why +// the decision matters, that an accepted name is always a direct child of the +// bundle's volumes/ directory. +func TestValidateImageVolumeName(t *testing.T) { + const bundle = "/var/lib/ate/bundles/b1" + volumes := filepath.Join(bundle, "volumes") + + for _, tc := range []struct { + name string + valid bool + }{ + {name: "agent", valid: true}, + {name: "my-volume-1", valid: true}, + // Dots are ordinary characters inside a path element. + {name: "..foo", valid: true}, + {name: "foo..", valid: true}, + + {name: "", valid: false}, + // "." resolves to volumes/ itself, which holds every other volume's + // mount point in the bundle. + {name: ".", valid: false}, + {name: "..", valid: false}, + {name: "../../../etc", valid: false}, + // A separator nests the mount somewhere below volumes/ instead; an + // absolute or dot-prefixed form is cleaned by filepath.Join into a + // different volume than the one named. Neither escapes the bundle, + // but neither is the directory the caller asked for either. + {name: "a/b", valid: false}, + {name: "./agent", valid: false}, + {name: "agent/", valid: false}, + {name: "/etc", valid: false}, + } { + t.Run(tc.name, func(t *testing.T) { + err := validateImageVolumeName(tc.name) + if got := err == nil; got != tc.valid { + t.Fatalf("validateImageVolumeName(%q) error = %v, want valid: %t", tc.name, err, tc.valid) + } + if !tc.valid { + return + } + if got := ImageVolumeMountPath(bundle, tc.name); filepath.Dir(got) != volumes { + t.Errorf("ImageVolumeMountPath(%q) = %q, want a direct child of %q", tc.name, got, volumes) + } + }) + } +} + +// TestImageVolumeMountPathEscapesOnTraversal records the behavior the check +// above exists to prevent: without it, the mount point leaves the bundle +// entirely, and setupImageVolumes would MkdirAll, unmount and mount over it. +func TestImageVolumeMountPathEscapesOnTraversal(t *testing.T) { + const bundle = "/var/lib/ate/bundles/b1" + if got, want := ImageVolumeMountPath(bundle, "../../../etc"), "/var/lib/ate/etc"; got != want { + t.Fatalf("ImageVolumeMountPath(%q, %q) = %q, want %q", bundle, "../../../etc", got, want) + } + if got, want := ImageVolumeMountPath(bundle, "."), filepath.Join(bundle, "volumes"); got != want { + t.Fatalf("ImageVolumeMountPath(%q, %q) = %q, want %q", bundle, ".", got, want) + } +} + func TestOverlaySpecRoundTrip(t *testing.T) { bundle := t.TempDir() in := &OverlaySpec{