Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/imagecache/bundle_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two suggestions here.

Validating inside the mount loop means a bad second volume fails after the first one is already mounted. Checking every name in a loop of its own before any mount keeps the rejection free of side effects.

for _, vol := range volumes {
	if err := validateImageVolumeName(vol.Name); err != nil {
		return err
	}
}

Also, a name that fails here means rootfs-overlay.json was changed after API validation, since the API already limits names to a DNS label. A WARN log with the bundle path and the name would leave a trace beyond the returned error. #2279 did the same for diffID mismatches.

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)
Expand Down
19 changes: 19 additions & 0 deletions internal/imagecache/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions internal/imagecache/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Naming subtests by the volume name makes the "" row show up as #00, and names with / become nested subtests, which is awkward with -run. A separate desc field in the table would read better.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd drop this test. It asserts that filepath.Join resolves .. out of the bundle, which tests the standard library rather than our code. If ImageVolumeMountPath is ever hardened to reject such names, this test fails for the wrong reason. The table test above already covers the fix.

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{
Expand Down