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.9~756 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1abd55c07ff6f8b5bc652eff50e8977a77c2ddb9;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 f8019d0b70..fd8dc0543e 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;