]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
fs-posix: fs_read_stream() - Don't close file's fd
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 25 Feb 2019 18:55:03 +0000 (20:55 +0200)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Thu, 28 Feb 2019 05:33:40 +0000 (07:33 +0200)
This is especially important with newly created files, because they may
still be accessed after reading. The next file access attempt after
fs_read_stream() might cause the file to be recreated and crash.

Fixes:
Panic: file fs-posix.c: line 252 (fs_posix_create): assertion failed: (file->temp_path == NULL)

src/lib-fs/fs-posix.c

index 20531db156aade0a5ceeb844c6e943734e3cfb8a..194fdec1e5db3dc1b01c32bd0864c55aa583957f 100644 (file)
@@ -444,12 +444,18 @@ fs_posix_read_stream(struct fs_file *_file, size_t max_buffer_size)
 {
        struct posix_fs_file *file = (struct posix_fs_file *)_file;
        struct istream *input;
+       int fd_dup;
 
        if (fs_posix_open_for_read(file) < 0)
                input = i_stream_create_error_str(errno, "%s", fs_last_error(_file->fs));
+       else if ((fd_dup = dup(file->fd)) == -1)
+               input = i_stream_create_error_str(errno, "dup() failed: %m");
        else {
-               /* the stream could live even after the fs_file */
-               input = i_stream_create_fd_autoclose(&file->fd, max_buffer_size);
+               /* The stream could live even after the fs_file.
+                  Don't use file->fd directly, because the fd may still be
+                  used for other purposes. It's especially important for files
+                  that were just created. */
+               input = i_stream_create_fd_autoclose(&fd_dup, max_buffer_size);
        }
        i_stream_set_name(input, file->full_path);
        return input;