Skip to contentSkip to conversation

Reorganize SELinux early boot scripts.

PATCH New Philip Tricca 1 participant 5 messages Nov 8, 2015

lore
Share & tools
lore
#0 lore raw

cover Reorganize SELinux early boot scripts.

With file system labeling support in the build now the init script in the selinux-config recipe has a lot of unnecessary checks / fixes. This patch set factors this script out into 3 new recipes:

selinux-labeldev: The first init script that a system needs to run in order to get devices nodes labeled properly.
selinux-autorelabel: A boot script to detect the existance of the /.autorelabel file and relabel the file system accordingly.
selinux-init: The remaining functionality. This is effectively a debugging script that detects system misconfiguration at boot time.

Some of the bonus features I've added:
Proper use of RDEPENDS in the recipes for dependency tracking. Packagegroups have been updated. core-selinux group is kept the same and the minimal system package group has only thge labeldev script which is all that's needed to boot.

Philip Tricca (4):
selinux-config: Separate init script into new recipe.
selinux-init: Move script logic into include.
selinux-init: Break labeling of /dev out into separate script.
selinux-init: Break handling of /.autorelabel out into separate
script.

 .../packagegroups/packagegroup-core-selinux.bb     |  3 +
 .../packagegroups/packagegroup-selinux-minimal.bb  |  1 +
 .../selinux-autorelabel/selinux-autorelabel.sh     | 22 +++++++
 .../selinux/selinux-autorelabel_0.1.bb             | 17 ++++++
 .../selinux/selinux-config/selinux-init.sh         | 71 ----------------------
 recipes-security/selinux/selinux-config_0.1.bb     | 14 +----
 .../selinux/selinux-init/selinux-init.sh           | 50 +++++++++++++++
 recipes-security/selinux/selinux-init_0.1.bb       | 20 ++++++
 recipes-security/selinux/selinux-initsh.inc        | 25 ++++++++
 .../selinux/selinux-labeldev/selinux-labeldev.sh   | 24 ++++++++
 recipes-security/selinux/selinux-labeldev_0.1.bb   | 16 +++++
 11 files changed, 179 insertions(+), 84 deletions(-)
 create mode 100644 recipes-security/selinux/selinux-autorelabel/selinux-autorelabel.sh
 create mode 100644 recipes-security/selinux/selinux-autorelabel_0.1.bb
 delete mode 100644 recipes-security/selinux/selinux-config/selinux-init.sh
 create mode 100644 recipes-security/selinux/selinux-init/selinux-init.sh
 create mode 100644 recipes-security/selinux/selinux-init_0.1.bb
 create mode 100644 recipes-security/selinux/selinux-initsh.inc
 create mode 100644 recipes-security/selinux/selinux-labeldev/selinux-labeldev.sh
 create mode 100644 recipes-security/selinux/selinux-labeldev_0.1.bb
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/4 selinux-config: Separate init script into new recipe.

Add runtime dependencies for init script.

---
 .../packagegroups/packagegroup-core-selinux.bb     |  1 +
 .../packagegroups/packagegroup-selinux-minimal.bb  |  1 +
 .../selinux/selinux-config/selinux-init.sh         | 71 ----------------------
 recipes-security/selinux/selinux-config_0.1.bb     | 14 +----
 .../selinux/selinux-init/selinux-init.sh           | 71 ++++++++++++++++++++++
 recipes-security/selinux/selinux-init_0.1.bb       | 37 +++++++++++
 6 files changed, 111 insertions(+), 84 deletions(-)
 delete mode 100644 recipes-security/selinux/selinux-config/selinux-init.sh
 create mode 100644 recipes-security/selinux/selinux-init/selinux-init.sh
 create mode 100644 recipes-security/selinux/selinux-init_0.1.bb
