Merge "Add SELinux policy for asec containers."
diff --git a/Android.mk b/Android.mk
index 2d06c03..c023783 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,7 +1,5 @@
 LOCAL_PATH:= $(call my-dir)
 
-include $(call all-makefiles-under,$(LOCAL_PATH))
-
 include $(CLEAR_VARS)
 
 # SELinux policy version.
@@ -12,16 +10,46 @@
 MLS_SENS=1
 MLS_CATS=1024
 
-LOCAL_POLICY_DIRS := $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/ device/*/$(TARGET_DEVICE)/ vendor/*/$(TARGET_DEVICE)/
+# Quick edge case error detection for BOARD_SEPOLICY_REPLACE.
+# Builds the singular path for each replace file.
+sepolicy_replace_paths :=
+$(foreach pf, $(BOARD_SEPOLICY_REPLACE), \
+  $(if $(filter $(pf), $(BOARD_SEPOLICY_UNION)), \
+    $(error Ambiguous request for sepolicy $(pf). Appears in both \
+      BOARD_SEPOLICY_REPLACE and BOARD_SEPOLICY_UNION), \
+  ) \
+  $(eval _paths := $(wildcard $(addsuffix /$(pf), $(BOARD_SEPOLICY_DIRS)))) \
+  $(eval _occurrences := $(words $(_paths))) \
+  $(if $(filter 0,$(_occurrences)), \
+    $(error No sepolicy file found for $(pf) in $(BOARD_SEPOLICY_DIRS)), \
+  ) \
+  $(if $(filter 1, $(_occurrences)), \
+    $(eval sepolicy_replace_paths += $(_paths)), \
+    $(error Multiple occurrences of replace file $(pf) in $(_paths)) \
+  ) \
+  $(if $(filter 0, $(words $(wildcard $(addsuffix /$(pf), $(LOCAL_PATH))))), \
+    $(error Specified the sepolicy file $(pf) in BOARD_SEPOLICY_REPLACE, \
+      but none found in $(LOCAL_PATH)), \
+  ) \
+)
 
-LOCAL_POLICY_FC := $(wildcard $(addsuffix sepolicy.fc, $(LOCAL_POLICY_DIRS)))
-LOCAL_POLICY_TE := $(wildcard $(addsuffix sepolicy.te, $(LOCAL_POLICY_DIRS)))
-LOCAL_POLICY_PC := $(wildcard $(addsuffix sepolicy.pc, $(LOCAL_POLICY_DIRS)))
-LOCAL_POLICY_FS_USE := $(wildcard $(addsuffix sepolicy.fs_use, $(LOCAL_POLICY_DIRS)))
-LOCAL_POLICY_PORT_CONTEXTS := $(wildcard $(addsuffix sepolicy.port_contexts, $(LOCAL_POLICY_DIRS)))
-LOCAL_POLICY_GENFS_CONTEXTS := $(wildcard $(addsuffix sepolicy.genfs_contexts, $(LOCAL_POLICY_DIRS)))
-LOCAL_POLICY_INITIAL_SID_CONTEXTS := $(wildcard $(addsuffix sepolicy.initial_sid_contexts, $(LOCAL_POLICY_DIRS)))
-LOCAL_POLICY_SC := $(wildcard $(addsuffix seapp_contexts, $(LOCAL_POLICY_DIRS)))
+# Builds paths for all requested policy files w.r.t
+# both BOARD_SEPOLICY_REPLACE and BOARD_SEPOLICY_UNION
+# product variables.
+# $(1): the set of policy name paths to build
+build_policy = $(foreach type, $(1), \
+  $(foreach expanded_type, $(notdir $(wildcard $(addsuffix /$(type), $(LOCAL_PATH)))), \
+    $(if $(filter $(expanded_type), $(BOARD_SEPOLICY_REPLACE)), \
+      $(wildcard $(addsuffix $(expanded_type), $(dir $(sepolicy_replace_paths)))), \
+      $(LOCAL_PATH)/$(expanded_type) \
+    ) \
+  ) \
+  $(foreach union_policy, $(wildcard $(addsuffix /$(type), $(BOARD_SEPOLICY_DIRS))), \
+    $(if $(filter $(notdir $(union_policy)), $(BOARD_SEPOLICY_UNION)), \
+      $(union_policy), \
+    ) \
+  ) \
+)
 
 ##################################
 include $(CLEAR_VARS)
