WARNING: bad usercopy in fanotify_read

48 views
Skip to first unread message

syzbot

unread,
Mar 11, 2019, 4:42:07ā€ÆPM3/11/19
to ak...@linux-foundation.org, c...@lca.pw, crec...@redhat.com, kees...@chromium.org, linux-...@vger.kernel.org, linu...@kvack.org, syzkall...@googlegroups.com
Hello,

syzbot found the following crash on:

HEAD commit: 12ad143e Merge branch 'perf-urgent-for-linus' of git://git..
git tree: upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=12776f57200000
kernel config: https://syzkaller.appspot.com/x/.config?x=e9d91b7192a5e96e
dashboard link: https://syzkaller.appspot.com/bug?extid=2c49971e251e36216d1f
compiler: gcc (GCC) 9.0.0 20181231 (experimental)
userspace arch: amd64
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1287516f200000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17ee410b200000

IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by: syzbot+2c4997...@syzkaller.appspotmail.com

------------[ cut here ]------------
Bad or missing usercopy whitelist? Kernel memory exposure attempt detected
from SLAB object 'fanotify_event' (offset 40, size 8)!
WARNING: CPU: 1 PID: 7649 at mm/usercopy.c:78 usercopy_warn+0xeb/0x110
mm/usercopy.c:78
Kernel panic - not syncing: panic_on_warn set ...
CPU: 1 PID: 7649 Comm: syz-executor381 Not tainted 5.0.0+ #17
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS
Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x172/0x1f0 lib/dump_stack.c:113
panic+0x2cb/0x65c kernel/panic.c:214
__warn.cold+0x20/0x45 kernel/panic.c:571
report_bug+0x263/0x2b0 lib/bug.c:186
fixup_bug arch/x86/kernel/traps.c:179 [inline]
fixup_bug arch/x86/kernel/traps.c:174 [inline]
do_error_trap+0x11b/0x200 arch/x86/kernel/traps.c:272
do_invalid_op+0x37/0x50 arch/x86/kernel/traps.c:291
invalid_op+0x14/0x20 arch/x86/entry/entry_64.S:973
RIP: 0010:usercopy_warn+0xeb/0x110 mm/usercopy.c:78
Code: c8 e8 d9 88 c0 ff 4c 8b 45 c0 4d 89 e9 4c 89 e1 48 8b 55 c8 41 57 48
89 de 48 c7 c7 e0 dc 74 87 ff 75 d0 41 56 e8 03 4b 93 ff <0f> 0b 48 83 c4
18 e9 46 ff ff ff 49 c7 c5 e0 da 74 87 4d 89 ee 4d
RSP: 0018:ffff8880a417fb18 EFLAGS: 00010282
RAX: 0000000000000000 RBX: ffffffff8774dca0 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff815ad7b6 RDI: ffffed101482ff55
RBP: ffff8880a417fb70 R08: ffff888088d78580 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000000 R12: ffffffff8859408d
R13: ffffffff8775d500 R14: ffffffff8774db20 R15: 0000000000000008
__check_heap_object+0x88/0xb3 mm/slab.c:4453
check_heap_object mm/usercopy.c:238 [inline]
__check_object_size mm/usercopy.c:284 [inline]
__check_object_size+0x342/0x42f mm/usercopy.c:254
check_object_size include/linux/thread_info.h:119 [inline]
check_copy_size include/linux/thread_info.h:150 [inline]
copy_to_user include/linux/uaccess.h:151 [inline]
copy_fid_to_user fs/notify/fanotify/fanotify_user.c:236 [inline]
copy_event_to_user fs/notify/fanotify/fanotify_user.c:294 [inline]
fanotify_read+0xde0/0x1430 fs/notify/fanotify/fanotify_user.c:362
__vfs_read+0x8d/0x110 fs/read_write.c:416
vfs_read+0x194/0x3e0 fs/read_write.c:452
ksys_read+0xea/0x1f0 fs/read_write.c:578
__do_sys_read fs/read_write.c:588 [inline]
__se_sys_read fs/read_write.c:586 [inline]
__x64_sys_read+0x73/0xb0 fs/read_write.c:586
do_syscall_64+0x103/0x610 arch/x86/entry/common.c:290
entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x4456b9
Code: e8 6c b6 02 00 48 83 c4 18 c3 0f 1f 80 00 00 00 00 48 89 f8 48 89 f7
48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff
ff 0f 83 2b 12 fc ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007fb296f31db8 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
RAX: ffffffffffffffda RBX: 00000000006dac28 RCX: 00000000004456b9
RDX: 000000000000006b RSI: 0000000020000000 RDI: 0000000000000004
RBP: 00000000006dac20 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 00000000006dac2c
R13: 00007ffd8eb3d16f R14: 00007fb296f329c0 R15: 20c49ba5e353f7cf
Kernel Offset: disabled
Rebooting in 86400 seconds..


---
This bug is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzk...@googlegroups.com.

syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#bug-status-tracking for how to communicate with
syzbot.
syzbot can test patches for this bug, for details see:
https://goo.gl/tpsmEJ#testing-patches

