From: Timo Sirainen Date: Mon, 25 Feb 2019 18:55:03 +0000 (+0200) Subject: fs-posix: fs_read_stream() - Don't close file's fd X-Git-Tag: 2.3.6~123 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ae72b863882cea401fa67ddb0be4549cba8b83de;p=thirdparty%2Fdovecot%2Fcore.git fs-posix: fs_read_stream() - Don't close file's fd 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) --- diff --git a/src/lib-fs/fs-posix.c b/src/lib-fs/fs-posix.c index 20531db156..194fdec1e5 100644 --- a/src/lib-fs/fs-posix.c +++ b/src/lib-fs/fs-posix.c @@ -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;