Blame SOURCES/0005-deploy-Try-to-rebuild-policy-in-new-deployment-if-ne.patch

b89242
From 62e62bcfd8a1770b906faed083d11e451a50f566 Mon Sep 17 00:00:00 2001
b89242
From: Ondrej Mosnacek <omosnace@redhat.com>
b89242
Date: Wed, 9 Mar 2022 15:27:11 +0100
b89242
Subject: [PATCH 5/6] deploy: Try to rebuild policy in new deployment if needed
b89242
b89242
Whenever the user has SELinux enabled and has any local
b89242
modules/modifications installed, it is necessary to rebuild the policy
b89242
in the final deployment, otherwise ostree will leave the binary policy
b89242
files unchanged from last deployment as it detects difference against
b89242
the base content (in rpm-ostree case this is the RPM content).
b89242
b89242
To avoid the situation where the policy binaries go stale once any local
b89242
customization of the policy is made, try to rebuild the policy as part
b89242
of sysroot_finalize_deployment(). Use the special
b89242
--rebuild-if-modules-changed switch, which detects if the input module
b89242
files have changed relative to last time the policy was built and skips
b89242
the most time-consuming part of the rebuild process if modules are
b89242
unchanged (thus making this a relatively cheap operation if the user
b89242
hasn't made any modifications to the shipped policy).
b89242
b89242
As suggested by Jonathan Lebon, this uses bubblewrap (via
b89242
g_spawn_sync()) to perform the rebuild inside the deployment's
b89242
filesystem tree, which also means that ostree will have a runtime
b89242
dependency on bubblewrap.
b89242
b89242
Partially addresses: https://github.com/coreos/fedora-coreos-tracker/issues/701
b89242
b89242
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
b89242
(cherry picked from commit edb4f3893474736156c654aa43bdbf3784991811)
b89242
---
b89242
 ci/gh-install.sh                      |   1 +
b89242
 src/libostree/ostree-sysroot-deploy.c | 117 ++++++++++++++++++++++++++
b89242
 2 files changed, 118 insertions(+)
b89242
b89242
diff --git a/src/libostree/ostree-sysroot-deploy.c b/src/libostree/ostree-sysroot-deploy.c
b89242
index fc5916d8..a44721d8 100644
b89242
--- a/src/libostree/ostree-sysroot-deploy.c
b89242
+++ b/src/libostree/ostree-sysroot-deploy.c
b89242
@@ -2830,6 +2830,118 @@ get_var_dfd (OstreeSysroot      *self,
b89242
   return glnx_opendirat (base_dfd, base_path, TRUE, ret_fd, error);
b89242
 }
b89242
 
b89242
+#ifdef HAVE_SELINUX
b89242
+static void
b89242
+child_setup_fchdir (gpointer data)
b89242
+{
b89242
+  int fd = (int) (uintptr_t) data;
b89242
+  int rc __attribute__((unused));
b89242
+
b89242
+  rc = fchdir (fd);
b89242
+}
b89242
+
b89242
+/*
b89242
+ * Derived from rpm-ostree's rust/src/bwrap.rs
b89242
+ */
b89242
+static gboolean
b89242
+run_in_deployment (int deployment_dfd,
b89242
+                   const gchar * const *child_argv,
b89242
+                   gsize child_argc,
b89242
+                   gint *exit_status,
b89242
+                   gchar **stdout,
b89242
+                   GError **error)
b89242
+{
b89242
+  static const gchar * const COMMON_ARGV[] = {
b89242
+    "/usr/bin/bwrap",
b89242
+    "--dev", "/dev", "--proc", "/proc", "--dir", "/run", "--dir", "/tmp",
b89242
+    "--chdir", "/",
b89242
+    "--die-with-parent",
b89242
+    "--unshare-pid",
b89242
+    "--unshare-uts",
b89242
+    "--unshare-ipc",
b89242
+    "--unshare-cgroup-try",
b89242
+    "--ro-bind", "/sys/block",    "/sys/block",
b89242
+    "--ro-bind", "/sys/bus",      "/sys/bus",
b89242
+    "--ro-bind", "/sys/class",    "/sys/class",
b89242
+    "--ro-bind", "/sys/dev",      "/sys/dev",
b89242
+    "--ro-bind", "/sys/devices",  "/sys/devices",
b89242
+    "--bind", "usr", "/usr",
b89242
+    "--bind", "etc", "/etc",
b89242
+    "--bind", "var", "/var",
b89242
+    "--symlink", "/usr/lib",      "/lib",
b89242
+    "--symlink", "/usr/lib32",    "/lib32",
b89242
+    "--symlink", "/usr/lib64",    "/lib64",
b89242
+    "--symlink", "/usr/bin",      "/bin",
b89242
+    "--symlink", "/usr/sbin",     "/sbin",
b89242
+  };
b89242
+  static const gsize COMMON_ARGC = sizeof (COMMON_ARGV) / sizeof (*COMMON_ARGV);
b89242
+
b89242
+  gsize i;
b89242
+  GPtrArray *args = g_ptr_array_sized_new (COMMON_ARGC + child_argc + 1);
b89242
+  g_autofree gchar **args_raw = NULL;
b89242
+
b89242
+  for (i = 0; i < COMMON_ARGC; i++)
b89242
+    g_ptr_array_add (args, (gchar *) COMMON_ARGV[i]);
b89242
+
b89242
+  for (i = 0; i < child_argc; i++)
b89242
+    g_ptr_array_add (args, (gchar *) child_argv[i]);
b89242
+
b89242
+  g_ptr_array_add (args, NULL);
b89242
+
b89242
+  args_raw = (gchar **) g_ptr_array_free (args, FALSE);
b89242
+
b89242
+  return g_spawn_sync (NULL, args_raw, NULL, 0, &child_setup_fchdir,
b89242
+                       (gpointer) (uintptr_t) deployment_dfd,
b89242
+                       stdout, NULL, exit_status, error);
b89242
+}
b89242
+
b89242
+/*
b89242
+ * Run semodule to check if the module content changed after merging /etc
b89242
+ * and rebuild the policy if needed.
b89242
+ */
b89242
+static gboolean
b89242
+sysroot_finalize_selinux_policy (int deployment_dfd, GError **error)
b89242
+{
b89242
+  struct stat stbuf;
b89242
+  gint exit_status;
b89242
+  g_autofree gchar *stdout = NULL;
b89242
+
b89242
+  if (!glnx_fstatat_allow_noent (deployment_dfd, "etc/selinux/config", &stbuf,
b89242
+                                 AT_SYMLINK_NOFOLLOW, error))
b89242
+    return FALSE;
b89242
+
b89242
+  /* Skip the SELinux policy refresh if /etc/selinux/config doesn't exist. */
b89242
+  if (errno != 0)
b89242
+    return TRUE;
b89242
+
b89242
+  /*
b89242
+   * Skip the SELinux policy refresh if the --rebuild-if-modules-changed
b89242
+   * flag is not supported by semodule.
b89242
+   */
b89242
+  static const gchar * const SEMODULE_HELP_ARGV[] = {
b89242
+    "semodule", "--help"
b89242
+  };
b89242
+  static const gsize SEMODULE_HELP_ARGC = sizeof (SEMODULE_HELP_ARGV) / sizeof (*SEMODULE_HELP_ARGV);
b89242
+  if (!run_in_deployment (deployment_dfd, SEMODULE_HELP_ARGV,
b89242
+                          SEMODULE_HELP_ARGC, &exit_status, &stdout, error))
b89242
+    return FALSE;
b89242
+  if (!g_spawn_check_exit_status (exit_status, error))
b89242
+    return FALSE;
b89242
+  if (!strstr(stdout, "--rebuild-if-modules-changed"))
b89242
+    return TRUE;
b89242
+
b89242
+  static const gchar * const SEMODULE_REBUILD_ARGV[] = {
b89242
+    "semodule", "-N", "--rebuild-if-modules-changed"
b89242
+  };
b89242
+  static const gsize SEMODULE_REBUILD_ARGC = sizeof (SEMODULE_REBUILD_ARGV) / sizeof (*SEMODULE_REBUILD_ARGV);
b89242
+
b89242
+  if (!run_in_deployment (deployment_dfd, SEMODULE_REBUILD_ARGV,
b89242
+                          SEMODULE_REBUILD_ARGC, &exit_status, NULL, error))
b89242
+    return FALSE;
b89242
+  return g_spawn_check_exit_status (exit_status, error);
b89242
+}
b89242
+#endif /* HAVE_SELINUX */
b89242
+
b89242
 static gboolean
b89242
 sysroot_finalize_deployment (OstreeSysroot     *self,
b89242
                              OstreeDeployment  *deployment,
b89242
@@ -2866,6 +2978,11 @@ sysroot_finalize_deployment (OstreeSysroot     *self,
b89242
         return FALSE;
b89242
     }
b89242
 
b89242
+#ifdef HAVE_SELINUX
b89242
+  if (!sysroot_finalize_selinux_policy(deployment_dfd, error))
b89242
+    return FALSE;
b89242
+#endif /* HAVE_SELINUX */
b89242
+
b89242
   const char *osdeploypath = glnx_strjoina ("ostree/deploy/", ostree_deployment_get_osname (deployment));
b89242
   glnx_autofd int os_deploy_dfd = -1;
b89242
   if (!glnx_opendirat (self->sysroot_fd, osdeploypath, TRUE, &os_deploy_dfd, error))
b89242
-- 
b89242
2.31.1
b89242