Blame SOURCES/fapolicyd-fix-escaping.patch

b63e47
diff --color -ru a/init/fapolicyd.trust b/init/fapolicyd.trust
b63e47
--- a/init/fapolicyd.trust	2021-11-12 20:21:54.000000000 +0100
b63e47
+++ b/init/fapolicyd.trust	2021-12-08 13:25:43.441187113 +0100
b63e47
@@ -1,3 +1,4 @@
b63e47
+# AUTOGENERATED FILE VERSION 2
b63e47
 # This file contains a list of trusted files
b63e47
 #
b63e47
 #  FULL PATH        SIZE                             SHA256
b63e47
diff --color -ru a/src/cli/file-cli.c b/src/cli/file-cli.c
b63e47
--- a/src/cli/file-cli.c	2021-11-12 20:21:54.000000000 +0100
b63e47
+++ b/src/cli/file-cli.c	2021-12-08 13:25:43.441187113 +0100
b63e47
@@ -89,9 +89,6 @@
b63e47
 	return 0;
b63e47
 }
b63e47
 
b63e47
-
b63e47
-
b63e47
-
b63e47
 int file_append(const char *path, const char *fname)
b63e47
 {
b63e47
 	set_message_mode(MSG_STDERR, DBG_NO);
b63e47
@@ -110,11 +107,14 @@
b63e47
 
b63e47
 	char *dest = fname ? fapolicyd_strcat(TRUST_DIR_PATH, fname) :
b63e47
 							TRUST_FILE_PATH;
b63e47
+
b63e47
 	int rc = trust_file_append(dest, &add_list);
b63e47
 
b63e47
+	list_empty(&add_list);
b63e47
+
b63e47
 	if (fname)
b63e47
 		free(dest);
b63e47
-	list_empty(&add_list);
b63e47
+
b63e47
 	return rc ? -1 : 0;
b63e47
 }
b63e47
 
b63e47
diff --color -ru a/src/library/trust-file.c b/src/library/trust-file.c
b63e47
--- a/src/library/trust-file.c	2021-11-12 20:21:54.000000000 +0100
b63e47
+++ b/src/library/trust-file.c	2021-12-08 15:42:15.787206923 +0100
b63e47
@@ -51,6 +51,7 @@
b63e47
 #define FTW_NOPENFD 1024
b63e47
 #define FTW_FLAGS (FTW_ACTIONRETVAL | FTW_PHYS)
b63e47
 
b63e47
+#define HEADER0 "# AUTOGENERATED FILE VERSION 2\n"
b63e47
 #define HEADER1 "# This file contains a list of trusted files\n"
b63e47
 #define HEADER2 "#\n"
b63e47
 #define HEADER3 "#  FULL PATH        SIZE                             SHA256\n"
b63e47
@@ -137,12 +138,19 @@
b63e47
 		return 1;
b63e47
 	}
b63e47
 
b63e47
-	size_t hlen = strlen(HEADER1);
b63e47
+	size_t hlen;
b63e47
+	hlen = strlen(HEADER0);
b63e47
+	fwrite(HEADER0, hlen, 1, f);
b63e47
+
b63e47
+	hlen = strlen(HEADER1);
b63e47
 	fwrite(HEADER1, hlen, 1, f);
b63e47
+
b63e47
 	hlen = strlen(HEADER2);
b63e47
 	fwrite(HEADER2, hlen, 1, f);
b63e47
+
b63e47
 	hlen = strlen(HEADER3);
b63e47
 	fwrite(HEADER3, hlen, 1, f);
b63e47
+
b63e47
 	hlen = strlen(HEADER4);
b63e47
 	fwrite(HEADER4, hlen, 1, f);
b63e47
 
b63e47
@@ -163,50 +171,49 @@
b63e47
 	return 0;
b63e47
 }
b63e47
 
