1
2 /*
3 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
4 */
5 /*
6 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
7 *
8 * Support for enhanced MLS infrastructure.
9 *
10 * Updated: Frank Mayer <mayerf@tresys.com>
11 * and Karl MacMillan <kmacmillan@tresys.com>
12 *
13 * Added conditional policy language extensions
14 *
15 * Updated: Red Hat, Inc. James Morris <jmorris@redhat.com>
16 *
17 * Fine-grained netlink support
18 * IPv6 support
19 * Code cleanup
20 *
21 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * Copyright (C) 2003 - 2004 Red Hat, Inc.
24 * Copyright (C) 2017 Mellanox Technologies Inc.
25 *
26 * This library is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU Lesser General Public
28 * License as published by the Free Software Foundation; either
29 * version 2.1 of the License, or (at your option) any later version.
30 *
31 * This library is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * Lesser General Public License for more details.
35 *
36 * You should have received a copy of the GNU Lesser General Public
37 * License along with this library; if not, write to the Free Software
38 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 */
40
41 /* FLASK */
42
43 /*
44 * Implementation of the security services.
45 */
46
47 /* Initial sizes malloc'd for sepol_compute_av_reason_buffer() support */
48 #define REASON_BUF_SIZE 2048
49 #define EXPR_BUF_SIZE 1024
50 #define STACK_LEN 32
51
52 #include <stdlib.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57
58 #include <sepol/policydb/policydb.h>
59 #include <sepol/policydb/sidtab.h>
60 #include <sepol/policydb/services.h>
61 #include <sepol/policydb/conditional.h>
62 #include <sepol/policydb/flask.h>
63 #include <sepol/policydb/util.h>
64
65 #include "debug.h"
66 #include "private.h"
67 #include "context.h"
68 #include "av_permissions.h"
69 #include "dso.h"
70 #include "mls.h"
71
72 #define BUG() do { ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
73 #define BUG_ON(x) do { if (x) ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
74
75 static int selinux_enforcing = 1;
76
77 static sidtab_t mysidtab, *sidtab = &mysidtab;
78 static policydb_t mypolicydb, *policydb = &mypolicydb;
79
80 /* Used by sepol_compute_av_reason_buffer() to keep track of entries */
81 static int reason_buf_used;
82 static int reason_buf_len;
83
84 /* Stack services for RPN to infix conversion. */
85 static char **stack;
86 static int stack_len;
87 static int next_stack_entry;
88
push(char * expr_ptr)89 static void push(char *expr_ptr)
90 {
91 if (next_stack_entry >= stack_len) {
92 char **new_stack = stack;
93 int new_stack_len;
94
95 if (stack_len == 0)
96 new_stack_len = STACK_LEN;
97 else
98 new_stack_len = stack_len * 2;
99
100 new_stack = realloc(stack, new_stack_len * sizeof(*stack));
101 if (!new_stack) {
102 ERR(NULL, "unable to allocate stack space");
103 return;
104 }
105 stack_len = new_stack_len;
106 stack = new_stack;
107 }
108 stack[next_stack_entry] = expr_ptr;
109 next_stack_entry++;
110 }
111
pop(void)112 static char *pop(void)
113 {
114 next_stack_entry--;
115 if (next_stack_entry < 0) {
116 next_stack_entry = 0;
117 ERR(NULL, "pop called with no stack entries");
118 return NULL;
119 }
120 return stack[next_stack_entry];
121 }
122 /* End Stack services */
123
sepol_set_sidtab(sidtab_t * s)124 int hidden sepol_set_sidtab(sidtab_t * s)
125 {
126 sidtab = s;
127 return 0;
128 }
129
sepol_set_policydb(policydb_t * p)130 int hidden sepol_set_policydb(policydb_t * p)
131 {
132 policydb = p;
133 return 0;
134 }
135
sepol_set_policydb_from_file(FILE * fp)136 int sepol_set_policydb_from_file(FILE * fp)
137 {
138 struct policy_file pf;
139
140 policy_file_init(&pf);
141 pf.fp = fp;
142 pf.type = PF_USE_STDIO;
143 if (mypolicydb.policy_type)
144 policydb_destroy(&mypolicydb);
145 if (policydb_init(&mypolicydb)) {
146 ERR(NULL, "Out of memory!");
147 return -1;
148 }
149 if (policydb_read(&mypolicydb, &pf, 0)) {
150 policydb_destroy(&mypolicydb);
151 ERR(NULL, "can't read binary policy: %s", strerror(errno));
152 return -1;
153 }
154 policydb = &mypolicydb;
155 return sepol_sidtab_init(sidtab);
156 }
157
158 /*
159 * The largest sequence number that has been used when
160 * providing an access decision to the access vector cache.
161 * The sequence number only changes when a policy change
162 * occurs.
163 */
164 static uint32_t latest_granting = 0;
165
166 /*
167 * cat_expr_buf adds a string to an expression buffer and handles
168 * realloc's if buffer is too small. The array of expression text
169 * buffer pointers and its counter are globally defined here as
170 * constraint_expr_eval_reason() sets them up and cat_expr_buf
171 * updates the e_buf pointer.
172 */
173 static int expr_counter;
174 static char **expr_list;
175 static int expr_buf_used;
176 static int expr_buf_len;
177
cat_expr_buf(char * e_buf,const char * string)178 static void cat_expr_buf(char *e_buf, const char *string)
179 {
180 int len, new_buf_len;
181 char *p, *new_buf = e_buf;
182
183 while (1) {
184 p = e_buf + expr_buf_used;
185 len = snprintf(p, expr_buf_len - expr_buf_used, "%s", string);
186 if (len < 0 || len >= expr_buf_len - expr_buf_used) {
187 new_buf_len = expr_buf_len + EXPR_BUF_SIZE;
188 new_buf = realloc(e_buf, new_buf_len);
189 if (!new_buf) {
190 ERR(NULL, "failed to realloc expr buffer");
191 return;
192 }
193 /* Update new ptr in expr list and locally + new len */
194 expr_list[expr_counter] = new_buf;
195 e_buf = new_buf;
196 expr_buf_len = new_buf_len;
197 } else {
198 expr_buf_used += len;
199 return;
200 }
201 }
202 }
203
204 /*
205 * If the POLICY_KERN version is >= POLICYDB_VERSION_CONSTRAINT_NAMES,
206 * then for 'types' only, read the types_names->types list as it will
207 * contain a list of types and attributes that were defined in the
208 * policy source.
209 * For user and role plus types (for policy vers <
210 * POLICYDB_VERSION_CONSTRAINT_NAMES) just read the e->names list.
211 */
get_name_list(constraint_expr_t * e,int type,const char * src,const char * op,int failed)212 static void get_name_list(constraint_expr_t *e, int type,
213 const char *src, const char *op, int failed)
214 {
215 ebitmap_t *types;
216 int rc = 0;
217 unsigned int i;
218 char tmp_buf[128];
219 int counter = 0;
220
221 if (policydb->policy_type == POLICY_KERN &&
222 policydb->policyvers >= POLICYDB_VERSION_CONSTRAINT_NAMES &&
223 type == CEXPR_TYPE)
224 types = &e->type_names->types;
225 else
226 types = &e->names;
227
228 /* Find out how many entries */
229 for (i = ebitmap_startbit(types); i < ebitmap_length(types); i++) {
230 rc = ebitmap_get_bit(types, i);
231 if (rc == 0)
232 continue;
233 else
234 counter++;
235 }
236 snprintf(tmp_buf, sizeof(tmp_buf), "(%s%s", src, op);
237 cat_expr_buf(expr_list[expr_counter], tmp_buf);
238
239 if (counter == 0)
240 cat_expr_buf(expr_list[expr_counter], "<empty_set> ");
241 if (counter > 1)
242 cat_expr_buf(expr_list[expr_counter], " {");
243 if (counter >= 1) {
244 for (i = ebitmap_startbit(types); i < ebitmap_length(types); i++) {
245 rc = ebitmap_get_bit(types, i);
246 if (rc == 0)
247 continue;
248
249 /* Collect entries */
250 switch (type) {
251 case CEXPR_USER:
252 snprintf(tmp_buf, sizeof(tmp_buf), " %s",
253 policydb->p_user_val_to_name[i]);
254 break;
255 case CEXPR_ROLE:
256 snprintf(tmp_buf, sizeof(tmp_buf), " %s",
257 policydb->p_role_val_to_name[i]);
258 break;
259 case CEXPR_TYPE:
260 snprintf(tmp_buf, sizeof(tmp_buf), " %s",
261 policydb->p_type_val_to_name[i]);
262 break;
263 }
264 cat_expr_buf(expr_list[expr_counter], tmp_buf);
265 }
266 }
267 if (counter > 1)
268 cat_expr_buf(expr_list[expr_counter], " }");
269 if (failed)
270 cat_expr_buf(expr_list[expr_counter], " -Fail-) ");
271 else
272 cat_expr_buf(expr_list[expr_counter], ") ");
273
274 return;
275 }
276
msgcat(const char * src,const char * tgt,const char * op,int failed)277 static void msgcat(const char *src, const char *tgt, const char *op, int failed)
278 {
279 char tmp_buf[128];
280 if (failed)
281 snprintf(tmp_buf, sizeof(tmp_buf), "(%s %s %s -Fail-) ",
282 src, op, tgt);
283 else
284 snprintf(tmp_buf, sizeof(tmp_buf), "(%s %s %s) ",
285 src, op, tgt);
286 cat_expr_buf(expr_list[expr_counter], tmp_buf);
287 }
288
289 /* Returns a buffer with class, statement type and permissions */
get_class_info(sepol_security_class_t tclass,constraint_node_t * constraint,context_struct_t * xcontext)290 static char *get_class_info(sepol_security_class_t tclass,
291 constraint_node_t *constraint,
292 context_struct_t *xcontext)
293 {
294 constraint_expr_t *e;
295 int mls, state_num;
296
297 /* Find if MLS statement or not */
298 mls = 0;
299 for (e = constraint->expr; e; e = e->next) {
300 if (e->attr >= CEXPR_L1L2) {
301 mls = 1;
302 break;
303 }
304 }
305
306 /* Determine statement type */
307 const char *statements[] = {
308 "constrain ", /* 0 */
309 "mlsconstrain ", /* 1 */
310 "validatetrans ", /* 2 */
311 "mlsvalidatetrans ", /* 3 */
312 0 };
313
314 if (xcontext == NULL)
315 state_num = mls + 0;
316 else
317 state_num = mls + 2;
318
319 int class_buf_len = 0;
320 int new_class_buf_len;
321 int len, buf_used;
322 char *class_buf = NULL, *p;
323 char *new_class_buf = NULL;
324
325 while (1) {
326 new_class_buf_len = class_buf_len + EXPR_BUF_SIZE;
327 new_class_buf = realloc(class_buf, new_class_buf_len);
328 if (!new_class_buf)
329 return NULL;
330 class_buf_len = new_class_buf_len;
331 class_buf = new_class_buf;
332 buf_used = 0;
333 p = class_buf;
334
335 /* Add statement type */
336 len = snprintf(p, class_buf_len - buf_used, "%s", statements[state_num]);
337 if (len < 0 || len >= class_buf_len - buf_used)
338 continue;
339
340 /* Add class entry */
341 p += len;
342 buf_used += len;
343 len = snprintf(p, class_buf_len - buf_used, "%s ",
344 policydb->p_class_val_to_name[tclass - 1]);
345 if (len < 0 || len >= class_buf_len - buf_used)
346 continue;
347
348 /* Add permission entries (validatetrans does not have perms) */
349 p += len;
350 buf_used += len;
351 if (state_num < 2) {
352 len = snprintf(p, class_buf_len - buf_used, "{%s } (",
353 sepol_av_to_string(policydb, tclass,
354 constraint->permissions));
355 } else {
356 len = snprintf(p, class_buf_len - buf_used, "(");
357 }
358 if (len < 0 || len >= class_buf_len - buf_used)
359 continue;
360 break;
361 }
362 return class_buf;
363 }
364
365 /*
366 * Modified version of constraint_expr_eval that will process each
367 * constraint as before but adds the information to text buffers that
368 * will hold various components. The expression will be in RPN format,
369 * therefore there is a stack based RPN to infix converter to produce
370 * the final readable constraint.
371 *
372 * Return the boolean value of a constraint expression
373 * when it is applied to the specified source and target
374 * security contexts.
375 *
376 * xcontext is a special beast... It is used by the validatetrans rules
377 * only. For these rules, scontext is the context before the transition,
378 * tcontext is the context after the transition, and xcontext is the
379 * context of the process performing the transition. All other callers
380 * of constraint_expr_eval_reason should pass in NULL for xcontext.
381 *
382 * This function will also build a buffer as the constraint is processed
383 * for analysis. If this option is not required, then:
384 * 'tclass' should be '0' and r_buf MUST be NULL.
385 */
constraint_expr_eval_reason(context_struct_t * scontext,context_struct_t * tcontext,context_struct_t * xcontext,sepol_security_class_t tclass,constraint_node_t * constraint,char ** r_buf,unsigned int flags)386 static int constraint_expr_eval_reason(context_struct_t *scontext,
387 context_struct_t *tcontext,
388 context_struct_t *xcontext,
389 sepol_security_class_t tclass,
390 constraint_node_t *constraint,
391 char **r_buf,
392 unsigned int flags)
393 {
394 uint32_t val1, val2;
395 context_struct_t *c;
396 role_datum_t *r1, *r2;
397 mls_level_t *l1, *l2;
398 constraint_expr_t *e;
399 int s[CEXPR_MAXDEPTH];
400 int sp = -1;
401 char tmp_buf[128];
402
403 /*
404 * Define the s_t_x_num values that make up r1, t2 etc. in text strings
405 * Set 1 = source, 2 = target, 3 = xcontext for validatetrans
406 */
407 #define SOURCE 1
408 #define TARGET 2
409 #define XTARGET 3
410
411 int s_t_x_num = SOURCE;
412
413 /* Set 0 = fail, u = CEXPR_USER, r = CEXPR_ROLE, t = CEXPR_TYPE */
414 int u_r_t = 0;
415
416 char *src = NULL;
417 char *tgt = NULL;
418 int rc = 0, x;
419 char *class_buf = NULL;
420
421 /*
422 * The array of expression answer buffer pointers and counter.
423 */
424 char **answer_list = NULL;
425 int answer_counter = 0;
426
427 class_buf = get_class_info(tclass, constraint, xcontext);
428 if (!class_buf) {
429 ERR(NULL, "failed to allocate class buffer");
430 return -ENOMEM;
431 }
432
433 /* Original function but with buffer support */
434 int expr_list_len = 0;
435 expr_counter = 0;
436 expr_list = NULL;
437 for (e = constraint->expr; e; e = e->next) {
438 /* Allocate a stack to hold expression buffer entries */
439 if (expr_counter >= expr_list_len) {
440 char **new_expr_list = expr_list;
441 int new_expr_list_len;
442
443 if (expr_list_len == 0)
444 new_expr_list_len = STACK_LEN;
445 else
446 new_expr_list_len = expr_list_len * 2;
447
448 new_expr_list = realloc(expr_list,
449 new_expr_list_len * sizeof(*expr_list));
450 if (!new_expr_list) {
451 ERR(NULL, "failed to allocate expr buffer stack");
452 rc = -ENOMEM;
453 goto out;
454 }
455 expr_list_len = new_expr_list_len;
456 expr_list = new_expr_list;
457 }
458
459 /*
460 * malloc a buffer to store each expression text component. If
461 * buffer is too small cat_expr_buf() will realloc extra space.
462 */
463 expr_buf_len = EXPR_BUF_SIZE;
464 expr_list[expr_counter] = malloc(expr_buf_len);
465 if (!expr_list[expr_counter]) {
466 ERR(NULL, "failed to allocate expr buffer");
467 rc = -ENOMEM;
468 goto out;
469 }
470 expr_buf_used = 0;
471
472 /* Now process each expression of the constraint */
473 switch (e->expr_type) {
474 case CEXPR_NOT:
475 BUG_ON(sp < 0);
476 s[sp] = !s[sp];
477 cat_expr_buf(expr_list[expr_counter], "not");
478 break;
479 case CEXPR_AND:
480 BUG_ON(sp < 1);
481 sp--;
482 s[sp] &= s[sp + 1];
483 cat_expr_buf(expr_list[expr_counter], "and");
484 break;
485 case CEXPR_OR:
486 BUG_ON(sp < 1);
487 sp--;
488 s[sp] |= s[sp + 1];
489 cat_expr_buf(expr_list[expr_counter], "or");
490 break;
491 case CEXPR_ATTR:
492 if (sp == (CEXPR_MAXDEPTH - 1))
493 goto out;
494
495 switch (e->attr) {
496 case CEXPR_USER:
497 val1 = scontext->user;
498 val2 = tcontext->user;
499 free(src); src = strdup("u1");
500 free(tgt); tgt = strdup("u2");
501 break;
502 case CEXPR_TYPE:
503 val1 = scontext->type;
504 val2 = tcontext->type;
505 free(src); src = strdup("t1");
506 free(tgt); tgt = strdup("t2");
507 break;
508 case CEXPR_ROLE:
509 val1 = scontext->role;
510 val2 = tcontext->role;
511 r1 = policydb->role_val_to_struct[val1 - 1];
512 r2 = policydb->role_val_to_struct[val2 - 1];
513 free(src); src = strdup("r1");
514 free(tgt); tgt = strdup("r2");
515
516 switch (e->op) {
517 case CEXPR_DOM:
518 s[++sp] = ebitmap_get_bit(&r1->dominates, val2 - 1);
519 msgcat(src, tgt, "dom", s[sp] == 0);
520 expr_counter++;
521 continue;
522 case CEXPR_DOMBY:
523 s[++sp] = ebitmap_get_bit(&r2->dominates, val1 - 1);
524 msgcat(src, tgt, "domby", s[sp] == 0);
525 expr_counter++;
526 continue;
527 case CEXPR_INCOMP:
528 s[++sp] = (!ebitmap_get_bit(&r1->dominates, val2 - 1)
529 && !ebitmap_get_bit(&r2->dominates, val1 - 1));
530 msgcat(src, tgt, "incomp", s[sp] == 0);
531 expr_counter++;
532 continue;
533 default:
534 break;
535 }
536 break;
537 case CEXPR_L1L2:
538 l1 = &(scontext->range.level[0]);
539 l2 = &(tcontext->range.level[0]);
540 free(src); src = strdup("l1");
541 free(tgt); tgt = strdup("l2");
542 goto mls_ops;
543 case CEXPR_L1H2:
544 l1 = &(scontext->range.level[0]);
545 l2 = &(tcontext->range.level[1]);
546 free(src); src = strdup("l1");
547 free(tgt); tgt = strdup("h2");
548 goto mls_ops;
549 case CEXPR_H1L2:
550 l1 = &(scontext->range.level[1]);
551 l2 = &(tcontext->range.level[0]);
552 free(src); src = strdup("h1");
553 free(tgt); tgt = strdup("l2");
554 goto mls_ops;
555 case CEXPR_H1H2:
556 l1 = &(scontext->range.level[1]);
557 l2 = &(tcontext->range.level[1]);
558 free(src); src = strdup("h1");
559 free(tgt); tgt = strdup("h2");
560 goto mls_ops;
561 case CEXPR_L1H1:
562 l1 = &(scontext->range.level[0]);
563 l2 = &(scontext->range.level[1]);
564 free(src); src = strdup("l1");
565 free(tgt); tgt = strdup("h1");
566 goto mls_ops;
567 case CEXPR_L2H2:
568 l1 = &(tcontext->range.level[0]);
569 l2 = &(tcontext->range.level[1]);
570 free(src); src = strdup("l2");
571 free(tgt); tgt = strdup("h2");
572 mls_ops:
573 switch (e->op) {
574 case CEXPR_EQ:
575 s[++sp] = mls_level_eq(l1, l2);
576 msgcat(src, tgt, "eq", s[sp] == 0);
577 expr_counter++;
578 continue;
579 case CEXPR_NEQ:
580 s[++sp] = !mls_level_eq(l1, l2);
581 msgcat(src, tgt, "!=", s[sp] == 0);
582 expr_counter++;
583 continue;
584 case CEXPR_DOM:
585 s[++sp] = mls_level_dom(l1, l2);
586 msgcat(src, tgt, "dom", s[sp] == 0);
587 expr_counter++;
588 continue;
589 case CEXPR_DOMBY:
590 s[++sp] = mls_level_dom(l2, l1);
591 msgcat(src, tgt, "domby", s[sp] == 0);
592 expr_counter++;
593 continue;
594 case CEXPR_INCOMP:
595 s[++sp] = mls_level_incomp(l2, l1);
596 msgcat(src, tgt, "incomp", s[sp] == 0);
597 expr_counter++;
598 continue;
599 default:
600 BUG();
601 goto out;
602 }
603 break;
604 default:
605 BUG();
606 goto out;
607 }
608
609 switch (e->op) {
610 case CEXPR_EQ:
611 s[++sp] = (val1 == val2);
612 msgcat(src, tgt, "==", s[sp] == 0);
613 break;
614 case CEXPR_NEQ:
615 s[++sp] = (val1 != val2);
616 msgcat(src, tgt, "!=", s[sp] == 0);
617 break;
618 default:
619 BUG();
620 goto out;
621 }
622 break;
623 case CEXPR_NAMES:
624 if (sp == (CEXPR_MAXDEPTH - 1))
625 goto out;
626 s_t_x_num = SOURCE;
627 c = scontext;
628 if (e->attr & CEXPR_TARGET) {
629 s_t_x_num = TARGET;
630 c = tcontext;
631 } else if (e->attr & CEXPR_XTARGET) {
632 s_t_x_num = XTARGET;
633 c = xcontext;
634 }
635 if (!c) {
636 BUG();
637 goto out;
638 }
639 if (e->attr & CEXPR_USER) {
640 u_r_t = CEXPR_USER;
641 val1 = c->user;
642 snprintf(tmp_buf, sizeof(tmp_buf), "u%d ", s_t_x_num);
643 free(src); src = strdup(tmp_buf);
644 } else if (e->attr & CEXPR_ROLE) {
645 u_r_t = CEXPR_ROLE;
646 val1 = c->role;
647 snprintf(tmp_buf, sizeof(tmp_buf), "r%d ", s_t_x_num);
648 free(src); src = strdup(tmp_buf);
649 } else if (e->attr & CEXPR_TYPE) {
650 u_r_t = CEXPR_TYPE;
651 val1 = c->type;
652 snprintf(tmp_buf, sizeof(tmp_buf), "t%d ", s_t_x_num);
653 free(src); src = strdup(tmp_buf);
654 } else {
655 BUG();
656 goto out;
657 }
658
659 switch (e->op) {
660 case CEXPR_EQ:
661 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
662 get_name_list(e, u_r_t, src, "==", s[sp] == 0);
663 break;
664
665 case CEXPR_NEQ:
666 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
667 get_name_list(e, u_r_t, src, "!=", s[sp] == 0);
668 break;
669 default:
670 BUG();
671 goto out;
672 }
673 break;
674 default:
675 BUG();
676 goto out;
677 }
678 expr_counter++;
679 }
680
681 /*
682 * At this point each expression of the constraint is in
683 * expr_list[n+1] and in RPN format. Now convert to 'infix'
684 */
685
686 /*
687 * Save expr count but zero expr_counter to detect if
688 * 'BUG(); goto out;' was called as we need to release any used
689 * expr_list malloc's. Normally they are released by the RPN to
690 * infix code.
691 */
692 int expr_count = expr_counter;
693 expr_counter = 0;
694
695 /*
696 * Generate the same number of answer buffer entries as expression
697 * buffers (as there will never be more).
698 */
699 answer_list = malloc(expr_count * sizeof(*answer_list));
700 if (!answer_list) {
701 ERR(NULL, "failed to allocate answer stack");
702 rc = -ENOMEM;
703 goto out;
704 }
705
706 /* The pop operands */
707 char *a;
708 char *b;
709 int a_len, b_len;
710
711 /* Convert constraint from RPN to infix notation. */
712 for (x = 0; x != expr_count; x++) {
713 if (strncmp(expr_list[x], "and", 3) == 0 || strncmp(expr_list[x],
714 "or", 2) == 0) {
715 b = pop();
716 b_len = strlen(b);
717 a = pop();
718 a_len = strlen(a);
719
720 /* get a buffer to hold the answer */
721 answer_list[answer_counter] = malloc(a_len + b_len + 8);
722 if (!answer_list[answer_counter]) {
723 ERR(NULL, "failed to allocate answer buffer");
724 rc = -ENOMEM;
725 goto out;
726 }
727 memset(answer_list[answer_counter], '\0', a_len + b_len + 8);
728
729 sprintf(answer_list[answer_counter], "%s %s %s", a,
730 expr_list[x], b);
731 push(answer_list[answer_counter++]);
732 free(a);
733 free(b);
734 free(expr_list[x]);
735 } else if (strncmp(expr_list[x], "not", 3) == 0) {
736 b = pop();
737 b_len = strlen(b);
738
739 answer_list[answer_counter] = malloc(b_len + 8);
740 if (!answer_list[answer_counter]) {
741 ERR(NULL, "failed to allocate answer buffer");
742 rc = -ENOMEM;
743 goto out;
744 }
745 memset(answer_list[answer_counter], '\0', b_len + 8);
746
747 if (strncmp(b, "not", 3) == 0)
748 sprintf(answer_list[answer_counter], "%s (%s)",
749 expr_list[x], b);
750 else
751 sprintf(answer_list[answer_counter], "%s%s",
752 expr_list[x], b);
753 push(answer_list[answer_counter++]);
754 free(b);
755 free(expr_list[x]);
756 } else {
757 push(expr_list[x]);
758 }
759 }
760 /* Get the final answer from tos and build constraint text */
761 a = pop();
762
763 /* validatetrans / constraint calculation:
764 rc = 0 is denied, rc = 1 is granted */
765 sprintf(tmp_buf, "%s %s\n",
766 xcontext ? "Validatetrans" : "Constraint",
767 s[0] ? "GRANTED" : "DENIED");
768
769 int len, new_buf_len;
770 char *p, **new_buf = r_buf;
771 /*
772 * These contain the constraint components that are added to the
773 * callers reason buffer.
774 */
775 const char *buffers[] = { class_buf, a, "); ", tmp_buf, 0 };
776
777 /*
778 * This will add the constraints to the callers reason buffer (who is
779 * responsible for freeing the memory). It will handle any realloc's
780 * should the buffer be too short.
781 * The reason_buf_used and reason_buf_len counters are defined
782 * globally as multiple constraints can be in the buffer.
783 */
784
785 if (r_buf && ((s[0] == 0) || ((s[0] == 1 &&
786 (flags & SHOW_GRANTED) == SHOW_GRANTED)))) {
787 for (x = 0; buffers[x] != NULL; x++) {
788 while (1) {
789 p = *r_buf + reason_buf_used;
790 len = snprintf(p, reason_buf_len - reason_buf_used,
791 "%s", buffers[x]);
792 if (len < 0 || len >= reason_buf_len - reason_buf_used) {
793 new_buf_len = reason_buf_len + REASON_BUF_SIZE;
794 *new_buf = realloc(*r_buf, new_buf_len);
795 if (!new_buf) {
796 ERR(NULL, "failed to realloc reason buffer");
797 goto out1;
798 }
799 **r_buf = **new_buf;
800 reason_buf_len = new_buf_len;
801 continue;
802 } else {
803 reason_buf_used += len;
804 break;
805 }
806 }
807 }
808 }
809
810 out1:
811 rc = s[0];
812 free(a);
813
814 out:
815 free(class_buf);
816 free(src);
817 free(tgt);
818
819 if (expr_counter) {
820 for (x = 0; expr_list[x] != NULL; x++)
821 free(expr_list[x]);
822 }
823 free(answer_list);
824 free(expr_list);
825 return rc;
826 }
827
828 /* Forward declaration */
829 static int context_struct_compute_av(context_struct_t * scontext,
830 context_struct_t * tcontext,
831 sepol_security_class_t tclass,
832 sepol_access_vector_t requested,
833 struct sepol_av_decision *avd,
834 unsigned int *reason,
835 char **r_buf,
836 unsigned int flags);
837
type_attribute_bounds_av(context_struct_t * scontext,context_struct_t * tcontext,sepol_security_class_t tclass,sepol_access_vector_t requested,struct sepol_av_decision * avd,unsigned int * reason)838 static void type_attribute_bounds_av(context_struct_t *scontext,
839 context_struct_t *tcontext,
840 sepol_security_class_t tclass,
841 sepol_access_vector_t requested,
842 struct sepol_av_decision *avd,
843 unsigned int *reason)
844 {
845 context_struct_t lo_scontext;
846 context_struct_t lo_tcontext, *tcontextp = tcontext;
847 struct sepol_av_decision lo_avd;
848 type_datum_t *source;
849 type_datum_t *target;
850 sepol_access_vector_t masked = 0;
851
852 source = policydb->type_val_to_struct[scontext->type - 1];
853 if (!source->bounds)
854 return;
855
856 target = policydb->type_val_to_struct[tcontext->type - 1];
857
858 memset(&lo_avd, 0, sizeof(lo_avd));
859
860 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
861 lo_scontext.type = source->bounds;
862
863 if (target->bounds) {
864 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
865 lo_tcontext.type = target->bounds;
866 tcontextp = &lo_tcontext;
867 }
868
869 context_struct_compute_av(&lo_scontext,
870 tcontextp,
871 tclass,
872 requested,
873 &lo_avd,
874 NULL, /* reason intentionally omitted */
875 NULL,
876 0);
877
878 masked = ~lo_avd.allowed & avd->allowed;
879
880 if (!masked)
881 return; /* no masked permission */
882
883 /* mask violated permissions */
884 avd->allowed &= ~masked;
885
886 *reason |= SEPOL_COMPUTEAV_BOUNDS;
887 }
888
889 /*
890 * Compute access vectors based on a context structure pair for
891 * the permissions in a particular class.
892 */
context_struct_compute_av(context_struct_t * scontext,context_struct_t * tcontext,sepol_security_class_t tclass,sepol_access_vector_t requested,struct sepol_av_decision * avd,unsigned int * reason,char ** r_buf,unsigned int flags)893 static int context_struct_compute_av(context_struct_t * scontext,
894 context_struct_t * tcontext,
895 sepol_security_class_t tclass,
896 sepol_access_vector_t requested,
897 struct sepol_av_decision *avd,
898 unsigned int *reason,
899 char **r_buf,
900 unsigned int flags)
901 {
902 constraint_node_t *constraint;
903 struct role_allow *ra;
904 avtab_key_t avkey;
905 class_datum_t *tclass_datum;
906 avtab_ptr_t node;
907 ebitmap_t *sattr, *tattr;
908 ebitmap_node_t *snode, *tnode;
909 unsigned int i, j;
910
911 if (!tclass || tclass > policydb->p_classes.nprim) {
912 ERR(NULL, "unrecognized class %d", tclass);
913 return -EINVAL;
914 }
915 tclass_datum = policydb->class_val_to_struct[tclass - 1];
916
917 /*
918 * Initialize the access vectors to the default values.
919 */
920 avd->allowed = 0;
921 avd->decided = 0xffffffff;
922 avd->auditallow = 0;
923 avd->auditdeny = 0xffffffff;
924 avd->seqno = latest_granting;
925 if (reason)
926 *reason = 0;
927
928 /*
929 * If a specific type enforcement rule was defined for
930 * this permission check, then use it.
931 */
932 avkey.target_class = tclass;
933 avkey.specified = AVTAB_AV;
934 sattr = &policydb->type_attr_map[scontext->type - 1];
935 tattr = &policydb->type_attr_map[tcontext->type - 1];
936 ebitmap_for_each_positive_bit(sattr, snode, i) {
937 ebitmap_for_each_positive_bit(tattr, tnode, j) {
938 avkey.source_type = i + 1;
939 avkey.target_type = j + 1;
940 for (node =
941 avtab_search_node(&policydb->te_avtab, &avkey);
942 node != NULL;
943 node =
944 avtab_search_node_next(node, avkey.specified)) {
945 if (node->key.specified == AVTAB_ALLOWED)
946 avd->allowed |= node->datum.data;
947 else if (node->key.specified ==
948 AVTAB_AUDITALLOW)
949 avd->auditallow |= node->datum.data;
950 else if (node->key.specified == AVTAB_AUDITDENY)
951 avd->auditdeny &= node->datum.data;
952 }
953
954 /* Check conditional av table for additional permissions */
955 cond_compute_av(&policydb->te_cond_avtab, &avkey, avd);
956
957 }
958 }
959
960 if (requested & ~avd->allowed) {
961 if (reason)
962 *reason |= SEPOL_COMPUTEAV_TE;
963 requested &= avd->allowed;
964 }
965
966 /*
967 * Remove any permissions prohibited by a constraint (this includes
968 * the MLS policy).
969 */
970 constraint = tclass_datum->constraints;
971 while (constraint) {
972 if ((constraint->permissions & (avd->allowed)) &&
973 !constraint_expr_eval_reason(scontext, tcontext, NULL,
974 tclass, constraint, r_buf, flags)) {
975 avd->allowed =
976 (avd->allowed) & ~(constraint->permissions);
977 }
978 constraint = constraint->next;
979 }
980
981 if (requested & ~avd->allowed) {
982 if (reason)
983 *reason |= SEPOL_COMPUTEAV_CONS;
984 requested &= avd->allowed;
985 }
986
987 /*
988 * If checking process transition permission and the
989 * role is changing, then check the (current_role, new_role)
990 * pair.
991 */
992 if (tclass == SECCLASS_PROCESS &&
993 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
994 scontext->role != tcontext->role) {
995 for (ra = policydb->role_allow; ra; ra = ra->next) {
996 if (scontext->role == ra->role &&
997 tcontext->role == ra->new_role)
998 break;
999 }
1000 if (!ra)
1001 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
1002 PROCESS__DYNTRANSITION);
1003 }
1004
1005 if (requested & ~avd->allowed) {
1006 if (reason)
1007 *reason |= SEPOL_COMPUTEAV_RBAC;
1008 requested &= avd->allowed;
1009 }
1010
1011 type_attribute_bounds_av(scontext, tcontext, tclass, requested, avd,
1012 reason);
1013 return 0;
1014 }
1015
sepol_validate_transition(sepol_security_id_t oldsid,sepol_security_id_t newsid,sepol_security_id_t tasksid,sepol_security_class_t tclass)1016 int hidden sepol_validate_transition(sepol_security_id_t oldsid,
1017 sepol_security_id_t newsid,
1018 sepol_security_id_t tasksid,
1019 sepol_security_class_t tclass)
1020 {
1021 context_struct_t *ocontext;
1022 context_struct_t *ncontext;
1023 context_struct_t *tcontext;
1024 class_datum_t *tclass_datum;
1025 constraint_node_t *constraint;
1026
1027 if (!tclass || tclass > policydb->p_classes.nprim) {
1028 ERR(NULL, "unrecognized class %d", tclass);
1029 return -EINVAL;
1030 }
1031 tclass_datum = policydb->class_val_to_struct[tclass - 1];
1032
1033 ocontext = sepol_sidtab_search(sidtab, oldsid);
1034 if (!ocontext) {
1035 ERR(NULL, "unrecognized SID %d", oldsid);
1036 return -EINVAL;
1037 }
1038
1039 ncontext = sepol_sidtab_search(sidtab, newsid);
1040 if (!ncontext) {
1041 ERR(NULL, "unrecognized SID %d", newsid);
1042 return -EINVAL;
1043 }
1044
1045 tcontext = sepol_sidtab_search(sidtab, tasksid);
1046 if (!tcontext) {
1047 ERR(NULL, "unrecognized SID %d", tasksid);
1048 return -EINVAL;
1049 }
1050
1051 constraint = tclass_datum->validatetrans;
1052 while (constraint) {
1053 if (!constraint_expr_eval_reason(ocontext, ncontext, tcontext,
1054 0, constraint, NULL, 0)) {
1055 return -EPERM;
1056 }
1057 constraint = constraint->next;
1058 }
1059
1060 return 0;
1061 }
1062
1063 /*
1064 * sepol_validate_transition_reason_buffer - the reason buffer is realloc'd
1065 * in the constraint_expr_eval_reason() function.
1066 */
sepol_validate_transition_reason_buffer(sepol_security_id_t oldsid,sepol_security_id_t newsid,sepol_security_id_t tasksid,sepol_security_class_t tclass,char ** reason_buf,unsigned int flags)1067 int hidden sepol_validate_transition_reason_buffer(sepol_security_id_t oldsid,
1068 sepol_security_id_t newsid,
1069 sepol_security_id_t tasksid,
1070 sepol_security_class_t tclass,
1071 char **reason_buf,
1072 unsigned int flags)
1073 {
1074 context_struct_t *ocontext;
1075 context_struct_t *ncontext;
1076 context_struct_t *tcontext;
1077 class_datum_t *tclass_datum;
1078 constraint_node_t *constraint;
1079
1080 if (!tclass || tclass > policydb->p_classes.nprim) {
1081 ERR(NULL, "unrecognized class %d", tclass);
1082 return -EINVAL;
1083 }
1084 tclass_datum = policydb->class_val_to_struct[tclass - 1];
1085
1086 ocontext = sepol_sidtab_search(sidtab, oldsid);
1087 if (!ocontext) {
1088 ERR(NULL, "unrecognized SID %d", oldsid);
1089 return -EINVAL;
1090 }
1091
1092 ncontext = sepol_sidtab_search(sidtab, newsid);
1093 if (!ncontext) {
1094 ERR(NULL, "unrecognized SID %d", newsid);
1095 return -EINVAL;
1096 }
1097
1098 tcontext = sepol_sidtab_search(sidtab, tasksid);
1099 if (!tcontext) {
1100 ERR(NULL, "unrecognized SID %d", tasksid);
1101 return -EINVAL;
1102 }
1103
1104 /*
1105 * Set the buffer to NULL as mls/validatetrans may not be processed.
1106 * If a buffer is required, then the routines in
1107 * constraint_expr_eval_reason will realloc in REASON_BUF_SIZE
1108 * chunks (as it gets called for each mls/validatetrans processed).
1109 * We just make sure these start from zero.
1110 */
1111 *reason_buf = NULL;
1112 reason_buf_used = 0;
1113 reason_buf_len = 0;
1114 constraint = tclass_datum->validatetrans;
1115 while (constraint) {
1116 if (!constraint_expr_eval_reason(ocontext, ncontext, tcontext,
1117 tclass, constraint, reason_buf, flags)) {
1118 return -EPERM;
1119 }
1120 constraint = constraint->next;
1121 }
1122 return 0;
1123 }
1124
sepol_compute_av_reason(sepol_security_id_t ssid,sepol_security_id_t tsid,sepol_security_class_t tclass,sepol_access_vector_t requested,struct sepol_av_decision * avd,unsigned int * reason)1125 int hidden sepol_compute_av_reason(sepol_security_id_t ssid,
1126 sepol_security_id_t tsid,
1127 sepol_security_class_t tclass,
1128 sepol_access_vector_t requested,
1129 struct sepol_av_decision *avd,
1130 unsigned int *reason)
1131 {
1132 context_struct_t *scontext = 0, *tcontext = 0;
1133 int rc = 0;
1134
1135 scontext = sepol_sidtab_search(sidtab, ssid);
1136 if (!scontext) {
1137 ERR(NULL, "unrecognized source SID %d", ssid);
1138 rc = -EINVAL;
1139 goto out;
1140 }
1141 tcontext = sepol_sidtab_search(sidtab, tsid);
1142 if (!tcontext) {
1143 ERR(NULL, "unrecognized target SID %d", tsid);
1144 rc = -EINVAL;
1145 goto out;
1146 }
1147
1148 rc = context_struct_compute_av(scontext, tcontext, tclass,
1149 requested, avd, reason, NULL, 0);
1150 out:
1151 return rc;
1152 }
1153
1154 /*
1155 * sepol_compute_av_reason_buffer - the reason buffer is malloc'd to
1156 * REASON_BUF_SIZE. If the buffer size is exceeded, then it is realloc'd
1157 * in the constraint_expr_eval_reason() function.
1158 */
sepol_compute_av_reason_buffer(sepol_security_id_t ssid,sepol_security_id_t tsid,sepol_security_class_t tclass,sepol_access_vector_t requested,struct sepol_av_decision * avd,unsigned int * reason,char ** reason_buf,unsigned int flags)1159 int hidden sepol_compute_av_reason_buffer(sepol_security_id_t ssid,
1160 sepol_security_id_t tsid,
1161 sepol_security_class_t tclass,
1162 sepol_access_vector_t requested,
1163 struct sepol_av_decision *avd,
1164 unsigned int *reason,
1165 char **reason_buf,
1166 unsigned int flags)
1167 {
1168 context_struct_t *scontext = 0, *tcontext = 0;
1169 int rc = 0;
1170
1171 scontext = sepol_sidtab_search(sidtab, ssid);
1172 if (!scontext) {
1173 ERR(NULL, "unrecognized source SID %d", ssid);
1174 rc = -EINVAL;
1175 goto out;
1176 }
1177 tcontext = sepol_sidtab_search(sidtab, tsid);
1178 if (!tcontext) {
1179 ERR(NULL, "unrecognized target SID %d", tsid);
1180 rc = -EINVAL;
1181 goto out;
1182 }
1183
1184 /*
1185 * Set the buffer to NULL as constraints may not be processed.
1186 * If a buffer is required, then the routines in
1187 * constraint_expr_eval_reason will realloc in REASON_BUF_SIZE
1188 * chunks (as it gets called for each constraint processed).
1189 * We just make sure these start from zero.
1190 */
1191 *reason_buf = NULL;
1192 reason_buf_used = 0;
1193 reason_buf_len = 0;
1194
1195 rc = context_struct_compute_av(scontext, tcontext, tclass,
1196 requested, avd, reason, reason_buf, flags);
1197 out:
1198 return rc;
1199 }
1200
sepol_compute_av(sepol_security_id_t ssid,sepol_security_id_t tsid,sepol_security_class_t tclass,sepol_access_vector_t requested,struct sepol_av_decision * avd)1201 int hidden sepol_compute_av(sepol_security_id_t ssid,
1202 sepol_security_id_t tsid,
1203 sepol_security_class_t tclass,
1204 sepol_access_vector_t requested,
1205 struct sepol_av_decision *avd)
1206 {
1207 unsigned int reason = 0;
1208 return sepol_compute_av_reason(ssid, tsid, tclass, requested, avd,
1209 &reason);
1210 }
1211
1212 /*
1213 * Return a class ID associated with the class string specified by
1214 * class_name.
1215 */
sepol_string_to_security_class(const char * class_name,sepol_security_class_t * tclass)1216 int hidden sepol_string_to_security_class(const char *class_name,
1217 sepol_security_class_t *tclass)
1218 {
1219 class_datum_t *tclass_datum;
1220
1221 tclass_datum = hashtab_search(policydb->p_classes.table,
1222 (hashtab_key_t) class_name);
1223 if (!tclass_datum) {
1224 ERR(NULL, "unrecognized class %s", class_name);
1225 return STATUS_ERR;
1226 }
1227 *tclass = tclass_datum->s.value;
1228 return STATUS_SUCCESS;
1229 }
1230
1231 /*
1232 * Return access vector bit associated with the class ID and permission
1233 * string.
1234 */
sepol_string_to_av_perm(sepol_security_class_t tclass,const char * perm_name,sepol_access_vector_t * av)1235 int hidden sepol_string_to_av_perm(sepol_security_class_t tclass,
1236 const char *perm_name,
1237 sepol_access_vector_t *av)
1238 {
1239 class_datum_t *tclass_datum;
1240 perm_datum_t *perm_datum;
1241
1242 if (!tclass || tclass > policydb->p_classes.nprim) {
1243 ERR(NULL, "unrecognized class %d", tclass);
1244 return -EINVAL;
1245 }
1246 tclass_datum = policydb->class_val_to_struct[tclass - 1];
1247
1248 /* Check for unique perms then the common ones (if any) */
1249 perm_datum = (perm_datum_t *)
1250 hashtab_search(tclass_datum->permissions.table,
1251 (hashtab_key_t)perm_name);
1252 if (perm_datum != NULL) {
1253 *av = 0x1 << (perm_datum->s.value - 1);
1254 return STATUS_SUCCESS;
1255 }
1256
1257 if (tclass_datum->comdatum == NULL)
1258 goto out;
1259
1260 perm_datum = (perm_datum_t *)
1261 hashtab_search(tclass_datum->comdatum->permissions.table,
1262 (hashtab_key_t)perm_name);
1263
1264 if (perm_datum != NULL) {
1265 *av = 0x1 << (perm_datum->s.value - 1);
1266 return STATUS_SUCCESS;
1267 }
1268 out:
1269 ERR(NULL, "could not convert %s to av bit", perm_name);
1270 return STATUS_ERR;
1271 }
1272
1273 /*
1274 * Write the security context string representation of
1275 * the context associated with `sid' into a dynamically
1276 * allocated string of the correct size. Set `*scontext'
1277 * to point to this string and set `*scontext_len' to
1278 * the length of the string.
1279 */
sepol_sid_to_context(sepol_security_id_t sid,sepol_security_context_t * scontext,size_t * scontext_len)1280 int hidden sepol_sid_to_context(sepol_security_id_t sid,
1281 sepol_security_context_t * scontext,
1282 size_t * scontext_len)
1283 {
1284 context_struct_t *context;
1285 int rc = 0;
1286
1287 context = sepol_sidtab_search(sidtab, sid);
1288 if (!context) {
1289 ERR(NULL, "unrecognized SID %d", sid);
1290 rc = -EINVAL;
1291 goto out;
1292 }
1293 rc = context_to_string(NULL, policydb, context, scontext, scontext_len);
1294 out:
1295 return rc;
1296
1297 }
1298
1299 /*
1300 * Return a SID associated with the security context that
1301 * has the string representation specified by `scontext'.
1302 */
sepol_context_to_sid(const sepol_security_context_t scontext,size_t scontext_len,sepol_security_id_t * sid)1303 int hidden sepol_context_to_sid(const sepol_security_context_t scontext,
1304 size_t scontext_len, sepol_security_id_t * sid)
1305 {
1306
1307 context_struct_t *context = NULL;
1308
1309 /* First, create the context */
1310 if (context_from_string(NULL, policydb, &context,
1311 scontext, scontext_len) < 0)
1312 goto err;
1313
1314 /* Obtain the new sid */
1315 if (sid && (sepol_sidtab_context_to_sid(sidtab, context, sid) < 0))
1316 goto err;
1317
1318 context_destroy(context);
1319 free(context);
1320 return STATUS_SUCCESS;
1321
1322 err:
1323 if (context) {
1324 context_destroy(context);
1325 free(context);
1326 }
1327 ERR(NULL, "could not convert %s to sid", scontext);
1328 return STATUS_ERR;
1329 }
1330
compute_sid_handle_invalid_context(context_struct_t * scontext,context_struct_t * tcontext,sepol_security_class_t tclass,context_struct_t * newcontext)1331 static inline int compute_sid_handle_invalid_context(context_struct_t *
1332 scontext,
1333 context_struct_t *
1334 tcontext,
1335 sepol_security_class_t
1336 tclass,
1337 context_struct_t *
1338 newcontext)
1339 {
1340 if (selinux_enforcing) {
1341 return -EACCES;
1342 } else {
1343 sepol_security_context_t s, t, n;
1344 size_t slen, tlen, nlen;
1345
1346 context_to_string(NULL, policydb, scontext, &s, &slen);
1347 context_to_string(NULL, policydb, tcontext, &t, &tlen);
1348 context_to_string(NULL, policydb, newcontext, &n, &nlen);
1349 ERR(NULL, "invalid context %s for "
1350 "scontext=%s tcontext=%s tclass=%s",
1351 n, s, t, policydb->p_class_val_to_name[tclass - 1]);
1352 free(s);
1353 free(t);
1354 free(n);
1355 return 0;
1356 }
1357 }
1358
sepol_compute_sid(sepol_security_id_t ssid,sepol_security_id_t tsid,sepol_security_class_t tclass,uint32_t specified,sepol_security_id_t * out_sid)1359 static int sepol_compute_sid(sepol_security_id_t ssid,
1360 sepol_security_id_t tsid,
1361 sepol_security_class_t tclass,
1362 uint32_t specified, sepol_security_id_t * out_sid)
1363 {
1364 context_struct_t *scontext = 0, *tcontext = 0, newcontext;
1365 struct role_trans *roletr = 0;
1366 avtab_key_t avkey;
1367 avtab_datum_t *avdatum;
1368 avtab_ptr_t node;
1369 int rc = 0;
1370
1371 scontext = sepol_sidtab_search(sidtab, ssid);
1372 if (!scontext) {
1373 ERR(NULL, "unrecognized SID %d", ssid);
1374 rc = -EINVAL;
1375 goto out;
1376 }
1377 tcontext = sepol_sidtab_search(sidtab, tsid);
1378 if (!tcontext) {
1379 ERR(NULL, "unrecognized SID %d", tsid);
1380 rc = -EINVAL;
1381 goto out;
1382 }
1383
1384 context_init(&newcontext);
1385
1386 /* Set the user identity. */
1387 switch (specified) {
1388 case AVTAB_TRANSITION:
1389 case AVTAB_CHANGE:
1390 /* Use the process user identity. */
1391 newcontext.user = scontext->user;
1392 break;
1393 case AVTAB_MEMBER:
1394 /* Use the related object owner. */
1395 newcontext.user = tcontext->user;
1396 break;
1397 }
1398
1399 /* Set the role and type to default values. */
1400 switch (tclass) {
1401 case SECCLASS_PROCESS:
1402 /* Use the current role and type of process. */
1403 newcontext.role = scontext->role;
1404 newcontext.type = scontext->type;
1405 break;
1406 default:
1407 /* Use the well-defined object role. */
1408 newcontext.role = OBJECT_R_VAL;
1409 /* Use the type of the related object. */
1410 newcontext.type = tcontext->type;
1411 }
1412
1413 /* Look for a type transition/member/change rule. */
1414 avkey.source_type = scontext->type;
1415 avkey.target_type = tcontext->type;
1416 avkey.target_class = tclass;
1417 avkey.specified = specified;
1418 avdatum = avtab_search(&policydb->te_avtab, &avkey);
1419
1420 /* If no permanent rule, also check for enabled conditional rules */
1421 if (!avdatum) {
1422 node = avtab_search_node(&policydb->te_cond_avtab, &avkey);
1423 for (; node != NULL;
1424 node = avtab_search_node_next(node, specified)) {
1425 if (node->key.specified & AVTAB_ENABLED) {
1426 avdatum = &node->datum;
1427 break;
1428 }
1429 }
1430 }
1431
1432 if (avdatum) {
1433 /* Use the type from the type transition/member/change rule. */
1434 newcontext.type = avdatum->data;
1435 }
1436
1437 /* Check for class-specific changes. */
1438 switch (tclass) {
1439 case SECCLASS_PROCESS:
1440 if (specified & AVTAB_TRANSITION) {
1441 /* Look for a role transition rule. */
1442 for (roletr = policydb->role_tr; roletr;
1443 roletr = roletr->next) {
1444 if (roletr->role == scontext->role &&
1445 roletr->type == tcontext->type) {
1446 /* Use the role transition rule. */
1447 newcontext.role = roletr->new_role;
1448 break;
1449 }
1450 }
1451 }
1452 break;
1453 default:
1454 break;
1455 }
1456
1457 /* Set the MLS attributes.
1458 This is done last because it may allocate memory. */
1459 rc = mls_compute_sid(policydb, scontext, tcontext, tclass, specified,
1460 &newcontext);
1461 if (rc)
1462 goto out;
1463
1464 /* Check the validity of the context. */
1465 if (!policydb_context_isvalid(policydb, &newcontext)) {
1466 rc = compute_sid_handle_invalid_context(scontext,
1467 tcontext,
1468 tclass, &newcontext);
1469 if (rc)
1470 goto out;
1471 }
1472 /* Obtain the sid for the context. */
1473 rc = sepol_sidtab_context_to_sid(sidtab, &newcontext, out_sid);
1474 out:
1475 context_destroy(&newcontext);
1476 return rc;
1477 }
1478
1479 /*
1480 * Compute a SID to use for labeling a new object in the
1481 * class `tclass' based on a SID pair.
1482 */
sepol_transition_sid(sepol_security_id_t ssid,sepol_security_id_t tsid,sepol_security_class_t tclass,sepol_security_id_t * out_sid)1483 int hidden sepol_transition_sid(sepol_security_id_t ssid,
1484 sepol_security_id_t tsid,
1485 sepol_security_class_t tclass,
1486 sepol_security_id_t * out_sid)
1487 {
1488 return sepol_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1489 }
1490
1491 /*
1492 * Compute a SID to use when selecting a member of a
1493 * polyinstantiated object of class `tclass' based on
1494 * a SID pair.
1495 */
sepol_member_sid(sepol_security_id_t ssid,sepol_security_id_t tsid,sepol_security_class_t tclass,sepol_security_id_t * out_sid)1496 int hidden sepol_member_sid(sepol_security_id_t ssid,
1497 sepol_security_id_t tsid,
1498 sepol_security_class_t tclass,
1499 sepol_security_id_t * out_sid)
1500 {
1501 return sepol_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1502 }
1503
1504 /*
1505 * Compute a SID to use for relabeling an object in the
1506 * class `tclass' based on a SID pair.
1507 */
sepol_change_sid(sepol_security_id_t ssid,sepol_security_id_t tsid,sepol_security_class_t tclass,sepol_security_id_t * out_sid)1508 int hidden sepol_change_sid(sepol_security_id_t ssid,
1509 sepol_security_id_t tsid,
1510 sepol_security_class_t tclass,
1511 sepol_security_id_t * out_sid)
1512 {
1513 return sepol_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1514 }
1515
1516 /*
1517 * Verify that each permission that is defined under the
1518 * existing policy is still defined with the same value
1519 * in the new policy.
1520 */
validate_perm(hashtab_key_t key,hashtab_datum_t datum,void * p)1521 static int validate_perm(hashtab_key_t key, hashtab_datum_t datum, void *p)
1522 {
1523 hashtab_t h;
1524 perm_datum_t *perdatum, *perdatum2;
1525
1526 h = (hashtab_t) p;
1527 perdatum = (perm_datum_t *) datum;
1528
1529 perdatum2 = (perm_datum_t *) hashtab_search(h, key);
1530 if (!perdatum2) {
1531 ERR(NULL, "permission %s disappeared", key);
1532 return -1;
1533 }
1534 if (perdatum->s.value != perdatum2->s.value) {
1535 ERR(NULL, "the value of permissions %s changed", key);
1536 return -1;
1537 }
1538 return 0;
1539 }
1540
1541 /*
1542 * Verify that each class that is defined under the
1543 * existing policy is still defined with the same
1544 * attributes in the new policy.
1545 */
validate_class(hashtab_key_t key,hashtab_datum_t datum,void * p)1546 static int validate_class(hashtab_key_t key, hashtab_datum_t datum, void *p)
1547 {
1548 policydb_t *newp;
1549 class_datum_t *cladatum, *cladatum2;
1550
1551 newp = (policydb_t *) p;
1552 cladatum = (class_datum_t *) datum;
1553
1554 cladatum2 =
1555 (class_datum_t *) hashtab_search(newp->p_classes.table, key);
1556 if (!cladatum2) {
1557 ERR(NULL, "class %s disappeared", key);
1558 return -1;
1559 }
1560 if (cladatum->s.value != cladatum2->s.value) {
1561 ERR(NULL, "the value of class %s changed", key);
1562 return -1;
1563 }
1564 if ((cladatum->comdatum && !cladatum2->comdatum) ||
1565 (!cladatum->comdatum && cladatum2->comdatum)) {
1566 ERR(NULL, "the inherits clause for the access "
1567 "vector definition for class %s changed", key);
1568 return -1;
1569 }
1570 if (cladatum->comdatum) {
1571 if (hashtab_map
1572 (cladatum->comdatum->permissions.table, validate_perm,
1573 cladatum2->comdatum->permissions.table)) {
1574 ERR(NULL,
1575 " in the access vector definition "
1576 "for class %s\n", key);
1577 return -1;
1578 }
1579 }
1580 if (hashtab_map(cladatum->permissions.table, validate_perm,
1581 cladatum2->permissions.table)) {
1582 ERR(NULL, " in access vector definition for class %s", key);
1583 return -1;
1584 }
1585 return 0;
1586 }
1587
1588 /* Clone the SID into the new SID table. */
clone_sid(sepol_security_id_t sid,context_struct_t * context,void * arg)1589 static int clone_sid(sepol_security_id_t sid,
1590 context_struct_t * context, void *arg)
1591 {
1592 sidtab_t *s = arg;
1593
1594 return sepol_sidtab_insert(s, sid, context);
1595 }
1596
convert_context_handle_invalid_context(context_struct_t * context)1597 static inline int convert_context_handle_invalid_context(context_struct_t *
1598 context)
1599 {
1600 if (selinux_enforcing) {
1601 return -EINVAL;
1602 } else {
1603 sepol_security_context_t s;
1604 size_t len;
1605
1606 context_to_string(NULL, policydb, context, &s, &len);
1607 ERR(NULL, "context %s is invalid", s);
1608 free(s);
1609 return 0;
1610 }
1611 }
1612
1613 typedef struct {
1614 policydb_t *oldp;
1615 policydb_t *newp;
1616 } convert_context_args_t;
1617
1618 /*
1619 * Convert the values in the security context
1620 * structure `c' from the values specified
1621 * in the policy `p->oldp' to the values specified
1622 * in the policy `p->newp'. Verify that the
1623 * context is valid under the new policy.
1624 */
convert_context(sepol_security_id_t key,context_struct_t * c,void * p)1625 static int convert_context(sepol_security_id_t key __attribute__ ((unused)),
1626 context_struct_t * c, void *p)
1627 {
1628 convert_context_args_t *args;
1629 context_struct_t oldc;
1630 role_datum_t *role;
1631 type_datum_t *typdatum;
1632 user_datum_t *usrdatum;
1633 sepol_security_context_t s;
1634 size_t len;
1635 int rc = -EINVAL;
1636
1637 args = (convert_context_args_t *) p;
1638
1639 if (context_cpy(&oldc, c))
1640 return -ENOMEM;
1641
1642 /* Convert the user. */
1643 usrdatum = (user_datum_t *) hashtab_search(args->newp->p_users.table,
1644 args->oldp->
1645 p_user_val_to_name[c->user -
1646 1]);
1647
1648 if (!usrdatum) {
1649 goto bad;
1650 }
1651 c->user = usrdatum->s.value;
1652
1653 /* Convert the role. */
1654 role = (role_datum_t *) hashtab_search(args->newp->p_roles.table,
1655 args->oldp->
1656 p_role_val_to_name[c->role - 1]);
1657 if (!role) {
1658 goto bad;
1659 }
1660 c->role = role->s.value;
1661
1662 /* Convert the type. */
1663 typdatum = (type_datum_t *)
1664 hashtab_search(args->newp->p_types.table,
1665 args->oldp->p_type_val_to_name[c->type - 1]);
1666 if (!typdatum) {
1667 goto bad;
1668 }
1669 c->type = typdatum->s.value;
1670
1671 rc = mls_convert_context(args->oldp, args->newp, c);
1672 if (rc)
1673 goto bad;
1674
1675 /* Check the validity of the new context. */
1676 if (!policydb_context_isvalid(args->newp, c)) {
1677 rc = convert_context_handle_invalid_context(&oldc);
1678 if (rc)
1679 goto bad;
1680 }
1681
1682 context_destroy(&oldc);
1683 return 0;
1684
1685 bad:
1686 context_to_string(NULL, policydb, &oldc, &s, &len);
1687 context_destroy(&oldc);
1688 ERR(NULL, "invalidating context %s", s);
1689 free(s);
1690 return rc;
1691 }
1692
1693 /* Reading from a policy "file". */
next_entry(void * buf,struct policy_file * fp,size_t bytes)1694 int hidden next_entry(void *buf, struct policy_file *fp, size_t bytes)
1695 {
1696 size_t nread;
1697
1698 switch (fp->type) {
1699 case PF_USE_STDIO:
1700 nread = fread(buf, bytes, 1, fp->fp);
1701
1702 if (nread != 1)
1703 return -1;
1704 break;
1705 case PF_USE_MEMORY:
1706 if (bytes > fp->len) {
1707 errno = EOVERFLOW;
1708 return -1;
1709 }
1710 memcpy(buf, fp->data, bytes);
1711 fp->data += bytes;
1712 fp->len -= bytes;
1713 break;
1714 default:
1715 errno = EINVAL;
1716 return -1;
1717 }
1718 return 0;
1719 }
1720
put_entry(const void * ptr,size_t size,size_t n,struct policy_file * fp)1721 size_t hidden put_entry(const void *ptr, size_t size, size_t n,
1722 struct policy_file *fp)
1723 {
1724 size_t bytes = size * n;
1725
1726 switch (fp->type) {
1727 case PF_USE_STDIO:
1728 return fwrite(ptr, size, n, fp->fp);
1729 case PF_USE_MEMORY:
1730 if (bytes > fp->len) {
1731 errno = ENOSPC;
1732 return 0;
1733 }
1734
1735 memcpy(fp->data, ptr, bytes);
1736 fp->data += bytes;
1737 fp->len -= bytes;
1738 return n;
1739 case PF_LEN:
1740 fp->len += bytes;
1741 return n;
1742 default:
1743 return 0;
1744 }
1745 return 0;
1746 }
1747
1748 /*
1749 * Reads a string and null terminates it from the policy file.
1750 * This is a port of str_read from the SE Linux kernel code.
1751 *
1752 * It returns:
1753 * 0 - Success
1754 * -1 - Failure with errno set
1755 */
str_read(char ** strp,struct policy_file * fp,size_t len)1756 int hidden str_read(char **strp, struct policy_file *fp, size_t len)
1757 {
1758 int rc;
1759 char *str;
1760
1761 if (zero_or_saturated(len)) {
1762 errno = EINVAL;
1763 return -1;
1764 }
1765
1766 str = malloc(len + 1);
1767 if (!str)
1768 return -1;
1769
1770 /* it's expected the caller should free the str */
1771 *strp = str;
1772
1773 /* next_entry sets errno */
1774 rc = next_entry(str, fp, len);
1775 if (rc)
1776 return rc;
1777
1778 str[len] = '\0';
1779 return 0;
1780 }
1781
1782 /*
1783 * Read a new set of configuration data from
1784 * a policy database binary representation file.
1785 *
1786 * Verify that each class that is defined under the
1787 * existing policy is still defined with the same
1788 * attributes in the new policy.
1789 *
1790 * Convert the context structures in the SID table to the
1791 * new representation and verify that all entries
1792 * in the SID table are valid under the new policy.
1793 *
1794 * Change the active policy database to use the new
1795 * configuration data.
1796 *
1797 * Reset the access vector cache.
1798 */
sepol_load_policy(void * data,size_t len)1799 int hidden sepol_load_policy(void *data, size_t len)
1800 {
1801 policydb_t oldpolicydb, newpolicydb;
1802 sidtab_t oldsidtab, newsidtab;
1803 convert_context_args_t args;
1804 int rc = 0;
1805 struct policy_file file, *fp;
1806
1807 policy_file_init(&file);
1808 file.type = PF_USE_MEMORY;
1809 file.data = data;
1810 file.len = len;
1811 fp = &file;
1812
1813 if (policydb_init(&newpolicydb))
1814 return -ENOMEM;
1815
1816 if (policydb_read(&newpolicydb, fp, 1)) {
1817 policydb_destroy(&mypolicydb);
1818 return -EINVAL;
1819 }
1820
1821 sepol_sidtab_init(&newsidtab);
1822
1823 /* Verify that the existing classes did not change. */
1824 if (hashtab_map
1825 (policydb->p_classes.table, validate_class, &newpolicydb)) {
1826 ERR(NULL, "the definition of an existing class changed");
1827 rc = -EINVAL;
1828 goto err;
1829 }
1830
1831 /* Clone the SID table. */
1832 sepol_sidtab_shutdown(sidtab);
1833 if (sepol_sidtab_map(sidtab, clone_sid, &newsidtab)) {
1834 rc = -ENOMEM;
1835 goto err;
1836 }
1837
1838 /* Convert the internal representations of contexts
1839 in the new SID table and remove invalid SIDs. */
1840 args.oldp = policydb;
1841 args.newp = &newpolicydb;
1842 sepol_sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1843
1844 /* Save the old policydb and SID table to free later. */
1845 memcpy(&oldpolicydb, policydb, sizeof *policydb);
1846 sepol_sidtab_set(&oldsidtab, sidtab);
1847
1848 /* Install the new policydb and SID table. */
1849 memcpy(policydb, &newpolicydb, sizeof *policydb);
1850 sepol_sidtab_set(sidtab, &newsidtab);
1851
1852 /* Free the old policydb and SID table. */
1853 policydb_destroy(&oldpolicydb);
1854 sepol_sidtab_destroy(&oldsidtab);
1855
1856 return 0;
1857
1858 err:
1859 sepol_sidtab_destroy(&newsidtab);
1860 policydb_destroy(&newpolicydb);
1861 return rc;
1862
1863 }
1864
1865 /*
1866 * Return the SIDs to use for an unlabeled file system
1867 * that is being mounted from the device with the
1868 * the kdevname `name'. The `fs_sid' SID is returned for
1869 * the file system and the `file_sid' SID is returned
1870 * for all files within that file system.
1871 */
sepol_fs_sid(char * name,sepol_security_id_t * fs_sid,sepol_security_id_t * file_sid)1872 int hidden sepol_fs_sid(char *name,
1873 sepol_security_id_t * fs_sid,
1874 sepol_security_id_t * file_sid)
1875 {
1876 int rc = 0;
1877 ocontext_t *c;
1878
1879 c = policydb->ocontexts[OCON_FS];
1880 while (c) {
1881 if (strcmp(c->u.name, name) == 0)
1882 break;
1883 c = c->next;
1884 }
1885
1886 if (c) {
1887 if (!c->sid[0] || !c->sid[1]) {
1888 rc = sepol_sidtab_context_to_sid(sidtab,
1889 &c->context[0],
1890 &c->sid[0]);
1891 if (rc)
1892 goto out;
1893 rc = sepol_sidtab_context_to_sid(sidtab,
1894 &c->context[1],
1895 &c->sid[1]);
1896 if (rc)
1897 goto out;
1898 }
1899 *fs_sid = c->sid[0];
1900 *file_sid = c->sid[1];
1901 } else {
1902 *fs_sid = SECINITSID_FS;
1903 *file_sid = SECINITSID_FILE;
1904 }
1905
1906 out:
1907 return rc;
1908 }
1909
1910 /*
1911 * Return the SID of the ibpkey specified by
1912 * `subnet prefix', and `pkey number'.
1913 */
sepol_ibpkey_sid(uint64_t subnet_prefix,uint16_t pkey,sepol_security_id_t * out_sid)1914 int hidden sepol_ibpkey_sid(uint64_t subnet_prefix,
1915 uint16_t pkey, sepol_security_id_t *out_sid)
1916 {
1917 ocontext_t *c;
1918 int rc = 0;
1919
1920 c = policydb->ocontexts[OCON_IBPKEY];
1921 while (c) {
1922 if (c->u.ibpkey.low_pkey <= pkey &&
1923 c->u.ibpkey.high_pkey >= pkey &&
1924 subnet_prefix == c->u.ibpkey.subnet_prefix)
1925 break;
1926 c = c->next;
1927 }
1928
1929 if (c) {
1930 if (!c->sid[0]) {
1931 rc = sepol_sidtab_context_to_sid(sidtab,
1932 &c->context[0],
1933 &c->sid[0]);
1934 if (rc)
1935 goto out;
1936 }
1937 *out_sid = c->sid[0];
1938 } else {
1939 *out_sid = SECINITSID_UNLABELED;
1940 }
1941
1942 out:
1943 return rc;
1944 }
1945
1946 /*
1947 * Return the SID of the subnet management interface specified by
1948 * `device name', and `port'.
1949 */
sepol_ibendport_sid(char * dev_name,uint8_t port,sepol_security_id_t * out_sid)1950 int hidden sepol_ibendport_sid(char *dev_name,
1951 uint8_t port,
1952 sepol_security_id_t *out_sid)
1953 {
1954 ocontext_t *c;
1955 int rc = 0;
1956
1957 c = policydb->ocontexts[OCON_IBENDPORT];
1958 while (c) {
1959 if (c->u.ibendport.port == port &&
1960 !strcmp(dev_name, c->u.ibendport.dev_name))
1961 break;
1962 c = c->next;
1963 }
1964
1965 if (c) {
1966 if (!c->sid[0]) {
1967 rc = sepol_sidtab_context_to_sid(sidtab,
1968 &c->context[0],
1969 &c->sid[0]);
1970 if (rc)
1971 goto out;
1972 }
1973 *out_sid = c->sid[0];
1974 } else {
1975 *out_sid = SECINITSID_UNLABELED;
1976 }
1977
1978 out:
1979 return rc;
1980 }
1981
1982
1983 /*
1984 * Return the SID of the port specified by
1985 * `domain', `type', `protocol', and `port'.
1986 */
sepol_port_sid(uint16_t domain,uint16_t type,uint8_t protocol,uint16_t port,sepol_security_id_t * out_sid)1987 int hidden sepol_port_sid(uint16_t domain __attribute__ ((unused)),
1988 uint16_t type __attribute__ ((unused)),
1989 uint8_t protocol,
1990 uint16_t port, sepol_security_id_t * out_sid)
1991 {
1992 ocontext_t *c;
1993 int rc = 0;
1994
1995 c = policydb->ocontexts[OCON_PORT];
1996 while (c) {
1997 if (c->u.port.protocol == protocol &&
1998 c->u.port.low_port <= port && c->u.port.high_port >= port)
1999 break;
2000 c = c->next;
2001 }
2002
2003 if (c) {
2004 if (!c->sid[0]) {
2005 rc = sepol_sidtab_context_to_sid(sidtab,
2006 &c->context[0],
2007 &c->sid[0]);
2008 if (rc)
2009 goto out;
2010 }
2011 *out_sid = c->sid[0];
2012 } else {
2013 *out_sid = SECINITSID_PORT;
2014 }
2015
2016 out:
2017 return rc;
2018 }
2019
2020 /*
2021 * Return the SIDs to use for a network interface
2022 * with the name `name'. The `if_sid' SID is returned for
2023 * the interface and the `msg_sid' SID is returned as
2024 * the default SID for messages received on the
2025 * interface.
2026 */
sepol_netif_sid(char * name,sepol_security_id_t * if_sid,sepol_security_id_t * msg_sid)2027 int hidden sepol_netif_sid(char *name,
2028 sepol_security_id_t * if_sid,
2029 sepol_security_id_t * msg_sid)
2030 {
2031 int rc = 0;
2032 ocontext_t *c;
2033
2034 c = policydb->ocontexts[OCON_NETIF];
2035 while (c) {
2036 if (strcmp(name, c->u.name) == 0)
2037 break;
2038 c = c->next;
2039 }
2040
2041 if (c) {
2042 if (!c->sid[0] || !c->sid[1]) {
2043 rc = sepol_sidtab_context_to_sid(sidtab,
2044 &c->context[0],
2045 &c->sid[0]);
2046 if (rc)
2047 goto out;
2048 rc = sepol_sidtab_context_to_sid(sidtab,
2049 &c->context[1],
2050 &c->sid[1]);
2051 if (rc)
2052 goto out;
2053 }
2054 *if_sid = c->sid[0];
2055 *msg_sid = c->sid[1];
2056 } else {
2057 *if_sid = SECINITSID_NETIF;
2058 *msg_sid = SECINITSID_NETMSG;
2059 }
2060
2061 out:
2062 return rc;
2063 }
2064
match_ipv6_addrmask(uint32_t * input,uint32_t * addr,uint32_t * mask)2065 static int match_ipv6_addrmask(uint32_t * input, uint32_t * addr,
2066 uint32_t * mask)
2067 {
2068 int i, fail = 0;
2069
2070 for (i = 0; i < 4; i++)
2071 if (addr[i] != (input[i] & mask[i])) {
2072 fail = 1;
2073 break;
2074 }
2075
2076 return !fail;
2077 }
2078
2079 /*
2080 * Return the SID of the node specified by the address
2081 * `addrp' where `addrlen' is the length of the address
2082 * in bytes and `domain' is the communications domain or
2083 * address family in which the address should be interpreted.
2084 */
sepol_node_sid(uint16_t domain,void * addrp,size_t addrlen,sepol_security_id_t * out_sid)2085 int hidden sepol_node_sid(uint16_t domain,
2086 void *addrp,
2087 size_t addrlen, sepol_security_id_t * out_sid)
2088 {
2089 int rc = 0;
2090 ocontext_t *c;
2091
2092 switch (domain) {
2093 case AF_INET:{
2094 uint32_t addr;
2095
2096 if (addrlen != sizeof(uint32_t)) {
2097 rc = -EINVAL;
2098 goto out;
2099 }
2100
2101 addr = *((uint32_t *) addrp);
2102
2103 c = policydb->ocontexts[OCON_NODE];
2104 while (c) {
2105 if (c->u.node.addr == (addr & c->u.node.mask))
2106 break;
2107 c = c->next;
2108 }
2109 break;
2110 }
2111
2112 case AF_INET6:
2113 if (addrlen != sizeof(uint64_t) * 2) {
2114 rc = -EINVAL;
2115 goto out;
2116 }
2117
2118 c = policydb->ocontexts[OCON_NODE6];
2119 while (c) {
2120 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2121 c->u.node6.mask))
2122 break;
2123 c = c->next;
2124 }
2125 break;
2126
2127 default:
2128 *out_sid = SECINITSID_NODE;
2129 goto out;
2130 }
2131
2132 if (c) {
2133 if (!c->sid[0]) {
2134 rc = sepol_sidtab_context_to_sid(sidtab,
2135 &c->context[0],
2136 &c->sid[0]);
2137 if (rc)
2138 goto out;
2139 }
2140 *out_sid = c->sid[0];
2141 } else {
2142 *out_sid = SECINITSID_NODE;
2143 }
2144
2145 out:
2146 return rc;
2147 }
2148
2149 /*
2150 * Generate the set of SIDs for legal security contexts
2151 * for a given user that can be reached by `fromsid'.
2152 * Set `*sids' to point to a dynamically allocated
2153 * array containing the set of SIDs. Set `*nel' to the
2154 * number of elements in the array.
2155 */
2156 #define SIDS_NEL 25
2157
sepol_get_user_sids(sepol_security_id_t fromsid,char * username,sepol_security_id_t ** sids,uint32_t * nel)2158 int hidden sepol_get_user_sids(sepol_security_id_t fromsid,
2159 char *username,
2160 sepol_security_id_t ** sids, uint32_t * nel)
2161 {
2162 context_struct_t *fromcon, usercon;
2163 sepol_security_id_t *mysids, *mysids2, sid;
2164 uint32_t mynel = 0, maxnel = SIDS_NEL;
2165 user_datum_t *user;
2166 role_datum_t *role;
2167 struct sepol_av_decision avd;
2168 int rc = 0;
2169 unsigned int i, j, reason;
2170 ebitmap_node_t *rnode, *tnode;
2171
2172 fromcon = sepol_sidtab_search(sidtab, fromsid);
2173 if (!fromcon) {
2174 rc = -EINVAL;
2175 goto out;
2176 }
2177
2178 user = (user_datum_t *) hashtab_search(policydb->p_users.table,
2179 username);
2180 if (!user) {
2181 rc = -EINVAL;
2182 goto out;
2183 }
2184 usercon.user = user->s.value;
2185
2186 mysids = malloc(maxnel * sizeof(sepol_security_id_t));
2187 if (!mysids) {
2188 rc = -ENOMEM;
2189 goto out;
2190 }
2191 memset(mysids, 0, maxnel * sizeof(sepol_security_id_t));
2192
2193 ebitmap_for_each_positive_bit(&user->roles.roles, rnode, i) {
2194 role = policydb->role_val_to_struct[i];
2195 usercon.role = i + 1;
2196 ebitmap_for_each_positive_bit(&role->types.types, tnode, j) {
2197 usercon.type = j + 1;
2198 if (usercon.type == fromcon->type)
2199 continue;
2200
2201 if (mls_setup_user_range
2202 (fromcon, user, &usercon, policydb->mls))
2203 continue;
2204
2205 rc = context_struct_compute_av(fromcon, &usercon,
2206 SECCLASS_PROCESS,
2207 PROCESS__TRANSITION,
2208 &avd, &reason, NULL, 0);
2209 if (rc || !(avd.allowed & PROCESS__TRANSITION))
2210 continue;
2211 rc = sepol_sidtab_context_to_sid(sidtab, &usercon,
2212 &sid);
2213 if (rc) {
2214 free(mysids);
2215 goto out;
2216 }
2217 if (mynel < maxnel) {
2218 mysids[mynel++] = sid;
2219 } else {
2220 maxnel += SIDS_NEL;
2221 mysids2 =
2222 malloc(maxnel *
2223 sizeof(sepol_security_id_t));
2224
2225 if (!mysids2) {
2226 rc = -ENOMEM;
2227 free(mysids);
2228 goto out;
2229 }
2230 memset(mysids2, 0,
2231 maxnel * sizeof(sepol_security_id_t));
2232 memcpy(mysids2, mysids,
2233 mynel * sizeof(sepol_security_id_t));
2234 free(mysids);
2235 mysids = mysids2;
2236 mysids[mynel++] = sid;
2237 }
2238 }
2239 }
2240
2241 *sids = mysids;
2242 *nel = mynel;
2243
2244 out:
2245 return rc;
2246 }
2247
2248 /*
2249 * Return the SID to use for a file in a filesystem
2250 * that cannot support a persistent label mapping or use another
2251 * fixed labeling behavior like transition SIDs or task SIDs.
2252 */
sepol_genfs_sid(const char * fstype,const char * path,sepol_security_class_t sclass,sepol_security_id_t * sid)2253 int hidden sepol_genfs_sid(const char *fstype,
2254 const char *path,
2255 sepol_security_class_t sclass,
2256 sepol_security_id_t * sid)
2257 {
2258 size_t len;
2259 genfs_t *genfs;
2260 ocontext_t *c;
2261 int rc = 0, cmp = 0;
2262
2263 for (genfs = policydb->genfs; genfs; genfs = genfs->next) {
2264 cmp = strcmp(fstype, genfs->fstype);
2265 if (cmp <= 0)
2266 break;
2267 }
2268
2269 if (!genfs || cmp) {
2270 *sid = SECINITSID_UNLABELED;
2271 rc = -ENOENT;
2272 goto out;
2273 }
2274
2275 for (c = genfs->head; c; c = c->next) {
2276 len = strlen(c->u.name);
2277 if ((!c->v.sclass || sclass == c->v.sclass) &&
2278 (strncmp(c->u.name, path, len) == 0))
2279 break;
2280 }
2281
2282 if (!c) {
2283 *sid = SECINITSID_UNLABELED;
2284 rc = -ENOENT;
2285 goto out;
2286 }
2287
2288 if (!c->sid[0]) {
2289 rc = sepol_sidtab_context_to_sid(sidtab,
2290 &c->context[0], &c->sid[0]);
2291 if (rc)
2292 goto out;
2293 }
2294
2295 *sid = c->sid[0];
2296 out:
2297 return rc;
2298 }
2299
sepol_fs_use(const char * fstype,unsigned int * behavior,sepol_security_id_t * sid)2300 int hidden sepol_fs_use(const char *fstype,
2301 unsigned int *behavior, sepol_security_id_t * sid)
2302 {
2303 int rc = 0;
2304 ocontext_t *c;
2305
2306 c = policydb->ocontexts[OCON_FSUSE];
2307 while (c) {
2308 if (strcmp(fstype, c->u.name) == 0)
2309 break;
2310 c = c->next;
2311 }
2312
2313 if (c) {
2314 *behavior = c->v.behavior;
2315 if (!c->sid[0]) {
2316 rc = sepol_sidtab_context_to_sid(sidtab,
2317 &c->context[0],
2318 &c->sid[0]);
2319 if (rc)
2320 goto out;
2321 }
2322 *sid = c->sid[0];
2323 } else {
2324 rc = sepol_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2325 if (rc) {
2326 *behavior = SECURITY_FS_USE_NONE;
2327 rc = 0;
2328 } else {
2329 *behavior = SECURITY_FS_USE_GENFS;
2330 }
2331 }
2332
2333 out:
2334 return rc;
2335 }
2336
2337 /* FLASK */
2338