recipes-security/packagegroups/packagegroup-core-selinux.bb +1 -0
index 40b35d1..472bf55 100644
@@ -23,6 +23,7 @@ RDEPENDS_${PN} = " \
 	setools \
 	setools-console \
 	selinux-config \
+	selinux-init \
 	refpolicy-standard \
 	refpolicy-mls \
 	coreutils \
recipes-security/packagegroups/packagegroup-selinux-minimal.bb +1 -0
index 2ff16f8..42fb82d 100644
@@ -22,5 +22,6 @@ RDEPENDS_${PN} = "\
 	policycoreutils-sestatus \
 	policycoreutils-setfiles \
 	selinux-config \
+	selinux-init \
 	refpolicy-mls \
 "
recipes-security/selinux/selinux-config/selinux-init.sh deleted +0 -71

71 lines not on this page · raw message

recipes-security/selinux/selinux-config_0.1.bb +1 -13
index f77b490..37fe4b7 100644
@@ -13,23 +13,11 @@ PR = "r4"
 
 S = "${WORKDIR}"
 
-SRC_URI = "file://selinux-init.sh"
-
-inherit update-rc.d
-
-INITSCRIPT_NAME = "0selinux-init"
-INITSCRIPT_PARAMS = "start 00 S ."
-
-CONFFILES_${PN} += "${sysconfdir}/selinux/config \
-	${sysconfdir}/init.d/0selinux-init \
-	"
+CONFFILES_${PN} += "${sysconfdir}/selinux/config"
 
 PACKAGE_ARCH = "${MACHINE_ARCH}"
 
 do_install () {
-	install -d ${D}${sysconfdir}/init.d/
-	install -m 0755 ${WORKDIR}/selinux-init.sh ${D}${sysconfdir}/init.d/0selinux-init
-
 	echo "\
 # This file controls the state of SELinux on the system.
 # SELINUX= can take one of these three values:
recipes-security/selinux/selinux-init/selinux-init.sh new file +71 -0

71 lines not on this page · raw message

recipes-security/selinux/selinux-init_0.1.bb new file +37 -0
new file mode 100644
index 0000000..d8e4944
@@ -0,0 +1,37 @@
+SUMMARY = "SELinux init script"
+DESCRIPTION = "\
+SELinux start up stuff for Yocto. \
+"
+
+SECTION = "base"
+LICENSE = "MIT"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
+
+${PN}_RDEPENDS = " \
+    coreutils \
+    libselinux-bin \
+    policycoreutils-secon \
+    policycoreutils-setfiles \
+"
+
+S = "${WORKDIR}"
+
+SRC_URI = "file://selinux-init.sh"
+
+inherit update-rc.d
+
+INITSCRIPT_NAME = "0selinux-init"
+INITSCRIPT_PARAMS = "start 00 S ."
+
+CONFFILES_${PN} += "${sysconfdir}/init.d/0selinux-init"
+
+PACKAGE_ARCH = "${MACHINE_ARCH}"
+
+do_install () {
+	install -d ${D}${sysconfdir}/init.d/
+	install -m 0755 ${WORKDIR}/selinux-init.sh ${D}${sysconfdir}/init.d/0selinux-init
+}
+
+sysroot_stage_all_append () {
+	sysroot_stage_dir ${D}${sysconfdir} ${SYSROOT_DESTDIR}${sysconfdir}
+}
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/4 selinux-init: Move script logic into include.

In reply to Philip Tricca

This will be useful when we have other init scripts.

---
 recipes-security/selinux/selinux-init_0.1.bb | 24 +++---------------------
 recipes-security/selinux/selinux-initsh.inc  | 25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 21 deletions(-)
 create mode 100644 recipes-security/selinux/selinux-initsh.inc
recipes-security/selinux/selinux-init_0.1.bb +3 -21
index d8e4944..cde142d 100644
@@ -3,7 +3,6 @@ DESCRIPTION = "\
 SELinux start up stuff for Yocto. \
 "
 
-SECTION = "base"
 LICENSE = "MIT"
 LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
 
@@ -14,24 +13,7 @@ ${PN}_RDEPENDS = " \
     policycoreutils-setfiles \
 "
 
-S = "${WORKDIR}"
+SRC_URI = "file://${BPN}.sh"
+SELINUX_SCRIPT_DST = "0${BPN}"
 
-SRC_URI = "file://selinux-init.sh"
-
-inherit update-rc.d
-
-INITSCRIPT_NAME = "0selinux-init"
-INITSCRIPT_PARAMS = "start 00 S ."
-
-CONFFILES_${PN} += "${sysconfdir}/init.d/0selinux-init"
-
-PACKAGE_ARCH = "${MACHINE_ARCH}"
-
-do_install () {
-	install -d ${D}${sysconfdir}/init.d/
-	install -m 0755 ${WORKDIR}/selinux-init.sh ${D}${sysconfdir}/init.d/0selinux-init
-}
-
-sysroot_stage_all_append () {
-	sysroot_stage_dir ${D}${sysconfdir} ${SYSROOT_DESTDIR}${sysconfdir}
-}
+require selinux-initsh.inc
recipes-security/selinux/selinux-initsh.inc new file +25 -0
new file mode 100644
index 0000000..f7348bb
@@ -0,0 +1,25 @@
+S ?= "${WORKDIR}"
+SECTION ?= "base"
+
+# Default is for script name to be the same as the recipe name.
+# Script must have .sh suffix.
+SELINUX_SCRIPT_SRC ?= "${BPN}"
+SELINUX_SCRIPT_DST ?= "${SELINUX_SCRIPT_SRC}"
+
+INITSCRIPT_NAME ?= "${SELINUX_SCRIPT_DST}"
+INITSCRIPT_PARAMS ?= "start 00 S ."
+
+CONFFILES_${PN} += "${sysconfdir}/init.d/${SELINUX_SCRIPT_DST}"
+
+PACKAGE_ARCH ?= "${MACHINE_ARCH}"
+
+inherit update-rc.d
+
+do_install () {
+	install -d ${D}${sysconfdir}/init.d/
+	install -m 0755 ${WORKDIR}/${SELINUX_SCRIPT_SRC}.sh ${D}${sysconfdir}/init.d/${SELINUX_SCRIPT_DST}
+}
+
+sysroot_stage_all_append () {
+	sysroot_stage_dir ${D}${sysconfdir} ${SYSROOT_DESTDIR}${sysconfdir}
+}
Signature · 1 line
2.1.4
#3 lore raw

PATCH 3/4 selinux-init: Break labeling of /dev out into separate script.

In reply to Philip Tricca

Remove selinux-init package from packagegroup-selinux-minimal.

---
 .../packagegroups/packagegroup-core-selinux.bb     |  1 +
 .../packagegroups/packagegroup-selinux-minimal.bb  |  2 +-
 .../selinux/selinux-init/selinux-init.sh           |  9 --------
 recipes-security/selinux/selinux-init_0.1.bb       |  2 +-
 .../selinux/selinux-labeldev/selinux-labeldev.sh   | 24 ++++++++++++++++++++++
 recipes-security/selinux/selinux-labeldev_0.1.bb   | 16 +++++++++++++++
 6 files changed, 43 insertions(+), 11 deletions(-)
 create mode 100644 recipes-security/selinux/selinux-labeldev/selinux-labeldev.sh
 create mode 100644 recipes-security/selinux/selinux-labeldev_0.1.bb
recipes-security/packagegroups/packagegroup-core-selinux.bb +1 -0
index 472bf55..e46cda7 100644
@@ -24,6 +24,7 @@ RDEPENDS_${PN} = " \
 	setools-console \
 	selinux-config \
 	selinux-init \
+	selinux-labeldev \
 	refpolicy-standard \
 	refpolicy-mls \
 	coreutils \
recipes-security/packagegroups/packagegroup-selinux-minimal.bb +1 -1
index 42fb82d..34c5f7c 100644
@@ -22,6 +22,6 @@ RDEPENDS_${PN} = "\
 	policycoreutils-sestatus \
 	policycoreutils-setfiles \
 	selinux-config \
-	selinux-init \
+	selinux-labeldev \
 	refpolicy-mls \
 "
recipes-security/selinux/selinux-init/selinux-init.sh +0 -9
index f9f0914..32c4de1 100644
@@ -34,12 +34,6 @@ check_rootfs()
 	/sbin/shutdown -f -h now
 }
 
