From 7b1b16b741217987570392363161a1fee7176b21 Mon Sep 17 00:00:00 2001 From: Ace-Tang Date: Thu, 18 Oct 2018 16:13:05 +0800 Subject: [PATCH] runtime-v2: add validation for runtime name add validation for runtime name, if runtime name is invalid, containerd will got panic. Signed-off-by: Ace-Tang --- runtime/v2/shim/util.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/runtime/v2/shim/util.go b/runtime/v2/shim/util.go index ca028a683..b7034ce50 100644 --- a/runtime/v2/shim/util.go +++ b/runtime/v2/shim/util.go @@ -49,6 +49,9 @@ func Command(ctx context.Context, runtime, containerdAddress, path string, cmdAr } args = append(args, cmdArgs...) name := BinaryName(runtime) + if name == "" { + return nil, fmt.Errorf("invalid runtime name %s, correct runtime name should format like io.containerd.runc.v1", runtime) + } var cmdPath string var lerr error if cmdPath, lerr = exec.LookPath(name); lerr != nil { @@ -69,10 +72,15 @@ func Command(ctx context.Context, runtime, containerdAddress, path string, cmdAr return cmd, nil } -// BinaryName returns the shim binary name from the runtime name +// BinaryName returns the shim binary name from the runtime name, +// empty string returns means runtime name is invalid func BinaryName(runtime string) string { + // runtime name should format like $prefix.name.version parts := strings.Split(runtime, ".") - // TODO: add validation for runtime + if len(parts) < 2 { + return "" + } + return fmt.Sprintf(shimBinaryFormat, parts[len(parts)-2], parts[len(parts)-1]) }