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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
/*
* String representation support for classes and permissions.
*/
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <selinux/flask.h>
#include <selinux/av_permissions.h>
#include "selinux_internal.h"
#include "policy.h"
#include "mapping.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/* The following code looks complicated, but it really is not. What it
does is to generate two variables. The first is basically a struct
of arrays. The second is the real array of structures which would
have used string pointers. But instead it now uses an offset value
into the first structure. Strings are accessed indirectly by an
explicit addition of the string index and the base address of the
structure with the strings (all type safe). The advantage is that
there are no relocations necessary in the array with the data as it
would be the case with string pointers. This has advantages at
load time, the data section is smaller, and it is read-only. */
#define L1(line) L2(line)
#define L2(line) str##line
static const union av_perm_to_string_data {
struct {
#define S_(c, v, s) char L1(__LINE__)[sizeof(s)];
#include "av_perm_to_string.h"
#undef S_
};
char str[0];
} av_perm_to_string_data = {
{
#define S_(c, v, s) s,
#include "av_perm_to_string.h"
#undef S_
}
};
static const struct av_perm_to_string {
uint16_t tclass;
uint16_t nameidx;
uint32_t value;
} av_perm_to_string[] = {
#define S_(c, v, s) { c, offsetof(union av_perm_to_string_data, L1(__LINE__)), v },
#include "av_perm_to_string.h"
#undef S_
};
#undef L1
#undef L2
#define L1(line) L2(line)
#define L2(line) str##line
static const union class_to_string_data {
struct {
#define S_(s) char L1(__LINE__)[sizeof(s)];
#include "class_to_string.h"
#undef S_
};
char str[0];
} class_to_string_data = {
{
#define S_(s) s,
#include "class_to_string.h"
#undef S_
}
};
static const uint16_t class_to_string[] = {
#define S_(s) offsetof(union class_to_string_data, L1(__LINE__)),
#include "class_to_string.h"
#undef S_
};
#undef L1
#undef L2
static const union common_perm_to_string_data {
struct {
#define L1(line) L2(line)
#define L2(line) str##line
#define S_(s) char L1(__LINE__)[sizeof(s)];
#define TB_(s)
#define TE_(s)
#include "common_perm_to_string.h"
#undef S_
#undef L1
#undef L2
};
char str[0];
} common_perm_to_string_data = {
{
#define S_(s) s,
#include "common_perm_to_string.h"
#undef S_
#undef TB_
#undef TE_
}
};
static const union common_perm_to_string {
struct {
#define TB_(s) struct {
#define TE_(s) } s##_part;
#define S_(s) uint16_t L1(__LINE__)
#define L1(l) L2(l)
#define L2(l) field_##l;
#include "common_perm_to_string.h"
#undef TB_
#undef TE_
#undef S_
#undef L1
#undef L2
};
uint16_t data[0];
} common_perm_to_string = {
{
#define TB_(s) {
#define TE_(s) },
#define S_(s) offsetof(union common_perm_to_string_data, L1(__LINE__)),
#define L1(line) L2(line)
#define L2(line) str##line
#include "common_perm_to_string.h"
#undef TB_
#undef TE_
#undef S_
#undef L1
#undef L2
}
};
static const struct av_inherit {
uint16_t tclass;
uint16_t common_pts_idx;
uint32_t common_base;
} av_inherit[] = {
#define S_(c, i, b) { c, offsetof(union common_perm_to_string, common_##i##_perm_to_string_part)/sizeof(uint16_t), b },
#include "av_inherit.h"
#undef S_
};
#define NCLASSES ARRAY_SIZE(class_to_string)
#define NVECTORS ARRAY_SIZE(av_perm_to_string)
#define MAXVECTORS 8*sizeof(access_vector_t)
static pthread_once_t once = PTHREAD_ONCE_INIT;
static int obj_class_compat;
static void init_obj_class_compat(void)
{
char path[PATH_MAX];
struct stat s;
if (!selinux_mnt)
return;
snprintf(path,PATH_MAX,"%s/class",selinux_mnt);
if (stat(path,&s) < 0)
return;
if (S_ISDIR(s.st_mode))
obj_class_compat = 0;
}
struct discover_class_node {
char *name;
security_class_t value;
char **perms;
struct discover_class_node *next;
};
static struct discover_class_node *discover_class_cache = NULL;
static struct discover_class_node * get_class_cache_entry_name(const char *s)
{
struct discover_class_node *node = discover_class_cache;
for (; node != NULL && strcmp(s,node->name) != 0; node = node->next);
return node;
}
static struct discover_class_node * get_class_cache_entry_value(security_class_t c)
{
struct discover_class_node *node = discover_class_cache;
for (; node != NULL && c != node->value; node = node->next);
return node;
}
static struct discover_class_node * discover_class(const char *s)
{
int fd, ret;
char path[PATH_MAX];
char buf[20];
DIR *dir;
struct dirent *dentry;
size_t i;
struct discover_class_node *node;
if (!selinux_mnt) {
errno = ENOENT;
return NULL;
}
/* allocate a node */
node = malloc(sizeof(struct discover_class_node));
if (node == NULL)
return NULL;
/* allocate array for perms */
node->perms = calloc(NVECTORS,sizeof(char*));
if (node->perms == NULL)
goto err1;
/* load up the name */
node->name = strdup(s);
if (node->name == NULL)
goto err2;
/* load up class index */
snprintf(path, sizeof path, "%s/class/%s/index", selinux_mnt,s);
fd = open(path, O_RDONLY);
if (fd < 0)
goto err3;
memset(buf, 0, sizeof(buf));
ret = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (ret < 0)
goto err3;
if (sscanf(buf, "%hu", &node->value) != 1)
goto err3;
/* load up permission indicies */
snprintf(path, sizeof path, "%s/class/%s/perms",selinux_mnt,s);
dir = opendir(path);
if (dir == NULL)
goto err3;
dentry = readdir(dir);
while (dentry != NULL) {
unsigned int value;
struct stat m;
snprintf(path, sizeof path, "%s/class/%s/perms/%s", selinux_mnt,s,dentry->d_name);
if (stat(path,&m) < 0)
goto err4;
if (m.st_mode & S_IFDIR) {
dentry = readdir(dir);
continue;
}
fd = open(path, O_RDONLY);
if (fd < 0)
goto err4;
memset(buf, 0, sizeof(buf));
ret = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (ret < 0)
goto err4;
if (sscanf(buf, "%u", &value) != 1)
goto err4;
node->perms[value-1] = strdup(dentry->d_name);
if (node->perms[value-1] == NULL)
goto err4;
dentry = readdir(dir);
}
closedir(dir);
node->next = discover_class_cache;
discover_class_cache = node;
return node;
err4:
closedir(dir);
for (i=0; i<NVECTORS; i++)
free(node->perms[i]);
err3:
free(node->name);
err2:
free(node->perms);
err1:
free(node);
return NULL;
}
static security_class_t string_to_security_class_compat(const char *s)
{
unsigned int val;
if (isdigit(s[0])) {
val = atoi(s);
if (val > 0 && val < NCLASSES)
return map_class(val);
} else {
for (val = 0; val < NCLASSES; val++) {
if (strcmp(s, (class_to_string_data.str
+ class_to_string[val])) == 0)
return map_class(val);
}
}
errno = EINVAL;
return 0;
}
static access_vector_t string_to_av_perm_compat(security_class_t kclass, const char *s)
{
const uint16_t *common_pts_idx = 0;
access_vector_t perm, common_base = 0;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(av_inherit); i++) {
if (av_inherit[i].tclass == kclass) {
common_pts_idx =
&common_perm_to_string.data[av_inherit[i].
common_pts_idx];
common_base = av_inherit[i].common_base;
break;
}
}
i = 0;
perm = 1;
while (perm < common_base) {
if (strcmp
(s,
common_perm_to_string_data.str + common_pts_idx[i]) == 0)
return perm;
perm <<= 1;
i++;
}
for (i = 0; i < NVECTORS; i++) {
if ((av_perm_to_string[i].tclass == kclass) &&
(strcmp(s, (av_perm_to_string_data.str
+ av_perm_to_string[i].nameidx)) == 0))
return av_perm_to_string[i].value;
}
errno = EINVAL;
return 0;
}
static const char *security_class_to_string_compat(security_class_t tclass)
{
if (tclass > 0 && tclass < NCLASSES)
return class_to_string_data.str + class_to_string[tclass];
errno = EINVAL;
return NULL;
}
static const char *security_av_perm_to_string_compat(security_class_t tclass,
access_vector_t av)
{
const uint16_t *common_pts_idx = 0;
access_vector_t common_base = 0;
unsigned int i;
if (!av) {
errno = EINVAL;
return NULL;
}
for (i = 0; i < ARRAY_SIZE(av_inherit); i++) {
if (av_inherit[i].tclass == tclass) {
common_pts_idx =
&common_perm_to_string.data[av_inherit[i].
common_pts_idx];
common_base = av_inherit[i].common_base;
break;
}
}
if (av < common_base) {
i = 0;
while (!(av & 1)) {
av >>= 1;
i++;
}
return common_perm_to_string_data.str + common_pts_idx[i];
}
for (i = 0; i < NVECTORS; i++) {
if (av_perm_to_string[i].tclass == tclass &&
av_perm_to_string[i].value == av)
return av_perm_to_string_data.str
+ av_perm_to_string[i].nameidx;
}
errno = EINVAL;
return NULL;
}
security_class_t string_to_security_class(const char *s)
{
struct discover_class_node *node;
__selinux_once(once, init_obj_class_compat);
if (obj_class_compat)
return string_to_security_class_compat(s);
node = get_class_cache_entry_name(s);
if (node == NULL) {
node = discover_class(s);
if (node == NULL) {
errno = EINVAL;
return 0;
}
}
return map_class(node->value);
}
security_class_t mode_to_security_class(mode_t m) {
if (S_ISREG(m))
return string_to_security_class("file");
if (S_ISDIR(m))
return string_to_security_class("dir");
if (S_ISCHR(m))
return string_to_security_class("chr_file");
if (S_ISBLK(m))
return string_to_security_class("blk_file");
if (S_ISFIFO(m))
return string_to_security_class("fifo_file");
if (S_ISLNK(m))
return string_to_security_class("lnk_file");
if (S_ISSOCK(m))
return string_to_security_class("sock_file");
errno=EINVAL;
return 0;
}
access_vector_t string_to_av_perm(security_class_t tclass, const char *s)
{
struct discover_class_node *node;
security_class_t kclass = unmap_class(tclass);
__selinux_once(once, init_obj_class_compat);
if (obj_class_compat)
return map_perm(tclass, string_to_av_perm_compat(kclass, s));
node = get_class_cache_entry_value(kclass);
if (node != NULL) {
size_t i;
for (i=0; i<MAXVECTORS && node->perms[i] != NULL; i++)
if (strcmp(node->perms[i],s) == 0)
return map_perm(tclass, 1<<i);
}
errno = EINVAL;
return 0;
}
const char *security_class_to_string(security_class_t tclass)
{
struct discover_class_node *node;
tclass = unmap_class(tclass);
__selinux_once(once, init_obj_class_compat);
if (obj_class_compat)
return security_class_to_string_compat(tclass);
node = get_class_cache_entry_value(tclass);
if (node == NULL)
return security_class_to_string_compat(tclass);
else
return node->name;
}
const char *security_av_perm_to_string(security_class_t tclass,
access_vector_t av)
{
struct discover_class_node *node;
size_t i;
av = unmap_perm(tclass, av);
tclass = unmap_class(tclass);
__selinux_once(once, init_obj_class_compat);
if (obj_class_compat)
return security_av_perm_to_string_compat(tclass,av);
node = get_class_cache_entry_value(tclass);
if (av && node)
for (i = 0; i<MAXVECTORS; i++)
if ((1<<i) & av)
return node->perms[i];
return security_av_perm_to_string_compat(tclass,av);
}
int security_av_string(security_class_t tclass, access_vector_t av, char **res)
{
unsigned int i = 0;
size_t len = 5;
access_vector_t tmp = av;
int rc = 0;
const char *str;
char *ptr;
/* first pass computes the required length */
while (tmp) {
if (tmp & 1) {
str = security_av_perm_to_string(tclass, av & (1<<i));
if (str)
len += strlen(str) + 1;
else {
rc = -1;
errno = EINVAL;
goto out;
}
}
tmp >>= 1;
i++;
}
*res = malloc(len);
if (!*res) {
rc = -1;
goto out;
}
/* second pass constructs the string */
i = 0;
tmp = av;
ptr = *res;
if (!av) {
sprintf(ptr, "null");
goto out;
}
ptr += sprintf(ptr, "{ ");
while (tmp) {
if (tmp & 1)
ptr += sprintf(ptr, "%s ", security_av_perm_to_string(
tclass, av & (1<<i)));
tmp >>= 1;
i++;
}
sprintf(ptr, "}");
out:
return rc;
}
void print_access_vector(security_class_t tclass, access_vector_t av)
{
const char *permstr;
access_vector_t bit = 1;
if (av == 0) {
printf(" null");
return;
}
printf(" {");
while (av) {
if (av & bit) {
permstr = security_av_perm_to_string(tclass, bit);
if (!permstr)
break;
printf(" %s", permstr);
av &= ~bit;
}
bit <<= 1;
}
if (av)
printf(" 0x%x", av);
printf(" }");
}
|