Skip to content

Commit

Permalink
[XrdXrootd] Fix time-of-check vs time-of-use race condition
Browse files Browse the repository at this point in the history
Between the call to stat and the open, the file size may change.
First open the file, then use fstat with the file descriptor to
ensure consistency.
  • Loading branch information
amadio committed Nov 26, 2024
1 parent 65a6eee commit c3696ef
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/XrdXrootd/XrdXrootdPrepare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,19 @@ int XrdXrootdPrepare::Open(const char *reqid, int &fsz)
strcpy(path, (const char *)LogDir);
strcpy(path+LogDirLen, reqid);

// Open the file and return the file descriptor
//
if ((fd = open((const char *)path, O_RDONLY)) < 0) return -errno;

// Get the file size
//
if (stat((const char *)path, &buf)) return -errno;
if (fstat(fd, &buf) != 0) {
close(fd);
return -errno;
}

fsz = buf.st_size;

// Open the file and return the file descriptor
//
if ((fd = open((const char *)path, O_RDONLY)) < 0) return -errno;
return fd;
}

Expand Down

0 comments on commit c3696ef

Please sign in to comment.