f226d6
From a0d8caa8090a78f627f26fcd9b47c4b099cbc1ba Mon Sep 17 00:00:00 2001
f226d6
From: Jonathan Lebon <jonathan@jlebon.com>
f226d6
Date: Thu, 17 Jun 2021 10:47:33 -0400
f226d6
Subject: [PATCH] fix(fips): handle s390x OSTree systems
f226d6
f226d6
On s390x, the `BOOT_IMAGE` karg injected by the bootloader is not a path
f226d6
to the kernel image, but rather an integer describing the index of the
f226d6
menu entry selected. Because of the way the s390x bootloader works,
f226d6
there is no information retained about e.g. the path of the kernel that
f226d6
was loaded.
f226d6
f226d6
This causes issues for the FIPS code which assumes that `BOOT_IMAGE` is
f226d6
a path to the kernel image to derive the HMAC path. In non-OSTree
f226d6
systems, this ends up working anyway, because the kernel is located at
f226d6
the root of the boot partition.  In OSTree systems, this is not the
f226d6
case. However, OSTree systems use BLS configs, and they are named in
f226d6
reverse order of precedence (i.e. menu ordering). So from the
f226d6
`BOOT_IMAGE` integer, we can figure out which BLS entry was selected.
f226d6
f226d6
Add some code to do just this on s390x. This isn't completely foolproof,
f226d6
because it presumes that (1) BLS configs were used to populate the
f226d6
bootloader (and that they were exactly in the same state they currently
f226d6
are when `zipl` was run), and (2) there are no other menu entries
f226d6
originating from outside the BLS configs. However, if these assumptions
f226d6
are wrong we would simply fail the boot, which is currently what is
f226d6
happening anyway.
f226d6
f226d6
See also:
f226d6
https://github.com/openshift/os/pull/546
f226d6
https://github.com/ibm-s390-linux/s390-tools/issues/78
f226d6
f226d6
Tested-by: Muhammad Adeel <muhammad.adeel@ibm.com>
f226d6
f226d6
Resolves: rhbz#2050567
f226d6
---
f226d6
 modules.d/01fips/fips.sh         | 21 +++++++++++++++++++++
f226d6
 modules.d/01fips/module-setup.sh |  2 +-
f226d6
 2 files changed, 22 insertions(+), 1 deletion(-)
f226d6
f226d6
diff --git a/modules.d/01fips/fips.sh b/modules.d/01fips/fips.sh
f226d6
index 821c26a2..3297cb17 100755
f226d6
--- a/modules.d/01fips/fips.sh
f226d6
+++ b/modules.d/01fips/fips.sh
f226d6
@@ -124,6 +124,27 @@ do_fips() {
f226d6
     else
f226d6
         BOOT_IMAGE="$(getarg BOOT_IMAGE)"
f226d6
 
f226d6
+        # On s390x, BOOT_IMAGE isn't a path but an integer representing the
f226d6
+        # entry number selected. Let's try the root of /boot first, and
f226d6
+        # otherwise fallback to trying to parse the BLS entries if it's a
f226d6
+        # BLS-based system.
f226d6
+        if [ "$(uname -m)" = s390x ]; then
f226d6
+            if [ -e "/boot/vmlinuz-${KERNEL}" ]; then
f226d6
+                BOOT_IMAGE="vmlinuz-${KERNEL}"
f226d6
+            elif [ -d /boot/loader/entries ]; then
f226d6
+                i=0
f226d6
+                for bls in $(ls -d /boot/loader/entries/*.conf | sort -rV); do
f226d6
+                  ((i++))
f226d6
+
f226d6
+                  if [ $i -eq ${BOOT_IMAGE:-0} ] && [ -r "$bls" ]; then
f226d6
+                      BOOT_IMAGE="$(grep -e '^linux' "$bls" | grep -o ' .*$')"
f226d6
+                      BOOT_IMAGE=${BOOT_IMAGE:1}
f226d6
+                      break
f226d6
+                  fi
f226d6
+                done
f226d6
+            fi
f226d6
+        fi
f226d6
+
f226d6
         # Trim off any leading GRUB boot device (e.g. ($root) )
f226d6
         BOOT_IMAGE="$(echo "${BOOT_IMAGE}" | sed 's/^(.*)//')"
f226d6
 
f226d6
diff --git a/modules.d/01fips/module-setup.sh b/modules.d/01fips/module-setup.sh
f226d6
index a1e499af..913a660c 100755
f226d6
--- a/modules.d/01fips/module-setup.sh
f226d6
+++ b/modules.d/01fips/module-setup.sh
f226d6
@@ -67,7 +67,7 @@ install() {
f226d6
     inst_hook pre-udev 01 "$moddir/fips-load-crypto.sh"
f226d6
     inst_script "$moddir/fips.sh" /sbin/fips.sh
f226d6
 
f226d6
-    inst_multiple sha512hmac rmmod insmod mount uname umount
f226d6
+    inst_multiple sha512hmac rmmod insmod mount uname umount grep sort
f226d6
 
f226d6
     inst_simple /etc/system-fips
f226d6
     [ -c "${initdir}"/dev/random ] || mknod "${initdir}"/dev/random c 1 8 \
f226d6