Skip to contentSkip to conversation

policycoreutils/scripts/fixfiles: search fallback configuration roots

PATCH In review Stephen Smalley 1 participant 2 messages Sep 23 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 policycoreutils/scripts/fixfiles: search fallback configuration roots

fixfiles reads /etc/selinux/config, /etc/selinux/fixfiles_exclude_dirs, and constructs FC=/etc/selinux/${SELINUXTYPE}/contexts/files/file_contexts directly. On a system where the SELinux configuration is shipped under /usr/lib/selinux and /etc/selinux is empty, config is not found so SELINUXTYPE defaults to "targeted" regardless of what the vendor shipped, and FC points at a non-existent path. setfiles takes an explicit SELABEL_OPT_PATH literally (only the no-path-given case in label_file.c goes through the search list), so the setfiles invocations fail.

Add a small selinux_resolve() that mirrors the /etc/selinux then /usr/lib/selinux search that libselinux applies, and use it for config, fixfiles_exclude_dirs, and FC. FC is resolved independently of
config: a vendor config under /usr/lib can still be paired with a libsemanage-written file_contexts under /etc.

While here, move the two mktemp calls in diff_filecontext() to $TMPDIR instead of alongside FC/PREFC. The scratch files are diff intermediates whose location does not matter, and when FC resolves under /usr/lib the directory is read-only. Creating and using the intermediate files in $TMPDIR is safe because the caller of fixfiles is trusted, mktemp usage is race-free by construction and creates the new file with mode 0600, and the sticky bit on /tmp (plus fs.protected_regular=1 on systems that enable it) prevents an attacker from replacing the file between mktemp and the shell redirections.

---
 policycoreutils/scripts/fixfiles   | 52 ++++++++++++++++++------------
 policycoreutils/scripts/fixfiles.8 |  9 ++++--
 2 files changed, 38 insertions(+), 23 deletions(-)
policycoreutils/scripts/fixfiles +32 -20
index 6e93d2ba..45f2d5ce 100755
@@ -66,22 +66,32 @@ for i in $FS; do
 done
 }
 
