Skip to content

Commit

Permalink
[XrdHttp] Fix time-of-check vs time-of-use race condition
Browse files Browse the repository at this point in the history
First open then fstat, instead of stat then fopen, to guarantee that
the information from stat and open are consistent with each other.
Afterwards, convert file descriptor to FILE* with fdopen. Ensure also
that the file is closed should an error occur.
  • Loading branch information
amadio committed Nov 26, 2024
1 parent c3696ef commit 63c4cb3
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/XrdHttp/XrdHttpProtocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2235,20 +2235,31 @@ int XrdHttpProtocol::xsecretkey(XrdOucStream & Config) {
if (val[0] == '/') {
struct stat st;
inFile = true;
if ( stat(val, &st) ) {
eDest.Emsg("Config", errno, "stat shared secret key file", val);
int fd = open(val, O_RDONLY);

if ( fd == -1 ) {
eDest.Emsg("Config", errno, "open shared secret key file", val);
return 1;
}

if ( fstat(fd, &st) != 0 ) {
eDest.Emsg("Config", errno, "fstat shared secret key file", val);
close(fd);
return 1;
}

if ( st.st_mode & S_IWOTH & S_IWGRP & S_IROTH) {
eDest.Emsg("Config", "For your own security, the shared secret key file cannot be world readable or group writable'", val, "'");
eDest.Emsg("Config",
"For your own security, the shared secret key file cannot be world readable or group writable '", val, "'");
close(fd);
return 1;
}

FILE *fp = fopen(val,"r");
FILE *fp = fdopen(fd, "r");

if( fp == NULL ) {
eDest.Emsg("Config", errno, "open shared secret key file", val);
if ( fp == nullptr ) {
eDest.Emsg("Config", errno, "fdopen shared secret key file", val);
close(fd);
return 1;
}

Expand Down

0 comments on commit 63c4cb3

Please sign in to comment.