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