Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
struct stat lstat_info;
int fd;
if (lstat("some_file", &lstat_info) == -1) {
  /* handle error */
}
if (!S_ISLNK(lstat_info.st_mode)) {
   if ((fd = open("some_file", O_EXCL|O_RDWR, 0600)) == -1) {
       /* handle error */
   }
}
write(fd, userbuf, userlen);

Some systems provide the O_NOFOLLOW flag to help mitigate this problem. If the supplied pathname is a symbolic link, then the open will fail.

Code Block
bgColor#ccccff

int fd;
if ((fd = open("some_file", O_EXCL|O_RDWR|O_NOFOLLOW, 0600)) == -1) {
  /* handle error */
}
write(fd, userbuf, userlen);

This compliant solution properly checks for the existence of a link and eliminates the race condition.

...