From patchwork Thu Oct 19 13:58:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 10017093 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 68128600CC for ; Thu, 19 Oct 2017 13:59:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F8C628C5D for ; Thu, 19 Oct 2017 13:59:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 51D6828D3B; Thu, 19 Oct 2017 13:59:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.4 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F250128C5D for ; Thu, 19 Oct 2017 13:59:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754890AbdJSN6q (ORCPT ); Thu, 19 Oct 2017 09:58:46 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:53069 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754880AbdJSN6o (ORCPT ); Thu, 19 Oct 2017 09:58:44 -0400 Received: by mail-wm0-f66.google.com with SMTP id k4so16617813wmc.1 for ; Thu, 19 Oct 2017 06:58:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tdmpcVR4YJn7YusKFEsP5j53NTEC+8CqxXKvJXm8gsw=; b=Rq1o5b4cSlD7IUMklnKm4U3ugh6jLFsdr87BT/xW2KrggVzZ/ovWtVkf8as48FALUn /vn0f5sRUQQopksGwtBb611qfCaD1K97RHYuDNsM/cJkd30ANC/VxS+CJ9/fWOc9UNwH SVsmB/gXdkjr8PkhuFDY3fXZBwilohPC9AFwALyZ5V+PI58xHEZIT8vyobO6RLzCWWBE S7lrGaYqGUfuzW5Rpn2p1V+p2v5XMYmsIJcDcFAJk9sWh72bzG/hC8Z2Xdf0CyUIpjwy bXl19C9A+pR0qM0JWgP8iyYnspIL97XIhxu0pc5SsNfAbTie1Cwt4oEKCFZROkW9XmM7 hvtg== X-Gm-Message-State: AMCzsaUlpGbPfkYdbspmcNSX1jcUt2umvf0PSTyn3gFSgZcz/ZE0Xe0t n3Ujb4NivDPM6hziloyUCl1rB/lGyiY= X-Google-Smtp-Source: ABhQp+QVg7//9Pm6BmZjhlWmFwC/3DgE13A+ZLKtip1v5iR2cMQN6/CPO0HNSAzfw7Aznyrlq0BnqA== X-Received: by 10.28.31.76 with SMTP id f73mr1887003wmf.139.1508421523528; Thu, 19 Oct 2017 06:58:43 -0700 (PDT) Received: from veci.piliscsaba.szeredi.hu (catv-176-63-54-97.catv.broadband.hu. [176.63.54.97]) by smtp.gmail.com with ESMTPSA id h185sm3613279wma.19.2017.10.19.06.58.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Oct 2017 06:58:43 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Jan Kara , Amir Goldstein , Xiong Zhou , linux-kernel@vger.kernel.org Subject: [PATCH 3/4] fanotify: fix fsnotify_prepare_user_wait() failure Date: Thu, 19 Oct 2017 15:58:36 +0200 Message-Id: <1508421517-22678-4-git-send-email-mszeredi@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1508421517-22678-1-git-send-email-mszeredi@redhat.com> References: <1508421517-22678-1-git-send-email-mszeredi@redhat.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. 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 + 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; }