From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AB8JxZp1iK6sV67zC9IAQ+9T0UHV+kaKGkIVawx7xJP4VwYHUov1KKbRHp6IarA98tUuBma1GqbK ARC-Seal: i=1; a=rsa-sha256; t=1525767288; cv=none; d=google.com; s=arc-20160816; b=qbpjZGAtdBZv0tFttq4CgSNK1kj28prS1S8EiUv6JHNeV1IKe/PpqNhze/M4XugjYS G5B12+t1OVvfExldj7M9owNcBoz0ejHuOl0unPn8A5u/egnpUvsXkDR102SIc2loLYHR VCmIcUoJjLyM1LcCBKI9vcya7gab5zda34bJarFxm/yL4zv4TdSvZX8nSrcrY4v8YqdE YRibw4CUMY/qnhBTn6RRFT+6s2CkHqHucE3i7vKgWE8fhdIaXRUqKYYF4Xokx5gpnaJM vHQnUdFuiUIELUaeEFxp511eaP1ANT0+lVlmef/4nU/VxA249fZcSfviUQ0VVvHqWTb4 Uf2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=LDgjFijAOBCEneuMZwJfKh3Hl5/wJReHYNF2eoy7NyI=; b=VCmXlGjDYHdTyHMhfEXuR8O4qozXup2ePhqZIveSW9sVXdspeiO38iZ7BJMJw0dYes jBWazYdU7f/cvWxe9/UZGFQeWBRDdjTGHMicD5rCLQiGFA3fj60Ne/jAyYrJgEchRyfJ nfVYOZ06NFcnKljWafHo/6mQCZgbXHUVU7azO1H/K3ENZFrxRkKCiimlODKdv0lBiU69 EnHJhIez0KOO6f3TPO8fLd+kgnTX3byhDIiIPR8GGcZUIosqIldkg1E7FxOrvM26cTea mGIZ3RWV9mfaiLw7aQ2N0klE2ili/OiP1FtoNjvtIweyQpIm0bmcHURhUXuYOK7xi+wy GhaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bWxKGkAh; spf=pass (google.com: domain of srs0=4in3=h3=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=SRS0=4In3=H3=linuxfoundation.org=gregkh@kernel.org Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bWxKGkAh; spf=pass (google.com: domain of srs0=4in3=h3=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=SRS0=4In3=H3=linuxfoundation.org=gregkh@kernel.org From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , Christoph Hellwig , "Darrick J. Wong" Subject: [PATCH 4.14 19/43] xfs: prevent creating negative-sized file via INSERT_RANGE Date: Tue, 8 May 2018 10:10:38 +0200 Message-Id: <20180508074007.143305089@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180508074003.984433784@linuxfoundation.org> References: <20180508074003.984433784@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-LABELS: =?utf-8?b?IlxcU2VudCI=?= X-GMAIL-THRID: =?utf-8?q?1599882807260392086?= X-GMAIL-MSGID: =?utf-8?q?1599882960922932536?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Darrick J. Wong commit 7d83fb14258b9961920cd86f0b921caaeb3ebe85 upstream. 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: a904b1ca5751 ("xfs: Add support FALLOC_FL_INSERT_RANGE for fallocate") Cc: # v4.1+ Originally-From: Eric Biggers Signed-off-by: Eric Biggers Reviewed-by: Christoph Hellwig Reviewed-by: Darrick J. Wong [darrick: fix signed integer addition overflow too] Signed-off-by: Darrick J. Wong Signed-off-by: Greg Kroah-Hartman --- fs/xfs/xfs_file.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -811,22 +811,26 @@ xfs_file_fallocate( if (error) goto out_unlock; } else if (mode & FALLOC_FL_INSERT_RANGE) { - unsigned int blksize_mask = i_blocksize(inode) - 1; + unsigned int blksize_mask = i_blocksize(inode) - 1; + loff_t isize = i_size_read(inode); - new_size = i_size_read(inode) + len; if (offset & blksize_mask || len & blksize_mask) { error = -EINVAL; goto out_unlock; } - /* check the new inode size does not wrap through zero */ - if (new_size > inode->i_sb->s_maxbytes) { + /* + * 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; } + 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; }