Skip to content

Commit b0df636

Browse files
jankaragregkh
authored andcommitted
xfs: Fix stale data exposure when readahead races with hole punch
commit 40144e4 upstream. Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both XFS_IOLOCK_EXCL and XFS_MMAPLOCK_EXCL which provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered by fadvise() or madvise() from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache in xfs_flush_unmap_range() but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption. Fix the problem by protecting handling of readahead requests by XFS_IOLOCK_SHARED similarly as we protect reads. CC: stable@vger.kernel.org Link: https://lore.kernel.org/linux-fsdevel/CAOQ4uxjQNmxqmtA_VbYW0Su9rKRk2zobJmahcyeaEVOFKVQ5dw@mail.gmail.com/ Reported-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b56335c commit b0df636

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

‎fs/xfs/xfs_file.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/pagevec.h>
3434
#include <linux/backing-dev.h>
3535
#include <linux/mman.h>
36+
#include <linux/fadvise.h>
3637

3738
static const struct vm_operations_struct xfs_file_vm_ops;
3839

@@ -939,6 +940,30 @@ xfs_file_fallocate(
939940
return error;
940941
}
941942

943+
STATIC int
944+
xfs_file_fadvise(
945+
struct file *file,
946+
loff_t start,
947+
loff_t end,
948+
int advice)
949+
{
950+
struct xfs_inode *ip = XFS_I(file_inode(file));
951+
int ret;
952+
int lockflags = 0;
953+
954+
/*
955+
* Operations creating pages in page cache need protection from hole
956+
* punching and similar ops
957+
*/
958+
if (advice == POSIX_FADV_WILLNEED) {
959+
lockflags = XFS_IOLOCK_SHARED;
960+
xfs_ilock(ip, lockflags);
961+
}
962+
ret = generic_fadvise(file, start, end, advice);
963+
if (lockflags)
964+
xfs_iunlock(ip, lockflags);
965+
return ret;
966+
}
942967

943968
STATIC loff_t
944969
xfs_file_remap_range(
@@ -1235,6 +1260,7 @@ const struct file_operations xfs_file_operations = {
12351260
.fsync = xfs_file_fsync,
12361261
.get_unmapped_area = thp_get_unmapped_area,
12371262
.fallocate = xfs_file_fallocate,
1263+
.fadvise = xfs_file_fadvise,
12381264
.remap_file_range = xfs_file_remap_range,
12391265
};
12401266

0 commit comments

Comments
 (0)