Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@docker.com> (github: crosbymichael)
Michael Crosby authored on 2014/06/20 03:07:57... | ... |
@@ -19,6 +19,7 @@ import ( |
19 | 19 |
"github.com/docker/libcontainer/label" |
20 | 20 |
"github.com/docker/libcontainer/mount/nodes" |
21 | 21 |
"github.com/dotcloud/docker/daemon/execdriver" |
22 |
+ "github.com/dotcloud/docker/pkg/system" |
|
22 | 23 |
"github.com/dotcloud/docker/utils" |
23 | 24 |
) |
24 | 25 |
|
... | ... |
@@ -36,7 +37,13 @@ func init() { |
36 | 36 |
if err := setupNetworking(args); err != nil { |
37 | 37 |
return err |
38 | 38 |
} |
39 |
- if err := setupCapabilities(args); err != nil { |
|
39 |
+ if err := setupWorkingDirectory(args); err != nil { |
|
40 |
+ return err |
|
41 |
+ } |
|
42 |
+ if err := system.CloseFdsFrom(3); err != nil { |
|
43 |
+ return err |
|
44 |
+ } |
|
45 |
+ if err := finalizeNamespace(args); err != nil { |
|
40 | 46 |
return err |
41 | 47 |
} |
42 | 48 |
|
... | ... |
@@ -11,9 +11,6 @@ import ( |
11 | 11 |
|
12 | 12 |
"github.com/docker/libcontainer/netlink" |
13 | 13 |
"github.com/dotcloud/docker/daemon/execdriver" |
14 |
- "github.com/dotcloud/docker/pkg/system" |
|
15 |
- "github.com/dotcloud/docker/pkg/user" |
|
16 |
- "github.com/syndtr/gocapability/capability" |
|
17 | 14 |
) |
18 | 15 |
|
19 | 16 |
// Clear environment pollution introduced by lxc-start |
... | ... |
@@ -108,126 +105,6 @@ func setupWorkingDirectory(args *execdriver.InitArgs) error { |
108 | 108 |
return nil |
109 | 109 |
} |
110 | 110 |
|
111 |
-// Takes care of dropping privileges to the desired user |
|
112 |
-func changeUser(args *execdriver.InitArgs) error { |
|
113 |
- uid, gid, suppGids, err := user.GetUserGroupSupplementary( |
|
114 |
- args.User, |
|
115 |
- syscall.Getuid(), syscall.Getgid(), |
|
116 |
- ) |
|
117 |
- if err != nil { |
|
118 |
- return err |
|
119 |
- } |
|
120 |
- |
|
121 |
- if err := syscall.Setgroups(suppGids); err != nil { |
|
122 |
- return fmt.Errorf("Setgroups failed: %v", err) |
|
123 |
- } |
|
124 |
- if err := syscall.Setgid(gid); err != nil { |
|
125 |
- return fmt.Errorf("Setgid failed: %v", err) |
|
126 |
- } |
|
127 |
- if err := syscall.Setuid(uid); err != nil { |
|
128 |
- return fmt.Errorf("Setuid failed: %v", err) |
|
129 |
- } |
|
130 |
- |
|
131 |
- return nil |
|
132 |
-} |
|
133 |
- |
|
134 |
-var whiteList = []capability.Cap{ |
|
135 |
- capability.CAP_MKNOD, |
|
136 |
- capability.CAP_SETUID, |
|
137 |
- capability.CAP_SETGID, |
|
138 |
- capability.CAP_CHOWN, |
|
139 |
- capability.CAP_NET_RAW, |
|
140 |
- capability.CAP_DAC_OVERRIDE, |
|
141 |
- capability.CAP_FOWNER, |
|
142 |
- capability.CAP_FSETID, |
|
143 |
- capability.CAP_KILL, |
|
144 |
- capability.CAP_SETGID, |
|
145 |
- capability.CAP_SETUID, |
|
146 |
- capability.CAP_LINUX_IMMUTABLE, |
|
147 |
- capability.CAP_NET_BIND_SERVICE, |
|
148 |
- capability.CAP_NET_BROADCAST, |
|
149 |
- capability.CAP_IPC_LOCK, |
|
150 |
- capability.CAP_IPC_OWNER, |
|
151 |
- capability.CAP_SYS_CHROOT, |
|
152 |
- capability.CAP_SYS_PTRACE, |
|
153 |
- capability.CAP_SYS_BOOT, |
|
154 |
- capability.CAP_LEASE, |
|
155 |
- capability.CAP_SETFCAP, |
|
156 |
- capability.CAP_WAKE_ALARM, |
|
157 |
- capability.CAP_BLOCK_SUSPEND, |
|
158 |
-} |
|
159 |
- |
|
160 |
-func dropBoundingSet() error { |
|
161 |
- c, err := capability.NewPid(os.Getpid()) |
|
162 |
- if err != nil { |
|
163 |
- return err |
|
164 |
- } |
|
165 |
- c.Clear(capability.BOUNDS) |
|
166 |
- c.Set(capability.BOUNDS, whiteList...) |
|
167 |
- |
|
168 |
- if err := c.Apply(capability.BOUNDS); err != nil { |
|
169 |
- return err |
|
170 |
- } |
|
171 |
- |
|
172 |
- return nil |
|
173 |
-} |
|
174 |
- |
|
175 |
-const allCapabilityTypes = capability.CAPS | capability.BOUNDS |
|
176 |
- |
|
177 |
-func dropCapabilities() error { |
|
178 |
- c, err := capability.NewPid(os.Getpid()) |
|
179 |
- if err != nil { |
|
180 |
- return err |
|
181 |
- } |
|
182 |
- c.Clear(allCapabilityTypes) |
|
183 |
- c.Set(allCapabilityTypes, whiteList...) |
|
184 |
- |
|
185 |
- if err := c.Apply(allCapabilityTypes); err != nil { |
|
186 |
- return err |
|
187 |
- } |
|
188 |
- |
|
189 |
- return nil |
|
190 |
-} |
|
191 |
- |
|
192 |
-func setupCapabilities(args *execdriver.InitArgs) error { |
|
193 |
- if err := system.CloseFdsFrom(3); err != nil { |
|
194 |
- return err |
|
195 |
- } |
|
196 |
- |
|
197 |
- if !args.Privileged { |
|
198 |
- // drop capabilities in bounding set before changing user |
|
199 |
- if err := dropBoundingSet(); err != nil { |
|
200 |
- return fmt.Errorf("drop bounding set %s", err) |
|
201 |
- } |
|
202 |
- |
|
203 |
- // preserve existing capabilities while we change users |
|
204 |
- if err := system.SetKeepCaps(); err != nil { |
|
205 |
- return fmt.Errorf("set keep caps %s", err) |
|
206 |
- } |
|
207 |
- } |
|
208 |
- |
|
209 |
- if err := changeUser(args); err != nil { |
|
210 |
- return err |
|
211 |
- } |
|
212 |
- |
|
213 |
- if !args.Privileged { |
|
214 |
- if err := system.ClearKeepCaps(); err != nil { |
|
215 |
- return fmt.Errorf("clear keep caps %s", err) |
|
216 |
- } |
|
217 |
- |
|
218 |
- // drop all other capabilities |
|
219 |
- if err := dropCapabilities(); err != nil { |
|
220 |
- return fmt.Errorf("drop capabilities %s", err) |
|
221 |
- } |
|
222 |
- } |
|
223 |
- |
|
224 |
- if err := setupWorkingDirectory(args); err != nil { |
|
225 |
- return err |
|
226 |
- } |
|
227 |
- |
|
228 |
- return nil |
|
229 |
-} |
|
230 |
- |
|
231 | 111 |
func getEnv(args *execdriver.InitArgs, key string) string { |
232 | 112 |
for _, kv := range args.Env { |
233 | 113 |
parts := strings.SplitN(kv, "=", 2) |
... | ... |
@@ -3,9 +3,51 @@ |
3 | 3 |
package lxc |
4 | 4 |
|
5 | 5 |
import ( |
6 |
+ "fmt" |
|
6 | 7 |
"syscall" |
8 |
+ |
|
9 |
+ "github.com/docker/libcontainer/namespaces" |
|
10 |
+ "github.com/docker/libcontainer/security/capabilities" |
|
11 |
+ "github.com/dotcloud/docker/daemon/execdriver" |
|
12 |
+ "github.com/dotcloud/docker/daemon/execdriver/native/template" |
|
13 |
+ "github.com/dotcloud/docker/pkg/system" |
|
7 | 14 |
) |
8 | 15 |
|
9 | 16 |
func setHostname(hostname string) error { |
10 | 17 |
return syscall.Sethostname([]byte(hostname)) |
11 | 18 |
} |
19 |
+ |
|
20 |
+func finalizeNamespace(args *execdriver.InitArgs) error { |
|
21 |
+ // We use the native drivers default template so that things like caps are consistent |
|
22 |
+ // across both drivers |
|
23 |
+ container := template.New() |
|
24 |
+ |
|
25 |
+ if !args.Privileged { |
|
26 |
+ // drop capabilities in bounding set before changing user |
|
27 |
+ if err := capabilities.DropBoundingSet(container); err != nil { |
|
28 |
+ return fmt.Errorf("drop bounding set %s", err) |
|
29 |
+ } |
|
30 |
+ |
|
31 |
+ // preserve existing capabilities while we change users |
|
32 |
+ if err := system.SetKeepCaps(); err != nil { |
|
33 |
+ return fmt.Errorf("set keep caps %s", err) |
|
34 |
+ } |
|
35 |
+ } |
|
36 |
+ |
|
37 |
+ if err := namespaces.SetupUser(args.User); err != nil { |
|
38 |
+ return fmt.Errorf("setup user %s", err) |
|
39 |
+ } |
|
40 |
+ |
|
41 |
+ if !args.Privileged { |
|
42 |
+ if err := system.ClearKeepCaps(); err != nil { |
|
43 |
+ return fmt.Errorf("clear keep caps %s", err) |
|
44 |
+ } |
|
45 |
+ |
|
46 |
+ // drop all other capabilities |
|
47 |
+ if err := capabilities.DropCapabilities(container); err != nil { |
|
48 |
+ return fmt.Errorf("drop capabilities %s", err) |
|
49 |
+ } |
|
50 |
+ } |
|
51 |
+ |
|
52 |
+ return nil |
|
53 |
+} |
... | ... |
@@ -2,6 +2,12 @@ |
2 | 2 |
|
3 | 3 |
package lxc |
4 | 4 |
|
5 |
+import "github.com/dotcloud/docker/daemon/execdriver" |
|
6 |
+ |
|
5 | 7 |
func setHostname(hostname string) error { |
6 | 8 |
panic("Not supported on darwin") |
7 | 9 |
} |
10 |
+ |
|
11 |
+func finalizeNamespace(args *execdriver.InitArgs) error { |
|
12 |
+ panic("Not supported on darwin") |
|
13 |
+} |
... | ... |
@@ -28,3 +28,11 @@ func GetClockTicks() int { |
28 | 28 |
func CreateMasterAndConsole() (*os.File, string, error) { |
29 | 29 |
return nil, "", ErrNotSupportedPlatform |
30 | 30 |
} |
31 |
+ |
|
32 |
+func SetKeepCaps() error { |
|
33 |
+ return ErrNotSupportedPlatform |
|
34 |
+} |
|
35 |
+ |
|
36 |
+func ClearKeepCaps() error { |
|
37 |
+ return ErrNotSupportedPlatform |
|
38 |
+} |