Text   |  XML   |  ReML   |   Visible Warnings:

File System Race Condition  at squat-trie.c:1706

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

squat_trie_write

(/home/sate/Testcases/c/cve/dovecot-1.2.0/src/plugins/fts-squat/squat-trie.c)expand/collapse
Show more  
 1609  static int squat_trie_write(struct squat_trie_build_context *ctx)
 1610  {
 1611          struct squat_trie *trie = ctx->trie;
 1612          struct file_lock *file_lock = NULL;
 1613          struct ostream *output;
 1614          const char *path;
 1615          int fd = -1, ret = 0;
 1616   
 1617          if ((trie->hdr.used_file_size > sizeof(trie->hdr) &&
 1618              trie->unmapped_child_count < trie->hdr.node_count/4) || 1) {
 1619                  /* we might as well recreate the file */
 1620                  ctx->compress_nodes = TRUE;
 1621   
 1622[+]                 path = t_strconcat(trie->path, ".tmp", NULL);
 1623[+]                 fd = squat_trie_create_fd(trie, path, O_TRUNC);
 1624                  if (fd == -1)
 1625                          return -1;
 1626   
 1627                  if (trie->lock_method != FILE_LOCK_METHOD_DOTLOCK) {
 1628                          ret = file_wait_lock(fd, path, F_WRLCK,
 1629                                               trie->lock_method,
 1630                                               SQUAT_TRIE_LOCK_TIMEOUT,
 1631                                               &file_lock);
 1632                          if (ret <= 0) {
 1633                                  if (ret == 0) {
 1634                                          i_error("file_wait_lock(%s) failed: %m",
 1635                                                  path);
 1636                                  }
 1637                                  (void)close(fd);
 1638                                  return -1;
 1639                          }
 1640                  }
 1641   
 1642                  output = o_stream_create_fd(fd, 0, FALSE);
 1643                  o_stream_cork(output);
 1644                  o_stream_send(output, &trie->hdr, sizeof(trie->hdr));
 1645          } else {
 1646                  /* we need to lock only while header is being written */
 1647                  path = trie->path;
 1648                  ctx->compress_nodes =
 1649                          trie->hdr.used_file_size == sizeof(trie->hdr);
 1650   
 1651                  if (trie->hdr.used_file_size == 0) {
 1652                          /* lock before opening the file, in case we reopen it */
 1653                          if (squat_trie_write_lock(ctx) < 0)
 1654                                  return -1;
 1655                  }
 1656                  output = o_stream_create_fd(trie->fd, 0, FALSE);
 1657                  o_stream_cork(output);
 1658   
 1659                  if (trie->hdr.used_file_size != 0)
 1660                          o_stream_seek(output, trie->hdr.used_file_size);
 1661                  else 
 1662                          o_stream_send(output, &trie->hdr, sizeof(trie->hdr));
 1663          }
 1664   
 1665          ctx->output = output;
 1666          ret = squat_write_nodes(ctx);
 1667          ctx->output = NULL;
 1668   
 1669          /* write 1 byte guard at the end of file, so that we can verify broken
 1670             squat_unpack_num() input by checking if data==end */
 1671          o_stream_send(output, "", 1);
 1672   
 1673          if (trie->corrupted)
 1674                  ret = -1;
 1675          if (ret == 0)
 1676                  ret = squat_trie_write_lock(ctx);
 1677          if (ret == 0) {
 1678                  trie->hdr.used_file_size = output->offset;
 1679                  o_stream_seek(output, 0);
 1680                  o_stream_send(output, &trie->hdr, sizeof(trie->hdr));
 1681          }
 1682          if (output->last_failed_errno != 0) {
 1683                  errno = output->last_failed_errno;
 1684                  i_error("write() to %s failed: %m", path);
 1685                  ret = -1;
 1686          }
 1687          o_stream_destroy(&output);
 1688   
 1689          if (fd == -1) {
 1690                  /* appended to the existing file */
 1691                  i_assert(file_lock == NULL);
 1692                  return ret;
 1693          }
 1694   
 1695          /* recreating the trie file */
 1696          if (ret < 0) {
 1697                  if (close(fd) < 0)
 1698                          i_error("close(%s) failed: %m", path);
 1699                  fd = -1;
 1700          } else if (rename(path, trie->path) < 0) {
 1701                  i_error("rename(%s, %s) failed: %m", path, trie->path);
 1702                  ret = -1;
 1703          }
 1704   
 1705          if (ret < 0) {
 1706                  if (unlink(path) < 0 && errno != ENOENT)
Show more  




Change Warning 11951.25517 : File System Race Condition

Priority:
State:
Finding:
Owner:
Note: