moby/cmd/dockerd
Sebastiaan van Stijn 25b709df48
windows: fix --register-service when executed from within binary directory
Go 1.15.7 contained a security fix for CVE-2021-3115, which allowed arbitrary
code to be executed at build time when using cgo on Windows.

This issue was not limited to the go command itself, and could also affect binaries
that use `os.Command`, `os.LookPath`, etc.

From the related blogpost (https://blog.golang.org/path-security):

> Are your own programs affected?
>
> If you use exec.LookPath or exec.Command in your own programs, you only need to
> be concerned if you (or your users) run your program in a directory with untrusted
> contents. If so, then a subprocess could be started using an executable from dot
> instead of from a system directory. (Again, using an executable from dot happens
> always on Windows and only with uncommon PATH settings on Unix.)
>
> If you are concerned, then we’ve published the more restricted variant of os/exec
> as golang.org/x/sys/execabs. You can use it in your program by simply replacing

At time of the go1.15 release, the Go team considered changing the behavior of
`os.LookPath()` and `exec.LookPath()` to be a breaking change, and made the
behavior "opt-in" by providing the `golang.org/x/sys/execabs` package as a
replacement.

However, for the go1.19 release, this changed, and the default behavior of
`os.LookPath()` and `exec.LookPath()` was changed. From the release notes:
https://go.dev/doc/go1.19#os-exec-path

> Command and LookPath no longer allow results from a PATH search to be found
> relative to the current directory. This removes a common source of security
> problems but may also break existing programs that depend on using, say,
> exec.Command("prog") to run a binary named prog (or, on Windows, prog.exe)
> in the current directory. See the os/exec package documentation for information
> about how best to update such programs.
>
> On Windows, Command and LookPath now respect the NoDefaultCurrentDirectoryInExePath
> environment variable, making it possible to disable the default implicit search
> of “.” in PATH lookups on Windows systems.

A result of this change was that registering the daemon as a Windows service
no longer worked when done from within the directory of the binary itself:

    C:\> cd "Program Files\Docker\Docker\resources"
    C:\Program Files\Docker\Docker\resources> dockerd --register-service
    exec: "dockerd": cannot run executable found relative to current directory

Note that using an absolute path would work around the issue:

    C:\Program Files\Docker\Docker>resources\dockerd.exe --register-service

This patch changes `registerService()` to use `os.Executable()`, instead of
depending on `os.Args[0]` and `exec.LookPath()` for resolving the absolute
path of the binary.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3e8fda0a70)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-13 21:58:08 +02:00
..
trap cmd/dockerd/trap: log to logrus directly 2023-04-26 09:53:01 -04:00
cobra.go cli: move cobra customizations into cmd/dockerd 2022-12-14 11:37:51 +01:00
config.go Make default options for newly created networks configurable 2023-03-01 07:58:26 +01:00
config_unix.go Merge pull request #45315 from thaJeztah/deprecate_oom_score_adjust 2023-04-14 00:06:58 +02:00
config_unix_test.go daemon/config: New(): initialize config with platform-specific defaults 2022-08-17 08:54:32 +02:00
config_windows.go daemon/config: New(): initialize config with platform-specific defaults 2022-08-17 08:54:32 +02:00
daemon.go build: use daemon id as worker id for the graph driver controller 2023-05-18 22:29:45 +02:00
daemon_freebsd.go daemon: move check for CPU-realtime daemon options 2022-03-03 19:50:27 +01:00
daemon_linux.go vendor: github.com/containerd/cgroups/v3 v3.0.1 2023-03-08 20:15:17 +09:00
daemon_linux_test.go cmd/dockerd: gracefully shut down the API server 2023-04-26 10:57:28 -04:00
daemon_test.go daemon/config: rename CommonTLSOptions to TLSOptions 2022-12-15 10:44:24 +01:00
daemon_unix.go daemon: deprecate --oom-score-adjust for the daemon 2023-04-13 00:02:39 +02:00
daemon_unix_test.go cmd/dockerd: produce error when using discovery options 2022-01-06 18:28:15 +01:00
daemon_windows.go daemon/config: remove TrustKeyPath, and local utilities 2022-11-28 20:29:19 +01:00
docker.go cmd/dockerd: ignore SIGPIPE using signal.Ignore 2023-04-25 17:50:12 -04:00
docker_unix.go Update to Go 1.17.0, and gofmt with Go 1.17 2021-08-24 23:33:27 +02:00
docker_windows.go daemon/config: remove TrustKeyPath, and local utilities 2022-11-28 20:29:19 +01:00
error.go cli: move cobra customizations into cmd/dockerd 2022-12-14 11:37:51 +01:00
genwinres_windows.go use go-winres for cross to create Windows resources 2022-04-14 19:52:35 +02:00
grpclog.go Silence GRPC logs unless our log level is debug 2023-03-21 21:01:10 +00:00
main_linux_test.go Fix listener parsering regression when no addr set 2023-03-09 23:53:05 +00:00
metrics.go set ReadHeaderTimeout to address G112: Potential Slowloris Attack (gosec) 2022-09-22 12:13:28 +02:00
options.go cli: remove cli/config package, integrate into cmd/dockerd 2022-12-14 12:52:40 +01:00
options_test.go cli: remove cli/config package, integrate into cmd/dockerd 2022-12-14 12:52:40 +01:00
README.md Fix readme doc for dockerd 2016-09-01 14:47:51 +08:00
required.go cli: move cobra customizations into cmd/dockerd 2022-12-14 11:37:51 +01:00
service_unsupported.go Update to Go 1.17.0, and gofmt with Go 1.17 2021-08-24 23:33:27 +02:00
service_windows.go windows: fix --register-service when executed from within binary directory 2023-08-13 21:58:08 +02:00

docker.go contains Docker daemon's main function.

This file provides first line CLI argument parsing and environment variable setting.