2024-09-20 10:11:03 +00:00
|
|
|
#!/usr/bin/bash
|
2024-02-12 11:26:33 +00:00
|
|
|
### varrun-convert.sh
|
|
|
|
### convert legacy filecontext entries containing /var/run to /run
|
|
|
|
### and load an extra selinux module with the new content
|
|
|
|
### the script takes a policy name as an argument
|
|
|
|
|
|
|
|
# Set DEBUG=yes before running the script to get more verbose output
|
2024-09-20 10:11:03 +00:00
|
|
|
# on the terminal and to the $LOG file
|
2024-02-12 11:26:33 +00:00
|
|
|
if [ "${DEBUG}" = "yes" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2024-09-20 10:11:03 +00:00
|
|
|
# Auxiliary and log files will be created in OUTPUTDIR
|
2024-02-12 11:26:33 +00:00
|
|
|
OUTPUTDIR="/run/selinux-policy"
|
|
|
|
LOG="$OUTPUTDIR/log"
|
|
|
|
mkdir -p ${OUTPUTDIR}
|
|
|
|
|
|
|
|
if [ -z ${1} ]; then
|
|
|
|
[ "${DEBUG}" = "yes" ] && echo "Error: Policy name required as an argument (e.g. targeted)" >> $LOG
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2024-09-20 10:11:03 +00:00
|
|
|
SEMODULEOPT="-s ${1}"
|
|
|
|
[ "${DEBUG}" = "yes" ] && SEMODULEOPT="-v ${SEMODULEOPT}"
|
|
|
|
|
|
|
|
# Take current file_contexts and unify whitespace separators
|
2024-02-12 11:26:33 +00:00
|
|
|
FILE_CONTEXTS="/etc/selinux/${1}/contexts/files/file_contexts"
|
2024-09-20 10:11:03 +00:00
|
|
|
FILE_CONTEXTS_UNIFIED="$OUTPUTDIR/file_contexts_unified"
|
2024-02-12 11:26:33 +00:00
|
|
|
if [ ! -f ${FILE_CONTEXTS} ]; then
|
|
|
|
[ "${DEBUG}" = "yes" ] && echo "Error: File context database file does not exist" >> $LOG
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! grep -q ^/var/run ${FILE_CONTEXTS}; then
|
|
|
|
[ "${DEBUG}" = "yes" ] && echo "Info: No entries containing /var/run" >> $LOG
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2024-09-20 10:11:03 +00:00
|
|
|
EXTRA_VARRUN_ENTRIES_WITHDUP="$OUTPUTDIR/extra_varrun_entries_dup.txt"
|
2024-02-12 11:26:33 +00:00
|
|
|
EXTRA_VARRUN_ENTRIES="$OUTPUTDIR/extra_varrun_entries.txt"
|
2024-09-20 10:11:03 +00:00
|
|
|
EXTRA_VARRUN_CIL="$OUTPUTDIR/extra_varrun.cil"
|
2024-02-12 11:26:33 +00:00
|
|
|
|
|
|
|
# Print only /var/run entries
|
2024-09-20 10:11:03 +00:00
|
|
|
grep ^/var/run ${FILE_CONTEXTS} > ${EXTRA_VARRUN_ENTRIES_WITHDUP}
|
2024-02-12 11:26:33 +00:00
|
|
|
|
|
|
|
# Unify whitespace separators
|
2024-09-20 10:11:03 +00:00
|
|
|
sed -i 's/[ \t]\+/ /g' ${EXTRA_VARRUN_ENTRIES_WITHDUP}
|
|
|
|
sed 's/[ \t]\+/ /g' ${FILE_CONTEXTS} > ${FILE_CONTEXTS_UNIFIED}
|
|
|
|
|
|
|
|
# Deduplicate already existing /var/run=/run entries
|
|
|
|
while read line
|
|
|
|
do
|
|
|
|
subline="${line#/var}"
|
|
|
|
if ! grep -q "^${subline}" ${FILE_CONTEXTS_UNIFIED}; then
|
|
|
|
echo "$line"
|
|
|
|
fi
|
|
|
|
done < ${EXTRA_VARRUN_ENTRIES_WITHDUP} > ${EXTRA_VARRUN_ENTRIES}
|
2024-02-12 11:26:33 +00:00
|
|
|
|
|
|
|
# Change /var/run to /run
|
|
|
|
sed -i 's|^/var/run|/run|' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
|
|
|
|
# Exception handling: packages with already duplicate entries
|
|
|
|
sed -i '/^\/run\/snapd/d' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i '/^\/run\/vfrnav/d' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i '/^\/run\/waydroid/d' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
|
|
|
|
# Change format to cil
|
|
|
|
sed -i 's/^\([^ ]\+\) \([^-]\)/\1 any \2/' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) -- /\1 file /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) -b /\1 block /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) -c /\1 char /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) -d /\1 dir /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) -l /\1 symlink /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) -p /\1 pipe /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) -s /\1 socket /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/^\([^ ]\+\) /(filecon "\1" /' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
sed -i 's/system_u:object_r:\([^:]*\):\(.*\)$/(system_u object_r \1 ((\2) (\2))))/' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
|
|
|
|
# Handle entries with <<none>> which do not match previous regexps
|
|
|
|
sed -i s'/ <<none>>$/ ())/' ${EXTRA_VARRUN_ENTRIES}
|
|
|
|
|
|
|
|
# Wrap each line with an optional block
|
|
|
|
i=1
|
|
|
|
while read line
|
|
|
|
do
|
|
|
|
echo "(optional extra_var_run_${i}"
|
|
|
|
echo " $line"
|
|
|
|
echo ")"
|
|
|
|
((i++))
|
|
|
|
done < ${EXTRA_VARRUN_ENTRIES} > ${EXTRA_VARRUN_CIL}
|
|
|
|
|
|
|
|
# Load module
|
2024-09-20 10:11:03 +00:00
|
|
|
[ -s ${EXTRA_VARRUN_CIL} ] &&
|
2024-02-12 11:26:33 +00:00
|
|
|
/usr/sbin/semodule ${SEMODULEOPT} -i ${EXTRA_VARRUN_CIL}
|
|
|
|
|