b63e47
-
b63e47
-
b63e47
-int trust_file_append(const char *fpath, const list_t *list) {
b63e47
-	int fd = open(fpath, O_CREAT | O_WRONLY | O_APPEND, 0600);
b63e47
-	if (fd == -1) {
b63e47
-		msg(LOG_ERR, "Cannot open %s", fpath);
b63e47
+int trust_file_append(const char *fpath, list_t *list)
b63e47
+{
b63e47
+	list_t content;
b63e47
+	list_init(&content);
b63e47
+	int rc = trust_file_load(fpath, &content);
b63e47
+	if (rc)
b63e47
 		return 1;
b63e47
-	}
b63e47
 
b63e47
 	for (list_item_t *lptr = list->first; lptr; lptr = lptr->next) {
b63e47
-		int count = 1;
b63e47
-		char *line = make_path_string(lptr->index, &count);
b63e47
-		if (!line)
b63e47
-			continue;
b63e47
-
b63e47
-		if (write(fd, line, count) == -1) {
b63e47
-			msg(LOG_ERR, "failed writing to %s\n", fpath);
b63e47
-			free(line);
b63e47
-			close(fd);
b63e47
-			return 2;
b63e47
-		}
b63e47
-		free(line);
b63e47
+		int i = 0;
b63e47
+		lptr->data = make_path_string(lptr->index, &i);
b63e47
 	}
b63e47
 
b63e47
-	close(fd);
b63e47
-	return 0;
b63e47
+	list_merge(&content, list);
b63e47
+	write_out_list(&content, fpath);
b63e47
+	list_empty(&content);
b63e47
+	return rc ? 1 : 0;
b63e47
 }
b63e47
 
b63e47
 int trust_file_load(const char *fpath, list_t *list)
b63e47
 {
b63e47
+	char buffer[BUFFER_SIZE];
b63e47
+	int escaped = 0;
b63e47
+	long line = 0;
b63e47
+
b63e47
 	FILE *file = fopen(fpath, "r");
b63e47
 	if (!file) {
b63e47
 		msg(LOG_ERR, "Cannot open %s", fpath);
b63e47
 		return 1;
b63e47
 	}
b63e47
 
b63e47
-	char buffer[BUFFER_SIZE];
b63e47
 	while (fgets(buffer, BUFFER_SIZE, file)) {
b63e47
-		char name[4097], sha[65], *index, *data;
b63e47
+		char name[4097], sha[65], *index = NULL, *data = NULL;
b63e47
 		unsigned long sz;
b63e47
 		unsigned int tsource = SRC_FILE_DB;
b63e47
 
b63e47
-		if (iscntrl(buffer[0]) || buffer[0] == '#')
b63e47
+		line++;
b63e47
+
b63e47
+		if (iscntrl(buffer[0]) || buffer[0] == '#') {
b63e47
+			if (line == 1 && strncmp(buffer, HEADER0, strlen(HEADER0)) == 0)
b63e47
+				escaped = 1;
b63e47
 			continue;
b63e47
+		}
b63e47
 
b63e47
 		if (sscanf(buffer, FILE_READ_FORMAT, name, &sz, sha) != 3) {
b63e47
 			msg(LOG_WARNING, "Can't parse %s", buffer);
b63e47
@@ -217,7 +224,7 @@
b63e47
 		if (asprintf(&data, DATA_FORMAT, tsource, sz, sha) == -1)
b63e47
 			data = NULL;
b63e47
 
b63e47
-		index = unescape(name);
b63e47
+		index = escaped ? unescape(name) : strdup(name);
b63e47
 		if (index == NULL) {
b63e47
 			msg(LOG_ERR, "Could not unescape %s from %s", name, fpath);
b63e47
 			free(data);
b63e47
@@ -311,33 +318,22 @@
b63e47
 
b63e47
 int trust_file_rm_duplicates(const char *fpath, list_t *list)
b63e47
 {
b63e47
-	FILE *file = fopen(fpath, "r");
b63e47
-	if (!file) {
b63e47
-		msg(LOG_ERR, "Cannot open %s", fpath);
b63e47
-		return 1;
b63e47
-	}
b63e47
-
b63e47
-	char buffer[BUFFER_SIZE];
b63e47
-	while (fgets(buffer, BUFFER_SIZE, file)) {
b63e47
-		char thash[65], tpath[4097];
b63e47
-		long unsigned size;
b63e47
+	list_t trust_file;
b63e47
+	list_init(&trust_file);
b63e47
 
b63e47
-		if (iscntrl(buffer[0]) || buffer[0] == '#')
b63e47
-			continue;
b63e47
+	int rc = trust_file_load(fpath, &trust_file);
b63e47
+	if (rc)
b63e47
+		goto cleanup;
b63e47
 
b63e47
-		if (sscanf(buffer, FILE_READ_FORMAT, tpath, &size, thash) != 3) {
b63e47
-			msg(LOG_WARNING, "Can't parse %s", buffer);
b63e47
-			fclose(file);
b63e47
-			return 2;
b63e47
-		}
b63e47
-
b63e47
-		list_remove(list, tpath);
b63e47
+	for (list_item_t *lptr = trust_file.first; lptr; lptr = lptr->next) {
b63e47
+		list_remove(list, lptr->index);
b63e47
 		if (list->count == 0)
b63e47
 			break;
b63e47
 	}
b63e47
 
b63e47
-	fclose(file);
b63e47
-	return 0;
b63e47
+cleanup:
b63e47
+	list_empty(&trust_file);
b63e47
+	return rc;
b63e47
 }
b63e47
 
b63e47
 
b63e47
diff --color -ru a/src/library/trust-file.h b/src/library/trust-file.h
b63e47
--- a/src/library/trust-file.h	2021-11-12 20:21:54.000000000 +0100
b63e47
+++ b/src/library/trust-file.h	2021-12-08 13:25:43.441187113 +0100
b63e47
@@ -30,8 +30,7 @@
b63e47
 #define TRUST_FILE_PATH "/etc/fapolicyd/fapolicyd.trust"
b63e47
 #define TRUST_DIR_PATH "/etc/fapolicyd/trust.d/"
b63e47
 
b63e47
-int trust_file_append(const char *fpath, const list_t *list);
b63e47
-
b63e47
+int trust_file_append(const char *fpath, list_t *list);
b63e47
 int trust_file_load(const char *fpath, list_t *list);
b63e47
 int trust_file_update_path(const char *fpath, const char *path);
b63e47
 int trust_file_delete_path(const char *fpath, const char *path);