-# Because /dev/console is not relabeled by kernel, many commands
-# would can not use it, including restorecon.
-${CHCON} -t `${MATCHPATHCON} -n /dev/null | cut -d: -f3` /dev/null
-${CHCON} -t `${MATCHPATHCON} -n /dev/console | cut -d: -f3` /dev/console
-
-
 # If /.autorelabel placed, the whole file system should be relabeled
 if [ -f /.autorelabel ]; then
 	echo "Checking SELinux security contexts:"
@@ -65,7 +59,4 @@ if [ "`${SECON} -t --pid 1`" = "kernel_t" ]; then
 	/sbin/reboot
 fi
 
-# Now, we should relabel /dev for most services.
-${RESTORECON} -RF /dev
-
 exit 0
recipes-security/selinux/selinux-init_0.1.bb +1 -1
index cde142d..87f8dad 100644
@@ -14,6 +14,6 @@ ${PN}_RDEPENDS = " \
 "
 
 SRC_URI = "file://${BPN}.sh"
-SELINUX_SCRIPT_DST = "0${BPN}"
+INITSCRIPT_PARAMS = "start 01 S ."
 
 require selinux-initsh.inc
recipes-security/selinux/selinux-labeldev/selinux-labeldev.sh new file +24 -0
new file mode 100644
index 0000000..62e7a42
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+/usr/sbin/selinuxenabled 2>/dev/null || exit 0
+
+CHCON=/usr/bin/chcon
+MATCHPATHCON=/usr/sbin/matchpathcon
+RESTORECON=/sbin/restorecon
+
+for i in ${CHCON} ${MATCHPATHCON} ${RESTORECON}; do
+	test -x $i && continue
+	echo "$i is missing in the system."
+	echo "Please add \"selinux=0\" in the kernel command line to disable SELinux."
+	exit 1
+done
+
+# Because /dev/console is not relabeled by kernel, many commands
+# would can not use it, including restorecon.
+${CHCON} -t `${MATCHPATHCON} -n /dev/null | cut -d: -f3` /dev/null
+${CHCON} -t `${MATCHPATHCON} -n /dev/console | cut -d: -f3` /dev/console
+
+# Now, we should relabel /dev for most services.
+${RESTORECON} -RF /dev
+
+exit 0
recipes-security/selinux/selinux-labeldev_0.1.bb new file +16 -0
new file mode 100644
index 0000000..b692bb2
@@ -0,0 +1,16 @@
+SUMMARY = "SELinux init script"
+DESCRIPTION = "Set SELinux labels for /dev."
+
+LICENSE = "MIT"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
+
+${PN}_RDEPENDS = " \
+    coreutils \
+    libselinux-bin \
+    policycoreutils-setfiles \
+"
+
+SRC_URI = "file://${BPN}.sh"
+SELINUX_SCRIPT_DST = "0${BPN}"
+
+require selinux-initsh.inc
Signature · 1 line
2.1.4
#4 lore raw

