Skip to content

Commit ee257f4

Browse files
committed
Allow internal reserved volumes on system mount paths
1 parent 8fbb6f1 commit ee257f4

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

lib/instances/create.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,9 @@ func validateVolumeAttachments(attachments []VolumeAttachment, allowSystemVolume
658658

659659
seenPaths := make(map[string]bool)
660660
for _, vol := range attachments {
661+
reservedPrefix := volumes.ReservedVolumeIDPrefix(vol.VolumeID)
662+
isReservedVolume := reservedPrefix != ""
663+
661664
// Validate mount path is absolute
662665
if !filepath.IsAbs(vol.MountPath) {
663666
return fmt.Errorf("volume %s: mount path %q must be absolute", vol.VolumeID, vol.MountPath)
@@ -666,16 +669,15 @@ func validateVolumeAttachments(attachments []VolumeAttachment, allowSystemVolume
666669
// Clean the path to normalize it
667670
cleanPath := filepath.Clean(vol.MountPath)
668671

669-
// Check for system directories
670-
if isSystemDirectory(cleanPath) {
672+
// Check for system directories. Internal instances may only bypass this
673+
// restriction when attaching reserved internal volumes.
674+
if isSystemDirectory(cleanPath) && !(allowSystemVolumes && isReservedVolume) {
671675
return fmt.Errorf("volume %s: cannot mount to system directory %q", vol.VolumeID, cleanPath)
672676
}
673677

674678
// Reserved internal volume IDs are attachable only by internal instances
675-
if !allowSystemVolumes {
676-
if prefix := volumes.ReservedVolumeIDPrefix(vol.VolumeID); prefix != "" {
677-
return fmt.Errorf("volume %s: volume IDs with the prefix %q are reserved for internal use", vol.VolumeID, prefix)
678-
}
679+
if !allowSystemVolumes && isReservedVolume {
680+
return fmt.Errorf("volume %s: volume IDs with the prefix %q are reserved for internal use", vol.VolumeID, reservedPrefix)
679681
}
680682

681683
// Check for duplicate mount paths

lib/instances/resource_limits_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,19 @@ func TestValidateVolumeAttachments_ReservedVolumeID(t *testing.T) {
251251
MountPath: "/mnt/data",
252252
}}, false)
253253
assert.NoError(t, err)
254+
255+
// Internal instances may mount reserved internal volumes under system paths.
256+
err = validateVolumeAttachments([]VolumeAttachment{{
257+
VolumeID: "builder-disk-abc123",
258+
MountPath: "/var/lib/buildkit",
259+
}}, true)
260+
assert.NoError(t, err)
261+
262+
// The system-path bypass does not apply to non-reserved volume IDs.
263+
err = validateVolumeAttachments([]VolumeAttachment{{
264+
VolumeID: "vol-1",
265+
MountPath: "/var/lib/buildkit",
266+
}}, true)
267+
assert.Error(t, err)
268+
assert.Contains(t, err.Error(), "system directory")
254269
}

0 commit comments

Comments
 (0)