psss / rpms / libsepol

Forked from rpms/libsepol 5 years ago
Clone
f50a75e
diff --exclude-from=exclude -N -u -r nsalibsepol/include/sepol/policydb/conditional.h libsepol-2.0.1/include/sepol/policydb/conditional.h
f50a75e
--- nsalibsepol/include/sepol/policydb/conditional.h	2006-11-16 17:14:15.000000000 -0500
f50a75e
+++ libsepol-2.0.1/include/sepol/policydb/conditional.h	2007-03-28 14:13:02.000000000 -0400
f50a75e
@@ -100,6 +100,8 @@
f50a75e
 				   cond_node_t * needle, cond_node_t * haystack,
f50a75e
 				   int *was_created);
f50a75e
 
f50a75e
+extern cond_node_t *cond_node_create(policydb_t * p, cond_node_t * node);
f50a75e
+
f50a75e
 extern cond_node_t *cond_node_search(policydb_t * p, cond_node_t * list,
f50a75e
 				     cond_node_t * cn);
f50a75e
 
f50a75e
diff --exclude-from=exclude -N -u -r nsalibsepol/src/conditional.c libsepol-2.0.1/src/conditional.c
f50a75e
--- nsalibsepol/src/conditional.c	2006-11-16 17:14:24.000000000 -0500
f50a75e
+++ libsepol-2.0.1/src/conditional.c	2007-03-28 14:13:02.000000000 -0400
f50a75e
@@ -26,9 +26,6 @@
f50a75e
 
f50a75e
 #include "private.h"
f50a75e
 
f50a75e
-#undef min
f50a75e
-#define min(a,b) (((a) < (b)) ? (a) : (b))
f50a75e
-
f50a75e
 /* move all type rules to top of t/f lists to help kernel on evaluation */
f50a75e
 static void cond_optimize(cond_av_list_t ** l)
f50a75e
 {
f50a75e
@@ -136,6 +133,38 @@
f50a75e
 	return 1;
f50a75e
 }
f50a75e
 
f50a75e
+/* Create a new conditional node, optionally copying
f50a75e
+ * the conditional expression from an existing node.
f50a75e
+ * If node is NULL then a new node will be created
f50a75e
+ * with no conditional expression.
f50a75e
+ */
f50a75e
+cond_node_t *cond_node_create(policydb_t * p, cond_node_t * node)
f50a75e
+{
f50a75e
+	cond_node_t *new_node;
f50a75e
+	unsigned int i;
f50a75e
+
f50a75e
+	new_node = (cond_node_t *)malloc(sizeof(cond_node_t));
f50a75e
+	if (!new_node) {
f50a75e
+		return NULL;
f50a75e
+	}
f50a75e
+	memset(new_node, 0, sizeof(cond_node_t));
f50a75e
+
f50a75e
+	if (node) {
f50a75e
+		new_node->expr = cond_copy_expr(node->expr);
f50a75e
+		if (!new_node->expr) {
f50a75e
+			free(new_node);
f50a75e
+			return NULL;
f50a75e
+		}
f50a75e
+		new_node->cur_state = cond_evaluate_expr(p, new_node->expr);
f50a75e
+		new_node->nbools = node->nbools;
f50a75e
+		for (i = 0; i < min(node->nbools, COND_MAX_BOOLS); i++)
f50a75e
+			new_node->bool_ids[i] = node->bool_ids[i];
f50a75e
+		new_node->expr_pre_comp = node->expr_pre_comp;
f50a75e
+	}
f50a75e
+
f50a75e
+	return new_node;
f50a75e
+}
f50a75e
+
f50a75e
 /* Find a conditional (the needle) within a list of existing ones (the
f50a75e
  * haystack) that has a matching expression.  If found, return a
f50a75e
  * pointer to the existing node, setting 'was_created' to 0.
f50a75e
@@ -145,9 +174,6 @@
f50a75e
 			    cond_node_t * needle, cond_node_t * haystack,
f50a75e
 			    int *was_created)
f50a75e
 {
f50a75e
-	cond_node_t *new_node;
f50a75e
-	unsigned int i;
f50a75e
-
f50a75e
 	while (haystack) {
f50a75e
 		if (cond_expr_equal(needle, haystack)) {
f50a75e
 			*was_created = 0;
f50a75e
@@ -156,26 +182,8 @@
f50a75e
 		haystack = haystack->next;
f50a75e
 	}
f50a75e
 	*was_created = 1;
f50a75e
-	new_node = (cond_node_t *) malloc(sizeof(cond_node_t));
f50a75e
-	if (!new_node) {
f50a75e
-		return NULL;
f50a75e
-	}
f50a75e
-	memset(new_node, 0, sizeof(cond_node_t));
f50a75e
-	new_node->expr = cond_copy_expr(needle->expr);
f50a75e
-	if (!new_node->expr) {
f50a75e
-		free(new_node);
f50a75e
-		return NULL;
f50a75e
-	}
f50a75e
-	new_node->cur_state = cond_evaluate_expr(p, new_node->expr);
f50a75e
-	new_node->nbools = needle->nbools;
f50a75e
-	for (i = 0; i < min(needle->nbools, COND_MAX_BOOLS); i++)
f50a75e
-		new_node->bool_ids[i] = needle->bool_ids[i];
f50a75e
-	new_node->expr_pre_comp = needle->expr_pre_comp;
f50a75e
-	new_node->true_list = NULL;
f50a75e
-	new_node->false_list = NULL;
f50a75e
-	new_node->avtrue_list = NULL;
f50a75e
-	new_node->avfalse_list = NULL;
f50a75e
-	return new_node;
f50a75e
+
f50a75e
+	return cond_node_create(p, needle);
f50a75e
 }
f50a75e
 
f50a75e
 /* return either a pre-existing matching node or create a new node */