@@ -36,7 +64,7 @@
 sepolicy_policy.conf := $(intermediates)/policy.conf
 $(sepolicy_policy.conf): PRIVATE_MLS_SENS := $(MLS_SENS)
 $(sepolicy_policy.conf): PRIVATE_MLS_CATS := $(MLS_CATS)
-$(sepolicy_policy.conf) : $(wildcard $(addprefix $(LOCAL_PATH)/,security_classes initial_sids access_vectors global_macros mls_macros mls policy_capabilities te_macros attributes *.te) $(LOCAL_POLICY_TE) $(addprefix $(LOCAL_PATH)/, roles users initial_sid_contexts) $(LOCAL_POLICY_INITIAL_SID_CONTEXTS) $(addprefix $(LOCAL_PATH)/,fs_use) $(LOCAL_POLICY_FS_USE) $(addprefix $(LOCAL_PATH)/,genfs_contexts) $(LOCAL_POLICY_GENFS_CONTEXTS) $(addprefix $(LOCAL_PATH)/,port_contexts) $(LOCAL_POLICY_PORT_CONTEXTS))
+$(sepolicy_policy.conf) : $(call build_policy, security_classes initial_sids access_vectors global_macros mls_macros mls policy_capabilities te_macros attributes *.te roles users initial_sid_contexts fs_use genfs_contexts port_contexts)
 	@mkdir -p $(dir $@)
 	$(hide) m4 -D mls_num_sens=$(PRIVATE_MLS_SENS) -D mls_num_cats=$(PRIVATE_MLS_CATS) -s $^ > $@
 
@@ -44,23 +72,12 @@
 	@mkdir -p $(dir $@)
 	$(hide) $(HOST_OUT_EXECUTABLES)/checkpolicy -M -c $(POLICYVERS) -o $@ $<
 
+built_sepolicy := $(LOCAL_BUILT_MODULE)
 sepolicy_policy.conf :=
 
 ###################################
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := checkfc
-LOCAL_MODULE_TAGS := optional
-LOCAL_C_INCLUDES += external/libsepol/include external/libselinux/include
-LOCAL_SRC_FILES := checkfc.c
-LOCAL_STATIC_LIBRARIES := libsepol libselinux
-LOCAL_MODULE_CLASS := EXECUTABLES
-
-include $(BUILD_HOST_EXECUTABLE)
-
-##################################
-include $(CLEAR_VARS)
-
 LOCAL_MODULE := file_contexts
 LOCAL_MODULE_CLASS := ETC
 LOCAL_MODULE_TAGS := optional
@@ -68,13 +85,13 @@
 
 include $(BUILD_SYSTEM)/base_rules.mk
 
-ALL_FC_FILES := $(LOCAL_PATH)/file_contexts $(LOCAL_POLICY_FC)
+ALL_FC_FILES := $(call build_policy, file_contexts)
 
-file_contexts := $(intermediates)/file_contexts
-$(file_contexts):  $(ALL_FC_FILES) sepolicy $(HOST_OUT_EXECUTABLES)/checkfc
+$(LOCAL_BUILT_MODULE): PRIVATE_SEPOLICY := $(built_sepolicy)
+$(LOCAL_BUILT_MODULE):  $(ALL_FC_FILES) $(built_sepolicy) $(HOST_OUT_EXECUTABLES)/checkfc
 	@mkdir -p $(dir $@)
 	$(hide) m4 -s $(ALL_FC_FILES) > $@
-	$(hide) $(HOST_OUT_EXECUTABLES)/checkfc $(TARGET_ROOT_OUT)/sepolicy $@
+	$(hide) $(HOST_OUT_EXECUTABLES)/checkfc $(PRIVATE_SEPOLICY) $@
 
 file_contexts :=
 
@@ -88,15 +105,17 @@
 include $(BUILD_SYSTEM)/base_rules.mk
 
 seapp_contexts.tmp := $(intermediates)/seapp_contexts.tmp
-$(seapp_contexts.tmp): $(LOCAL_PATH)/seapp_contexts $(LOCAL_POLICY_SC)
+$(seapp_contexts.tmp): $(call build_policy, seapp_contexts)
 	@mkdir -p $(dir $@)
 	$(hide) m4 -s $^ > $@
 
-$(LOCAL_BUILT_MODULE) : $(seapp_contexts.tmp) $(TARGET_ROOT_OUT)/sepolicy $(HOST_OUT_EXECUTABLES)/checkseapp
+$(LOCAL_BUILT_MODULE): PRIVATE_SEPOLICY := $(built_sepolicy)
+$(LOCAL_BUILT_MODULE) : $(seapp_contexts.tmp) $(built_sepolicy) $(HOST_OUT_EXECUTABLES)/checkseapp
 	@mkdir -p $(dir $@)
