This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
/
pod.go
783 lines (661 loc) · 24.2 KB
/
pod.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// Copyright 2014 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//+build linux
package common
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"github.com/rkt/rkt/pkg/acl"
stage1commontypes "github.com/rkt/rkt/stage1/common/types"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
"github.com/coreos/go-systemd/unit"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/common"
"github.com/rkt/rkt/pkg/fileutil"
"github.com/rkt/rkt/pkg/user"
)
const (
// FlavorFile names the file storing the pod's flavor
FlavorFile = "flavor"
)
// execEscape uses Golang's string quoting for ", \, \n, and regex for special cases
func execEscape(i int, str string) string {
escapeMap := map[string]string{
`'`: `\`,
}
if i > 0 { // These are escaped only after the first argument
escapeMap[`$`] = `$`
}
escArg := fmt.Sprintf("%q", str)
for k := range escapeMap {
reStr := `([` + regexp.QuoteMeta(k) + `])`
re := regexp.MustCompile(reStr)
escArg = re.ReplaceAllStringFunc(escArg, func(s string) string {
escaped := escapeMap[s] + s
return escaped
})
}
return escArg
}
// quoteExec returns an array of quoted strings appropriate for systemd execStart usage
func quoteExec(exec []string) string {
if len(exec) == 0 {
// existing callers always include at least the binary so this shouldn't occur.
panic("empty exec")
}
var qexec []string
for i, arg := range exec {
escArg := execEscape(i, arg)
qexec = append(qexec, escArg)
}
return strings.Join(qexec, " ")
}
func writeAppReaper(p *stage1commontypes.Pod, appName string, appRootDirectory string, binPath string) error {
opts := []*unit.UnitOption{
unit.NewUnitOption("Unit", "Description", fmt.Sprintf("%s Reaper", appName)),
unit.NewUnitOption("Unit", "DefaultDependencies", "false"),
unit.NewUnitOption("Unit", "StopWhenUnneeded", "yes"),
unit.NewUnitOption("Unit", "Wants", "shutdown.service"),
unit.NewUnitOption("Unit", "After", "shutdown.service"),
unit.NewUnitOption("Unit", "Conflicts", "exit.target"),
unit.NewUnitOption("Unit", "Conflicts", "halt.target"),
unit.NewUnitOption("Unit", "Conflicts", "poweroff.target"),
unit.NewUnitOption("Service", "RemainAfterExit", "yes"),
unit.NewUnitOption("Service", "ExecStop", fmt.Sprintf("/reaper.sh \"%s\" \"%s\" \"%s\"", appName, appRootDirectory, binPath)),
}
unitsPath := filepath.Join(common.Stage1RootfsPath(p.Root), UnitsDir)
file, err := os.OpenFile(filepath.Join(unitsPath, fmt.Sprintf("reaper-%s.service", appName)), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return errwrap.Wrap(errors.New("failed to create service unit file"), err)
}
defer file.Close()
if _, err = io.Copy(file, unit.Serialize(opts)); err != nil {
return errwrap.Wrap(errors.New("failed to write service unit file"), err)
}
return nil
}
// SetJournalPermissions sets ACLs and permissions so the rkt group can access
// the pod's logs
func SetJournalPermissions(p *stage1commontypes.Pod) error {
s1 := common.Stage1ImagePath(p.Root)
rktgid, err := common.LookupGid(common.RktGroup)
if err != nil {
return fmt.Errorf("group %q not found", common.RktGroup)
}
journalPath := filepath.Join(s1, "rootfs", "var", "log", "journal")
if err := os.MkdirAll(journalPath, os.FileMode(0755)); err != nil {
return errwrap.Wrap(errors.New("error creating journal dir"), err)
}
a, err := acl.InitACL()
if err != nil {
return err
}
defer a.Free()
if err := a.ParseACL(fmt.Sprintf("g:%d:r-x,m:r-x", rktgid)); err != nil {
return errwrap.Wrap(errors.New("error parsing ACL string"), err)
}
if err := a.AddBaseEntries(journalPath); err != nil {
return errwrap.Wrap(errors.New("error adding base ACL entries"), err)
}
if err := a.Valid(); err != nil {
return err
}
if err := a.SetFileACLDefault(journalPath); err != nil {
return errwrap.Wrap(fmt.Errorf("error setting default ACLs on %q", journalPath), err)
}
return nil
}
func generateGidArg(gid int, supplGid []int) string {
arg := []string{strconv.Itoa(gid)}
for _, sg := range supplGid {
arg = append(arg, strconv.Itoa(sg))
}
return strings.Join(arg, ",")
}
// findHostPort returns the port number on the host that corresponds to an
// image manifest port identified by name
func findHostPort(pm schema.PodManifest, name types.ACName) uint {
var port uint
for _, p := range pm.Ports {
if p.Name == name {
port = p.HostPort
}
}
return port
}
// generateSysusers generates systemd sysusers files for a given app so that
// corresponding entries in /etc/passwd and /etc/group are created in stage1.
// This is needed to use the "User=" and "Group=" options in the systemd
// service files of apps.
// If there're several apps defining the same UIDs/GIDs, systemd will take care
// of only generating one /etc/{passwd,group} entry
func generateSysusers(p *stage1commontypes.Pod, ra *schema.RuntimeApp, uid_ int, gid_ int, uidRange *user.UidRange) error {
var toShift []string
app := ra.App
appName := ra.Name
sysusersDir := path.Join(common.Stage1RootfsPath(p.Root), "usr/lib/sysusers.d")
toShift = append(toShift, sysusersDir)
if err := os.MkdirAll(sysusersDir, 0755); err != nil {
return err
}
gids := append(app.SupplementaryGIDs, gid_)
// Create the Unix user and group
var sysusersConf []string
for _, g := range gids {
groupname := "gen" + strconv.Itoa(g)
sysusersConf = append(sysusersConf, fmt.Sprintf("g %s %d\n", groupname, g))
}
username := "gen" + strconv.Itoa(uid_)
sysusersConf = append(sysusersConf, fmt.Sprintf("u %s %d \"%s\"\n", username, uid_, username))
sysusersFile := path.Join(common.Stage1RootfsPath(p.Root), "usr/lib/sysusers.d", ServiceUnitName(appName)+".conf")
toShift = append(toShift, sysusersFile)
if err := ioutil.WriteFile(sysusersFile, []byte(strings.Join(sysusersConf, "\n")), 0640); err != nil {
return err
}
if err := user.ShiftFiles(toShift, uidRange); err != nil {
return err
}
return nil
}
// lookupPathInsideApp returns the path (relative to the app rootfs) of the
// given binary. It will look up on "paths" (also relative to the app rootfs)
// and evaluate possible symlinks to check if the resulting path is actually
// executable.
func lookupPathInsideApp(bin string, paths string, appRootfs string, workDir string) (string, error) {
pathsArr := filepath.SplitList(paths)
var appPathsArr []string
for _, p := range pathsArr {
if !filepath.IsAbs(p) {
p = filepath.Join(workDir, p)
}
appPathsArr = append(appPathsArr, filepath.Join(appRootfs, p))
}
for _, path := range appPathsArr {
binPath := filepath.Join(path, bin)
stage2Path := strings.TrimPrefix(binPath, appRootfs)
binRealPath, err := EvaluateSymlinksInsideApp(appRootfs, stage2Path)
if err != nil {
return "", errwrap.Wrap(fmt.Errorf("could not evaluate path %v", stage2Path), err)
}
binRealPath = filepath.Join(appRootfs, binRealPath)
if fileutil.IsExecutable(binRealPath) {
// The real path is executable, return the path relative to the app
return stage2Path, nil
}
}
return "", fmt.Errorf("unable to find %q in %q", bin, paths)
}
// appSearchPaths returns a list of paths where we should search for
// non-absolute exec binaries
func appSearchPaths(p *stage1commontypes.Pod, workDir string, app types.App) []string {
appEnv := app.Environment
if imgPath, ok := appEnv.Get("PATH"); ok {
return strings.Split(imgPath, ":")
}
// emulate exec(3) behavior, first check working directory and then the
// list of directories returned by confstr(_CS_PATH). That's typically
// "/bin:/usr/bin" so let's use that.
return []string{workDir, "/bin", "/usr/bin"}
}
// FindBinPath takes a binary path and returns a the absolute path of the
// binary relative to the app rootfs. This can be passed to ExecStart on the
// app's systemd service file directly.
func FindBinPath(p *stage1commontypes.Pod, ra *schema.RuntimeApp) (string, error) {
if len(ra.App.Exec) == 0 {
return "", errors.New("app has no executable")
}
bin := ra.App.Exec[0]
var binPath string
switch {
// absolute path, just use it
case filepath.IsAbs(bin):
binPath = bin
// non-absolute path containing a slash, look in the working dir
case strings.Contains(bin, "/"):
binPath = filepath.Join(ra.App.WorkingDirectory, bin)
// filename, search in the app's $PATH
default:
absRoot, err := filepath.Abs(p.Root)
if err != nil {
return "", errwrap.Wrap(errors.New("could not get pod's root absolute path"), err)
}
appRootfs := common.AppRootfsPath(absRoot, ra.Name)
appPathDirs := appSearchPaths(p, ra.App.WorkingDirectory, *ra.App)
appPath := strings.Join(appPathDirs, ":")
binPath, err = lookupPathInsideApp(bin, appPath, appRootfs, ra.App.WorkingDirectory)
if err != nil {
return "", errwrap.Wrap(fmt.Errorf("error looking up %q", bin), err)
}
}
return binPath, nil
}
// generateDeviceAllows generates a DeviceAllow= line for an app.
// To make it work, the path needs to start with "/dev" but the device won't
// exist inside the container. So for a given mount, if the volume is a device
// node, we create a symlink to its target in "/rkt/volumes". Later,
// prepare-app will copy those to "/dev/.rkt/" so that's what we use in the
// DeviceAllow= line.
func generateDeviceAllows(root string, appName types.ACName, mountPoints []types.MountPoint, mounts []Mount, uidRange *user.UidRange) ([]string, error) {
var devAllow []string
rktVolumeLinksPath := filepath.Join(root, "rkt", "volumes")
if err := os.MkdirAll(rktVolumeLinksPath, 0600); err != nil {
return nil, err
}
if err := user.ShiftFiles([]string{rktVolumeLinksPath}, uidRange); err != nil {
return nil, err
}
for _, m := range mounts {
if m.Volume.Kind != "host" {
continue
}
if fileutil.IsDeviceNode(m.Volume.Source) {
mode := "r"
if !m.ReadOnly {
mode += "w"
}
tgt := filepath.Join(common.RelAppRootfsPath(appName), m.Mount.Path)
// the DeviceAllow= line needs the link path in /dev/.rkt/
linkRel := filepath.Join("/dev/.rkt", m.Volume.Name.String())
// the real link should be in /rkt/volumes for now
link := filepath.Join(rktVolumeLinksPath, m.Volume.Name.String())
err := os.Symlink(tgt, link)
// if the link already exists, we don't need to do anything
if err != nil && !os.IsExist(err) {
return nil, err
}
devAllow = append(devAllow, linkRel+" "+mode)
}
}
return devAllow, nil
}
// supportsNotify returns true if in the image manifest appc.io/executor/supports-systemd-notify is set to true
func supportsNotify(p *stage1commontypes.Pod, appName string) bool {
appImg := p.Images[appName]
if appImg == nil {
return false
}
supportNotifyAnnotation, ok := appImg.Annotations.Get("appc.io/executor/supports-systemd-notify")
supportNotify, err := strconv.ParseBool(supportNotifyAnnotation)
if ok && supportNotify && err == nil {
return true
}
return false
}
// ParseUserGroup parses the User and Group fields of an App and returns its
// UID and GID.
// The User and Group fields accept several formats:
// 1. the hardcoded string "root"
// 2. a path
// 3. a number
// 4. a name in reference to /etc/{group,passwd} in the image
// See https://github.com/appc/spec/blob/master/spec/aci.md#image-manifest-schema
func ParseUserGroup(p *stage1commontypes.Pod, ra *schema.RuntimeApp) (int, int, error) {
var uidResolver, gidResolver user.Resolver
var uid, gid int
var err error
root := common.AppRootfsPath(p.Root, ra.Name)
uidResolver, err = user.NumericIDs(ra.App.User)
if err != nil {
uidResolver, err = user.IDsFromStat(root, ra.App.User, &p.UidRange)
}
if err != nil {
uidResolver, err = user.IDsFromEtc(root, ra.App.User, "")
}
if err != nil { // give up
return -1, -1, errwrap.Wrap(fmt.Errorf("invalid user %q", ra.App.User), err)
}
if uid, _, err = uidResolver.IDs(); err != nil {
return -1, -1, errwrap.Wrap(fmt.Errorf("failed to configure user %q", ra.App.User), err)
}
gidResolver, err = user.NumericIDs(ra.App.Group)
if err != nil {
gidResolver, err = user.IDsFromStat(root, ra.App.Group, &p.UidRange)
}
if err != nil {
gidResolver, err = user.IDsFromEtc(root, "", ra.App.Group)
}
if err != nil { // give up
return -1, -1, errwrap.Wrap(fmt.Errorf("invalid group %q", ra.App.Group), err)
}
if _, gid, err = gidResolver.IDs(); err != nil {
// If we can't resolve the GID, it might be an image converted from
// docker.
//
// Docker uses the UID as GID if you only specify the "user". In that
// case, docker2aci sets the group name to the user name because the
// appc spec requires both user and group to be set. This will fail
// because that group name won't be found in /etc/group. Let's detect
// if the image was converted from docker and set the GID to the UID in
// that case.
//
// We only do this if the group in RuntimeApp is the same as the one in
// the image, otherwise we trust that the user knows what they're
// doing.
img := p.Images[ra.Name.String()]
if ConvertedFromDocker(img) && img.App.User == img.App.Group && ra.App.Group == img.App.Group {
gid = uid
} else {
return -1, -1, errwrap.Wrap(fmt.Errorf("failed to configure group %q", ra.App.Group), err)
}
}
return uid, gid, nil
}
// EvaluateSymlinksInsideApp tries to resolve symlinks within the path.
// It returns the actual path relative to the app rootfs for the given path.
// This is needed for absolute symlinks - we are in a different rootfs.
func EvaluateSymlinksInsideApp(appRootfs, path string) (string, error) {
chroot, err := newChroot(appRootfs)
if err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("chroot to %q failed", appRootfs), err)
}
target, err := fileutil.EvalSymlinksAlways(path)
if err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("evaluating symlinks of %q failed", path), err)
}
// EvalSymlinksAlways might return a relative path
abs, err := filepath.Abs(target)
if err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("failed to get absolute representation of %q", target), err)
}
if err := chroot.escape(); err != nil {
return "", errwrap.Wrapf(fmt.Sprintf("escaping chroot %q failed", appRootfs), err)
}
return abs, nil
}
// appToNspawnArgs transforms the given app manifest, with the given associated
// app name, into a subset of applicable systemd-nspawn argument
func appToNspawnArgs(p *stage1commontypes.Pod, ra *schema.RuntimeApp) ([]string, error) {
var args []string
appName := ra.Name
app := ra.App
sharedVolPath, err := common.CreateSharedVolumesPath(p.Root)
if err != nil {
return nil, err
}
vols := make(map[types.ACName]types.Volume)
for _, v := range p.Manifest.Volumes {
vols[v.Name] = v
}
imageManifest := p.Images[appName.String()]
mounts, err := GenerateMounts(ra, p.Manifest.Volumes, ConvertedFromDocker(imageManifest))
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("could not generate app %q mounts", appName), err)
}
for _, m := range mounts {
shPath := filepath.Join(sharedVolPath, m.Volume.Name.String())
absRoot, err := filepath.Abs(p.Root) // Absolute path to the pod's rootfs.
if err != nil {
return nil, errwrap.Wrap(errors.New("could not get pod's root absolute path"), err)
}
appRootfs := common.AppRootfsPath(absRoot, appName)
// Evaluate symlinks within the app's rootfs. This is needed because symlinks
// within the container can be absolute, which will, of course, be wrong in our ns.
// Systemd also gets this wrong, see https://github.com/systemd/systemd/issues/2860
// When the above issue is fixed, we can pass the un-evaluated path to --bind instead.
mntPath, err := EvaluateSymlinksInsideApp(appRootfs, m.Mount.Path)
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("could not evaluate path %v", m.Mount.Path), err)
}
mntAbsPath := filepath.Join(appRootfs, mntPath)
if err := PrepareMountpoints(shPath, mntAbsPath, &m.Volume, m.DockerImplicit); err != nil {
return nil, err
}
opt := make([]string, 6)
if m.ReadOnly {
opt[0] = "--bind-ro="
} else {
opt[0] = "--bind="
}
opt[1] = m.Source(absRoot)
opt[2] = ":"
opt[3] = filepath.Join(common.RelAppRootfsPath(appName), mntPath)
opt[4] = ":"
// If Recursive is not set, default to recursive.
recursive := true
if m.Volume.Recursive != nil {
recursive = *m.Volume.Recursive
}
// rbind/norbind options exist since systemd-nspawn v226
if recursive {
opt[5] = "rbind"
} else {
opt[5] = "norbind"
}
args = append(args, strings.Join(opt, ""))
}
if !p.InsecureOptions.DisableCapabilities {
capabilitiesStr, err := getAppCapabilities(app.Isolators)
if err != nil {
return nil, err
}
capList := strings.Join(capabilitiesStr, ",")
args = append(args, "--capability="+capList)
}
return args, nil
}
// PodToNspawnArgs renders a prepared Pod as a systemd-nspawn
// argument list ready to be executed
func PodToNspawnArgs(p *stage1commontypes.Pod) ([]string, error) {
args := []string{
"--uuid=" + p.UUID.String(),
"--machine=" + GetMachineID(p),
"--directory=" + common.Stage1RootfsPath(p.Root),
}
for i := range p.Manifest.Apps {
aa, err := appToNspawnArgs(p, &p.Manifest.Apps[i])
if err != nil {
return nil, err
}
args = append(args, aa...)
}
if p.InsecureOptions.DisableCapabilities {
args = append(args, "--capability=all")
}
return args, nil
}
// GetFlavor populates a flavor string based on the flavor itself and respectively the systemd version
// If the systemd version couldn't be guessed, it will be set to 0.
func GetFlavor(p *stage1commontypes.Pod) (flavor string, systemdVersion int, err error) {
flavor, err = os.Readlink(filepath.Join(common.Stage1RootfsPath(p.Root), "flavor"))
if err != nil {
return "", -1, errwrap.Wrap(errors.New("unable to determine stage1 flavor"), err)
}
if flavor == "host" {
// This flavor does not contain systemd, parse "systemctl --version"
systemctlBin, err := common.LookupPath("systemctl", os.Getenv("PATH"))
if err != nil {
return "", -1, err
}
systemdVersion, err := common.SystemdVersion(systemctlBin)
if err != nil {
return "", -1, errwrap.Wrap(errors.New("error finding systemctl version"), err)
}
return flavor, systemdVersion, nil
}
systemdVersionBytes, err := ioutil.ReadFile(filepath.Join(common.Stage1RootfsPath(p.Root), "systemd-version"))
if err != nil {
return "", -1, errwrap.Wrap(errors.New("unable to determine stage1's systemd version"), err)
}
systemdVersionString := strings.Trim(string(systemdVersionBytes), " \n")
// systemdVersionString is either a tag name or a branch name. If it's a
// tag name it's of the form "v229", remove the first character to get the
// number.
systemdVersion, err = strconv.Atoi(systemdVersionString[1:])
if err != nil {
// If we get a syntax error, it means the parsing of the version string
// of the form "v229" failed, set it to 0 to indicate we couldn't guess
// it.
if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrSyntax {
systemdVersion = 0
} else {
return "", -1, errwrap.Wrap(errors.New("error parsing stage1's systemd version"), err)
}
}
return flavor, systemdVersion, nil
}
// GetAppHashes returns a list of hashes of the apps in this pod
func GetAppHashes(p *stage1commontypes.Pod) []types.Hash {
var names []types.Hash
for _, a := range p.Manifest.Apps {
names = append(names, a.Image.ID)
}
return names
}
// GetMachineID returns the machine id string of the pod to be passed to
// systemd-nspawn
func GetMachineID(p *stage1commontypes.Pod) string {
return "rkt-" + p.UUID.String()
}
// getAppCapabilities computes the set of Linux capabilities that an app
// should have based on its isolators. Only the following capabalities matter:
// - os/linux/capabilities-retain-set
// - os/linux/capabilities-remove-set
//
// The resulting capabilities are generated following the rules from the spec:
// See: https://github.com/appc/spec/blob/master/spec/ace.md#linux-isolators
func getAppCapabilities(isolators types.Isolators) ([]string, error) {
var capsToRetain []string
var capsToRemove []string
// Default caps defined in
// https://github.com/appc/spec/blob/master/spec/ace.md#linux-isolators
appDefaultCapabilities := []string{
"CAP_AUDIT_WRITE",
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
"CAP_KILL",
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_NET_BIND_SERVICE",
"CAP_SETUID",
"CAP_SETGID",
"CAP_SETPCAP",
"CAP_SETFCAP",
"CAP_SYS_CHROOT",
}
// Iterate over the isolators defined in
// https://github.com/appc/spec/blob/master/spec/ace.md#linux-isolators
// Only read the capababilities isolators:
// - os/linux/capabilities-retain-set
// - os/linux/capabilities-remove-set
for _, isolator := range isolators {
if capSet, ok := isolator.Value().(types.LinuxCapabilitiesSet); ok {
switch isolator.Name {
case types.LinuxCapabilitiesRetainSetName:
capsToRetain = append(capsToRetain, parseLinuxCapabilitiesSet(capSet)...)
case types.LinuxCapabilitiesRevokeSetName:
capsToRemove = append(capsToRemove, parseLinuxCapabilitiesSet(capSet)...)
}
}
}
// appc/spec does not allow to have both the retain set and the remove
// set defined.
if len(capsToRetain) > 0 && len(capsToRemove) > 0 {
return nil, errors.New("cannot have both os/linux/capabilities-retain-set and os/linux/capabilities-remove-set")
}
// Neither the retain set or the remove set are defined
if len(capsToRetain) == 0 && len(capsToRemove) == 0 {
return appDefaultCapabilities, nil
}
if len(capsToRetain) > 0 {
return capsToRetain, nil
}
if len(capsToRemove) == 0 {
panic("len(capsToRetain) is negative. This cannot happen.")
}
caps := appDefaultCapabilities
for _, rc := range capsToRemove {
// backward loop to be safe against deletion
for i := len(caps) - 1; i >= 0; i-- {
if caps[i] == rc {
caps = append(caps[:i], caps[i+1:]...)
}
}
}
return caps, nil
}
// parseLinuxCapabilitySet parses a LinuxCapabilitiesSet into string slice
func parseLinuxCapabilitiesSet(capSet types.LinuxCapabilitiesSet) []string {
var capsStr []string
for _, cap := range capSet.Set() {
capsStr = append(capsStr, string(cap))
}
return capsStr
}
func getAppNoNewPrivileges(isolators types.Isolators) bool {
for _, isolator := range isolators {
noNewPrivileges, ok := isolator.Value().(*types.LinuxNoNewPrivileges)
if ok && bool(*noNewPrivileges) {
return true
}
}
return false
}
// chroot is the struct that represents a chroot environment
type chroot struct {
wd string // the working directory in the outer root
root *os.File // the outer root directory
}
// newChroot creates a new chroot environment for the given path.
// Unless the caller calls Escape() all system operations will be invoked in that environment.
// It stores the working directory at the point it was invoked.
func newChroot(path string) (*chroot, error) {
var err error
var c chroot
c.wd, err = os.Getwd()
if err != nil {
return nil, errwrap.Wrapf("getwd before chroot failed", err)
}
c.root, err = os.Open("/")
if err != nil {
return nil, errwrap.Wrapf("error opening outer root", err)
}
if err := syscall.Chroot(path); err != nil {
return nil, errwrap.Wrapf("chroot to "+path+" failed", err)
}
if err := os.Chdir("/"); err != nil {
return nil, errwrap.Wrapf("chdir to \"/\" failed", err)
}
return &c, nil
}
// Escape escapes the chroot environment changing back to the original working directory where newChroot was invoked.
func (c *chroot) escape() error {
// change directory to outer root and close it
if err := syscall.Fchdir(int(c.root.Fd())); err != nil {
return errwrap.Wrapf("changing directory to outer root failed", err)
}
if err := c.root.Close(); err != nil {
return errwrap.Wrapf("closing outer root failed", err)
}
// chroot to current directory aka "." being the outer root
if err := syscall.Chroot("."); err != nil {
return errwrap.Wrapf("chroot to current directory failed", err)
}
// chdir into previous working directory
if err := os.Chdir(c.wd); err != nil {
return errwrap.Wrapf("chdir to working directory failed", err)
}
return nil
}