From patchwork Sun Dec 2 11:38:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10707977 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 8D92C1057 for ; Sun, 2 Dec 2018 11:38:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7C9532AE9F for ; Sun, 2 Dec 2018 11:38:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6E6882AEBA; Sun, 2 Dec 2018 11:38:51 +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 F2A422AE9F for ; Sun, 2 Dec 2018 11:38:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725853AbeLBLiw (ORCPT ); Sun, 2 Dec 2018 06:38:52 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:33646 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725841AbeLBLiv (ORCPT ); Sun, 2 Dec 2018 06:38:51 -0500 Received: by mail-wr1-f66.google.com with SMTP id c14so9326923wrr.0; Sun, 02 Dec 2018 03:38:47 -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=C9xxaINtgcJr6kr15lPd6LBVj+7/G26YZvjuzO+QBpo=; b=c4UucPw+NGouS5c4mGoaKz7LFjfVDxYVca3ZD+N57xXgbVLWJXNMZRMRbuJM8YRqLe TlBrvZ5VMfuCcZhqYWYoftPoxepbtczLepCc+Ia0mYJc0kO4+towFDonEf+8U766q6Uh pgbVNbXJOxdYspv7ebbsNN7obb9AU9qgqCDKY++d25H9EDZJsN9K/SEoX7fWEtffod9k riqWfpnJdwoyM9M+OZGnBcvo2Gud21DtfS+abTFZLVFwlVo++u4NcXzc0tI0JWhfozLl R74dqQ/4uZjkFa9JEnGjWRsz59Pw2nThhBP2HLh3IY1ECtV3RnxqoZrCGKy7hH19N1QO k5TA== 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=C9xxaINtgcJr6kr15lPd6LBVj+7/G26YZvjuzO+QBpo=; b=Y60DtTvHzsNOSQiGOdeKdZvycoJ/Bs/gnZ41C0AajheAaFse7NzB/VVIpGQgbsmSo6 vCUBAe8vm29VaRpV40QGrbLER+VVUAYK5IZvNwZx5DU3A/vc7kD8KxyEP7Ogr1SKNBT4 uRgErrXfgHLpqblM9vF0CcrWh0KRVIKMhJdnhlfPx58OoloVndGnaGqMXFLZkfXM22sH zHQ/nLUQ7eGvEAwDvMee++v2Oep/jp/mHmT1eIqnggkXIEIhZ1PzHIVWE8v8GKKvmvQf cuhRM81eKxgN9obLzGB3bdty80PsTmv4RgtnKCktnUuGmZ6JRv6IX8xwchL0yu7oeMqC Yj/A== X-Gm-Message-State: AA+aEWZAh3UPcURdu9CFOSA8z3BVHhSZZ4BbL6d3qGXkPCXGXNF7E1dx lQ4N0SyGYfJTCMe+k/v34xVlT2sP X-Google-Smtp-Source: AFSGD/Wol4PMzhDrkvZQABPy86pM5Z8NddDWdIhwgocuIxpgdolFFGm9dWq/Lg7NsMYnPHl97H2OPw== X-Received: by 2002:a05:6000:1189:: with SMTP id g9mr11294068wrx.221.1543750727067; Sun, 02 Dec 2018 03:38:47 -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 c3-v6sm3448672wmb.46.2018.12.02.03.38.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 02 Dec 2018 03:38:46 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH v4 08/15] fanotify: enable FAN_REPORT_FID init flag Date: Sun, 2 Dec 2018 13:38:19 +0200 Message-Id: <20181202113826.32133-9-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181202113826.32133-1-amir73il@gmail.com> References: <20181202113826.32133-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 | 57 +++++++++++++++++++++++++++++- include/linux/fanotify.h | 2 +- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 032a9a225a21..3b8d442e67cd 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 @@ -850,6 +852,52 @@ 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; + struct path root = { + .mnt = path->mnt, + .dentry = path->dentry->d_sb->s_root, + }; + int err; + + /* + * Make sure path is not in filesystem with zero fsid (e.g. tmpfs). + */ + 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 = vfs_statfs(&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) { @@ -935,6 +983,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; @@ -963,6 +1017,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); @@ -999,7 +1054,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/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)