-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
apparmor.c
265 lines (230 loc) · 6.05 KB
/
apparmor.c
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
/* apparmor
*
* Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
* Copyright © 2012 Canonical Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include "log.h"
#include "lsm.h"
#include "conf.h"
#include "utils.h"
lxc_log_define(lxc_apparmor, lxc);
/* set by lsm_apparmor_drv_init if true */
static int aa_enabled = 0;
static int mount_features_enabled = 0;
#define AA_DEF_PROFILE "lxc-container-default"
#define AA_DEF_PROFILE_CGNS "lxc-container-default-cgns"
#define AA_MOUNT_RESTR "/sys/kernel/security/apparmor/features/mount/mask"
#define AA_ENABLED_FILE "/sys/module/apparmor/parameters/enabled"
#define AA_UNCHANGED "unchanged"
static bool check_mount_feature_enabled(void)
{
return mount_features_enabled == 1;
}
static void load_mount_features_enabled(void)
{
struct stat statbuf;
int ret;
ret = stat(AA_MOUNT_RESTR, &statbuf);
if (ret == 0)
mount_features_enabled = 1;
}
/* aa_getcon is not working right now. Use our hand-rolled version below */
static int apparmor_enabled(void)
{
FILE *fin;
char e;
int ret;
fin = fopen(AA_ENABLED_FILE, "r");
if (!fin)
return 0;
ret = fscanf(fin, "%c", &e);
fclose(fin);
if (ret == 1 && e == 'Y') {
load_mount_features_enabled();
return 1;
}
return 0;
}
static char *apparmor_process_label_get(pid_t pid)
{
char path[100], *space;
int ret;
char *buf = NULL, *newbuf;
int sz = 0;
FILE *f;
ret = snprintf(path, 100, "/proc/%d/attr/current", pid);
if (ret < 0 || ret >= 100) {
ERROR("path name too long");
return NULL;
}
again:
f = fopen(path, "r");
if (!f) {
SYSERROR("opening %s", path);
free(buf);
return NULL;
}
sz += 1024;
newbuf = realloc(buf, sz);
if (!newbuf) {
free(buf);
ERROR("out of memory");
fclose(f);
return NULL;
}
buf = newbuf;
memset(buf, 0, sz);
ret = fread(buf, 1, sz - 1, f);
fclose(f);
if (ret < 0) {
ERROR("reading %s", path);
free(buf);
return NULL;
}
if (ret >= sz)
goto again;
space = strchr(buf, '\n');
if (space)
*space = '\0';
space = strchr(buf, ' ');
if (space)
*space = '\0';
return buf;
}
/*
* Probably makes sense to reorganize these to only read
* the label once
*/
static bool apparmor_am_unconfined(void)
{
char *p = apparmor_process_label_get(lxc_raw_getpid());
bool ret = false;
if (!p || strcmp(p, "unconfined") == 0)
ret = true;
free(p);
return ret;
}
/* aa stacking is not yet supported */
static bool aa_stacking_supported(void) {
return false;
}
static bool aa_needs_transition(char *curlabel)
{
if (!curlabel)
return false;
if (strcmp(curlabel, "unconfined") == 0)
return false;
if (strcmp(curlabel, "/usr/bin/lxc-start") == 0)
return false;
return true;
}
/*
* apparmor_process_label_set: Set AppArmor process profile
*
* @label : the profile to set
* @conf : the container configuration to use @label is NULL
* @default : use the default profile if label is NULL
* @on_exec : this is ignored. Apparmor profile will be changed immediately
*
* Returns 0 on success, < 0 on failure
*
* Notes: This relies on /proc being available.
*/
static int apparmor_process_label_set(const char *inlabel, struct lxc_conf *conf,
bool use_default, bool on_exec)
{
int label_fd, ret;
pid_t tid;
const char *label = inlabel ? inlabel : conf->lsm_aa_profile;
char *curlabel;
if (!aa_enabled)
return 0;
/* user may request that we just ignore apparmor */
if (label && strcmp(label, AA_UNCHANGED) == 0) {
INFO("apparmor profile unchanged per user request");
return 0;
}
curlabel = apparmor_process_label_get(lxc_raw_getpid());
if (!aa_stacking_supported() && aa_needs_transition(curlabel)) {
/* we're already confined, and stacking isn't supported */
if (!label || strcmp(curlabel, label) == 0) {
/* no change requested */
free(curlabel);
return 0;
}
ERROR("already apparmor confined, but new label requested.");
free(curlabel);
return -1;
}
free(curlabel);
if (!label) {
if (use_default) {
if (cgns_supported())
label = AA_DEF_PROFILE_CGNS;
else
label = AA_DEF_PROFILE;
}
else
label = "unconfined";
}
if (!check_mount_feature_enabled() && strcmp(label, "unconfined") != 0) {
WARN("Incomplete AppArmor support in your kernel");
if (!conf->lsm_aa_allow_incomplete) {
ERROR("If you really want to start this container, set");
ERROR("lxc.apparmor.allow_incomplete = 1");
ERROR("in your container configuration file");
return -1;
}
}
if (strcmp(label, "unconfined") == 0 && apparmor_am_unconfined()) {
INFO("apparmor profile unchanged");
return 0;
}
tid = lxc_raw_gettid();
label_fd = lsm_process_label_fd_get(tid, on_exec);
if (label_fd < 0) {
SYSERROR("Failed to change apparmor profile to %s", label);
return -1;
}
ret = lsm_process_label_set_at(label_fd, label, on_exec);
close(label_fd);
if (ret < 0) {
SYSERROR("Failed to change apparmor profile to %s", label);
return -1;
}
INFO("Changed apparmor profile to %s", label);
return 0;
}
static struct lsm_drv apparmor_drv = {
.name = "AppArmor",
.enabled = apparmor_enabled,
.process_label_get = apparmor_process_label_get,
.process_label_set = apparmor_process_label_set,
};
struct lsm_drv *lsm_apparmor_drv_init(void)
{
if (!apparmor_enabled())
return NULL;
aa_enabled = 1;
return &apparmor_drv;
}