moby/cmd/dockerd
Sebastiaan van Stijn bd6e4ae215
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:49 +02:00
..
trap cmd/dockerd: use default SIGQUIT behaviour 2023-01-17 13:41:01 +01:00
config.go daemon/config: New(): initialize config with platform-specific defaults 2023-01-10 11:03:04 +01:00
config_unix.go rootless: move ./rootless to ./pkg/rootless 2023-01-10 11:26:15 +01:00
config_unix_test.go daemon/config: New(): initialize config with platform-specific defaults 2023-01-10 11:03:04 +01:00
config_windows.go daemon/config: New(): initialize config with platform-specific defaults 2023-01-10 11:03:04 +01:00
daemon.go build: use daemon id as worker id for the graph driver controller 2023-05-18 22:22:23 +02:00
daemon_freebsd.go daemon: move check for CPU-realtime daemon options 2022-03-03 19:50:27 +01:00
daemon_linux.go daemon: move check for CPU-realtime daemon options 2022-03-03 19:50:27 +01:00
daemon_test.go cmd/dockerd: loadDaemonCliConfig() safeguard for unparsed flags 2022-06-29 19:55:06 +02:00
daemon_unix.go daemon: improve some errors 2022-06-29 19:54:57 +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 and local trustkey utilities 2022-12-03 18:09:16 +01:00
docker.go Silence GRPC logs unless our log level is debug 2023-04-21 12:33:33 +02: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 and local trustkey utilities 2022-12-03 18:09:16 +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-04-21 12:33:33 +02:00
metrics.go set ReadHeaderTimeout to address G112: Potential Slowloris Attack (gosec) 2022-09-22 12:27:32 +02:00
options.go use ad-hoc libtrust key 2022-12-03 18:09:16 +01:00
options_test.go cmd/dockerd: un-export config methods, and don't pass flags "twice" 2022-04-27 00:34:17 +02:00
README.md Fix readme doc for dockerd 2016-09-01 14:47:51 +08: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:49 +02:00

docker.go contains Docker daemon's main function.

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