From: Song Liu <songliubraving@meta.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: "Amir Goldstein" <amir73il@gmail.com>,
"Song Liu" <song@kernel.org>, bpf <bpf@vger.kernel.org>,
Linux-Fsdevel <linux-fsdevel@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
"LSM List" <linux-security-module@vger.kernel.org>,
"Kernel Team" <kernel-team@meta.com>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Eddy Z" <eddyz87@gmail.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Christian Brauner" <brauner@kernel.org>,
"Jan Kara" <jack@suse.cz>, "KP Singh" <kpsingh@kernel.org>,
"Matt Bobrowski" <mattbobrowski@google.com>,
"repnop@google.com" <repnop@google.com>,
"Jeff Layton" <jlayton@kernel.org>,
"Josef Bacik" <josef@toxicpanda.com>,
"Mickaël Salaün" <mic@digikod.net>,
"gnoack@google.com" <gnoack@google.com>
Subject: Re: [PATCH v3 fanotify 2/2] samples/fanotify: Add a sample fanotify fiter
Date: Wed, 27 Nov 2024 02:16:09 +0000 [thread overview]
Message-ID: <21A94434-5519-4659-83FA-3AB782F064E2@fb.com> (raw)
In-Reply-To: <CAADnVQK-6MFdwD_0j-3x2-t8VUjbNJUuGrTXEWJ0ttdpHvtLOA@mail.gmail.com>
> On Nov 26, 2024, at 4:50 PM, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
>
[...]
>>> +
>>> +static void sample_filter_free(struct fanotify_filter_hook *filter_hook)
>>> +{
>>> + struct fan_filter_sample_data *data = filter_hook->data;
>>> +
>>> + path_put(&data->subtree_path);
>>> + kfree(data);
>>> +}
>>> +
>>
>> Hi Song,
>>
>> This example looks fine but it raises a question.
>> This filter will keep the mount of subtree_path busy until the group is closed
>> or the filter is detached.
>> This is probably fine for many services that keep the mount busy anyway.
>>
>> But what if this wasn't the intention?
>> What if an Anti-malware engine that watches all mounts wanted to use that
>> for configuring some ignore/block subtree filters?
>>
>> One way would be to use a is_subtree() variant that looks for a
>> subtree root inode
>> number and then verifies it with a subtree root fid.
>> A production subtree filter will need to use a variant of is_subtree()
>> anyway that
>> looks for a set of subtree root inodes, because doing a loop of is_subtree() for
>> multiple paths is a no go.
>>
>> Don't need to change anything in the example, unless other people
>> think that we do need to set a better example to begin with...
>
> I think we have to treat this patch as a real filter and not as an example
> to make sure that the whole approach is workable end to end.
> The point about not holding path/dentry is very valid.
> The algorithm needs to support that.
Hmm.. I am not sure whether we cannot hold a refcount. If that is a
requirement, the algorithm will be more complex.
IIUC, fsnotify_mark on a inode does not hold a refcount to inode.
And when the inode is evicted, the mark is freed. I guess this
requires the user space, the AntiVirus scanner for example, to
hold a reference to the inode? If this is the case, I think it
is OK for the filter, either bpf or kernel module, to hold a
reference to the subtree root.
> It may very well turn out that the logic of handling many filters
> without a loop and not grabbing a path refcnt is too complex for bpf.
> Then this subtree filtering would have to stay as a kernel module
> or extra flag/feature for fanotify.
Handling multiple subtrees is indeed an issue. Since we rely on
the mark in the SB, multiple subtrees under the same SB will share
that mark. Unless we use some cache, accessing a file will
trigger multiple is_subdir() calls.
One possible solution is that have a new helper that checks
is_subdir() for a list of parent subtrees with a single series
of dentry walk. IOW, something like:
bool is_subdir_of_any(struct dentry *new_dentry,
struct list_head *list_of_dentry).
For BPF, one possible solution is to walk the dentry tree
up to the root, under bpf_rcu_read_lock().
Does this sound reasonable?
Thanks,
Song
next prev parent reply other threads:[~2024-11-27 2:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-22 22:59 [PATCH v3 fanotify 0/2] Fanotify in kernel filter Song Liu
2024-11-22 22:59 ` [PATCH v3 fanotify 1/2] fanotify: Introduce fanotify filter Song Liu
2024-11-24 6:25 ` Amir Goldstein
2024-11-24 19:08 ` Song Liu
2024-11-28 16:37 ` Jan Kara
2024-11-25 11:01 ` kernel test robot
2024-11-26 0:06 ` kernel test robot
2024-11-22 22:59 ` [PATCH v3 fanotify 2/2] samples/fanotify: Add a sample fanotify fiter Song Liu
2024-11-24 5:07 ` Amir Goldstein
2024-11-24 18:59 ` Song Liu
2024-11-27 0:50 ` Alexei Starovoitov
2024-11-27 2:16 ` Song Liu [this message]
2024-11-28 17:10 ` Jan Kara
2024-12-02 17:38 ` Song Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=21A94434-5519-4659-83FA-3AB782F064E2@fb.com \
--to=songliubraving@meta.com \
--cc=alexei.starovoitov@gmail.com \
--cc=amir73il@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=gnoack@google.com \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=josef@toxicpanda.com \
--cc=kernel-team@meta.com \
--cc=kpsingh@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mattbobrowski@google.com \
--cc=mic@digikod.net \
--cc=repnop@google.com \
--cc=song@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox