This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
app.go
239 lines (209 loc) · 6.3 KB
/
app.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
// Copyright 2016 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"
"os"
"path/filepath"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/common"
"github.com/rkt/rkt/common/cgroup"
stage1commontypes "github.com/rkt/rkt/stage1/common/types"
)
// preparedApp contains some internal state needed to actually run an app.
// We add this intermediate step to prevent unit file generation from being
// totally unwieldy.
type preparedApp struct {
app *schema.RuntimeApp
uid uint32
gid uint32
env types.Environment
resources appResources
mounts []Mount
noNewPrivileges bool
capabilities []string
seccomp *seccompFilter
// Path restrictions
roPaths []string
hiddenPaths []string
hiddenDirs []string
}
type appResources struct {
MemoryLimit *uint64 // Memory limit in bytes
CPUQuota *uint64 // The hard (absolute) CPU quota as a percent (100 = 1 core)
LinuxCPUShares *uint64 // The relative CPU weight in the app's cgroup.
LinuxOOMScoreAdjust *int // OOMScoreAdjust knob
}
/*
* Paths to protect for non-provileged applications
* AKA protectKernelTunables
*/
var protectKernelROPaths = []string{
"/proc/bus/",
"/proc/sys/kernel/core_pattern",
"/proc/sys/kernel/modprobe",
"/proc/sys/vm/panic_on_oom",
"/proc/sysrq-trigger",
"/sys/block/",
"/sys/bus/",
"/sys/class/",
"/sys/dev/",
"/sys/devices/",
"/sys/kernel/",
}
var protectKernelHiddenDirs = []string{
"/sys/firmware/",
"/sys/fs/",
"/sys/hypervisor/",
"/sys/module/",
"/sys/power/",
}
// This is separate because systemd <231 didn't support masking files,
// only directories
var protectKernelHiddenPaths = []string{
"/proc/config.gz",
"/proc/kallsyms",
"/proc/sched_debug",
"/proc/kcore",
"/proc/kmem",
"/proc/mem",
}
// prepareApp sets up the internal runtime context for a specific app.
func prepareApp(p *stage1commontypes.Pod, ra *schema.RuntimeApp) (*preparedApp, error) {
pa := preparedApp{
app: ra,
env: ra.App.Environment,
noNewPrivileges: getAppNoNewPrivileges(ra.App.Isolators),
}
var err error
// Determine numeric uid and gid
u, g, err := ParseUserGroup(p, ra)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to determine app's uid and gid"), err)
}
if u < 0 || g < 0 {
return nil, errors.New("Invalid uid or gid")
}
pa.uid = uint32(u)
pa.gid = uint32(g)
// Set some rkt-provided environment variables
pa.env.Set("AC_APP_NAME", ra.Name.String())
if p.MetadataServiceURL != "" {
pa.env.Set("AC_METADATA_URL", p.MetadataServiceURL)
}
// Determine capability set
pa.capabilities, err = getAppCapabilities(ra.App.Isolators)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to construct capabilities"), err)
}
// Determine mounts
cfd := ConvertedFromDocker(p.Images[ra.Name.String()])
pa.mounts, err = GenerateMounts(ra, p.Manifest.Volumes, cfd)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to compute mounts"), err)
}
// Compute resources
pa.resources, err = computeAppResources(ra.App.Isolators)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to compute resources"), err)
}
// Protect kernel tunables by default
if !p.InsecureOptions.DisablePaths {
pa.roPaths = append(pa.roPaths, protectKernelROPaths...)
pa.hiddenPaths = append(pa.hiddenDirs, protectKernelHiddenPaths...)
pa.hiddenDirs = append(pa.hiddenDirs, protectKernelHiddenDirs...)
}
// Seccomp
if !p.InsecureOptions.DisableSeccomp {
pa.seccomp, err = generateSeccompFilter(p, &pa)
if err != nil {
return nil, err
}
if pa.seccomp != nil && pa.seccomp.forceNoNewPrivileges {
pa.noNewPrivileges = true
}
}
// Write the systemd-sysusers config file
if err := generateSysusers(p, pa.app, int(pa.uid), int(pa.gid), &p.UidRange); err != nil {
return nil, errwrap.Wrapf("unable to generate sysusers file", err)
}
return &pa, nil
}
// computeAppResources processes any isolators that manipulate cgroups.
func computeAppResources(isolators types.Isolators) (appResources, error) {
res := appResources{}
var err error
withIsolator := func(name string, f func() error) error {
ok, err := cgroup.IsIsolatorSupported(name)
if err != nil {
return errwrap.Wrapf("could not check for isolator "+name, err)
}
if !ok {
fmt.Fprintf(os.Stderr, "warning: resource/%s isolator set but support disabled in the kernel, skipping\n", name)
return nil
}
return f()
}
for _, isolator := range isolators {
if err != nil {
return res, err
}
switch v := isolator.Value().(type) {
case *types.ResourceMemory:
err = withIsolator("memory", func() error {
if v.Limit() == nil {
return nil
}
val := uint64(v.Limit().Value())
res.MemoryLimit = &val
return nil
})
case *types.ResourceCPU:
err = withIsolator("cpu", func() error {
if v.Limit() == nil {
return nil
}
if v.Limit().Value() > MaxMilliValue {
return fmt.Errorf("cpu limit exceeds the maximum millivalue: %v", v.Limit().String())
}
val := uint64(v.Limit().MilliValue() / 10)
res.CPUQuota = &val
return nil
})
case *types.LinuxCPUShares:
err = withIsolator("cpu", func() error {
val := uint64(*v)
res.LinuxCPUShares = &val
return nil
})
case *types.LinuxOOMScoreAdj:
val := int(*v)
res.LinuxOOMScoreAdjust = &val
}
}
return res, err
}
// relAppPaths prepends the relative app path (/opt/stage1/rootfs/) to a list
// of paths. Useful for systemd unit directives.
func (pa *preparedApp) relAppPaths(paths []string) []string {
out := make([]string, 0, len(paths))
for _, p := range paths {
out = append(out, filepath.Join(common.RelAppRootfsPath(pa.app.Name), p))
}
return out
}