mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
From the volume UUID commit's security review. A folder at its own recorded path on a disk numbered anew (platform.SameFolder's renumbered case) must now also be on the volume its state records, when both are known (platform.KeepsVolume): the finder checks it (finder.sameFolder, Env.keepsVolume, so a launch refuses with vm_other_folder) and so does recordFolder under the state's lock, so a copy of the disk with a volume ID of its own mounted at the folder's path is another folder. A volume that can't be told leaves the device's rule, as for a state that records none. mountedBy and mountsRepo keep the check without volumes (madeFor), which errs towards warning. hereOf reads the folder's identity, uniqueness, owner and volume from one open folder (platform.StatFolder, now with Owner), so the folder checked as the user's is the one matched and recorded. StatFolder is one function for macOS and Linux (folderstat_unix.go) over each system's openFolder, which SameFolder's renumbered case uses too, in place of openFolderID and darwin's copy. devvm ls and devvm space look volumes up through the run's Env (Env.FolderOf, Inventory.List(ctx, e)), each mount once a run, in place of diskutil for each VM; FolderOf, parentVolume and movedAway go. FolderDevice is unexported (folderDevice), so only platform reads a device; OnRecordedDisk takes a record without a device as on any disk. The folder-ids rule tracks names assigned from such names, however many steps on, and maps keyed by an identity, indexed or in a map's literal, without counting a struct literal's field; its loop is sitesWhereIn, shared with sitesWhere. The hdiutil e2e check makes sure macOS gave the disk a new number, attaching up to three images first, and is skipped when it never does. The design notes say who can forge a volume ID and what it gets them. Tests: TestKeepsVolume, OnRecordedDisk without a device, TestRenumberedOnAnotherVolume (a launch and recordFolder), TestListLooksUpVolumesOnce, StatFolder's Owner on both systems, the folder-ids rule's new cases, and fixture_v010_renumbered.txtar (another volume at its own path refused with nothing recorded; ls calls the moved folder gone by its recorded volume, where it said not_connected). Co-Authored-By: Claude Opus 5.5 <[email protected]>
95 lines
2.9 KiB
Go
95 lines
2.9 KiB
Go
package platform
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/xml"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
// diskutil is macOS's tool for its disks, which devvm runs by its absolute path.
|
|
const diskutil = "/usr/sbin/diskutil"
|
|
|
|
// uuidForm is a volume UUID's form, as diskutil writes it.
|
|
var uuidForm = regexp.MustCompile(`^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$`)
|
|
|
|
// VolumeUUID is the volume of the folder s describes, "uuid:<VolumeUUID>", as diskutil says for
|
|
// its mount point: the UUID macOS keeps on the volume, which no renumbering of its disk changes.
|
|
// It runs diskutil itself, not through the run log, whose LoggedCmd keeps a command's output,
|
|
// which this needs; the output holds no secret. The answer must be for that mount point and the
|
|
// device mounted there, as StatFolder read them from the open folder, so a volume mounted there
|
|
// since isn't taken for it. It fails for a read-only volume or a snapshot, which it never
|
|
// identifies, and within ctx.
|
|
func VolumeUUID(ctx context.Context, s FolderStat) (string, error) {
|
|
if s.ReadOnly || s.Mount == "" || s.Device == "" {
|
|
return "", errors.New("no volume to identify")
|
|
}
|
|
cmd := exec.CommandContext(ctx, diskutil, "info", "-plist", s.Mount) //nolint:gosec // devvm's own arguments: a mount point read from an open folder
|
|
cmd.WaitDelay = time.Second // its pipes, once ctx ends, even if a child of its holds them
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return "", fmt.Errorf("diskutil info %s: %w", s.Mount, err)
|
|
}
|
|
info, err := plistStrings(out)
|
|
if err != nil {
|
|
return "", fmt.Errorf("diskutil info %s: %w", s.Mount, err)
|
|
}
|
|
if info["MountPoint"] != s.Mount || info["DeviceIdentifier"] != s.Device || !uuidForm.MatchString(info["VolumeUUID"]) {
|
|
return "", fmt.Errorf("diskutil info %s: not the volume mounted there", s.Mount)
|
|
}
|
|
return "uuid:" + info["VolumeUUID"], nil
|
|
}
|
|
|
|
// plistMax bounds what of diskutil's plist devvm reads.
|
|
const plistMax = 1 << 20
|
|
|
|
// plistStrings is the string values of a plist's top dictionary, by key: an XML property list,
|
|
// read with encoding/xml, each <key> followed by a <string>; other values are skipped.
|
|
func plistStrings(data []byte) (map[string]string, error) {
|
|
if len(data) > plistMax {
|
|
return nil, errors.New("a plist too big")
|
|
}
|
|
d := xml.NewDecoder(bytes.NewReader(data))
|
|
out := map[string]string{}
|
|
depth, key := 0, ""
|
|
for {
|
|
tok, err := d.Token()
|
|
if errors.Is(err, io.EOF) {
|
|
return out, nil
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
switch t := tok.(type) {
|
|
case xml.StartElement:
|
|
depth++
|
|
if depth != 3 {
|
|
key = ""
|
|
continue
|
|
}
|
|
var s string
|
|
if err := d.DecodeElement(&s, &t); err != nil {
|
|
return nil, err
|
|
}
|
|
depth--
|
|
switch t.Name.Local {
|
|
case "key":
|
|
key = s
|
|
case "string":
|
|
if key != "" {
|
|
out[key] = s
|
|
}
|
|
key = ""
|
|
default:
|
|
key = ""
|
|
}
|
|
case xml.EndElement:
|
|
depth--
|
|
}
|
|
}
|
|
}
|