1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/string.h>
11 #include <linux/spinlock.h>
12 #include <linux/slab.h>
15 #include "conditional.h"
19 * cond_evaluate_expr evaluates a conditional expr
20 * in reverse polish notation. It returns true (1), false (0),
21 * or undefined (-1). Undefined occurs when the expression
22 * exceeds the stack depth of COND_EXPR_MAXDEPTH.
24 static int cond_evaluate_expr(struct policydb *p, struct cond_expr *expr)
27 struct cond_expr *cur;
28 int s[COND_EXPR_MAXDEPTH];
31 for (cur = expr; cur; cur = cur->next) {
32 switch (cur->expr_type) {
34 if (sp == (COND_EXPR_MAXDEPTH - 1))
37 s[sp] = p->bool_val_to_struct[cur->bool - 1]->state;
66 s[sp] = (s[sp] == s[sp + 1]);
72 s[sp] = (s[sp] != s[sp + 1]);
82 * evaluate_cond_node evaluates the conditional stored in
83 * a struct cond_node and if the result is different than the
84 * current state of the node it sets the rules in the true/false
85 * list appropriately. If the result of the expression is undefined
86 * all of the rules are disabled for safety.
88 int evaluate_cond_node(struct policydb *p, struct cond_node *node)
91 struct cond_av_list *cur;
93 new_state = cond_evaluate_expr(p, node->expr);
94 if (new_state != node->cur_state) {
95 node->cur_state = new_state;
97 pr_err("SELinux: expression result was undefined - disabling all rules.\n");
98 /* turn the rules on or off */
99 for (cur = node->true_list; cur; cur = cur->next) {
101 cur->node->key.specified &= ~AVTAB_ENABLED;
103 cur->node->key.specified |= AVTAB_ENABLED;
106 for (cur = node->false_list; cur; cur = cur->next) {
109 cur->node->key.specified &= ~AVTAB_ENABLED;
111 cur->node->key.specified |= AVTAB_ENABLED;
117 int cond_policydb_init(struct policydb *p)
121 p->bool_val_to_struct = NULL;
124 rc = avtab_init(&p->te_cond_avtab);
131 static void cond_av_list_destroy(struct cond_av_list *list)
133 struct cond_av_list *cur, *next;
134 for (cur = list; cur; cur = next) {
136 /* the avtab_ptr_t node is destroy by the avtab */
141 static void cond_node_destroy(struct cond_node *node)
143 struct cond_expr *cur_expr, *next_expr;
145 for (cur_expr = node->expr; cur_expr; cur_expr = next_expr) {
146 next_expr = cur_expr->next;
149 cond_av_list_destroy(node->true_list);
150 cond_av_list_destroy(node->false_list);
154 static void cond_list_destroy(struct cond_node *list)
156 struct cond_node *next, *cur;
161 for (cur = list; cur; cur = next) {
163 cond_node_destroy(cur);
167 void cond_policydb_destroy(struct policydb *p)
169 kfree(p->bool_val_to_struct);
170 avtab_destroy(&p->te_cond_avtab);
171 cond_list_destroy(p->cond_list);
174 int cond_init_bool_indexes(struct policydb *p)
176 kfree(p->bool_val_to_struct);
177 p->bool_val_to_struct = kmalloc_array(p->p_bools.nprim,
178 sizeof(*p->bool_val_to_struct),
180 if (!p->bool_val_to_struct)
185 int cond_destroy_bool(void *key, void *datum, void *p)
192 int cond_index_bool(void *key, void *datum, void *datap)
195 struct cond_bool_datum *booldatum;
200 if (!booldatum->value || booldatum->value > p->p_bools.nprim)
203 p->sym_val_to_name[SYM_BOOLS][booldatum->value - 1] = key;
204 p->bool_val_to_struct[booldatum->value - 1] = booldatum;
209 static int bool_isvalid(struct cond_bool_datum *b)
211 if (!(b->state == 0 || b->state == 1))
216 int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
219 struct cond_bool_datum *booldatum;
224 booldatum = kzalloc(sizeof(*booldatum), GFP_KERNEL);
228 rc = next_entry(buf, fp, sizeof buf);
232 booldatum->value = le32_to_cpu(buf[0]);
233 booldatum->state = le32_to_cpu(buf[1]);
236 if (!bool_isvalid(booldatum))
239 len = le32_to_cpu(buf[2]);
240 if (((len == 0) || (len == (u32)-1)))
244 key = kmalloc(len + 1, GFP_KERNEL);
247 rc = next_entry(key, fp, len);
251 rc = hashtab_insert(h, key, booldatum);
257 cond_destroy_bool(key, booldatum, NULL);
261 struct cond_insertf_data {
263 struct cond_av_list *other;
264 struct cond_av_list *head;
265 struct cond_av_list *tail;
268 static int cond_insertf(struct avtab *a, struct avtab_key *k, struct avtab_datum *d, void *ptr)
270 struct cond_insertf_data *data = ptr;
271 struct policydb *p = data->p;
272 struct cond_av_list *other = data->other, *list, *cur;
273 struct avtab_node *node_ptr;
278 * For type rules we have to make certain there aren't any
279 * conflicting rules by searching the te_avtab and the
282 if (k->specified & AVTAB_TYPE) {
283 if (avtab_search(&p->te_avtab, k)) {
284 pr_err("SELinux: type rule already exists outside of a conditional.\n");
288 * If we are reading the false list other will be a pointer to
289 * the true list. We can have duplicate entries if there is only
290 * 1 other entry and it is in our true list.
292 * If we are reading the true list (other == NULL) there shouldn't
293 * be any other entries.
296 node_ptr = avtab_search_node(&p->te_cond_avtab, k);
298 if (avtab_search_node_next(node_ptr, k->specified)) {
299 pr_err("SELinux: too many conflicting type rules.\n");
303 for (cur = other; cur; cur = cur->next) {
304 if (cur->node == node_ptr) {
310 pr_err("SELinux: conflicting type rules.\n");
315 if (avtab_search(&p->te_cond_avtab, k)) {
316 pr_err("SELinux: conflicting type rules when adding type rule for true.\n");
322 node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d);
324 pr_err("SELinux: could not insert rule.\n");
329 list = kzalloc(sizeof(*list), GFP_KERNEL);
335 list->node = node_ptr;
339 data->tail->next = list;
344 cond_av_list_destroy(data->head);
349 static int cond_read_av_list(struct policydb *p, void *fp, struct cond_av_list **ret_list, struct cond_av_list *other)
354 struct cond_insertf_data data;
358 rc = next_entry(buf, fp, sizeof(u32));
362 len = le32_to_cpu(buf[0]);
370 for (i = 0; i < len; i++) {
371 rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf,
377 *ret_list = data.head;
381 static int expr_isvalid(struct policydb *p, struct cond_expr *expr)
383 if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
384 pr_err("SELinux: conditional expressions uses unknown operator.\n");
388 if (expr->bool > p->p_bools.nprim) {
389 pr_err("SELinux: conditional expressions uses unknown bool.\n");
395 static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
400 struct cond_expr *expr = NULL, *last = NULL;
402 rc = next_entry(buf, fp, sizeof(u32) * 2);
406 node->cur_state = le32_to_cpu(buf[0]);
409 len = le32_to_cpu(buf[1]);
411 for (i = 0; i < len; i++) {
412 rc = next_entry(buf, fp, sizeof(u32) * 2);
417 expr = kzalloc(sizeof(*expr), GFP_KERNEL);
421 expr->expr_type = le32_to_cpu(buf[0]);
422 expr->bool = le32_to_cpu(buf[1]);
424 if (!expr_isvalid(p, expr)) {
437 rc = cond_read_av_list(p, fp, &node->true_list, NULL);
440 rc = cond_read_av_list(p, fp, &node->false_list, node->true_list);
445 cond_node_destroy(node);
449 int cond_read_list(struct policydb *p, void *fp)
451 struct cond_node *node, *last = NULL;
456 rc = next_entry(buf, fp, sizeof buf);
460 len = le32_to_cpu(buf[0]);
462 rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel);
466 for (i = 0; i < len; i++) {
468 node = kzalloc(sizeof(*node), GFP_KERNEL);
472 rc = cond_read_node(p, node, fp);
484 cond_list_destroy(p->cond_list);
489 int cond_write_bool(void *vkey, void *datum, void *ptr)
492 struct cond_bool_datum *booldatum = datum;
493 struct policy_data *pd = ptr;
500 buf[0] = cpu_to_le32(booldatum->value);
501 buf[1] = cpu_to_le32(booldatum->state);
502 buf[2] = cpu_to_le32(len);
503 rc = put_entry(buf, sizeof(u32), 3, fp);
506 rc = put_entry(key, 1, len, fp);
513 * cond_write_cond_av_list doesn't write out the av_list nodes.
514 * Instead it writes out the key/value pairs from the avtab. This
515 * is necessary because there is no way to uniquely identifying rules
516 * in the avtab so it is not possible to associate individual rules
517 * in the avtab with a conditional without saving them as part of
518 * the conditional. This means that the avtab with the conditional
519 * rules will not be saved but will be rebuilt on policy load.
521 static int cond_write_av_list(struct policydb *p,
522 struct cond_av_list *list, struct policy_file *fp)
525 struct cond_av_list *cur_list;
530 for (cur_list = list; cur_list != NULL; cur_list = cur_list->next)
533 buf[0] = cpu_to_le32(len);
534 rc = put_entry(buf, sizeof(u32), 1, fp);
541 for (cur_list = list; cur_list != NULL; cur_list = cur_list->next) {
542 rc = avtab_write_item(p, cur_list->node, fp);
550 static int cond_write_node(struct policydb *p, struct cond_node *node,
551 struct policy_file *fp)
553 struct cond_expr *cur_expr;
558 buf[0] = cpu_to_le32(node->cur_state);
559 rc = put_entry(buf, sizeof(u32), 1, fp);
563 for (cur_expr = node->expr; cur_expr != NULL; cur_expr = cur_expr->next)
566 buf[0] = cpu_to_le32(len);
567 rc = put_entry(buf, sizeof(u32), 1, fp);
571 for (cur_expr = node->expr; cur_expr != NULL; cur_expr = cur_expr->next) {
572 buf[0] = cpu_to_le32(cur_expr->expr_type);
573 buf[1] = cpu_to_le32(cur_expr->bool);
574 rc = put_entry(buf, sizeof(u32), 2, fp);
579 rc = cond_write_av_list(p, node->true_list, fp);
582 rc = cond_write_av_list(p, node->false_list, fp);
589 int cond_write_list(struct policydb *p, struct cond_node *list, void *fp)
591 struct cond_node *cur;
597 for (cur = list; cur != NULL; cur = cur->next)
599 buf[0] = cpu_to_le32(len);
600 rc = put_entry(buf, sizeof(u32), 1, fp);
604 for (cur = list; cur != NULL; cur = cur->next) {
605 rc = cond_write_node(p, cur, fp);
613 void cond_compute_xperms(struct avtab *ctab, struct avtab_key *key,
614 struct extended_perms_decision *xpermd)
616 struct avtab_node *node;
618 if (!ctab || !key || !xpermd)
621 for (node = avtab_search_node(ctab, key); node;
622 node = avtab_search_node_next(node, key->specified)) {
623 if (node->key.specified & AVTAB_ENABLED)
624 services_compute_xperms_decision(xpermd, node);
629 /* Determine whether additional permissions are granted by the conditional
630 * av table, and if so, add them to the result
632 void cond_compute_av(struct avtab *ctab, struct avtab_key *key,
633 struct av_decision *avd, struct extended_perms *xperms)
635 struct avtab_node *node;
637 if (!ctab || !key || !avd)
640 for (node = avtab_search_node(ctab, key); node;
641 node = avtab_search_node_next(node, key->specified)) {
642 if ((u16)(AVTAB_ALLOWED|AVTAB_ENABLED) ==
643 (node->key.specified & (AVTAB_ALLOWED|AVTAB_ENABLED)))
644 avd->allowed |= node->datum.u.data;
645 if ((u16)(AVTAB_AUDITDENY|AVTAB_ENABLED) ==
646 (node->key.specified & (AVTAB_AUDITDENY|AVTAB_ENABLED)))
647 /* Since a '0' in an auditdeny mask represents a
648 * permission we do NOT want to audit (dontaudit), we use
649 * the '&' operand to ensure that all '0's in the mask
650 * are retained (much unlike the allow and auditallow cases).
652 avd->auditdeny &= node->datum.u.data;
653 if ((u16)(AVTAB_AUDITALLOW|AVTAB_ENABLED) ==
654 (node->key.specified & (AVTAB_AUDITALLOW|AVTAB_ENABLED)))
655 avd->auditallow |= node->datum.u.data;
656 if (xperms && (node->key.specified & AVTAB_ENABLED) &&
657 (node->key.specified & AVTAB_XPERMS))
658 services_compute_xperms_drivers(xperms, node);