From b3aa447ad9eabd9e95efe7a46ddd6cd2de7e760b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Thu, 29 Oct 2026 16:11:48 +0100 Subject: [PATCH] daemon: Validate restart signals before changing container state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Return an invalid-parameter error for invalid signals without starting to change container's state. Signed-off-by: Paweł Gronowski --- daemon/restart.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/daemon/restart.go b/daemon/restart.go index 4e166dbfe3..8e50bf65b8 100644 --- a/daemon/restart.go +++ b/daemon/restart.go @@ -3,11 +3,14 @@ package daemon import ( "context" "fmt" + "runtime" containertypes "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/events" "github.com/moby/moby/v2/daemon/container" "github.com/moby/moby/v2/daemon/server/backend" + "github.com/moby/moby/v2/errdefs" + "github.com/moby/sys/signal" ) // ContainerRestart stops and starts a container. It attempts to @@ -23,7 +26,7 @@ func (daemon *Daemon) ContainerRestart(ctx context.Context, name string, options } err = daemon.containerRestart(ctx, daemon.config(), ctr, options) if err != nil { - return fmt.Errorf("Cannot restart container %s: %v", name, err) + return fmt.Errorf("Cannot restart container %s: %w", name, err) } return nil } @@ -33,6 +36,17 @@ func (daemon *Daemon) ContainerRestart(ctx context.Context, name string, options // gracefully stop, before forcefully terminating the container. If // given a negative duration, wait forever for a graceful stop. func (daemon *Daemon) containerRestart(ctx context.Context, daemonCfg *configStore, container *container.Container, options backend.ContainerStopOptions) (retErr error) { + if options.Signal != "" { + sig, err := signal.ParseSignal(options.Signal) + if err != nil { + return errdefs.InvalidParameter(err) + } + + if !signal.ValidSignalForPlatform(sig) { + return errdefs.InvalidParameter(fmt.Errorf("the %s daemon does not support signal %d", runtime.GOOS, sig)) + } + } + // Restarting is expected to be an atomic operation, and cancelling // the request should not cancel the stop -> start sequence. ctx = context.WithoutCancel(ctx)