Blame SOURCES/0012-libsemanage-optionally-rebuild-policy-when-modules-a.patch

634418
From 090a82e33be97d42eaa75bec85a32ccb6b7a13e8 Mon Sep 17 00:00:00 2001
634418
From: Ondrej Mosnacek <omosnace@redhat.com>
634418
Date: Thu, 3 Feb 2022 17:53:26 +0100
634418
Subject: [PATCH] libsemanage: optionally rebuild policy when modules are
634418
 changed externally
634418
634418
In Fedora/RHEL's selinux-policy package we ship a pre-built SELinux
634418
policy store in the RPMs. When updating the main policy RPM, care must
634418
be taken to rebuild the policy using `semodule -B` if there are any
634418
other SELinux modules installed (whether shipped via another RPM or
634418
manually installed locally).
634418
634418
However, this way of shipping/managing the policy creates complications
634418
on systems, where system files are managed by rpm-ostree (such as Fedora
634418
CoreOS or Red Hat CoreOS), where the "package update" process is more
634418
sophisticated.
634418
634418
(Disclaimer: The following is written according to my current limited
634418
understanding of rpm-ostree and may not be entirely accurate, but the
634418
gist of it should match the reality.)
634418
634418
Basically, one can think of rpm-ostree as a kind of Git for system
634418
files. The package content is provided on a "branch", where each
634418
"commit" represents a set of package updates layered on top of the
634418
previous commit (i.e. it is a rolling release with some defined
634418
package content snapshots). The user can then maintain their own branch
634418
with additional package updates/installations/... and "rebase" it on top
634418
of the main branch as needed. On top of that, the user can also have
634418
additional configuration files (or modifications to existing files) in
634418
/etc, which represent an additional layer on top of the package content.
634418
634418
When updating the system (i.e. rebasing on a new "commit" of the "main
634418
branch"), the files on the running system are not touched and the new
634418
system state is prepared under a new root directory, which is chrooted
634418
into on the next reboot.
634418
634418
When an rpm-ostree system is updated, there are three moments when the
634418
SELinux module store needs to be rebuilt to ensure that all modules are
634418
included in the binary policy:
634418
1. When the local RPM installations are applied on top of the base
634418
   system snapshot.
634418
2. When local user configuartion is applied on top of that.
634418
3. On system shutdown, to ensure that any changes in local configuration
634418
   performed since (2.) are reflected in the final new system image.
634418
634418
Forcing a full rebuild at each step is not optimal and in many cases is
634418
not necessary, as the user may not have any custom modules installed.
634418
634418
Thus, this patch extends libsemanage to compute a checksum of the
634418
content of all enabled modules, which is stored in the store, and adds a
634418
flag to the libsemanage handle that instructs it to check the module
634418
content checksum against the one from the last successful transaction
634418
and force a full policy rebuild if they don't match.
634418
634418
This will allow rpm-ostree systems to potentially reduce delays when
634418
reconciling the module store when applying updates.
634418
634418
I wasn't able to measure any noticeable overhead of the hash
634418
computation, which is now added for every transaction (both before and
634418
after this change a full policy rebuild took about 7 seconds on my test
634418
x86 VM). With the new option check_ext_changes enabled, rebuilding a
634418
policy store with unchanged modules took only about 0.96 seconds.
634418
634418
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
634418
---
634418
 libsemanage/include/semanage/handle.h |   5 +
634418
 libsemanage/src/direct_api.c          | 187 +++++++++++++++++++++-----
634418
 libsemanage/src/handle.c              |  11 +-
634418
 libsemanage/src/handle.h              |   1 +
634418
 libsemanage/src/libsemanage.map       |   1 +
634418
 libsemanage/src/modules.c             |   4 +-
634418
 libsemanage/src/modules.h             |   3 +
634418
 libsemanage/src/semanage_store.c      |   1 +
634418
 libsemanage/src/semanage_store.h      |   1 +
634418
 9 files changed, 175 insertions(+), 39 deletions(-)
634418
634418
diff --git a/libsemanage/include/semanage/handle.h b/libsemanage/include/semanage/handle.h
634418
index c8165900..7f298a49 100644
634418
--- a/libsemanage/include/semanage/handle.h
634418
+++ b/libsemanage/include/semanage/handle.h
634418
@@ -66,6 +66,11 @@ void semanage_set_reload(semanage_handle_t * handle, int do_reload);
634418
  * 1 for yes, 0 for no (default) */
634418
 void semanage_set_rebuild(semanage_handle_t * handle, int do_rebuild);
634418
 
634418
+/* set whether to rebuild the policy on commit when potential changes
634418
+ * to module files since last rebuild are detected,
634418
+ * 1 for yes (default), 0 for no */
634418
+extern void semanage_set_check_ext_changes(semanage_handle_t * handle, int do_check);
634418
+
634418
 /* Fills *compiler_path with the location of the hll compiler sh->conf->compiler_directory_path
634418
  * corresponding to lang_ext.
634418
  * Upon success returns 0, -1 on error. */
634418
diff --git a/libsemanage/src/direct_api.c b/libsemanage/src/direct_api.c
634418
index 7e99a59f..bbdca2b2 100644
634418
--- a/libsemanage/src/direct_api.c
634418
+++ b/libsemanage/src/direct_api.c
634418
@@ -33,6 +33,8 @@
634418
 #include <unistd.h>
634418
 #include <sys/stat.h>
634418
 #include <sys/types.h>
634418
+#include <sys/mman.h>
634418
+#include <sys/wait.h>
634418
 #include <limits.h>
634418
 #include <errno.h>
634418
 #include <dirent.h>
634418
@@ -56,8 +58,7 @@
634418
 #include "semanage_store.h"
634418
 #include "database_policydb.h"
634418
 #include "policy.h"
634418
-#include <sys/mman.h>
634418
-#include <sys/wait.h>
634418
+#include "sha256.h"
634418
 
634418
 #define PIPE_READ 0
634418
 #define PIPE_WRITE 1
634418
@@ -450,7 +451,7 @@ static int parse_module_headers(semanage_handle_t * sh, char *module_data,
634418
 /* Writes a block of data to a file.  Returns 0 on success, -1 on
634418
  * error. */
634418
 static int write_file(semanage_handle_t * sh,
634418
-		      const char *filename, char *data, size_t num_bytes)
634418
+		      const char *filename, const char *data, size_t num_bytes)
634418
 {
634418
 	int out;
634418
 
634418
@@ -850,8 +851,21 @@ cleanup:
634418
 	return ret;
634418
 }
634418
 
634418
+static void update_checksum_with_len(Sha256Context *context, size_t s)
634418
+{
634418
+	int i;
634418
+	uint8_t buffer[8];
634418
+
634418
+	for (i = 0; i < 8; i++) {
634418
+		buffer[i] = s & 0xff;
634418
+		s >>= 8;
634418
+	}
634418
+	Sha256Update(context, buffer, 8);
634418
+}
634418
+
634418
 static int semanage_compile_module(semanage_handle_t *sh,
634418
-				semanage_module_info_t *modinfo)
634418
+				   semanage_module_info_t *modinfo,
634418
+				   Sha256Context *context)
634418
 {
634418
 	char cil_path[PATH_MAX];
634418
 	char hll_path[PATH_MAX];
634418
@@ -922,6 +936,11 @@ static int semanage_compile_module(semanage_handle_t *sh,
634418
 		goto cleanup;
634418
 	}
634418
 
634418
+	if (context) {
634418
+		update_checksum_with_len(context, cil_data_len);
634418
+		Sha256Update(context, cil_data, cil_data_len);
634418
+	}
634418
+
634418
 	status = write_compressed_file(sh, cil_path, cil_data, cil_data_len);
634418
 	if (status == -1) {
634418
 		ERR(sh, "Failed to write %s\n", cil_path);
634418
@@ -950,18 +969,40 @@ cleanup:
634418
 	return status;
634418
 }
634418
 
634418
+static int modinfo_cmp(const void *a, const void *b)
634418
+{
634418
+	const semanage_module_info_t *ma = a;
634418
+	const semanage_module_info_t *mb = b;
634418
+
634418
+	return strcmp(ma->name, mb->name);
634418
+}
634418
+
634418
 static int semanage_compile_hll_modules(semanage_handle_t *sh,
634418
-				semanage_module_info_t *modinfos,
634418
-				int num_modinfos)
634418
+					semanage_module_info_t *modinfos,
634418
+					int num_modinfos,
634418
+					char *cil_checksum)
634418
 {
634418
-	int status = 0;
634418
-	int i;
634418
+	/* to be incremented when checksum input data format changes */
634418
+	static const size_t CHECKSUM_EPOCH = 1;
634418
+
634418
+	int i, status = 0;
634418
 	char cil_path[PATH_MAX];
634418
 	struct stat sb;
634418
+	Sha256Context context;
634418
+	SHA256_HASH hash;
634418
+	struct file_contents contents = {};
634418
 
634418
 	assert(sh);
634418
 	assert(modinfos);
634418
 
634418
+	/* Sort modules by name to get consistent ordering. */
634418
+	qsort(modinfos, num_modinfos, sizeof(*modinfos), &modinfo_cmp);
634418
+
634418
+	Sha256Initialise(&context);
634418
+	update_checksum_with_len(&context, CHECKSUM_EPOCH);
634418
+
634418
+	/* prefix with module count to avoid collisions */
634418
+	update_checksum_with_len(&context, num_modinfos);
634418
 	for (i = 0; i < num_modinfos; i++) {
634418
 		status = semanage_module_get_path(
634418
 				sh,
634418
@@ -969,29 +1010,91 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
634418
 				SEMANAGE_MODULE_PATH_CIL,
634418
 				cil_path,
634418
 				sizeof(cil_path));
634418
-		if (status != 0) {
634418
-			goto cleanup;
634418
-		}
634418
+		if (status != 0)
634418
+			return -1;
634418
 
634418
-		if (semanage_get_ignore_module_cache(sh) == 0 &&
634418
-				(status = stat(cil_path, &sb)) == 0) {
634418
-			continue;
634418
-		}
634418
-		if (status != 0 && errno != ENOENT) {
634418
-			ERR(sh, "Unable to access %s: %s\n", cil_path, strerror(errno));
634418
-			goto cleanup; //an error in the "stat" call
634418
+		if (!semanage_get_ignore_module_cache(sh)) {
634418
+			status = stat(cil_path, &sb);
634418
+			if (status == 0) {
634418
+				status = map_compressed_file(sh, cil_path, &contents);
634418
+				if (status < 0) {
634418
+					ERR(sh, "Error mapping file: %s", cil_path);
634418
+					return -1;
634418
+				}
634418
+
634418
+				/* prefix with length to avoid collisions */
634418
+				update_checksum_with_len(&context, contents.len);
634418
+				Sha256Update(&context, contents.data, contents.len);
634418
+
634418
+				unmap_compressed_file(&contents);
634418
+				continue;
634418
+			} else if (errno != ENOENT) {
634418
+				ERR(sh, "Unable to access %s: %s\n", cil_path,
634418
+				    strerror(errno));
634418
+				return -1; //an error in the "stat" call
634418
+			}
634418
 		}
634418
 
634418
-		status = semanage_compile_module(sh, &modinfos[i]);
634418
-		if (status < 0) {
634418
-			goto cleanup;
634418
+		status = semanage_compile_module(sh, &modinfos[i], &context);
634418
+		if (status < 0)
634418
+			return -1;
634418
+	}
634418
+	Sha256Finalise(&context, &hash);
634418
+
634418
+	semanage_hash_to_checksum_string(hash.bytes, cil_checksum);
634418
+	return 0;
634418
+}
634418
+
634418
+static int semanage_compare_checksum(semanage_handle_t *sh, const char *reference)
634418
+{
634418
+	const char *path = semanage_path(SEMANAGE_TMP, SEMANAGE_MODULES_CHECKSUM);
634418
+	struct stat sb;
634418
+	int fd, retval;
634418
+	char *data;
634418
+
634418
+	fd = open(path, O_RDONLY);
634418
+	if (fd == -1) {
634418
+		if (errno != ENOENT) {
634418
+			ERR(sh, "Unable to open %s: %s\n", path, strerror(errno));
634418
+			return -1;
634418
 		}
634418
+		/* Checksum file not present - force a rebuild. */
634418
+		return 1;
634418
+	}
634418
+
634418
+	if (fstat(fd, &sb) == -1) {
634418
+		ERR(sh, "Unable to stat %s\n", path);
634418
+		retval = -1;
634418
+		goto out_close;
634418
 	}
634418
 
634418
-	status = 0;
634418
+	if (sb.st_size != (off_t)CHECKSUM_CONTENT_SIZE) {
634418
+		/* Incompatible/invalid hash type - just force a rebuild. */
634418
+		WARN(sh, "Module checksum invalid - forcing a rebuild\n");
634418
+		retval = 1;
634418
+		goto out_close;
634418
+	}
634418
 
634418
-cleanup:
634418
-	return status;
634418
+	data = mmap(NULL, CHECKSUM_CONTENT_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
634418
+	if (data == MAP_FAILED) {
634418
+		ERR(sh, "Unable to mmap %s\n", path);
634418
+		retval = -1;
634418
+		goto out_close;
634418
+	}
634418
+
634418
+	retval = memcmp(data, reference, CHECKSUM_CONTENT_SIZE) != 0;
634418
+	munmap(data, sb.st_size);
634418
+out_close:
634418
+	close(fd);
634418
+	return retval;
634418
+}
634418
+
634418
+static int semanage_write_modules_checksum(semanage_handle_t *sh,
634418
+					   const char *checksum)
634418
+{
634418
+	const char *path = semanage_path(SEMANAGE_TMP, SEMANAGE_MODULES_CHECKSUM);
634418
+
634418
+	return write_file(sh, path, checksum, CHECKSUM_CONTENT_SIZE);
634418
 }
634418
 
634418
 /* Files that must exist in order to skip policy rebuild. */
634418
@@ -1030,6 +1133,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
634418
 	semanage_module_info_t *modinfos = NULL;
634418
 	mode_t mask = umask(0077);
634418
 	struct stat sb;
634418
+	char modules_checksum[CHECKSUM_CONTENT_SIZE + 1 /* '\0' */];
634418
 
634418
 	int do_rebuild, do_write_kernel, do_install;
634418
 	int fcontexts_modified, ports_modified, seusers_modified,
634418
@@ -1159,28 +1263,45 @@ static int semanage_direct_commit(semanage_handle_t * sh)
634418
 		}
634418
 	}
634418
 
634418
-	/*
634418
-	 * If there were policy changes, or explicitly requested, or
634418
-	 * any required files are missing, rebuild the policy.
634418
-	 */
634418
-	if (do_rebuild) {
634418
-		/* =================== Module expansion =============== */
634418
-
634418
+	if (do_rebuild || sh->check_ext_changes) {
634418
 		retval = semanage_get_active_modules(sh, &modinfos, &num_modinfos);
634418
 		if (retval < 0) {
634418
 			goto cleanup;
634418
 		}
634418
 
634418
+		/* No modules - nothing to rebuild. */
634418
 		if (num_modinfos == 0) {
634418
 			goto cleanup;
634418
 		}
634418
 
634418
-		retval = semanage_compile_hll_modules(sh, modinfos, num_modinfos);
634418
+		retval = semanage_compile_hll_modules(sh, modinfos, num_modinfos,
634418
+						      modules_checksum);
634418
 		if (retval < 0) {
634418
 			ERR(sh, "Failed to compile hll files into cil files.\n");
634418
 			goto cleanup;
634418
 		}
634418
 
634418
+		if (!do_rebuild && sh->check_ext_changes) {
634418
+			retval = semanage_compare_checksum(sh, modules_checksum);
634418
+			if (retval < 0)
634418
+				goto cleanup;
634418
+			do_rebuild = retval;
634418
+		}
634418
+
634418
+		retval = semanage_write_modules_checksum(sh, modules_checksum);
634418
+		if (retval < 0) {
634418
+			ERR(sh, "Failed to write module checksum file.\n");
634418
+			goto cleanup;
634418
+		}
634418
+	}
634418
+
634418
+	/*
634418
+	 * If there were policy changes, or explicitly requested, or
634418
+	 * any required files are missing, rebuild the policy.
634418
+	 */
634418
+	if (do_rebuild) {
634418
+		/* =================== Module expansion =============== */
634418
+
634418
 		retval = semanage_get_cil_paths(sh, modinfos, num_modinfos, &mod_filenames);
634418
 		if (retval < 0)
634418
 			goto cleanup;
634418
@@ -1696,7 +1817,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
634418
 			goto cleanup;
634418
 		}
634418
 
634418
-		rc = semanage_compile_module(sh, _modinfo);
634418
+		rc = semanage_compile_module(sh, _modinfo, NULL);
634418
 		if (rc < 0) {
634418
 			goto cleanup;
634418
 		}
634418
diff --git a/libsemanage/src/handle.c b/libsemanage/src/handle.c
634418
index e5109aef..8a01c53a 100644
634418
--- a/libsemanage/src/handle.c
634418
+++ b/libsemanage/src/handle.c
634418
@@ -118,20 +118,23 @@ semanage_handle_t *semanage_handle_create(void)
634418
 
634418
 void semanage_set_rebuild(semanage_handle_t * sh, int do_rebuild)
634418
 {
634418
-
634418
 	assert(sh != NULL);
634418
 
634418
 	sh->do_rebuild = do_rebuild;
634418
-	return;
634418
 }
634418
 
634418
 void semanage_set_reload(semanage_handle_t * sh, int do_reload)
634418
 {
634418
-
634418
 	assert(sh != NULL);
634418
 
634418
 	sh->do_reload = do_reload;
634418
-	return;
634418
+}
634418
+
634418
+void semanage_set_check_ext_changes(semanage_handle_t * sh, int do_check)
634418
+{
634418
+	assert(sh != NULL);
634418
+
634418
+	sh->check_ext_changes = do_check;
634418
 }
634418
 
634418
 int semanage_get_hll_compiler_path(semanage_handle_t *sh,
634418
diff --git a/libsemanage/src/handle.h b/libsemanage/src/handle.h
634418
index a91907b0..c4a6e7ea 100644
634418
--- a/libsemanage/src/handle.h
634418
+++ b/libsemanage/src/handle.h
634418
@@ -62,6 +62,7 @@ struct semanage_handle {
634418
 	int is_in_transaction;
634418
 	int do_reload;		/* whether to reload policy after commit */
634418
 	int do_rebuild;		/* whether to rebuild policy if there were no changes */
634418
+	int check_ext_changes;	/* whether to rebuild if external changes are detected via checksum */
634418
 	int commit_err;		/* set by semanage_direct_commit() if there are
634418
 				 * any errors when building or committing the
634418
 				 * sandbox to kernel policy at /etc/selinux
634418
diff --git a/libsemanage/src/libsemanage.map b/libsemanage/src/libsemanage.map
634418
index a986b2d2..1ef664be 100644
634418
--- a/libsemanage/src/libsemanage.map
634418
+++ b/libsemanage/src/libsemanage.map
634418
@@ -66,4 +66,5 @@ LIBSEMANAGE_1.1 {
634418
 
634418
 LIBSEMANAGE_3.4 {
634418
     semanage_module_compute_checksum;
634418
+    semanage_set_check_ext_changes;
634418
 } LIBSEMANAGE_1.1;
634418
diff --git a/libsemanage/src/modules.c b/libsemanage/src/modules.c
634418
index 3a82d275..f1fe160d 100644
634418
--- a/libsemanage/src/modules.c
634418
+++ b/libsemanage/src/modules.c
634418
@@ -1149,9 +1149,9 @@ int semanage_module_remove_key(semanage_handle_t *sh,
634418
 }
634418
 
634418
 static const char CHECKSUM_TYPE[] = "sha256";
634418
-static const size_t CHECKSUM_CONTENT_SIZE = sizeof(CHECKSUM_TYPE) + 1 + 2 * SHA256_HASH_SIZE;
634418
+const size_t CHECKSUM_CONTENT_SIZE = sizeof(CHECKSUM_TYPE) + 1 + 2 * SHA256_HASH_SIZE;
634418
 
634418
-static void semanage_hash_to_checksum_string(const uint8_t *hash, char *checksum)
634418
+void semanage_hash_to_checksum_string(const uint8_t *hash, char *checksum)
634418
 {
634418
 	size_t i;
634418
 
634418
diff --git a/libsemanage/src/modules.h b/libsemanage/src/modules.h
634418
index 8a5c01f4..b828a534 100644
634418
--- a/libsemanage/src/modules.h
634418
+++ b/libsemanage/src/modules.h
634418
@@ -109,4 +109,7 @@ int semanage_module_get_path(semanage_handle_t *sh,
634418
 			     char *path,
634418
 			     size_t len);
634418
 
634418
+extern const size_t CHECKSUM_CONTENT_SIZE;
634418
+void semanage_hash_to_checksum_string(const uint8_t *hash, char *checksum);
634418
+
634418
 #endif
634418
diff --git a/libsemanage/src/semanage_store.c b/libsemanage/src/semanage_store.c
634418
index c5ce071c..1fff667d 100644
634418
--- a/libsemanage/src/semanage_store.c
634418
+++ b/libsemanage/src/semanage_store.c
634418
@@ -115,6 +115,7 @@ static const char *semanage_sandbox_paths[SEMANAGE_STORE_NUM_PATHS] = {
634418
 	"/disable_dontaudit",
634418
 	"/preserve_tunables",
634418
 	"/modules/disabled",
634418
+	"/modules_checksum",
634418
 	"/policy.kern",
634418
 	"/file_contexts.local",
634418
 	"/file_contexts.homedirs",
634418
diff --git a/libsemanage/src/semanage_store.h b/libsemanage/src/semanage_store.h
634418
index b9ec5664..1fc77da8 100644
634418
--- a/libsemanage/src/semanage_store.h
634418
+++ b/libsemanage/src/semanage_store.h
634418
@@ -60,6 +60,7 @@ enum semanage_sandbox_defs {
634418
 	SEMANAGE_DISABLE_DONTAUDIT,
634418
 	SEMANAGE_PRESERVE_TUNABLES,
634418
 	SEMANAGE_MODULES_DISABLED,
634418
+	SEMANAGE_MODULES_CHECKSUM,
634418
 	SEMANAGE_STORE_KERNEL,
634418
 	SEMANAGE_STORE_FC_LOCAL,
634418
 	SEMANAGE_STORE_FC_HOMEDIRS,
634418
-- 
634418
2.30.2
634418