Kees Cook

unread,
Mar 13, 2019, 2:26:38ā€ÆAM3/13/19
to syzbot, Amir Goldstein, Jan Kara, Andrew Morton, c...@lca.pw, Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Mon, Mar 11, 2019 at 1:42 PM syzbot
<syzbot+2c4997...@syzkaller.appspotmail.com> wrote:
> C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17ee410b200000
> [...]
> ------------[ cut here ]------------
> Bad or missing usercopy whitelist? Kernel memory exposure attempt detected
> from SLAB object 'fanotify_event' (offset 40, size 8)!
> [...]
> copy_to_user include/linux/uaccess.h:151 [inline]
> copy_fid_to_user fs/notify/fanotify/fanotify_user.c:236 [inline]
> copy_event_to_user fs/notify/fanotify/fanotify_user.c:294 [inline]

Looks like this is the fh/ext_fh union in struct fanotify_fid, field
"fid" in struct fanotify_event. Given that "fid" is itself in a union
against a struct path, I think instead of a whitelist using
KMEM_CACHE_USERCOPY(), this should just use a bounce buffer to avoid
leaving a whitelist open for path or ext_fh exposure.

Maybe something like this (untested):

diff --git a/fs/notify/fanotify/fanotify_user.c
b/fs/notify/fanotify/fanotify_user.c
index 56992b32c6bb..b87da9580b3c 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -207,6 +207,7 @@ static int process_access_response(struct
fsnotify_group *group,
static int copy_fid_to_user(struct fanotify_event *event, char __user *buf)
{
struct fanotify_event_info_fid info = { };
+ unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh;
struct file_handle handle = { };
size_t fh_len = event->fh_len;
size_t len = fanotify_event_info_len(event);
@@ -233,7 +234,18 @@ static int copy_fid_to_user(struct fanotify_event
*event, char __user *buf)

buf += sizeof(handle);
len -= sizeof(handle);
- if (copy_to_user(buf, fanotify_event_fh(event), fh_len))
+
+ /*
+ * For an inline fh, copy through stack to exclude the copy from
+ * usercopy hardening protections.
+ */
+ fh = fanotify_event_fh(event);
+ if (fh_len <= sizeof(bounce)) {
+ memcpy(bounce, fh, fh_len);
+ fh = bounce;
+ }
+
+ if (copy_to_user(buf, fh, fh_len))
return -EFAULT;

/* Pad with 0's */


--
Kees Cook

Amir Goldstein

unread,
Mar 13, 2019, 2:42:36ā€ÆAM3/13/19
to Kees Cook, syzbot, Jan Kara, Andrew Morton, c...@lca.pw, Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 8:26 AM Kees Cook <kees...@chromium.org> wrote:
>
> On Mon, Mar 11, 2019 at 1:42 PM syzbot
> <syzbot+2c4997...@syzkaller.appspotmail.com> wrote:
> > C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17ee410b200000
> > [...]
> > ------------[ cut here ]------------
> > Bad or missing usercopy whitelist? Kernel memory exposure attempt detected
> > from SLAB object 'fanotify_event' (offset 40, size 8)!
> > [...]
> > copy_to_user include/linux/uaccess.h:151 [inline]
> > copy_fid_to_user fs/notify/fanotify/fanotify_user.c:236 [inline]
> > copy_event_to_user fs/notify/fanotify/fanotify_user.c:294 [inline]
>
> Looks like this is the fh/ext_fh union in struct fanotify_fid, field
> "fid" in struct fanotify_event. Given that "fid" is itself in a union
> against a struct path, I think instead of a whitelist using
> KMEM_CACHE_USERCOPY(), this should just use a bounce buffer to avoid
> leaving a whitelist open for path or ext_fh exposure.
>
> Maybe something like this (untested):

I tested. Patch is fine by me with minor nit.
You may add:
Reviewed-by: Amir Goldstein <amir...@gmail.com>
Prefer <= FANOTIFY_INLINE_FH_LEN

Jan Kara

unread,
Mar 13, 2019, 10:35:05ā€ÆAM3/13/19
to Kees Cook, syzbot, Amir Goldstein, Jan Kara, Andrew Morton, c...@lca.pw, Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Tue 12-03-19 23:26:22, Kees Cook wrote:
> On Mon, Mar 11, 2019 at 1:42 PM syzbot
> <syzbot+2c4997...@syzkaller.appspotmail.com> wrote:
> > C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17ee410b200000
> > [...]
> > ------------[ cut here ]------------
> > Bad or missing usercopy whitelist? Kernel memory exposure attempt detected
> > from SLAB object 'fanotify_event' (offset 40, size 8)!
> > [...]
> > copy_to_user include/linux/uaccess.h:151 [inline]
> > copy_fid_to_user fs/notify/fanotify/fanotify_user.c:236 [inline]
> > copy_event_to_user fs/notify/fanotify/fanotify_user.c:294 [inline]
>
> Looks like this is the fh/ext_fh union in struct fanotify_fid, field
> "fid" in struct fanotify_event. Given that "fid" is itself in a union
> against a struct path, I think instead of a whitelist using
> KMEM_CACHE_USERCOPY(), this should just use a bounce buffer to avoid
> leaving a whitelist open for path or ext_fh exposure.

Do you mean to protect it from a situation when some other code (i.e. not
copy_fid_to_user()) would be tricked into copying ext_fh containing slab
pointer to userspace?

Honza
--
Jan Kara <ja...@suse.com>
SUSE Labs, CR

Kees Cook

unread,
Mar 13, 2019, 11:35:47ā€ÆAM3/13/19
to Jan Kara, syzbot, Amir Goldstein, Andrew Morton, c...@lca.pw, Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <ja...@suse.cz> wrote:
> On Tue 12-03-19 23:26:22, Kees Cook wrote:
> > On Mon, Mar 11, 2019 at 1:42 PM syzbot
> > <syzbot+2c4997...@syzkaller.appspotmail.com> wrote:
> > > C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17ee410b200000
> > > [...]
> > > ------------[ cut here ]------------
> > > Bad or missing usercopy whitelist? Kernel memory exposure attempt detected
> > > from SLAB object 'fanotify_event' (offset 40, size 8)!
> > > [...]
> > > copy_to_user include/linux/uaccess.h:151 [inline]
> > > copy_fid_to_user fs/notify/fanotify/fanotify_user.c:236 [inline]
> > > copy_event_to_user fs/notify/fanotify/fanotify_user.c:294 [inline]
> >
> > Looks like this is the fh/ext_fh union in struct fanotify_fid, field
> > "fid" in struct fanotify_event. Given that "fid" is itself in a union
> > against a struct path, I think instead of a whitelist using
> > KMEM_CACHE_USERCOPY(), this should just use a bounce buffer to avoid
> > leaving a whitelist open for path or ext_fh exposure.
>
> Do you mean to protect it from a situation when some other code (i.e. not
> copy_fid_to_user()) would be tricked into copying ext_fh containing slab
> pointer to userspace?

Yes. That's the design around the usercopy hardening. The
"whitelisting" is either via code (with a bounce buffer, so only the
specific "expected" code path can copy it), with a
kmem_create_usercopy() range marking (generally best for areas that
are not unions or when bounce buffers would be too big/slow), or with
implicit whitelisting (via a constant copy size that cannot change at
run-time, like: copy_to_user(dst, src, 6)).

In this case, since there are multiple unions in place and
FANOTIFY_INLINE_FH_LEN is small, it seemed best to go with a bounce
buffer.

--
Kees Cook

Jan Kara

unread,
Mar 13, 2019, 11:47:14ā€ÆAM3/13/19
to Kees Cook, Jan Kara, syzbot, Amir Goldstein, Andrew Morton, c...@lca.pw, Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
OK, makes sense. I'll replace tha patch using kmem_create_usercopy() in my
tree with a variant you've suggested.

Honza

syzbot

unread,
Mar 16, 2019, 10:49:02ā€ÆAM3/16/19
to ak...@linux-foundation.org, amir...@gmail.com, c...@lca.pw, crec...@redhat.com, ja...@suse.cz, kees...@chromium.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linu...@kvack.org, syzkall...@googlegroups.com
syzbot has bisected this bug to:

commit a8b13aa20afb69161b5123b4f1acc7ea0a03d360
Author: Amir Goldstein <amir...@gmail.com>
Date: Thu Jan 10 17:04:36 2019 +0000

fanotify: enable FAN_REPORT_FID init flag

bisection log: https://syzkaller.appspot.com/x/bisect.txt?x=11e78d6f200000
start commit: a8b13aa2 fanotify: enable FAN_REPORT_FID init flag
git tree: upstream
final crash: https://syzkaller.appspot.com/x/report.txt?x=13e78d6f200000
console output: https://syzkaller.appspot.com/x/log.txt?x=15e78d6f200000
Reported-by: syzbot+2c4997...@syzkaller.appspotmail.com
Fixes: a8b13aa2 ("fanotify: enable FAN_REPORT_FID init flag")

Kees Cook

unread,
Mar 18, 2019, 2:27:28ā€ÆPM3/18/19
to Jan Kara, syzbot, Amir Goldstein, Andrew Morton, c...@lca.pw, Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
Thanks! If you're able to update the patch, it would be nice to include:

Reported-by: syzbot+2c4997...@syzkaller.appspotmail.com
Fixes: a8b13aa2 ("fanotify: enable FAN_REPORT_FID init flag")

Regardless, I'll flag the fix for syzbot:

#syz fix: fanotify: Allow copying of file handle to userspace

--
Kees Cook

Jan Kara

unread,
Mar 19, 2019, 4:32:28ā€ÆAM3/19/19
to Kees Cook, Jan Kara, syzbot, Amir Goldstein, Andrew Morton, c...@lca.pw, Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
Yeah, it's easy enough to amend the commit at this point. Done.
Reply all
Reply to author
Forward
0 new messages