From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 360C482490; Sun, 28 Jul 2024 00:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722127715; cv=none; b=YnuxQw7iMACMP6Ee/9BbLTXlnYjikHn2uZD4ygbP2fUmJ5T5dB7Z83SOlzNGjag5p5LwHZ6a6JZyf9KPjl5GnFvvcfizcw0BBiGeVoYUaU3cOeH925yZzTvPq6NuM9XdYKlQjvtyoCI0dsxxIFRJbdrulUsi8AKgOacExE99UEA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722127715; c=relaxed/simple; bh=wpFTFCkAiw+MrSe0s3jP4O4FSRpIAlZ6VyiF+BPLdbA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=rI5+aYF6is0ZMiB3PveAgl2TZUTgRjCliGZ0tOhRxRCInNhjq5cnU4WsE/QvAH7zycl9H8ddo1gETYT0/ilXIl0jiNz7FDhwrad0PFUEtUS/5BJ8Q6vD/BlkepJE3ruPJRQ1kqYd9HKrbzT9YROtIl20ChY3r2KcwHPH+Z4kmrg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sVqzgrZU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sVqzgrZU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78AE0C32781; Sun, 28 Jul 2024 00:48:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722127715; bh=wpFTFCkAiw+MrSe0s3jP4O4FSRpIAlZ6VyiF+BPLdbA=; h=From:To:Cc:Subject:Date:From; b=sVqzgrZUIPKBHN92dC9ZKlMw48bqs6xVuwl5HmifxTS5EktpbBil8+edXGddpXU8A fW4JcmwWGohvCtx0bj5bLxATPpj2VpAYY42l3Q6oaoeAP87hix/DiTo9efRx7XLKoa FgYNEexdsRl3oMTtcptb/ceQLCoxjo3WtKrpxnKgHUzHZW+QASjyWEwCgEyWTOSKNZ xt+oYj8IwghEt6KzNjlArmjOv6rtEy0zUYNXZx8pImwMvktpeDRQqqzOdsxWnBIlH6 +FFWq1hFoltdN71qFaXjMVA0SKFnExhsLlMlCmvzof3kh62LgTRE3VoFVvbX0lPLin zviytcK5hHPZA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Justin Stitt , linux-hardening@vger.kernel.org, Kees Cook , Jan Kara , Christian Brauner , Sasha Levin , viro@zeniv.linux.org.uk, nathan@kernel.org, linux-fsdevel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH AUTOSEL 6.1 1/8] fs: remove accidental overflow during wraparound check Date: Sat, 27 Jul 2024 20:48:23 -0400 Message-ID: <20240728004831.1702511-1-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.102 Content-Transfer-Encoding: 8bit From: Justin Stitt [ Upstream commit 23cc6ef6fd453b13502caae23130844e7d6ed0fe ] Running syzkaller with the newly enabled signed integer overflow sanitizer produces this report: [ 195.401651] ------------[ cut here ]------------ [ 195.404808] UBSAN: signed-integer-overflow in ../fs/open.c:321:15 [ 195.408739] 9223372036854775807 + 562984447377399 cannot be represented in type 'loff_t' (aka 'long long') [ 195.414683] CPU: 1 PID: 703 Comm: syz-executor.0 Not tainted 6.8.0-rc2-00039-g14de58dbe653-dirty #11 [ 195.420138] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 195.425804] Call Trace: [ 195.427360] [ 195.428791] dump_stack_lvl+0x93/0xd0 [ 195.431150] handle_overflow+0x171/0x1b0 [ 195.433640] vfs_fallocate+0x459/0x4f0 ... [ 195.490053] ------------[ cut here ]------------ [ 195.493146] UBSAN: signed-integer-overflow in ../fs/open.c:321:61 [ 195.497030] 9223372036854775807 + 562984447377399 cannot be represented in type 'loff_t' (aka 'long long) [ 195.502940] CPU: 1 PID: 703 Comm: syz-executor.0 Not tainted 6.8.0-rc2-00039-g14de58dbe653-dirty #11 [ 195.508395] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 195.514075] Call Trace: [ 195.515636] [ 195.517000] dump_stack_lvl+0x93/0xd0 [ 195.519255] handle_overflow+0x171/0x1b0 [ 195.521677] vfs_fallocate+0x4cb/0x4f0 [ 195.524033] __x64_sys_fallocate+0xb2/0xf0 Historically, the signed integer overflow sanitizer did not work in the kernel due to its interaction with `-fwrapv` but this has since been changed [1] in the newest version of Clang. It was re-enabled in the kernel with Commit 557f8c582a9ba8ab ("ubsan: Reintroduce signed overflow sanitizer"). Let's use the check_add_overflow helper to first verify the addition stays within the bounds of its type (long long); then we can use that sum for the following check. Link: https://github.com/llvm/llvm-project/pull/82432 [1] Closes: https://github.com/KSPP/linux/issues/356 Cc: linux-hardening@vger.kernel.org Reviewed-by: Kees Cook Signed-off-by: Justin Stitt Link: https://lore.kernel.org/r/20240513-b4-sio-vfs_fallocate-v2-1-db415872fb16@google.com Reviewed-by: Jan Kara Signed-off-by: Christian Brauner Signed-off-by: Sasha Levin --- fs/open.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fs/open.c b/fs/open.c index 0d63c94e1c5e6..dd68725cd7247 100644 --- a/fs/open.c +++ b/fs/open.c @@ -244,6 +244,7 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) { struct inode *inode = file_inode(file); long ret; + loff_t sum; if (offset < 0 || len <= 0) return -EINVAL; @@ -312,8 +313,11 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) return -ENODEV; - /* Check for wrap through zero too */ - if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0)) + /* Check for wraparound */ + if (check_add_overflow(offset, len, &sum)) + return -EFBIG; + + if (sum > inode->i_sb->s_maxbytes) return -EFBIG; if (!file->f_op->fallocate) -- 2.43.0