Blame SOURCES/0002-depmod-prevent-module-dependency-files-corruption-du.patch

5fb698
From a06bacf500d56b72b5f9b121ebf7f6af9e3df185 Mon Sep 17 00:00:00 2001
5fb698
From: Michal Suchanek <msuchanek@suse.de>
5fb698
Date: Mon, 17 Dec 2018 23:46:28 +0100
5fb698
Subject: [PATCH 2/2] depmod: prevent module dependency files corruption due to
5fb698
 parallel invocation.
5fb698
5fb698
Depmod does not use unique filename for temporary files. There is no
5fb698
guarantee the user does not attempt to run mutiple depmod processes in
5fb698
parallel. If that happens a temporary file might be created by
5fb698
depmod(1st), truncated by depmod(2nd), and renamed to final name by
5fb698
depmod(1st) resulting in corrupted file seen by user.
5fb698
5fb698
Due to missing mkstempat() this is more complex than it should be.
5fb698
Adding PID and timestamp to the filename should be reasonably reliable.
5fb698
Adding O_EXCL as mkstemp does fails creating the file rather than
5fb698
corrupting existing file.
5fb698
5fb698
Signed-off-by: Michal Suchanek <msuchanek@suse.de>
5fb698
---
5fb698
 tools/depmod.c | 9 +++++++--
5fb698
 1 file changed, 7 insertions(+), 2 deletions(-)
5fb698
5fb698
diff --git a/tools/depmod.c b/tools/depmod.c
5fb698
index 18c0d61b2db3..0f7e33ccfd59 100644
5fb698
--- a/tools/depmod.c
5fb698
+++ b/tools/depmod.c
5fb698
@@ -29,6 +29,7 @@
5fb698
 #include <string.h>
5fb698
 #include <unistd.h>
5fb698
 #include <sys/stat.h>
5fb698
+#include <sys/time.h>
5fb698
 #include <sys/utsname.h>
5fb698
 
5fb698
 #include <shared/array.h>
5fb698
@@ -2398,6 +2399,9 @@ static int depmod_output(struct depmod *depmod, FILE *out)
5fb698
 	};
5fb698
 	const char *dname = depmod->cfg->dirname;
5fb698
 	int dfd, err = 0;
5fb698
+	struct timeval tv;
5fb698
+
5fb698
+	gettimeofday(&tv, NULL);
5fb698
 
5fb698
 	if (out != NULL)
5fb698
 		dfd = -1;
5fb698
@@ -2416,11 +2420,12 @@ static int depmod_output(struct depmod *depmod, FILE *out)
5fb698
 		int r, ferr;
5fb698
 
5fb698
 		if (fp == NULL) {
5fb698
-			int flags = O_CREAT | O_TRUNC | O_WRONLY;
5fb698
+			int flags = O_CREAT | O_EXCL | O_WRONLY;
5fb698
 			int mode = 0644;
5fb698
 			int fd;
5fb698
 
5fb698
-			snprintf(tmp, sizeof(tmp), "%s.tmp", itr->name);
5fb698
+			snprintf(tmp, sizeof(tmp), "%s.%i.%li.%li", itr->name, getpid(),
5fb698
+					tv.tv_usec, tv.tv_sec);
5fb698
 			fd = openat(dfd, tmp, flags, mode);
5fb698
 			if (fd < 0) {
5fb698
 				ERR("openat(%s, %s, %o, %o): %m\n",
5fb698
-- 
5fb698
2.33.0
5fb698