3cd12006bb
Signed-off-by: Dean Troyer <dtroyer@gmail.com>
51 lines
1.5 KiB
Diff
51 lines
1.5 KiB
Diff
From aff4a30807218a52b6b5f200c5aa0eea335547ba Mon Sep 17 00:00:00 2001
|
|
From: Kamil Dudka <kdudka@redhat.com>
|
|
Date: Mon, 17 Oct 2016 17:59:31 +0200
|
|
Subject: [PATCH] createOutputFile: eliminate stat/open TOCTOU race
|
|
|
|
---
|
|
logrotate.c | 15 ++++++++++-----
|
|
1 file changed, 10 insertions(+), 5 deletions(-)
|
|
|
|
diff --git a/logrotate.c b/logrotate.c
|
|
index 10f4b52..79f4755 100644
|
|
--- a/logrotate.c
|
|
+++ b/logrotate.c
|
|
@@ -366,11 +366,18 @@ static int runScript(struct logInfo *log, char *logfn, char *script)
|
|
|
|
int createOutputFile(char *fileName, int flags, struct stat *sb, acl_type acl, int force_mode)
|
|
{
|
|
- int fd;
|
|
+ int fd = -1;
|
|
struct stat sb_create;
|
|
int acl_set = 0;
|
|
+ int i;
|
|
+
|
|
+ for (i = 0; i < 2; ++i) {
|
|
+ fd = open(fileName, (flags | O_EXCL | O_NOFOLLOW),
|
|
+ (S_IRUSR | S_IWUSR) & sb->st_mode);
|
|
+
|
|
+ if ((fd >= 0) || (errno != EEXIST))
|
|
+ break;
|
|
|
|
- if (stat(fileName, &sb_create) == 0) {
|
|
/* the destination file already exists, while it should not */
|
|
struct tm now = *localtime(&nowSecs);
|
|
size_t fileName_size = strlen(fileName);
|
|
@@ -384,11 +391,9 @@ int createOutputFile(char *fileName, int flags, struct stat *sb, acl_type acl, i
|
|
fileName, backupName, strerror(errno));
|
|
return -1;
|
|
}
|
|
+ /* existing file renamed, try it once again */
|
|
}
|
|
|
|
- fd = open(fileName, (flags | O_EXCL | O_NOFOLLOW),
|
|
- (S_IRUSR | S_IWUSR) & sb->st_mode);
|
|
-
|
|
if (fd < 0) {
|
|
message(MESS_ERROR, "error creating output file %s: %s\n",
|
|
fileName, strerror(errno));
|
|
--
|
|
1.8.3.1
|
|
|