From: Zhang Yi <yi.zhang@huawei.com> hulk inclusion category: bugfix bugzilla: https://atomgit.com/openeuler/kernel/issues/8477 -------------------------------- In cases of appending write beyond the end of file (EOF), ext4_block_zero_eof should be called within ext4_iomap_buffered_write and ext4_iomap_write_end to zero out the partial block beyond the EOF. This prevents exposing stale data that might be written through mmap. Fixes: 5721968224e0 ("ext4: implement zero_range iomap path") Signed-off-by: Zhang Yi <yi.zhang@huawei.com> Signed-off-by: Yongjian Sun <sunyongjian1@huawei.com> --- fs/ext4/file.c | 17 +++++++++++++++++ fs/ext4/inode.c | 4 +++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/fs/ext4/file.c b/fs/ext4/file.c index b8440444d997..e3cbb520f8bc 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -291,6 +291,23 @@ static ssize_t ext4_iomap_buffered_write(struct kiocb *iocb, { struct inode *inode = file_inode(iocb->ki_filp); const struct iomap_ops *iomap_ops; + unsigned int blocksize = i_blocksize(inode); + loff_t old_size = i_size_read(inode); + int ret; + + /* + * If the position is beyond the EOF, it is necessary to zero out the + * partial block that beyond the existing EOF, as it may contains + * stale data written through mmap. + */ + if (iocb->ki_pos > old_size && (old_size & (blocksize - 1))) { + loff_t end = round_up(old_size, blocksize); + if (iocb->ki_pos < end) + end = iocb->ki_pos; + ret = ext4_block_zero_eof(inode, old_size, end); + if (ret < 0) + return ret; + } if (test_opt(inode->i_sb, DELALLOC) && !ext4_nonda_switch(inode->i_sb)) iomap_ops = &ext4_iomap_buffered_da_write_ops; diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index da5de8561a6d..a78245878298 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -4085,8 +4085,10 @@ static int ext4_iomap_write_end(struct file *file, folio_unlock(folio); folio_put(folio); - if (old_size < pos) + if (old_size < pos) { pagecache_isize_extended(inode, old_size, pos); + ext4_block_zero_eof(inode, old_size, pos); + } /* * For delalloc, if we have pre-allocated more blocks and copied -- 2.39.2