blob: 985e995d2a3984f2d1808e64e23628b5937ecb12 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05002#include <linux/fanotify.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05003#include <linux/fdtable.h>
4#include <linux/fsnotify_backend.h>
5#include <linux/init.h>
Eric Paris9e66e422009-12-17 21:24:34 -05006#include <linux/jiffies.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05007#include <linux/kernel.h> /* UINT_MAX */
Eric Paris1c529062009-12-17 21:24:28 -05008#include <linux/mount.h>
Eric Paris9e66e422009-12-17 21:24:34 -05009#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <linux/sched/user.h>
Eric W. Biederman7a360942017-09-26 12:45:33 -050011#include <linux/sched/signal.h>
Eric Parisff0b16a2009-12-17 21:24:25 -050012#include <linux/types.h>
Eric Paris9e66e422009-12-17 21:24:34 -050013#include <linux/wait.h>
Steve Grubbde8cd832017-10-02 20:21:39 -040014#include <linux/audit.h>
Shakeel Buttd46eb14b2018-08-17 15:46:39 -070015#include <linux/sched/mm.h>
Amir Goldsteine9e0c892019-01-10 19:04:34 +020016#include <linux/statfs.h>
Amir Goldstein7e3e5c62021-03-04 12:48:24 +020017#include <linux/stringhash.h>
Eric Parisff0b16a2009-12-17 21:24:25 -050018
Jan Kara7053aee2014-01-21 15:48:14 -080019#include "fanotify.h"
Eric Paris767cd462009-12-17 21:24:25 -050020
Jan Karaafc894c2020-03-24 16:55:37 +010021static bool fanotify_path_equal(struct path *p1, struct path *p2)
22{
23 return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
24}
25
Amir Goldstein7e3e5c62021-03-04 12:48:24 +020026static unsigned int fanotify_hash_path(const struct path *path)
27{
28 return hash_ptr(path->dentry, FANOTIFY_EVENT_HASH_BITS) ^
29 hash_ptr(path->mnt, FANOTIFY_EVENT_HASH_BITS);
30}
31
Jan Karaafc894c2020-03-24 16:55:37 +010032static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
33 __kernel_fsid_t *fsid2)
34{
Nathan Chancellor6def1a12020-03-27 10:10:30 -070035 return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
Jan Karaafc894c2020-03-24 16:55:37 +010036}
37
Amir Goldstein7e3e5c62021-03-04 12:48:24 +020038static unsigned int fanotify_hash_fsid(__kernel_fsid_t *fsid)
39{
40 return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^
41 hash_32(fsid->val[1], FANOTIFY_EVENT_HASH_BITS);
42}
43
Jan Karaafc894c2020-03-24 16:55:37 +010044static bool fanotify_fh_equal(struct fanotify_fh *fh1,
Jan Kara7088f352020-03-24 17:04:20 +010045 struct fanotify_fh *fh2)
Jan Karaafc894c2020-03-24 16:55:37 +010046{
47 if (fh1->type != fh2->type || fh1->len != fh2->len)
48 return false;
49
Jan Karaafc894c2020-03-24 16:55:37 +010050 return !fh1->len ||
51 !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
52}
53
Amir Goldstein7e3e5c62021-03-04 12:48:24 +020054static unsigned int fanotify_hash_fh(struct fanotify_fh *fh)
55{
56 long salt = (long)fh->type | (long)fh->len << 8;
57
58 /*
59 * full_name_hash() works long by long, so it handles fh buf optimally.
60 */
61 return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len);
62}
63
Jan Kara7088f352020-03-24 17:04:20 +010064static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
65 struct fanotify_fid_event *ffe2)
66{
67 /* Do not merge fid events without object fh */
68 if (!ffe1->object_fh.len)
69 return false;
70
71 return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
72 fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
73}
74
Amir Goldsteinf454fa62020-07-16 11:42:17 +030075static bool fanotify_info_equal(struct fanotify_info *info1,
76 struct fanotify_info *info2)
77{
78 if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
Amir Goldstein3cf984e2021-11-29 22:15:33 +020079 info1->dir2_fh_totlen != info2->dir2_fh_totlen ||
Amir Goldsteinf454fa62020-07-16 11:42:17 +030080 info1->file_fh_totlen != info2->file_fh_totlen ||
Amir Goldstein3cf984e2021-11-29 22:15:33 +020081 info1->name_len != info2->name_len ||
82 info1->name2_len != info2->name2_len)
Amir Goldsteinf454fa62020-07-16 11:42:17 +030083 return false;
84
85 if (info1->dir_fh_totlen &&
86 !fanotify_fh_equal(fanotify_info_dir_fh(info1),
87 fanotify_info_dir_fh(info2)))
88 return false;
89
Amir Goldstein3cf984e2021-11-29 22:15:33 +020090 if (info1->dir2_fh_totlen &&
91 !fanotify_fh_equal(fanotify_info_dir2_fh(info1),
92 fanotify_info_dir2_fh(info2)))
93 return false;
94
Amir Goldsteinf454fa62020-07-16 11:42:17 +030095 if (info1->file_fh_totlen &&
96 !fanotify_fh_equal(fanotify_info_file_fh(info1),
97 fanotify_info_file_fh(info2)))
98 return false;
99
Amir Goldstein3cf984e2021-11-29 22:15:33 +0200100 if (info1->name_len &&
101 memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
102 info1->name_len))
103 return false;
104
105 return !info1->name2_len ||
106 !memcmp(fanotify_info_name2(info1), fanotify_info_name2(info2),
107 info1->name2_len);
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300108}
109
Amir Goldsteincacfb952020-03-19 17:10:21 +0200110static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
111 struct fanotify_name_event *fne2)
112{
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300113 struct fanotify_info *info1 = &fne1->info;
114 struct fanotify_info *info2 = &fne2->info;
115
Amir Goldstein6ad1aad2020-07-16 11:42:11 +0300116 /* Do not merge name events without dir fh */
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300117 if (!info1->dir_fh_totlen)
Amir Goldsteincacfb952020-03-19 17:10:21 +0200118 return false;
119
Jan Kara8aed8ce2020-07-28 10:58:07 +0200120 if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
121 return false;
122
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300123 return fanotify_info_equal(info1, info2);
Amir Goldsteincacfb952020-03-19 17:10:21 +0200124}
125
Gabriel Krisman Bertazi8a6ae642021-10-25 16:27:36 -0300126static bool fanotify_error_event_equal(struct fanotify_error_event *fee1,
127 struct fanotify_error_event *fee2)
128{
129 /* Error events against the same file system are always merged. */
130 if (!fanotify_fsid_equal(&fee1->fsid, &fee2->fsid))
131 return false;
132
133 return true;
134}
135
Amir Goldstein8988f112021-03-04 12:48:23 +0200136static bool fanotify_should_merge(struct fanotify_event *old,
137 struct fanotify_event *new)
Jan Kara7053aee2014-01-21 15:48:14 -0800138{
Amir Goldstein8988f112021-03-04 12:48:23 +0200139 pr_debug("%s: old=%p new=%p\n", __func__, old, new);
Jan Kara7053aee2014-01-21 15:48:14 -0800140
Amir Goldstein8988f112021-03-04 12:48:23 +0200141 if (old->hash != new->hash ||
Jan Kara7088f352020-03-24 17:04:20 +0100142 old->type != new->type || old->pid != new->pid)
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200143 return false;
144
Amir Goldstein103ff6a2020-07-16 11:42:10 +0300145 /*
146 * We want to merge many dirent events in the same dir (i.e.
147 * creates/unlinks/renames), but we do not want to merge dirent
148 * events referring to subdirs with dirent events referring to
149 * non subdirs, otherwise, user won't be able to tell from a
150 * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
151 * unlink pair or rmdir+create pair of events.
152 */
153 if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
154 return false;
155
Amir Goldstein7326e3822021-11-29 22:15:36 +0200156 /*
157 * FAN_RENAME event is reported with special info record types,
158 * so we cannot merge it with other events.
159 */
160 if ((old->mask & FAN_RENAME) != (new->mask & FAN_RENAME))
161 return false;
162
Jan Kara7088f352020-03-24 17:04:20 +0100163 switch (old->type) {
164 case FANOTIFY_EVENT_TYPE_PATH:
Jan Karaafc894c2020-03-24 16:55:37 +0100165 return fanotify_path_equal(fanotify_event_path(old),
166 fanotify_event_path(new));
Jan Kara7088f352020-03-24 17:04:20 +0100167 case FANOTIFY_EVENT_TYPE_FID:
Jan Kara7088f352020-03-24 17:04:20 +0100168 return fanotify_fid_event_equal(FANOTIFY_FE(old),
169 FANOTIFY_FE(new));
Amir Goldsteincacfb952020-03-19 17:10:21 +0200170 case FANOTIFY_EVENT_TYPE_FID_NAME:
171 return fanotify_name_event_equal(FANOTIFY_NE(old),
172 FANOTIFY_NE(new));
Gabriel Krisman Bertazi8a6ae642021-10-25 16:27:36 -0300173 case FANOTIFY_EVENT_TYPE_FS_ERROR:
174 return fanotify_error_event_equal(FANOTIFY_EE(old),
175 FANOTIFY_EE(new));
Jan Kara7088f352020-03-24 17:04:20 +0100176 default:
177 WARN_ON_ONCE(1);
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200178 }
179
Eric Paris767cd462009-12-17 21:24:25 -0500180 return false;
181}
182
Amir Goldsteinb8cd0ee2021-03-04 12:48:26 +0200183/* Limit event merges to limit CPU overhead per event */
184#define FANOTIFY_MAX_MERGE_EVENTS 128
185
Eric Parisf70ab542010-07-28 10:18:37 -0400186/* and the list better be locked by something too! */
Amir Goldstein94e00d22021-03-04 12:48:25 +0200187static int fanotify_merge(struct fsnotify_group *group,
188 struct fsnotify_event *event)
Eric Paris767cd462009-12-17 21:24:25 -0500189{
Amir Goldstein8988f112021-03-04 12:48:23 +0200190 struct fanotify_event *old, *new = FANOTIFY_E(event);
Amir Goldstein94e00d22021-03-04 12:48:25 +0200191 unsigned int bucket = fanotify_event_hash_bucket(group, new);
192 struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
Amir Goldsteinb8cd0ee2021-03-04 12:48:26 +0200193 int i = 0;
Eric Paris767cd462009-12-17 21:24:25 -0500194
Amir Goldstein94e00d22021-03-04 12:48:25 +0200195 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
196 group, event, bucket);
Eric Paris767cd462009-12-17 21:24:25 -0500197
Jan Kara13116df2014-01-28 18:29:24 +0100198 /*
199 * Don't merge a permission event with any other event so that we know
200 * the event structure we have created in fanotify_handle_event() is the
201 * one we should check for permission response.
202 */
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200203 if (fanotify_is_perm_event(new->mask))
Jan Kara83c0e1b2014-01-28 18:53:22 +0100204 return 0;
Jan Kara13116df2014-01-28 18:29:24 +0100205
Amir Goldstein94e00d22021-03-04 12:48:25 +0200206 hlist_for_each_entry(old, hlist, merge_list) {
Amir Goldsteinb8cd0ee2021-03-04 12:48:26 +0200207 if (++i > FANOTIFY_MAX_MERGE_EVENTS)
208 break;
Amir Goldstein8988f112021-03-04 12:48:23 +0200209 if (fanotify_should_merge(old, new)) {
210 old->mask |= new->mask;
Gabriel Krisman Bertazi8a6ae642021-10-25 16:27:36 -0300211
212 if (fanotify_is_error_event(old->mask))
213 FANOTIFY_EE(old)->err_count++;
214
Kinglong Mee6c711002017-02-09 20:45:22 +0800215 return 1;
Eric Parisa12a7dd2009-12-17 21:24:25 -0500216 }
217 }
Eric Parisf70ab542010-07-28 10:18:37 -0400218
Kinglong Mee6c711002017-02-09 20:45:22 +0800219 return 0;
Eric Paris767cd462009-12-17 21:24:25 -0500220}
221
Jan Karafabf7f22019-01-08 15:18:02 +0100222/*
223 * Wait for response to permission event. The function also takes care of
224 * freeing the permission event (or offloads that in case the wait is canceled
225 * by a signal). The function returns 0 in case access got allowed by userspace,
226 * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
227 * the wait got interrupted by a signal.
228 */
Jan Karaf0834412014-04-03 14:46:33 -0700229static int fanotify_get_response(struct fsnotify_group *group,
Amir Goldstein33913992019-01-10 19:04:32 +0200230 struct fanotify_perm_event *event,
Jan Kara05f0e382016-11-10 17:45:16 +0100231 struct fsnotify_iter_info *iter_info)
Eric Paris9e66e422009-12-17 21:24:34 -0500232{
233 int ret;
234
235 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
236
Jan Karab5190572019-02-21 11:47:23 +0100237 ret = wait_event_killable(group->fanotify_data.access_waitq,
238 event->state == FAN_EVENT_ANSWERED);
Jan Karafabf7f22019-01-08 15:18:02 +0100239 /* Signal pending? */
240 if (ret < 0) {
241 spin_lock(&group->notification_lock);
242 /* Event reported to userspace and no answer yet? */
243 if (event->state == FAN_EVENT_REPORTED) {
244 /* Event will get freed once userspace answers to it */
245 event->state = FAN_EVENT_CANCELED;
246 spin_unlock(&group->notification_lock);
247 return ret;
248 }
249 /* Event not yet reported? Just remove it. */
Amir Goldstein94e00d22021-03-04 12:48:25 +0200250 if (event->state == FAN_EVENT_INIT) {
Jan Karafabf7f22019-01-08 15:18:02 +0100251 fsnotify_remove_queued_event(group, &event->fae.fse);
Amir Goldstein94e00d22021-03-04 12:48:25 +0200252 /* Permission events are not supposed to be hashed */
253 WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
254 }
Jan Karafabf7f22019-01-08 15:18:02 +0100255 /*
256 * Event may be also answered in case signal delivery raced
257 * with wakeup. In that case we have nothing to do besides
258 * freeing the event and reporting error.
259 */
260 spin_unlock(&group->notification_lock);
261 goto out;
262 }
Eric Paris9e66e422009-12-17 21:24:34 -0500263
264 /* userspace responded, convert to something usable */
Steve Grubbde8cd832017-10-02 20:21:39 -0400265 switch (event->response & ~FAN_AUDIT) {
Eric Paris9e66e422009-12-17 21:24:34 -0500266 case FAN_ALLOW:
267 ret = 0;
268 break;
269 case FAN_DENY:
270 default:
271 ret = -EPERM;
272 }
Steve Grubbde8cd832017-10-02 20:21:39 -0400273
274 /* Check if the response should be audited */
275 if (event->response & FAN_AUDIT)
276 audit_fanotify(event->response & ~FAN_AUDIT);
277
Eric Parisb2d87902009-12-17 21:24:34 -0500278 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
279 group, event, ret);
Jan Karafabf7f22019-01-08 15:18:02 +0100280out:
281 fsnotify_destroy_event(group, &event->fae.fse);
Amir Goldstein83b535d2019-01-10 19:04:42 +0200282
Eric Paris9e66e422009-12-17 21:24:34 -0500283 return ret;
284}
Eric Paris9e66e422009-12-17 21:24:34 -0500285
Matthew Bobrowski2d10b232018-11-08 14:05:49 +1100286/*
287 * This function returns a mask for an event that only contains the flags
288 * that have been specifically requested by the user. Flags that may have
289 * been included within the event mask, but have not been explicitly
290 * requested by the user, will not be present in the returned mask.
291 */
Amir Goldstein83b535d2019-01-10 19:04:42 +0200292static u32 fanotify_group_event_mask(struct fsnotify_group *group,
293 struct fsnotify_iter_info *iter_info,
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200294 u32 *match_mask, u32 event_mask,
295 const void *data, int data_type,
296 struct inode *dir)
Eric Paris1c529062009-12-17 21:24:28 -0500297{
Amir Goldstein54a307b2018-04-04 23:42:18 +0300298 __u32 marks_mask = 0, marks_ignored_mask = 0;
Amir Goldstein0badfa02020-07-16 11:42:09 +0300299 __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
300 FANOTIFY_EVENT_FLAGS;
Amir Goldsteinaa93bdc2020-03-19 17:10:12 +0200301 const struct path *path = fsnotify_data_path(data, data_type);
Amir Goldsteind809daf2020-07-16 11:42:12 +0300302 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Amir Goldstein837a3932018-04-20 16:10:54 -0700303 struct fsnotify_mark *mark;
304 int type;
Eric Paris1968f5e2010-07-28 10:18:39 -0400305
Amir Goldstein837a3932018-04-20 16:10:54 -0700306 pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
307 __func__, iter_info->report_mask, event_mask, data, data_type);
Eric Paris1968f5e2010-07-28 10:18:39 -0400308
Amir Goldsteind809daf2020-07-16 11:42:12 +0300309 if (!fid_mode) {
Amir Goldstein83b535d2019-01-10 19:04:42 +0200310 /* Do we have path to open a file descriptor? */
Amir Goldsteinaa93bdc2020-03-19 17:10:12 +0200311 if (!path)
Amir Goldstein83b535d2019-01-10 19:04:42 +0200312 return 0;
313 /* Path type events are only relevant for files and dirs */
314 if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
315 return 0;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300316 } else if (!(fid_mode & FAN_REPORT_FID)) {
317 /* Do we have a directory inode to report? */
318 if (!dir && !(event_mask & FS_ISDIR))
319 return 0;
Amir Goldstein83b535d2019-01-10 19:04:42 +0200320 }
Eric Parise1c048b2010-10-28 17:21:58 -0400321
Amir Goldstein1c9007d2021-11-29 22:15:28 +0200322 fsnotify_foreach_iter_type(type) {
Amir Goldstein837a3932018-04-20 16:10:54 -0700323 if (!fsnotify_iter_should_report_type(iter_info, type))
324 continue;
325 mark = iter_info->marks[type];
Amir Goldstein2f02fd32020-05-24 10:24:41 +0300326
327 /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
328 marks_ignored_mask |= mark->ignored_mask;
329
Amir Goldstein837a3932018-04-20 16:10:54 -0700330 /*
Amir Goldstein55bf8822020-03-19 17:10:17 +0200331 * If the event is on dir and this mark doesn't care about
332 * events on dir, don't send it!
333 */
334 if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
335 continue;
336
337 /*
Amir Goldsteinfecc4552020-12-02 14:07:09 +0200338 * If the event is on a child and this mark is on a parent not
Amir Goldstein497b0c52020-07-16 11:42:22 +0300339 * watching children, don't send it!
Amir Goldstein837a3932018-04-20 16:10:54 -0700340 */
Amir Goldstein1c9007d2021-11-29 22:15:28 +0200341 if (type == FSNOTIFY_ITER_TYPE_PARENT &&
Amir Goldsteinfecc4552020-12-02 14:07:09 +0200342 !(mark->mask & FS_EVENT_ON_CHILD))
Amir Goldstein837a3932018-04-20 16:10:54 -0700343 continue;
Amir Goldstein54a307b2018-04-04 23:42:18 +0300344
Amir Goldstein837a3932018-04-20 16:10:54 -0700345 marks_mask |= mark->mask;
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200346
347 /* Record the mark types of this group that matched the event */
348 *match_mask |= 1U << type;
Eric Paris1968f5e2010-07-28 10:18:39 -0400349 }
350
Amir Goldsteine7fce6d2019-01-10 19:04:44 +0200351 test_mask = event_mask & marks_mask & ~marks_ignored_mask;
352
353 /*
Amir Goldstein9e2ba2c2020-03-19 17:10:19 +0200354 * For dirent modification events (create/delete/move) that do not carry
355 * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
356 * so user can differentiate them from creat/unlink.
Amir Goldsteine7fce6d2019-01-10 19:04:44 +0200357 *
358 * For backward compatibility and consistency, do not report FAN_ONDIR
359 * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
Amir Goldstein0badfa02020-07-16 11:42:09 +0300360 * to user in fid mode for all event types.
361 *
362 * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
363 * fanotify_alloc_event() when group is reporting fid as indication
364 * that event happened on child.
Amir Goldsteine7fce6d2019-01-10 19:04:44 +0200365 */
Amir Goldsteind809daf2020-07-16 11:42:12 +0300366 if (fid_mode) {
Amir Goldstein0badfa02020-07-16 11:42:09 +0300367 /* Do not report event flags without any event */
368 if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
Amir Goldsteine7fce6d2019-01-10 19:04:44 +0200369 return 0;
370 } else {
Amir Goldstein0badfa02020-07-16 11:42:09 +0300371 user_mask &= ~FANOTIFY_EVENT_FLAGS;
Amir Goldsteine7fce6d2019-01-10 19:04:44 +0200372 }
373
Amir Goldsteine7fce6d2019-01-10 19:04:44 +0200374 return test_mask & user_mask;
Eric Paris1c529062009-12-17 21:24:28 -0500375}
376
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300377/*
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300378 * Check size needed to encode fanotify_fh.
379 *
380 * Return size of encoded fh without fanotify_fh header.
381 * Return 0 on failure to encode.
382 */
383static int fanotify_encode_fh_len(struct inode *inode)
384{
385 int dwords = 0;
Gabriel Krisman Bertazi572c28f2021-10-25 16:27:40 -0300386 int fh_len;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300387
388 if (!inode)
389 return 0;
390
391 exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
Gabriel Krisman Bertazi572c28f2021-10-25 16:27:40 -0300392 fh_len = dwords << 2;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300393
Gabriel Krisman Bertazi572c28f2021-10-25 16:27:40 -0300394 /*
395 * struct fanotify_error_event might be preallocated and is
396 * limited to MAX_HANDLE_SZ. This should never happen, but
397 * safeguard by forcing an invalid file handle.
398 */
399 if (WARN_ON_ONCE(fh_len > MAX_HANDLE_SZ))
400 return 0;
401
402 return fh_len;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300403}
404
405/*
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300406 * Encode fanotify_fh.
407 *
408 * Return total size of encoded fh including fanotify_fh header.
409 * Return 0 on failure to encode.
410 */
411static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200412 unsigned int fh_len, unsigned int *hash,
413 gfp_t gfp)
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200414{
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300415 int dwords, type = 0;
Jan Karaafc894c2020-03-24 16:55:37 +0100416 char *ext_buf = NULL;
417 void *buf = fh->buf;
418 int err;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200419
Amir Goldstein6ad1aad2020-07-16 11:42:11 +0300420 fh->type = FILEID_ROOT;
421 fh->len = 0;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300422 fh->flags = 0;
Gabriel Krisman Bertazi272531a2021-10-25 16:27:30 -0300423
424 /*
425 * Invalid FHs are used by FAN_FS_ERROR for errors not
426 * linked to any inode. The f_handle won't be reported
427 * back to userspace.
428 */
Amir Goldsteincacfb952020-03-19 17:10:21 +0200429 if (!inode)
Gabriel Krisman Bertazi272531a2021-10-25 16:27:30 -0300430 goto out;
Amir Goldsteincacfb952020-03-19 17:10:21 +0200431
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300432 /*
433 * !gpf means preallocated variable size fh, but fh_len could
434 * be zero in that case if encoding fh len failed.
435 */
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200436 err = -ENOENT;
Amir Goldstein2d9374f2021-11-29 22:15:31 +0200437 if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4) || fh_len > MAX_HANDLE_SZ)
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200438 goto out_err;
439
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300440 /* No external buffer in a variable size allocated fh */
441 if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
442 /* Treat failure to allocate fh as failure to encode fh */
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200443 err = -ENOMEM;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300444 ext_buf = kmalloc(fh_len, gfp);
Jan Karaafc894c2020-03-24 16:55:37 +0100445 if (!ext_buf)
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200446 goto out_err;
Jan Karaafc894c2020-03-24 16:55:37 +0100447
448 *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
449 buf = ext_buf;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300450 fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200451 }
452
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300453 dwords = fh_len >> 2;
Jan Karaafc894c2020-03-24 16:55:37 +0100454 type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200455 err = -EINVAL;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300456 if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200457 goto out_err;
458
Jan Karaafc894c2020-03-24 16:55:37 +0100459 fh->type = type;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300460 fh->len = fh_len;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200461
Gabriel Krisman Bertazi272531a2021-10-25 16:27:30 -0300462out:
Gabriel Krisman Bertazi74fe4732021-10-25 16:27:29 -0300463 /*
464 * Mix fh into event merge key. Hash might be NULL in case of
465 * unhashed FID events (i.e. FAN_FS_ERROR).
466 */
467 if (hash)
468 *hash ^= fanotify_hash_fh(fh);
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200469
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300470 return FANOTIFY_FH_HDR_LEN + fh_len;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200471
472out_err:
Jan Karaafc894c2020-03-24 16:55:37 +0100473 pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300474 type, fh_len, err);
Jan Karaafc894c2020-03-24 16:55:37 +0100475 kfree(ext_buf);
476 *fanotify_fh_ext_buf_ptr(fh) = NULL;
477 /* Report the event without a file identifier on encode error */
478 fh->type = FILEID_INVALID;
479 fh->len = 0;
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300480 return 0;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200481}
482
Amir Goldstein83b535d2019-01-10 19:04:42 +0200483/*
Amir Goldsteind61fd652021-11-29 22:15:29 +0200484 * FAN_REPORT_FID is ambiguous in that it reports the fid of the child for
485 * some events and the fid of the parent for create/delete/move events.
486 *
487 * With the FAN_REPORT_TARGET_FID flag, the fid of the child is reported
488 * also in create/delete/move events in addition to the fid of the parent
489 * and the name of the child.
490 */
491static inline bool fanotify_report_child_fid(unsigned int fid_mode, u32 mask)
492{
493 if (mask & ALL_FSNOTIFY_DIRENT_EVENTS)
494 return (fid_mode & FAN_REPORT_TARGET_FID);
495
496 return (fid_mode & FAN_REPORT_FID) && !(mask & FAN_ONDIR);
497}
498
499/*
500 * The inode to use as identifier when reporting fid depends on the event
501 * and the group flags.
502 *
503 * With the group flag FAN_REPORT_TARGET_FID, always report the child fid.
504 *
505 * Without the group flag FAN_REPORT_TARGET_FID, report the modified directory
506 * fid on dirent events and the child fid otherwise.
507 *
Amir Goldstein83b535d2019-01-10 19:04:42 +0200508 * For example:
Amir Goldsteind61fd652021-11-29 22:15:29 +0200509 * FS_ATTRIB reports the child fid even if reported on a watched parent.
510 * FS_CREATE reports the modified dir fid without FAN_REPORT_TARGET_FID.
511 * and reports the created child fid with FAN_REPORT_TARGET_FID.
Amir Goldstein83b535d2019-01-10 19:04:42 +0200512 */
Amir Goldsteinb54cecf2020-06-07 12:10:40 +0300513static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
Amir Goldsteind61fd652021-11-29 22:15:29 +0200514 int data_type, struct inode *dir,
515 unsigned int fid_mode)
Amir Goldstein83b535d2019-01-10 19:04:42 +0200516{
Amir Goldsteind61fd652021-11-29 22:15:29 +0200517 if ((event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) &&
518 !(fid_mode & FAN_REPORT_TARGET_FID))
Amir Goldsteinb54cecf2020-06-07 12:10:40 +0300519 return dir;
Amir Goldsteinaa93bdc2020-03-19 17:10:12 +0200520
Amir Goldsteincbcf47a2020-07-08 14:11:38 +0300521 return fsnotify_data_inode(data, data_type);
Amir Goldstein83b535d2019-01-10 19:04:42 +0200522}
523
Amir Goldstein83b7a592020-07-16 11:42:26 +0300524/*
525 * The inode to use as identifier when reporting dir fid depends on the event.
526 * Report the modified directory inode on dirent modification events.
527 * Report the "victim" inode if "victim" is a directory.
528 * Report the parent inode if "victim" is not a directory and event is
529 * reported to parent.
530 * Otherwise, do not report dir fid.
531 */
532static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
533 int data_type, struct inode *dir)
534{
535 struct inode *inode = fsnotify_data_inode(data, data_type);
536
537 if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
538 return dir;
539
Gabriel Krisman Bertazi12f47bf2021-10-25 16:27:28 -0300540 if (inode && S_ISDIR(inode->i_mode))
Amir Goldstein83b7a592020-07-16 11:42:26 +0300541 return inode;
542
543 return dir;
544}
545
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300546static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200547 unsigned int *hash,
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300548 gfp_t gfp)
549{
550 struct fanotify_path_event *pevent;
551
552 pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
553 if (!pevent)
554 return NULL;
555
556 pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
557 pevent->path = *path;
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200558 *hash ^= fanotify_hash_path(path);
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300559 path_get(path);
560
561 return &pevent->fae;
562}
563
564static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
565 gfp_t gfp)
566{
567 struct fanotify_perm_event *pevent;
568
569 pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
570 if (!pevent)
571 return NULL;
572
573 pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
574 pevent->response = 0;
575 pevent->state = FAN_EVENT_INIT;
576 pevent->path = *path;
577 path_get(path);
578
579 return &pevent->fae;
580}
581
582static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
583 __kernel_fsid_t *fsid,
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200584 unsigned int *hash,
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300585 gfp_t gfp)
586{
587 struct fanotify_fid_event *ffe;
588
589 ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
590 if (!ffe)
591 return NULL;
592
593 ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
594 ffe->fsid = *fsid;
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200595 *hash ^= fanotify_hash_fsid(fsid);
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300596 fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200597 hash, gfp);
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300598
599 return &ffe->fae;
600}
601
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200602static struct fanotify_event *fanotify_alloc_name_event(struct inode *dir,
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300603 __kernel_fsid_t *fsid,
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200604 const struct qstr *name,
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300605 struct inode *child,
Amir Goldstein39825342021-11-29 22:15:34 +0200606 struct dentry *moved,
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200607 unsigned int *hash,
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300608 gfp_t gfp)
609{
610 struct fanotify_name_event *fne;
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300611 struct fanotify_info *info;
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300612 struct fanotify_fh *dfh, *ffh;
Amir Goldstein39825342021-11-29 22:15:34 +0200613 struct inode *dir2 = moved ? d_inode(moved->d_parent) : NULL;
614 const struct qstr *name2 = moved ? &moved->d_name : NULL;
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200615 unsigned int dir_fh_len = fanotify_encode_fh_len(dir);
Amir Goldstein39825342021-11-29 22:15:34 +0200616 unsigned int dir2_fh_len = fanotify_encode_fh_len(dir2);
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300617 unsigned int child_fh_len = fanotify_encode_fh_len(child);
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200618 unsigned long name_len = name ? name->len : 0;
Amir Goldstein39825342021-11-29 22:15:34 +0200619 unsigned long name2_len = name2 ? name2->len : 0;
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200620 unsigned int len, size;
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300621
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200622 /* Reserve terminating null byte even for empty name */
Amir Goldstein39825342021-11-29 22:15:34 +0200623 size = sizeof(*fne) + name_len + name2_len + 2;
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200624 if (dir_fh_len)
625 size += FANOTIFY_FH_HDR_LEN + dir_fh_len;
Amir Goldstein39825342021-11-29 22:15:34 +0200626 if (dir2_fh_len)
627 size += FANOTIFY_FH_HDR_LEN + dir2_fh_len;
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300628 if (child_fh_len)
629 size += FANOTIFY_FH_HDR_LEN + child_fh_len;
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300630 fne = kmalloc(size, gfp);
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300631 if (!fne)
632 return NULL;
633
634 fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
635 fne->fsid = *fsid;
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200636 *hash ^= fanotify_hash_fsid(fsid);
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300637 info = &fne->info;
638 fanotify_info_init(info);
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200639 if (dir_fh_len) {
640 dfh = fanotify_info_dir_fh(info);
641 len = fanotify_encode_fh(dfh, dir, dir_fh_len, hash, 0);
642 fanotify_info_set_dir_fh(info, len);
643 }
Amir Goldstein39825342021-11-29 22:15:34 +0200644 if (dir2_fh_len) {
645 dfh = fanotify_info_dir2_fh(info);
646 len = fanotify_encode_fh(dfh, dir2, dir2_fh_len, hash, 0);
647 fanotify_info_set_dir2_fh(info, len);
648 }
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300649 if (child_fh_len) {
650 ffh = fanotify_info_file_fh(info);
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200651 len = fanotify_encode_fh(ffh, child, child_fh_len, hash, 0);
652 fanotify_info_set_file_fh(info, len);
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300653 }
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200654 if (name_len) {
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200655 fanotify_info_copy_name(info, name);
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200656 *hash ^= full_name_hash((void *)name_len, name->name, name_len);
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200657 }
Amir Goldstein39825342021-11-29 22:15:34 +0200658 if (name2_len) {
659 fanotify_info_copy_name2(info, name2);
660 *hash ^= full_name_hash((void *)name2_len, name2->name,
661 name2_len);
662 }
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300663
Amir Goldstein1a9515a2021-11-29 22:15:32 +0200664 pr_debug("%s: size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
665 __func__, size, dir_fh_len, child_fh_len,
Amir Goldsteinf35c4152020-07-16 11:42:18 +0300666 info->name_len, info->name_len, fanotify_info_name(info));
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300667
Amir Goldstein39825342021-11-29 22:15:34 +0200668 if (dir2_fh_len) {
669 pr_debug("%s: dir2_fh_len=%u name2_len=%u name2='%.*s'\n",
670 __func__, dir2_fh_len, info->name2_len,
671 info->name2_len, fanotify_info_name2(info));
672 }
673
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300674 return &fne->fae;
675}
676
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -0300677static struct fanotify_event *fanotify_alloc_error_event(
678 struct fsnotify_group *group,
679 __kernel_fsid_t *fsid,
Gabriel Krisman Bertazi8a6ae642021-10-25 16:27:36 -0300680 const void *data, int data_type,
681 unsigned int *hash)
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -0300682{
683 struct fs_error_report *report =
684 fsnotify_data_error_report(data, data_type);
Gabriel Krisman Bertazi936d6a32021-10-25 16:27:41 -0300685 struct inode *inode;
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -0300686 struct fanotify_error_event *fee;
Gabriel Krisman Bertazi936d6a32021-10-25 16:27:41 -0300687 int fh_len;
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -0300688
689 if (WARN_ON_ONCE(!report))
690 return NULL;
691
692 fee = mempool_alloc(&group->fanotify_data.error_events_pool, GFP_NOFS);
693 if (!fee)
694 return NULL;
695
696 fee->fae.type = FANOTIFY_EVENT_TYPE_FS_ERROR;
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300697 fee->error = report->error;
Gabriel Krisman Bertazi8a6ae642021-10-25 16:27:36 -0300698 fee->err_count = 1;
699 fee->fsid = *fsid;
700
Gabriel Krisman Bertazi936d6a32021-10-25 16:27:41 -0300701 inode = report->inode;
702 fh_len = fanotify_encode_fh_len(inode);
703
704 /* Bad fh_len. Fallback to using an invalid fh. Should never happen. */
705 if (!fh_len && inode)
706 inode = NULL;
707
708 fanotify_encode_fh(&fee->object_fh, inode, fh_len, NULL, 0);
709
Gabriel Krisman Bertazi8a6ae642021-10-25 16:27:36 -0300710 *hash ^= fanotify_hash_fsid(fsid);
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -0300711
712 return &fee->fae;
713}
714
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200715static struct fanotify_event *fanotify_alloc_event(
716 struct fsnotify_group *group,
717 u32 mask, const void *data, int data_type,
718 struct inode *dir, const struct qstr *file_name,
719 __kernel_fsid_t *fsid, u32 match_mask)
Jan Karaf0834412014-04-03 14:46:33 -0700720{
Amir Goldstein33913992019-01-10 19:04:32 +0200721 struct fanotify_event *event = NULL;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700722 gfp_t gfp = GFP_KERNEL_ACCOUNT;
Amir Goldsteind61fd652021-11-29 22:15:29 +0200723 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
724 struct inode *id = fanotify_fid_inode(mask, data, data_type, dir,
725 fid_mode);
Amir Goldstein83b7a592020-07-16 11:42:26 +0300726 struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
Amir Goldsteinaa93bdc2020-03-19 17:10:12 +0200727 const struct path *path = fsnotify_data_path(data, data_type);
Roman Gushchinb87d8ce2020-10-17 16:13:40 -0700728 struct mem_cgroup *old_memcg;
Amir Goldstein39825342021-11-29 22:15:34 +0200729 struct dentry *moved = NULL;
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300730 struct inode *child = NULL;
Amir Goldstein08b95c332020-07-08 14:11:52 +0300731 bool name_event = false;
Amir Goldstein8988f112021-03-04 12:48:23 +0200732 unsigned int hash = 0;
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200733 bool ondir = mask & FAN_ONDIR;
734 struct pid *pid;
Jan Kara1f5eaa92018-02-21 14:10:59 +0100735
Amir Goldstein929943b2020-07-16 11:42:28 +0300736 if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300737 /*
Amir Goldsteind61fd652021-11-29 22:15:29 +0200738 * For certain events and group flags, report the child fid
Amir Goldstein691d9762020-07-16 11:42:30 +0300739 * in addition to reporting the parent fid and maybe child name.
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300740 */
Amir Goldsteind61fd652021-11-29 22:15:29 +0200741 if (fanotify_report_child_fid(fid_mode, mask) && id != dirid)
Amir Goldstein7e8283a2020-07-16 11:42:29 +0300742 child = id;
743
Amir Goldstein83b7a592020-07-16 11:42:26 +0300744 id = dirid;
745
Amir Goldstein929943b2020-07-16 11:42:28 +0300746 /*
747 * We record file name only in a group with FAN_REPORT_NAME
748 * and when we have a directory inode to report.
749 *
750 * For directory entry modification event, we record the fid of
751 * the directory and the name of the modified entry.
752 *
753 * For event on non-directory that is reported to parent, we
754 * record the fid of the parent and the name of the child.
Amir Goldstein691d9762020-07-16 11:42:30 +0300755 *
756 * Even if not reporting name, we need a variable length
757 * fanotify_name_event if reporting both parent and child fids.
Amir Goldstein929943b2020-07-16 11:42:28 +0300758 */
Amir Goldstein691d9762020-07-16 11:42:30 +0300759 if (!(fid_mode & FAN_REPORT_NAME)) {
760 name_event = !!child;
761 file_name = NULL;
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200762 } else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) {
Amir Goldstein929943b2020-07-16 11:42:28 +0300763 name_event = true;
Amir Goldstein691d9762020-07-16 11:42:30 +0300764 }
Amir Goldstein39825342021-11-29 22:15:34 +0200765
766 /*
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200767 * In the special case of FAN_RENAME event, use the match_mask
768 * to determine if we need to report only the old parent+name,
769 * only the new parent+name or both.
Amir Goldstein39825342021-11-29 22:15:34 +0200770 * 'dirid' and 'file_name' are the old parent+name and
771 * 'moved' has the new parent+name.
772 */
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200773 if (mask & FAN_RENAME) {
774 bool report_old, report_new;
775
776 if (WARN_ON_ONCE(!match_mask))
777 return NULL;
778
779 /* Report both old and new parent+name if sb watching */
780 report_old = report_new =
781 match_mask & (1U << FSNOTIFY_ITER_TYPE_SB);
782 report_old |=
783 match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE);
784 report_new |=
785 match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE2);
786
787 if (!report_old) {
788 /* Do not report old parent+name */
789 dirid = NULL;
790 file_name = NULL;
791 }
792 if (report_new) {
793 /* Report new parent+name */
794 moved = fsnotify_data_dentry(data, data_type);
795 }
796 }
Amir Goldstein929943b2020-07-16 11:42:28 +0300797 }
798
Jan Kara1f5eaa92018-02-21 14:10:59 +0100799 /*
800 * For queues with unlimited length lost events are not expected and
801 * can possibly have security implications. Avoid losing events when
Shakeel Buttec165452019-07-11 20:55:52 -0700802 * memory is short. For the limited size queues, avoid OOM killer in the
803 * target monitoring memcg as it may have security repercussion.
Jan Kara1f5eaa92018-02-21 14:10:59 +0100804 */
805 if (group->max_events == UINT_MAX)
806 gfp |= __GFP_NOFAIL;
Shakeel Buttec165452019-07-11 20:55:52 -0700807 else
808 gfp |= __GFP_RETRY_MAYFAIL;
Jan Karaf0834412014-04-03 14:46:33 -0700809
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700810 /* Whoever is interested in the event, pays for the allocation. */
Roman Gushchinb87d8ce2020-10-17 16:13:40 -0700811 old_memcg = set_active_memcg(group->memcg);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700812
Miklos Szeredi6685df32017-10-30 21:14:56 +0100813 if (fanotify_is_perm_event(mask)) {
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300814 event = fanotify_alloc_perm_event(path, gfp);
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -0300815 } else if (fanotify_is_error_event(mask)) {
816 event = fanotify_alloc_error_event(group, fsid, data,
Gabriel Krisman Bertazi8a6ae642021-10-25 16:27:36 -0300817 data_type, &hash);
Amir Goldstein39825342021-11-29 22:15:34 +0200818 } else if (name_event && (file_name || moved || child)) {
819 event = fanotify_alloc_name_event(dirid, fsid, file_name, child,
820 moved, &hash, gfp);
Amir Goldsteind809daf2020-07-16 11:42:12 +0300821 } else if (fid_mode) {
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200822 event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
Jan Kara7088f352020-03-24 17:04:20 +0100823 } else {
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200824 event = fanotify_alloc_path_event(path, &hash, gfp);
Jan Kara7088f352020-03-24 17:04:20 +0100825 }
826
Amir Goldstein9c61f3b2020-03-30 10:55:28 +0300827 if (!event)
828 goto out;
829
Amir Goldsteind0a6a872018-10-04 00:25:38 +0300830 if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200831 pid = get_pid(task_pid(current));
Amir Goldsteind0a6a872018-10-04 00:25:38 +0300832 else
Amir Goldstein7e3e5c62021-03-04 12:48:24 +0200833 pid = get_pid(task_tgid(current));
834
835 /* Mix event info, FAN_ONDIR flag and pid into event merge key */
836 hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
837 fanotify_init_event(event, hash, mask);
838 event->pid = pid;
Jan Kara7088f352020-03-24 17:04:20 +0100839
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700840out:
Roman Gushchinb87d8ce2020-10-17 16:13:40 -0700841 set_active_memcg(old_memcg);
Jan Karaf0834412014-04-03 14:46:33 -0700842 return event;
843}
844
Amir Goldstein77115222019-01-10 19:04:37 +0200845/*
846 * Get cached fsid of the filesystem containing the object from any connector.
847 * All connectors are supposed to have the same fsid, but we do not verify that
848 * here.
849 */
850static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
851{
852 int type;
853 __kernel_fsid_t fsid = {};
854
Amir Goldstein1c9007d2021-11-29 22:15:28 +0200855 fsnotify_foreach_iter_type(type) {
Jan Karab1da6a52019-04-24 18:39:57 +0200856 struct fsnotify_mark_connector *conn;
857
Amir Goldstein77115222019-01-10 19:04:37 +0200858 if (!fsnotify_iter_should_report_type(iter_info, type))
859 continue;
860
Jan Karab1da6a52019-04-24 18:39:57 +0200861 conn = READ_ONCE(iter_info->marks[type]->connector);
862 /* Mark is just getting destroyed or created? */
863 if (!conn)
864 continue;
Amir Goldsteinc285a2f2019-06-19 13:34:44 +0300865 if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
866 continue;
867 /* Pairs with smp_wmb() in fsnotify_add_mark_list() */
868 smp_rmb();
Jan Karab1da6a52019-04-24 18:39:57 +0200869 fsid = conn->fsid;
Amir Goldstein77115222019-01-10 19:04:37 +0200870 if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
871 continue;
872 return fsid;
873 }
874
875 return fsid;
876}
877
Amir Goldstein94e00d22021-03-04 12:48:25 +0200878/*
879 * Add an event to hash table for faster merge.
880 */
881static void fanotify_insert_event(struct fsnotify_group *group,
882 struct fsnotify_event *fsn_event)
883{
884 struct fanotify_event *event = FANOTIFY_E(fsn_event);
885 unsigned int bucket = fanotify_event_hash_bucket(group, event);
886 struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
887
888 assert_spin_locked(&group->notification_lock);
889
Gabriel Krisman Bertazicc53b552021-10-25 16:27:19 -0300890 if (!fanotify_is_hashed_event(event->mask))
891 return;
892
Amir Goldstein94e00d22021-03-04 12:48:25 +0200893 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
894 group, event, bucket);
895
896 hlist_add_head(&event->merge_list, hlist);
897}
898
Amir Goldsteinb54cecf2020-06-07 12:10:40 +0300899static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
900 const void *data, int data_type,
901 struct inode *dir,
Al Viroe43e9c32019-04-26 13:51:03 -0400902 const struct qstr *file_name, u32 cookie,
Jan Kara9385a842016-11-10 17:51:50 +0100903 struct fsnotify_iter_info *iter_info)
Jan Kara7053aee2014-01-21 15:48:14 -0800904{
905 int ret = 0;
Amir Goldstein33913992019-01-10 19:04:32 +0200906 struct fanotify_event *event;
Jan Kara7053aee2014-01-21 15:48:14 -0800907 struct fsnotify_event *fsn_event;
Amir Goldstein77115222019-01-10 19:04:37 +0200908 __kernel_fsid_t fsid = {};
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200909 u32 match_mask = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800910
911 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
912 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
Amir Goldstein235328d2019-01-10 19:04:43 +0200913 BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
Jan Kara7053aee2014-01-21 15:48:14 -0800914 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
915 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
916 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
Amir Goldstein235328d2019-01-10 19:04:43 +0200917 BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
918 BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
919 BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
920 BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
921 BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
922 BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
Jan Kara7053aee2014-01-21 15:48:14 -0800923 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
924 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
925 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
926 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
927 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
Matthew Bobrowski9b076f12018-11-08 14:07:14 +1100928 BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
Matthew Bobrowski66917a32018-11-08 14:12:44 +1100929 BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
Gabriel Krisman Bertazi8d11a4f2021-10-25 16:27:33 -0300930 BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR);
Amir Goldstein39825342021-11-29 22:15:34 +0200931 BUILD_BUG_ON(FAN_RENAME != FS_RENAME);
Jan Kara7053aee2014-01-21 15:48:14 -0800932
Amir Goldstein8cc3b1c2021-11-29 22:15:37 +0200933 BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +0300934
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200935 mask = fanotify_group_event_mask(group, iter_info, &match_mask,
936 mask, data, data_type, dir);
Matthew Bobrowski2d10b232018-11-08 14:05:49 +1100937 if (!mask)
Jan Kara83c4c4b2014-01-21 15:48:15 -0800938 return 0;
939
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200940 pr_debug("%s: group=%p mask=%x report_mask=%x\n", __func__,
941 group, mask, match_mask);
Jan Kara7053aee2014-01-21 15:48:14 -0800942
Miklos Szeredi6685df32017-10-30 21:14:56 +0100943 if (fanotify_is_perm_event(mask)) {
Miklos Szeredif37650f2017-10-30 21:14:56 +0100944 /*
945 * fsnotify_prepare_user_wait() fails if we race with mark
946 * deletion. Just let the operation pass in that case.
947 */
948 if (!fsnotify_prepare_user_wait(iter_info))
949 return 0;
950 }
Miklos Szeredif37650f2017-10-30 21:14:56 +0100951
Amir Goldsteind809daf2020-07-16 11:42:12 +0300952 if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
Amir Goldstein77115222019-01-10 19:04:37 +0200953 fsid = fanotify_get_fsid(iter_info);
Jan Karab1da6a52019-04-24 18:39:57 +0200954 /* Racing with mark destruction or creation? */
955 if (!fsid.val[0] && !fsid.val[1])
956 return 0;
957 }
Amir Goldstein77115222019-01-10 19:04:37 +0200958
Amir Goldsteinb54cecf2020-06-07 12:10:40 +0300959 event = fanotify_alloc_event(group, mask, data, data_type, dir,
Amir Goldstein2bfbccc2021-11-29 22:15:35 +0200960 file_name, &fsid, match_mask);
Miklos Szeredif37650f2017-10-30 21:14:56 +0100961 ret = -ENOMEM;
Jan Kara7b1f6412018-02-21 15:07:52 +0100962 if (unlikely(!event)) {
963 /*
964 * We don't queue overflow events for permission events as
965 * there the access is denied and so no event is in fact lost.
966 */
967 if (!fanotify_is_perm_event(mask))
968 fsnotify_queue_overflow(group);
Miklos Szeredif37650f2017-10-30 21:14:56 +0100969 goto finish;
Jan Kara7b1f6412018-02-21 15:07:52 +0100970 }
Jan Kara7053aee2014-01-21 15:48:14 -0800971
972 fsn_event = &event->fse;
Gabriel Krisman Bertazi1ad03c32021-10-25 16:27:24 -0300973 ret = fsnotify_insert_event(group, fsn_event, fanotify_merge,
974 fanotify_insert_event);
Jan Kara83c0e1b2014-01-28 18:53:22 +0100975 if (ret) {
Jan Kara482ef062014-02-21 19:07:54 +0100976 /* Permission events shouldn't be merged */
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300977 BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
Jan Kara7053aee2014-01-21 15:48:14 -0800978 /* Our event wasn't used in the end. Free it. */
979 fsnotify_destroy_event(group, fsn_event);
Jan Kara482ef062014-02-21 19:07:54 +0100980
Miklos Szeredif37650f2017-10-30 21:14:56 +0100981 ret = 0;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100982 } else if (fanotify_is_perm_event(mask)) {
Jan Kara7088f352020-03-24 17:04:20 +0100983 ret = fanotify_get_response(group, FANOTIFY_PERM(event),
Jan Kara05f0e382016-11-10 17:45:16 +0100984 iter_info);
Jan Kara85816792014-01-28 21:38:06 +0100985 }
Miklos Szeredif37650f2017-10-30 21:14:56 +0100986finish:
Miklos Szeredi6685df32017-10-30 21:14:56 +0100987 if (fanotify_is_perm_event(mask))
Miklos Szeredif37650f2017-10-30 21:14:56 +0100988 fsnotify_finish_user_wait(iter_info);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100989
Jan Kara7053aee2014-01-21 15:48:14 -0800990 return ret;
991}
992
Eric Paris4afeff82010-10-28 17:21:58 -0400993static void fanotify_free_group_priv(struct fsnotify_group *group)
994{
Amir Goldstein94e00d22021-03-04 12:48:25 +0200995 kfree(group->fanotify_data.merge_hash);
Amir Goldstein5b8fea62021-03-04 13:29:20 +0200996 if (group->fanotify_data.ucounts)
997 dec_ucount(group->fanotify_data.ucounts,
998 UCOUNT_FANOTIFY_GROUPS);
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -0300999
1000 if (mempool_initialized(&group->fanotify_data.error_events_pool))
1001 mempool_exit(&group->fanotify_data.error_events_pool);
Eric Paris4afeff82010-10-28 17:21:58 -04001002}
1003
Jan Kara7088f352020-03-24 17:04:20 +01001004static void fanotify_free_path_event(struct fanotify_event *event)
1005{
1006 path_put(fanotify_event_path(event));
1007 kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
1008}
1009
1010static void fanotify_free_perm_event(struct fanotify_event *event)
1011{
1012 path_put(fanotify_event_path(event));
1013 kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
1014}
1015
1016static void fanotify_free_fid_event(struct fanotify_event *event)
1017{
1018 struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
1019
1020 if (fanotify_fh_has_ext_buf(&ffe->object_fh))
1021 kfree(fanotify_fh_ext_buf(&ffe->object_fh));
1022 kmem_cache_free(fanotify_fid_event_cachep, ffe);
1023}
1024
Amir Goldsteincacfb952020-03-19 17:10:21 +02001025static void fanotify_free_name_event(struct fanotify_event *event)
1026{
Amir Goldsteinf35c4152020-07-16 11:42:18 +03001027 kfree(FANOTIFY_NE(event));
Amir Goldsteincacfb952020-03-19 17:10:21 +02001028}
1029
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -03001030static void fanotify_free_error_event(struct fsnotify_group *group,
1031 struct fanotify_event *event)
1032{
1033 struct fanotify_error_event *fee = FANOTIFY_EE(event);
1034
1035 mempool_free(fee, &group->fanotify_data.error_events_pool);
1036}
1037
Gabriel Krisman Bertazi330ae772021-10-25 16:27:27 -03001038static void fanotify_free_event(struct fsnotify_group *group,
1039 struct fsnotify_event *fsn_event)
Jan Kara7053aee2014-01-21 15:48:14 -08001040{
Amir Goldstein33913992019-01-10 19:04:32 +02001041 struct fanotify_event *event;
Jan Kara7053aee2014-01-21 15:48:14 -08001042
1043 event = FANOTIFY_E(fsn_event);
Amir Goldsteind0a6a872018-10-04 00:25:38 +03001044 put_pid(event->pid);
Jan Kara7088f352020-03-24 17:04:20 +01001045 switch (event->type) {
1046 case FANOTIFY_EVENT_TYPE_PATH:
1047 fanotify_free_path_event(event);
1048 break;
1049 case FANOTIFY_EVENT_TYPE_PATH_PERM:
1050 fanotify_free_perm_event(event);
1051 break;
1052 case FANOTIFY_EVENT_TYPE_FID:
1053 fanotify_free_fid_event(event);
1054 break;
Amir Goldsteincacfb952020-03-19 17:10:21 +02001055 case FANOTIFY_EVENT_TYPE_FID_NAME:
1056 fanotify_free_name_event(event);
1057 break;
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +03001058 case FANOTIFY_EVENT_TYPE_OVERFLOW:
1059 kfree(event);
1060 break;
Gabriel Krisman Bertazi83e9acb2021-10-25 16:27:35 -03001061 case FANOTIFY_EVENT_TYPE_FS_ERROR:
1062 fanotify_free_error_event(group, event);
1063 break;
Jan Kara7088f352020-03-24 17:04:20 +01001064 default:
1065 WARN_ON_ONCE(1);
Jan Karaf0834412014-04-03 14:46:33 -07001066 }
Jan Kara7053aee2014-01-21 15:48:14 -08001067}
1068
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001069static void fanotify_freeing_mark(struct fsnotify_mark *mark,
1070 struct fsnotify_group *group)
1071{
1072 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1073 dec_ucount(group->fanotify_data.ucounts, UCOUNT_FANOTIFY_MARKS);
1074}
1075
Jan Kara054c6362016-12-21 18:06:12 +01001076static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
1077{
1078 kmem_cache_free(fanotify_mark_cache, fsn_mark);
1079}
1080
Eric Parisff0b16a2009-12-17 21:24:25 -05001081const struct fsnotify_ops fanotify_fsnotify_ops = {
1082 .handle_event = fanotify_handle_event,
Eric Paris4afeff82010-10-28 17:21:58 -04001083 .free_group_priv = fanotify_free_group_priv,
Jan Kara7053aee2014-01-21 15:48:14 -08001084 .free_event = fanotify_free_event,
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001085 .freeing_mark = fanotify_freeing_mark,
Jan Kara054c6362016-12-21 18:06:12 +01001086 .free_mark = fanotify_free_mark,
Eric Parisff0b16a2009-12-17 21:24:25 -05001087};