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