From faf9da01ad93bd48523f0966646bbd3ca85a2951 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 9 Mar 2015 18:58:47 -0400 Subject: [PATCH] journalctl: unlink without checking with access first It is more elegant to do this in one step. Coverity complains about the TOCTOU difference, but it is not an actual problem (CID #1237777). --- src/journal/journalctl.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 2b0e00ee8..f0f03b069 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1316,19 +1316,16 @@ static int setup_keys(void) { SD_ID128_FORMAT_VAL(machine)) < 0) return log_oom(); - if (access(p, F_OK) >= 0) { - if (arg_force) { - r = unlink(p); - if (r < 0) { - log_error_errno(errno, "unlink(\"%s\") failed: %m", p); - r = -errno; - goto finish; - } - } else { - log_error("Sealing key file %s exists already. (--force to recreate)", p); - r = -EEXIST; + if (arg_force) { + r = unlink(p); + if (r < 0 && errno != ENOENT) { + r = log_error_errno(errno, "unlink(\"%s\") failed: %m", p); goto finish; } + } else if (access(p, F_OK) >= 0) { + log_error("Sealing key file %s exists already. Use --force to recreate.", p); + r = -EEXIST; + goto finish; } if (asprintf(&k, "/var/log/journal/" SD_ID128_FORMAT_STR "/fss.tmp.XXXXXX", -- 2.30.2