forked from checkpoint-restore/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsm.c
251 lines (206 loc) · 4.79 KB
/
lsm.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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "config.h"
#include "pstree.h"
#include "util.h"
#include "cr_options.h"
#include "protobuf.h"
#include "protobuf/inventory.pb-c.h"
#include "protobuf/creds.pb-c.h"
#undef CONFIG_HAS_SELINUX
#ifdef CONFIG_HAS_SELINUX
#include <selinux/selinux.h>
#endif
static Lsmtype lsmtype;
static int (*get_label)(pid_t, char **) = NULL;
static char *name = NULL;
static int apparmor_get_label(pid_t pid, char **profile_name)
{
FILE *f;
char *space;
f = fopen_proc(pid, "attr/current");
if (!f)
return -1;
if (fscanf(f, "%ms", profile_name) != 1) {
fclose(f);
pr_perror("err scanfing");
return -1;
}
fclose(f);
/*
* A profile name can be followed by an enforcement mode, e.g.
* lxc-default-with-nesting (enforced)
* but the profile name is just the part before the space.
*/
space = strstr(*profile_name, " ");
if (space)
*space = 0;
/*
* An "unconfined" value means there is no profile, so we don't need to
* worry about trying to restore one.
*/
if (strcmp(*profile_name, "unconfined") == 0) {
free(*profile_name);
*profile_name = NULL;
}
return 0;
}
#ifdef CONFIG_HAS_SELINUX
static int selinux_get_label(pid_t pid, char **output)
{
security_context_t ctx;
char *pos, *last;
int i;
if (getpidcon_raw(pid, &ctx) < 0) {
pr_perror("getting selinux profile failed");
return -1;
}
*output = NULL;
/*
* Since SELinux attributes can be finer grained than at the task
* level, and we currently don't try to dump any of these other bits,
* let's only allow unconfined profiles, which look something like:
*
* unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
*/
pos = (char*)ctx;
for (i = 0; i < 3; i++) {
last = pos;
pos = strstr(pos, ":");
if (!pos) {
pr_err("Invalid selinux context %s\n", (char *)ctx);
freecon(ctx);
return -1;
}
*pos = 0;
if (!strstartswith(last, "unconfined_")) {
pr_err("Non unconfined selinux contexts not supported %s\n", last);
freecon(ctx);
return -1;
}
pos++;
}
freecon(ctx);
return 0;
}
#endif
void kerndat_lsm(void)
{
/* On restore, if someone passes --lsm-profile, we might end up doing
* detection twice, once during flag parsing and once for
* kerndat_init_rst(). Let's detect when we've already done detection
* and not do it again.
*/
if (name)
return;
if (access("/sys/kernel/security/apparmor", F_OK) == 0) {
get_label = apparmor_get_label;
lsmtype = LSMTYPE__APPARMOR;
name = "apparmor";
return;
}
#ifdef CONFIG_HAS_SELINUX
/*
* This seems to be the canonical place to mount this fs if it is
* enabled, although we may (?) want to check /selinux for posterity as
* well.
*/
if (access("/sys/fs/selinux", F_OK) == 0) {
get_label = selinux_get_label;
lsmtype = LSMTYPE__SELINUX;
name = "selinux";
return;
}
#endif
get_label = NULL;
lsmtype = LSMTYPE__NO_LSM;
name = "none";
}
Lsmtype host_lsm_type(void)
{
return lsmtype;
}
int collect_lsm_profile(pid_t pid, CredsEntry *ce)
{
ce->lsm_profile = NULL;
if (lsmtype == LSMTYPE__NO_LSM)
return 0;
if (get_label(pid, &ce->lsm_profile) < 0)
return -1;
if (ce->lsm_profile)
pr_info("%d has lsm profile %s\n", pid, ce->lsm_profile);
return 0;
}
// in inventory.c
extern Lsmtype image_lsm;
int validate_lsm(char *lsm_profile)
{
if (image_lsm == LSMTYPE__NO_LSM || image_lsm == lsmtype)
return 0;
/*
* This is really only a problem if the processes have actually
* specified an LSM profile. If not, we won't restore anything anyway,
* so it's fine.
*/
if (lsm_profile) {
pr_err("mismatched lsm types and lsm profile specified\n");
return -1;
}
return 0;
}
int render_lsm_profile(char *profile, char **val)
{
*val = NULL;
switch (lsmtype) {
case LSMTYPE__APPARMOR:
if (strcmp(profile, "unconfined") != 0 && asprintf(val, "changeprofile %s", profile) < 0) {
*val = NULL;
return -1;
}
break;
case LSMTYPE__SELINUX:
if (asprintf(val, "%s", profile) < 0) {
*val = NULL;
return -1;
}
break;
default:
return -1;
}
return 0;
}
int parse_lsm_arg(char *arg)
{
char *aux;
kerndat_lsm();
aux = strchr(arg, ':');
if (aux == NULL) {
pr_err("invalid argument %s for --lsm-profile", arg);
return -1;
}
*aux = '\0';
aux++;
if (strcmp(arg, "apparmor") == 0) {
if (lsmtype != LSMTYPE__APPARMOR) {
pr_err("apparmor LSM specified but apparmor not supported by kernel\n");
return -1;
}
opts.lsm_profile = aux;
} else if (strcmp(arg, "selinux") == 0) {
if (lsmtype != LSMTYPE__SELINUX) {
pr_err("selinux LSM specified but selinux not supported by kernel\n");
return -1;
}
opts.lsm_profile = aux;
} else if (strcmp(arg, "none") == 0) {
opts.lsm_profile = NULL;
} else {
pr_err("unknown lsm %s\n", arg);
return -1;
}
opts.lsm_supplied = true;
return 0;
}