/
instance_forkproxy.go
237 lines (203 loc) · 6.63 KB
/
instance_forkproxy.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
package apparmor
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
deviceConfig "github.com/lxc/lxd/lxd/device/config"
"github.com/lxc/lxd/lxd/project"
"github.com/lxc/lxd/lxd/sys"
"github.com/lxc/lxd/lxd/util"
"github.com/lxc/lxd/shared"
)
// Internal copy of the device interface.
type device interface {
Config() deviceConfig.Device
Name() string
}
var forkproxyProfileTpl = template.Must(template.New("forkproxyProfile").Parse(`#include <tunables/global>
profile "{{ .name }}" flags=(attach_disconnected,mediate_deleted) {
#include <abstractions/base>
# Capabilities
capability chown,
capability dac_read_search,
capability dac_override,
capability fowner,
capability fsetid,
capability kill,
capability net_bind_service,
capability setgid,
capability setuid,
capability sys_admin,
capability sys_chroot,
capability sys_ptrace,
# Network access
network inet dgram,
network inet6 dgram,
network inet stream,
network inet6 stream,
network unix stream,
# Forkproxy operation
{{ .logPath }}/** rw,
@{PROC}/** rw,
/ rw,
ptrace (read),
ptrace (trace),
/etc/machine-id r,
{{ .rootPath }}/run/systemd/resolve/stub-resolv.conf r,
{{ .rootPath }}/run/{resolvconf,NetworkManager,systemd/resolve,connman,netconfig}/resolv.conf r,
{{ .rootPath }}/usr/lib/systemd/resolv.conf r,
# Needed for lxd fork commands
{{ .exePath }} mr,
@{PROC}/@{pid}/cmdline r,
{{ .rootPath }}/{etc,lib,usr/lib}/os-release r,
{{if .sockets -}}
{{range $index, $element := .sockets}}
{{$element}} rw,
{{- end }}
{{- end }}
# Things that we definitely don't need
deny @{PROC}/@{pid}/cgroup r,
deny /sys/module/apparmor/parameters/enabled r,
deny /sys/kernel/mm/transparent_hugepage/hpage_pmd_size r,
deny /sys/devices/virtual/dmi/id/product_uuid r,
{{- if .snap }}
# The binary itself (for nesting)
/var/snap/lxd/common/lxd.debug mr,
/snap/lxd/*/bin/lxd mr,
# Snap-specific libraries
/snap/lxd/*/lib/**.so* mr,
{{- end }}
{{if .libraryPath }}
# Entries from LD_LIBRARY_PATH
{{range $index, $element := .libraryPath}}
{{$element}}/** mr,
{{- end }}
{{- end }}
}
`))
// forkproxyProfile generates the AppArmor profile template from the given network.
func forkproxyProfile(sysOS *sys.OS, inst instance, dev device) (string, error) {
rootPath := ""
if shared.InSnap() {
rootPath = "/var/lib/snapd/hostfs"
}
// Add any socket used by forkproxy.
sockets := []string{}
fields := strings.SplitN(dev.Config()["listen"], ":", 2)
if fields[0] == "unix" && !strings.HasPrefix(fields[1], "@") {
if dev.Config()["bind"] == "host" || dev.Config()["bind"] == "" {
hostPath := shared.HostPath(fields[1])
sockets = append(sockets, hostPath)
if hostPath != fields[1] {
// AppArmor can get confused on Ubuntu Core so allow both paths.
sockets = append(sockets, fields[1])
}
} else {
sockets = append(sockets, fields[1])
}
}
fields = strings.SplitN(dev.Config()["connect"], ":", 2)
if fields[0] == "unix" && !strings.HasPrefix(fields[1], "@") {
if dev.Config()["bind"] == "host" || dev.Config()["bind"] == "" {
sockets = append(sockets, fields[1])
} else {
hostPath := shared.HostPath(fields[1])
sockets = append(sockets, hostPath)
if hostPath != fields[1] {
// AppArmor can get confused on Ubuntu Core so allow both paths.
sockets = append(sockets, fields[1])
}
}
}
// AppArmor requires deref of all paths.
for k := range sockets {
// Skip non-existing because of the additional entry for the host side.
if !shared.PathExists(sockets[k]) {
continue
}
v, err := filepath.EvalSymlinks(sockets[k])
if err != nil {
return "", err
}
if !shared.StringInSlice(v, sockets) {
sockets = append(sockets, v)
}
}
execPath := util.GetExecPath()
execPathFull, err := filepath.EvalSymlinks(execPath)
if err == nil {
execPath = execPathFull
}
// Render the profile.
var sb *strings.Builder = &strings.Builder{}
err = forkproxyProfileTpl.Execute(sb, map[string]any{
"name": ForkproxyProfileName(inst, dev),
"varPath": shared.VarPath(""),
"rootPath": rootPath,
"snap": shared.InSnap(),
"exePath": execPath,
"logPath": inst.LogPath(),
"libraryPath": strings.Split(os.Getenv("LD_LIBRARY_PATH"), ":"),
"sockets": sockets,
})
if err != nil {
return "", err
}
return sb.String(), nil
}
// ForkproxyProfileName returns the AppArmor profile name.
func ForkproxyProfileName(inst instance, dev device) string {
path := shared.VarPath("")
name := fmt.Sprintf("%s_%s_<%s>", dev.Name(), project.Instance(inst.Project().Name, inst.Name()), path)
return profileName("forkproxy", name)
}
// forkproxyProfileFilename returns the name of the on-disk profile name.
func forkproxyProfileFilename(inst instance, dev device) string {
name := fmt.Sprintf("%s_%s", dev.Name(), project.Instance(inst.Project().Name, inst.Name()))
return profileName("forkproxy", name)
}
// ForkproxyLoad ensures that the instances's policy is loaded into the kernel so the it can boot.
func ForkproxyLoad(sysOS *sys.OS, inst instance, dev device) error {
/* In order to avoid forcing a profile parse (potentially slow) on
* every container start, let's use AppArmor's binary policy cache,
* which checks mtime of the files to figure out if the policy needs to
* be regenerated.
*
* Since it uses mtimes, we shouldn't just always write out our local
* AppArmor template; instead we should check to see whether the
* template is the same as ours. If it isn't we should write our
* version out so that the new changes are reflected and we definitely
* force a recompile.
*/
profile := filepath.Join(aaPath, "profiles", forkproxyProfileFilename(inst, dev))
content, err := os.ReadFile(profile)
if err != nil && !os.IsNotExist(err) {
return err
}
updated, err := forkproxyProfile(sysOS, inst, dev)
if err != nil {
return err
}
if string(content) != string(updated) {
err = os.WriteFile(profile, []byte(updated), 0600)
if err != nil {
return err
}
}
err = loadProfile(sysOS, forkproxyProfileFilename(inst, dev))
if err != nil {
return err
}
return nil
}
// ForkproxyUnload ensures that the instances's policy namespace is unloaded to free kernel memory.
// This does not delete the policy from disk or cache.
func ForkproxyUnload(sysOS *sys.OS, inst instance, dev device) error {
return unloadProfile(sysOS, ForkproxyProfileName(inst, dev), forkproxyProfileFilename(inst, dev))
}
// ForkproxyDelete removes the policy from cache/disk.
func ForkproxyDelete(sysOS *sys.OS, inst instance, dev device) error {
return deleteProfile(sysOS, ForkproxyProfileName(inst, dev), forkproxyProfileFilename(inst, dev))
}