PATCH 4/4 selinux-init: Break handling of /.autorelabel out into separate script.

In reply to Philip Tricca

Fixup DESCRIPTION in old selinux-init recipe. Exclude this autorelabel script from the minimal packagegroup.

---
 .../packagegroups/packagegroup-core-selinux.bb     |  1 +
 .../selinux-autorelabel/selinux-autorelabel.sh     | 22 ++++++++++++++++++++++
 .../selinux/selinux-autorelabel_0.1.bb             | 17 +++++++++++++++++
 .../selinux/selinux-init/selinux-init.sh           | 14 +-------------
 recipes-security/selinux/selinux-init_0.1.bb       |  3 ++-
 5 files changed, 43 insertions(+), 14 deletions(-)
 create mode 100644 recipes-security/selinux/selinux-autorelabel/selinux-autorelabel.sh
 create mode 100644 recipes-security/selinux/selinux-autorelabel_0.1.bb
recipes-security/packagegroups/packagegroup-core-selinux.bb +1 -0
index e46cda7..a2480a3 100644
@@ -23,6 +23,7 @@ RDEPENDS_${PN} = " \
 	setools \
 	setools-console \
 	selinux-config \
+	selinux-autorelabel \
 	selinux-init \
 	selinux-labeldev \
 	refpolicy-standard \
recipes-security/selinux/selinux-autorelabel/selinux-autorelabel.sh new file +22 -0
new file mode 100644
index 0000000..154dad1
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+/usr/sbin/selinuxenabled 2>/dev/null || exit 0
+
+FIXFILES=/sbin/fixfiles
+
+if ! test -x ${FIXFILES}; then
+	echo "${FIXFILES} is missing in the system."
+	echo "Please add \"selinux=0\" in the kernel command line to disable SELinux."
+	exit 1
+fi
+
+# If /.autorelabel placed, the whole file system should be relabeled
+if [ -f /.autorelabel ]; then
+	echo "SELinux: /.autorelabel placed, filesystem will be relabeled..."
+	${FIXFILES} -F -f relabel
+	/bin/rm -f /.autorelabel
+	echo " * Relabel done, rebooting the system."
+	/sbin/reboot
+fi
+
+exit 0
recipes-security/selinux/selinux-autorelabel_0.1.bb new file +17 -0
new file mode 100644
index 0000000..2664863
@@ -0,0 +1,17 @@
+SUMMARY = "SELinux autorelabel script"
+DESCRIPTION = "\
+Script to reset SELinux labels on the root file system when /.autorelabel \
+file is present.\
+"
+
+LICENSE = "MIT"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
+
+${PN}_RDEPENDS = " \
+    policycoreutils-setfiles \
+"
+
+SRC_URI = "file://${BPN}.sh"
+INITSCRIPT_PARAMS = "start 01 S ."
+
+require selinux-initsh.inc
recipes-security/selinux/selinux-init/selinux-init.sh +1 -13
index 32c4de1..ead4f00 100644
@@ -4,12 +4,11 @@
 
 CHCON=/usr/bin/chcon
 MATCHPATHCON=/usr/sbin/matchpathcon
