Browse code

Merge pull request #5464 from tianon/close-leftover-fds

Michael Crosby authored on 2014/05/01 04:27:52
Showing 7 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"github.com/dotcloud/docker/daemon/execdriver"
6 6
 	"github.com/dotcloud/docker/pkg/cgroups"
7 7
 	"github.com/dotcloud/docker/pkg/label"
8
+	"github.com/dotcloud/docker/pkg/system"
8 9
 	"github.com/dotcloud/docker/utils"
9 10
 	"io/ioutil"
10 11
 	"log"
... ...
@@ -42,6 +43,10 @@ func init() {
42 42
 			return err
43 43
 		}
44 44
 
45
+		if err := system.CloseFdsFrom(3); err != nil {
46
+			return err
47
+		}
48
+
45 49
 		if err := changeUser(args); err != nil {
46 50
 			return err
47 51
 		}
... ...
@@ -70,22 +70,6 @@ grep -q :devices: /proc/1/cgroup ||
70 70
 grep -qw devices /proc/1/cgroup ||
71 71
 	echo "WARNING: it looks like the 'devices' cgroup is not mounted."
72 72
 
73
-# Now, close extraneous file descriptors.
74
-pushd /proc/self/fd >/dev/null
75
-for FD in *
76
-do
77
-	case "$FD" in
78
-	# Keep stdin/stdout/stderr
79
-	[012])
80
-		;;
81
-	# Nuke everything else
82
-	*)
83
-		eval exec "$FD>&-"
84
-		;;
85
-	esac
86
-done
87
-popd >/dev/null
88
-
89 73
 # Mount /tmp
90 74
 mount -t tmpfs none /tmp
91 75
 
... ...
@@ -19,6 +19,9 @@ bundle_test_integration_cli() {
19 19
 		false
20 20
 	fi
21 21
 
22
+	# intentionally open a couple bogus file descriptors to help test that they get scrubbed in containers
23
+	exec 41>&1 42>&2
24
+
22 25
 	( set -x; exec \
23 26
 		docker --daemon --debug \
24 27
 		--storage-driver "$DOCKER_GRAPHDRIVER" \
... ...
@@ -92,6 +92,22 @@ func TestDockerRunEchoNamedContainer(t *testing.T) {
92 92
 	logDone("run - echo with named container")
93 93
 }
94 94
 
95
+// docker run should not leak file descriptors
96
+func TestDockerRunLeakyFileDescriptors(t *testing.T) {
97
+	runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
98
+	out, _, _, err := runCommandWithStdoutStderr(runCmd)
99
+	errorOut(err, t, out)
100
+
101
+	// normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
102
+	if out != "0  1  2  3\n" {
103
+		t.Errorf("container should've printed '0  1  2  3', not: %s", out)
104
+	}
105
+
106
+	deleteAllContainers()
107
+
108
+	logDone("run - check file descriptor leakage")
109
+}
110
+
95 111
 // it should be possible to ping Google DNS resolver
96 112
 // this will fail when Internet access is unavailable
97 113
 func TestDockerRunPingGoogle(t *testing.T) {
... ...
@@ -117,12 +117,16 @@ func setupNetwork(container *libcontainer.Container, context libcontainer.Contex
117 117
 	return nil
118 118
 }
119 119
 
120
-// finalizeNamespace drops the caps and sets the correct user
121
-// and working dir before execing the command inside the namespace
120
+// finalizeNamespace drops the caps, sets the correct user
121
+// and working dir, and closes any leaky file descriptors
122
+// before execing the command inside the namespace
122 123
 func finalizeNamespace(container *libcontainer.Container) error {
123 124
 	if err := capabilities.DropCapabilities(container); err != nil {
124 125
 		return fmt.Errorf("drop capabilities %s", err)
125 126
 	}
127
+	if err := system.CloseFdsFrom(3); err != nil {
128
+		return fmt.Errorf("close open file descriptors %s", err)
129
+	}
126 130
 	if err := setupUser(container); err != nil {
127 131
 		return fmt.Errorf("setup user %s", err)
128 132
 	}
129 133
new file mode 100644
... ...
@@ -0,0 +1,38 @@
0
+package system
1
+
2
+import (
3
+	"io/ioutil"
4
+	"strconv"
5
+	"syscall"
6
+)
7
+
8
+// Works similarly to OpenBSD's "closefrom(2)":
9
+//   The closefrom() call deletes all descriptors numbered fd and higher from
10
+//   the per-process file descriptor table.  It is effectively the same as
11
+//   calling close(2) on each descriptor.
12
+// http://www.openbsd.org/cgi-bin/man.cgi?query=closefrom&sektion=2
13
+//
14
+// See also http://stackoverflow.com/a/918469/433558
15
+func CloseFdsFrom(minFd int) error {
16
+	fdList, err := ioutil.ReadDir("/proc/self/fd")
17
+	if err != nil {
18
+		return err
19
+	}
20
+	for _, fi := range fdList {
21
+		fd, err := strconv.Atoi(fi.Name())
22
+		if err != nil {
23
+			// ignore non-numeric file names
24
+			continue
25
+		}
26
+
27
+		if fd < minFd {
28
+			// ignore descriptors lower than our specified minimum
29
+			continue
30
+		}
31
+
32
+		// intentionally ignore errors from syscall.Close
33
+		syscall.Close(fd)
34
+		// the cases where this might fail are basically file descriptors that have already been closed (including and especially the one that was created when ioutil.ReadDir did the "opendir" syscall)
35
+	}
36
+	return nil
37
+}
0 38
new file mode 100644
... ...
@@ -0,0 +1,12 @@
0
+// +build !linux
1
+
2
+package system
3
+
4
+import (
5
+	"fmt"
6
+	"runtime"
7
+)
8
+
9
+func CloseFdsFrom(minFd int) error {
10
+	return fmt.Errorf("CloseFdsFrom is unsupported on this platform (%s/%s)", runtime.GOOS, runtime.GOARCH)
11
+}