From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de ([195.135.220.15]:41648 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727339AbeLEMJY (ORCPT ); Wed, 5 Dec 2018 07:09:24 -0500 Date: Wed, 5 Dec 2018 13:09:21 +0100 From: Jan Kara To: Kees Cook Cc: Jan Kara , Heinrich Schuchardt , Amir Goldstein , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] fanotify: Make sure to check event_len when copying Message-ID: <20181205120921.GA30615@quack2.suse.cz> References: <20181204234446.GA4676@beast> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181204234446.GA4676@beast> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: 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 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 SUSE Labs, CR