Text   |  XML   |  ReML   |   Visible Warnings:

File System Race Condition  at safe-mkdir.c:58

No properties have been set. | edit properties
Jump to warning location ↓ warning details...
Show Events | Options

safe_mkdir

(/home/sate/Testcases/c/cve/dovecot-1.2.0/src/lib/safe-mkdir.c)expand/collapse
Show more  
 10  int safe_mkdir(const char *dir, mode_t mode, uid_t uid, gid_t gid)
 11  {
 12          struct stat st;
 13          int fd, ret = 2, changed_ret = 0;
 14   
 15          if (lstat(dir, &st) < 0) {
 16                  if (errno != ENOENT)
 17                          i_fatal("lstat() failed for %s: %m", dir);
 18   
 19                  if (mkdir(dir, mode) < 0) {
 20                          if (errno != EEXIST)
 21                                  i_fatal("Can't create directory %s: %m", dir);
 22                  } else {
 23                          /* created it */
 24                          ret = changed_ret = 1;
 25                  }
 26          }
 27   
 28          /* use fchown() and fchmod() just to make sure we aren't following 
 29             symbolic links. */
 30          fd = open(dir, O_RDONLY);
 31          if (fd == -1)
 32                  i_fatal("open() failed for %s: %m", dir);
 33   
 34          if (fstat(fd, &st) < 0)
 35                  i_fatal("fstat() failed for %s: %m", dir);
 36   
 37          if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
 38                  i_fatal("Not a directory %s", dir);
 39   
 40          /* change the file owner first, since it's the only user one who 
 41             can mess up with the file mode. */
 42          if (st.st_uid != uid || st.st_gid != gid) {
 43                  if (fchown(fd, uid, gid) < 0)
 44                          i_fatal("fchown() failed for %s: %m", dir);
 45                  ret = changed_ret;
 46          }
 47   
 48          if ((st.st_mode & 07777) != mode) {
 49                  if (fchmod(fd, mode) < 0)
 50                          i_fatal("chmod() failed for %s: %m", dir);
 51                  ret = changed_ret;
 52          }
 53   
 54          if (close(fd) < 0)
 55                  i_fatal("close() failed for %s: %m", dir);
 56   
 57          /* paranoia: make sure we succeeded in everything. */
 58          if (lstat(dir, &st) < 0)
Show more  




Change Warning 7125.24612 : File System Race Condition

Priority:
State:
Finding:
Owner:
Note: