The direct path is not supported on verity files. Attempts to use direct I/O path on such files should fall back to buffered I/O path. Add a fall back to buffered I/O at two place, in a common fast path and latter when lock is acquired. The second check prevents TOCTOU issue with reading fsverity_active() status and resetting IOCB_DIRECT flag. If one thread saw fsverity_active() to be false, and then second thread acquired XFS_IOLOCK_EXCL and enabled fsverity. The first thread will go through the DIO path. Signed-off-by: Darrick J. Wong Signed-off-by: Andrey Albershteyn --- fs/xfs/xfs_file.c | 59 +++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index e3f4353b3b78..cba194d678fc 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -246,6 +246,25 @@ static const struct iomap_dio_ops xfs_dio_read_bounce_ops = { .bio_set = &iomap_ioend_bioset, }; +STATIC ssize_t +xfs_file_buffered_read( + struct kiocb *iocb, + struct iov_iter *to) +{ + struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp)); + ssize_t ret; + + trace_xfs_file_buffered_read(iocb, to); + + ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED); + if (ret) + return ret; + ret = generic_file_read_iter(iocb, to); + xfs_iunlock(ip, XFS_IOLOCK_SHARED); + + return ret; +} + STATIC ssize_t xfs_file_dio_read( struct kiocb *iocb, @@ -266,6 +285,16 @@ xfs_file_dio_read( ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED); if (ret) return ret; + /* + * Re-check verity status after acquiring lock. This prevents TOCTOU in + * xfs_file_read_iter() while falling back from DIO to buffered I/O as + * now we are holding a lock + */ + if (fsverity_active(VFS_I(ip))) { + xfs_iunlock(ip, XFS_IOLOCK_SHARED); + iocb->ki_flags &= ~IOCB_DIRECT; + return xfs_file_buffered_read(iocb, to); + } if (mapping_stable_writes(iocb->ki_filp->f_mapping)) { dio_ops = &xfs_dio_read_bounce_ops; dio_flags |= IOMAP_DIO_BOUNCE; @@ -282,7 +311,8 @@ xfs_file_dax_read( struct kiocb *iocb, struct iov_iter *to) { - struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host); + struct inode *inode = iocb->ki_filp->f_mapping->host; + struct xfs_inode *ip = XFS_I(inode); ssize_t ret = 0; trace_xfs_file_dax_read(iocb, to); @@ -300,25 +330,6 @@ xfs_file_dax_read( return ret; } -STATIC ssize_t -xfs_file_buffered_read( - struct kiocb *iocb, - struct iov_iter *to) -{ - struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp)); - ssize_t ret; - - trace_xfs_file_buffered_read(iocb, to); - - ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED); - if (ret) - return ret; - ret = generic_file_read_iter(iocb, to); - xfs_iunlock(ip, XFS_IOLOCK_SHARED); - - return ret; -} - STATIC ssize_t xfs_file_read_iter( struct kiocb *iocb, @@ -333,6 +344,14 @@ xfs_file_read_iter( if (xfs_is_shutdown(mp)) return -EIO; + /* + * In case fs-verity is enabled, we also fallback to the buffered read + * from the direct read path. Therefore, IOCB_DIRECT is set and need to + * be cleared (see generic_file_read_iter()) + */ + if (fsverity_active(inode)) + iocb->ki_flags &= ~IOCB_DIRECT; + if (IS_DAX(inode)) ret = xfs_file_dax_read(iocb, to); else if (iocb->ki_flags & IOCB_DIRECT) -- 2.54.0