* Re: WARNING: bad usercopy in fanotify_read
@ 2019-03-13 6:26 ` Kees Cook
0 siblings, 0 replies; 15+ messages in thread
From: Kees Cook @ 2019-03-13 6:26 UTC (permalink / raw)
To: syzbot, Amir Goldstein, Jan Kara
Cc: Andrew Morton, cai, Chris von Recklinghausen, LKML, Linux-MM,
syzkaller-bugs
On Mon, Mar 11, 2019 at 1:42 PM syzbot
<syzbot+2c49971e251e36216d1f@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
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
2019-03-13 6:26 ` Kees Cook
@ 2019-03-13 6:42 ` Amir Goldstein
-1 siblings, 0 replies; 15+ messages in thread
From: Amir Goldstein @ 2019-03-13 6:42 UTC (permalink / raw)
To: Kees Cook
Cc: syzbot, Jan Kara, Andrew Morton, cai, Chris von Recklinghausen,
LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 8:26 AM Kees Cook <keescook@chromium.org> wrote:
>
> On Mon, Mar 11, 2019 at 1:42 PM syzbot
> <syzbot+2c49971e251e36216d1f@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 <amir73il@gmail.com>
>
> 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)) {
Prefer <= FANOTIFY_INLINE_FH_LEN
> + memcpy(bounce, fh, fh_len);
> + fh = bounce;
> + }
> +
> + if (copy_to_user(buf, fh, fh_len))
> return -EFAULT;
>
> /* Pad with 0's */
>
>
> --
> Kees Cook
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
@ 2019-03-13 6:42 ` Amir Goldstein
0 siblings, 0 replies; 15+ messages in thread
From: Amir Goldstein @ 2019-03-13 6:42 UTC (permalink / raw)
To: Kees Cook
Cc: syzbot, Jan Kara, Andrew Morton, cai, Chris von Recklinghausen,
LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 8:26 AM Kees Cook <keescook@chromium.org> wrote:
>
> On Mon, Mar 11, 2019 at 1:42 PM syzbot
> <syzbot+2c49971e251e36216d1f@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 <amir73il@gmail.com>
>
> 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)) {
Prefer <= FANOTIFY_INLINE_FH_LEN
> + memcpy(bounce, fh, fh_len);
> + fh = bounce;
> + }
> +
> + if (copy_to_user(buf, fh, fh_len))
> return -EFAULT;
>
> /* Pad with 0's */
>
>
> --
> Kees Cook
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
2019-03-13 6:26 ` Kees Cook
(?)
(?)
@ 2019-03-13 14:35 ` Jan Kara
2019-03-13 15:35 ` Kees Cook
-1 siblings, 1 reply; 15+ messages in thread
From: Jan Kara @ 2019-03-13 14:35 UTC (permalink / raw)
To: Kees Cook
Cc: syzbot, Amir Goldstein, Jan Kara, Andrew Morton, cai,
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+2c49971e251e36216d1f@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
>
> 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
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
2019-03-13 14:35 ` Jan Kara
@ 2019-03-13 15:35 ` Kees Cook
0 siblings, 0 replies; 15+ messages in thread
From: Kees Cook @ 2019-03-13 15:35 UTC (permalink / raw)
To: Jan Kara
Cc: syzbot, Amir Goldstein, Andrew Morton, cai,
Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <jack@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+2c49971e251e36216d1f@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
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
@ 2019-03-13 15:35 ` Kees Cook
0 siblings, 0 replies; 15+ messages in thread
From: Kees Cook @ 2019-03-13 15:35 UTC (permalink / raw)
To: Jan Kara
Cc: syzbot, Amir Goldstein, Andrew Morton, cai,
Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <jack@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+2c49971e251e36216d1f@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
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
2019-03-13 15:35 ` Kees Cook
(?)
@ 2019-03-13 15:47 ` Jan Kara
2019-03-18 18:27 ` Kees Cook
-1 siblings, 1 reply; 15+ messages in thread
From: Jan Kara @ 2019-03-13 15:47 UTC (permalink / raw)
To: Kees Cook
Cc: Jan Kara, syzbot, Amir Goldstein, Andrew Morton, cai,
Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Wed 13-03-19 08:35:33, Kees Cook wrote:
> On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <jack@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+2c49971e251e36216d1f@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.
OK, makes sense. I'll replace tha patch using kmem_create_usercopy() in my
tree with a variant you've suggested.
Honza
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
2019-03-13 15:47 ` Jan Kara
@ 2019-03-18 18:27 ` Kees Cook
0 siblings, 0 replies; 15+ messages in thread
From: Kees Cook @ 2019-03-18 18:27 UTC (permalink / raw)
To: Jan Kara
Cc: syzbot, Amir Goldstein, Andrew Morton, cai,
Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 8:47 AM Jan Kara <jack@suse.cz> wrote:
>
> On Wed 13-03-19 08:35:33, Kees Cook wrote:
> > On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <jack@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+2c49971e251e36216d1f@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.
>
> 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@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
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
@ 2019-03-18 18:27 ` Kees Cook
0 siblings, 0 replies; 15+ messages in thread
From: Kees Cook @ 2019-03-18 18:27 UTC (permalink / raw)
To: Jan Kara
Cc: syzbot, Amir Goldstein, Andrew Morton, cai,
Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Wed, Mar 13, 2019 at 8:47 AM Jan Kara <jack@suse.cz> wrote:
>
> On Wed 13-03-19 08:35:33, Kees Cook wrote:
> > On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <jack@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+2c49971e251e36216d1f@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.
>
> 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@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
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: WARNING: bad usercopy in fanotify_read
2019-03-18 18:27 ` Kees Cook
(?)
@ 2019-03-19 8:32 ` Jan Kara
-1 siblings, 0 replies; 15+ messages in thread
From: Jan Kara @ 2019-03-19 8:32 UTC (permalink / raw)
To: Kees Cook
Cc: Jan Kara, syzbot, Amir Goldstein, Andrew Morton, cai,
Chris von Recklinghausen, LKML, Linux-MM, syzkaller-bugs
On Mon 18-03-19 11:27:12, Kees Cook wrote:
> On Wed, Mar 13, 2019 at 8:47 AM Jan Kara <jack@suse.cz> wrote:
> >
> > On Wed 13-03-19 08:35:33, Kees Cook wrote:
> > > On Wed, Mar 13, 2019 at 7:35 AM Jan Kara <jack@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+2c49971e251e36216d1f@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.
> >
> > 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@syzkaller.appspotmail.com
> 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@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 15+ messages in thread