diff mbox

[v2] xfs: prevent creating negative-sized file via INSERT_RANGE

Message ID 20180417053907.GE5203@magnolia (mailing list archive)
State Superseded
Headers show

Commit Message

Darrick J. Wong April 17, 2018, 5:39 a.m. UTC
How about this instead?

--D

---
From: Eric Biggers <ebiggers@google.com>

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 doing an unsigned comparison instead.

Reproducer:
    xfs_io -f file -c "truncate $(((1<<63)-1))" -c "finsert 0 4096"

Fixes: a904b1ca5751 ("xfs: Add support FALLOC_FL_INSERT_RANGE for fallocate")
Cc: <stable@vger.kernel.org> # v4.1+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
[darrick: rearrange this whole function to use subtraction to avoid
 overflow of the signed integer addition]
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 fs/xfs/xfs_file.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Christoph Hellwig April 17, 2018, 7:09 a.m. UTC | #1
>  	} else if (mode & FALLOC_FL_INSERT_RANGE) {
>  		unsigned int blksize_mask = i_blocksize(inode) - 1;
> +		loff_t		isize;
>  
> +		isize = i_size_read(inode);

Maybe move the assignment up:

		
		loff_t		isize = i_size_read(inode);

But functionally this looks fine:

Reviewed-by: Christoph Hellwig <hch@lst.de>
--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c
index 9fd9dd7..a385334 100644
--- a/fs/xfs/xfs_file.c
+++ b/fs/xfs/xfs_file.c
@@ -779,21 +779,27 @@  xfs_file_fallocate(
 			goto out_unlock;
 	} else if (mode & FALLOC_FL_INSERT_RANGE) {
 		unsigned int blksize_mask = i_blocksize(inode) - 1;
+		loff_t		isize;
 
-		new_size = i_size_read(inode) + len;
-		if (offset & blksize_mask || len & blksize_mask) {
-			error = -EINVAL;
+		isize = i_size_read(inode);
+
+		/*
+		 * New inode size must not exceed ->s_maxbytes, accounting for
+		 * possible signed overflow.
+		 */
+		if (inode->i_sb->s_maxbytes - isize < len) {
+			error = -EFBIG;
 			goto out_unlock;
 		}
 
-		/* check the new inode size does not wrap through zero */
-		if (new_size > inode->i_sb->s_maxbytes) {
-			error = -EFBIG;
+		if (offset & blksize_mask || len & blksize_mask) {
+			error = -EINVAL;
 			goto out_unlock;
 		}
 
+		new_size = isize + len;
 		/* Offset should be less than i_size */
-		if (offset >= i_size_read(inode)) {
+		if (offset >= isize) {
 			error = -EINVAL;
 			goto out_unlock;
 		}