Browse code

dockerd: fix rootless detection (alternative to #39024)

The `--rootless` flag had a couple of issues:
* #38702: euid=0, $USER="root" but no access to cgroup ("rootful" Docker in rootless Docker)
* #39009: euid=0 but $USER="docker" (rootful boot2docker)

To fix #38702, XDG dirs are ignored as in rootful Docker, unless the
dockerd is directly running under RootlessKit namespaces.

RootlessKit detection is implemented by checking whether `$ROOTLESSKIT_STATE_DIR` is set.

To fix #39009, the non-robust `$USER` check is now completely removed.

The entire logic can be illustrated as follows:

```
withRootlessKit := getenv("ROOTLESSKIT_STATE_DIR")
rootlessMode := withRootlessKit || cliFlag("--rootless")
honorXDG := withRootlessKit
useRootlessKitDockerProxy := withRootlessKit
removeCgroupSpec := rootlessMode
adjustOOMScoreAdj := rootlessMode
```

Close #39024
Fix #38702 #39009

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
(cherry picked from commit 3518383ed990202d93e5458782d2c975c48ececd)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Akihiro Suda authored on 2019/04/19 16:53:58
Showing 8 changed files
... ...
@@ -9,12 +9,11 @@ import (
9 9
 	"github.com/docker/docker/daemon/config"
10 10
 	"github.com/docker/docker/opts"
11 11
 	"github.com/docker/docker/pkg/homedir"
12
-	"github.com/docker/docker/rootless"
13 12
 	"github.com/spf13/pflag"
14 13
 )
15 14
 
16 15
 func getDefaultPidFile() (string, error) {
17
-	if !rootless.RunningWithNonRootUsername() {
16
+	if !honorXDG {
18 17
 		return "/var/run/docker.pid", nil
19 18
 	}
20 19
 	runtimeDir, err := homedir.GetRuntimeDir()
... ...
@@ -25,7 +24,7 @@ func getDefaultPidFile() (string, error) {
25 25
 }
26 26
 
27 27
 func getDefaultDataRoot() (string, error) {
28
-	if !rootless.RunningWithNonRootUsername() {
28
+	if !honorXDG {
29 29
 		return "/var/lib/docker", nil
30 30
 	}
31 31
 	dataHome, err := homedir.GetDataHome()
... ...
@@ -36,7 +35,7 @@ func getDefaultDataRoot() (string, error) {
36 36
 }
37 37
 
38 38
 func getDefaultExecRoot() (string, error) {
39
-	if !rootless.RunningWithNonRootUsername() {
39
+	if !honorXDG {
40 40
 		return "/var/run/docker", nil
41 41
 	}
42 42
 	runtimeDir, err := homedir.GetRuntimeDir()
... ...
@@ -3,10 +3,13 @@
3 3
 package main
4 4
 
5 5
 import (
6
+	"os/exec"
7
+
6 8
 	"github.com/docker/docker/daemon/config"
7 9
 	"github.com/docker/docker/opts"
8 10
 	"github.com/docker/docker/rootless"
9 11
 	"github.com/docker/go-units"
12
+	"github.com/pkg/errors"
10 13
 	"github.com/spf13/pflag"
11 14
 )
12 15
 
... ...
@@ -35,7 +38,16 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error {
35 35
 	flags.BoolVar(&conf.BridgeConfig.EnableIPv6, "ipv6", false, "Enable IPv6 networking")
36 36
 	flags.StringVar(&conf.BridgeConfig.FixedCIDRv6, "fixed-cidr-v6", "", "IPv6 subnet for fixed IPs")
37 37
 	flags.BoolVar(&conf.BridgeConfig.EnableUserlandProxy, "userland-proxy", true, "Use userland proxy for loopback traffic")
38
-	flags.StringVar(&conf.BridgeConfig.UserlandProxyPath, "userland-proxy-path", "", "Path to the userland proxy binary")
38
+	defaultUserlandProxyPath := ""
39
+	if rootless.RunningWithRootlessKit() {
40
+		var err error
41
+		// use rootlesskit-docker-proxy for exposing the ports in RootlessKit netns to the initial namespace.
42
+		defaultUserlandProxyPath, err = exec.LookPath(rootless.RootlessKitDockerProxyBinary)
43
+		if err != nil {
44
+			return errors.Wrapf(err, "running with RootlessKit, but %s not installed", rootless.RootlessKitDockerProxyBinary)
45
+		}
46
+	}
47
+	flags.StringVar(&conf.BridgeConfig.UserlandProxyPath, "userland-proxy-path", defaultUserlandProxyPath, "Path to the userland proxy binary")
39 48
 	flags.StringVar(&conf.CgroupParent, "cgroup-parent", "", "Set parent cgroup for all containers")
40 49
 	flags.StringVar(&conf.RemappedRoot, "userns-remap", "", "User/Group setting for user namespaces")
41 50
 	flags.BoolVar(&conf.LiveRestoreEnabled, "live-restore", false, "Enable live restore of docker when containers are still running")
... ...
@@ -49,7 +61,8 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error {
49 49
 	flags.BoolVar(&conf.NoNewPrivileges, "no-new-privileges", false, "Set no-new-privileges by default for new containers")
50 50
 	flags.StringVar(&conf.IpcMode, "default-ipc-mode", config.DefaultIpcMode, `Default mode for containers ipc ("shareable" | "private")`)
51 51
 	flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "Default address pools for node specific local networks")
52
-	// Mostly users don't need to set this flag explicitly.
53
-	flags.BoolVar(&conf.Rootless, "rootless", rootless.RunningWithNonRootUsername(), "Enable rootless mode (experimental)")
52
+	// rootless needs to be explicitly specified for running "rootful" dockerd in rootless dockerd (#38702)
53
+	// Note that defaultUserlandProxyPath and honorXDG are configured according to the value of rootless.RunningWithRootlessKit, not the value of --rootless.
54
+	flags.BoolVar(&conf.Rootless, "rootless", rootless.RunningWithRootlessKit(), "Enable rootless mode; typically used with RootlessKit (experimental)")
54 55
 	return nil
55 56
 }
... ...
@@ -103,6 +103,12 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
103 103
 		if cli.Config.IsRootless() {
104 104
 			logrus.Warn("Running in rootless mode. Cgroups, AppArmor, and CRIU are disabled.")
105 105
 		}
106
+		if rootless.RunningWithRootlessKit() {
107
+			logrus.Info("Running with RootlessKit integration")
108
+			if !cli.Config.IsRootless() {
109
+				return fmt.Errorf("rootless mode needs to be enabled for running with RootlessKit")
110
+			}
111
+		}
106 112
 	} else {
107 113
 		if cli.Config.IsRootless() {
108 114
 			return fmt.Errorf("rootless mode is supported only when running in experimental mode")
... ...
@@ -591,7 +597,7 @@ func loadListeners(cli *DaemonCli, serverConfig *apiserver.Config) ([]string, er
591 591
 	var hosts []string
592 592
 	for i := 0; i < len(cli.Config.Hosts); i++ {
593 593
 		var err error
594
-		if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, rootless.RunningWithNonRootUsername(), cli.Config.Hosts[i]); err != nil {
594
+		if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, honorXDG, cli.Config.Hosts[i]); err != nil {
595 595
 			return nil, errors.Wrapf(err, "error parsing -H %s", cli.Config.Hosts[i])
596 596
 		}
597 597
 
... ...
@@ -668,9 +674,9 @@ func validateAuthzPlugins(requestedPlugins []string, pg plugingetter.PluginGette
668 668
 	return nil
669 669
 }
670 670
 
671
-func systemContainerdRunning(isRootless bool) (string, bool, error) {
671
+func systemContainerdRunning(honorXDG bool) (string, bool, error) {
672 672
 	addr := containerddefaults.DefaultAddress
673
-	if isRootless {
673
+	if honorXDG {
674 674
 		runtimeDir, err := homedir.GetRuntimeDir()
675 675
 		if err != nil {
676 676
 			return "", false, err
... ...
@@ -18,14 +18,13 @@ import (
18 18
 	"github.com/docker/docker/daemon/config"
19 19
 	"github.com/docker/docker/libcontainerd/supervisor"
20 20
 	"github.com/docker/docker/pkg/homedir"
21
-	"github.com/docker/docker/rootless"
22 21
 	"github.com/docker/libnetwork/portallocator"
23 22
 	"github.com/pkg/errors"
24 23
 	"golang.org/x/sys/unix"
25 24
 )
26 25
 
27 26
 func getDefaultDaemonConfigDir() (string, error) {
28
-	if !rootless.RunningWithNonRootUsername() {
27
+	if !honorXDG {
29 28
 		return "/etc/docker", nil
30 29
 	}
31 30
 	// NOTE: CLI uses ~/.docker while the daemon uses ~/.config/docker, because
... ...
@@ -148,7 +147,7 @@ func newCgroupParent(config *config.Config) string {
148 148
 func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) error, error) {
149 149
 	var waitForShutdown func(time.Duration) error
150 150
 	if cli.Config.ContainerdAddr == "" {
151
-		systemContainerdAddr, ok, err := systemContainerdRunning(cli.Config.IsRootless())
151
+		systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG)
152 152
 		if err != nil {
153 153
 			return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
154 154
 		}
... ...
@@ -10,11 +10,16 @@ import (
10 10
 	"github.com/docker/docker/pkg/jsonmessage"
11 11
 	"github.com/docker/docker/pkg/reexec"
12 12
 	"github.com/docker/docker/pkg/term"
13
+	"github.com/docker/docker/rootless"
13 14
 	"github.com/moby/buildkit/util/apicaps"
14 15
 	"github.com/sirupsen/logrus"
15 16
 	"github.com/spf13/cobra"
16 17
 )
17 18
 
19
+var (
20
+	honorXDG bool
21
+)
22
+
18 23
 func newDaemonCommand() (*cobra.Command, error) {
19 24
 	opts := newDaemonOptions(config.New())
20 25
 
... ...
@@ -53,6 +58,14 @@ func init() {
53 53
 	if dockerversion.ProductName != "" {
54 54
 		apicaps.ExportedProduct = dockerversion.ProductName
55 55
 	}
56
+	// When running with RootlessKit, $XDG_RUNTIME_DIR, $XDG_DATA_HOME, and $XDG_CONFIG_HOME needs to be
57
+	// honored as the default dirs, because we are unlikely to have permissions to access the system-wide
58
+	// directories.
59
+	//
60
+	// Note that even running with --rootless, when not running with RootlessKit, honorXDG needs to be kept false,
61
+	// because the system-wide directories in the current mount namespace are expected to be accessible.
62
+	// ("rootful" dockerd in rootless dockerd, #38702)
63
+	honorXDG = rootless.RunningWithRootlessKit()
56 64
 }
57 65
 
58 66
 func main() {
... ...
@@ -54,10 +54,9 @@ penguin:231072:65536
54 54
 You need to run `dockerd-rootless.sh` instead of `dockerd`.
55 55
 
56 56
 ```console
57
-$ dockerd-rootless.sh --experimental --userland-proxy --userland-proxy-path=$(which rootlesskit-docker-proxy)"
57
+$ dockerd-rootless.sh --experimental
58 58
 ```
59 59
 As Rootless mode is experimental per se, currently you always need to run `dockerd-rootless.sh` with `--experimental`.
60
-Also, to expose ports, you need to set `--userland-proxy-path` to the path of `rootlesskit-docker-proxy` binary.
61 60
 
62 61
 Remarks:
63 62
 * The socket path is set to `$XDG_RUNTIME_DIR/docker.sock` by default. `$XDG_RUNTIME_DIR` is typically set to `/run/user/$UID`.
... ...
@@ -45,13 +45,13 @@ func ValidateHost(val string) (string, error) {
45 45
 }
46 46
 
47 47
 // ParseHost and set defaults for a Daemon host string.
48
-// defaultToTLS is preferred over defaultToUnixRootless.
49
-func ParseHost(defaultToTLS, defaultToUnixRootless bool, val string) (string, error) {
48
+// defaultToTLS is preferred over defaultToUnixXDG.
49
+func ParseHost(defaultToTLS, defaultToUnixXDG bool, val string) (string, error) {
50 50
 	host := strings.TrimSpace(val)
51 51
 	if host == "" {
52 52
 		if defaultToTLS {
53 53
 			host = DefaultTLSHost
54
-		} else if defaultToUnixRootless {
54
+		} else if defaultToUnixXDG {
55 55
 			runtimeDir, err := homedir.GetRuntimeDir()
56 56
 			if err != nil {
57 57
 				return "", err
... ...
@@ -5,22 +5,21 @@ import (
5 5
 	"sync"
6 6
 )
7 7
 
8
+const (
9
+	// RootlessKitDockerProxyBinary is the binary name of rootlesskit-docker-proxy
10
+	RootlessKitDockerProxyBinary = "rootlesskit-docker-proxy"
11
+)
12
+
8 13
 var (
9
-	runningWithNonRootUsername     bool
10
-	runningWithNonRootUsernameOnce sync.Once
14
+	runningWithRootlessKit     bool
15
+	runningWithRootlessKitOnce sync.Once
11 16
 )
12 17
 
13
-// RunningWithNonRootUsername returns true if we $USER is set to a non-root value,
14
-// regardless to the UID/EUID value.
15
-//
16
-// The value of this variable is mostly used for configuring default paths.
17
-// If the value is true, $HOME and $XDG_RUNTIME_DIR should be honored for setting up the default paths.
18
-// If false (not only EUID==0 but also $USER==root), $HOME and $XDG_RUNTIME_DIR should be ignored
19
-// even if we are in a user namespace.
20
-func RunningWithNonRootUsername() bool {
21
-	runningWithNonRootUsernameOnce.Do(func() {
22
-		u := os.Getenv("USER")
23
-		runningWithNonRootUsername = u != "" && u != "root"
18
+// RunningWithRootlessKit returns true if running under RootlessKit namespaces.
19
+func RunningWithRootlessKit() bool {
20
+	runningWithRootlessKitOnce.Do(func() {
21
+		u := os.Getenv("ROOTLESSKIT_STATE_DIR")
22
+		runningWithRootlessKit = u != ""
24 23
 	})
25
-	return runningWithNonRootUsername
24
+	return runningWithRootlessKit
26 25
 }