+SELINUX_CONFDIRS="/etc/selinux /usr/lib/selinux"
+selinux_resolve() {
+    for d in $SELINUX_CONFDIRS; do
+	if [ -e "$d/$1" ]; then
+	    echo "$d/$1"
+	    return 0
+	fi
+    done
+    return 1
+}
+
 exclude_dirs_from_relabelling() {
+    local f
     exclude_from_relabelling=
-    if [ -e /etc/selinux/fixfiles_exclude_dirs ]
-    then
-	while read i
-	do
-	  # skip blank line and comment
-	  # skip not absolute path
-	  # skip not directory
-	  [ -z "${i}" ] && continue
-	  [[ "${i}" =~ ^[[:blank:]]*# ]] && continue
-	  [[ ! "${i}" =~ ^/.* ]] && continue
-	  [[ ! -d "${i}" ]] && continue
-	  exclude_from_relabelling="$exclude_from_relabelling -e $i"
-	done < /etc/selinux/fixfiles_exclude_dirs
-    fi
+    f=$(selinux_resolve fixfiles_exclude_dirs) || return 0
+    while read i
+    do
+	# skip blank line and comment
+	# skip not absolute path
+	# skip not directory
+	[ -z "${i}" ] && continue
+	[[ "${i}" =~ ^[[:blank:]]*# ]] && continue
+	[[ ! "${i}" =~ ^/.* ]] && continue
+	[[ ! -d "${i}" ]] && continue
+	exclude_from_relabelling="$exclude_from_relabelling -e $i"
+    done < "$f"
     echo "$exclude_from_relabelling"
 }
 
@@ -103,10 +113,12 @@ RESTORECON=/sbin/restorecon
 FILESYSTEMSRW=`get_rw_labeled_mounts`
 FILESYSTEMSRO=`get_ro_labeled_mounts`
 SELINUXTYPE="targeted"
-if [ -e /etc/selinux/config ]; then
-    . /etc/selinux/config
-    FC=/etc/selinux/${SELINUXTYPE}/contexts/files/file_contexts
-else
+SELINUXCONFIG=$(selinux_resolve config)
+if [ -n "$SELINUXCONFIG" ]; then
+    . "$SELINUXCONFIG"
+fi
+FC=$(selinux_resolve "${SELINUXTYPE}/contexts/files/file_contexts")
+if [ -z "$FC" ]; then
     FC=/etc/security/selinux/file_contexts
 fi
 
@@ -153,9 +165,9 @@ done
 LogExcluded
 
 if [ -f ${PREFC} -a -x /usr/bin/diff ]; then
-	TEMPFILE=`mktemp ${FC}.XXXXXXXXXX`
+	TEMPFILE=`mktemp`
 	test -z "$TEMPFILE" && exit
-	PREFCTEMPFILE=`mktemp ${PREFC}.XXXXXXXXXX`
+	PREFCTEMPFILE=`mktemp`
 	sed -r -e 's,:s0, ,g' $PREFC | sort -u > ${PREFCTEMPFILE}
 	sed -r -e 's,:s0, ,g' $FC | sort -u |
 	/usr/bin/diff -b ${PREFCTEMPFILE} - |
policycoreutils/scripts/fixfiles.8 +6 -3
index f5b5d9ac..00cfd3e7 100644
@@ -38,8 +38,9 @@ new policy, or  just check whether the file contexts are all
 as you expect.  By default it will relabel all mounted ext2, ext3, ext4, gfs2, xfs,
 jfs and btrfs file systems as long as they do not have a security context mount
 option.  You can use the \-R flag to use rpmpackages as an alternative.
-The file /etc/selinux/fixfiles_exclude_dirs can contain a list of directories
-excluded from relabeling.
+The file /etc/selinux/fixfiles_exclude_dirs (or
+/usr/lib/selinux/fixfiles_exclude_dirs if that does not exist) can
+contain a list of directories excluded from relabeling.
 .P
 .B fixfiles onboot
 will setup the machine to relabel on the next reboot.
@@ -114,7 +115,9 @@ List of files or directories trees that you wish to check file context on.
 
 .SH EXAMPLE
 .nf
-Relabel the whole filesystem, except paths listed in /etc/selinux/fixfiles_exclude_dirs
+Relabel the whole filesystem, except paths listed in
+/etc/selinux/fixfiles_exclude_dirs (or
+/usr/lib/selinux/fixfiles_exclude_dirs if the former does not exist)
 # fixfiles relabel
 Schedule the machine to relabel on the next boot and force relabeling of customizable types
 # fixfiles -F onboot
Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 28 lines · Stephen Smalley

fixfiles reads /etc/selinux/config, /etc/selinux/fixfiles_exclude_dirs, and constructs FC=/etc/selinux/${SELINUXTYPE}/contexts/files/file_contexts directly. On a system where the SELinux configuration is shipped under /usr/lib/selinux and /etc/selinux is empty, config is not found so SELINUXTYPE defaults to "targeted" regardless of what the vendor shipped, and FC points at a non-existent path. setfiles takes an explicit SELABEL_OPT_PATH literally (only the no-path-given case in label_file.c goes through the search list), so the setfiles invocations fail.

Add a small selinux_resolve() that mirrors the /etc/selinux then /usr/lib/selinux search that libselinux applies, and use it for config, fixfiles_exclude_dirs, and FC. FC is resolved independently of
config: a vendor config under /usr/lib can still be paired with a libsemanage-written file_contexts under /etc.

While here, move the two mktemp calls in diff_filecontext() to $TMPDIR instead of alongside FC/PREFC. The scratch files are diff intermediates whose location does not matter, and when FC resolves under /usr/lib the directory is read-only. Creating and using the intermediate files in $TMPDIR is safe because the caller of fixfiles is trusted, mktemp usage is race-free by construction and creates the new file with mode 0600, and the sticky bit on /tmp (plus fs.protected_regular=1 on systems that enable it) prevents an attacker from replacing the file between mktemp and the shell redirections.

Quoted 1 line

This has been merged.

Known residual references to /etc/selinux that do not have a fallback still:
- mcstrans setrans.conf Include= targets (literal string is passed to glob; if you care, the vendor-supplied setrans.conf should specify /usr/lib/selinux prefixes in its setrans.conf and any child configs).
- python/sepolgen /etc/selinux/sepolgen.conf passed to PathChooser() by src/sepolgen/defaults.py but degrades gracefully to hardcoded SELINUX_DEVEL_PATH.
- python/chcat/chcat direct open of selinux.selinux_translations_path(); needs a SWIG wrapper for selinux_policy_open() to fix and only fails for chcat -L on a vendor-only setrans.conf.
- gui/statusPage.py does os.listdir("/etc/selinux/") to provide GUI dropdown of available policy types. Vendor-only policy -> empty list.

None of these seem like blockers to me to move forward with vendor-shipped policy under /usr/lib/selinux but I'm also happy to take patches addressing any of these if someone cares enough.