resolved: fix TOCTOU in hook discovery

Coverity complains that the directory is not pinned by FD
so it might changed between the stat and the open

CID#1643236

Follow-up for 8209f4adcd
This commit is contained in:
Luca Boccassi
2026-03-28 19:56:31 +00:00
parent bf37ed0a65
commit 3e889473c9
+9 -13
View File
@@ -391,19 +391,6 @@ static int manager_hook_discover(Manager *m) {
usec_t seen_usec = now(CLOCK_MONOTONIC);
struct stat st;
if (stat(dp, &st) < 0) {
if (errno == ENOENT)
r = 0;
else
r = log_warning_errno(errno, "Failed to stat %s/: %m", dp);
goto finish;
}
if (stat_inode_unmodified(&st, &m->hook_stat))
return 0;
d = opendir(dp);
if (!d) {
if (errno == ENOENT)
@@ -414,6 +401,15 @@ static int manager_hook_discover(Manager *m) {
goto finish;
}
struct stat st;
if (fstat(dirfd(d), &st) < 0) {
r = log_warning_errno(errno, "Failed to fstat %s/: %m", dp);
goto finish;
}
if (stat_inode_unmodified(&st, &m->hook_stat))
return 0;
for (;;) {
errno = 0;
struct dirent *de = readdir_no_dot(d);