Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 770709c

Browse files
committed
annotations: Correct unit tests to validate new protections
Add the verification of some basic protections, namely that: - EnableAnnotations is honored - Dangerous paths cannot be modified if no match - Errors are returned when expected Fixes: #3004 Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
1 parent 4ea2807 commit 770709c

1 file changed

Lines changed: 83 additions & 3 deletions

File tree

virtcontainers/pkg/oci/utils_test.go

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,20 @@ func TestAddAssetAnnotations(t *testing.T) {
690690
Console: consolePath,
691691
}
692692

693-
addAnnotations(ocispec, &config, runtimeConfig)
693+
// Try annotations without enabling them first
694+
err := addAnnotations(ocispec, &config, runtimeConfig)
695+
assert.Error(err)
696+
assert.Exactly(map[string]string{}, config.Annotations)
697+
698+
// Check if annotation not enabled correctly
699+
runtimeConfig.HypervisorConfig.EnableAnnotations = []string{"nonexistent"}
700+
err = addAnnotations(ocispec, &config, runtimeConfig)
701+
assert.Error(err)
702+
703+
// Check that it works if all annotation are enabled
704+
runtimeConfig.HypervisorConfig.EnableAnnotations = []string{".*"}
705+
err = addAnnotations(ocispec, &config, runtimeConfig)
706+
assert.NoError(err)
694707
assert.Exactly(expectedAnnotations, config.Annotations)
695708
}
696709

@@ -789,6 +802,9 @@ func TestAddHypervisorAnnotations(t *testing.T) {
789802
ShimType: vc.KataShimType,
790803
Console: consolePath,
791804
}
805+
runtimeConfig.HypervisorConfig.EnableAnnotations = []string{".*"}
806+
runtimeConfig.HypervisorConfig.FileBackedMemRootList = []string{"/dev/shm*"}
807+
runtimeConfig.HypervisorConfig.VirtioFSDaemonList = []string{"/bin/*ls*"}
792808

793809
ocispec.Annotations[vcAnnotations.KernelParams] = "vsyscall=emulate iommu=on"
794810
addHypervisorConfigOverrides(ocispec, &config, runtimeConfig)
@@ -812,7 +828,7 @@ func TestAddHypervisorAnnotations(t *testing.T) {
812828
ocispec.Annotations[vcAnnotations.BlockDeviceCacheDirect] = "true"
813829
ocispec.Annotations[vcAnnotations.BlockDeviceCacheNoflush] = "true"
814830
ocispec.Annotations[vcAnnotations.SharedFS] = "virtio-fs"
815-
ocispec.Annotations[vcAnnotations.VirtioFSDaemon] = "/home/virtiofsd"
831+
ocispec.Annotations[vcAnnotations.VirtioFSDaemon] = "/bin/false"
816832
ocispec.Annotations[vcAnnotations.VirtioFSCache] = "/home/cache"
817833
ocispec.Annotations[vcAnnotations.Msize9p] = "512"
818834
ocispec.Annotations[vcAnnotations.MachineType] = "q35"
@@ -846,7 +862,7 @@ func TestAddHypervisorAnnotations(t *testing.T) {
846862
assert.Equal(config.HypervisorConfig.BlockDeviceCacheDirect, true)
847863
assert.Equal(config.HypervisorConfig.BlockDeviceCacheNoflush, true)
848864
assert.Equal(config.HypervisorConfig.SharedFS, "virtio-fs")
849-
assert.Equal(config.HypervisorConfig.VirtioFSDaemon, "/home/virtiofsd")
865+
assert.Equal(config.HypervisorConfig.VirtioFSDaemon, "/bin/false")
850866
assert.Equal(config.HypervisorConfig.VirtioFSCache, "/home/cache")
851867
assert.Equal(config.HypervisorConfig.Msize9p, uint32(512))
852868
assert.Equal(config.HypervisorConfig.HypervisorMachineType, "q35")
@@ -880,6 +896,70 @@ func TestAddHypervisorAnnotations(t *testing.T) {
880896
assert.Error(err)
881897
}
882898

899+
func TestAddProtectedHypervisorAnnotations(t *testing.T) {
900+
assert := assert.New(t)
901+
902+
config := vc.SandboxConfig{
903+
Annotations: make(map[string]string),
904+
}
905+
906+
ocispec := specs.Spec{
907+
Annotations: make(map[string]string),
908+
}
909+
910+
runtimeConfig := RuntimeConfig{
911+
HypervisorType: vc.QemuHypervisor,
912+
AgentType: vc.KataContainersAgent,
913+
ProxyType: vc.KataProxyType,
914+
ShimType: vc.KataShimType,
915+
Console: consolePath,
916+
}
917+
ocispec.Annotations[vcAnnotations.KernelParams] = "vsyscall=emulate iommu=on"
918+
err := addAnnotations(ocispec, &config, runtimeConfig)
919+
assert.Error(err)
920+
assert.Exactly(vc.HypervisorConfig{}, config.HypervisorConfig)
921+
922+
// Enable annotations
923+
runtimeConfig.HypervisorConfig.EnableAnnotations = []string{".*"}
924+
925+
ocispec.Annotations[vcAnnotations.FileBackedMemRootDir] = "/dev/shm"
926+
ocispec.Annotations[vcAnnotations.VirtioFSDaemon] = "/bin/false"
927+
928+
config.HypervisorConfig.FileBackedMemRootDir = "do-not-touch"
929+
config.HypervisorConfig.VirtioFSDaemon = "dangerous-daemon"
930+
931+
err = addAnnotations(ocispec, &config, runtimeConfig)
932+
assert.Error(err)
933+
assert.Equal(config.HypervisorConfig.FileBackedMemRootDir, "do-not-touch")
934+
assert.Equal(config.HypervisorConfig.VirtioFSDaemon, "dangerous-daemon")
935+
936+
// Now enable them and check again
937+
runtimeConfig.HypervisorConfig.FileBackedMemRootList = []string{"/dev/*m"}
938+
runtimeConfig.HypervisorConfig.VirtioFSDaemonList = []string{"/bin/*ls*"}
939+
err = addAnnotations(ocispec, &config, runtimeConfig)
940+
assert.NoError(err)
941+
assert.Equal(config.HypervisorConfig.FileBackedMemRootDir, "/dev/shm")
942+
assert.Equal(config.HypervisorConfig.VirtioFSDaemon, "/bin/false")
943+
944+
// In case an absurd large value is provided, the config value if not over-ridden
945+
ocispec.Annotations[vcAnnotations.DefaultVCPUs] = "655536"
946+
err = addAnnotations(ocispec, &config, runtimeConfig)
947+
assert.Error(err)
948+
949+
ocispec.Annotations[vcAnnotations.DefaultVCPUs] = "-1"
950+
err = addAnnotations(ocispec, &config, runtimeConfig)
951+
assert.Error(err)
952+
953+
ocispec.Annotations[vcAnnotations.DefaultVCPUs] = "1"
954+
ocispec.Annotations[vcAnnotations.DefaultMaxVCPUs] = "-1"
955+
err = addAnnotations(ocispec, &config, runtimeConfig)
956+
assert.Error(err)
957+
958+
ocispec.Annotations[vcAnnotations.DefaultMaxVCPUs] = "1"
959+
ocispec.Annotations[vcAnnotations.DefaultMemory] = fmt.Sprintf("%d", vc.MinHypervisorMemory+1)
960+
assert.Error(err)
961+
}
962+
883963
func TestAddRuntimeAnnotations(t *testing.T) {
884964
assert := assert.New(t)
885965

0 commit comments

Comments
 (0)