-
Notifications
You must be signed in to change notification settings - Fork 344
/
get_context_list.c
499 lines (433 loc) · 10.1 KB
/
get_context_list.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pwd.h>
#include "selinux_internal.h"
#include "callbacks.h"
#include "context_internal.h"
#include "get_context_list_internal.h"
int get_default_context_with_role(const char *user,
const char *role,
const char *fromcon,
char ** newcon)
{
char **conary;
char **ptr;
context_t con;
const char *role2;
int rc;
rc = get_ordered_context_list(user, fromcon, &conary);
if (rc <= 0)
return -1;
for (ptr = conary; *ptr; ptr++) {
con = context_new(*ptr);
if (!con)
continue;
role2 = context_role_get(con);
if (role2 && !strcmp(role, role2)) {
context_free(con);
break;
}
context_free(con);
}
rc = -1;
if (!(*ptr)) {
errno = EINVAL;
goto out;
}
*newcon = strdup(*ptr);
if (!(*newcon))
goto out;
rc = 0;
out:
freeconary(conary);
return rc;
}
int get_default_context_with_rolelevel(const char *user,
const char *role,
const char *level,
const char *fromcon,
char ** newcon)
{
int rc;
char *backup_fromcon = NULL;
context_t con;
const char *newfromcon;
if (!level)
return get_default_context_with_role(user, role, fromcon,
newcon);
if (!fromcon) {
rc = getcon(&backup_fromcon);
if (rc < 0)
return rc;
fromcon = backup_fromcon;
}
rc = -1;
con = context_new(fromcon);
if (!con)
goto out;
if (context_range_set(con, level))
goto out;
newfromcon = context_str(con);
if (!newfromcon)
goto out;
rc = get_default_context_with_role(user, role, newfromcon, newcon);
out:
context_free(con);
freecon(backup_fromcon);
return rc;
}
int get_default_context(const char *user,
const char *fromcon, char ** newcon)
{
char **conary;
int rc;
rc = get_ordered_context_list(user, fromcon, &conary);
if (rc <= 0)
return -1;
*newcon = strdup(conary[0]);
freeconary(conary);
if (!(*newcon))
return -1;
return 0;
}
static int is_in_reachable(char **reachable, const char *usercon_str)
{
if (!reachable)
return 0;
for (; *reachable != NULL; reachable++) {
if (strcmp(*reachable, usercon_str) == 0) {
return 1;
}
}
return 0;
}
static int get_context_user(FILE * fp,
context_t fromcon,
const char * user,
char ***reachable,
unsigned int *nreachable)
{
char *start, *end = NULL;
char *line = NULL;
size_t line_len = 0, usercon_len;
size_t user_len = strlen(user);
ssize_t len;
int found = 0;
const char *fromrole, *fromtype, *fromlevel;
char *linerole, *linetype;
char **new_reachable = NULL;
char *usercon_str;
const char *usercon_str2;
context_t usercon;
int rc;
errno = EINVAL;
/* Extract the role and type of the fromcon for matching.
User identity and MLS range can be variable. */
fromrole = context_role_get(fromcon);
fromtype = context_type_get(fromcon);
fromlevel = context_range_get(fromcon);
if (!fromrole || !fromtype) {
return -1;
}
while ((len = getline(&line, &line_len, fp)) > 0) {
if (line[len - 1] == '\n')
line[len - 1] = 0;
/* Skip leading whitespace. */
start = line;
while (*start && isspace((unsigned char)*start))
start++;
if (!(*start))
continue;
/* Find the end of the (partial) fromcon in the line. */
end = start;
while (*end && !isspace((unsigned char)*end))
end++;
if (!(*end))
continue;
/* Check for a match. */
linerole = start;
while (*start && !isspace((unsigned char)*start) && *start != ':')
start++;
if (*start != ':')
continue;
*start = 0;
linetype = ++start;
while (*start && !isspace((unsigned char)*start) && *start != ':')
start++;
if (!(*start))
continue;
*start = 0;
if (!strcmp(fromrole, linerole) && !strcmp(fromtype, linetype)) {
found = 1;
break;
}
}
if (!found) {
errno = ENOENT;
rc = -1;
goto out;
}
start = ++end;
while (*start) {
/* Skip leading whitespace */
while (*start && isspace((unsigned char)*start))
start++;
if (!(*start))
break;
/* Find the end of this partial context. */
end = start;
while (*end && !isspace((unsigned char)*end))
end++;
if (*end)
*end++ = 0;
/* Check whether a new context is valid */
if (SIZE_MAX - user_len < strlen(start) + 2) {
selinux_log(SELINUX_ERROR, "%s: one of partial contexts is too big\n", __FUNCTION__);
errno = EINVAL;
rc = -1;
goto out;
}
usercon_len = user_len + strlen(start) + 2;
usercon_str = malloc(usercon_len);
if (!usercon_str) {
rc = -1;
goto out;
}
/* set range from fromcon in the new usercon */
snprintf(usercon_str, usercon_len, "%s:%s", user, start);
usercon = context_new(usercon_str);
if (!usercon) {
if (errno != EINVAL) {
free(usercon_str);
rc = -1;
goto out;
}
selinux_log(SELINUX_ERROR,
"%s: can't create a context from %s, skipping\n",
__FUNCTION__, usercon_str);
free(usercon_str);
start = end;
continue;
}
free(usercon_str);
if (context_range_set(usercon, fromlevel) != 0) {
context_free(usercon);
rc = -1;
goto out;
}
usercon_str2 = context_str(usercon);
if (!usercon_str2) {
context_free(usercon);
rc = -1;
goto out;
}
/* check whether usercon is already in reachable */
if (is_in_reachable(*reachable, usercon_str2)) {
context_free(usercon);
start = end;
continue;
}
if (security_check_context(usercon_str2) == 0) {
new_reachable = reallocarray(*reachable, *nreachable + 2, sizeof(char *));
if (!new_reachable) {
context_free(usercon);
rc = -1;
goto out;
}
*reachable = new_reachable;
new_reachable[*nreachable] = strdup(usercon_str2);
if (new_reachable[*nreachable] == NULL) {
context_free(usercon);
rc = -1;
goto out;
}
new_reachable[*nreachable + 1] = 0;
*nreachable += 1;
}
context_free(usercon);
start = end;
}
rc = 0;
out:
free(line);
return rc;
}
static int get_failsafe_context(const char *user, char ** newcon)
{
FILE *fp;
char buf[255], *ptr;
size_t plen, nlen;
int rc;
fp = fopen(selinux_failsafe_context_path(), "re");
if (!fp)
return -1;
ptr = fgets_unlocked(buf, sizeof buf, fp);
fclose(fp);
if (!ptr)
return -1;
plen = strlen(ptr);
if (buf[plen - 1] == '\n')
buf[plen - 1] = 0;
nlen = strlen(user) + 1 + plen + 1;
*newcon = malloc(nlen);
if (!(*newcon))
return -1;
rc = snprintf(*newcon, nlen, "%s:%s", user, ptr);
if (rc < 0 || (size_t) rc >= nlen) {
free(*newcon);
*newcon = 0;
return -1;
}
/* If possible, check the context to catch
errors early rather than waiting until the
caller tries to use setexeccon on the context.
But this may not always be possible, e.g. if
selinuxfs isn't mounted. */
if (security_check_context(*newcon) && errno != ENOENT) {
free(*newcon);
*newcon = 0;
return -1;
}
return 0;
}
int get_ordered_context_list_with_level(const char *user,
const char *level,
const char *fromcon,
char *** list)
{
int rc;
char *backup_fromcon = NULL;
context_t con;
const char *newfromcon;
if (!level)
return get_ordered_context_list(user, fromcon, list);
if (!fromcon) {
rc = getcon(&backup_fromcon);
if (rc < 0)
return rc;
fromcon = backup_fromcon;
}
rc = -1;
con = context_new(fromcon);
if (!con)
goto out;
if (context_range_set(con, level))
goto out;
newfromcon = context_str(con);
if (!newfromcon)
goto out;
rc = get_ordered_context_list(user, newfromcon, list);
out:
context_free(con);
freecon(backup_fromcon);
return rc;
}
int get_default_context_with_level(const char *user,
const char *level,
const char *fromcon,
char ** newcon)
{
char **conary;
int rc;
rc = get_ordered_context_list_with_level(user, level, fromcon, &conary);
if (rc <= 0)
return -1;
*newcon = strdup(conary[0]);
freeconary(conary);
if (!(*newcon))
return -1;
return 0;
}
int get_ordered_context_list(const char *user,
const char *fromcon,
char *** list)
{
char **reachable = NULL;
int rc = 0;
unsigned nreachable = 0;
char *backup_fromcon = NULL;
FILE *fp;
char *fname = NULL;
size_t fname_len;
const char *user_contexts_path = selinux_user_contexts_path();
context_t con = NULL;
if (!fromcon) {
/* Get the current context and use it for the starting context */
rc = getcon(&backup_fromcon);
if (rc < 0)
return rc;
fromcon = backup_fromcon;
}
con = context_new(fromcon);
if (!con)
goto failsafe;
/* Determine the ordering to apply from the optional per-user config
and from the global config. */
fname_len = strlen(user_contexts_path) + strlen(user) + 2;
fname = malloc(fname_len);
if (!fname)
goto failsafe;
snprintf(fname, fname_len, "%s%s", user_contexts_path, user);
fp = fopen(fname, "re");
if (fp) {
__fsetlocking(fp, FSETLOCKING_BYCALLER);
rc = get_context_user(fp, con, user, &reachable, &nreachable);
fclose(fp);
if (rc < 0 && errno != ENOENT) {
selinux_log(SELINUX_ERROR,
"%s: error in processing configuration file %s\n",
__FUNCTION__, fname);
/* Fall through, try global config */
}
}
free(fname);
fp = fopen(selinux_default_context_path(), "re");
if (fp) {
__fsetlocking(fp, FSETLOCKING_BYCALLER);
rc = get_context_user(fp, con, user, &reachable, &nreachable);
fclose(fp);
if (rc < 0 && errno != ENOENT) {
selinux_log(SELINUX_ERROR,
"%s: error in processing configuration file %s\n",
__FUNCTION__, selinux_default_context_path());
/* Fall through */
}
}
if (!nreachable)
goto failsafe;
out:
if (nreachable > 0) {
*list = reachable;
rc = nreachable;
}
else
freeconary(reachable);
context_free(con);
freecon(backup_fromcon);
return rc;
failsafe:
/* Unable to determine a reachable context list, try to fall back to
the "failsafe" context to at least permit root login
for emergency recovery if possible. */
freeconary(reachable);
reachable = malloc(2 * sizeof(char *));
if (!reachable) {
rc = -1;
goto out;
}
reachable[0] = reachable[1] = 0;
rc = get_failsafe_context(user, &reachable[0]);
if (rc < 0) {
freeconary(reachable);
reachable = NULL;
goto out;
}
nreachable = 1; /* one context in the list */
goto out;
}