Blame SOURCES/0007-backport-GH2694-secure-execution-enablement-s390x.patch

f4b9f1
From 00697be199c08242e54c02e4557e20834030aaf3 Mon Sep 17 00:00:00 2001
f4b9f1
From: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
Date: Mon, 4 Apr 2022 16:09:50 +0200
f4b9f1
Subject: [PATCH 1/5] s390x: generate sd-boot at its own partition
f4b9f1
f4b9f1
Signed-off-by: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
---
f4b9f1
 src/libostree/ostree-bootloader-zipl.c | 36 ++++++++++++++++++++++----
f4b9f1
 src/libostree/s390x-se-luks-gencpio    |  4 +--
f4b9f1
 2 files changed, 33 insertions(+), 7 deletions(-)
f4b9f1
f4b9f1
diff --git a/src/libostree/ostree-bootloader-zipl.c b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
index 02c10826c3..fe024d8046 100644
f4b9f1
--- a/src/libostree/ostree-bootloader-zipl.c
f4b9f1
+++ b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
@@ -21,12 +21,17 @@
f4b9f1
 #include "ostree-bootloader-zipl.h"
f4b9f1
 #include "ostree-deployment-private.h"
f4b9f1
 #include "otutil.h"
f4b9f1
+#include <sys/mount.h>
f4b9f1
+#include <sys/stat.h>
f4b9f1
 #include <string.h>
f4b9f1
 
f4b9f1
-#define SECURE_EXECUTION_BOOT_IMAGE     "/boot/sd-boot"
f4b9f1
+#define SECURE_EXECUTION_PARTITION      "/dev/disk/by-label/se"
f4b9f1
+#define SECURE_EXECUTION_MOUNTPOINT     "/sysroot/se"
f4b9f1
+#define SECURE_EXECUTION_BOOT_IMAGE     SECURE_EXECUTION_MOUNTPOINT "/sd-boot"
f4b9f1
 #define SECURE_EXECUTION_HOSTKEY_PATH   "/etc/se-hostkeys/"
f4b9f1
 #define SECURE_EXECUTION_HOSTKEY_PREFIX "ibm-z-hostkey"
f4b9f1
 #define SECURE_EXECUTION_LUKS_ROOT_KEY  "/etc/luks/root"
f4b9f1
+#define SECURE_EXECUTION_LUKS_BOOT_KEY  "/etc/luks/boot"
f4b9f1
 #define SECURE_EXECUTION_LUKS_CONFIG    "/etc/crypttab"
f4b9f1
 #define SECURE_EXECUTION_RAMDISK_TOOL   PKGLIBEXECDIR "/s390x-se-luks-gencpio"
f4b9f1
 
f4b9f1
@@ -67,6 +72,25 @@ _ostree_bootloader_zipl_get_name (OstreeBootloader *bootloader)
f4b9f1
   return "zipl";
f4b9f1
 }
f4b9f1
 
f4b9f1
+static gboolean
f4b9f1
+_ostree_secure_execution_mount(GError **error)
f4b9f1
+{
f4b9f1
+  const char *device = realpath (SECURE_EXECUTION_PARTITION, NULL);
f4b9f1
+  if (device == NULL)
f4b9f1
+    return glnx_throw_errno_prefix(error, "s390x SE: resolving %s", SECURE_EXECUTION_PARTITION);
f4b9f1
+  if (mount (device, SECURE_EXECUTION_MOUNTPOINT, "ext4", 0, NULL) < 0)
f4b9f1
+    return glnx_throw_errno_prefix (error, "s390x SE: Mounting %s", device);
f4b9f1
+  return TRUE;
f4b9f1
+}
f4b9f1
+
f4b9f1
+static gboolean
f4b9f1
+_ostree_secure_execution_umount(GError **error)
f4b9f1
+{
f4b9f1
+  if (umount (SECURE_EXECUTION_MOUNTPOINT) < 0)
f4b9f1
+    return glnx_throw_errno_prefix (error, "s390x SE: Unmounting %s", SECURE_EXECUTION_MOUNTPOINT);
f4b9f1
+  return TRUE;
f4b9f1
+}
f4b9f1
+
f4b9f1
 static gboolean
