-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsecurity_test.go
640 lines (560 loc) · 19.3 KB
/
security_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package garden_integration_tests_test
import (
"regexp"
"runtime"
"strings"
"code.cloudfoundry.org/garden"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("Security", func() {
var (
peaImage garden.ImageRef
noImage garden.ImageRef
)
BeforeEach(func() {
if runtime.GOOS == "windows" {
Skip("pending for windows")
}
peaImage = garden.ImageRef{URI: gardenRootfs}
noImage = garden.ImageRef{}
})
Describe("PID namespace", func() {
BeforeEach(func() {
imageRef.URI = limitsTestURI
})
It("isolates processes so that only processes from inside the container are visible", func() {
createUser(container, "alice")
_, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "/bin/sleep",
Args: []string{"989898"},
}, garden.ProcessIO{
Stdout: GinkgoWriter,
Stderr: GinkgoWriter,
})
Expect(err).ToNot(HaveOccurred())
Eventually(func() []string {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "sh",
Args: []string{"-c", "ps -a"},
})
return strings.Split(string(stdout.Contents()), "\n")
}).Should(HaveLen(5)) // header, garden-init, sleep, ps, \n
})
It("does not leak fds in to spawned processes", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "ls",
Args: []string{"/proc/self/fd"},
})
Expect(stdout).To(gbytes.Say("0\n1\n2\n3\n")) // stdin, stdout, stderr, /proc/self/fd
})
})
Describe("File system", func() {
It("/tmp is world-writable in the container", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "ls",
Args: []string{"-al", "/tmp"},
})
Expect(stdout).To(gbytes.Say(`drwxrwxrwt`))
})
It("/tmp IS mounted as tmpfs", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "cat",
Args: []string{"/proc/mounts"},
})
Expect(stdout).To(gbytes.Say("tmpfs /dev/shm tmpfs"))
})
Context("in an unprivileged container", func() {
BeforeEach(func() {
privilegedContainer = false
})
It("/sys IS mounted as Read-Only", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "cat",
Args: []string{"/proc/mounts"},
})
Expect(stdout).To(gbytes.Say("sysfs /sys sysfs ro"))
})
It("cgroup filesystems are mounted as read-only", func() {
getCGroupMountOptions := func(mounts string) map[string]bool {
cgroupMountRegex := regexp.MustCompile(`.*\s/sys/fs/cgroup/[^\s]*\s([^\s]*).*cgroup cgroup *\s([^\s]*)`)
matches := cgroupMountRegex.FindAllStringSubmatch(mounts, -1)
cgroupMountOptions := make(map[string]bool)
for _, m := range matches {
if len(m) == 3 && strings.Contains(m[1], "ro") {
for _, option := range strings.Split(m[2], ",") {
cgroupMountOptions[option] = true
}
}
}
return cgroupMountOptions
}
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "grep",
Args: []string{"cgroup", "/proc/self/mountinfo"},
})
cgroupMountOptions := getCGroupMountOptions(string(stdout.Contents()))
Expect(cgroupMountOptions).To(HaveKey("cpu"))
Expect(cgroupMountOptions).To(HaveKey("cpuacct"))
Expect(cgroupMountOptions).To(HaveKey("net_cls"))
Expect(cgroupMountOptions).To(HaveKey("net_prio"))
Expect(cgroupMountOptions).To(HaveKey("memory"))
Expect(cgroupMountOptions).To(HaveKey("cpuset"))
Expect(cgroupMountOptions).To(HaveKey("blkio"))
Expect(cgroupMountOptions).To(HaveKey("devices"))
Expect(cgroupMountOptions).To(HaveKey("freezer"))
Expect(cgroupMountOptions).To(HaveKey("perf_event"))
Expect(cgroupMountOptions).To(HaveKey("pids"))
Expect(cgroupMountOptions).To(HaveKey("hugetlb"))
})
})
Context("in a privileged container", func() {
BeforeEach(func() {
setPrivileged()
})
It("/proc IS mounted as Read-Write", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "cat",
Args: []string{"/proc/mounts"},
})
Expect(stdout).To(gbytes.Say("proc /proc proc rw"))
})
It("/sys IS mounted as Read-Only", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "cat",
Args: []string{"/proc/mounts"},
})
Expect(stdout).To(gbytes.Say("sysfs /sys sysfs ro"))
})
It("cgroup filesystems are not mounted", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "cat",
Args: []string{"/proc/mounts"},
})
Expect(stdout).NotTo(gbytes.Say("cgroup"))
})
})
})
Describe("Control groups", func() {
It("places the container in the required cgroup subsystems", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "/bin/sh",
Args: []string{"-c", "cat /proc/$$/cgroup"},
})
op := stdout.Contents()
Expect(op).To(MatchRegexp(`\bcpu\b`))
Expect(op).To(MatchRegexp(`\bcpuacct\b`))
Expect(op).To(MatchRegexp(`\bcpuset\b`))
Expect(op).To(MatchRegexp(`\bdevices\b`))
Expect(op).To(MatchRegexp(`\bmemory\b`))
})
})
Describe("rlimits", func() {
BeforeEach(func() {
imageRef.URI = limitsTestURI
})
It("sets requested rlimits", func() {
limit := uint64(4567)
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "/bin/sh",
Args: []string{"-c", "ulimit -a"},
Limits: garden.ResourceLimits{
Nproc: &limit,
},
})
Expect(stdout).To(gbytes.Say("processes\\W+(-u)\\W+4567"))
})
})
Describe("Users and groups", func() {
BeforeEach(func() {
imageRef.URI = "docker:///cloudfoundry/garden-rootfs"
})
JustBeforeEach(func() {
createUser(container, "alice")
})
It("maintains setuid permissions in unprivileged containers", func() {
skipIfWoot("Woot blindly chmods to Maximus thus screwing the suid flag")
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "ls",
Args: []string{"-l", "/bin/usemem-with-setuid"},
})
Eventually(stdout).Should(gbytes.Say("-rws"))
})
Context("when running a command in a working dir", func() {
It("executes with setuid and setgid", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Dir: "/usr",
Path: "pwd",
})
Expect(stdout).To(gbytes.Say("^/usr\n"))
})
})
Context("when running a command as a non-root user", func() {
It("executes with correct uid, gid, and supplementary gids", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "/bin/sh",
Args: []string{"-c", "id -u; id -g; id -G"},
})
Expect(stdout).To(gbytes.Say("1001\n1010\n1010 1011\n"))
})
It("sets $HOME, $USER, and $PATH", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "/bin/sh",
Args: []string{"-c", "env | sort"},
})
Expect(stdout).To(gbytes.Say("HOME=/home/alice\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/from-dockerfile\nPWD=/home/alice\nSHLVL=1\nTEST=second-test-from-dockerfile:test-from-dockerfile\nUSER=alice\n"))
})
Context("when $HOME is set in the spec", func() {
It("sets $HOME from the spec", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "/bin/sh",
Args: []string{"-c", "echo $HOME"},
Env: []string{
"HOME=/nowhere",
},
})
Expect(stdout).To(gbytes.Say("/nowhere"))
})
})
Context("when env is set in the spec", func() {
It("sets env from the spec", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "/bin/sh",
Args: []string{"-c", "env"},
Env: []string{
"USER=nobody",
},
})
Expect(stdout).To(gbytes.Say("USER=nobody"))
Expect(stdout).To(gbytes.Say("HOME=/home/alice"))
})
})
It("executes in the user's home directory", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "/bin/pwd",
})
Expect(stdout).To(gbytes.Say("/home/alice\n"))
})
It("searches a sanitized path not including /sbin for the executable", func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "alice",
Path: "ls",
})
Expect(exitCode).To(Equal(0))
_, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "hello-world", // hello-world is only available in /sbin
}, garden.ProcessIO{
Stdout: GinkgoWriter,
Stderr: GinkgoWriter,
})
Expect(err).To(HaveOccurred())
})
})
Context("when running a command as root", func() {
It("executes with uid 0, gid 0, and supplementary gids from /etc/group", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "/bin/sh",
Args: []string{"-c", "id -u; id -g; id -G"},
})
Expect(stdout).To(gbytes.Say("0\n0\n0 10\n"))
})
It("sets $HOME, $USER, and $PATH", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "/bin/sh",
Args: []string{"-c", "env | sort"},
})
Expect(stdout).To(gbytes.Say("HOME=/root\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/from-dockerfile\nPWD=/root\nSHLVL=1\nTEST=second-test-from-dockerfile:test-from-dockerfile\nUSER=root\n"))
})
It("executes in root's home directory", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "/bin/pwd",
})
Expect(stdout).To(gbytes.Say("/root\n"))
})
It("searches a sanitized path not including /sbin for the executable", func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "root",
Path: "hello-world", // hello-world is only available in /sbin
Env: []string{"PATH=/sbin"},
})
Expect(exitCode).To(Equal(0))
})
})
})
Context("by default (unprivileged)", func() {
Describe("seccomp", func() {
itAppliesSeccomp := func(image garden.ImageRef) {
It("blocks syscalls not whitelisted in the default seccomp profile", func() {
exitCode, _, stderr := runProcess(container, garden.ProcessSpec{
Path: "unshare",
Args: []string{"--user", "whoami"},
Image: image,
})
Expect(exitCode).NotTo(Equal(0))
Expect(stderr).To(gbytes.Say("Operation not permitted"))
})
It("applies seccomp in filter mode", func() {
stdout := runForStdout(container, garden.ProcessSpec{
Path: "grep",
Args: []string{"Seccomp", "/proc/self/status"},
Image: image,
})
Expect(string(stdout.Contents())).To(MatchRegexp(`Seccomp:\s+2`))
})
}
itAppliesSeccomp(noImage)
Context("when running a pea", func() {
itAppliesSeccomp(peaImage)
})
})
It("does not get root privileges on host resources", func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
Path: "sh",
User: "root",
Args: []string{"-c", "echo h > /proc/sysrq-trigger"},
})
Expect(exitCode).ToNot(Equal(0))
})
It("can write to files in the /root directory", func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `touch /root/potato`},
})
Expect(exitCode).To(Equal(0))
})
Describe("capabilities", func() {
Describe("the init process", func() {
It("has a reduced set of capabilities, not including CAP_SYS_ADMIN", func() {
stdout := runForStdout(container, garden.ProcessSpec{
Path: "cat",
Args: []string{"/proc/1/status"},
})
Eventually(stdout).Should(gbytes.Say("CapInh:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapPrm:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapEff:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapBnd:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapAmb:\\W+0000000000000000"))
})
})
Describe("a process running as the root user", func() {
itHasReducedCapabilities := func(image garden.ImageRef) {
It("has a reduced set of capabilities, not including CAP_SYS_ADMIN", func() {
stdout := runForStdout(container, garden.ProcessSpec{
Path: "cat",
Args: []string{"/proc/self/status"},
Image: image,
})
Eventually(stdout).Should(gbytes.Say("CapInh:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapPrm:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapEff:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapBnd:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapAmb:\\W+0000000000000000"))
})
}
itHasReducedCapabilities(noImage)
Context("when running a pea", func() {
itHasReducedCapabilities(peaImage)
})
})
Describe("a process running as a non-root user", func() {
itHasCorrectCapabilities := func(image garden.ImageRef) {
It("it has no effective caps and a reduced set of bounding capabilities, not including CAP_SYS_ADMIN", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "1000:1000",
Path: "cat",
Args: []string{"/proc/self/status"},
Image: image,
})
Eventually(stdout).Should(gbytes.Say("CapInh:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapPrm:\\W+0000000000000000"))
Eventually(stdout).Should(gbytes.Say("CapEff:\\W+0000000000000000"))
Eventually(stdout).Should(gbytes.Say("CapBnd:\\W+00000000a80425fb"))
Eventually(stdout).Should(gbytes.Say("CapAmb:\\W+0000000000000000"))
})
}
itHasCorrectCapabilities(noImage)
Context("when running a pea", func() {
itHasCorrectCapabilities(peaImage)
})
})
})
Context("with a docker image", func() {
BeforeEach(func() {
imageRef.URI = "docker:///cfgarden/preexisting_users"
})
It("sees root-owned files in the rootfs as owned by the container's root user", func() {
skipIfWoot("Woot blindly chmods to Maximus thus screwing the suid flag")
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `ls -l /bin | grep -v wsh | grep -v hook | grep -v proc_starter | grep -v initd`},
})
Expect(stdout).NotTo(gbytes.Say("nobody"))
Expect(stdout).NotTo(gbytes.Say("65534"))
Expect(stdout).To(gbytes.Say(" root "))
})
It("sees the /dev/pts and /dev/ptmx as owned by the container's root user", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", "ls -l /dev/pts /dev/ptmx /dev/pts/ptmx"},
})
Expect(stdout).NotTo(gbytes.Say("nobody"))
Expect(stdout).NotTo(gbytes.Say("65534"))
Expect(stdout).To(gbytes.Say(" root "))
})
It("sees alice-owned files as owned by alice", func() {
skipIfWoot("Woot blindly chmods to Maximus thus screwing the suid flag")
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "sh",
Args: []string{"-c", `ls -la /home/alice`},
})
Expect(stdout).To(gbytes.Say(" alice "))
Expect(stdout).To(gbytes.Say(" alicesfile"))
})
It("lets alice write in /home/alice", func() {
skipIfWoot("Woot blindly chmods to Maximus thus screwing the suid flag")
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "alice",
Path: "touch",
Args: []string{"/home/alice/newfile"},
})
Expect(exitCode).To(Equal(0))
})
It("lets root write to files in the /root directory", func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `touch /root/potato`},
})
Expect(exitCode).To(Equal(0))
})
It("preserves pre-existing dotfiles from base image", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "cat",
Args: []string{"/.foo"},
})
Expect(stdout).To(gbytes.Say("this is a pre-existing dotfile"))
})
})
})
Context("when the 'privileged' flag is set on the create call", func() {
BeforeEach(func() {
setPrivileged()
})
Context("and the user is root", func() {
It("has a full set of capabilities", func() {
stdout := runForStdout(container, garden.ProcessSpec{
Path: "cat",
Args: []string{"/proc/self/status"},
})
Expect(stdout).To(gbytes.Say("CapInh:\\W+0000003fffffffff"))
Expect(stdout).To(gbytes.Say("CapPrm:\\W+0000003fffffffff"))
Expect(stdout).To(gbytes.Say("CapEff:\\W+0000003fffffffff"))
Expect(stdout).To(gbytes.Say("CapBnd:\\W+0000003fffffffff"))
Expect(stdout).To(gbytes.Say("CapAmb:\\W+0000000000000000"))
})
})
Context("and the user is not root", func() {
JustBeforeEach(func() {
createUser(container, "alice")
})
It("has no effective capabilities, and a reduced set of capabilities that does include CAP_SYS_ADMIN", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "alice",
Path: "cat",
Args: []string{"/proc/self/status"},
})
Expect(stdout).To(gbytes.Say("CapInh:\\W+00000000a82425fb"))
Expect(stdout).To(gbytes.Say("CapPrm:\\W+0000000000000000"))
Expect(stdout).To(gbytes.Say("CapEff:\\W+0000000000000000"))
Expect(stdout).To(gbytes.Say("CapBnd:\\W+00000000a82425fb"))
Expect(stdout).To(gbytes.Say("CapAmb:\\W+0000000000000000"))
})
})
It("can write to files in the /root directory", func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `touch /root/potato`},
})
Expect(exitCode).To(Equal(0))
})
It("sees root-owned files in the rootfs as owned by the container's root user", func() {
stdout := runForStdout(container, garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `ls -l /bin | grep -v wsh | grep -v hook`},
})
Expect(stdout).NotTo(gbytes.Say("nobody"))
Expect(stdout).NotTo(gbytes.Say("65534"))
Expect(stdout).To(gbytes.Say(" root "))
})
Context("when the process is run as non-root user", func() {
BeforeEach(func() {
skipIfWoot("Ubuntu docker image layers contain whiteouts and woot does not handle them properly")
imageRef.URI = "docker:///ubuntu#14.04"
})
Context("and the user changes to root", func() {
JustBeforeEach(func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `echo "ALL ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers`},
})
Expect(exitCode).To(Equal(0))
exitCode, _, _ = runProcess(container, garden.ProcessSpec{
User: "root",
Path: "useradd",
Args: []string{"-U", "-m", "bob"},
})
Expect(exitCode).To(Equal(0))
})
It("can chown files", func() {
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "bob",
Path: "sudo",
Args: []string{"chown", "-R", "bob", "/tmp"},
})
Expect(exitCode).To(Equal(0))
})
It("does not have certain capabilities", func() {
// This attempts to set system time which requires the CAP_SYS_TIME permission.
exitCode, _, _ := runProcess(container, garden.ProcessSpec{
User: "bob",
Path: "sudo",
Args: []string{"date", "--set", "+2 minutes"},
})
Expect(exitCode).ToNot(Equal(0))
})
})
})
})
})