From patchwork Sun Nov 25 13:43:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10696839 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 184F25A4 for ; Sun, 25 Nov 2018 13:44:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 07D882916A for ; Sun, 25 Nov 2018 13:44:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F08CA291E8; Sun, 25 Nov 2018 13:44:22 +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 68CDE2916A for ; Sun, 25 Nov 2018 13:44:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726395AbeKZAf2 (ORCPT ); Sun, 25 Nov 2018 19:35:28 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:36232 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726312AbeKZAf1 (ORCPT ); Sun, 25 Nov 2018 19:35:27 -0500 Received: by mail-wr1-f66.google.com with SMTP id t3so16227371wrr.3; Sun, 25 Nov 2018 05:44:19 -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=Ecb42mmhmmejMdNdFr5Jp/UPMVDl/EYOkRwDzt1j+FY=; b=UrWZrUeoL1fpQzG6GJXi9DSeDoG+hVAYr10w9H2xarlgGqjpANBm/8rDpmdFHF7zy0 5gigI9pI5z1F3Nwgf2OfkP+xnVoXF6bV7b8ZtkHA5CVqwKcDwxauatdU9OplaSfUN1L0 AT50EwHyIk0m/ilowQWJCLvEGUNThkeBQ6JSpid5sO0UJSwzuQfsl9AouEt0Dtgn8t21 TrolB+r53rLKDEF7GWWdYmwaQy13yRLjDQz90+ZSfsuM092bj3wRk9rEqihL6c33x0Sj WQ9VJ6KMyWdNhIz1zXAXzDOPwbEGj9NCgSjnA+2A8Pj5KZVK2bwqCDPUpvRbroL+lj0J Gplg== 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=Ecb42mmhmmejMdNdFr5Jp/UPMVDl/EYOkRwDzt1j+FY=; b=bLzYr4FuSIcuMvacOLn6AnkEg6qF+heGC9zZeVDKPng7fEOvr1Z7METN/mYTDQV2Ep azu7OmCIeOfIZiQF/Tw4VAduE4lZ01llBV71ZymBpuIHovfPJByNh4DJIr24CmcaR2RV pKiRup99kVFP5F6EuV0D97wTMl8O4wwJgWwODQQaXiNy9+0TYtqmnD460lH7bXJhiJBw ueu7OLGofz4xzFJmDTGjkzsvdZirpuay6opHb+2ziNv3iPCOius/oNAlm+MAtsz4kYgt ZSOO2dPGD/FD8lbb/o5Nx9Hbc6dbvs3lpwFJ4MfnD3BYpwtOGg3UPKKy/HwGpZSEi9kO X20g== X-Gm-Message-State: AA+aEWZSRzMyCpZgWpfS/7Haw2rJJrLajkpKFUYLlZMlSa6zrb8lW3D0 P4BIrZqaSNN2LygmZipyMuKsfd0Z X-Google-Smtp-Source: AFSGD/XZb6gT6P9fXsVgRsVcmOi/K8mFskLtbTx7P3iGM/a/cs/DKKbV1pDzimyTO79vtqZK+NrvZg== X-Received: by 2002:a05:6000:1189:: with SMTP id g9mr21691677wrx.221.1543153458853; Sun, 25 Nov 2018 05:44:18 -0800 (PST) Received: from localhost.localdomain (bzq-166-168-31-246.red.bezeqint.net. [31.168.166.246]) by smtp.gmail.com with ESMTPSA id 125sm17354274wml.35.2018.11.25.05.44.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Nov 2018 05:44:18 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH v3 08/13] fanotify: enable FAN_REPORT_FID init flag Date: Sun, 25 Nov 2018 15:43:47 +0200 Message-Id: <20181125134352.21499-9-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181125134352.21499-1-amir73il@gmail.com> References: <20181125134352.21499-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 When setting up an fanotify listener, user may request to get fid information in event instead of an open file descriptor. The fid obtained with event on a watched object contains the file handle returned by name_to_handle_at(2) and fsid returned by statfs(2). When setting a mark, we need to make sure that the filesystem supports encoding file handles with name_to_handle_at(2) and that statfs(2) encodes a non-zero fsid. Cc: Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 54 +++++++++++++++++++++++++++++- fs/statfs.c | 4 ++- include/linux/fanotify.h | 2 +- include/linux/statfs.h | 3 ++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index ea8e81a3e80b..d7aa2f392a64 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include @@ -857,6 +859,49 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) return fd; } +/* Check if filesystem can encode a unique fid */ +static int fanotify_test_fid(struct path *path) +{ + struct kstatfs stat, root_stat; + int err; + + /* + * Make sure path is not in filesystem with zero fsid (e.g. tmpfs). + * TODO: cache fsid in the mark connector. + */ + err = vfs_statfs(path, &stat); + if (err) + return err; + + if (!stat.f_fsid.val[0] && !stat.f_fsid.val[1]) + return -ENODEV; + + /* + * Make sure path is not inside a filesystem subvolume (e.g. btrfs) + * which uses a different fsid than sb root. + */ + err = statfs_by_dentry(path->dentry->d_sb->s_root, &root_stat); + if (err) + return err; + + if (root_stat.f_fsid.val[0] != stat.f_fsid.val[0] || + root_stat.f_fsid.val[1] != stat.f_fsid.val[1]) + return -EXDEV; + + /* + * We need to make sure that the file system supports at least + * encoding a file handle so user can use name_to_handle_at() to + * compare fid returned with event to the file handle of watched + * objects. However, name_to_handle_at() requires that the + * filesystem also supports decoding file handles. + */ + if (!path->dentry->d_sb->s_export_op || + !path->dentry->d_sb->s_export_op->fh_to_dentry) + return -EOPNOTSUPP; + + return 0; +} + static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, int dfd, const char __user *pathname) { @@ -942,6 +987,12 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, if (ret) goto fput_and_out; + if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { + ret = fanotify_test_fid(&path); + if (ret) + goto path_put_and_out; + } + /* inode held in place by reference to path; group by fget on fd */ if (mark_type == FAN_MARK_INODE) inode = path.dentry->d_inode; @@ -970,6 +1021,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, ret = -EINVAL; } +path_put_and_out: path_put(&path); fput_and_out: fdput(f); @@ -1006,7 +1058,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark, */ static int __init fanotify_user_setup(void) { - BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7); + BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, diff --git a/fs/statfs.c b/fs/statfs.c index f0216629621d..6a5d840a2d8d 100644 --- a/fs/statfs.c +++ b/fs/statfs.c @@ -50,7 +50,8 @@ static int calculate_f_flags(struct vfsmount *mnt) flags_by_sb(mnt->mnt_sb->s_flags); } -static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf) +/* Does not set buf->f_flags */ +int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf) { int retval; @@ -66,6 +67,7 @@ static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf) buf->f_frsize = buf->f_bsize; return retval; } +EXPORT_SYMBOL(statfs_by_dentry); int vfs_statfs(const struct path *path, struct kstatfs *buf) { diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 9e2142795335..f59be967f72b 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -19,7 +19,7 @@ FAN_CLASS_PRE_CONTENT) #define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \ - FAN_REPORT_TID | \ + FAN_REPORT_TID | FAN_REPORT_FID | \ FAN_CLOEXEC | FAN_NONBLOCK | \ FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS) diff --git a/include/linux/statfs.h b/include/linux/statfs.h index 3142e98546ac..2c3ca7cb8c98 100644 --- a/include/linux/statfs.h +++ b/include/linux/statfs.h @@ -41,4 +41,7 @@ struct kstatfs { #define ST_NODIRATIME 0x0800 /* do not update directory access times */ #define ST_RELATIME 0x1000 /* update atime relative to mtime/ctime */ +struct dentry; +extern int statfs_by_dentry(struct dentry *dentry, struct kstatfs *stat); + #endif