]> Sergey Matveev's repositories - bfs.git/commitdiff
bftw: Make sure we don't close a directory while we unwrap it
authorTavian Barnes <tavianator@tavianator.com>
Thu, 12 Oct 2023 19:50:59 +0000 (15:50 -0400)
committerTavian Barnes <tavianator@tavianator.com>
Thu, 12 Oct 2023 19:50:59 +0000 (15:50 -0400)
bftw_cache_reserve() can lead to bftw_cache_pop(), which could close the
directory we're trying to unwrap!  If that happened, we would try
dup_cloexec(-1), which would fail with EBADF, so there was no observable
bug.  But it's better to avoid the whole situation.

src/bftw.c

index 1995e125ea92309e256a666f71a31987114bd6c3..06a0085ea4ea75055ace362660e5b83df7776137 100644 (file)
@@ -811,8 +811,12 @@ static int bftw_unwrapdir(struct bftw_state *state, struct bftw_file *file) {
                return bftw_close(state, file);
        }
 
-       if (bftw_cache_reserve(state) != 0) {
-               return -1;
+       // Make room for dup()
+       bftw_cache_pin(cache, file);
+       int ret = bftw_cache_reserve(state);
+       bftw_cache_unpin(cache, file);
+       if (ret != 0) {
+               return ret;
        }
 
        int fd = dup_cloexec(file->fd);