Skip to content

Commit 7d83fb1

Browse files
committed
xfs: prevent creating negative-sized file via INSERT_RANGE
During the "insert range" fallocate operation, i_size grows by the specified 'len' bytes. XFS verifies that i_size + len < s_maxbytes, as it should. But this comparison is done using the signed 'loff_t', and 'i_size + len' can wrap around to a negative value, causing the check to incorrectly pass, resulting in an inode with "negative" i_size. This is possible on 64-bit platforms, where XFS sets s_maxbytes = LLONG_MAX. ext4 and f2fs don't run into this because they set a smaller s_maxbytes. Fix it by using subtraction instead. Reproducer: xfs_io -f file -c "truncate $(((1<<63)-1))" -c "finsert 0 4096" Fixes: a904b1c ("xfs: Add support FALLOC_FL_INSERT_RANGE for fallocate") Cc: <stable@vger.kernel.org> # v4.1+ Originally-From: Eric Biggers <ebiggers@google.com> Signed-off-by: Eric Biggers <ebiggers@google.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> [darrick: fix signed integer addition overflow too] Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
1 parent 2c4306f commit 7d83fb1

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎fs/xfs/xfs_file.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,22 +778,26 @@ xfs_file_fallocate(
778778
if (error)
779779
goto out_unlock;
780780
} else if (mode & FALLOC_FL_INSERT_RANGE) {
781-
unsigned int blksize_mask = i_blocksize(inode) - 1;
781+
unsigned int blksize_mask = i_blocksize(inode) - 1;
782+
loff_t isize = i_size_read(inode);
782783

783-
new_size = i_size_read(inode) + len;
784784
if (offset & blksize_mask || len & blksize_mask) {
785785
error = -EINVAL;
786786
goto out_unlock;
787787
}
788788

789-
/* check the new inode size does not wrap through zero */
790-
if (new_size > inode->i_sb->s_maxbytes) {
789+
/*
790+
* New inode size must not exceed ->s_maxbytes, accounting for
791+
* possible signed overflow.
792+
*/
793+
if (inode->i_sb->s_maxbytes - isize < len) {
791794
error = -EFBIG;
792795
goto out_unlock;
793796
}
797+
new_size = isize + len;
794798

795799
/* Offset should be less than i_size */
796-
if (offset >= i_size_read(inode)) {
800+
if (offset >= isize) {
797801
error = -EINVAL;
798802
goto out_unlock;
799803
}

0 commit comments

Comments
 (0)