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