Versions Compared

Key

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

...

Code Block
bgColor#ffcccc
if (!fgets(filename, sizeof(filename), stdin)) {
    /* handle error */
}

if (open(filename, O_WRONLY) == -1) {
    /* handle error */
}
/* if filename is a fifo or a locked device the program may now hang in the open call */

...

Code Block
bgColor#ccccff
struct stat pre_s;
struct stat post_s;
int fildes;

if (!fgets(filename, sizeof(filename), stdin)) {
    /* handle error */
}

if ((stat(filename, &pre_s) != 0) || (!S_ISREG(pre_s.st_mode))) {
    /* handle error */
}

/* due to a race condition here, we will verify with fstat later */

if ((fildes = open(filename, O_WRONLY)) == -1) {
    /* handle error */
}

if (fstat(fildes, &post_s) != 0) {
    /* handle error */
}

if(!(pre_s.st_mode == post_s.st_mode &&
     pre_s.st_ino  == post_s.st_ino  &&
     pre_s.st_dev  == post_s.st_dev)) {
    /* handle error */
}

/* operate on file */

...

Code Block
bgColor#ccccff
HANDLE hFile = CreateFile(
  pFullPathName,
  0, 0, NULL, OPEN_EXISTING, 0, NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
  /* handle error */
}
else {
	  if (GetFileType(hFile) != FILE_TYPE_DISK) {
   		 /* handle error */
	  }
	  /* operate on file */
}

Risk Assessment

...