Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4001858imm; Mon, 14 May 2018 00:46:38 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoC8s6Dg0NqU6zBx63fYe1amyrjYr2ApNtbmz/TdQH9fQ1UoS8TYAcmG0s1wq4/TrrNsmxQ X-Received: by 2002:a17:902:31a4:: with SMTP id x33-v6mr8779569plb.355.1526283998154; Mon, 14 May 2018 00:46:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526283998; cv=none; d=google.com; s=arc-20160816; b=za4sUcKJp6viIEhyYwr2S4v6zGxTHSaoORyCQW2JdTauqQien+n9I0MmRkj4OrhY4l zWWyxhM/hIsyqFKt1CZl65GpS/F0Snze6MGg8VOdMQ5fNMgCGuK7SrDYoyDvc6zjeZpi c6VUBg0/kxgHJBt4JKMijR6ig5UYHoYIujsFXBB43apuJjV7fvETrUR3/7TlhGh3+Hky R3BiOcG5Wap17TMSsuMvCI5QenHEthRFW3A1VrwLINuyPnmS6gYy7ca/9XJ6xn2GgQuI YWy4dYjtvXhXJ2SNawFVD7ipzdmk5/U+jNqM4kjDBDfmLj0vN7vOIoVFt+LQhgyKXmIG 0jMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=9iPSzpzGc6HOvRZdesJ0EXMVA/Lt804cnXBvwJU6O+M=; b=I+EvclEYDNrGGGlM4wk6LgTNq+IQ/Z8GGBZ7ER9QsA+3DHaxV9+3el4oOf9ahpXLew b73JBLKxE8P+G2WEkQUYdM2g5uO8DDiWPz+4JR9l+HWkZV1ExjBcfOkL+PGTY7YEtgc5 IgdxEsse1jb+acDO3J15FfUGicXoqp+kRbLZZyNHPvXPzALqw34d6uuTW94WwUt5S57X EaZpf6Il3oGTShcH4ABfHqZpqC9h+POitGMbyhi8A5qKiNAxuagfGmXCrxtUX5ZG0udd L8DOLP4yMMlgOB1eN+L2+TmYnYhq+3fn/SEizt2DixKMnJIvBfSLEaVCp0fQngbyf0Hz YwmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nt6RFseZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3-v6si8405683ply.192.2018.05.14.00.46.23; Mon, 14 May 2018 00:46:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nt6RFseZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752466AbeENGv0 (ORCPT + 99 others); Mon, 14 May 2018 02:51:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:58032 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752436AbeENGvZ (ORCPT ); Mon, 14 May 2018 02:51:25 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 06AED21746; Mon, 14 May 2018 06:51:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526280684; bh=X3BZWivzIgTtJ9zb/JYrqHs3oWUhRgQJTNT3K0woDI8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nt6RFseZm9U4KRLxCPXmjalJcjY/p2lIbSfrQkHD7MjBR3PE3WVDU7KLI6xe+HNCy q/8anQixDDcInXl5WsXeyyuM8VFVSk8brO+rIBbfFusPEp0z9Qc+NHjWFBU9kmVF04 0eabaXH/HWsm6kb7b+ZgRCbSEayf8ilsI4Lt0les= 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.4 19/56] xfs: prevent creating negative-sized file via INSERT_RANGE Date: Mon, 14 May 2018 08:48:24 +0200 Message-Id: <20180514064756.810779737@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180514064754.853201981@linuxfoundation.org> References: <20180514064754.853201981@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-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 @@ -969,22 +969,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; }