forked from canonical/lxd
/
apparmor.go
259 lines (205 loc) · 5.56 KB
/
apparmor.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
package apparmor
import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/lxc/lxd/lxd/state"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/version"
)
const (
cmdLoad = "r"
cmdUnload = "R"
cmdParse = "Q"
)
var aaPath = shared.VarPath("security", "apparmor")
// runApparmor runs the relevant AppArmor command.
func runApparmor(state *state.State, command string, name string) error {
if !state.OS.AppArmorAvailable {
return nil
}
_, err := shared.RunCommand("apparmor_parser", []string{
fmt.Sprintf("-%sWL", command),
filepath.Join(aaPath, "cache"),
filepath.Join(aaPath, "profiles", name),
}...)
if err != nil {
return err
}
return nil
}
// createNamespace creates a new AppArmor namespace.
func createNamespace(state *state.State, name string) error {
if !state.OS.AppArmorAvailable {
return nil
}
if !state.OS.AppArmorStacking || state.OS.AppArmorStacked {
return nil
}
p := filepath.Join("/sys/kernel/security/apparmor/policy/namespaces", name)
err := os.Mkdir(p, 0755)
if err != nil && !os.IsExist(err) {
return err
}
return nil
}
// deleteNamespace destroys an AppArmor namespace.
func deleteNamespace(state *state.State, name string) error {
if !state.OS.AppArmorAvailable {
return nil
}
if !state.OS.AppArmorStacking || state.OS.AppArmorStacked {
return nil
}
p := filepath.Join("/sys/kernel/security/apparmor/policy/namespaces", name)
err := os.Remove(p)
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// hasProfile checks if the profile is already loaded.
func hasProfile(state *state.State, name string) (bool, error) {
mangled := strings.Replace(strings.Replace(strings.Replace(name, "/", ".", -1), "<", "", -1), ">", "", -1)
profilesPath := "/sys/kernel/security/apparmor/policy/profiles"
if shared.PathExists(profilesPath) {
entries, err := ioutil.ReadDir(profilesPath)
if err != nil {
return false, err
}
for _, entry := range entries {
fields := strings.Split(entry.Name(), ".")
if mangled == strings.Join(fields[0:len(fields)-1], ".") {
return true, nil
}
}
}
return false, nil
}
// parseProfile parses the profile without loading it into the kernel.
func parseProfile(state *state.State, name string) error {
if !state.OS.AppArmorAvailable {
return nil
}
return runApparmor(state, cmdParse, name)
}
// loadProfile loads the AppArmor profile into the kernel.
func loadProfile(state *state.State, name string) error {
if !state.OS.AppArmorAdmin {
return nil
}
return runApparmor(state, cmdLoad, name)
}
// unloadProfile removes the profile from the kernel.
func unloadProfile(state *state.State, fullName string, name string) error {
if !state.OS.AppArmorAvailable {
return nil
}
ok, err := hasProfile(state, fullName)
if err != nil {
return err
}
if !ok {
return nil
}
return runApparmor(state, cmdUnload, name)
}
// deleteProfile unloads and delete profile and cache for a profile.
func deleteProfile(state *state.State, fullName string, name string) error {
if !state.OS.AppArmorAdmin {
return nil
}
cacheDir, err := getCacheDir(state)
if err != nil {
return err
}
err = unloadProfile(state, fullName, name)
if err != nil {
return err
}
err = os.Remove(filepath.Join(cacheDir, name))
if err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "Failed to remove: %s", filepath.Join(cacheDir, name))
}
err = os.Remove(filepath.Join(aaPath, "profiles", name))
if err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "Failed to remove: %s", filepath.Join(aaPath, "profiles", name))
}
return nil
}
// parserSupports checks if the parser supports a particular feature.
func parserSupports(state *state.State, feature string) (bool, error) {
if !state.OS.AppArmorAvailable {
return false, nil
}
ver, err := getVersion(state)
if err != nil {
return false, err
}
if feature == "unix" {
minVer, err := version.NewDottedVersion("2.10.95")
if err != nil {
return false, err
}
return ver.Compare(minVer) >= 0, nil
}
return false, nil
}
// getVersion reads and parses the AppArmor version.
func getVersion(state *state.State) (*version.DottedVersion, error) {
if !state.OS.AppArmorAvailable {
return version.NewDottedVersion("0.0")
}
out, err := shared.RunCommand("apparmor_parser", "--version")
if err != nil {
return nil, err
}
fields := strings.Fields(strings.Split(out, "\n")[0])
return version.Parse(fields[len(fields)-1])
}
// getCacheDir returns the applicable AppArmor cache directory.
func getCacheDir(state *state.State) (string, error) {
basePath := filepath.Join(aaPath, "cache")
if !state.OS.AppArmorAvailable {
return basePath, nil
}
ver, err := getVersion(state)
if err != nil {
return "", err
}
// Multiple policy cache directories were only added in v2.13.
minVer, err := version.NewDottedVersion("2.13")
if err != nil {
return "", err
}
if ver.Compare(minVer) < 0 {
return basePath, nil
}
output, err := shared.RunCommand("apparmor_parser", "-L", basePath, "--print-cache-dir")
if err != nil {
return "", err
}
return strings.TrimSpace(output), nil
}
// profileName handles generating valid profile names.
func profileName(prefix string, name string) string {
separators := 1
if len(prefix) > 0 {
separators = 2
}
// Max length in AppArmor is 253 chars.
if len(name)+len(prefix)+3+separators >= 253 {
hash := sha256.New()
io.WriteString(hash, name)
name = fmt.Sprintf("%x", hash.Sum(nil))
}
if len(prefix) > 0 {
return fmt.Sprintf("lxd_%s-%s", prefix, name)
}
return fmt.Sprintf("lxd-%s", name)
}