From 3bfef0158951049d96716814f99d4c79b02053c7 Mon Sep 17 00:00:00 2001 From: Hui Zhu Date: Tue, 25 Dec 2018 13:21:43 +0800 Subject: [PATCH] Fix the issue that pod or container config file without metadata will crash containerd Because RunPodSandbox and CreateContainer will access metadata without check, pod or container config file without metadata will crash containerd. This patch add checks to handle the issue. Fixes: #1009 Signed-off-by: Hui Zhu --- pkg/server/container_create.go | 6 +++++- pkg/server/sandbox_run.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/server/container_create.go b/pkg/server/container_create.go index 782626878..42b61ccc2 100644 --- a/pkg/server/container_create.go +++ b/pkg/server/container_create.go @@ -92,7 +92,11 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta // Reserve the container name to avoid concurrent `CreateContainer` request creating // the same container. id := util.GenerateID() - name := makeContainerName(config.GetMetadata(), sandboxConfig.GetMetadata()) + metadata := config.GetMetadata() + if metadata == nil { + return nil, errors.New("container config must include metadata") + } + name := makeContainerName(metadata, sandboxConfig.GetMetadata()) logrus.Debugf("Generated id %q for container %q", id, name) if err = c.containerNameIndex.Reserve(name, id); err != nil { return nil, errors.Wrapf(err, "failed to reserve container name %q", name) diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go index d5aa700b3..e9070296d 100644 --- a/pkg/server/sandbox_run.go +++ b/pkg/server/sandbox_run.go @@ -58,7 +58,11 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox // Generate unique id and name for the sandbox and reserve the name. id := util.GenerateID() - name := makeSandboxName(config.GetMetadata()) + metadata := config.GetMetadata() + if metadata == nil { + return nil, errors.New("sandbox config must include metadata") + } + name := makeSandboxName(metadata) logrus.Debugf("Generated id %q for sandbox %q", id, name) // Reserve the sandbox name to avoid concurrent `RunPodSandbox` request starting the // same sandbox.