From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751127AbdJTMZT (ORCPT ); Fri, 20 Oct 2017 08:25:19 -0400 Received: from mail-yw0-f194.google.com ([209.85.161.194]:51263 "EHLO mail-yw0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750964AbdJTMZS (ORCPT ); Fri, 20 Oct 2017 08:25:18 -0400 X-Google-Smtp-Source: ABhQp+TPqSD0dObgjv3XOJ4KDrzeNOzWdTNDRLQszM3J8XhdMxAS29TKDVJ6LuKgyb1w2h5kp26KOy9tW2KqQq6kP+U= MIME-Version: 1.0 In-Reply-To: <1508421517-22678-4-git-send-email-mszeredi@redhat.com> References: <1508421517-22678-1-git-send-email-mszeredi@redhat.com> <1508421517-22678-4-git-send-email-mszeredi@redhat.com> From: Amir Goldstein Date: Fri, 20 Oct 2017 15:25:16 +0300 Message-ID: Subject: Re: [PATCH 3/4] fanotify: fix fsnotify_prepare_user_wait() failure To: Miklos Szeredi Cc: linux-fsdevel , Jan Kara , Xiong Zhou , linux-kernel Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 19, 2017 at 4:58 PM, Miklos Szeredi wrote: > If fsnotify_prepare_user_wait() fails, we leave the event on the > notification list. Which will result in a warning in > fsnotify_destroy_event() and later use-after-free. > > Instead of adding a new helper to remove the event from the list in this > case, I opted to move the prepare/finish up into fanotify_handle_event(). > > This will allow these to be moved further out into the generic code later, > and perhaps let us move to non-sleeping RCU. Interesting. Because all marks are ordered by group priority and there are no permission events in priority 0, as soon as we see group prio 0 on both inode and vfs mark, we can maybe change the mark iteration? > > Signed-off-by: Miklos Szeredi > Fixes: 05f0e38724e8 ("fanotify: Release SRCU lock when waiting for userspace response") > Cc: # v4.12 > --- > fs/notify/fanotify/fanotify.c | 33 ++++++++++++++++++++------------- > 1 file changed, 20 insertions(+), 13 deletions(-) > > diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c > index 2fa99aeaa095..fb7a1339982c 100644 > --- a/fs/notify/fanotify/fanotify.c > +++ b/fs/notify/fanotify/fanotify.c > @@ -64,19 +64,8 @@ static int fanotify_get_response(struct fsnotify_group *group, > > pr_debug("%s: group=%p event=%p\n", __func__, group, event); > > - /* > - * fsnotify_prepare_user_wait() fails if we race with mark deletion. > - * Just let the operation pass in that case. > - */ > - if (!fsnotify_prepare_user_wait(iter_info)) { > - event->response = FAN_ALLOW; > - goto out; > - } > - > wait_event(group->fanotify_data.access_waitq, event->response); > > - fsnotify_finish_user_wait(iter_info); > -out: > /* userspace responded, convert to something usable */ > switch (event->response) { > case FAN_ALLOW: > @@ -211,9 +200,21 @@ static int fanotify_handle_event(struct fsnotify_group *group, > pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, > mask); > > +#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS > + if (mask & FAN_ALL_PERM_EVENTS) { > + /* > + * fsnotify_prepare_user_wait() fails if we race with mark deletion. > + * Just let the operation pass in that case. > + */ > + if (!fsnotify_prepare_user_wait(iter_info)) > + return 0; > + } > +#endif Well you are not the one to introduce ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS to this code, but I have to wonder, what are all those ifdefs doing in this code? What are they trying to save? FAN_ALL_PERM_EVENTS is masked out from valid event flags in fanotify_mark() I don't really see a reason for any other ifdef in non headers here. > + > event = fanotify_alloc_event(inode, mask, data); > + ret = -ENOMEM; > if (unlikely(!event)) > - return -ENOMEM; > + goto finish; > > fsn_event = &event->fse; > ret = fsnotify_add_event(group, fsn_event, fanotify_merge); > @@ -223,7 +224,8 @@ static int fanotify_handle_event(struct fsnotify_group *group, > /* Our event wasn't used in the end. Free it. */ > fsnotify_destroy_event(group, fsn_event); > > - return 0; > + ret = 0; > + goto finish; > } > > #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS > @@ -232,6 +234,11 @@ static int fanotify_handle_event(struct fsnotify_group *group, > iter_info); > fsnotify_destroy_event(group, fsn_event); > } > +finish: > + if (mask & FAN_ALL_PERM_EVENTS) > + fsnotify_finish_user_wait(iter_info); > +#else > +finish: > #endif > return ret; > } > -- > 2.5.5 >