stat test reduced to regular files and FIFOs
Stat checks now allow searching only in regular files and FIFOs.
The stat test were duplicated to prevent TOCTOU.
related to issue
@@ -257,7 +257,8 @@ void search_file(const char *file_full_path) {
goto cleanup;
}
- if ((statbuf.st_mode & S_IFMT) == 0) {
+ // handling only regular files and FIFOs
+ if (!S_ISREG(statbuf.st_mode) && !S_ISFIFO(statbuf.st_mode)) {
log_err("Skipping %s: Mode %u is not a file.", file_full_path, statbuf.st_mode);
goto cleanup;
}
@@ -269,6 +270,24 @@ void search_file(const char *file_full_path) {
goto cleanup;
}
+ // repeating stat check with file handle to prevent TOCTOU issue
+ rv = fstat(fd, &statbuf);
+ if (rv != 0) {
+ log_err("Skipping %s: Error fstat()ing file.", file_full_path);
+ goto cleanup;
+ }
+
+ if (opts.stdout_inode != 0 && opts.stdout_inode == statbuf.st_ino) {
+ log_debug("Skipping %s: stdout is redirected to it", file_full_path);
+ goto cleanup;
+ }
+
+ // handling only regular files and FIFOs
+ if (!S_ISREG(statbuf.st_mode) && !S_ISFIFO(statbuf.st_mode)) {
+ log_err("Skipping %s: Mode %u is not a file.", file_full_path, statbuf.st_mode);
+ goto cleanup;
+ }
+
print_init_context();
if (statbuf.st_mode & S_IFIFO) {
