lib/utmp: merge file access

Avoid checking if the file exists before opening it.

Resolves a CodeQL report of Time-of-check time-of-use filesystem race
condition.
This commit is contained in:
Christian Göttsche
2023-12-11 17:18:38 +01:00
committed by Serge Hallyn
parent 0d7cb003b7
commit 7f20bb88ad

View File

@@ -85,15 +85,13 @@ static void failtmp (const char *username, const struct utmp *failent)
* feature to be used.
*/
if (access (ftmp, F_OK) != 0) {
return;
}
fd = open (ftmp, O_WRONLY | O_APPEND);
if (-1 == fd) {
SYSLOG ((LOG_WARN,
"Can't append failure of user %s to %s.",
username, ftmp));
if (errno != ENOENT) {
SYSLOG ((LOG_WARN,
"Can't append failure of user %s to %s: %m",
username, ftmp));
}
return;
}