Skip to content

Commit

Permalink
runtime: Validate hypervisor section name in config file
Browse files Browse the repository at this point in the history
Previously, if you accidentally modified the name of the hypervisor
section in the config file, the default golang runtime gives a cryptic
error message ("`VM memory cannot be zero`"). This can be demonstrated
using the `kata-runtime` utility program which uses the same golang
config package as the actual runtime (`containerd-shim-kata-v2`):

```bash
$ kata-runtime env >/dev/null; echo $?
0
$ sudo sed -i 's!^\[hypervisor\.qemu\]!\[hypervisor\.foo\]!g' /etc/kata-containers/configuration.toml
$ kata-runtime env >/dev/null; echo $?
VM memory cannot be zero
1
```

The hypervisor name is now validated so that the behaviour becomes:

```bash
$ kata-runtime env >/dev/null; echo $?
0
$ sudo sed -i 's!^\[hypervisor\.qemu\]!\[hypervisor\.foo\]!g' /etc/kata-containers/configuration.toml
$ ./kata-runtime env >/dev/null; echo $?
/etc/kata-containers/configuration.toml: configuration file contains invalid hypervisor section: "foo"
1
```

Fixes: #8212.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
(cherry picked from commit 3e8cf69)
Signed-off-by: Greg Kurz <groug@kaod.org>
  • Loading branch information
jodh-intel authored and gkurz committed Oct 19, 2023
1 parent 8cf5506 commit 92f283f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/runtime/pkg/katautils/config.go
Expand Up @@ -55,6 +55,8 @@ const (

// the maximum amount of PCI bridges that can be cold plugged in a VM
maxPCIBridges uint32 = 5

errInvalidHypervisorPrefix = "configuration file contains invalid hypervisor section"
)

type tomlConfig struct {
Expand Down Expand Up @@ -1176,6 +1178,8 @@ func updateRuntimeConfigHypervisor(configPath string, tomlConf tomlConfig, confi
case dragonballHypervisorTableType:
config.HypervisorType = vc.DragonballHypervisor
hConfig, err = newDragonballHypervisorConfig(hypervisor)
default:
err = fmt.Errorf("%s: %+q", errInvalidHypervisorPrefix, k)
}

if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions src/runtime/pkg/katautils/config_test.go
Expand Up @@ -1735,3 +1735,59 @@ vfio_mode="vfio"
assert.Equal(t, config.Runtime.InterNetworkModel, "macvtap")
assert.Equal(t, config.Runtime.VfioMode, "vfio")
}

func TestUpdateRuntimeConfigHypervisor(t *testing.T) {
assert := assert.New(t)

type tableTypeEntry struct {
name string
valid bool
}

configFile := "/some/where/configuration.toml"

// Note: We cannot test acrnHypervisorTableType since
// newAcrnHypervisorConfig() expects ACRN binaries to be
// installed.
var entries = []tableTypeEntry{
{clhHypervisorTableType, true},
{dragonballHypervisorTableType, true},
{firecrackerHypervisorTableType, true},
{qemuHypervisorTableType, true},
{"foo", false},
{"bar", false},
{clhHypervisorTableType + "baz", false},
}

for i, h := range entries {
config := oci.RuntimeConfig{}

tomlConf := tomlConfig{
Hypervisor: map[string]hypervisor{
h.name: {
NumVCPUs: int32(2),
MemorySize: uint32(2048),
Path: "/",
Kernel: "/",
Image: "/",
Firmware: "/",
FirmwareVolume: "/",
SharedFS: "virtio-fs",
VirtioFSDaemon: "/usr/libexec/kata-qemu/virtiofsd",
},
},
}

err := updateRuntimeConfigHypervisor(configFile, tomlConf, &config)

if h.valid {
assert.NoError(err, "test %d (%+v)", i, h)
} else {
assert.Error(err, "test %d (%+v)", i, h)

expectedErr := fmt.Errorf("%v: %v: %+q", configFile, errInvalidHypervisorPrefix, h.name)

assert.Equal(err, expectedErr, "test %d (%+v)", i, h)
}
}
}

0 comments on commit 92f283f

Please sign in to comment.