Skip to content

Instantly share code, notes, and snippets.

@Rashed97
Created July 14, 2019 05:40
Show Gist options
  • Save Rashed97/72378ca3983fa8c3ab1a75f58fc8b522 to your computer and use it in GitHub Desktop.
Save Rashed97/72378ca3983fa8c3ab1a75f58fc8b522 to your computer and use it in GitHub Desktop.
VENDOR_SEPOL_PATH="${1}"
OUT_DIR="${2}"
rm -rf contexts contexts-sorted vendor-contexts ${OUT_DIR}
mkdir -p ${OUT_DIR}/vendor
context_files="file_contexts hwservice_contexts property_contexts seapp_contexts"
for context_file in $context_files; do
while read context_line; do
if ! grep -Frq "`echo "${context_line}" | cut -f1`" ../../../device/qcom/sepolicy/ ../../../system/sepolicy/ --exclude-dir=prebuilts --exclude-dir=compat; then
echo ${context_line} >> ${OUT_DIR}/vendor/${context_file}
fi
done < ${VENDOR_SEPOL_PATH}/vendor_${context_file}
done
# vndservice_contexts is special...
while read context_line; do
if ! grep -Frq "`echo "${context_line}" | cut -f1`" ../../../device/qcom/sepolicy/ ../../../system/sepolicy/ --exclude-dir=prebuilts --exclude-dir=compat; then
echo ${context_line} >> ${OUT_DIR}/vendor/service_contexts
fi
done < ${VENDOR_SEPOL_PATH}/vndservice_contexts
### file.te ###
grep -v '^#' ${OUT_DIR}/vendor/file_contexts | while read line; do
echo $line | sed 's#^.*\(u:object_r.*\)#\1#g' | sed 's#u:object_r:##g' | sed 's#:s0##g' >> contexts
done
sort contexts | uniq > contexts-sorted
while read context; do
grep -Frq "type ${context}," ../../../device/qcom/sepolicy/ ../../../system/sepolicy/ --exclude-dir=prebuilts --exclude-dir=compat || echo $context >> vendor-contexts
done < contexts-sorted
while read type; do
if grep -q "(typealiasactual ${type}" ${VENDOR_SEPOL_PATH}/vendor_sepolicy.cil; then
aliasarray=($(cat ${VENDOR_SEPOL_PATH}/vendor_sepolicy.cil | grep "^(typealiasactual*" | grep " ${type} " | sed 's#^(typealiasactual ##g' | sed 's#(.*##g' | sort | sed 's#)##'))
echo "`echo type ${aliasarray[1]}, ` `echo $(cat ${VENDOR_SEPOL_PATH}/vendor_sepolicy.cil | grep "^(typeattributeset*" | grep " ${aliasarray[1]} " | sed 's#^(typeattributeset ##g' | sed 's#(.*##g' | sort) | sed 's# #, #g'`" | sed 's#$#;#g' >> ${OUT_DIR}/vendor/file.te
echo "`echo typealias ` `echo ${aliasarray[1]}` `echo "alias"` `echo ${aliasarray[0]}`" | sed 's#$#;#g' >> ${OUT_DIR}/vendor/file.te
else
echo "`echo type ${type}, ` `echo $(cat ${VENDOR_SEPOL_PATH}/vendor_sepolicy.cil | grep "^(typeattributeset*" | grep " ${type} " | sed 's#^(typeattributeset ##g' | sed 's#(.*##g' | sort) | sed 's# #, #g'`" | sed 's#$#;#g' >> ${OUT_DIR}/vendor/file.te
fi
done < vendor-contexts
rm -rf contexts contexts-sorted vendor-contexts
### property.te ###
# in addition to skipping commented lines, we also don't want to re-define exported#_default_prop
grep -Ev '^#|exported' ${OUT_DIR}/vendor/property_contexts | while read line; do
echo $line | sed 's#^.*\(u:object_r.*\)#\1#g' | sed 's#u:object_r:##g' | sed 's#:s0##g' >> contexts
done
sort contexts | uniq > contexts-sorted
while read context; do
grep -nrq "type ${context}," ../../../device/qcom/sepolicy/ ../../../system/sepolicy/ --exclude-dir=prebuilts --exclude-dir=compat || echo $context >> vendor-contexts
done < contexts-sorted
while read type; do
if grep -q "(typealiasactual ${type}" ${VENDOR_SEPOL_PATH}/vendor_sepolicy.cil; then
aliasarray=($(cat ${VENDOR_SEPOL_PATH}/vendor_sepolicy.cil | grep "^(typealiasactual*" | grep " ${type} " | sed 's#^(typealiasactual ##g' | sed 's#(.*##g' | sort | sed 's#)##'))
echo "`echo typealias ` `echo ${aliasarray[1]}` `echo "alias"` `echo ${aliasarray[0]}`" | sed 's#$#;#g' >> ${OUT_DIR}/vendor/property.te
else
echo "`echo type ${type}, ` `echo $(cat ${VENDOR_SEPOL_PATH}/vendor_sepolicy.cil | grep "^(typeattributeset*" | grep " ${type} " | sed 's#^(typeattributeset ##g' | sed 's#(.*##g' | sort) | sed 's# #, #g'`" | sed 's#$#;#g' >> ${OUT_DIR}/vendor/property.te
fi
done < vendor-contexts
rm -rf contexts contexts-sorted vendor-contexts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment