1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Authors: Karl MacMillan <kmacmillan@tresys.com>
3 *	    Frank Mayer <mayerf@tresys.com>
4 *          Copyright (C) 2003 - 2004 Tresys Technology, LLC
5 */
6
7#include <linux/kernel.h>
8#include <linux/errno.h>
9#include <linux/string.h>
10#include <linux/spinlock.h>
11#include <linux/slab.h>
12
13#include "security.h"
14#include "conditional.h"
15#include "services.h"
16
17/*
18 * cond_evaluate_expr evaluates a conditional expr
19 * in reverse polish notation. It returns true (1), false (0),
20 * or undefined (-1). Undefined occurs when the expression
21 * exceeds the stack depth of COND_EXPR_MAXDEPTH.
22 */
23static int cond_evaluate_expr(struct policydb *p, struct cond_expr *expr)
24{
25	u32 i;
26	int s[COND_EXPR_MAXDEPTH];
27	int sp = -1;
28
29	if (expr->len == 0)
30		return -1;
31
32	for (i = 0; i < expr->len; i++) {
33		struct cond_expr_node *node = &expr->nodes[i];
34
35		switch (node->expr_type) {
36		case COND_BOOL:
37			if (sp == (COND_EXPR_MAXDEPTH - 1))
38				return -1;
39			sp++;
40			s[sp] = p->bool_val_to_struct[node->boolean - 1]->state;
41			break;
42		case COND_NOT:
43			if (sp < 0)
44				return -1;
45			s[sp] = !s[sp];
46			break;
47		case COND_OR:
48			if (sp < 1)
49				return -1;
50			sp--;
51			s[sp] |= s[sp + 1];
52			break;
53		case COND_AND:
54			if (sp < 1)
55				return -1;
56			sp--;
57			s[sp] &= s[sp + 1];
58			break;
59		case COND_XOR:
60			if (sp < 1)
61				return -1;
62			sp--;
63			s[sp] ^= s[sp + 1];
64			break;
65		case COND_EQ:
66			if (sp < 1)
67				return -1;
68			sp--;
69			s[sp] = (s[sp] == s[sp + 1]);
70			break;
71		case COND_NEQ:
72			if (sp < 1)
73				return -1;
74			sp--;
75			s[sp] = (s[sp] != s[sp + 1]);
76			break;
77		default:
78			return -1;
79		}
80	}
81	return s[0];
82}
83
84/*
85 * evaluate_cond_node evaluates the conditional stored in
86 * a struct cond_node and if the result is different than the
87 * current state of the node it sets the rules in the true/false
88 * list appropriately. If the result of the expression is undefined
89 * all of the rules are disabled for safety.
90 */
91static void evaluate_cond_node(struct policydb *p, struct cond_node *node)
92{
93	struct avtab_node *avnode;
94	int new_state;
95	u32 i;
96
97	new_state = cond_evaluate_expr(p, &node->expr);
98	if (new_state != node->cur_state) {
99		node->cur_state = new_state;
100		if (new_state == -1)
101			pr_err("SELinux: expression result was undefined - disabling all rules.\n");
102		/* turn the rules on or off */
103		for (i = 0; i < node->true_list.len; i++) {
104			avnode = node->true_list.nodes[i];
105			if (new_state <= 0)
106				avnode->key.specified &= ~AVTAB_ENABLED;
107			else
108				avnode->key.specified |= AVTAB_ENABLED;
109		}
110
111		for (i = 0; i < node->false_list.len; i++) {
112			avnode = node->false_list.nodes[i];
113			/* -1 or 1 */
114			if (new_state)
115				avnode->key.specified &= ~AVTAB_ENABLED;
116			else
117				avnode->key.specified |= AVTAB_ENABLED;
118		}
119	}
120}
121
122void evaluate_cond_nodes(struct policydb *p)
123{
124	u32 i;
125
126	for (i = 0; i < p->cond_list_len; i++)
127		evaluate_cond_node(p, &p->cond_list[i]);
128}
129
130void cond_policydb_init(struct policydb *p)
131{
132	p->bool_val_to_struct = NULL;
133	p->cond_list = NULL;
134	p->cond_list_len = 0;
135
136	avtab_init(&p->te_cond_avtab);
137}
138
139static void cond_node_destroy(struct cond_node *node)
140{
141	kfree(node->expr.nodes);
142	/* the avtab_ptr_t nodes are destroyed by the avtab */
143	kfree(node->true_list.nodes);
144	kfree(node->false_list.nodes);
145}
146
147static void cond_list_destroy(struct policydb *p)
148{
149	u32 i;
150
151	for (i = 0; i < p->cond_list_len; i++)
152		cond_node_destroy(&p->cond_list[i]);
153	kfree(p->cond_list);
154	p->cond_list = NULL;
155	p->cond_list_len = 0;
156}
157
158void cond_policydb_destroy(struct policydb *p)
159{
160	kfree(p->bool_val_to_struct);
161	avtab_destroy(&p->te_cond_avtab);
162	cond_list_destroy(p);
163}
164
165int cond_init_bool_indexes(struct policydb *p)
166{
167	kfree(p->bool_val_to_struct);
168	p->bool_val_to_struct = kmalloc_array(
169		p->p_bools.nprim, sizeof(*p->bool_val_to_struct), GFP_KERNEL);
170	if (!p->bool_val_to_struct)
171		return -ENOMEM;
172	return 0;
173}
174
175int cond_destroy_bool(void *key, void *datum, void *p)
176{
177	kfree(key);
178	kfree(datum);
179	return 0;
180}
181
182int cond_index_bool(void *key, void *datum, void *datap)
183{
184	struct policydb *p;
185	struct cond_bool_datum *booldatum;
186
187	booldatum = datum;
188	p = datap;
189
190	if (!booldatum->value || booldatum->value > p->p_bools.nprim)
191		return -EINVAL;
192
193	p->sym_val_to_name[SYM_BOOLS][booldatum->value - 1] = key;
194	p->bool_val_to_struct[booldatum->value - 1] = booldatum;
195
196	return 0;
197}
198
199static int bool_isvalid(struct cond_bool_datum *b)
200{
201	if (!(b->state == 0 || b->state == 1))
202		return 0;
203	return 1;
204}
205
206int cond_read_bool(struct policydb *p, struct symtab *s, void *fp)
207{
208	char *key = NULL;
209	struct cond_bool_datum *booldatum;
210	__le32 buf[3];
211	u32 len;
212	int rc;
213
214	booldatum = kzalloc(sizeof(*booldatum), GFP_KERNEL);
215	if (!booldatum)
216		return -ENOMEM;
217
218	rc = next_entry(buf, fp, sizeof(buf));
219	if (rc)
220		goto err;
221
222	booldatum->value = le32_to_cpu(buf[0]);
223	booldatum->state = le32_to_cpu(buf[1]);
224
225	rc = -EINVAL;
226	if (!bool_isvalid(booldatum))
227		goto err;
228
229	len = le32_to_cpu(buf[2]);
230	if (((len == 0) || (len == (u32)-1)))
231		goto err;
232
233	rc = -ENOMEM;
234	key = kmalloc(len + 1, GFP_KERNEL);
235	if (!key)
236		goto err;
237	rc = next_entry(key, fp, len);
238	if (rc)
239		goto err;
240	key[len] = '\0';
241	rc = symtab_insert(s, key, booldatum);
242	if (rc)
243		goto err;
244
245	return 0;
246err:
247	cond_destroy_bool(key, booldatum, NULL);
248	return rc;
249}
250
251struct cond_insertf_data {
252	struct policydb *p;
253	struct avtab_node **dst;
254	struct cond_av_list *other;
255};
256
257static int cond_insertf(struct avtab *a, const struct avtab_key *k,
258			const struct avtab_datum *d, void *ptr)
259{
260	struct cond_insertf_data *data = ptr;
261	struct policydb *p = data->p;
262	struct cond_av_list *other = data->other;
263	struct avtab_node *node_ptr;
264	u32 i;
265	bool found;
266
267	/*
268	 * For type rules we have to make certain there aren't any
269	 * conflicting rules by searching the te_avtab and the
270	 * cond_te_avtab.
271	 */
272	if (k->specified & AVTAB_TYPE) {
273		if (avtab_search_node(&p->te_avtab, k)) {
274			pr_err("SELinux: type rule already exists outside of a conditional.\n");
275			return -EINVAL;
276		}
277		/*
278		 * If we are reading the false list other will be a pointer to
279		 * the true list. We can have duplicate entries if there is only
280		 * 1 other entry and it is in our true list.
281		 *
282		 * If we are reading the true list (other == NULL) there shouldn't
283		 * be any other entries.
284		 */
285		if (other) {
286			node_ptr = avtab_search_node(&p->te_cond_avtab, k);
287			if (node_ptr) {
288				if (avtab_search_node_next(node_ptr,
289							   k->specified)) {
290					pr_err("SELinux: too many conflicting type rules.\n");
291					return -EINVAL;
292				}
293				found = false;
294				for (i = 0; i < other->len; i++) {
295					if (other->nodes[i] == node_ptr) {
296						found = true;
297						break;
298					}
299				}
300				if (!found) {
301					pr_err("SELinux: conflicting type rules.\n");
302					return -EINVAL;
303				}
304			}
305		} else {
306			if (avtab_search_node(&p->te_cond_avtab, k)) {
307				pr_err("SELinux: conflicting type rules when adding type rule for true.\n");
308				return -EINVAL;
309			}
310		}
311	}
312
313	node_ptr = avtab_insert_nonunique(&p->te_cond_avtab, k, d);
314	if (!node_ptr) {
315		pr_err("SELinux: could not insert rule.\n");
316		return -ENOMEM;
317	}
318
319	*data->dst = node_ptr;
320	return 0;
321}
322
323static int cond_read_av_list(struct policydb *p, void *fp,
324			     struct cond_av_list *list,
325			     struct cond_av_list *other)
326{
327	int rc;
328	__le32 buf[1];
329	u32 i, len;
330	struct cond_insertf_data data;
331
332	rc = next_entry(buf, fp, sizeof(u32));
333	if (rc)
334		return rc;
335
336	len = le32_to_cpu(buf[0]);
337	if (len == 0)
338		return 0;
339
340	list->nodes = kcalloc(len, sizeof(*list->nodes), GFP_KERNEL);
341	if (!list->nodes)
342		return -ENOMEM;
343
344	data.p = p;
345	data.other = other;
346	for (i = 0; i < len; i++) {
347		data.dst = &list->nodes[i];
348		rc = avtab_read_item(&p->te_cond_avtab, fp, p, cond_insertf,
349				     &data);
350		if (rc) {
351			kfree(list->nodes);
352			list->nodes = NULL;
353			return rc;
354		}
355	}
356
357	list->len = len;
358	return 0;
359}
360
361static int expr_node_isvalid(struct policydb *p, struct cond_expr_node *expr)
362{
363	if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
364		pr_err("SELinux: conditional expressions uses unknown operator.\n");
365		return 0;
366	}
367
368	if (expr->boolean > p->p_bools.nprim) {
369		pr_err("SELinux: conditional expressions uses unknown bool.\n");
370		return 0;
371	}
372	return 1;
373}
374
375static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
376{
377	__le32 buf[2];
378	u32 i, len;
379	int rc;
380
381	rc = next_entry(buf, fp, sizeof(u32) * 2);
382	if (rc)
383		return rc;
384
385	node->cur_state = le32_to_cpu(buf[0]);
386
387	/* expr */
388	len = le32_to_cpu(buf[1]);
389	node->expr.nodes = kcalloc(len, sizeof(*node->expr.nodes), GFP_KERNEL);
390	if (!node->expr.nodes)
391		return -ENOMEM;
392
393	node->expr.len = len;
394
395	for (i = 0; i < len; i++) {
396		struct cond_expr_node *expr = &node->expr.nodes[i];
397
398		rc = next_entry(buf, fp, sizeof(u32) * 2);
399		if (rc)
400			return rc;
401
402		expr->expr_type = le32_to_cpu(buf[0]);
403		expr->boolean = le32_to_cpu(buf[1]);
404
405		if (!expr_node_isvalid(p, expr))
406			return -EINVAL;
407	}
408
409	rc = cond_read_av_list(p, fp, &node->true_list, NULL);
410	if (rc)
411		return rc;
412	return cond_read_av_list(p, fp, &node->false_list, &node->true_list);
413}
414
415int cond_read_list(struct policydb *p, void *fp)
416{
417	__le32 buf[1];
418	u32 i, len;
419	int rc;
420
421	rc = next_entry(buf, fp, sizeof(buf));
422	if (rc)
423		return rc;
424
425	len = le32_to_cpu(buf[0]);
426
427	p->cond_list = kcalloc(len, sizeof(*p->cond_list), GFP_KERNEL);
428	if (!p->cond_list)
429		return -ENOMEM;
430
431	rc = avtab_alloc(&(p->te_cond_avtab), p->te_avtab.nel);
432	if (rc)
433		goto err;
434
435	p->cond_list_len = len;
436
437	for (i = 0; i < len; i++) {
438		rc = cond_read_node(p, &p->cond_list[i], fp);
439		if (rc)
440			goto err;
441	}
442	return 0;
443err:
444	cond_list_destroy(p);
445	return rc;
446}
447
448int cond_write_bool(void *vkey, void *datum, void *ptr)
449{
450	char *key = vkey;
451	struct cond_bool_datum *booldatum = datum;
452	struct policy_data *pd = ptr;
453	void *fp = pd->fp;
454	__le32 buf[3];
455	u32 len;
456	int rc;
457
458	len = strlen(key);
459	buf[0] = cpu_to_le32(booldatum->value);
460	buf[1] = cpu_to_le32(booldatum->state);
461	buf[2] = cpu_to_le32(len);
462	rc = put_entry(buf, sizeof(u32), 3, fp);
463	if (rc)
464		return rc;
465	rc = put_entry(key, 1, len, fp);
466	if (rc)
467		return rc;
468	return 0;
469}
470
471/*
472 * cond_write_cond_av_list doesn't write out the av_list nodes.
473 * Instead it writes out the key/value pairs from the avtab. This
474 * is necessary because there is no way to uniquely identifying rules
475 * in the avtab so it is not possible to associate individual rules
476 * in the avtab with a conditional without saving them as part of
477 * the conditional. This means that the avtab with the conditional
478 * rules will not be saved but will be rebuilt on policy load.
479 */
480static int cond_write_av_list(struct policydb *p, struct cond_av_list *list,
481			      struct policy_file *fp)
482{
483	__le32 buf[1];
484	u32 i;
485	int rc;
486
487	buf[0] = cpu_to_le32(list->len);
488	rc = put_entry(buf, sizeof(u32), 1, fp);
489	if (rc)
490		return rc;
491
492	for (i = 0; i < list->len; i++) {
493		rc = avtab_write_item(p, list->nodes[i], fp);
494		if (rc)
495			return rc;
496	}
497
498	return 0;
499}
500
501static int cond_write_node(struct policydb *p, struct cond_node *node,
502			   struct policy_file *fp)
503{
504	__le32 buf[2];
505	int rc;
506	u32 i;
507
508	buf[0] = cpu_to_le32(node->cur_state);
509	rc = put_entry(buf, sizeof(u32), 1, fp);
510	if (rc)
511		return rc;
512
513	buf[0] = cpu_to_le32(node->expr.len);
514	rc = put_entry(buf, sizeof(u32), 1, fp);
515	if (rc)
516		return rc;
517
518	for (i = 0; i < node->expr.len; i++) {
519		buf[0] = cpu_to_le32(node->expr.nodes[i].expr_type);
520		buf[1] = cpu_to_le32(node->expr.nodes[i].boolean);
521		rc = put_entry(buf, sizeof(u32), 2, fp);
522		if (rc)
523			return rc;
524	}
525
526	rc = cond_write_av_list(p, &node->true_list, fp);
527	if (rc)
528		return rc;
529	rc = cond_write_av_list(p, &node->false_list, fp);
530	if (rc)
531		return rc;
532
533	return 0;
534}
535
536int cond_write_list(struct policydb *p, void *fp)
537{
538	u32 i;
539	__le32 buf[1];
540	int rc;
541
542	buf[0] = cpu_to_le32(p->cond_list_len);
543	rc = put_entry(buf, sizeof(u32), 1, fp);
544	if (rc)
545		return rc;
546
547	for (i = 0; i < p->cond_list_len; i++) {
548		rc = cond_write_node(p, &p->cond_list[i], fp);
549		if (rc)
550			return rc;
551	}
552
553	return 0;
554}
555
556void cond_compute_xperms(struct avtab *ctab, struct avtab_key *key,
557			 struct extended_perms_decision *xpermd)
558{
559	struct avtab_node *node;
560
561	if (!ctab || !key || !xpermd)
562		return;
563
564	for (node = avtab_search_node(ctab, key); node;
565	     node = avtab_search_node_next(node, key->specified)) {
566		if (node->key.specified & AVTAB_ENABLED)
567			services_compute_xperms_decision(xpermd, node);
568	}
569}
570/* Determine whether additional permissions are granted by the conditional
571 * av table, and if so, add them to the result
572 */
573void cond_compute_av(struct avtab *ctab, struct avtab_key *key,
574		     struct av_decision *avd, struct extended_perms *xperms)
575{
576	struct avtab_node *node;
577
578	if (!ctab || !key || !avd)
579		return;
580
581	for (node = avtab_search_node(ctab, key); node;
582	     node = avtab_search_node_next(node, key->specified)) {
583		if ((u16)(AVTAB_ALLOWED | AVTAB_ENABLED) ==
584		    (node->key.specified & (AVTAB_ALLOWED | AVTAB_ENABLED)))
585			avd->allowed |= node->datum.u.data;
586		if ((u16)(AVTAB_AUDITDENY | AVTAB_ENABLED) ==
587		    (node->key.specified & (AVTAB_AUDITDENY | AVTAB_ENABLED)))
588			/* Since a '0' in an auditdeny mask represents a
589			 * permission we do NOT want to audit (dontaudit), we use
590			 * the '&' operand to ensure that all '0's in the mask
591			 * are retained (much unlike the allow and auditallow cases).
592			 */
593			avd->auditdeny &= node->datum.u.data;
594		if ((u16)(AVTAB_AUDITALLOW | AVTAB_ENABLED) ==
595		    (node->key.specified & (AVTAB_AUDITALLOW | AVTAB_ENABLED)))
596			avd->auditallow |= node->datum.u.data;
597		if (xperms && (node->key.specified & AVTAB_ENABLED) &&
598		    (node->key.specified & AVTAB_XPERMS))
599			services_compute_xperms_drivers(xperms, node);
600	}
601}
602
603static int cond_dup_av_list(struct cond_av_list *new, struct cond_av_list *orig,
604			    struct avtab *avtab)
605{
606	u32 i;
607
608	memset(new, 0, sizeof(*new));
609
610	new->nodes = kcalloc(orig->len, sizeof(*new->nodes), GFP_KERNEL);
611	if (!new->nodes)
612		return -ENOMEM;
613
614	for (i = 0; i < orig->len; i++) {
615		new->nodes[i] = avtab_insert_nonunique(
616			avtab, &orig->nodes[i]->key, &orig->nodes[i]->datum);
617		if (!new->nodes[i])
618			return -ENOMEM;
619		new->len++;
620	}
621
622	return 0;
623}
624
625static int duplicate_policydb_cond_list(struct policydb *newp,
626					struct policydb *origp)
627{
628	int rc;
629	u32 i;
630
631	rc = avtab_alloc_dup(&newp->te_cond_avtab, &origp->te_cond_avtab);
632	if (rc)
633		return rc;
634
635	newp->cond_list_len = 0;
636	newp->cond_list = kcalloc(origp->cond_list_len,
637				  sizeof(*newp->cond_list), GFP_KERNEL);
638	if (!newp->cond_list)
639		goto error;
640
641	for (i = 0; i < origp->cond_list_len; i++) {
642		struct cond_node *newn = &newp->cond_list[i];
643		struct cond_node *orign = &origp->cond_list[i];
644
645		newp->cond_list_len++;
646
647		newn->cur_state = orign->cur_state;
648		newn->expr.nodes =
649			kmemdup(orign->expr.nodes,
650				orign->expr.len * sizeof(*orign->expr.nodes),
651				GFP_KERNEL);
652		if (!newn->expr.nodes)
653			goto error;
654
655		newn->expr.len = orign->expr.len;
656
657		rc = cond_dup_av_list(&newn->true_list, &orign->true_list,
658				      &newp->te_cond_avtab);
659		if (rc)
660			goto error;
661
662		rc = cond_dup_av_list(&newn->false_list, &orign->false_list,
663				      &newp->te_cond_avtab);
664		if (rc)
665			goto error;
666	}
667
668	return 0;
669
670error:
671	avtab_destroy(&newp->te_cond_avtab);
672	cond_list_destroy(newp);
673	return -ENOMEM;
674}
675
676static int cond_bools_destroy(void *key, void *datum, void *args)
677{
678	/* key was not copied so no need to free here */
679	kfree(datum);
680	return 0;
681}
682
683static int cond_bools_copy(struct hashtab_node *new, struct hashtab_node *orig,
684			   void *args)
685{
686	struct cond_bool_datum *datum;
687
688	datum = kmemdup(orig->datum, sizeof(struct cond_bool_datum),
689			GFP_KERNEL);
690	if (!datum)
691		return -ENOMEM;
692
693	new->key = orig->key; /* No need to copy, never modified */
694	new->datum = datum;
695	return 0;
696}
697
698static int cond_bools_index(void *key, void *datum, void *args)
699{
700	struct cond_bool_datum *booldatum, **cond_bool_array;
701
702	booldatum = datum;
703	cond_bool_array = args;
704	cond_bool_array[booldatum->value - 1] = booldatum;
705
706	return 0;
707}
708
709static int duplicate_policydb_bools(struct policydb *newdb,
710				    struct policydb *orig)
711{
712	struct cond_bool_datum **cond_bool_array;
713	int rc;
714
715	cond_bool_array = kmalloc_array(orig->p_bools.nprim,
716					sizeof(*orig->bool_val_to_struct),
717					GFP_KERNEL);
718	if (!cond_bool_array)
719		return -ENOMEM;
720
721	rc = hashtab_duplicate(&newdb->p_bools.table, &orig->p_bools.table,
722			       cond_bools_copy, cond_bools_destroy, NULL);
723	if (rc) {
724		kfree(cond_bool_array);
725		return -ENOMEM;
726	}
727
728	hashtab_map(&newdb->p_bools.table, cond_bools_index, cond_bool_array);
729	newdb->bool_val_to_struct = cond_bool_array;
730
731	newdb->p_bools.nprim = orig->p_bools.nprim;
732
733	return 0;
734}
735
736void cond_policydb_destroy_dup(struct policydb *p)
737{
738	hashtab_map(&p->p_bools.table, cond_bools_destroy, NULL);
739	hashtab_destroy(&p->p_bools.table);
740	cond_policydb_destroy(p);
741}
742
743int cond_policydb_dup(struct policydb *new, struct policydb *orig)
744{
745	cond_policydb_init(new);
746
747	if (duplicate_policydb_bools(new, orig))
748		return -ENOMEM;
749
750	if (duplicate_policydb_cond_list(new, orig)) {
751		cond_policydb_destroy_dup(new);
752		return -ENOMEM;
753	}
754
755	return 0;
756}
757