Blob Blame History Raw
From 128e22d0c638aed81337a6dbbfa664e5bfc9ea06 Mon Sep 17 00:00:00 2001
From: Steve Grubb <sgrubb@redhat.com>
Date: Wed, 3 Mar 2021 13:34:58 -0500
Subject: [PATCH] If db migration fails due to unlinking problem, fail startup

---
 ChangeLog               |  1 +
 src/cli/fapolicyd-cli.c |  5 +++--
 src/library/database.c  | 22 ++++++++++++++++------
 src/library/database.h  |  4 ++--
 4 files changed, 22 insertions(+), 10 deletions(-)

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