f4b9f1
 _ostree_bootloader_zipl_write_config (OstreeBootloader  *bootloader,
f4b9f1
                                           int                bootversion,
f4b9f1
@@ -152,8 +176,8 @@ _ostree_secure_execution_get_bls_config (OstreeBootloaderZipl *self,
f4b9f1
 static gboolean
f4b9f1
 _ostree_secure_execution_luks_key_exists (void)
f4b9f1
 {
f4b9f1
-  return (access(SECURE_EXECUTION_LUKS_ROOT_KEY, F_OK) == 0 &&
f4b9f1
-          access(SECURE_EXECUTION_LUKS_CONFIG, F_OK) == 0);
f4b9f1
+  return (access(SECURE_EXECUTION_LUKS_CONFIG, F_OK) == 0 &&
f4b9f1
+    (access(SECURE_EXECUTION_LUKS_ROOT_KEY, F_OK) == 0 || access(SECURE_EXECUTION_LUKS_BOOT_KEY, F_OK) == 0));
f4b9f1
 }
f4b9f1
 
f4b9f1
 static gboolean
f4b9f1
@@ -250,7 +274,7 @@ static gboolean
f4b9f1
 _ostree_secure_execution_call_zipl (GError **error)
f4b9f1
 {
f4b9f1
   int status = 0;
f4b9f1
-  const char *const zipl_argv[] = {"zipl", "-V", "-t", "/boot", "-i", SECURE_EXECUTION_BOOT_IMAGE, NULL};
f4b9f1
+  const char *const zipl_argv[] = {"zipl", "-V", "-t", SECURE_EXECUTION_MOUNTPOINT, "-i", SECURE_EXECUTION_BOOT_IMAGE, NULL};
f4b9f1
   if (!g_spawn_sync (NULL, (char**)zipl_argv, NULL, G_SPAWN_SEARCH_PATH,
f4b9f1
                        NULL, NULL, NULL, NULL, &status, error))
f4b9f1
     return glnx_prefix_error(error, "s390x SE: spawning zipl");
f4b9f1
@@ -274,9 +298,11 @@ _ostree_secure_execution_enable (OstreeBootloaderZipl *self,
f4b9f1
   g_autofree gchar* options = NULL;
f4b9f1
 
f4b9f1
   gboolean rc =
f4b9f1
+      _ostree_secure_execution_mount (error) &&
f4b9f1
       _ostree_secure_execution_get_bls_config (self, bootversion, &vmlinuz, &initramfs, &options, cancellable, error) &&
f4b9f1
       _ostree_secure_execution_generate_sdboot (vmlinuz, initramfs, options, keys, error) &&
f4b9f1
-      _ostree_secure_execution_call_zipl (error);
f4b9f1
+      _ostree_secure_execution_call_zipl (error) &&
f4b9f1
+      _ostree_secure_execution_umount (error);
f4b9f1
 
f4b9f1
   return rc;
f4b9f1
 }
f4b9f1
diff --git a/src/libostree/s390x-se-luks-gencpio b/src/libostree/s390x-se-luks-gencpio
f4b9f1
index f0ad24eb32..7d62258a31 100755
f4b9f1
--- a/src/libostree/s390x-se-luks-gencpio
f4b9f1
+++ b/src/libostree/s390x-se-luks-gencpio
f4b9f1
@@ -12,11 +12,11 @@ gzip -cd ${old_initrd} | cpio -imd --quiet
f4b9f1
 
f4b9f1
 # Adding LUKS root key and crypttab config
f4b9f1
 mkdir -p etc/luks
f4b9f1
-cp -f /etc/luks/root etc/luks/
f4b9f1
+cp -f /etc/luks/* etc/luks/
f4b9f1
 cp -f /etc/crypttab etc/
f4b9f1
 
f4b9f1
 # Creating new initramdisk image
f4b9f1
-find . | cpio --quiet -H newc -o | gzip -9 -n >> ${new_initrd}
f4b9f1
+find . -mindepth 1 | cpio --quiet -H newc -o | gzip -9 -n >> ${new_initrd}
f4b9f1
 
f4b9f1
 # Cleanup
f4b9f1
 rm -rf ${workdir}
f4b9f1
f4b9f1
From 91e71022ebc2422f278c285e55f4c88d7f572eeb Mon Sep 17 00:00:00 2001
f4b9f1
From: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
Date: Mon, 23 May 2022 17:28:54 +0200
f4b9f1
Subject: [PATCH 2/5] s390x: ensure SecureExecution is enabled before sd-boot
f4b9f1
 generation
f4b9f1
f4b9f1
Signed-off-by: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
---
f4b9f1
 src/libostree/ostree-bootloader-zipl.c | 24 ++++++++++++++++++------
f4b9f1
 1 file changed, 18 insertions(+), 6 deletions(-)
f4b9f1
f4b9f1
diff --git a/src/libostree/ostree-bootloader-zipl.c b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
index fe024d8046..348dfe036d 100644
f4b9f1
--- a/src/libostree/ostree-bootloader-zipl.c
f4b9f1
+++ b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
@@ -25,6 +25,7 @@
f4b9f1
 #include <sys/stat.h>
f4b9f1
 #include <string.h>
f4b9f1
 
f4b9f1
+#define SECURE_EXECUTION_SYSFS_FLAG     "/sys/firmware/uv/prot_virt_guest"
f4b9f1
 #define SECURE_EXECUTION_PARTITION      "/dev/disk/by-label/se"
f4b9f1
 #define SECURE_EXECUTION_MOUNTPOINT     "/sysroot/se"
f4b9f1
 #define SECURE_EXECUTION_BOOT_IMAGE     SECURE_EXECUTION_MOUNTPOINT "/sd-boot"
f4b9f1
@@ -109,6 +110,14 @@ _ostree_bootloader_zipl_write_config (OstreeBootloader  *bootloader,
f4b9f1
   return TRUE;
f4b9f1
 }
f4b9f1
 
f4b9f1
+static gboolean _ostree_secure_execution_is_enabled (GCancellable *cancellable) {
f4b9f1
+  gsize len = 0;
f4b9f1
+  g_autofree char *data = glnx_file_get_contents_utf8_at (-1, SECURE_EXECUTION_SYSFS_FLAG, &len, cancellable, NULL);
f4b9f1
+  if (!data)
f4b9f1
+    return FALSE;
f4b9f1
+  return strstr (data, "1") != NULL;
f4b9f1
+}
f4b9f1
+
f4b9f1
 static gboolean
f4b9f1
 _ostree_secure_execution_get_keys (GPtrArray **keys,
f4b9f1
                                    GCancellable *cancellable,
f4b9f1
@@ -329,12 +338,15 @@ _ostree_bootloader_zipl_post_bls_sync (OstreeBootloader  *bootloader,
f4b9f1
     return TRUE;
f4b9f1
 
f4b9f1
   /* Try with Secure Execution */
f4b9f1
-  g_autoptr(GPtrArray) keys = NULL;
f4b9f1
-  if (!_ostree_secure_execution_get_keys (&keys, cancellable, error))
f4b9f1
-    return FALSE;
f4b9f1
-  if (keys && keys->len)
f4b9f1
-    return _ostree_secure_execution_enable (self, bootversion, keys, cancellable, error);
f4b9f1
-
f4b9f1
+  if ( _ostree_secure_execution_is_enabled (cancellable) )
f4b9f1
+    {
f4b9f1
+      g_autoptr(GPtrArray) keys = NULL;
f4b9f1
+      if (!_ostree_secure_execution_get_keys (&keys, cancellable, error))
f4b9f1
+        return FALSE;
f4b9f1
+      if (!keys || keys->len == 0)
f4b9f1
+          return glnx_throw (error, "s390x SE: no keys");
f4b9f1
+      return _ostree_secure_execution_enable (self, bootversion, keys, cancellable, error);
f4b9f1
+    }
f4b9f1
   /* Fallback to non-SE setup */
f4b9f1
   const char *const zipl_argv[] = {"zipl", NULL};
f4b9f1
   int estatus;
f4b9f1
f4b9f1
From 2e2854239189044cc1ffd100959b7c7bfe92b0f9 Mon Sep 17 00:00:00 2001
f4b9f1
From: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
Date: Tue, 24 May 2022 19:30:35 +0200
f4b9f1
Subject: [PATCH 3/5] s390x: fail on error during reading of SecureExecution
f4b9f1
 sysfs flag
f4b9f1
f4b9f1
---
f4b9f1
 src/libostree/ostree-bootloader-zipl.c | 24 ++++++++++++++++++------
f4b9f1
 1 file changed, 18 insertions(+), 6 deletions(-)
f4b9f1
f4b9f1
diff --git a/src/libostree/ostree-bootloader-zipl.c b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
index 348dfe036d..87b9b67aec 100644
f4b9f1
--- a/src/libostree/ostree-bootloader-zipl.c
f4b9f1
+++ b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
@@ -110,12 +110,21 @@ _ostree_bootloader_zipl_write_config (OstreeBootloader  *bootloader,
f4b9f1
   return TRUE;
f4b9f1
 }
f4b9f1
 
f4b9f1
-static gboolean _ostree_secure_execution_is_enabled (GCancellable *cancellable) {
f4b9f1
-  gsize len = 0;
f4b9f1
-  g_autofree char *data = glnx_file_get_contents_utf8_at (-1, SECURE_EXECUTION_SYSFS_FLAG, &len, cancellable, NULL);
f4b9f1
+static gboolean _ostree_secure_execution_is_enabled (gboolean *out_enabled,
f4b9f1
+                                                     GCancellable *cancellable,
f4b9f1
+                                                     GError **error)
f4b9f1
+{
f4b9f1
+  *out_enabled = FALSE;
f4b9f1
+  glnx_autofd int fd = -1;
f4b9f1
+  if (!ot_openat_ignore_enoent (AT_FDCWD, SECURE_EXECUTION_SYSFS_FLAG, &fd, error))
f4b9f1
+    return FALSE;
f4b9f1
+  if (fd == -1)
f4b9f1
+    return TRUE; //ENOENT --> SecureExecution is disabled
f4b9f1
+  g_autofree char *data = glnx_fd_readall_utf8 (fd, NULL, cancellable, error);
f4b9f1
   if (!data)
f4b9f1
     return FALSE;
f4b9f1
-  return strstr (data, "1") != NULL;
f4b9f1
+  *out_enabled = strstr (data, "1") != NULL;
f4b9f1
+  return TRUE;
f4b9f1
 }
f4b9f1
 
f4b9f1
 static gboolean
f4b9f1
@@ -338,13 +347,16 @@ _ostree_bootloader_zipl_post_bls_sync (OstreeBootloader  *bootloader,
f4b9f1
     return TRUE;
f4b9f1
 
f4b9f1
   /* Try with Secure Execution */
f4b9f1
-  if ( _ostree_secure_execution_is_enabled (cancellable) )
f4b9f1
+  gboolean se_enabled = FALSE;
f4b9f1
+  if ( !_ostree_secure_execution_is_enabled (&se_enabled, cancellable, error))
f4b9f1
+    return FALSE;
f4b9f1
+  if (se_enabled)
f4b9f1
     {
f4b9f1
       g_autoptr(GPtrArray) keys = NULL;
f4b9f1
       if (!_ostree_secure_execution_get_keys (&keys, cancellable, error))
f4b9f1
         return FALSE;
f4b9f1
       if (!keys || keys->len == 0)
f4b9f1
-          return glnx_throw (error, "s390x SE: no keys");
f4b9f1
+        return glnx_throw (error, "s390x SE: no keys");
f4b9f1
       return _ostree_secure_execution_enable (self, bootversion, keys, cancellable, error);
f4b9f1
     }
f4b9f1
   /* Fallback to non-SE setup */
f4b9f1
f4b9f1
From 89ed46e8a9f584e2a6c1966fbf4c99f0fe51424e Mon Sep 17 00:00:00 2001
f4b9f1
From: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
Date: Fri, 27 May 2022 09:13:18 +0200
f4b9f1
Subject: [PATCH 4/5] s390x: do not unpack existing initrd, just append LUKS
f4b9f1
 keys to its copy
f4b9f1
f4b9f1
Signed-off-by: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
---
f4b9f1
 src/libostree/s390x-se-luks-gencpio | 12 ++++++------
f4b9f1
 1 file changed, 6 insertions(+), 6 deletions(-)
f4b9f1
f4b9f1
diff --git a/src/libostree/s390x-se-luks-gencpio b/src/libostree/s390x-se-luks-gencpio
f4b9f1
index 7d62258a31..f444198a40 100755
f4b9f1
--- a/src/libostree/s390x-se-luks-gencpio
f4b9f1
+++ b/src/libostree/s390x-se-luks-gencpio
f4b9f1
@@ -4,19 +4,19 @@ set -euo pipefail
f4b9f1
 
f4b9f1
 old_initrd=$1
f4b9f1
 new_initrd=$2
f4b9f1
+currdir=$PWD
f4b9f1
 
f4b9f1
-# Unpacking existing initramdisk
f4b9f1
+# Copying existing initramdisk
f4b9f1
+cp ${old_initrd} ${new_initrd}
f4b9f1
+
f4b9f1
+# Appending LUKS root keys and crypttab config to the end of initrd
f4b9f1
 workdir=$(mktemp -d -p /tmp se-initramfs-XXXXXX)
f4b9f1
 cd ${workdir}
f4b9f1
-gzip -cd ${old_initrd} | cpio -imd --quiet
f4b9f1
-
f4b9f1
-# Adding LUKS root key and crypttab config
f4b9f1
 mkdir -p etc/luks
f4b9f1
 cp -f /etc/luks/* etc/luks/
f4b9f1
 cp -f /etc/crypttab etc/
f4b9f1
-
f4b9f1
-# Creating new initramdisk image
f4b9f1
 find . -mindepth 1 | cpio --quiet -H newc -o | gzip -9 -n >> ${new_initrd}
f4b9f1
 
f4b9f1
 # Cleanup
f4b9f1
+cd ${currdir}
f4b9f1
 rm -rf ${workdir}
f4b9f1
f4b9f1
From 2c8d5b95c7f2fee90e73bdd9222e002c44e797b7 Mon Sep 17 00:00:00 2001
f4b9f1
From: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
Date: Thu, 23 Jun 2022 15:54:04 +0200
f4b9f1
Subject: [PATCH 5/5] s390x: rename sd-boot to sdboot
f4b9f1
f4b9f1
Signed-off-by: Nikita Dubrovskii <nikita@linux.ibm.com>
f4b9f1
---
f4b9f1
 src/libostree/ostree-bootloader-zipl.c | 2 +-
f4b9f1
 1 file changed, 1 insertion(+), 1 deletion(-)
f4b9f1
f4b9f1
diff --git a/src/libostree/ostree-bootloader-zipl.c b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
index 87b9b67aec..0ff350f942 100644
f4b9f1
--- a/src/libostree/ostree-bootloader-zipl.c
f4b9f1
+++ b/src/libostree/ostree-bootloader-zipl.c
f4b9f1
@@ -28,7 +28,7 @@
f4b9f1
 #define SECURE_EXECUTION_SYSFS_FLAG     "/sys/firmware/uv/prot_virt_guest"
f4b9f1
 #define SECURE_EXECUTION_PARTITION      "/dev/disk/by-label/se"
f4b9f1
 #define SECURE_EXECUTION_MOUNTPOINT     "/sysroot/se"
f4b9f1
-#define SECURE_EXECUTION_BOOT_IMAGE     SECURE_EXECUTION_MOUNTPOINT "/sd-boot"
f4b9f1
+#define SECURE_EXECUTION_BOOT_IMAGE     SECURE_EXECUTION_MOUNTPOINT "/sdboot"
f4b9f1
 #define SECURE_EXECUTION_HOSTKEY_PATH   "/etc/se-hostkeys/"
f4b9f1
 #define SECURE_EXECUTION_HOSTKEY_PREFIX "ibm-z-hostkey"
f4b9f1
 #define SECURE_EXECUTION_LUKS_ROOT_KEY  "/etc/luks/root"