* [PATCH] fs: remove accidental overflow during wraparound check
@ 2024-05-07 23:17 Justin Stitt
2024-05-08 0:10 ` Al Viro
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Justin Stitt @ 2024-05-07 23:17 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner, Jan Kara, Nathan Chancellor,
Bill Wendling
Cc: linux-fsdevel, linux-kernel, llvm, linux-hardening, Justin Stitt
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] <TASK>
[ 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] <TASK>
[ 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
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
I wonder, though, why isn't loff_t an unsigned type? We have plently of
checks to ensure they are positive:
if (offset < 0 || len <= 0)
return -EINVAL;
...
if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
... are there ABI concerns?
Here's the syzkaller reproducer:
r0 = openat(0xffffffffffffff9c, &(0x7f0000000040)='./file1\x00', 0x42, 0x0)
fallocate(r0, 0x10, 0x7fffffffffffffff, 0x2000807fffff7)
... which was used against Kees' tree here (v6.8rc2):
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=wip/v6.9-rc2/unsigned-overflow-sanitizer
... with this config:
https://gist.github.com/JustinStitt/824976568b0f228ccbcbe49f3dee9bf4
---
fs/open.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/fs/open.c b/fs/open.c
index ee8460c83c77..d216e69d6872 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -247,6 +247,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;
@@ -319,8 +320,12 @@ 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;
+
+ /* Now, check bounds */
+ if (sum > inode->i_sb->s_maxbytes || sum < 0)
return -EFBIG;
if (!file->f_op->fallocate)
---
base-commit: 0106679839f7c69632b3b9833c3268c316c0a9fc
change-id: 20240507-b4-sio-vfs_fallocate-7b5223ba3a81
Best regards,
--
Justin Stitt <justinstitt@google.com>
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] fs: remove accidental overflow during wraparound check
2024-05-07 23:17 [PATCH] fs: remove accidental overflow during wraparound check Justin Stitt
@ 2024-05-08 0:10 ` Al Viro
2024-05-08 0:11 ` Kees Cook
2024-05-09 15:53 ` Jan Kara
2 siblings, 0 replies; 6+ messages in thread
From: Al Viro @ 2024-05-08 0:10 UTC (permalink / raw)
To: Justin Stitt
Cc: Christian Brauner, Jan Kara, Nathan Chancellor, Bill Wendling,
linux-fsdevel, linux-kernel, llvm, linux-hardening
On Tue, May 07, 2024 at 11:17:57PM +0000, Justin Stitt wrote:
> I wonder, though, why isn't loff_t an unsigned type?
Consider
lseek(fd, -10, SEEK_CUR)
PS: the above is *not* an endorsement of the proposed patch or
KASAN overflow nonsense in general.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] fs: remove accidental overflow during wraparound check
2024-05-07 23:17 [PATCH] fs: remove accidental overflow during wraparound check Justin Stitt
2024-05-08 0:10 ` Al Viro
@ 2024-05-08 0:11 ` Kees Cook
2024-05-09 15:53 ` Jan Kara
2 siblings, 0 replies; 6+ messages in thread
From: Kees Cook @ 2024-05-08 0:11 UTC (permalink / raw)
To: Justin Stitt
Cc: Alexander Viro, Christian Brauner, Jan Kara, Nathan Chancellor,
Bill Wendling, linux-fsdevel, linux-kernel, llvm, linux-hardening
On Tue, May 07, 2024 at 11:17:57PM +0000, Justin Stitt wrote:
> 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] <TASK>
> [ 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] <TASK>
> [ 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
> Signed-off-by: Justin Stitt <justinstitt@google.com>
I think this makes the checking more reading too. Thanks
Reviewed-by: Kees Cook <keescook@chromium.org>
--
Kees Cook
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] fs: remove accidental overflow during wraparound check
2024-05-07 23:17 [PATCH] fs: remove accidental overflow during wraparound check Justin Stitt
2024-05-08 0:10 ` Al Viro
2024-05-08 0:11 ` Kees Cook
@ 2024-05-09 15:53 ` Jan Kara
2024-05-09 22:10 ` Justin Stitt
2 siblings, 1 reply; 6+ messages in thread
From: Jan Kara @ 2024-05-09 15:53 UTC (permalink / raw)
To: Justin Stitt
Cc: Alexander Viro, Christian Brauner, Jan Kara, Nathan Chancellor,
Bill Wendling, linux-fsdevel, linux-kernel, llvm, linux-hardening
On Tue 07-05-24 23:17:57, Justin Stitt wrote:
> 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] <TASK>
> [ 195.428791] dump_stack_lvl+0x93/0xd0
> [ 195.431150] handle_overflow+0x171/0x1b0
> [ 195.433640] vfs_fallocate+0x459/0x4f0
Well, we compile the kernel with -fno-strict-overflow for a reason so I
wouldn't consider this a bug. But check_add_overflow() is easier to digest
since we don't have to worry about type details so I'm for this change.
> @@ -319,8 +320,12 @@ 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;
> +
> + /* Now, check bounds */
> + if (sum > inode->i_sb->s_maxbytes || sum < 0)
> return -EFBIG;
But why do you check for sum < 0? We know from previous checks offset >= 0
&& len > 0 so unless we overflow, sum is guaranteed to be > 0.
Honza
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] fs: remove accidental overflow during wraparound check
2024-05-09 15:53 ` Jan Kara
@ 2024-05-09 22:10 ` Justin Stitt
2024-05-12 8:05 ` Jan Kara
0 siblings, 1 reply; 6+ messages in thread
From: Justin Stitt @ 2024-05-09 22:10 UTC (permalink / raw)
To: Jan Kara
Cc: Alexander Viro, Christian Brauner, Nathan Chancellor,
Bill Wendling, linux-fsdevel, linux-kernel, llvm, linux-hardening
On Thu, May 9, 2024 at 8:53 AM Jan Kara <jack@suse.cz> wrote:
> > @@ -319,8 +320,12 @@ 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;
> > +
> > + /* Now, check bounds */
> > + if (sum > inode->i_sb->s_maxbytes || sum < 0)
> > return -EFBIG;
>
> But why do you check for sum < 0? We know from previous checks offset >= 0
> && len > 0 so unless we overflow, sum is guaranteed to be > 0.
Fair enough. I suppose with the overflow check in place we can no
longer have a sum less than zero there. If nothing else, it tells
readers of this code what the domain of (offset+len) is. I don't mind
sending a new version, though.
>
> Honza
> --
> Jan Kara <jack@suse.com>
> SUSE Labs, CR
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] fs: remove accidental overflow during wraparound check
2024-05-09 22:10 ` Justin Stitt
@ 2024-05-12 8:05 ` Jan Kara
0 siblings, 0 replies; 6+ messages in thread
From: Jan Kara @ 2024-05-12 8:05 UTC (permalink / raw)
To: Justin Stitt
Cc: Jan Kara, Alexander Viro, Christian Brauner, Nathan Chancellor,
Bill Wendling, linux-fsdevel, linux-kernel, llvm, linux-hardening
On Thu 09-05-24 15:10:07, Justin Stitt wrote:
> On Thu, May 9, 2024 at 8:53 AM Jan Kara <jack@suse.cz> wrote:
> > > @@ -319,8 +320,12 @@ 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;
> > > +
> > > + /* Now, check bounds */
> > > + if (sum > inode->i_sb->s_maxbytes || sum < 0)
> > > return -EFBIG;
> >
> > But why do you check for sum < 0? We know from previous checks offset >= 0
> > && len > 0 so unless we overflow, sum is guaranteed to be > 0.
>
> Fair enough. I suppose with the overflow check in place we can no
> longer have a sum less than zero there. If nothing else, it tells
> readers of this code what the domain of (offset+len) is. I don't mind
> sending a new version, though.
Well, for normal readers offset+len is always a positive number. That's
what you expect. If you see a check for offset+len < 0, you start wondering
what are you missing... only to find you miss nothing and the check is
pointless. So yes, please send a version without the pointless check.
Honza
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-05-13 13:41 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-07 23:17 [PATCH] fs: remove accidental overflow during wraparound check Justin Stitt
2024-05-08 0:10 ` Al Viro
2024-05-08 0:11 ` Kees Cook
2024-05-09 15:53 ` Jan Kara
2024-05-09 22:10 ` Justin Stitt
2024-05-12 8:05 ` Jan Kara
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).