Skip to contentSkip to conversation

selinux-image.bbclass: refactor bbclass

PATCH New Yi Zhao 1 participant 3 messages Sep 22, 2023

lore
Share & tools
lore

Yi Zhao

author
#0 lore raw

PATCH 1/3 selinux-image.bbclass: refactor bbclass

The selinux_set_labels function should run as late as possible. To guarantee that, we append it to IMAGE_PREPROCESS_COMMAND in RecipePreFinalise event handler, this ensures it is the last function in IMAGE_PREPROCESS_COMMAND.

After refactoring, system using systemd can also label selinux contexts during build.

---
 classes/selinux-image.bbclass | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
classes/selinux-image.bbclass +23 -9
index 23645b7..b4f9321 100644
@@ -1,15 +1,29 @@
-selinux_set_labels () {
-    POL_TYPE=$(sed -n -e "s&^SELINUXTYPE[[:space:]]*=[[:space:]]*\([0-9A-Za-z_]\+\)&\1&p" ${IMAGE_ROOTFS}/${sysconfdir}/selinux/config)
-    if ! setfiles -m -r ${IMAGE_ROOTFS} ${IMAGE_ROOTFS}/${sysconfdir}/selinux/${POL_TYPE}/contexts/files/file_contexts ${IMAGE_ROOTFS}
-    then
-        echo WARNING: Unable to set filesystem context, setfiles / restorecon must be run on the live image.
-        touch ${IMAGE_ROOTFS}/.autorelabel
-        exit 0
+selinux_set_labels() {
+    if [ -f ${IMAGE_ROOTFS}/${sysconfdir}/selinux/config ]; then
+        POL_TYPE=$(sed -n -e "s&^SELINUXTYPE[[:space:]]*=[[:space:]]*\([0-9A-Za-z_]\+\)&\1&p" ${IMAGE_ROOTFS}/${sysconfdir}/selinux/config)
+        if ! setfiles -m -r ${IMAGE_ROOTFS} ${IMAGE_ROOTFS}/${sysconfdir}/selinux/${POL_TYPE}/contexts/files/file_contexts ${IMAGE_ROOTFS}
+        then
+            bbwarn "Failed to set security contexts. Restoring security contexts will run on first boot."
+            echo "# first boot relabelling" > ${IMAGE_ROOTFS}/.autorelabel
+        fi
     fi
 }
 
-DEPENDS += "policycoreutils-native"
+# The selinux_set_labels function should run as late as possible. Append
+# it to IMAGE_PREPROCESS_COMMAND in RecipePreFinalise event handler,
+# this ensures it is the last function in IMAGE_PREPROCESS_COMMAND.
+python selinux_setlabels_handler() {
+    if not d or 'selinux' not in d.getVar('DISTRO_FEATURES').split():
+        return
 
-IMAGE_PREPROCESS_COMMAND:append = " selinux_set_labels ;"
+    if d.getVar('FIRST_BOOT_RELABEL') == '1':
+        return
+
+    d.appendVar('IMAGE_PREPROCESS_COMMAND', ' selinux_set_labels; ')
+    d.appendVarFlag('do_image', 'depends', ' policycoreutils-native:do_populate_sysroot')
+}
+
+addhandler selinux_setlabels_handler
+selinux_setlabels_handler[eventmask] = "bb.event.RecipePreFinalise"
 
 inherit core-image
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/3 selinux-autorelabel: enable labeling during build

Previously, system using systemd would label selinux contexts on first boot. While system using sysvinit would label during build. Add a variable FIRST_BOOT_RELABEL as a switch to control labeling to make the behavior of sysvinit and systemd consistent.

Set FIRST_BOOT_RELABEL to 1 in local.conf to enable labeling on first boot.

---
 recipes-security/selinux-scripts/selinux-autorelabel_0.1.bb | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
recipes-security/selinux-scripts/selinux-autorelabel_0.1.bb +3 -3
index a919445..9fd066c 100644
@@ -20,7 +20,7 @@ INITSCRIPT_PARAMS = "start 01 S ."
 require selinux-initsh.inc
 
 do_install:append() {
-	if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then
-		echo "# first boot relabelling" > ${D}/.autorelabel
-	fi
+    if ${@bb.utils.contains('FIRST_BOOT_RELABEL', '1', 'true', 'false', d)}; then
+        echo "# first boot relabelling" > ${D}/.autorelabel
+    fi
 }
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH 3/3 README: update

In reply to Yi Zhao

Add how to enable labeling on first boot.

---
 README | 8 ++++++++
 1 file changed, 8 insertions(+)
README +8 -0
index 77b6253..67708f7 100644
@@ -75,6 +75,14 @@ VIRTUAL-RUNTIME_init_manager = "systemd"
 DISTRO_FEATURES_BACKFILL_CONSIDERED = ""
 
 
+Enable labeling on first boot
+----------------------------
+By default, the system will label selinux contexts during build. To enable
+labeling on first boot. Set FIRST_BOOT_RELABEL to 1 in local.conf:
+
+FIRST_BOOT_RELABEL = "1"
+
+
 Starting up the system
 ----------------------
 Most likely the reference policy selected will not just work "out of the box".
Signature · 1 line
2.25.1