Blame SOURCES/fapolicyd-unlink3.patch

25361f
From 128e22d0c638aed81337a6dbbfa664e5bfc9ea06 Mon Sep 17 00:00:00 2001
25361f
From: Steve Grubb <sgrubb@redhat.com>
25361f
Date: Wed, 3 Mar 2021 13:34:58 -0500
25361f
Subject: [PATCH] If db migration fails due to unlinking problem, fail startup
25361f
25361f
---
25361f
 ChangeLog               |  1 +
25361f
 src/cli/fapolicyd-cli.c |  5 +++--
25361f
 src/library/database.c  | 22 ++++++++++++++++------
25361f
 src/library/database.h  |  4 ++--
25361f
 4 files changed, 22 insertions(+), 10 deletions(-)
25361f
25361f
diff --git a/src/cli/fapolicyd-cli.c b/src/cli/fapolicyd-cli.c
25361f
index 994c9a6..fb9081b 100644
25361f
--- a/src/cli/fapolicyd-cli.c
25361f
+++ b/src/cli/fapolicyd-cli.c
25361f
@@ -1,6 +1,6 @@
25361f
 /*
25361f
  * fapolicy-cli.c - CLI tool for fapolicyd
25361f
- * Copyright (c) 2019,2020 Red Hat Inc.
25361f
+ * Copyright (c) 2019-2021 Red Hat Inc.
25361f
  * All Rights Reserved.
25361f
  *
25361f
  * This software may be freely redistributed and/or modified under the
25361f
@@ -89,7 +89,8 @@ static char *get_line(FILE *f, unsigned *lineno)
25361f
 
25361f
 static int do_delete_db(void)
25361f
 {
25361f
-	unlink_db();
25361f
+	if (unlink_db())
25361f
+		return 1;
25361f
 	return 0;
25361f
 }
25361f
 
25361f
diff --git a/src/library/database.c b/src/library/database.c
25361f
index 831ec74..a010923 100644
25361f
--- a/src/library/database.c
25361f
+++ b/src/library/database.c
25361f
@@ -1,6 +1,6 @@
25361f
 /*
25361f
  * database.c - Trust database
25361f
- * Copyright (c) 2016,2018-20 Red Hat Inc.
25361f
+ * Copyright (c) 2016,2018-21 Red Hat Inc.
25361f
  * All Rights Reserved.
25361f
  *
25361f
  * This software may be freely redistributed and/or modified under the
25361f
@@ -711,23 +711,32 @@ static int check_database_copy(void)
25361f
 /*
25361f
  * This function removes the trust database files.
25361f
  */
25361f
-void unlink_db(void)
25361f
+int unlink_db(void)
25361f
 {
25361f
-	int rc;
25361f
+	int rc, ret_val = 0;
25361f
 	char path[64];
25361f
 
25361f
 	snprintf(path, sizeof(path), "%s/data.mdb", data_dir);
25361f
 	rc = unlink(path);
25361f
-	if (rc)
25361f
+	if (rc) {
25361f
 		msg(LOG_ERR, "Could not unlink %s (%s)", path, strerror(errno));
25361f
+		ret_val = 1;
25361f
+	}
25361f
 	snprintf(path, sizeof(path), "%s/lock.mdb", data_dir);
25361f
 	rc = unlink(path);
25361f
-	if (rc)
25361f
+	if (rc) {
25361f
 		msg(LOG_ERR, "Could not unlink %s (%s)", path, strerror(errno));
25361f
+		ret_val = 1;
25361f
+	}
25361f
+
25361f
+	return ret_val;
25361f
 }
25361f
 
25361f
 
25361f
 /*
25361f
+ * DB version 1 = unique keys (0.8 - 0.9.2)
25361f
+ * DB version 2 = allow duplicate keys (0.9.3 - )
25361f
+ *
25361f
  * This function is used to detect if we are using version1 of the database.
25361f
  * If so, we have to delete the database and rebuild it. We cannot mix
25361f
  * database versions because lmdb doesn't do that.
25361f
@@ -744,7 +753,8 @@ static int migrate_database(void)
25361f
 		msg(LOG_INFO, "Database migration will be performed.");
25361f
 
25361f
 		// Then we have a version1 db since it does not track versions
25361f
-		unlink_db();
25361f
+		if (unlink_db())
25361f
+			return 1;
25361f
 
25361f
 		// Create the new, db version tracker and write current version
25361f
 		fd = open(vpath, O_CREAT|O_EXCL|O_WRONLY, 0640);
25361f
diff --git a/src/library/database.h b/src/library/database.h
25361f
index e828503..f4516b2 100644
25361f
--- a/src/library/database.h
25361f
+++ b/src/library/database.h
25361f
@@ -1,6 +1,6 @@
25361f
 /*
25361f
  * database.h - Header file for trust database
25361f
- * Copyright (c) 2018-20 Red Hat Inc.
25361f
+ * Copyright (c) 2018-21 Red Hat Inc.
25361f
  * All Rights Reserved.
25361f
  *
25361f
  * This software may be freely redistributed and/or modified under the
25361f
@@ -41,7 +41,7 @@ int init_database(conf_t *config);
25361f
 int check_trust_database(const char *path, struct file_info *info, int fd);
25361f
 void close_database(void);
25361f
 void database_report(FILE *f);
25361f
-void unlink_db(void);
25361f
+int unlink_db(void);
25361f
 void unlink_fifo(void);
25361f
 
25361f
 #endif