Blame SOURCES/0004-libsemanage-sync-filesystem-with-sandbox.patch

4f4165
From 11e381e5aa3468aa5c2634f14706336c7824f226 Mon Sep 17 00:00:00 2001
4f4165
From: Petr Lautrbach <plautrba@redhat.com>
4f4165
Date: Wed, 27 Jan 2021 12:00:55 +0100
4f4165
Subject: [PATCH] libsemanage: sync filesystem with sandbox
4f4165
4f4165
Commit 331a109f91ea ("libsemanage: fsync final files before rename")
4f4165
added fsync() for policy files and improved situation when something
4f4165
unexpected happens right after rename(). However the module store could
4f4165
be affected as well. After the following steps module files could be 0
4f4165
size:
4f4165
4f4165
1. Run `semanage fcontext -a -t var_t "/tmp/abc"`
4f4165
2. Force shutdown the server during the command is run, or right after
4f4165
   it's finished
4f4165
3. Boot the system and look for empty files:
4f4165
    # find /var/lib/selinux/targeted/ -type f -size 0 | wc -l
4f4165
    1266
4f4165
4f4165
It looks like this situation can be avoided if the filesystem with the
4f4165
sandbox is sync()ed before we start to rename() directories in the
4f4165
store.
4f4165
4f4165
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
4f4165
Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
4f4165
---
4f4165
 libsemanage/src/semanage_store.c | 13 +++++++++++++
4f4165
 1 file changed, 13 insertions(+)
4f4165
4f4165
diff --git a/libsemanage/src/semanage_store.c b/libsemanage/src/semanage_store.c
4f4165
index 733df8da37c2..ae023582e907 100644
4f4165
--- a/libsemanage/src/semanage_store.c
4f4165
+++ b/libsemanage/src/semanage_store.c
4f4165
@@ -1737,6 +1737,19 @@ static int semanage_commit_sandbox(semanage_handle_t * sh)
4f4165
 	}
4f4165
 	close(fd);
4f4165
 
4f4165
+	/* sync changes in sandbox to filesystem */
4f4165
+	fd = open(sandbox, O_DIRECTORY);
4f4165
+	if (fd == -1) {
4f4165
+		ERR(sh, "Error while opening %s for syncfs(): %d", sandbox, errno);
4f4165
+		return -1;
4f4165
+	}
4f4165
+	if (syncfs(fd) == -1) {
4f4165
+		ERR(sh, "Error while syncing %s to filesystem: %d", sandbox, errno);
4f4165
+		close(fd);
4f4165
+		return -1;
4f4165
+	}
4f4165
+	close(fd);
4f4165
+
4f4165
 	retval = commit_number;
4f4165
 
4f4165
 	if (semanage_get_active_lock(sh) < 0) {
4f4165
-- 
4f4165
2.30.0
4f4165