214f533
diff -up libselinux-2.0.77/include/selinux/avc.h.pre.create.cache libselinux-2.0.77/include/selinux/avc.h
214f533
--- libselinux-2.0.77/include/selinux/avc.h.pre.create.cache	2009-01-27 14:47:32.000000000 -0500
214f533
+++ libselinux-2.0.77/include/selinux/avc.h	2009-03-02 14:52:40.859167987 -0500
214f533
@@ -353,6 +353,7 @@ int avc_compute_member(security_id_t ssi
214f533
 #define AVC_CALLBACK_AUDITALLOW_DISABLE	32
214f533
 #define AVC_CALLBACK_AUDITDENY_ENABLE	64
214f533
 #define AVC_CALLBACK_AUDITDENY_DISABLE	128
214f533
+#define AVC_CALLBACK_ADD_CREATE		256
214f533
 
214f533
 /**
214f533
  * avc_add_callback - Register a callback for security events.
214f533
diff -up libselinux-2.0.77/src/avc.c.pre.create.cache libselinux-2.0.77/src/avc.c
214f533
--- libselinux-2.0.77/src/avc.c.pre.create.cache	2009-01-27 14:47:32.000000000 -0500
214f533
+++ libselinux-2.0.77/src/avc.c	2009-03-02 15:57:54.764288907 -0500
214f533
@@ -20,6 +20,8 @@ struct avc_entry {
214f533
 	security_id_t tsid;
214f533
 	security_class_t tclass;
214f533
 	struct av_decision avd;
214f533
+	security_id_t	create_sid;
214f533
+	unsigned	create_decided	:1;
214f533
 	int used;		/* used recently */
214f533
 };
214f533
 
214f533
@@ -58,6 +60,11 @@ static struct avc_cache_stats cache_stat
214f533
 static struct avc_callback_node *avc_callbacks = NULL;
214f533
 static struct sidtab avc_sidtab;
214f533
 
214f533
+/* forward declaration */
214f533
+static int avc_update_cache(uint32_t event, security_id_t ssid,
214f533
+			    security_id_t tsid, security_class_t tclass,
214f533
+			    access_vector_t perms, security_id_t create_sid);
214f533
+
214f533
 static inline int avc_hash(security_id_t ssid,
214f533
 			   security_id_t tsid, security_class_t tclass)
214f533
 {
214f533
@@ -340,6 +347,16 @@ static inline struct avc_node *avc_recla
214f533
 	return cur;
214f533
 }
214f533
 
214f533
+static inline void avc_clear_avc_entry(struct avc_entry *ae)
214f533
+{
214f533
+	ae->ssid = ae->tsid = ae->create_sid = NULL;
214f533
+	ae->tclass = 0;
214f533
+	ae->create_decided = 0;
214f533
+	ae->avd.allowed = ae->avd.decided = 0;
214f533
+	ae->avd.auditallow = ae->avd.auditdeny = 0;
214f533
+	ae->used = 0;
214f533
+}
214f533
+
214f533
 static inline struct avc_node *avc_claim_node(security_id_t ssid,
214f533
 					      security_id_t tsid,
214f533
 					      security_class_t tclass)
214f533
@@ -361,6 +378,7 @@ static inline struct avc_node *avc_claim
214f533
 	}
214f533
 
214f533
 	hvalue = avc_hash(ssid, tsid, tclass);
214f533
+	avc_clear_avc_entry(&new->ae);
214f533
 	new->ae.used = 1;
214f533
 	new->ae.ssid = ssid;
214f533
 	new->ae.tsid = tsid;
