License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 22:07:57 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2009-12-18 10:24:29 +08:00
|
|
|
#include <linux/fanotify.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
#include <linux/fdtable.h>
|
|
|
|
#include <linux/fsnotify_backend.h>
|
|
|
|
#include <linux/init.h>
|
2009-12-18 10:24:34 +08:00
|
|
|
#include <linux/jiffies.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
#include <linux/kernel.h> /* UINT_MAX */
|
2009-12-18 10:24:28 +08:00
|
|
|
#include <linux/mount.h>
|
2009-12-18 10:24:34 +08:00
|
|
|
#include <linux/sched.h>
|
2017-02-03 00:54:15 +08:00
|
|
|
#include <linux/sched/user.h>
|
2017-09-27 01:45:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
#include <linux/types.h>
|
2009-12-18 10:24:34 +08:00
|
|
|
#include <linux/wait.h>
|
2017-10-03 08:21:39 +08:00
|
|
|
#include <linux/audit.h>
|
fs: fsnotify: account fsnotify metadata to kmemcg
Patch series "Directed kmem charging", v8.
The Linux kernel's memory cgroup allows limiting the memory usage of the
jobs running on the system to provide isolation between the jobs. All
the kernel memory allocated in the context of the job and marked with
__GFP_ACCOUNT will also be included in the memory usage and be limited
by the job's limit.
The kernel memory can only be charged to the memcg of the process in
whose context kernel memory was allocated. However there are cases
where the allocated kernel memory should be charged to the memcg
different from the current processes's memcg. This patch series
contains two such concrete use-cases i.e. fsnotify and buffer_head.
The fsnotify event objects can consume a lot of system memory for large
or unlimited queues if there is either no or slow listener. The events
are allocated in the context of the event producer. However they should
be charged to the event consumer. Similarly the buffer_head objects can
be allocated in a memcg different from the memcg of the page for which
buffer_head objects are being allocated.
To solve this issue, this patch series introduces mechanism to charge
kernel memory to a given memcg. In case of fsnotify events, the memcg
of the consumer can be used for charging and for buffer_head, the memcg
of the page can be charged. For directed charging, the caller can use
the scope API memalloc_[un]use_memcg() to specify the memcg to charge
for all the __GFP_ACCOUNT allocations within the scope.
This patch (of 2):
A lot of memory can be consumed by the events generated for the huge or
unlimited queues if there is either no or slow listener. This can cause
system level memory pressure or OOMs. So, it's better to account the
fsnotify kmem caches to the memcg of the listener.
However the listener can be in a different memcg than the memcg of the
producer and these allocations happen in the context of the event
producer. This patch introduces remote memcg charging API which the
producer can use to charge the allocations to the memcg of the listener.
There are seven fsnotify kmem caches and among them allocations from
dnotify_struct_cache, dnotify_mark_cache, fanotify_mark_cache and
inotify_inode_mark_cachep happens in the context of syscall from the
listener. So, SLAB_ACCOUNT is enough for these caches.
The objects from fsnotify_mark_connector_cachep are not accounted as
they are small compared to the notification mark or events and it is
unclear whom to account connector to since it is shared by all events
attached to the inode.
The allocations from the event caches happen in the context of the event
producer. For such caches we will need to remote charge the allocations
to the listener's memcg. Thus we save the memcg reference in the
fsnotify_group structure of the listener.
This patch has also moved the members of fsnotify_group to keep the size
same, at least for 64 bit build, even with additional member by filling
the holes.
[shakeelb@google.com: use GFP_KERNEL_ACCOUNT rather than open-coding it]
Link: http://lkml.kernel.org/r/20180702215439.211597-1-shakeelb@google.com
Link: http://lkml.kernel.org/r/20180627191250.209150-2-shakeelb@google.com
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-08-18 06:46:39 +08:00
|
|
|
#include <linux/sched/mm.h>
|
2019-01-11 01:04:34 +08:00
|
|
|
#include <linux/statfs.h>
|
2021-03-04 18:48:24 +08:00
|
|
|
#include <linux/stringhash.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
#include "fanotify.h"
|
|
|
|
|
2020-03-24 23:55:37 +08:00
|
|
|
static bool fanotify_path_equal(struct path *p1, struct path *p2)
|
|
|
|
{
|
|
|
|
return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:48:24 +08:00
|
|
|
static unsigned int fanotify_hash_path(const struct path *path)
|
|
|
|
{
|
|
|
|
return hash_ptr(path->dentry, FANOTIFY_EVENT_HASH_BITS) ^
|
|
|
|
hash_ptr(path->mnt, FANOTIFY_EVENT_HASH_BITS);
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:55:37 +08:00
|
|
|
static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
|
|
|
|
__kernel_fsid_t *fsid2)
|
|
|
|
{
|
2020-03-28 01:10:30 +08:00
|
|
|
return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
|
2020-03-24 23:55:37 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 18:48:24 +08:00
|
|
|
static unsigned int fanotify_hash_fsid(__kernel_fsid_t *fsid)
|
|
|
|
{
|
|
|
|
return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^
|
|
|
|
hash_32(fsid->val[1], FANOTIFY_EVENT_HASH_BITS);
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:55:37 +08:00
|
|
|
static bool fanotify_fh_equal(struct fanotify_fh *fh1,
|
2020-03-25 00:04:20 +08:00
|
|
|
struct fanotify_fh *fh2)
|
2020-03-24 23:55:37 +08:00
|
|
|
{
|
|
|
|
if (fh1->type != fh2->type || fh1->len != fh2->len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !fh1->len ||
|
|
|
|
!memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:48:24 +08:00
|
|
|
static unsigned int fanotify_hash_fh(struct fanotify_fh *fh)
|
|
|
|
{
|
|
|
|
long salt = (long)fh->type | (long)fh->len << 8;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* full_name_hash() works long by long, so it handles fh buf optimally.
|
|
|
|
*/
|
|
|
|
return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len);
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:04:20 +08:00
|
|
|
static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
|
|
|
|
struct fanotify_fid_event *ffe2)
|
|
|
|
{
|
|
|
|
/* Do not merge fid events without object fh */
|
|
|
|
if (!ffe1->object_fh.len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
|
|
|
|
fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:42:17 +08:00
|
|
|
static bool fanotify_info_equal(struct fanotify_info *info1,
|
|
|
|
struct fanotify_info *info2)
|
|
|
|
{
|
|
|
|
if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
|
|
|
|
info1->file_fh_totlen != info2->file_fh_totlen ||
|
|
|
|
info1->name_len != info2->name_len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info1->dir_fh_totlen &&
|
|
|
|
!fanotify_fh_equal(fanotify_info_dir_fh(info1),
|
|
|
|
fanotify_info_dir_fh(info2)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info1->file_fh_totlen &&
|
|
|
|
!fanotify_fh_equal(fanotify_info_file_fh(info1),
|
|
|
|
fanotify_info_file_fh(info2)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !info1->name_len ||
|
|
|
|
!memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
|
|
|
|
info1->name_len);
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:10:21 +08:00
|
|
|
static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
|
|
|
|
struct fanotify_name_event *fne2)
|
|
|
|
{
|
2020-07-16 16:42:17 +08:00
|
|
|
struct fanotify_info *info1 = &fne1->info;
|
|
|
|
struct fanotify_info *info2 = &fne2->info;
|
2020-03-19 23:10:21 +08:00
|
|
|
|
2020-07-16 16:42:17 +08:00
|
|
|
/* Do not merge name events without dir fh */
|
|
|
|
if (!info1->dir_fh_totlen)
|
2020-03-19 23:10:21 +08:00
|
|
|
return false;
|
|
|
|
|
2020-07-28 16:58:07 +08:00
|
|
|
if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
|
|
|
|
return false;
|
|
|
|
|
2020-07-16 16:42:17 +08:00
|
|
|
return fanotify_info_equal(info1, info2);
|
2020-03-19 23:10:21 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 18:48:23 +08:00
|
|
|
static bool fanotify_should_merge(struct fanotify_event *old,
|
|
|
|
struct fanotify_event *new)
|
2009-12-18 10:24:25 +08:00
|
|
|
{
|
2021-03-04 18:48:23 +08:00
|
|
|
pr_debug("%s: old=%p new=%p\n", __func__, old, new);
|
2009-12-18 10:24:25 +08:00
|
|
|
|
2021-03-04 18:48:23 +08:00
|
|
|
if (old->hash != new->hash ||
|
2020-03-25 00:04:20 +08:00
|
|
|
old->type != new->type || old->pid != new->pid)
|
2019-01-11 01:04:34 +08:00
|
|
|
return false;
|
|
|
|
|
2020-07-16 16:42:10 +08:00
|
|
|
/*
|
|
|
|
* We want to merge many dirent events in the same dir (i.e.
|
|
|
|
* creates/unlinks/renames), but we do not want to merge dirent
|
|
|
|
* events referring to subdirs with dirent events referring to
|
|
|
|
* non subdirs, otherwise, user won't be able to tell from a
|
|
|
|
* mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
|
|
|
|
* unlink pair or rmdir+create pair of events.
|
|
|
|
*/
|
|
|
|
if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
|
|
|
|
return false;
|
|
|
|
|
2020-03-25 00:04:20 +08:00
|
|
|
switch (old->type) {
|
|
|
|
case FANOTIFY_EVENT_TYPE_PATH:
|
2020-03-24 23:55:37 +08:00
|
|
|
return fanotify_path_equal(fanotify_event_path(old),
|
|
|
|
fanotify_event_path(new));
|
2020-03-25 00:04:20 +08:00
|
|
|
case FANOTIFY_EVENT_TYPE_FID:
|
|
|
|
return fanotify_fid_event_equal(FANOTIFY_FE(old),
|
|
|
|
FANOTIFY_FE(new));
|
2020-03-19 23:10:21 +08:00
|
|
|
case FANOTIFY_EVENT_TYPE_FID_NAME:
|
|
|
|
return fanotify_name_event_equal(FANOTIFY_NE(old),
|
|
|
|
FANOTIFY_NE(new));
|
2020-03-25 00:04:20 +08:00
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
2019-01-11 01:04:34 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:48:26 +08:00
|
|
|
/* Limit event merges to limit CPU overhead per event */
|
|
|
|
#define FANOTIFY_MAX_MERGE_EVENTS 128
|
|
|
|
|
2010-07-28 22:18:37 +08:00
|
|
|
/* and the list better be locked by something too! */
|
2021-03-04 18:48:25 +08:00
|
|
|
static int fanotify_merge(struct fsnotify_group *group,
|
|
|
|
struct fsnotify_event *event)
|
2009-12-18 10:24:25 +08:00
|
|
|
{
|
2021-03-04 18:48:23 +08:00
|
|
|
struct fanotify_event *old, *new = FANOTIFY_E(event);
|
2021-03-04 18:48:25 +08:00
|
|
|
unsigned int bucket = fanotify_event_hash_bucket(group, new);
|
|
|
|
struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
|
2021-03-04 18:48:26 +08:00
|
|
|
int i = 0;
|
2009-12-18 10:24:25 +08:00
|
|
|
|
2021-03-04 18:48:25 +08:00
|
|
|
pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
|
|
|
|
group, event, bucket);
|
2009-12-18 10:24:25 +08:00
|
|
|
|
2014-01-29 01:29:24 +08:00
|
|
|
/*
|
|
|
|
* Don't merge a permission event with any other event so that we know
|
|
|
|
* the event structure we have created in fanotify_handle_event() is the
|
|
|
|
* one we should check for permission response.
|
|
|
|
*/
|
2019-01-11 01:04:31 +08:00
|
|
|
if (fanotify_is_perm_event(new->mask))
|
2014-01-29 01:53:22 +08:00
|
|
|
return 0;
|
2014-01-29 01:29:24 +08:00
|
|
|
|
2021-03-04 18:48:25 +08:00
|
|
|
hlist_for_each_entry(old, hlist, merge_list) {
|
2021-03-04 18:48:26 +08:00
|
|
|
if (++i > FANOTIFY_MAX_MERGE_EVENTS)
|
|
|
|
break;
|
2021-03-04 18:48:23 +08:00
|
|
|
if (fanotify_should_merge(old, new)) {
|
|
|
|
old->mask |= new->mask;
|
2017-02-09 20:45:22 +08:00
|
|
|
return 1;
|
2009-12-18 10:24:25 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-28 22:18:37 +08:00
|
|
|
|
2017-02-09 20:45:22 +08:00
|
|
|
return 0;
|
2009-12-18 10:24:25 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:18:02 +08:00
|
|
|
/*
|
|
|
|
* Wait for response to permission event. The function also takes care of
|
|
|
|
* freeing the permission event (or offloads that in case the wait is canceled
|
|
|
|
* by a signal). The function returns 0 in case access got allowed by userspace,
|
|
|
|
* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
|
|
|
|
* the wait got interrupted by a signal.
|
|
|
|
*/
|
2014-04-04 05:46:33 +08:00
|
|
|
static int fanotify_get_response(struct fsnotify_group *group,
|
2019-01-11 01:04:32 +08:00
|
|
|
struct fanotify_perm_event *event,
|
2016-11-11 00:45:16 +08:00
|
|
|
struct fsnotify_iter_info *iter_info)
|
2009-12-18 10:24:34 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
|
|
|
|
|
2019-02-21 18:47:23 +08:00
|
|
|
ret = wait_event_killable(group->fanotify_data.access_waitq,
|
|
|
|
event->state == FAN_EVENT_ANSWERED);
|
2019-01-08 22:18:02 +08:00
|
|
|
/* Signal pending? */
|
|
|
|
if (ret < 0) {
|
|
|
|
spin_lock(&group->notification_lock);
|
|
|
|
/* Event reported to userspace and no answer yet? */
|
|
|
|
if (event->state == FAN_EVENT_REPORTED) {
|
|
|
|
/* Event will get freed once userspace answers to it */
|
|
|
|
event->state = FAN_EVENT_CANCELED;
|
|
|
|
spin_unlock(&group->notification_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* Event not yet reported? Just remove it. */
|
2021-03-04 18:48:25 +08:00
|
|
|
if (event->state == FAN_EVENT_INIT) {
|
2019-01-08 22:18:02 +08:00
|
|
|
fsnotify_remove_queued_event(group, &event->fae.fse);
|
2021-03-04 18:48:25 +08:00
|
|
|
/* Permission events are not supposed to be hashed */
|
|
|
|
WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
|
|
|
|
}
|
2019-01-08 22:18:02 +08:00
|
|
|
/*
|
|
|
|
* Event may be also answered in case signal delivery raced
|
|
|
|
* with wakeup. In that case we have nothing to do besides
|
|
|
|
* freeing the event and reporting error.
|
|
|
|
*/
|
|
|
|
spin_unlock(&group->notification_lock);
|
|
|
|
goto out;
|
|
|
|
}
|
2009-12-18 10:24:34 +08:00
|
|
|
|
|
|
|
/* userspace responded, convert to something usable */
|
2017-10-03 08:21:39 +08:00
|
|
|
switch (event->response & ~FAN_AUDIT) {
|
2009-12-18 10:24:34 +08:00
|
|
|
case FAN_ALLOW:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case FAN_DENY:
|
|
|
|
default:
|
|
|
|
ret = -EPERM;
|
|
|
|
}
|
2017-10-03 08:21:39 +08:00
|
|
|
|
|
|
|
/* Check if the response should be audited */
|
|
|
|
if (event->response & FAN_AUDIT)
|
|
|
|
audit_fanotify(event->response & ~FAN_AUDIT);
|
|
|
|
|
2009-12-18 10:24:34 +08:00
|
|
|
pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
|
|
|
|
group, event, ret);
|
2019-01-08 22:18:02 +08:00
|
|
|
out:
|
|
|
|
fsnotify_destroy_event(group, &event->fae.fse);
|
2019-01-11 01:04:42 +08:00
|
|
|
|
2009-12-18 10:24:34 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
fanotify: return only user requested event types in event mask
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 <mbobrowski@mbobrowski.org>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
2018-11-08 11:05:49 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-01-11 01:04:42 +08:00
|
|
|
static u32 fanotify_group_event_mask(struct fsnotify_group *group,
|
|
|
|
struct fsnotify_iter_info *iter_info,
|
|
|
|
u32 event_mask, const void *data,
|
2020-07-16 16:42:26 +08:00
|
|
|
int data_type, struct inode *dir)
|
2009-12-18 10:24:28 +08:00
|
|
|
{
|
2018-04-05 04:42:18 +08:00
|
|
|
__u32 marks_mask = 0, marks_ignored_mask = 0;
|
2020-07-16 16:42:09 +08:00
|
|
|
__u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
|
|
|
|
FANOTIFY_EVENT_FLAGS;
|
2020-03-19 23:10:12 +08:00
|
|
|
const struct path *path = fsnotify_data_path(data, data_type);
|
2020-07-16 16:42:12 +08:00
|
|
|
unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
|
2018-04-21 07:10:54 +08:00
|
|
|
struct fsnotify_mark *mark;
|
|
|
|
int type;
|
2010-07-28 22:18:39 +08:00
|
|
|
|
2018-04-21 07:10:54 +08:00
|
|
|
pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
|
|
|
|
__func__, iter_info->report_mask, event_mask, data, data_type);
|
2010-07-28 22:18:39 +08:00
|
|
|
|
2020-07-16 16:42:12 +08:00
|
|
|
if (!fid_mode) {
|
2019-01-11 01:04:42 +08:00
|
|
|
/* Do we have path to open a file descriptor? */
|
2020-03-19 23:10:12 +08:00
|
|
|
if (!path)
|
2019-01-11 01:04:42 +08:00
|
|
|
return 0;
|
|
|
|
/* Path type events are only relevant for files and dirs */
|
|
|
|
if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
|
|
|
|
return 0;
|
2020-07-16 16:42:26 +08:00
|
|
|
} else if (!(fid_mode & FAN_REPORT_FID)) {
|
|
|
|
/* Do we have a directory inode to report? */
|
|
|
|
if (!dir && !(event_mask & FS_ISDIR))
|
|
|
|
return 0;
|
2019-01-11 01:04:42 +08:00
|
|
|
}
|
2010-10-29 05:21:58 +08:00
|
|
|
|
2018-04-21 07:10:54 +08:00
|
|
|
fsnotify_foreach_obj_type(type) {
|
|
|
|
if (!fsnotify_iter_should_report_type(iter_info, type))
|
|
|
|
continue;
|
|
|
|
mark = iter_info->marks[type];
|
2020-05-24 15:24:41 +08:00
|
|
|
|
|
|
|
/* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
|
|
|
|
marks_ignored_mask |= mark->ignored_mask;
|
|
|
|
|
2020-03-19 23:10:17 +08:00
|
|
|
/*
|
|
|
|
* If the event is on dir and this mark doesn't care about
|
|
|
|
* events on dir, don't send it!
|
|
|
|
*/
|
|
|
|
if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
|
|
|
|
continue;
|
|
|
|
|
2018-04-21 07:10:54 +08:00
|
|
|
/*
|
2020-12-02 20:07:09 +08:00
|
|
|
* If the event is on a child and this mark is on a parent not
|
fsnotify: send event to parent and child with single callback
Instead of calling fsnotify() twice, once with parent inode and once
with child inode, if event should be sent to parent inode, send it
with both parent and child inodes marks in object type iterator and call
the backend handle_event() callback only once.
The parent inode is assigned to the standard "inode" iterator type and
the child inode is assigned to the special "child" iterator type.
In that case, the bit FS_EVENT_ON_CHILD will be set in the event mask,
the dir argument to handle_event will be the parent inode, the file_name
argument to handle_event is non NULL and refers to the name of the child
and the child inode can be accessed with fsnotify_data_inode().
This will allow fanotify to make decisions based on child or parent's
ignored mask. For example, when a parent is interested in a specific
event on its children, but a specific child wishes to ignore this event,
the event will not be reported. This is not what happens with current
code, but according to man page, it is the expected behavior.
Link: https://lore.kernel.org/r/20200716084230.30611-15-amir73il@gmail.com
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
2020-07-16 16:42:22 +08:00
|
|
|
* watching children, don't send it!
|
2018-04-21 07:10:54 +08:00
|
|
|
*/
|
2020-12-02 20:07:09 +08:00
|
|
|
if (type == FSNOTIFY_OBJ_TYPE_PARENT &&
|
|
|
|
!(mark->mask & FS_EVENT_ON_CHILD))
|
2018-04-21 07:10:54 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
marks_mask |= mark->mask;
|
2010-07-28 22:18:39 +08:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:04:44 +08:00
|
|
|
test_mask = event_mask & marks_mask & ~marks_ignored_mask;
|
|
|
|
|
|
|
|
/*
|
fanotify: send FAN_DIR_MODIFY event flavor with dir inode and name
Dirent events are going to be supported in two flavors:
1. Directory fid info + mask that includes the specific event types
(e.g. FAN_CREATE) and an optional FAN_ONDIR flag.
2. Directory fid info + name + mask that includes only FAN_DIR_MODIFY.
To request the second event flavor, user needs to set the event type
FAN_DIR_MODIFY in the mark mask.
The first flavor is supported since kernel v5.1 for groups initialized
with flag FAN_REPORT_FID. It is intended to be used for watching
directories in "batch mode" - the watcher is notified when directory is
changed and re-scans the directory content in response. This event
flavor is stored more compactly in the event queue, so it is optimal
for workloads with frequent directory changes.
The second event flavor is intended to be used for watching large
directories, where the cost of re-scan of the directory on every change
is considered too high. The watcher getting the event with the directory
fid and entry name is expected to call fstatat(2) to query the content of
the entry after the change.
Legacy inotify events are reported with name and event mask (e.g. "foo",
FAN_CREATE | FAN_ONDIR). That can lead users to the conclusion that
there is *currently* an entry "foo" that is a sub-directory, when in fact
"foo" may be negative or non-dir by the time user gets the event.
To make it clear that the current state of the named entry is unknown,
when reporting an event with name info, fanotify obfuscates the specific
event types (e.g. create,delete,rename) and uses a common event type -
FAN_DIR_MODIFY to describe the change. This should make it harder for
users to make wrong assumptions and write buggy filesystem monitors.
At this point, name info reporting is not yet implemented, so trying to
set FAN_DIR_MODIFY in mark mask will return -EINVAL.
Link: https://lore.kernel.org/r/20200319151022.31456-12-amir73il@gmail.com
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
2020-03-19 23:10:19 +08:00
|
|
|
* For dirent modification events (create/delete/move) that do not carry
|
|
|
|
* the child entry name information, we report FAN_ONDIR for mkdir/rmdir
|
|
|
|
* so user can differentiate them from creat/unlink.
|
2019-01-11 01:04:44 +08:00
|
|
|
*
|
|
|
|
* For backward compatibility and consistency, do not report FAN_ONDIR
|
|
|
|
* to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
|
2020-07-16 16:42:09 +08:00
|
|
|
* to user in fid mode for all event types.
|
|
|
|
*
|
|
|
|
* We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
|
|
|
|
* fanotify_alloc_event() when group is reporting fid as indication
|
|
|
|
* that event happened on child.
|
2019-01-11 01:04:44 +08:00
|
|
|
*/
|
2020-07-16 16:42:12 +08:00
|
|
|
if (fid_mode) {
|
2020-07-16 16:42:09 +08:00
|
|
|
/* Do not report event flags without any event */
|
|
|
|
if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
|
2019-01-11 01:04:44 +08:00
|
|
|
return 0;
|
|
|
|
} else {
|
2020-07-16 16:42:09 +08:00
|
|
|
user_mask &= ~FANOTIFY_EVENT_FLAGS;
|
2019-01-11 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return test_mask & user_mask;
|
2009-12-18 10:24:28 +08:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:42:18 +08:00
|
|
|
/*
|
|
|
|
* Check size needed to encode fanotify_fh.
|
|
|
|
*
|
|
|
|
* Return size of encoded fh without fanotify_fh header.
|
|
|
|
* Return 0 on failure to encode.
|
|
|
|
*/
|
|
|
|
static int fanotify_encode_fh_len(struct inode *inode)
|
|
|
|
{
|
|
|
|
int dwords = 0;
|
|
|
|
|
|
|
|
if (!inode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
|
|
|
|
|
|
|
|
return dwords << 2;
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:42:17 +08:00
|
|
|
/*
|
|
|
|
* Encode fanotify_fh.
|
|
|
|
*
|
|
|
|
* Return total size of encoded fh including fanotify_fh header.
|
|
|
|
* Return 0 on failure to encode.
|
|
|
|
*/
|
|
|
|
static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
|
2021-03-04 18:48:24 +08:00
|
|
|
unsigned int fh_len, unsigned int *hash,
|
|
|
|
gfp_t gfp)
|
2019-01-11 01:04:34 +08:00
|
|
|
{
|
2020-07-16 16:42:18 +08:00
|
|
|
int dwords, type = 0;
|
2020-03-24 23:55:37 +08:00
|
|
|
char *ext_buf = NULL;
|
|
|
|
void *buf = fh->buf;
|
|
|
|
int err;
|
2019-01-11 01:04:34 +08:00
|
|
|
|
2020-07-16 16:42:11 +08:00
|
|
|
fh->type = FILEID_ROOT;
|
|
|
|
fh->len = 0;
|
2020-07-16 16:42:18 +08:00
|
|
|
fh->flags = 0;
|
2020-03-19 23:10:21 +08:00
|
|
|
if (!inode)
|
2020-07-16 16:42:17 +08:00
|
|
|
return 0;
|
2020-03-19 23:10:21 +08:00
|
|
|
|
2020-07-16 16:42:18 +08:00
|
|
|
/*
|
|
|
|
* !gpf means preallocated variable size fh, but fh_len could
|
|
|
|
* be zero in that case if encoding fh len failed.
|
|
|
|
*/
|
2019-01-11 01:04:34 +08:00
|
|
|
err = -ENOENT;
|
2020-07-16 16:42:18 +08:00
|
|
|
if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
|
2019-01-11 01:04:34 +08:00
|
|
|
goto out_err;
|
|
|
|
|
2020-07-16 16:42:18 +08:00
|
|
|
/* No external buffer in a variable size allocated fh */
|
|
|
|
if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
|
|
|
|
/* Treat failure to allocate fh as failure to encode fh */
|
2019-01-11 01:04:34 +08:00
|
|
|
err = -ENOMEM;
|
2020-07-16 16:42:18 +08:00
|
|
|
ext_buf = kmalloc(fh_len, gfp);
|
2020-03-24 23:55:37 +08:00
|
|
|
if (!ext_buf)
|
2019-01-11 01:04:34 +08:00
|
|
|
goto out_err;
|
2020-03-24 23:55:37 +08:00
|
|
|
|
|
|
|
*fanotify_fh_ext_buf_ptr(fh) = ext_buf;
|
|
|
|
buf = ext_buf;
|
2020-07-16 16:42:18 +08:00
|
|
|
fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
|
2019-01-11 01:04:34 +08:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:42:18 +08:00
|
|
|
dwords = fh_len >> 2;
|
2020-03-24 23:55:37 +08:00
|
|
|
type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
|
2019-01-11 01:04:34 +08:00
|
|
|
err = -EINVAL;
|
2020-07-16 16:42:18 +08:00
|
|
|
if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
|
2019-01-11 01:04:34 +08:00
|
|
|
goto out_err;
|
|
|
|
|
2020-03-24 23:55:37 +08:00
|
|
|
fh->type = type;
|
2020-07-16 16:42:18 +08:00
|
|
|
fh->len = fh_len;
|
2019-01-11 01:04:34 +08:00
|
|
|
|
2021-03-04 18:48:24 +08:00
|
|
|
/* Mix fh into event merge key */
|
|
|
|
*hash ^= fanotify_hash_fh(fh);
|
|
|
|
|
2020-07-16 16:42:18 +08:00
|
|
|
return FANOTIFY_FH_HDR_LEN + fh_len;
|
2019-01-11 01:04:34 +08:00
|
|
|
|
|
|
|
out_err:
|
2020-03-24 23:55:37 +08:00
|
|
|
pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
|
2020-07-16 16:42:18 +08:00
|
|
|
type, fh_len, err);
|
2020-03-24 23:55:37 +08:00
|
|
|
kfree(ext_buf);
|
|
|
|
*fanotify_fh_ext_buf_ptr(fh) = NULL;
|
|
|
|
/* Report the event without a file identifier on encode error */
|
|
|
|
fh->type = FILEID_INVALID;
|
|
|
|
fh->len = 0;
|
2020-07-16 16:42:17 +08:00
|
|
|
return 0;
|
2019-01-11 01:04:34 +08:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:04:42 +08:00
|
|
|
/*
|
|
|
|
* The inode to use as identifier when reporting fid depends on the event.
|
|
|
|
* Report the modified directory inode on dirent modification events.
|
|
|
|
* Report the "victim" inode otherwise.
|
|
|
|
* For example:
|
|
|
|
* FS_ATTRIB reports the child inode even if reported on a watched parent.
|
|
|
|
* FS_CREATE reports the modified dir inode and not the created inode.
|
|
|
|
*/
|
2020-06-07 17:10:40 +08:00
|
|
|
static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
|
|
|
|
int data_type, struct inode *dir)
|
2019-01-11 01:04:42 +08:00
|
|
|
{
|
|
|
|
if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
|
2020-06-07 17:10:40 +08:00
|
|
|
return dir;
|
2020-03-19 23:10:12 +08:00
|
|
|
|
2020-07-08 19:11:38 +08:00
|
|
|
return fsnotify_data_inode(data, data_type);
|
2019-01-11 01:04:42 +08:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:42:26 +08:00
|
|
|
/*
|
|
|
|
* The inode to use as identifier when reporting dir fid depends on the event.
|
|
|
|
* Report the modified directory inode on dirent modification events.
|
|
|
|
* Report the "victim" inode if "victim" is a directory.
|
|
|
|
* Report the parent inode if "victim" is not a directory and event is
|
|
|
|
* reported to parent.
|
|
|
|
* Otherwise, do not report dir fid.
|
|
|
|
*/
|
|
|
|
static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
|
|
|
|
int data_type, struct inode *dir)
|
|
|
|
{
|
|
|
|
struct inode *inode = fsnotify_data_inode(data, data_type);
|
|
|
|
|
|
|
|
if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
|
|
|
|
return dir;
|
|
|
|
|
|
|
|
if (S_ISDIR(inode->i_mode))
|
|
|
|
return inode;
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:55:28 +08:00
|
|
|
static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
|
2021-03-04 18:48:24 +08:00
|
|
|
unsigned int *hash,
|
2020-03-30 15:55:28 +08:00
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct fanotify_path_event *pevent;
|
|
|
|
|
|
|
|
pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
|
|
|
|
if (!pevent)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
|
|
|
|
pevent->path = *path;
|
2021-03-04 18:48:24 +08:00
|
|
|
*hash ^= fanotify_hash_path(path);
|
2020-03-30 15:55:28 +08:00
|
|
|
path_get(path);
|
|
|
|
|
|
|
|
return &pevent->fae;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
|
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct fanotify_perm_event *pevent;
|
|
|
|
|
|
|
|
pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
|
|
|
|
if (!pevent)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
|
|
|
|
pevent->response = 0;
|
|
|
|
pevent->state = FAN_EVENT_INIT;
|
|
|
|
pevent->path = *path;
|
|
|
|
path_get(path);
|
|
|
|
|
|
|
|
return &pevent->fae;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
|
|
|
|
__kernel_fsid_t *fsid,
|
2021-03-04 18:48:24 +08:00
|
|
|
unsigned int *hash,
|
2020-03-30 15:55:28 +08:00
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct fanotify_fid_event *ffe;
|
|
|
|
|
|
|
|
ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
|
|
|
|
if (!ffe)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
|
|
|
|
ffe->fsid = *fsid;
|
2021-03-04 18:48:24 +08:00
|
|
|
*hash ^= fanotify_hash_fsid(fsid);
|
2020-07-16 16:42:18 +08:00
|
|
|
fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
|
2021-03-04 18:48:24 +08:00
|
|
|
hash, gfp);
|
2020-03-30 15:55:28 +08:00
|
|
|
|
|
|
|
return &ffe->fae;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
|
|
|
|
__kernel_fsid_t *fsid,
|
2021-03-04 18:48:24 +08:00
|
|
|
const struct qstr *name,
|
2020-07-16 16:42:29 +08:00
|
|
|
struct inode *child,
|
2021-03-04 18:48:24 +08:00
|
|
|
unsigned int *hash,
|
2020-03-30 15:55:28 +08:00
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct fanotify_name_event *fne;
|
2020-07-16 16:42:17 +08:00
|
|
|
struct fanotify_info *info;
|
2020-07-16 16:42:29 +08:00
|
|
|
struct fanotify_fh *dfh, *ffh;
|
2020-07-16 16:42:18 +08:00
|
|
|
unsigned int dir_fh_len = fanotify_encode_fh_len(id);
|
2020-07-16 16:42:29 +08:00
|
|
|
unsigned int child_fh_len = fanotify_encode_fh_len(child);
|
2020-07-16 16:42:18 +08:00
|
|
|
unsigned int size;
|
2020-03-30 15:55:28 +08:00
|
|
|
|
2020-07-16 16:42:18 +08:00
|
|
|
size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len;
|
2020-07-16 16:42:29 +08:00
|
|
|
if (child_fh_len)
|
|
|
|
size += FANOTIFY_FH_HDR_LEN + child_fh_len;
|
2021-03-04 18:48:24 +08:00
|
|
|
if (name)
|
|
|
|
size += name->len + 1;
|
2020-07-16 16:42:18 +08:00
|
|
|
fne = kmalloc(size, gfp);
|
2020-03-30 15:55:28 +08:00
|
|
|
if (!fne)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
|
|
|
|
fne->fsid = *fsid;
|
2021-03-04 18:48:24 +08:00
|
|
|
*hash ^= fanotify_hash_fsid(fsid);
|
2020-07-16 16:42:17 +08:00
|
|
|
info = &fne->info;
|
|
|
|
fanotify_info_init(info);
|
|
|
|
dfh = fanotify_info_dir_fh(info);
|
2021-03-04 18:48:24 +08:00
|
|
|
info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, hash, 0);
|
2020-07-16 16:42:29 +08:00
|
|
|
if (child_fh_len) {
|
|
|
|
ffh = fanotify_info_file_fh(info);
|
2021-03-04 18:48:24 +08:00
|
|
|
info->file_fh_totlen = fanotify_encode_fh(ffh, child,
|
|
|
|
child_fh_len, hash, 0);
|
|
|
|
}
|
|
|
|
if (name) {
|
|
|
|
long salt = name->len;
|
|
|
|
|
|
|
|
fanotify_info_copy_name(info, name);
|
|
|
|
*hash ^= full_name_hash((void *)salt, name->name, name->len);
|
2020-07-16 16:42:29 +08:00
|
|
|
}
|
2020-07-16 16:42:18 +08:00
|
|
|
|
2020-07-16 16:42:29 +08:00
|
|
|
pr_debug("%s: ino=%lu size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
|
|
|
|
__func__, id->i_ino, size, dir_fh_len, child_fh_len,
|
2020-07-16 16:42:18 +08:00
|
|
|
info->name_len, info->name_len, fanotify_info_name(info));
|
2020-03-30 15:55:28 +08:00
|
|
|
|
|
|
|
return &fne->fae;
|
|
|
|
}
|
|
|
|
|
2020-07-08 19:11:42 +08:00
|
|
|
static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
|
2020-06-07 17:10:40 +08:00
|
|
|
u32 mask, const void *data,
|
|
|
|
int data_type, struct inode *dir,
|
2020-03-30 15:55:28 +08:00
|
|
|
const struct qstr *file_name,
|
|
|
|
__kernel_fsid_t *fsid)
|
2014-04-04 05:46:33 +08:00
|
|
|
{
|
2019-01-11 01:04:32 +08:00
|
|
|
struct fanotify_event *event = NULL;
|
fs: fsnotify: account fsnotify metadata to kmemcg
Patch series "Directed kmem charging", v8.
The Linux kernel's memory cgroup allows limiting the memory usage of the
jobs running on the system to provide isolation between the jobs. All
the kernel memory allocated in the context of the job and marked with
__GFP_ACCOUNT will also be included in the memory usage and be limited
by the job's limit.
The kernel memory can only be charged to the memcg of the process in
whose context kernel memory was allocated. However there are cases
where the allocated kernel memory should be charged to the memcg
different from the current processes's memcg. This patch series
contains two such concrete use-cases i.e. fsnotify and buffer_head.
The fsnotify event objects can consume a lot of system memory for large
or unlimited queues if there is either no or slow listener. The events
are allocated in the context of the event producer. However they should
be charged to the event consumer. Similarly the buffer_head objects can
be allocated in a memcg different from the memcg of the page for which
buffer_head objects are being allocated.
To solve this issue, this patch series introduces mechanism to charge
kernel memory to a given memcg. In case of fsnotify events, the memcg
of the consumer can be used for charging and for buffer_head, the memcg
of the page can be charged. For directed charging, the caller can use
the scope API memalloc_[un]use_memcg() to specify the memcg to charge
for all the __GFP_ACCOUNT allocations within the scope.
This patch (of 2):
A lot of memory can be consumed by the events generated for the huge or
unlimited queues if there is either no or slow listener. This can cause
system level memory pressure or OOMs. So, it's better to account the
fsnotify kmem caches to the memcg of the listener.
However the listener can be in a different memcg than the memcg of the
producer and these allocations happen in the context of the event
producer. This patch introduces remote memcg charging API which the
producer can use to charge the allocations to the memcg of the listener.
There are seven fsnotify kmem caches and among them allocations from
dnotify_struct_cache, dnotify_mark_cache, fanotify_mark_cache and
inotify_inode_mark_cachep happens in the context of syscall from the
listener. So, SLAB_ACCOUNT is enough for these caches.
The objects from fsnotify_mark_connector_cachep are not accounted as
they are small compared to the notification mark or events and it is
unclear whom to account connector to since it is shared by all events
attached to the inode.
The allocations from the event caches happen in the context of the event
producer. For such caches we will need to remote charge the allocations
to the listener's memcg. Thus we save the memcg reference in the
fsnotify_group structure of the listener.
This patch has also moved the members of fsnotify_group to keep the size
same, at least for 64 bit build, even with additional member by filling
the holes.
[shakeelb@google.com: use GFP_KERNEL_ACCOUNT rather than open-coding it]
Link: http://lkml.kernel.org/r/20180702215439.211597-1-shakeelb@google.com
Link: http://lkml.kernel.org/r/20180627191250.209150-2-shakeelb@google.com
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-08-18 06:46:39 +08:00
|
|
|
gfp_t gfp = GFP_KERNEL_ACCOUNT;
|
2020-06-07 17:10:40 +08:00
|
|
|
struct inode *id = fanotify_fid_inode(mask, data, data_type, dir);
|
2020-07-16 16:42:26 +08:00
|
|
|
struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
|
2020-03-19 23:10:12 +08:00
|
|
|
const struct path *path = fsnotify_data_path(data, data_type);
|
2020-07-16 16:42:12 +08:00
|
|
|
unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
|
2020-10-18 07:13:40 +08:00
|
|
|
struct mem_cgroup *old_memcg;
|
2020-07-16 16:42:29 +08:00
|
|
|
struct inode *child = NULL;
|
2020-07-08 19:11:52 +08:00
|
|
|
bool name_event = false;
|
2021-03-04 18:48:23 +08:00
|
|
|
unsigned int hash = 0;
|
2021-03-04 18:48:24 +08:00
|
|
|
bool ondir = mask & FAN_ONDIR;
|
|
|
|
struct pid *pid;
|
2018-02-21 21:10:59 +08:00
|
|
|
|
2020-07-16 16:42:28 +08:00
|
|
|
if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
|
2020-07-16 16:42:29 +08:00
|
|
|
/*
|
|
|
|
* With both flags FAN_REPORT_DIR_FID and FAN_REPORT_FID, we
|
|
|
|
* report the child fid for events reported on a non-dir child
|
2020-07-16 16:42:30 +08:00
|
|
|
* in addition to reporting the parent fid and maybe child name.
|
2020-07-16 16:42:29 +08:00
|
|
|
*/
|
2021-03-04 18:48:24 +08:00
|
|
|
if ((fid_mode & FAN_REPORT_FID) && id != dirid && !ondir)
|
2020-07-16 16:42:29 +08:00
|
|
|
child = id;
|
|
|
|
|
2020-07-16 16:42:26 +08:00
|
|
|
id = dirid;
|
|
|
|
|
2020-07-16 16:42:28 +08:00
|
|
|
/*
|
|
|
|
* We record file name only in a group with FAN_REPORT_NAME
|
|
|
|
* and when we have a directory inode to report.
|
|
|
|
*
|
|
|
|
* For directory entry modification event, we record the fid of
|
|
|
|
* the directory and the name of the modified entry.
|
|
|
|
*
|
|
|
|
* For event on non-directory that is reported to parent, we
|
|
|
|
* record the fid of the parent and the name of the child.
|
2020-07-16 16:42:30 +08:00
|
|
|
*
|
|
|
|
* Even if not reporting name, we need a variable length
|
|
|
|
* fanotify_name_event if reporting both parent and child fids.
|
2020-07-16 16:42:28 +08:00
|
|
|
*/
|
2020-07-16 16:42:30 +08:00
|
|
|
if (!(fid_mode & FAN_REPORT_NAME)) {
|
|
|
|
name_event = !!child;
|
|
|
|
file_name = NULL;
|
2021-03-04 18:48:24 +08:00
|
|
|
} else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) {
|
2020-07-16 16:42:28 +08:00
|
|
|
name_event = true;
|
2020-07-16 16:42:30 +08:00
|
|
|
}
|
2020-07-16 16:42:28 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 21:10:59 +08:00
|
|
|
/*
|
|
|
|
* For queues with unlimited length lost events are not expected and
|
|
|
|
* can possibly have security implications. Avoid losing events when
|
2019-07-12 11:55:52 +08:00
|
|
|
* memory is short. For the limited size queues, avoid OOM killer in the
|
|
|
|
* target monitoring memcg as it may have security repercussion.
|
2018-02-21 21:10:59 +08:00
|
|
|
*/
|
|
|
|
if (group->max_events == UINT_MAX)
|
|
|
|
gfp |= __GFP_NOFAIL;
|
2019-07-12 11:55:52 +08:00
|
|
|
else
|
|
|
|
gfp |= __GFP_RETRY_MAYFAIL;
|
2014-04-04 05:46:33 +08:00
|
|
|
|
fs: fsnotify: account fsnotify metadata to kmemcg
Patch series "Directed kmem charging", v8.
The Linux kernel's memory cgroup allows limiting the memory usage of the
jobs running on the system to provide isolation between the jobs. All
the kernel memory allocated in the context of the job and marked with
__GFP_ACCOUNT will also be included in the memory usage and be limited
by the job's limit.
The kernel memory can only be charged to the memcg of the process in
whose context kernel memory was allocated. However there are cases
where the allocated kernel memory should be charged to the memcg
different from the current processes's memcg. This patch series
contains two such concrete use-cases i.e. fsnotify and buffer_head.
The fsnotify event objects can consume a lot of system memory for large
or unlimited queues if there is either no or slow listener. The events
are allocated in the context of the event producer. However they should
be charged to the event consumer. Similarly the buffer_head objects can
be allocated in a memcg different from the memcg of the page for which
buffer_head objects are being allocated.
To solve this issue, this patch series introduces mechanism to charge
kernel memory to a given memcg. In case of fsnotify events, the memcg
of the consumer can be used for charging and for buffer_head, the memcg
of the page can be charged. For directed charging, the caller can use
the scope API memalloc_[un]use_memcg() to specify the memcg to charge
for all the __GFP_ACCOUNT allocations within the scope.
This patch (of 2):
A lot of memory can be consumed by the events generated for the huge or
unlimited queues if there is either no or slow listener. This can cause
system level memory pressure or OOMs. So, it's better to account the
fsnotify kmem caches to the memcg of the listener.
However the listener can be in a different memcg than the memcg of the
producer and these allocations happen in the context of the event
producer. This patch introduces remote memcg charging API which the
producer can use to charge the allocations to the memcg of the listener.
There are seven fsnotify kmem caches and among them allocations from
dnotify_struct_cache, dnotify_mark_cache, fanotify_mark_cache and
inotify_inode_mark_cachep happens in the context of syscall from the
listener. So, SLAB_ACCOUNT is enough for these caches.
The objects from fsnotify_mark_connector_cachep are not accounted as
they are small compared to the notification mark or events and it is
unclear whom to account connector to since it is shared by all events
attached to the inode.
The allocations from the event caches happen in the context of the event
producer. For such caches we will need to remote charge the allocations
to the listener's memcg. Thus we save the memcg reference in the
fsnotify_group structure of the listener.
This patch has also moved the members of fsnotify_group to keep the size
same, at least for 64 bit build, even with additional member by filling
the holes.
[shakeelb@google.com: use GFP_KERNEL_ACCOUNT rather than open-coding it]
Link: http://lkml.kernel.org/r/20180702215439.211597-1-shakeelb@google.com
Link: http://lkml.kernel.org/r/20180627191250.209150-2-shakeelb@google.com
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-08-18 06:46:39 +08:00
|
|
|
/* Whoever is interested in the event, pays for the allocation. */
|
2020-10-18 07:13:40 +08:00
|
|
|
old_memcg = set_active_memcg(group->memcg);
|
fs: fsnotify: account fsnotify metadata to kmemcg
Patch series "Directed kmem charging", v8.
The Linux kernel's memory cgroup allows limiting the memory usage of the
jobs running on the system to provide isolation between the jobs. All
the kernel memory allocated in the context of the job and marked with
__GFP_ACCOUNT will also be included in the memory usage and be limited
by the job's limit.
The kernel memory can only be charged to the memcg of the process in
whose context kernel memory was allocated. However there are cases
where the allocated kernel memory should be charged to the memcg
different from the current processes's memcg. This patch series
contains two such concrete use-cases i.e. fsnotify and buffer_head.
The fsnotify event objects can consume a lot of system memory for large
or unlimited queues if there is either no or slow listener. The events
are allocated in the context of the event producer. However they should
be charged to the event consumer. Similarly the buffer_head objects can
be allocated in a memcg different from the memcg of the page for which
buffer_head objects are being allocated.
To solve this issue, this patch series introduces mechanism to charge
kernel memory to a given memcg. In case of fsnotify events, the memcg
of the consumer can be used for charging and for buffer_head, the memcg
of the page can be charged. For directed charging, the caller can use
the scope API memalloc_[un]use_memcg() to specify the memcg to charge
for all the __GFP_ACCOUNT allocations within the scope.
This patch (of 2):
A lot of memory can be consumed by the events generated for the huge or
unlimited queues if there is either no or slow listener. This can cause
system level memory pressure or OOMs. So, it's better to account the
fsnotify kmem caches to the memcg of the listener.
However the listener can be in a different memcg than the memcg of the
producer and these allocations happen in the context of the event
producer. This patch introduces remote memcg charging API which the
producer can use to charge the allocations to the memcg of the listener.
There are seven fsnotify kmem caches and among them allocations from
dnotify_struct_cache, dnotify_mark_cache, fanotify_mark_cache and
inotify_inode_mark_cachep happens in the context of syscall from the
listener. So, SLAB_ACCOUNT is enough for these caches.
The objects from fsnotify_mark_connector_cachep are not accounted as
they are small compared to the notification mark or events and it is
unclear whom to account connector to since it is shared by all events
attached to the inode.
The allocations from the event caches happen in the context of the event
producer. For such caches we will need to remote charge the allocations
to the listener's memcg. Thus we save the memcg reference in the
fsnotify_group structure of the listener.
This patch has also moved the members of fsnotify_group to keep the size
same, at least for 64 bit build, even with additional member by filling
the holes.
[shakeelb@google.com: use GFP_KERNEL_ACCOUNT rather than open-coding it]
Link: http://lkml.kernel.org/r/20180702215439.211597-1-shakeelb@google.com
Link: http://lkml.kernel.org/r/20180627191250.209150-2-shakeelb@google.com
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-08-18 06:46:39 +08:00
|
|
|
|
2017-10-31 04:14:56 +08:00
|
|
|
if (fanotify_is_perm_event(mask)) {
|
2020-03-30 15:55:28 +08:00
|
|
|
event = fanotify_alloc_perm_event(path, gfp);
|
2020-07-16 16:42:30 +08:00
|
|
|
} else if (name_event && (file_name || child)) {
|
2020-07-16 16:42:29 +08:00
|
|
|
event = fanotify_alloc_name_event(id, fsid, file_name, child,
|
2021-03-04 18:48:24 +08:00
|
|
|
&hash, gfp);
|
2020-07-16 16:42:12 +08:00
|
|
|
} else if (fid_mode) {
|
2021-03-04 18:48:24 +08:00
|
|
|
event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
|
2020-03-25 00:04:20 +08:00
|
|
|
} else {
|
2021-03-04 18:48:24 +08:00
|
|
|
event = fanotify_alloc_path_event(path, &hash, gfp);
|
2020-03-25 00:04:20 +08:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:55:28 +08:00
|
|
|
if (!event)
|
|
|
|
goto out;
|
|
|
|
|
2018-10-04 05:25:38 +08:00
|
|
|
if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
|
2021-03-04 18:48:24 +08:00
|
|
|
pid = get_pid(task_pid(current));
|
2018-10-04 05:25:38 +08:00
|
|
|
else
|
2021-03-04 18:48:24 +08:00
|
|
|
pid = get_pid(task_tgid(current));
|
|
|
|
|
|
|
|
/* Mix event info, FAN_ONDIR flag and pid into event merge key */
|
|
|
|
hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
|
|
|
|
fanotify_init_event(event, hash, mask);
|
|
|
|
event->pid = pid;
|
2020-03-25 00:04:20 +08:00
|
|
|
|
fs: fsnotify: account fsnotify metadata to kmemcg
Patch series "Directed kmem charging", v8.
The Linux kernel's memory cgroup allows limiting the memory usage of the
jobs running on the system to provide isolation between the jobs. All
the kernel memory allocated in the context of the job and marked with
__GFP_ACCOUNT will also be included in the memory usage and be limited
by the job's limit.
The kernel memory can only be charged to the memcg of the process in
whose context kernel memory was allocated. However there are cases
where the allocated kernel memory should be charged to the memcg
different from the current processes's memcg. This patch series
contains two such concrete use-cases i.e. fsnotify and buffer_head.
The fsnotify event objects can consume a lot of system memory for large
or unlimited queues if there is either no or slow listener. The events
are allocated in the context of the event producer. However they should
be charged to the event consumer. Similarly the buffer_head objects can
be allocated in a memcg different from the memcg of the page for which
buffer_head objects are being allocated.
To solve this issue, this patch series introduces mechanism to charge
kernel memory to a given memcg. In case of fsnotify events, the memcg
of the consumer can be used for charging and for buffer_head, the memcg
of the page can be charged. For directed charging, the caller can use
the scope API memalloc_[un]use_memcg() to specify the memcg to charge
for all the __GFP_ACCOUNT allocations within the scope.
This patch (of 2):
A lot of memory can be consumed by the events generated for the huge or
unlimited queues if there is either no or slow listener. This can cause
system level memory pressure or OOMs. So, it's better to account the
fsnotify kmem caches to the memcg of the listener.
However the listener can be in a different memcg than the memcg of the
producer and these allocations happen in the context of the event
producer. This patch introduces remote memcg charging API which the
producer can use to charge the allocations to the memcg of the listener.
There are seven fsnotify kmem caches and among them allocations from
dnotify_struct_cache, dnotify_mark_cache, fanotify_mark_cache and
inotify_inode_mark_cachep happens in the context of syscall from the
listener. So, SLAB_ACCOUNT is enough for these caches.
The objects from fsnotify_mark_connector_cachep are not accounted as
they are small compared to the notification mark or events and it is
unclear whom to account connector to since it is shared by all events
attached to the inode.
The allocations from the event caches happen in the context of the event
producer. For such caches we will need to remote charge the allocations
to the listener's memcg. Thus we save the memcg reference in the
fsnotify_group structure of the listener.
This patch has also moved the members of fsnotify_group to keep the size
same, at least for 64 bit build, even with additional member by filling
the holes.
[shakeelb@google.com: use GFP_KERNEL_ACCOUNT rather than open-coding it]
Link: http://lkml.kernel.org/r/20180702215439.211597-1-shakeelb@google.com
Link: http://lkml.kernel.org/r/20180627191250.209150-2-shakeelb@google.com
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-08-18 06:46:39 +08:00
|
|
|
out:
|
2020-10-18 07:13:40 +08:00
|
|
|
set_active_memcg(old_memcg);
|
2014-04-04 05:46:33 +08:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2019-01-11 01:04:37 +08:00
|
|
|
/*
|
|
|
|
* Get cached fsid of the filesystem containing the object from any connector.
|
|
|
|
* All connectors are supposed to have the same fsid, but we do not verify that
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
__kernel_fsid_t fsid = {};
|
|
|
|
|
|
|
|
fsnotify_foreach_obj_type(type) {
|
2019-04-25 00:39:57 +08:00
|
|
|
struct fsnotify_mark_connector *conn;
|
|
|
|
|
2019-01-11 01:04:37 +08:00
|
|
|
if (!fsnotify_iter_should_report_type(iter_info, type))
|
|
|
|
continue;
|
|
|
|
|
2019-04-25 00:39:57 +08:00
|
|
|
conn = READ_ONCE(iter_info->marks[type]->connector);
|
|
|
|
/* Mark is just getting destroyed or created? */
|
|
|
|
if (!conn)
|
|
|
|
continue;
|
2019-06-19 18:34:44 +08:00
|
|
|
if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
|
|
|
|
continue;
|
|
|
|
/* Pairs with smp_wmb() in fsnotify_add_mark_list() */
|
|
|
|
smp_rmb();
|
2019-04-25 00:39:57 +08:00
|
|
|
fsid = conn->fsid;
|
2019-01-11 01:04:37 +08:00
|
|
|
if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
|
|
|
|
continue;
|
|
|
|
return fsid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fsid;
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:48:25 +08:00
|
|
|
/*
|
|
|
|
* Add an event to hash table for faster merge.
|
|
|
|
*/
|
|
|
|
static void fanotify_insert_event(struct fsnotify_group *group,
|
|
|
|
struct fsnotify_event *fsn_event)
|
|
|
|
{
|
|
|
|
struct fanotify_event *event = FANOTIFY_E(fsn_event);
|
|
|
|
unsigned int bucket = fanotify_event_hash_bucket(group, event);
|
|
|
|
struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
|
|
|
|
|
|
|
|
assert_spin_locked(&group->notification_lock);
|
|
|
|
|
|
|
|
pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
|
|
|
|
group, event, bucket);
|
|
|
|
|
|
|
|
hlist_add_head(&event->merge_list, hlist);
|
|
|
|
}
|
|
|
|
|
2020-06-07 17:10:40 +08:00
|
|
|
static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
|
|
|
|
const void *data, int data_type,
|
|
|
|
struct inode *dir,
|
2019-04-27 01:51:03 +08:00
|
|
|
const struct qstr *file_name, u32 cookie,
|
2016-11-11 00:51:50 +08:00
|
|
|
struct fsnotify_iter_info *iter_info)
|
2014-01-22 07:48:14 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2019-01-11 01:04:32 +08:00
|
|
|
struct fanotify_event *event;
|
2014-01-22 07:48:14 +08:00
|
|
|
struct fsnotify_event *fsn_event;
|
2019-01-11 01:04:37 +08:00
|
|
|
__kernel_fsid_t fsid = {};
|
2014-01-22 07:48:14 +08:00
|
|
|
|
|
|
|
BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
|
|
|
|
BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
|
2019-01-11 01:04:43 +08:00
|
|
|
BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
|
2014-01-22 07:48:14 +08:00
|
|
|
BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
|
|
|
|
BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
|
|
|
|
BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
|
2019-01-11 01:04:43 +08:00
|
|
|
BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
|
|
|
|
BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
|
|
|
|
BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
|
|
|
|
BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
|
|
|
|
BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
|
|
|
|
BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
|
2014-01-22 07:48:14 +08:00
|
|
|
BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
|
|
|
|
BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
|
|
|
|
BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
|
|
|
|
BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
|
|
|
|
BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
|
2018-11-08 11:07:14 +08:00
|
|
|
BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
|
2018-11-08 11:12:44 +08:00
|
|
|
BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
|
2014-01-22 07:48:14 +08:00
|
|
|
|
2020-05-27 20:54:55 +08:00
|
|
|
BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
|
2018-10-04 05:25:37 +08:00
|
|
|
|
2019-01-11 01:04:42 +08:00
|
|
|
mask = fanotify_group_event_mask(group, iter_info, mask, data,
|
2020-07-16 16:42:26 +08:00
|
|
|
data_type, dir);
|
fanotify: return only user requested event types in event mask
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 <mbobrowski@mbobrowski.org>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
2018-11-08 11:05:49 +08:00
|
|
|
if (!mask)
|
2014-01-22 07:48:15 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-06-07 17:10:40 +08:00
|
|
|
pr_debug("%s: group=%p mask=%x\n", __func__, group, mask);
|
2014-01-22 07:48:14 +08:00
|
|
|
|
2017-10-31 04:14:56 +08:00
|
|
|
if (fanotify_is_perm_event(mask)) {
|
2017-10-31 04:14:56 +08:00
|
|
|
/*
|
|
|
|
* fsnotify_prepare_user_wait() fails if we race with mark
|
|
|
|
* deletion. Just let the operation pass in that case.
|
|
|
|
*/
|
|
|
|
if (!fsnotify_prepare_user_wait(iter_info))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:42:12 +08:00
|
|
|
if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
|
2019-01-11 01:04:37 +08:00
|
|
|
fsid = fanotify_get_fsid(iter_info);
|
2019-04-25 00:39:57 +08:00
|
|
|
/* Racing with mark destruction or creation? */
|
|
|
|
if (!fsid.val[0] && !fsid.val[1])
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-11 01:04:37 +08:00
|
|
|
|
2020-06-07 17:10:40 +08:00
|
|
|
event = fanotify_alloc_event(group, mask, data, data_type, dir,
|
2020-03-19 23:10:21 +08:00
|
|
|
file_name, &fsid);
|
2017-10-31 04:14:56 +08:00
|
|
|
ret = -ENOMEM;
|
2018-02-21 22:07:52 +08:00
|
|
|
if (unlikely(!event)) {
|
|
|
|
/*
|
|
|
|
* We don't queue overflow events for permission events as
|
|
|
|
* there the access is denied and so no event is in fact lost.
|
|
|
|
*/
|
|
|
|
if (!fanotify_is_perm_event(mask))
|
|
|
|
fsnotify_queue_overflow(group);
|
2017-10-31 04:14:56 +08:00
|
|
|
goto finish;
|
2018-02-21 22:07:52 +08:00
|
|
|
}
|
2014-01-22 07:48:14 +08:00
|
|
|
|
|
|
|
fsn_event = &event->fse;
|
2021-03-04 18:48:25 +08:00
|
|
|
ret = fsnotify_add_event(group, fsn_event, fanotify_merge,
|
|
|
|
fanotify_is_hashed_event(mask) ?
|
|
|
|
fanotify_insert_event : NULL);
|
2014-01-29 01:53:22 +08:00
|
|
|
if (ret) {
|
2014-02-22 02:07:54 +08:00
|
|
|
/* Permission events shouldn't be merged */
|
2018-10-04 05:25:35 +08:00
|
|
|
BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
|
2014-01-22 07:48:14 +08:00
|
|
|
/* Our event wasn't used in the end. Free it. */
|
|
|
|
fsnotify_destroy_event(group, fsn_event);
|
2014-02-22 02:07:54 +08:00
|
|
|
|
2017-10-31 04:14:56 +08:00
|
|
|
ret = 0;
|
2017-10-31 04:14:56 +08:00
|
|
|
} else if (fanotify_is_perm_event(mask)) {
|
2020-03-25 00:04:20 +08:00
|
|
|
ret = fanotify_get_response(group, FANOTIFY_PERM(event),
|
2016-11-11 00:45:16 +08:00
|
|
|
iter_info);
|
2014-01-29 04:38:06 +08:00
|
|
|
}
|
2017-10-31 04:14:56 +08:00
|
|
|
finish:
|
2017-10-31 04:14:56 +08:00
|
|
|
if (fanotify_is_perm_event(mask))
|
2017-10-31 04:14:56 +08:00
|
|
|
fsnotify_finish_user_wait(iter_info);
|
2017-10-31 04:14:56 +08:00
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-10-29 05:21:58 +08:00
|
|
|
static void fanotify_free_group_priv(struct fsnotify_group *group)
|
|
|
|
{
|
2021-03-04 18:48:25 +08:00
|
|
|
kfree(group->fanotify_data.merge_hash);
|
2021-03-04 19:29:20 +08:00
|
|
|
if (group->fanotify_data.ucounts)
|
|
|
|
dec_ucount(group->fanotify_data.ucounts,
|
|
|
|
UCOUNT_FANOTIFY_GROUPS);
|
2010-10-29 05:21:58 +08:00
|
|
|
}
|
|
|
|
|
2020-03-25 00:04:20 +08:00
|
|
|
static void fanotify_free_path_event(struct fanotify_event *event)
|
|
|
|
{
|
|
|
|
path_put(fanotify_event_path(event));
|
|
|
|
kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fanotify_free_perm_event(struct fanotify_event *event)
|
|
|
|
{
|
|
|
|
path_put(fanotify_event_path(event));
|
|
|
|
kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fanotify_free_fid_event(struct fanotify_event *event)
|
|
|
|
{
|
|
|
|
struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
|
|
|
|
|
|
|
|
if (fanotify_fh_has_ext_buf(&ffe->object_fh))
|
|
|
|
kfree(fanotify_fh_ext_buf(&ffe->object_fh));
|
|
|
|
kmem_cache_free(fanotify_fid_event_cachep, ffe);
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:10:21 +08:00
|
|
|
static void fanotify_free_name_event(struct fanotify_event *event)
|
|
|
|
{
|
2020-07-16 16:42:18 +08:00
|
|
|
kfree(FANOTIFY_NE(event));
|
2020-03-19 23:10:21 +08:00
|
|
|
}
|
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
static void fanotify_free_event(struct fsnotify_event *fsn_event)
|
|
|
|
{
|
2019-01-11 01:04:32 +08:00
|
|
|
struct fanotify_event *event;
|
2014-01-22 07:48:14 +08:00
|
|
|
|
|
|
|
event = FANOTIFY_E(fsn_event);
|
2018-10-04 05:25:38 +08:00
|
|
|
put_pid(event->pid);
|
2020-03-25 00:04:20 +08:00
|
|
|
switch (event->type) {
|
|
|
|
case FANOTIFY_EVENT_TYPE_PATH:
|
|
|
|
fanotify_free_path_event(event);
|
|
|
|
break;
|
|
|
|
case FANOTIFY_EVENT_TYPE_PATH_PERM:
|
|
|
|
fanotify_free_perm_event(event);
|
|
|
|
break;
|
|
|
|
case FANOTIFY_EVENT_TYPE_FID:
|
|
|
|
fanotify_free_fid_event(event);
|
|
|
|
break;
|
2020-03-19 23:10:21 +08:00
|
|
|
case FANOTIFY_EVENT_TYPE_FID_NAME:
|
|
|
|
fanotify_free_name_event(event);
|
|
|
|
break;
|
2020-07-08 19:11:42 +08:00
|
|
|
case FANOTIFY_EVENT_TYPE_OVERFLOW:
|
|
|
|
kfree(event);
|
|
|
|
break;
|
2020-03-25 00:04:20 +08:00
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
2014-04-04 05:46:33 +08:00
|
|
|
}
|
2014-01-22 07:48:14 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 19:29:20 +08:00
|
|
|
static void fanotify_freeing_mark(struct fsnotify_mark *mark,
|
|
|
|
struct fsnotify_group *group)
|
|
|
|
{
|
|
|
|
if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
|
|
|
|
dec_ucount(group->fanotify_data.ucounts, UCOUNT_FANOTIFY_MARKS);
|
|
|
|
}
|
|
|
|
|
2016-12-22 01:06:12 +08:00
|
|
|
static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
|
|
|
|
{
|
|
|
|
kmem_cache_free(fanotify_mark_cache, fsn_mark);
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:25 +08:00
|
|
|
const struct fsnotify_ops fanotify_fsnotify_ops = {
|
|
|
|
.handle_event = fanotify_handle_event,
|
2010-10-29 05:21:58 +08:00
|
|
|
.free_group_priv = fanotify_free_group_priv,
|
2014-01-22 07:48:14 +08:00
|
|
|
.free_event = fanotify_free_event,
|
2021-03-04 19:29:20 +08:00
|
|
|
.freeing_mark = fanotify_freeing_mark,
|
2016-12-22 01:06:12 +08:00
|
|
|
.free_mark = fanotify_free_mark,
|
2009-12-18 10:24:25 +08:00
|
|
|
};
|