From 7cf5a6a112622519d15f0e925a6e268462ca0b12 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 14 Dec 2022 13:54:09 +0900 Subject: [PATCH 1/2] vz: fix nil pointer dereference with mountType: reverse-sshfs ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x10075a5a4] goroutine 1 [running]: github.com/Code-Hex/vz/v3/internal/objc.ConvertToNSMutableArray.func1(0x2?, 0x600000c143f0?) /Users/suda/gopath/pkg/mod/github.com/!code-!hex/vz/v3@v3.0.2/internal/objc/objc.go:134 +0x24 github.com/Code-Hex/vz/v3/internal/objc.ConvertToNSMutableArray({0xc0002c85c0, 0x2, 0xc0001fdb20?}) /Users/suda/gopath/pkg/mod/github.com/!code-!hex/vz/v3@v3.0.2/internal/objc/objc.go:134 +0xef github.com/Code-Hex/vz/v3.(*VirtualMachineConfiguration).SetDirectorySharingDevicesVirtualMachineConfiguration(0xc00012e000?, {0xc0002c85a0, 0x2, 0x1f?}) /Users/suda/gopath/pkg/mod/github.com/!code-!hex/vz/v3@v3.0.2/configuration.go:188 +0x77 github.com/lima-vm/lima/pkg/vz.attachFolderMounts(0xc00047e780, 0xb?) /Users/suda/gopath/src/github.com/lima-vm/lima/pkg/vz/vm_darwin.go:381 +0x214 github.com/lima-vm/lima/pkg/vz.createVM(0x1000811e5?, 0xc00012e040?) /Users/suda/gopath/src/github.com/lima-vm/lima/pkg/vz/vm_darwin.go:124 +0xf8 github.com/lima-vm/lima/pkg/vz.startVM({0x100b42d30?, 0xc0000da008}, 0xc00047e780) /Users/suda/gopath/src/github.com/lima-vm/lima/pkg/vz/vm_darwin.go:32 +0x5b github.com/lima-vm/lima/pkg/vz.(*LimaVzDriver).Start(0xc0004b62b0, {0x100b42d30, 0xc0000da008}) /Users/suda/gopath/src/github.com/lima-vm/lima/pkg/vz/vz_driver_darwin.go:106 +0xf3 github.com/lima-vm/lima/pkg/hostagent.(*HostAgent).Run(0xc0000e0480, {0x100b42d30?, 0xc0000da008}) /Users ``` Signed-off-by: Akihiro Suda --- pkg/vz/vm_darwin.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/vz/vm_darwin.go b/pkg/vz/vm_darwin.go index 286ca1cb3dc..4f8f0530090 100644 --- a/pkg/vz/vm_darwin.go +++ b/pkg/vz/vm_darwin.go @@ -335,7 +335,7 @@ func attachConsole(_ *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguratio } func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error { - mounts := make([]vz.DirectorySharingDeviceConfiguration, len(driver.Yaml.Mounts)) + var mounts []vz.DirectorySharingDeviceConfiguration if *driver.Yaml.MountType == limayaml.VIRTIOFS { for i, mount := range driver.Yaml.Mounts { expandedPath, err := localpathutil.Expand(mount.Location) @@ -364,7 +364,7 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo return err } config.SetDirectoryShare(share) - mounts[i] = config + mounts = append(mounts, config) } } @@ -378,7 +378,9 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo } } - vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts) + if len(mounts) > 0 { + vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts) + } return nil } From 25cd97ed4ad8b9f410dd44698990d4b711aeb4c8 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 14 Dec 2022 14:05:10 +0900 Subject: [PATCH 2/2] vz: validate disk format Signed-off-by: Akihiro Suda --- docs/vmtype.md | 3 +++ examples/default.yaml | 2 ++ pkg/vz/vm_darwin.go | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/docs/vmtype.md b/docs/vmtype.md index d3db89186e5..1512bda5cc7 100644 --- a/docs/vmtype.md +++ b/docs/vmtype.md @@ -4,6 +4,9 @@ Lima supports two ways of running guest machines: - [qemu](#qemu) - [vz](#vz) +The vmType can be specified only on creating the instance. +The vmType of existing instances cannot be changed. + ## QEMU "qemu" option makes use of QEMU to run guest operating system. This option is used by default if "vmType" is not set. diff --git a/examples/default.yaml b/examples/default.yaml index c7b63976d80..e321dc9c929 100644 --- a/examples/default.yaml +++ b/examples/default.yaml @@ -6,6 +6,8 @@ # so they can be overridden by the $LIMA_HOME/_config/default.yaml mechanism documented at the end of this file. # VM type: "qemu" or "vz" (on macOS 13 and later). +# The vmType can be specified only on creating the instance. +# The vmType of existing instances cannot be changed. # 🟢 Builtin default: "qemu" vmType: null diff --git a/pkg/vz/vm_darwin.go b/pkg/vz/vm_darwin.go index 4f8f0530090..247195aebe2 100644 --- a/pkg/vz/vm_darwin.go +++ b/pkg/vz/vm_darwin.go @@ -20,6 +20,7 @@ import ( "github.com/lima-vm/lima/pkg/limayaml" "github.com/lima-vm/lima/pkg/localpathutil" "github.com/lima-vm/lima/pkg/networks" + "github.com/lima-vm/lima/pkg/qemu/imgutil" "github.com/lima-vm/lima/pkg/store/filenames" "github.com/sirupsen/logrus" ) @@ -242,6 +243,18 @@ func attachNetwork(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigu return nil } +func validateDiskFormat(diskPath string) error { + format, err := imgutil.DetectFormat(diskPath) + if err != nil { + return fmt.Errorf("failed to detect the format of %q: %w", diskPath, err) + } + if format != "raw" { + return fmt.Errorf("expected the format of %q to be \"raw\", got %q", diskPath, format) + } + // TODO: ensure that the disk is formatted with GPT or ISO9660 + return nil +} + func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error { baseDiskPath := filepath.Join(driver.Instance.Dir, filenames.BaseDisk) diffDiskPath := filepath.Join(driver.Instance.Dir, filenames.DiffDisk) @@ -253,6 +266,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura var configurations []vz.StorageDeviceConfiguration if isBaseDiskCDROM { + if err = validateDiskFormat(baseDiskPath); err != nil { + return err + } baseDiskAttachment, err := vz.NewDiskImageStorageDeviceAttachment(baseDiskPath, true) if err != nil { return err @@ -263,6 +279,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura } configurations = append(configurations, baseDisk) } + if err = validateDiskFormat(diffDiskPath); err != nil { + return err + } diffDiskAttachment, err := vz.NewDiskImageStorageDeviceAttachment(diffDiskPath, false) if err != nil { return err @@ -273,6 +292,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura } configurations = append(configurations, diffDisk) + if err = validateDiskFormat(ciDataPath); err != nil { + return err + } ciDataAttachment, err := vz.NewDiskImageStorageDeviceAttachment(ciDataPath, true) if err != nil { return err