From patchwork Thu Nov 15 18:45:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10684855 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 8A62C109C for ; Thu, 15 Nov 2018 18:46:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7B0AD2CF57 for ; Thu, 15 Nov 2018 18:46:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6F67D2CF63; Thu, 15 Nov 2018 18:46:08 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,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 00F972CF57 for ; Thu, 15 Nov 2018 18:46:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388851AbeKPEzC (ORCPT ); Thu, 15 Nov 2018 23:55:02 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:34341 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726453AbeKPEzC (ORCPT ); Thu, 15 Nov 2018 23:55:02 -0500 Received: by mail-wr1-f67.google.com with SMTP id j26-v6so22330167wre.1; Thu, 15 Nov 2018 10:46:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=f6uSqVDkKF1chwyd5zTPp5FNVtjqNe77q5Vu35NIKnA=; b=WMtFoB3hsViaTOEEwXStXVWj3D7XklyYbua3aYq/+FBbb4+L35glojaBgqtbkaagUk muXOGSsLQ67MZSFw+2HL7AF78krcY1rFEzdNXWXebTa/SXqWgkKikQcm3NEzNtAeDy5p C7qZCpwABMUKVs4PUmOjFoYHdVZEYETacsDBpXIBL0Sc0OYECqBQhlNi1QdWEThnWhGQ rvhcV435vlujDl5qgzleaZNimQrR5fquU8jcxfQ6EEVHHN2wna/P7vI46ZPAG3zGW7Un T+2xEG3Q4ViMinHui2cNw6yWiru2X6wSQ13oicg5z9auBvENmZbRqlavydfPhUEUYT9v ObJQ== 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=f6uSqVDkKF1chwyd5zTPp5FNVtjqNe77q5Vu35NIKnA=; b=kPpVV8Aj4N8J7osMLSMtgzL9GKSrZ3BmQCsSWj+Oy10Iyf0UXErogYoegUDNoPlBYa v6zpb5Po2xHOq/gq/ck5SrcHGNNSrD+UoMhfNjqwC1E7M1hKpswQ3IKzTnWQkIDRxZAy vsXGXvtYHYX7f/yPusulhmGyAf95iUrrnBGN4VwrRY3ucWRiswoCTbDSZNo/DMiBKlj0 WFfH6p2naX29vForedov8FJV4mUZGCi39a0y/h9sxLfyZawjmaWmHc2LoUbgdZdSTQ+q YVupkPGhAe3y3AAWwjwj6t/YfogyB8iRzsjFOWnOjao/c5SRy3FBXJO1tWgj0C6FnpaY HXEA== X-Gm-Message-State: AGRZ1gJ8hATZ3SWW2eXjIhqWQnw/hAccxZ0fC5DylTNkzKsZ0w2teRxp 6QscOO9BhjC5Szu8oKn9epc= X-Google-Smtp-Source: AJdET5fTycAgd7rUPj7P3k+S3v6CmcGykRMZky4GFAqkYdHh+7xPwFSnnvkm/g1jK47XQY1Brxq7Ag== X-Received: by 2002:adf:ff91:: with SMTP id j17-v6mr6628423wrr.214.1542307563431; Thu, 15 Nov 2018 10:46:03 -0800 (PST) Received: from localhost.localdomain ([37.46.46.74]) by smtp.gmail.com with ESMTPSA id s1sm13976327wro.9.2018.11.15.10.46.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Nov 2018 10:46:02 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH v2 9/9] fanotify: report FAN_ONDIR to listener for filename events Date: Thu, 15 Nov 2018 20:45:44 +0200 Message-Id: <20181115184544.30681-10-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181115184544.30681-1-amir73il@gmail.com> References: <20181115184544.30681-1-amir73il@gmail.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 For events reporting fid of parent (create/delete/move), we report FAN_ONDIR, so user can differentiate mkdir/rmdir from creat/unlink events. Unlike legacy fanotify events (open/access/close), mkdir/rmdir will be reported regardless if user requested FAN_ONDIR, but the FAN_ONDIR flag itself will only be reported if the user asked for it. For backward compatibility, we do not report FAN_ONDIR to user for legacy fanotify events. While it is not documented that this event flags is NOT reported, it is better to be on the safe side and preserve the existing behavior. At the moment the new event FAN_ATTRIB (setattr/setxattr) behaves like the legacy events, because like them, it reports the fid of the victim, so user has a way to identify the victim as a directory. Not sure if FAN_ATTRIB should behave like filename events w.r.t FAN_ONDIR and not sure whether legacy events behavior should change with FAN_REPORT_FID. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 42 ++++++++++++++++++++++++++++++----- include/linux/fanotify.h | 9 +++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index a2d63e86ba47..f1a431b1a4d9 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -119,10 +119,12 @@ static struct dentry *fanotify_event_dentry(const void *data, int data_type) * 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) + u32 event_mask, const void *data, + int data_type) { __u32 marks_mask = 0, marks_ignored_mask = 0; + __u32 test_mask, user_mask = FANOTIFY_EVENT_TYPES; + bool is_filename = (event_mask & FANOTIFY_FILENAME_EVENTS); struct dentry *dentry = fanotify_event_dentry(data, data_type); struct fsnotify_mark *mark; int type; @@ -151,12 +153,42 @@ static u32 fanotify_group_event_mask(struct fsnotify_iter_info *iter_info, marks_ignored_mask |= mark->ignored_mask; } - if (d_is_dir(dentry) && + if (is_filename) { + /* + * For filename events (create/delete/move), we report + * FAN_ONDIR for mkdir/rmdir so user can differentiate them + * from creat/unlink events. Unlike legacy fanotify events + * (open/access/close), mkdir/rmdir will be reported regardless + * if user requested FAN_ONDIR, but the FAN_ONDIR flag itself + * will only be reported if the user asked for it. + */ + user_mask |= FAN_ONDIR; + } else { + /* + * For backward compatibility, do not report FAN_ONDIR to user + * in legacy fanotify mode (reporting fd). While it is not + * documented that this event flag is NOT reported, it is + * better to be on the safe side and preserve the existing + * behavior. + */ + BUILD_BUG_ON(FANOTIFY_EVENT_TYPES & FANOTIFY_EVENT_FLAGS); + } + + /* Do not report FAN_ONDIR without an event type */ + test_mask = event_mask & marks_mask & ~marks_ignored_mask; + if (!(test_mask & FANOTIFY_EVENT_TYPES)) + return 0; + + /* + * Unlike legacy fanotify events (open/access/close), mkdir/rmdir + * events will be reported even if FAN_ONDIR is in ignore mask, but + * the FAN_ONDIR flag itself will not be reported in this case. + */ + if (d_is_dir(dentry) && !is_filename && !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) return 0; - return event_mask & FANOTIFY_OUTGOING_EVENTS & marks_mask & - ~marks_ignored_mask; + return test_mask & user_mask; } static struct fanotify_event_fid *fanotify_alloc_fid(struct dentry *dentry, diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index fbda82b25ae3..7ccbcebc35e7 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -59,13 +59,16 @@ #define FANOTIFY_PERM_EVENTS (FAN_OPEN_PERM | FAN_ACCESS_PERM | \ FAN_OPEN_EXEC_PERM) +/* Events types that may be reported from vfs */ +#define FANOTIFY_EVENT_TYPES (FANOTIFY_EVENTS | \ + FANOTIFY_PERM_EVENTS) + /* Extra flags that may be reported with event or control handling of events */ #define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR) /* Events that may be reported to user */ -#define FANOTIFY_OUTGOING_EVENTS (FANOTIFY_EVENTS | \ - FANOTIFY_PERM_EVENTS | \ - FAN_Q_OVERFLOW) +#define FANOTIFY_OUTGOING_EVENTS (FANOTIFY_EVENT_TYPES | \ + FAN_Q_OVERFLOW | FAN_ONDIR) #define ALL_FANOTIFY_EVENT_BITS (FANOTIFY_OUTGOING_EVENTS | \ FANOTIFY_EVENT_FLAGS)