-FIXFILES=/sbin/fixfiles
 RESTORECON=/sbin/restorecon
 SECON=/usr/bin/secon
 SETENFORCE=/usr/sbin/setenforce
 
-for i in ${CHCON} ${MATCHPATHCON} ${FIXFILES} ${RESTORECON} ${SECON} ${SETENFORCE}; do
+for i in ${CHCON} ${MATCHPATHCON} ${RESTORECON} ${SECON} ${SETENFORCE}; do
 	test -x $i && continue
 	echo "$i is missing in the system."
 	echo "Please add \"selinux=0\" in the kernel command line to disable SELinux."
@@ -34,17 +33,6 @@ check_rootfs()
 	/sbin/shutdown -f -h now
 }
 
-# If /.autorelabel placed, the whole file system should be relabeled
-if [ -f /.autorelabel ]; then
-	echo "Checking SELinux security contexts:"
-	check_rootfs
-	echo " * /.autorelabel placed, filesystem will be relabeled..."
-	${FIXFILES} -F -f relabel
-	/bin/rm -f /.autorelabel
-	echo " * Relabel done, rebooting the system."
-	/sbin/reboot
-fi
-
 # If first booting, the security context type of init would be
 # "kernel_t", and the whole file system should be relabeled.
 if [ "`${SECON} -t --pid 1`" = "kernel_t" ]; then
recipes-security/selinux/selinux-init_0.1.bb +2 -1
index 87f8dad..54932e8 100644
@@ -1,6 +1,7 @@
 SUMMARY = "SELinux init script"
 DESCRIPTION = "\
-SELinux start up stuff for Yocto. \
+Script to detect and attempt to correct a misconfigured SELinux system at \
+boot time. \
 "
 
 LICENSE = "MIT"
Signature · 1 line
2.1.4