f50a75e
diff --exclude-from=exclude -N -u -r nsalibsepol/src/expand.c libsepol-2.0.1/src/expand.c
f50a75e
--- nsalibsepol/src/expand.c	2007-02-07 12:11:48.000000000 -0500
f50a75e
+++ libsepol-2.0.1/src/expand.c	2007-03-28 14:13:02.000000000 -0400
f50a75e
@@ -35,10 +35,12 @@
f50a75e
 #include <assert.h>
f50a75e
 
f50a75e
 #include "debug.h"
f50a75e
+#include "private.h"
f50a75e
 
f50a75e
 typedef struct expand_state {
f50a75e
 	int verbose;
f50a75e
 	uint32_t *typemap;
f50a75e
+	uint32_t *boolmap;
f50a75e
 	policydb_t *base;
f50a75e
 	policydb_t *out;
f50a75e
 	sepol_handle_t *handle;
f50a75e
@@ -791,8 +793,8 @@
f50a75e
 		return -1;
f50a75e
 	}
f50a75e
 
f50a75e
-	new_bool->s.value = bool->s.value;
f50a75e
 	state->out->p_bools.nprim++;
f50a75e
+	new_bool->s.value = state->out->p_bools.nprim;
f50a75e
 
f50a75e
 	ret = hashtab_insert(state->out->p_bools.table,
f50a75e
 			     (hashtab_key_t) new_id,
f50a75e
@@ -804,6 +806,8 @@
f50a75e
 		return -1;
f50a75e
 	}
f50a75e
 
f50a75e
+	state->boolmap[bool->s.value - 1] = new_bool->s.value;
f50a75e
+
f50a75e
 	new_bool->state = bool->state;
f50a75e
 
f50a75e
 	return 0;
f50a75e
@@ -1555,12 +1559,35 @@
f50a75e
 	return 0;
f50a75e
 }
f50a75e
 
