Re: WARNING: bad usercopy in fanotify_read

From: Jan Kara
Date: Tue Mar 19 2019 - 04:32:30 EST


On Mon 18-03-19 11:27:12, Kees Cook wrote:
> On Wed, Mar 13, 2019 at 8:47 AM Jan Kara <jack@xxxxxxx> wrote:
> >
> > On Wed 13-03-19 08:35:33, Kees Cook wrote:
> > > On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <jack@xxxxxxx> wrote:
> > > > On Tue 12-03-19 23:26:22, Kees Cook wrote:
> > > > > On Mon, Mar 11, 2019 at 1:42 PM syzbot
> > > > > <syzbot+2c49971e251e36216d1f@xxxxxxxxxxxxxxxxxxxxxxxxx> 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.
> >
> > OK, makes sense. I'll replace tha patch using kmem_create_usercopy() in my
> > tree with a variant you've suggested.
>
> Thanks! If you're able to update the patch, it would be nice to include:
>
> Reported-by: syzbot+2c49971e251e36216d1f@xxxxxxxxxxxxxxxxxxxxxxxxx
> Fixes: a8b13aa2 ("fanotify: enable FAN_REPORT_FID init flag")

Yeah, it's easy enough to amend the commit at this point. Done.

Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR