From patchwork Wed Oct 17 09:05:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Bobrowski X-Patchwork-Id: 10645141 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9CB75109C for ; Wed, 17 Oct 2018 09:05:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A4C12AB61 for ; Wed, 17 Oct 2018 09:05:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B20A2AC39; Wed, 17 Oct 2018 09:05:31 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham 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 B549A2ABC4 for ; Wed, 17 Oct 2018 09:05:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727257AbeJQRAN (ORCPT ); Wed, 17 Oct 2018 13:00:13 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:41769 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727040AbeJQRAN (ORCPT ); Wed, 17 Oct 2018 13:00:13 -0400 Received: by mail-pg1-f194.google.com with SMTP id 23-v6so12216522pgc.8 for ; Wed, 17 Oct 2018 02:05:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mbobrowski-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=DPkyJTDeO5eRnpNSoP0zYPSk/syJlOe3qWZ8sKZjxfU=; b=v06o/Aea57LKNx9o2gpyUEwtaqQU099EkedLAFYYB9SGQCid9O7LVsQ25TP7AYUuze 1Y014wJT8LtLZN8w4zklvgqt75cZUvQv3CuqY0y3dtHjF+mNisA0eTgrtHwq3rSVzxKH iuY4/XbVtqoALwgW6FSH3CnpCwRM8cQTerYFgfiH3Sn5uXXNhFAdyKQyouJ/F42CKRID JTkayTLAi0XsYa8WpPJ2cBnyfLh3LCni7UPrvimTxOxMgAsxACWa2esdE1W2fpdO06D1 FD4OfROq+P2q0j6IG4dfKFY95CB2YSWDN2LoZauOQMmCmoOAunHA6mu0f1haBGUk8LAA Bbmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=DPkyJTDeO5eRnpNSoP0zYPSk/syJlOe3qWZ8sKZjxfU=; b=cWYnIL1UhhcR4Vzr7sgLgXNkWZQzqN6csN6KMqIOUPHXIpobztXa6hNkB9eU/okLJr 6ekt6kvxyqk+WqZrgaIGLr1eIt5XFcPGLnEl7+rqlk5kVISEFmW29JBruSWCGZo3GV3J i0X29AtWIJm1TA+tM/S+ozq2Dw6ZeEzYQ8raurjRd7A6RsD+2C49huQm5ADABhVHEXhm w/kCv76joKQ51eEU4NDZBdMKqWILtrWofW2xvlDg8yW7pLlvGVr7PgcHFkLMC3W21aZr CkvZ0qCQENiGpdUll5mcnIpJ8fnFpDYV0pE6BkYpp5iQQJYW334Zeq9vy5xiWs/fC3ia az3Q== X-Gm-Message-State: ABuFfohxIKSqDnOZkzVlKV4cjHAZz65r8UOQLHHFf7Ai4cxUQba1cDvc UI1m83jBa0910C1Cs48IOaW1 X-Google-Smtp-Source: ACcGV60ohpLs4qx1ILxSMSZiXRDhAZOkEZU43HRlfrM91YTQe3uQHLbz4DUShc0u6evIvFVv36SGIQ== X-Received: by 2002:a62:a50d:: with SMTP id v13-v6mr25492225pfm.18.1539767128611; Wed, 17 Oct 2018 02:05:28 -0700 (PDT) Received: from development.internal.lab (n114-74-18-206.sbr2.nsw.optusnet.com.au. [114.74.18.206]) by smtp.gmail.com with ESMTPSA id j25-v6sm19507407pff.116.2018.10.17.02.05.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 17 Oct 2018 02:05:28 -0700 (PDT) Date: Wed, 17 Oct 2018 20:05:22 +1100 From: Matthew Bobrowski To: jack@suse.cz Cc: amir73il@gmail.com, linux-api@vger.kernel.org, sgrubb@redhat.com, linux-fsdevel@vger.kernel.org Subject: [PATCH v5 2/3] fanotify: return only user requested event types in event mask Message-ID: <40d8b5664ea978a0afa85a0533053a2e2b263b0b.1539758834.git.mbobrowski@mbobrowski.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) 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 Modify fanotify_should_send_event() so that it now returns a mask for an event that contains ONLY flags for the event types that have been specifically requested by the user. Flags that may have been included within the event mask, but have not been explicitly requested by the user will not be present in the returned value. As an example, given the situation where a user requests events of type FAN_OPEN. Traditionally, the event mask returned within an event that occurred on a filesystem object that has been marked for monitoring and is opened, will only ever have the FAN_OPEN bit set. With the introduction of the new flags like FAN_OPEN_EXEC, and perhaps any other future event flags, there is a possibility of the returned event mask containing more than a single bit set, despite having only requested the single event type. Prior to these modifications performed to fanotify_should_send_event(), a user would have received a bundled event mask containing flags FAN_OPEN and FAN_OPEN_EXEC in the instance that a file was opened for execution via execve(), for example. This means that a user would receive event types in the returned event mask that have not been requested. This runs the possibility of breaking existing systems and causing other unforeseen issues. To mitigate this possibility, fanotify_should_send_event() has been modified to return the event mask containing ONLY event types explicitly requested by the user. This means that we will NOT report events that the user did no set a mask for, and we will NOT report events that the user has set an ignore mask for. The function name fanotify_should_send_event() has also been updated so that it's more relevant to what it has been designed to do. Signed-off-by: Matthew Bobrowski --- fs/notify/fanotify/fanotify.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index b3e92302ed84..8a49a0adab73 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -89,7 +89,13 @@ static int fanotify_get_response(struct fsnotify_group *group, return ret; } -static bool fanotify_should_send_event(struct fsnotify_iter_info *iter_info, +/* + * This function returns a mask for an event that only contains the flags + * that have been specifically requested by the user. Flags that may have + * been included within the event mask, but have not been explicitly + * requested by the user, will not be present in the returned mask. + */ +static u32 fanotify_group_event_mask(struct fsnotify_iter_info *iter_info, u32 event_mask, const void *data, int data_type) { @@ -101,21 +107,21 @@ static bool fanotify_should_send_event(struct fsnotify_iter_info *iter_info, pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", __func__, iter_info->report_mask, event_mask, data, data_type); - /* if we don't have enough info to send an event to userspace say no */ + /* If we don't have enough info to send an event to userspace say no */ if (data_type != FSNOTIFY_EVENT_PATH) - return false; + return 0; - /* sorry, fanotify only gives a damn about files and dirs */ + /* Sorry, fanotify only gives a damn about files and dirs */ if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry)) - return false; + return 0; fsnotify_foreach_obj_type(type) { if (!fsnotify_iter_should_report_type(iter_info, type)) continue; mark = iter_info->marks[type]; /* - * if the event is for a child and this inode doesn't care about + * If the event is for a child and this inode doesn't care about * events on the child, don't send it! */ if (type == FSNOTIFY_OBJ_TYPE_INODE && @@ -129,13 +135,10 @@ static bool fanotify_should_send_event(struct fsnotify_iter_info *iter_info, if (d_is_dir(path->dentry) && !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) - return false; - - if (event_mask & FANOTIFY_OUTGOING_EVENTS & - marks_mask & ~marks_ignored_mask) - return true; + return 0; - return false; + return event_mask & FANOTIFY_OUTGOING_EVENTS & marks_mask & + ~marks_ignored_mask; } struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, @@ -211,7 +214,8 @@ static int fanotify_handle_event(struct fsnotify_group *group, BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 11); - if (!fanotify_should_send_event(iter_info, mask, data, data_type)) + mask = fanotify_group_event_mask(iter_info, mask, data, data_type); + if (!mask) return 0; pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,