* [PATCH] fanotify: Make sure to check event_len when copying
@ 2018-12-04 23:44 Kees Cook
2018-12-05 7:15 ` Amir Goldstein
2018-12-05 12:09 ` Jan Kara
0 siblings, 2 replies; 4+ messages in thread
From: Kees Cook @ 2018-12-04 23:44 UTC (permalink / raw)
To: Jan Kara; +Cc: Heinrich Schuchardt, Amir Goldstein, linux-fsdevel, linux-kernel
As a precaution, make sure we check event_len when copying to userspace.
Based on old feedback: https://lkml.kernel.org/r/542D9FE5.3010009@gmx.de
Signed-off-by: Kees Cook <keescook@chromium.org>
---
fs/notify/fanotify/fanotify_user.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index e03be5071362..d9484a0ac6b3 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -206,7 +206,7 @@ static int process_access_response(struct fsnotify_group *group,
static ssize_t copy_event_to_user(struct fsnotify_group *group,
struct fsnotify_event *event,
- char __user *buf)
+ char __user *buf, size_t count)
{
struct fanotify_event_metadata fanotify_event_metadata;
struct file *f;
@@ -220,6 +220,12 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
fd = fanotify_event_metadata.fd;
ret = -EFAULT;
+ /*
+ * Sanity check copy size in case get_one_event() and
+ * fill_event_metadata() event_len sizes ever get out of sync.
+ */
+ if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count))
+ goto out_close_fd;
if (copy_to_user(buf, &fanotify_event_metadata,
fanotify_event_metadata.event_len))
goto out_close_fd;
@@ -295,7 +301,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
continue;
}
- ret = copy_event_to_user(group, kevent, buf);
+ ret = copy_event_to_user(group, kevent, buf, count);
if (unlikely(ret == -EOPENSTALE)) {
/*
* We cannot report events with stale fd so drop it.
--
2.17.1
--
Kees Cook
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] fanotify: Make sure to check event_len when copying
2018-12-04 23:44 [PATCH] fanotify: Make sure to check event_len when copying Kees Cook
@ 2018-12-05 7:15 ` Amir Goldstein
2018-12-05 12:10 ` Jan Kara
2018-12-05 12:09 ` Jan Kara
1 sibling, 1 reply; 4+ messages in thread
From: Amir Goldstein @ 2018-12-05 7:15 UTC (permalink / raw)
To: keescook; +Cc: Jan Kara, xypron.glpk, linux-fsdevel, linux-kernel
On Wed, Dec 5, 2018 at 1:44 AM Kees Cook <keescook@chromium.org> wrote:
>
> As a precaution, make sure we check event_len when copying to userspace.
> Based on old feedback: https://lkml.kernel.org/r/542D9FE5.3010009@gmx.de
>
This precaution serves the new fanotify_event_info_fid format well:
https://marc.info/?l=linux-fsdevel&m=154375073318106&w=2
but also conflicts with my series.
I can weave your patch into my series and add a similar sanity check in
copy_fid_to_user() or if Jan applies your patch and has more comments
on my v4 series, I'll rebase on top of that.
Thanks,
Amir.
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> fs/notify/fanotify/fanotify_user.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
> index e03be5071362..d9484a0ac6b3 100644
> --- a/fs/notify/fanotify/fanotify_user.c
> +++ b/fs/notify/fanotify/fanotify_user.c
> @@ -206,7 +206,7 @@ static int process_access_response(struct fsnotify_group *group,
>
> static ssize_t copy_event_to_user(struct fsnotify_group *group,
> struct fsnotify_event *event,
> - char __user *buf)
> + char __user *buf, size_t count)
> {
> struct fanotify_event_metadata fanotify_event_metadata;
> struct file *f;
> @@ -220,6 +220,12 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
>
> fd = fanotify_event_metadata.fd;
> ret = -EFAULT;
> + /*
> + * Sanity check copy size in case get_one_event() and
> + * fill_event_metadata() event_len sizes ever get out of sync.
> + */
> + if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count))
> + goto out_close_fd;
> if (copy_to_user(buf, &fanotify_event_metadata,
> fanotify_event_metadata.event_len))
> goto out_close_fd;
> @@ -295,7 +301,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
> continue;
> }
>
> - ret = copy_event_to_user(group, kevent, buf);
> + ret = copy_event_to_user(group, kevent, buf, count);
> if (unlikely(ret == -EOPENSTALE)) {
> /*
> * We cannot report events with stale fd so drop it.
> --
> 2.17.1
>
>
> --
> Kees Cook
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] fanotify: Make sure to check event_len when copying
2018-12-04 23:44 [PATCH] fanotify: Make sure to check event_len when copying Kees Cook
2018-12-05 7:15 ` Amir Goldstein
@ 2018-12-05 12:09 ` Jan Kara
1 sibling, 0 replies; 4+ messages in thread
From: Jan Kara @ 2018-12-05 12:09 UTC (permalink / raw)
To: Kees Cook
Cc: Jan Kara, Heinrich Schuchardt, Amir Goldstein, linux-fsdevel,
linux-kernel
On Tue 04-12-18 15:44:46, Kees Cook wrote:
> As a precaution, make sure we check event_len when copying to userspace.
> Based on old feedback: https://lkml.kernel.org/r/542D9FE5.3010009@gmx.de
>
> Signed-off-by: Kees Cook <keescook@chromium.org>
Thanks for the patch, I've added it to my tree.
Honza
> ---
> fs/notify/fanotify/fanotify_user.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
> index e03be5071362..d9484a0ac6b3 100644
> --- a/fs/notify/fanotify/fanotify_user.c
> +++ b/fs/notify/fanotify/fanotify_user.c
> @@ -206,7 +206,7 @@ static int process_access_response(struct fsnotify_group *group,
>
> static ssize_t copy_event_to_user(struct fsnotify_group *group,
> struct fsnotify_event *event,
> - char __user *buf)
> + char __user *buf, size_t count)
> {
> struct fanotify_event_metadata fanotify_event_metadata;
> struct file *f;
> @@ -220,6 +220,12 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
>
> fd = fanotify_event_metadata.fd;
> ret = -EFAULT;
> + /*
> + * Sanity check copy size in case get_one_event() and
> + * fill_event_metadata() event_len sizes ever get out of sync.
> + */
> + if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count))
> + goto out_close_fd;
> if (copy_to_user(buf, &fanotify_event_metadata,
> fanotify_event_metadata.event_len))
> goto out_close_fd;
> @@ -295,7 +301,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
> continue;
> }
>
> - ret = copy_event_to_user(group, kevent, buf);
> + ret = copy_event_to_user(group, kevent, buf, count);
> if (unlikely(ret == -EOPENSTALE)) {
> /*
> * We cannot report events with stale fd so drop it.
> --
> 2.17.1
>
>
> --
> Kees Cook
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] fanotify: Make sure to check event_len when copying
2018-12-05 7:15 ` Amir Goldstein
@ 2018-12-05 12:10 ` Jan Kara
0 siblings, 0 replies; 4+ messages in thread
From: Jan Kara @ 2018-12-05 12:10 UTC (permalink / raw)
To: Amir Goldstein
Cc: keescook, Jan Kara, xypron.glpk, linux-fsdevel, linux-kernel
On Wed 05-12-18 09:15:44, Amir Goldstein wrote:
> On Wed, Dec 5, 2018 at 1:44 AM Kees Cook <keescook@chromium.org> wrote:
> >
> > As a precaution, make sure we check event_len when copying to userspace.
> > Based on old feedback: https://lkml.kernel.org/r/542D9FE5.3010009@gmx.de
> >
>
> This precaution serves the new fanotify_event_info_fid format well:
> https://marc.info/?l=linux-fsdevel&m=154375073318106&w=2
> but also conflicts with my series.
>
> I can weave your patch into my series and add a similar sanity check in
> copy_fid_to_user() or if Jan applies your patch and has more comments
> on my v4 series, I'll rebase on top of that.
Yeah, I don't think there's any need for you to rebase just now. Either
I'll fixup your patch or you'll do the rebase if there are further changes
to the series...
Honza
>
> Thanks,
> Amir.
>
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> > fs/notify/fanotify/fanotify_user.c | 10 ++++++++--
> > 1 file changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
> > index e03be5071362..d9484a0ac6b3 100644
> > --- a/fs/notify/fanotify/fanotify_user.c
> > +++ b/fs/notify/fanotify/fanotify_user.c
> > @@ -206,7 +206,7 @@ static int process_access_response(struct fsnotify_group *group,
> >
> > static ssize_t copy_event_to_user(struct fsnotify_group *group,
> > struct fsnotify_event *event,
> > - char __user *buf)
> > + char __user *buf, size_t count)
> > {
> > struct fanotify_event_metadata fanotify_event_metadata;
> > struct file *f;
> > @@ -220,6 +220,12 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
> >
> > fd = fanotify_event_metadata.fd;
> > ret = -EFAULT;
> > + /*
> > + * Sanity check copy size in case get_one_event() and
> > + * fill_event_metadata() event_len sizes ever get out of sync.
> > + */
> > + if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count))
> > + goto out_close_fd;
> > if (copy_to_user(buf, &fanotify_event_metadata,
> > fanotify_event_metadata.event_len))
> > goto out_close_fd;
> > @@ -295,7 +301,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
> > continue;
> > }
> >
> > - ret = copy_event_to_user(group, kevent, buf);
> > + ret = copy_event_to_user(group, kevent, buf, count);
> > if (unlikely(ret == -EOPENSTALE)) {
> > /*
> > * We cannot report events with stale fd so drop it.
> > --
> > 2.17.1
> >
> >
> > --
> > Kees Cook
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-12-05 12:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-04 23:44 [PATCH] fanotify: Make sure to check event_len when copying Kees Cook
2018-12-05 7:15 ` Amir Goldstein
2018-12-05 12:10 ` Jan Kara
2018-12-05 12:09 ` 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).