214f533
@@ -498,8 +516,8 @@ static int avc_insert(security_id_t ssid
214f533
  * avc_remove - Remove AVC and sidtab entries for SID.
214f533
  * @sid: security identifier to be removed
214f533
  *
214f533
- * Remove all AVC entries containing @sid as source
214f533
- * or target, and remove @sid from the SID table.
214f533
+ * Remove all AVC entries containing @sid as source, target, or
214f533
+ * create_sid, and remove @sid from the SID table.
214f533
  * Free the memory allocated for the structure corresponding
214f533
  * to @sid.  After this function has been called, @sid must
214f533
  * not be used until another call to avc_context_to_sid() has
214f533
@@ -514,19 +532,15 @@ static void avc_remove(security_id_t sid
214f533
 		cur = avc_cache.slots[i];
214f533
 		prev = NULL;
214f533
 		while (cur) {
214f533
-			if (sid == cur->ae.ssid || sid == cur->ae.tsid) {
214f533
+			if (sid == cur->ae.ssid || sid == cur->ae.tsid ||
214f533
+			    (cur->ae.create_decided && sid == cur->ae.create_sid)) {
214f533
 				if (prev)
214f533
 					prev->next = cur->next;
214f533
 				else
214f533
 					avc_cache.slots[i] = cur->next;
214f533
 				tmp = cur;
214f533
 				cur = cur->next;
214f533
-				tmp->ae.ssid = tmp->ae.tsid = NULL;
214f533
-				tmp->ae.tclass = 0;
214f533
-				tmp->ae.avd.allowed = tmp->ae.avd.decided = 0;
214f533
-				tmp->ae.avd.auditallow = tmp->ae.avd.auditdeny =
214f533
-				    0;
214f533
-				tmp->ae.used = 0;
214f533
+				avc_clear_avc_entry(&tmp->ae);
214f533
 				tmp->next = avc_node_freelist;
214f533
 				avc_node_freelist = tmp;
214f533
 				avc_cache.active_nodes--;
214f533
@@ -570,11 +584,7 @@ int avc_reset(void)
214f533
 		while (node) {
214f533
 			tmp = node;
214f533
 			node = node->next;
214f533
-			tmp->ae.ssid = tmp->ae.tsid = NULL;
214f533
-			tmp->ae.tclass = 0;
214f533
-			tmp->ae.avd.allowed = tmp->ae.avd.decided = 0;
214f533
-			tmp->ae.avd.auditallow = tmp->ae.avd.auditdeny = 0;
214f533
-			tmp->ae.used = 0;
214f533
+			avc_clear_avc_entry(&tmp->ae);
214f533
 			tmp->next = avc_node_freelist;
214f533
 			avc_node_freelist = tmp;
214f533
 			avc_cache.active_nodes--;
214f533
@@ -896,24 +906,52 @@ int avc_compute_create(security_id_t ssi
214f533
 		       security_class_t tclass, security_id_t *newsid)
214f533
 {
214f533
 	int rc;
214f533
+	struct avc_entry_ref aeref;
214f533
+	security_context_t ctx = NULL;
214f533
+
214f533
 	*newsid = NULL;
214f533
+
214f533
+	avc_entry_ref_init(&aeref);
214f533
+retry:
214f533
 	avc_get_lock(avc_lock);
214f533
-	if (ssid->refcnt > 0 && tsid->refcnt > 0) {
214f533
-		security_context_t ctx = NULL;
214f533
-		rc = security_compute_create_raw(ssid->ctx, tsid->ctx, tclass,
214f533
-						 &ctx;;
214f533
-		if (rc)
214f533
-			goto out;
214f533
-		rc = sidtab_context_to_sid(&avc_sidtab, ctx, newsid);
214f533
-		if (!rc)
214f533
-			(*newsid)->refcnt++;
214f533
-		freecon(ctx);
214f533
-	} else {
214f533
+	if (ssid->refcnt <= 0 || tsid->refcnt <= 0) {
214f533
 		errno = EINVAL;	/* bad reference count */
214f533
 		rc = -1;
214f533
+		goto out;
214f533
+	}
214f533
+
214f533
+	rc = avc_lookup(ssid, tsid, tclass, 0, &aeref);
214f533
+	if (!rc) {
214f533
+		/* we found something in the avc */
214f533
+		if (aeref.ae->create_decided) {
214f533
+			*newsid = aeref.ae->create_sid;
214f533
+			goto out;
214f533
+		} else {
214f533
+			goto compute;
214f533
+		}
214f533
 	}
214f533
+	/* there is nothing in the avd for this tuple, so, lets get something */
214f533
+	avc_release_lock(avc_lock);
214f533
+	avc_has_perm_noaudit(ssid, tsid, tclass, 0, &aeref, NULL);
214f533
+	goto retry;
214f533
+
214f533
+compute:
214f533
+	rc = security_compute_create_raw(ssid->ctx, tsid->ctx, tclass,
214f533
+					 &ctx;;
214f533
+	if (rc)
214f533
+		goto out;
214f533
+	rc = sidtab_context_to_sid(&avc_sidtab, ctx, newsid);
214f533
+	if (rc)
214f533
+		goto out;
214f533
+
214f533
+	avc_update_cache(AVC_CALLBACK_ADD_CREATE, ssid, tsid, tclass, 0,
214f533
+			 *newsid);
214f533
+
214f533
 out:
214f533
+	if (*newsid)
214f533
+		(*newsid)->refcnt++;
214f533
 	avc_release_lock(avc_lock);
214f533
+	freecon(ctx);
214f533
 	return rc;
214f533
 }
214f533
 
214f533
@@ -978,7 +1016,8 @@ static inline int avc_sidcmp(security_id
214f533
 }
214f533
 
214f533
 static inline void avc_update_node(uint32_t event, struct avc_node *node,
214f533
-				   access_vector_t perms)
214f533
+				   access_vector_t perms,
214f533
+				   security_id_t create_sid)
214f533
 {
214f533
 	switch (event) {
214f533
 	case AVC_CALLBACK_GRANT:
214f533
@@ -1000,12 +1039,16 @@ static inline void avc_update_node(uint3
214f533
 	case AVC_CALLBACK_AUDITDENY_DISABLE:
214f533
 		node->ae.avd.auditdeny &= ~perms;
214f533
 		break;
214f533
+	case AVC_CALLBACK_ADD_CREATE:
214f533
+		node->ae.create_sid = create_sid;
214f533
+		node->ae.create_decided = 1;
214f533
+		break;
214f533
 	}
214f533
 }
214f533
 
214f533
 static int avc_update_cache(uint32_t event, security_id_t ssid,
214f533
 			    security_id_t tsid, security_class_t tclass,
214f533
-			    access_vector_t perms)
214f533
+			    access_vector_t perms, security_id_t create_sid)
214f533
 {
214f533
 	struct avc_node *node;
214f533
 	int i;
214f533
@@ -1019,7 +1062,7 @@ static int avc_update_cache(uint32_t eve
214f533
 				if (avc_sidcmp(ssid, node->ae.ssid) &&
214f533
 				    avc_sidcmp(tsid, node->ae.tsid) &&
214f533
 				    tclass == node->ae.tclass) {
214f533
-					avc_update_node(event, node, perms);
214f533
+					avc_update_node(event, node, perms, create_sid);
214f533
 				}
214f533
 			}
214f533
 		}
214f533
@@ -1027,7 +1070,7 @@ static int avc_update_cache(uint32_t eve
214f533
 		/* apply to one node */
214f533
 		node = avc_search_node(ssid, tsid, tclass, 0);
214f533
 		if (node) {
214f533
-			avc_update_node(event, node, perms);
214f533
+			avc_update_node(event, node, perms, create_sid);
214f533
 		}
214f533
 	}
214f533
 
214f533
@@ -1058,7 +1101,7 @@ static int avc_control(uint32_t event, s
214f533
 	 * been invoked to update the cache state.
214f533
 	 */
214f533
 	if (event != AVC_CALLBACK_TRY_REVOKE)
214f533
-		avc_update_cache(event, ssid, tsid, tclass, perms);
214f533
+		avc_update_cache(event, ssid, tsid, tclass, perms, NULL);
214f533
 
214f533
 	for (c = avc_callbacks; c; c = c->next) {
214f533
 		if ((c->events & event) &&
214f533
@@ -1080,7 +1123,7 @@ static int avc_control(uint32_t event, s
214f533
 	if (event == AVC_CALLBACK_TRY_REVOKE) {
214f533
 		/* revoke any unretained permissions */
214f533
 		perms &= ~tretained;
214f533
-		avc_update_cache(event, ssid, tsid, tclass, perms);
214f533
+		avc_update_cache(event, ssid, tsid, tclass, perms, NULL);
214f533
 		*out_retained = tretained;
214f533
 	}
214f533
 
214f533