Permalink
Browse files

Merge pull request #1052 from jan-ruzicka/patch-1

stat test reduced to regular files and FIFOs
  • Loading branch information...
2 parents da79cac + 902efac commit 88a5c0c2fe9bbc93d748a69e516b300be86882ca @ggreer committed on GitHub Mar 12, 2017
Showing with 20 additions and 1 deletion.
  1. +20 −1 src/search.c
View
@@ -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) {

0 comments on commit 88a5c0c

Please sign in to comment.