-	$(HOST_OUT_EXECUTABLES)/checkseapp -p $(TARGET_ROOT_OUT)/sepolicy -o $@ $<
+	$(HOST_OUT_EXECUTABLES)/checkseapp -p $(PRIVATE_SEPOLICY) -o $@ $<
 
 seapp_contexts.tmp :=
+built_sepolicy :=
 ##################################
 include $(CLEAR_VARS)
 
@@ -108,7 +127,7 @@
 include $(BUILD_SYSTEM)/base_rules.mk
 
 property_contexts := $(intermediates)/property_contexts
-$(property_contexts): $(LOCAL_PATH)/property_contexts $(LOCAL_POLICY_PC)
+$(property_contexts): $(call build_policy, property_contexts)
 	@mkdir -p $(dir $@)
 	$(hide) m4 -s $^ > $@
 
@@ -139,3 +158,8 @@
 include $(BUILD_PREBUILT)
 
 ##################################
+
+build_policy :=
+sepolicy_replace_paths :=
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/check_seapp/Android.mk b/check_seapp/Android.mk
deleted file mode 100644
index e4a4acc..0000000
--- a/check_seapp/Android.mk
+++ /dev/null
@@ -1,15 +0,0 @@
-##
-# checkseapp
-#
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := checkseapp
-LOCAL_MODULE_TAGS := optional
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/../libsepol/include/ 
-LOCAL_CFLAGS := -DLINK_SEPOL_STATIC 
-LOCAL_SRC_FILES := check_seapp/check_seapp.c
-LOCAL_STATIC_LIBRARIES := libsepol
-LOCAL_MODULE_CLASS := EXECUTABLES
-
-include $(BUILD_HOST_EXECUTABLE)
diff --git a/tools/Android.mk b/tools/Android.mk
new file mode 100644
index 0000000..8d61e25
--- /dev/null
+++ b/tools/Android.mk
@@ -0,0 +1,24 @@
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := checkseapp
+LOCAL_MODULE_TAGS := optional
+LOCAL_C_INCLUDES := external/libsepol/include/
+LOCAL_CFLAGS := -DLINK_SEPOL_STATIC
+LOCAL_SRC_FILES := check_seapp.c
+LOCAL_STATIC_LIBRARIES := libsepol
+
+include $(BUILD_HOST_EXECUTABLE)
+
+###################################
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := checkfc
+LOCAL_MODULE_TAGS := optional
+LOCAL_C_INCLUDES := external/libsepol/include \
+                    external/libselinux/include
+LOCAL_SRC_FILES := checkfc.c
+LOCAL_STATIC_LIBRARIES := libsepol libselinux
+
+include $(BUILD_HOST_EXECUTABLE)
diff --git a/check_seapp/check_seapp.c b/tools/check_seapp.c
similarity index 98%
rename from check_seapp/check_seapp.c
rename to tools/check_seapp.c
index 93ecb2f..dea2e5f 100644
--- a/check_seapp/check_seapp.c
+++ b/tools/check_seapp.c
@@ -428,8 +428,11 @@
 		free(m->data);
 	}
 
+/* hdestroy() frees comparsion keys for non glibc */
+#ifdef __GLIBC__
 	if(s == rule_map_destroy_key && rm->key)
 		free(rm->key);
+#endif
 
 	free(rm);
 }
@@ -778,7 +781,10 @@
 			 */
 			preserved_key = tmp->r->key;
 			rule_map_free(tmp->r, rule_map_preserve_key);
+/*  hdestroy() frees comparsion keys for non glibc */
+#ifdef __GLIBC__
 			free(rm->key);
+#endif
 			rm->key = preserved_key;
 			tmp->r = rm;
 		}
@@ -845,11 +851,11 @@
 		log_info("Got line %d\n", lineno);
 		len = strlen(line_buf);
 		if (line_buf[len - 1] == '\n')
-			line_buf[len - 1] = 0;
+			line_buf[len - 1] = '\0';
 		p = line_buf;
 		while (isspace(*p))
 			p++;
-		if (*p == '#' || *p == 0)
+		if (*p == '#' || *p == '\0')
 			continue;
 
 		token = strtok_r(p, " \t", &saveptr);
diff --git a/checkfc.c b/tools/checkfc.c
similarity index 100%
rename from checkfc.c
rename to tools/checkfc.c