f50a75e
+static int cond_node_map_bools(expand_state_t * state, cond_node_t * cn)
f50a75e
+{
f50a75e
+	cond_expr_t *cur;
f50a75e
+	unsigned int i;
f50a75e
+
f50a75e
+	cur = cn->expr;
f50a75e
+	while (cur) {
f50a75e
+		if (cur->bool)
f50a75e
+			cur->bool = state->boolmap[cur->bool - 1];
f50a75e
+		cur = cur->next;
f50a75e
+	}
f50a75e
+
f50a75e
+	for (i = 0; i < min(cn->nbools, COND_MAX_BOOLS); i++)
f50a75e
+		cn->bool_ids[i] = state->boolmap[cn->bool_ids[i] - 1];
f50a75e
+
f50a75e
+	if (cond_normalize_expr(state->out, cn)) {
f50a75e
+		ERR(state->handle, "Error while normalizing conditional");
f50a75e
+		return -1;
f50a75e
+	}
f50a75e
+
f50a75e
+	return 0;
f50a75e
+}
f50a75e
+
f50a75e
 /* copy the nodes in *reverse* order -- the result is that the last
f50a75e
  * given conditional appears first in the policy, so as to match the
f50a75e
  * behavior of the upstream compiler */
f50a75e
 static int cond_node_copy(expand_state_t * state, cond_node_t * cn)
f50a75e
 {
f50a75e
-	cond_node_t *new_cond;
f50a75e
+	cond_node_t *new_cond, *tmp;
f50a75e
 
f50a75e
 	if (cn == NULL) {
f50a75e
 		return 0;
f50a75e
@@ -1573,11 +1600,26 @@
f50a75e
 		return -1;
f50a75e
 	}
f50a75e
 
f50a75e
-	new_cond = cond_node_search(state->out, state->out->cond_list, cn);
f50a75e
+	/* create a new temporary conditional node with the booleans
f50a75e
+	 * mapped */
f50a75e
+	tmp = cond_node_create(state->base, cn);
f50a75e
+	if (!tmp) {
f50a75e
+		ERR(state->handle, "Out of memory");
f50a75e
+		return -1;
f50a75e
+	}
f50a75e
+
f50a75e
+	if (cond_node_map_bools(state, tmp)) {
f50a75e
+		ERR(state->handle, "Error mapping booleans");
f50a75e
+		return -1;
f50a75e
+	}
f50a75e
+
f50a75e
+	new_cond = cond_node_search(state->out, state->out->cond_list, tmp);
f50a75e
 	if (!new_cond) {
f50a75e
+		cond_node_destroy(tmp);
f50a75e
 		ERR(state->handle, "Out of memory!");
f50a75e
 		return -1;
f50a75e
 	}
f50a75e
+	cond_node_destroy(tmp);
f50a75e
 
f50a75e
 	if (cond_avrule_list_copy
f50a75e
 	    (state->out, cn->avtrue_list, &state->out->te_cond_avtab,
f50a75e
@@ -2210,6 +2252,12 @@
f50a75e
 		goto cleanup;
f50a75e
 	}
f50a75e
 
f50a75e
+	state.boolmap = (uint32_t *)calloc(state.base->p_bools.nprim, sizeof(uint32_t));
f50a75e
+	if (!state.boolmap) {
f50a75e
+		ERR(handle, "Out of memory!");
f50a75e
+		goto cleanup;
f50a75e
+	}
f50a75e
+
f50a75e
 	/* order is important - types must be first */
f50a75e
 
f50a75e
 	/* copy types */
f50a75e
@@ -2364,6 +2412,7 @@
f50a75e
 
f50a75e
       cleanup:
f50a75e
 	free(state.typemap);
f50a75e
+	free(state.boolmap);
f50a75e
 	return retval;
f50a75e
 }
f50a75e
 
f50a75e
diff --exclude-from=exclude -N -u -r nsalibsepol/src/private.h libsepol-2.0.1/src/private.h
f50a75e
--- nsalibsepol/src/private.h	2007-02-07 12:11:48.000000000 -0500
f50a75e
+++ libsepol-2.0.1/src/private.h	2007-03-28 14:13:02.000000000 -0400
f50a75e
@@ -24,6 +24,9 @@
f50a75e
 #define le64_to_cpu(x) bswap_64(x)
f50a75e
 #endif
f50a75e
 
f50a75e
+#undef min
f50a75e
+#define min(a,b) (((a) < (b)) ? (a) : (b))
f50a75e
+
f50a75e
 /* Policy compatibility information. */
f50a75e
 struct policydb_compat_info {
f50a75e
 	unsigned int type;