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

Commit db5fb82

Browse files
c3dfidencio
authored andcommitted
config: Use glob instead of regexp to match paths in annotations
When filtering annotations that correspond to paths, e.g. hypervisor.path, it is better to use a glob syntax than a regexp syntax, as it is more usual for paths, and prevents classes of matches that are undesirable in our case, such as matching .. against .* Fixes: #3004 Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
1 parent 344e338 commit db5fb82

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

virtcontainers/pkg/oci/utils.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ func regexpContains(s []string, e string) bool {
212212
return false
213213
}
214214

215+
func checkPathIsInGlobList(list []string, path string) bool {
216+
for _, glob := range list {
217+
filenames, _ := filepath.Glob(glob)
218+
for _, a := range filenames {
219+
if path == a {
220+
return true
221+
}
222+
}
223+
}
224+
return false
225+
}
226+
215227
func newLinuxDeviceInfo(d specs.LinuxDevice) (*config.DeviceInfo, error) {
216228
allowedDeviceTypes := []string{"c", "b", "u", "p"}
217229

@@ -398,21 +410,21 @@ func addHypervisorConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig,
398410
}
399411

400412
if value, ok := ocispec.Annotations[vcAnnotations.HypervisorPath]; ok {
401-
if !regexpContains(runtime.HypervisorConfig.HypervisorPathList, value) {
413+
if !checkPathIsInGlobList(runtime.HypervisorConfig.HypervisorPathList, value) {
402414
return fmt.Errorf("hypervisor %v required from annotation is not valid", value)
403415
}
404416
config.HypervisorConfig.HypervisorPath = value
405417
}
406418

407419
if value, ok := ocispec.Annotations[vcAnnotations.JailerPath]; ok {
408-
if !regexpContains(runtime.HypervisorConfig.JailerPathList, value) {
420+
if !checkPathIsInGlobList(runtime.HypervisorConfig.JailerPathList, value) {
409421
return fmt.Errorf("jailer %v required from annotation is not valid", value)
410422
}
411423
config.HypervisorConfig.JailerPath = value
412424
}
413425

414426
if value, ok := ocispec.Annotations[vcAnnotations.CtlPath]; ok {
415-
if !regexpContains(runtime.HypervisorConfig.HypervisorCtlPathList, value) {
427+
if !checkPathIsInGlobList(runtime.HypervisorConfig.HypervisorCtlPathList, value) {
416428
return fmt.Errorf("hypervisor control %v required from annotation is not valid", value)
417429
}
418430
config.HypervisorConfig.HypervisorCtlPath = value
@@ -451,7 +463,7 @@ func addHypervisorConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig,
451463
}
452464

453465
if value, ok := ocispec.Annotations[vcAnnotations.VhostUserStorePath]; ok {
454-
if !regexpContains(runtime.HypervisorConfig.VhostUserStorePathList, value) {
466+
if !checkPathIsInGlobList(runtime.HypervisorConfig.VhostUserStorePathList, value) {
455467
return fmt.Errorf("vhost store path %v required from annotation is not valid", value)
456468
}
457469
config.HypervisorConfig.VhostUserStorePath = value
@@ -567,7 +579,7 @@ func addHypervisorMemoryOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig
567579
}
568580

569581
if value, ok := ocispec.Annotations[vcAnnotations.FileBackedMemRootDir]; ok {
570-
if !regexpContains(runtime.HypervisorConfig.FileBackedMemRootList, value) {
582+
if !checkPathIsInGlobList(runtime.HypervisorConfig.FileBackedMemRootList, value) {
571583
return fmt.Errorf("file_mem_backend value %v required from annotation is not valid", value)
572584
}
573585
sbConfig.HypervisorConfig.FileBackedMemRootDir = value
@@ -705,7 +717,7 @@ func addHypervisorVirtioFsOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConf
705717
}
706718

707719
if value, ok := ocispec.Annotations[vcAnnotations.VirtioFSDaemon]; ok {
708-
if !regexpContains(runtime.HypervisorConfig.VirtioFSDaemonList, value) {
720+
if !checkPathIsInGlobList(runtime.HypervisorConfig.VirtioFSDaemonList, value) {
709721
return fmt.Errorf("virtiofs daemon %v required from annotation is not valid", value)
710722
}
711723
sbConfig.HypervisorConfig.VirtioFSDaemon = value

0 commit comments

Comments
 (0)