1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/fanotify.h>
3*4882a593Smuzhiyun #include <linux/fdtable.h>
4*4882a593Smuzhiyun #include <linux/fsnotify_backend.h>
5*4882a593Smuzhiyun #include <linux/init.h>
6*4882a593Smuzhiyun #include <linux/jiffies.h>
7*4882a593Smuzhiyun #include <linux/kernel.h> /* UINT_MAX */
8*4882a593Smuzhiyun #include <linux/mount.h>
9*4882a593Smuzhiyun #include <linux/sched.h>
10*4882a593Smuzhiyun #include <linux/sched/user.h>
11*4882a593Smuzhiyun #include <linux/sched/signal.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/wait.h>
14*4882a593Smuzhiyun #include <linux/audit.h>
15*4882a593Smuzhiyun #include <linux/sched/mm.h>
16*4882a593Smuzhiyun #include <linux/statfs.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "fanotify.h"
19*4882a593Smuzhiyun
fanotify_path_equal(struct path * p1,struct path * p2)20*4882a593Smuzhiyun static bool fanotify_path_equal(struct path *p1, struct path *p2)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun
fanotify_fsid_equal(__kernel_fsid_t * fsid1,__kernel_fsid_t * fsid2)25*4882a593Smuzhiyun static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
26*4882a593Smuzhiyun __kernel_fsid_t *fsid2)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
29*4882a593Smuzhiyun }
30*4882a593Smuzhiyun
fanotify_fh_equal(struct fanotify_fh * fh1,struct fanotify_fh * fh2)31*4882a593Smuzhiyun static bool fanotify_fh_equal(struct fanotify_fh *fh1,
32*4882a593Smuzhiyun struct fanotify_fh *fh2)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun if (fh1->type != fh2->type || fh1->len != fh2->len)
35*4882a593Smuzhiyun return false;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun return !fh1->len ||
38*4882a593Smuzhiyun !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
fanotify_fid_event_equal(struct fanotify_fid_event * ffe1,struct fanotify_fid_event * ffe2)41*4882a593Smuzhiyun static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
42*4882a593Smuzhiyun struct fanotify_fid_event *ffe2)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun /* Do not merge fid events without object fh */
45*4882a593Smuzhiyun if (!ffe1->object_fh.len)
46*4882a593Smuzhiyun return false;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
49*4882a593Smuzhiyun fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
fanotify_info_equal(struct fanotify_info * info1,struct fanotify_info * info2)52*4882a593Smuzhiyun static bool fanotify_info_equal(struct fanotify_info *info1,
53*4882a593Smuzhiyun struct fanotify_info *info2)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
56*4882a593Smuzhiyun info1->file_fh_totlen != info2->file_fh_totlen ||
57*4882a593Smuzhiyun info1->name_len != info2->name_len)
58*4882a593Smuzhiyun return false;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun if (info1->dir_fh_totlen &&
61*4882a593Smuzhiyun !fanotify_fh_equal(fanotify_info_dir_fh(info1),
62*4882a593Smuzhiyun fanotify_info_dir_fh(info2)))
63*4882a593Smuzhiyun return false;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun if (info1->file_fh_totlen &&
66*4882a593Smuzhiyun !fanotify_fh_equal(fanotify_info_file_fh(info1),
67*4882a593Smuzhiyun fanotify_info_file_fh(info2)))
68*4882a593Smuzhiyun return false;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun return !info1->name_len ||
71*4882a593Smuzhiyun !memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
72*4882a593Smuzhiyun info1->name_len);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
fanotify_name_event_equal(struct fanotify_name_event * fne1,struct fanotify_name_event * fne2)75*4882a593Smuzhiyun static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
76*4882a593Smuzhiyun struct fanotify_name_event *fne2)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun struct fanotify_info *info1 = &fne1->info;
79*4882a593Smuzhiyun struct fanotify_info *info2 = &fne2->info;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* Do not merge name events without dir fh */
82*4882a593Smuzhiyun if (!info1->dir_fh_totlen)
83*4882a593Smuzhiyun return false;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
86*4882a593Smuzhiyun return false;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return fanotify_info_equal(info1, info2);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
fanotify_should_merge(struct fsnotify_event * old_fsn,struct fsnotify_event * new_fsn)91*4882a593Smuzhiyun static bool fanotify_should_merge(struct fsnotify_event *old_fsn,
92*4882a593Smuzhiyun struct fsnotify_event *new_fsn)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun struct fanotify_event *old, *new;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
97*4882a593Smuzhiyun old = FANOTIFY_E(old_fsn);
98*4882a593Smuzhiyun new = FANOTIFY_E(new_fsn);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun if (old_fsn->objectid != new_fsn->objectid ||
101*4882a593Smuzhiyun old->type != new->type || old->pid != new->pid)
102*4882a593Smuzhiyun return false;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun * We want to merge many dirent events in the same dir (i.e.
106*4882a593Smuzhiyun * creates/unlinks/renames), but we do not want to merge dirent
107*4882a593Smuzhiyun * events referring to subdirs with dirent events referring to
108*4882a593Smuzhiyun * non subdirs, otherwise, user won't be able to tell from a
109*4882a593Smuzhiyun * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
110*4882a593Smuzhiyun * unlink pair or rmdir+create pair of events.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
113*4882a593Smuzhiyun return false;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun switch (old->type) {
116*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_PATH:
117*4882a593Smuzhiyun return fanotify_path_equal(fanotify_event_path(old),
118*4882a593Smuzhiyun fanotify_event_path(new));
119*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_FID:
120*4882a593Smuzhiyun return fanotify_fid_event_equal(FANOTIFY_FE(old),
121*4882a593Smuzhiyun FANOTIFY_FE(new));
122*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_FID_NAME:
123*4882a593Smuzhiyun return fanotify_name_event_equal(FANOTIFY_NE(old),
124*4882a593Smuzhiyun FANOTIFY_NE(new));
125*4882a593Smuzhiyun default:
126*4882a593Smuzhiyun WARN_ON_ONCE(1);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun return false;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* Limit event merges to limit CPU overhead per event */
133*4882a593Smuzhiyun #define FANOTIFY_MAX_MERGE_EVENTS 128
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* and the list better be locked by something too! */
fanotify_merge(struct list_head * list,struct fsnotify_event * event)136*4882a593Smuzhiyun static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun struct fsnotify_event *test_event;
139*4882a593Smuzhiyun struct fanotify_event *new;
140*4882a593Smuzhiyun int i = 0;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun pr_debug("%s: list=%p event=%p\n", __func__, list, event);
143*4882a593Smuzhiyun new = FANOTIFY_E(event);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * Don't merge a permission event with any other event so that we know
147*4882a593Smuzhiyun * the event structure we have created in fanotify_handle_event() is the
148*4882a593Smuzhiyun * one we should check for permission response.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun if (fanotify_is_perm_event(new->mask))
151*4882a593Smuzhiyun return 0;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun list_for_each_entry_reverse(test_event, list, list) {
154*4882a593Smuzhiyun if (++i > FANOTIFY_MAX_MERGE_EVENTS)
155*4882a593Smuzhiyun break;
156*4882a593Smuzhiyun if (fanotify_should_merge(test_event, event)) {
157*4882a593Smuzhiyun FANOTIFY_E(test_event)->mask |= new->mask;
158*4882a593Smuzhiyun return 1;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return 0;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * Wait for response to permission event. The function also takes care of
167*4882a593Smuzhiyun * freeing the permission event (or offloads that in case the wait is canceled
168*4882a593Smuzhiyun * by a signal). The function returns 0 in case access got allowed by userspace,
169*4882a593Smuzhiyun * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
170*4882a593Smuzhiyun * the wait got interrupted by a signal.
171*4882a593Smuzhiyun */
fanotify_get_response(struct fsnotify_group * group,struct fanotify_perm_event * event,struct fsnotify_iter_info * iter_info)172*4882a593Smuzhiyun static int fanotify_get_response(struct fsnotify_group *group,
173*4882a593Smuzhiyun struct fanotify_perm_event *event,
174*4882a593Smuzhiyun struct fsnotify_iter_info *iter_info)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun int ret;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun pr_debug("%s: group=%p event=%p\n", __func__, group, event);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun ret = wait_event_killable(group->fanotify_data.access_waitq,
181*4882a593Smuzhiyun event->state == FAN_EVENT_ANSWERED);
182*4882a593Smuzhiyun /* Signal pending? */
183*4882a593Smuzhiyun if (ret < 0) {
184*4882a593Smuzhiyun spin_lock(&group->notification_lock);
185*4882a593Smuzhiyun /* Event reported to userspace and no answer yet? */
186*4882a593Smuzhiyun if (event->state == FAN_EVENT_REPORTED) {
187*4882a593Smuzhiyun /* Event will get freed once userspace answers to it */
188*4882a593Smuzhiyun event->state = FAN_EVENT_CANCELED;
189*4882a593Smuzhiyun spin_unlock(&group->notification_lock);
190*4882a593Smuzhiyun return ret;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun /* Event not yet reported? Just remove it. */
193*4882a593Smuzhiyun if (event->state == FAN_EVENT_INIT)
194*4882a593Smuzhiyun fsnotify_remove_queued_event(group, &event->fae.fse);
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * Event may be also answered in case signal delivery raced
197*4882a593Smuzhiyun * with wakeup. In that case we have nothing to do besides
198*4882a593Smuzhiyun * freeing the event and reporting error.
199*4882a593Smuzhiyun */
200*4882a593Smuzhiyun spin_unlock(&group->notification_lock);
201*4882a593Smuzhiyun goto out;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /* userspace responded, convert to something usable */
205*4882a593Smuzhiyun switch (event->response & ~FAN_AUDIT) {
206*4882a593Smuzhiyun case FAN_ALLOW:
207*4882a593Smuzhiyun ret = 0;
208*4882a593Smuzhiyun break;
209*4882a593Smuzhiyun case FAN_DENY:
210*4882a593Smuzhiyun default:
211*4882a593Smuzhiyun ret = -EPERM;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /* Check if the response should be audited */
215*4882a593Smuzhiyun if (event->response & FAN_AUDIT)
216*4882a593Smuzhiyun audit_fanotify(event->response & ~FAN_AUDIT);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
219*4882a593Smuzhiyun group, event, ret);
220*4882a593Smuzhiyun out:
221*4882a593Smuzhiyun fsnotify_destroy_event(group, &event->fae.fse);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun return ret;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun * This function returns a mask for an event that only contains the flags
228*4882a593Smuzhiyun * that have been specifically requested by the user. Flags that may have
229*4882a593Smuzhiyun * been included within the event mask, but have not been explicitly
230*4882a593Smuzhiyun * requested by the user, will not be present in the returned mask.
231*4882a593Smuzhiyun */
fanotify_group_event_mask(struct fsnotify_group * group,struct fsnotify_iter_info * iter_info,u32 event_mask,const void * data,int data_type,struct inode * dir)232*4882a593Smuzhiyun static u32 fanotify_group_event_mask(struct fsnotify_group *group,
233*4882a593Smuzhiyun struct fsnotify_iter_info *iter_info,
234*4882a593Smuzhiyun u32 event_mask, const void *data,
235*4882a593Smuzhiyun int data_type, struct inode *dir)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun __u32 marks_mask = 0, marks_ignored_mask = 0;
238*4882a593Smuzhiyun __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
239*4882a593Smuzhiyun FANOTIFY_EVENT_FLAGS;
240*4882a593Smuzhiyun const struct path *path = fsnotify_data_path(data, data_type);
241*4882a593Smuzhiyun unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
242*4882a593Smuzhiyun struct fsnotify_mark *mark;
243*4882a593Smuzhiyun int type;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
246*4882a593Smuzhiyun __func__, iter_info->report_mask, event_mask, data, data_type);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun if (!fid_mode) {
249*4882a593Smuzhiyun /* Do we have path to open a file descriptor? */
250*4882a593Smuzhiyun if (!path)
251*4882a593Smuzhiyun return 0;
252*4882a593Smuzhiyun /* Path type events are only relevant for files and dirs */
253*4882a593Smuzhiyun if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
254*4882a593Smuzhiyun return 0;
255*4882a593Smuzhiyun } else if (!(fid_mode & FAN_REPORT_FID)) {
256*4882a593Smuzhiyun /* Do we have a directory inode to report? */
257*4882a593Smuzhiyun if (!dir && !(event_mask & FS_ISDIR))
258*4882a593Smuzhiyun return 0;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun fsnotify_foreach_obj_type(type) {
262*4882a593Smuzhiyun if (!fsnotify_iter_should_report_type(iter_info, type))
263*4882a593Smuzhiyun continue;
264*4882a593Smuzhiyun mark = iter_info->marks[type];
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
267*4882a593Smuzhiyun marks_ignored_mask |= mark->ignored_mask;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun * If the event is on dir and this mark doesn't care about
271*4882a593Smuzhiyun * events on dir, don't send it!
272*4882a593Smuzhiyun */
273*4882a593Smuzhiyun if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
274*4882a593Smuzhiyun continue;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun * If the event is on a child and this mark is on a parent not
278*4882a593Smuzhiyun * watching children, don't send it!
279*4882a593Smuzhiyun */
280*4882a593Smuzhiyun if (type == FSNOTIFY_OBJ_TYPE_PARENT &&
281*4882a593Smuzhiyun !(mark->mask & FS_EVENT_ON_CHILD))
282*4882a593Smuzhiyun continue;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun marks_mask |= mark->mask;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun test_mask = event_mask & marks_mask & ~marks_ignored_mask;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * For dirent modification events (create/delete/move) that do not carry
291*4882a593Smuzhiyun * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
292*4882a593Smuzhiyun * so user can differentiate them from creat/unlink.
293*4882a593Smuzhiyun *
294*4882a593Smuzhiyun * For backward compatibility and consistency, do not report FAN_ONDIR
295*4882a593Smuzhiyun * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
296*4882a593Smuzhiyun * to user in fid mode for all event types.
297*4882a593Smuzhiyun *
298*4882a593Smuzhiyun * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
299*4882a593Smuzhiyun * fanotify_alloc_event() when group is reporting fid as indication
300*4882a593Smuzhiyun * that event happened on child.
301*4882a593Smuzhiyun */
302*4882a593Smuzhiyun if (fid_mode) {
303*4882a593Smuzhiyun /* Do not report event flags without any event */
304*4882a593Smuzhiyun if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
305*4882a593Smuzhiyun return 0;
306*4882a593Smuzhiyun } else {
307*4882a593Smuzhiyun user_mask &= ~FANOTIFY_EVENT_FLAGS;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun return test_mask & user_mask;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun /*
314*4882a593Smuzhiyun * Check size needed to encode fanotify_fh.
315*4882a593Smuzhiyun *
316*4882a593Smuzhiyun * Return size of encoded fh without fanotify_fh header.
317*4882a593Smuzhiyun * Return 0 on failure to encode.
318*4882a593Smuzhiyun */
fanotify_encode_fh_len(struct inode * inode)319*4882a593Smuzhiyun static int fanotify_encode_fh_len(struct inode *inode)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun int dwords = 0;
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (!inode)
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun return dwords << 2;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /*
332*4882a593Smuzhiyun * Encode fanotify_fh.
333*4882a593Smuzhiyun *
334*4882a593Smuzhiyun * Return total size of encoded fh including fanotify_fh header.
335*4882a593Smuzhiyun * Return 0 on failure to encode.
336*4882a593Smuzhiyun */
fanotify_encode_fh(struct fanotify_fh * fh,struct inode * inode,unsigned int fh_len,gfp_t gfp)337*4882a593Smuzhiyun static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
338*4882a593Smuzhiyun unsigned int fh_len, gfp_t gfp)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun int dwords, type = 0;
341*4882a593Smuzhiyun char *ext_buf = NULL;
342*4882a593Smuzhiyun void *buf = fh->buf;
343*4882a593Smuzhiyun int err;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun fh->type = FILEID_ROOT;
346*4882a593Smuzhiyun fh->len = 0;
347*4882a593Smuzhiyun fh->flags = 0;
348*4882a593Smuzhiyun if (!inode)
349*4882a593Smuzhiyun return 0;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /*
352*4882a593Smuzhiyun * !gpf means preallocated variable size fh, but fh_len could
353*4882a593Smuzhiyun * be zero in that case if encoding fh len failed.
354*4882a593Smuzhiyun */
355*4882a593Smuzhiyun err = -ENOENT;
356*4882a593Smuzhiyun if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
357*4882a593Smuzhiyun goto out_err;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /* No external buffer in a variable size allocated fh */
360*4882a593Smuzhiyun if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
361*4882a593Smuzhiyun /* Treat failure to allocate fh as failure to encode fh */
362*4882a593Smuzhiyun err = -ENOMEM;
363*4882a593Smuzhiyun ext_buf = kmalloc(fh_len, gfp);
364*4882a593Smuzhiyun if (!ext_buf)
365*4882a593Smuzhiyun goto out_err;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
368*4882a593Smuzhiyun buf = ext_buf;
369*4882a593Smuzhiyun fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun dwords = fh_len >> 2;
373*4882a593Smuzhiyun type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
374*4882a593Smuzhiyun err = -EINVAL;
375*4882a593Smuzhiyun if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
376*4882a593Smuzhiyun goto out_err;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun fh->type = type;
379*4882a593Smuzhiyun fh->len = fh_len;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun return FANOTIFY_FH_HDR_LEN + fh_len;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun out_err:
384*4882a593Smuzhiyun pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
385*4882a593Smuzhiyun type, fh_len, err);
386*4882a593Smuzhiyun kfree(ext_buf);
387*4882a593Smuzhiyun *fanotify_fh_ext_buf_ptr(fh) = NULL;
388*4882a593Smuzhiyun /* Report the event without a file identifier on encode error */
389*4882a593Smuzhiyun fh->type = FILEID_INVALID;
390*4882a593Smuzhiyun fh->len = 0;
391*4882a593Smuzhiyun return 0;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /*
395*4882a593Smuzhiyun * The inode to use as identifier when reporting fid depends on the event.
396*4882a593Smuzhiyun * Report the modified directory inode on dirent modification events.
397*4882a593Smuzhiyun * Report the "victim" inode otherwise.
398*4882a593Smuzhiyun * For example:
399*4882a593Smuzhiyun * FS_ATTRIB reports the child inode even if reported on a watched parent.
400*4882a593Smuzhiyun * FS_CREATE reports the modified dir inode and not the created inode.
401*4882a593Smuzhiyun */
fanotify_fid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir)402*4882a593Smuzhiyun static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
403*4882a593Smuzhiyun int data_type, struct inode *dir)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
406*4882a593Smuzhiyun return dir;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun return fsnotify_data_inode(data, data_type);
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun /*
412*4882a593Smuzhiyun * The inode to use as identifier when reporting dir fid depends on the event.
413*4882a593Smuzhiyun * Report the modified directory inode on dirent modification events.
414*4882a593Smuzhiyun * Report the "victim" inode if "victim" is a directory.
415*4882a593Smuzhiyun * Report the parent inode if "victim" is not a directory and event is
416*4882a593Smuzhiyun * reported to parent.
417*4882a593Smuzhiyun * Otherwise, do not report dir fid.
418*4882a593Smuzhiyun */
fanotify_dfid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir)419*4882a593Smuzhiyun static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
420*4882a593Smuzhiyun int data_type, struct inode *dir)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun struct inode *inode = fsnotify_data_inode(data, data_type);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
425*4882a593Smuzhiyun return dir;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode))
428*4882a593Smuzhiyun return inode;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun return dir;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun
fanotify_alloc_path_event(const struct path * path,gfp_t gfp)433*4882a593Smuzhiyun static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
434*4882a593Smuzhiyun gfp_t gfp)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun struct fanotify_path_event *pevent;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
439*4882a593Smuzhiyun if (!pevent)
440*4882a593Smuzhiyun return NULL;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
443*4882a593Smuzhiyun pevent->path = *path;
444*4882a593Smuzhiyun path_get(path);
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun return &pevent->fae;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun
fanotify_alloc_perm_event(const struct path * path,gfp_t gfp)449*4882a593Smuzhiyun static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
450*4882a593Smuzhiyun gfp_t gfp)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun struct fanotify_perm_event *pevent;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
455*4882a593Smuzhiyun if (!pevent)
456*4882a593Smuzhiyun return NULL;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
459*4882a593Smuzhiyun pevent->response = 0;
460*4882a593Smuzhiyun pevent->state = FAN_EVENT_INIT;
461*4882a593Smuzhiyun pevent->path = *path;
462*4882a593Smuzhiyun path_get(path);
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun return &pevent->fae;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
fanotify_alloc_fid_event(struct inode * id,__kernel_fsid_t * fsid,gfp_t gfp)467*4882a593Smuzhiyun static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
468*4882a593Smuzhiyun __kernel_fsid_t *fsid,
469*4882a593Smuzhiyun gfp_t gfp)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun struct fanotify_fid_event *ffe;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
474*4882a593Smuzhiyun if (!ffe)
475*4882a593Smuzhiyun return NULL;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
478*4882a593Smuzhiyun ffe->fsid = *fsid;
479*4882a593Smuzhiyun fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
480*4882a593Smuzhiyun gfp);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun return &ffe->fae;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
fanotify_alloc_name_event(struct inode * id,__kernel_fsid_t * fsid,const struct qstr * file_name,struct inode * child,gfp_t gfp)485*4882a593Smuzhiyun static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
486*4882a593Smuzhiyun __kernel_fsid_t *fsid,
487*4882a593Smuzhiyun const struct qstr *file_name,
488*4882a593Smuzhiyun struct inode *child,
489*4882a593Smuzhiyun gfp_t gfp)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun struct fanotify_name_event *fne;
492*4882a593Smuzhiyun struct fanotify_info *info;
493*4882a593Smuzhiyun struct fanotify_fh *dfh, *ffh;
494*4882a593Smuzhiyun unsigned int dir_fh_len = fanotify_encode_fh_len(id);
495*4882a593Smuzhiyun unsigned int child_fh_len = fanotify_encode_fh_len(child);
496*4882a593Smuzhiyun unsigned int size;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len;
499*4882a593Smuzhiyun if (child_fh_len)
500*4882a593Smuzhiyun size += FANOTIFY_FH_HDR_LEN + child_fh_len;
501*4882a593Smuzhiyun if (file_name)
502*4882a593Smuzhiyun size += file_name->len + 1;
503*4882a593Smuzhiyun fne = kmalloc(size, gfp);
504*4882a593Smuzhiyun if (!fne)
505*4882a593Smuzhiyun return NULL;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
508*4882a593Smuzhiyun fne->fsid = *fsid;
509*4882a593Smuzhiyun info = &fne->info;
510*4882a593Smuzhiyun fanotify_info_init(info);
511*4882a593Smuzhiyun dfh = fanotify_info_dir_fh(info);
512*4882a593Smuzhiyun info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, 0);
513*4882a593Smuzhiyun if (child_fh_len) {
514*4882a593Smuzhiyun ffh = fanotify_info_file_fh(info);
515*4882a593Smuzhiyun info->file_fh_totlen = fanotify_encode_fh(ffh, child, child_fh_len, 0);
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun if (file_name)
518*4882a593Smuzhiyun fanotify_info_copy_name(info, file_name);
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun pr_debug("%s: ino=%lu size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
521*4882a593Smuzhiyun __func__, id->i_ino, size, dir_fh_len, child_fh_len,
522*4882a593Smuzhiyun info->name_len, info->name_len, fanotify_info_name(info));
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun return &fne->fae;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun
fanotify_alloc_event(struct fsnotify_group * group,u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,__kernel_fsid_t * fsid)527*4882a593Smuzhiyun static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
528*4882a593Smuzhiyun u32 mask, const void *data,
529*4882a593Smuzhiyun int data_type, struct inode *dir,
530*4882a593Smuzhiyun const struct qstr *file_name,
531*4882a593Smuzhiyun __kernel_fsid_t *fsid)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun struct fanotify_event *event = NULL;
534*4882a593Smuzhiyun gfp_t gfp = GFP_KERNEL_ACCOUNT;
535*4882a593Smuzhiyun struct inode *id = fanotify_fid_inode(mask, data, data_type, dir);
536*4882a593Smuzhiyun struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
537*4882a593Smuzhiyun const struct path *path = fsnotify_data_path(data, data_type);
538*4882a593Smuzhiyun unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
539*4882a593Smuzhiyun struct mem_cgroup *old_memcg;
540*4882a593Smuzhiyun struct inode *child = NULL;
541*4882a593Smuzhiyun bool name_event = false;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
544*4882a593Smuzhiyun /*
545*4882a593Smuzhiyun * With both flags FAN_REPORT_DIR_FID and FAN_REPORT_FID, we
546*4882a593Smuzhiyun * report the child fid for events reported on a non-dir child
547*4882a593Smuzhiyun * in addition to reporting the parent fid and maybe child name.
548*4882a593Smuzhiyun */
549*4882a593Smuzhiyun if ((fid_mode & FAN_REPORT_FID) &&
550*4882a593Smuzhiyun id != dirid && !(mask & FAN_ONDIR))
551*4882a593Smuzhiyun child = id;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun id = dirid;
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun /*
556*4882a593Smuzhiyun * We record file name only in a group with FAN_REPORT_NAME
557*4882a593Smuzhiyun * and when we have a directory inode to report.
558*4882a593Smuzhiyun *
559*4882a593Smuzhiyun * For directory entry modification event, we record the fid of
560*4882a593Smuzhiyun * the directory and the name of the modified entry.
561*4882a593Smuzhiyun *
562*4882a593Smuzhiyun * For event on non-directory that is reported to parent, we
563*4882a593Smuzhiyun * record the fid of the parent and the name of the child.
564*4882a593Smuzhiyun *
565*4882a593Smuzhiyun * Even if not reporting name, we need a variable length
566*4882a593Smuzhiyun * fanotify_name_event if reporting both parent and child fids.
567*4882a593Smuzhiyun */
568*4882a593Smuzhiyun if (!(fid_mode & FAN_REPORT_NAME)) {
569*4882a593Smuzhiyun name_event = !!child;
570*4882a593Smuzhiyun file_name = NULL;
571*4882a593Smuzhiyun } else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
572*4882a593Smuzhiyun !(mask & FAN_ONDIR)) {
573*4882a593Smuzhiyun name_event = true;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun /*
578*4882a593Smuzhiyun * For queues with unlimited length lost events are not expected and
579*4882a593Smuzhiyun * can possibly have security implications. Avoid losing events when
580*4882a593Smuzhiyun * memory is short. For the limited size queues, avoid OOM killer in the
581*4882a593Smuzhiyun * target monitoring memcg as it may have security repercussion.
582*4882a593Smuzhiyun */
583*4882a593Smuzhiyun if (group->max_events == UINT_MAX)
584*4882a593Smuzhiyun gfp |= __GFP_NOFAIL;
585*4882a593Smuzhiyun else
586*4882a593Smuzhiyun gfp |= __GFP_RETRY_MAYFAIL;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /* Whoever is interested in the event, pays for the allocation. */
589*4882a593Smuzhiyun old_memcg = set_active_memcg(group->memcg);
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun if (fanotify_is_perm_event(mask)) {
592*4882a593Smuzhiyun event = fanotify_alloc_perm_event(path, gfp);
593*4882a593Smuzhiyun } else if (name_event && (file_name || child)) {
594*4882a593Smuzhiyun event = fanotify_alloc_name_event(id, fsid, file_name, child,
595*4882a593Smuzhiyun gfp);
596*4882a593Smuzhiyun } else if (fid_mode) {
597*4882a593Smuzhiyun event = fanotify_alloc_fid_event(id, fsid, gfp);
598*4882a593Smuzhiyun } else {
599*4882a593Smuzhiyun event = fanotify_alloc_path_event(path, gfp);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun if (!event)
603*4882a593Smuzhiyun goto out;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun /*
606*4882a593Smuzhiyun * Use the victim inode instead of the watching inode as the id for
607*4882a593Smuzhiyun * event queue, so event reported on parent is merged with event
608*4882a593Smuzhiyun * reported on child when both directory and child watches exist.
609*4882a593Smuzhiyun */
610*4882a593Smuzhiyun fanotify_init_event(event, (unsigned long)id, mask);
611*4882a593Smuzhiyun if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
612*4882a593Smuzhiyun event->pid = get_pid(task_pid(current));
613*4882a593Smuzhiyun else
614*4882a593Smuzhiyun event->pid = get_pid(task_tgid(current));
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun out:
617*4882a593Smuzhiyun set_active_memcg(old_memcg);
618*4882a593Smuzhiyun return event;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun * Get cached fsid of the filesystem containing the object from any connector.
623*4882a593Smuzhiyun * All connectors are supposed to have the same fsid, but we do not verify that
624*4882a593Smuzhiyun * here.
625*4882a593Smuzhiyun */
fanotify_get_fsid(struct fsnotify_iter_info * iter_info)626*4882a593Smuzhiyun static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun int type;
629*4882a593Smuzhiyun __kernel_fsid_t fsid = {};
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun fsnotify_foreach_obj_type(type) {
632*4882a593Smuzhiyun struct fsnotify_mark_connector *conn;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun if (!fsnotify_iter_should_report_type(iter_info, type))
635*4882a593Smuzhiyun continue;
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun conn = READ_ONCE(iter_info->marks[type]->connector);
638*4882a593Smuzhiyun /* Mark is just getting destroyed or created? */
639*4882a593Smuzhiyun if (!conn)
640*4882a593Smuzhiyun continue;
641*4882a593Smuzhiyun if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
642*4882a593Smuzhiyun continue;
643*4882a593Smuzhiyun /* Pairs with smp_wmb() in fsnotify_add_mark_list() */
644*4882a593Smuzhiyun smp_rmb();
645*4882a593Smuzhiyun fsid = conn->fsid;
646*4882a593Smuzhiyun if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
647*4882a593Smuzhiyun continue;
648*4882a593Smuzhiyun return fsid;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun return fsid;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun
fanotify_handle_event(struct fsnotify_group * group,u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,u32 cookie,struct fsnotify_iter_info * iter_info)654*4882a593Smuzhiyun static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
655*4882a593Smuzhiyun const void *data, int data_type,
656*4882a593Smuzhiyun struct inode *dir,
657*4882a593Smuzhiyun const struct qstr *file_name, u32 cookie,
658*4882a593Smuzhiyun struct fsnotify_iter_info *iter_info)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun int ret = 0;
661*4882a593Smuzhiyun struct fanotify_event *event;
662*4882a593Smuzhiyun struct fsnotify_event *fsn_event;
663*4882a593Smuzhiyun __kernel_fsid_t fsid = {};
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
666*4882a593Smuzhiyun BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
667*4882a593Smuzhiyun BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
668*4882a593Smuzhiyun BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
669*4882a593Smuzhiyun BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
670*4882a593Smuzhiyun BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
671*4882a593Smuzhiyun BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
672*4882a593Smuzhiyun BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
673*4882a593Smuzhiyun BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
674*4882a593Smuzhiyun BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
675*4882a593Smuzhiyun BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
676*4882a593Smuzhiyun BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
677*4882a593Smuzhiyun BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
678*4882a593Smuzhiyun BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
679*4882a593Smuzhiyun BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
680*4882a593Smuzhiyun BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
681*4882a593Smuzhiyun BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
682*4882a593Smuzhiyun BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
683*4882a593Smuzhiyun BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun mask = fanotify_group_event_mask(group, iter_info, mask, data,
688*4882a593Smuzhiyun data_type, dir);
689*4882a593Smuzhiyun if (!mask)
690*4882a593Smuzhiyun return 0;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun pr_debug("%s: group=%p mask=%x\n", __func__, group, mask);
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun if (fanotify_is_perm_event(mask)) {
695*4882a593Smuzhiyun /*
696*4882a593Smuzhiyun * fsnotify_prepare_user_wait() fails if we race with mark
697*4882a593Smuzhiyun * deletion. Just let the operation pass in that case.
698*4882a593Smuzhiyun */
699*4882a593Smuzhiyun if (!fsnotify_prepare_user_wait(iter_info))
700*4882a593Smuzhiyun return 0;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
704*4882a593Smuzhiyun fsid = fanotify_get_fsid(iter_info);
705*4882a593Smuzhiyun /* Racing with mark destruction or creation? */
706*4882a593Smuzhiyun if (!fsid.val[0] && !fsid.val[1])
707*4882a593Smuzhiyun return 0;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun event = fanotify_alloc_event(group, mask, data, data_type, dir,
711*4882a593Smuzhiyun file_name, &fsid);
712*4882a593Smuzhiyun ret = -ENOMEM;
713*4882a593Smuzhiyun if (unlikely(!event)) {
714*4882a593Smuzhiyun /*
715*4882a593Smuzhiyun * We don't queue overflow events for permission events as
716*4882a593Smuzhiyun * there the access is denied and so no event is in fact lost.
717*4882a593Smuzhiyun */
718*4882a593Smuzhiyun if (!fanotify_is_perm_event(mask))
719*4882a593Smuzhiyun fsnotify_queue_overflow(group);
720*4882a593Smuzhiyun goto finish;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun fsn_event = &event->fse;
724*4882a593Smuzhiyun ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
725*4882a593Smuzhiyun if (ret) {
726*4882a593Smuzhiyun /* Permission events shouldn't be merged */
727*4882a593Smuzhiyun BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
728*4882a593Smuzhiyun /* Our event wasn't used in the end. Free it. */
729*4882a593Smuzhiyun fsnotify_destroy_event(group, fsn_event);
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun ret = 0;
732*4882a593Smuzhiyun } else if (fanotify_is_perm_event(mask)) {
733*4882a593Smuzhiyun ret = fanotify_get_response(group, FANOTIFY_PERM(event),
734*4882a593Smuzhiyun iter_info);
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun finish:
737*4882a593Smuzhiyun if (fanotify_is_perm_event(mask))
738*4882a593Smuzhiyun fsnotify_finish_user_wait(iter_info);
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun return ret;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
fanotify_free_group_priv(struct fsnotify_group * group)743*4882a593Smuzhiyun static void fanotify_free_group_priv(struct fsnotify_group *group)
744*4882a593Smuzhiyun {
745*4882a593Smuzhiyun struct user_struct *user;
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun user = group->fanotify_data.user;
748*4882a593Smuzhiyun atomic_dec(&user->fanotify_listeners);
749*4882a593Smuzhiyun free_uid(user);
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun
fanotify_free_path_event(struct fanotify_event * event)752*4882a593Smuzhiyun static void fanotify_free_path_event(struct fanotify_event *event)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun path_put(fanotify_event_path(event));
755*4882a593Smuzhiyun kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun
fanotify_free_perm_event(struct fanotify_event * event)758*4882a593Smuzhiyun static void fanotify_free_perm_event(struct fanotify_event *event)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun path_put(fanotify_event_path(event));
761*4882a593Smuzhiyun kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun
fanotify_free_fid_event(struct fanotify_event * event)764*4882a593Smuzhiyun static void fanotify_free_fid_event(struct fanotify_event *event)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun if (fanotify_fh_has_ext_buf(&ffe->object_fh))
769*4882a593Smuzhiyun kfree(fanotify_fh_ext_buf(&ffe->object_fh));
770*4882a593Smuzhiyun kmem_cache_free(fanotify_fid_event_cachep, ffe);
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
fanotify_free_name_event(struct fanotify_event * event)773*4882a593Smuzhiyun static void fanotify_free_name_event(struct fanotify_event *event)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun kfree(FANOTIFY_NE(event));
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun
fanotify_free_event(struct fsnotify_event * fsn_event)778*4882a593Smuzhiyun static void fanotify_free_event(struct fsnotify_event *fsn_event)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun struct fanotify_event *event;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun event = FANOTIFY_E(fsn_event);
783*4882a593Smuzhiyun put_pid(event->pid);
784*4882a593Smuzhiyun switch (event->type) {
785*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_PATH:
786*4882a593Smuzhiyun fanotify_free_path_event(event);
787*4882a593Smuzhiyun break;
788*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_PATH_PERM:
789*4882a593Smuzhiyun fanotify_free_perm_event(event);
790*4882a593Smuzhiyun break;
791*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_FID:
792*4882a593Smuzhiyun fanotify_free_fid_event(event);
793*4882a593Smuzhiyun break;
794*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_FID_NAME:
795*4882a593Smuzhiyun fanotify_free_name_event(event);
796*4882a593Smuzhiyun break;
797*4882a593Smuzhiyun case FANOTIFY_EVENT_TYPE_OVERFLOW:
798*4882a593Smuzhiyun kfree(event);
799*4882a593Smuzhiyun break;
800*4882a593Smuzhiyun default:
801*4882a593Smuzhiyun WARN_ON_ONCE(1);
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun
fanotify_free_mark(struct fsnotify_mark * fsn_mark)805*4882a593Smuzhiyun static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun kmem_cache_free(fanotify_mark_cache, fsn_mark);
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun const struct fsnotify_ops fanotify_fsnotify_ops = {
811*4882a593Smuzhiyun .handle_event = fanotify_handle_event,
812*4882a593Smuzhiyun .free_group_priv = fanotify_free_group_priv,
813*4882a593Smuzhiyun .free_event = fanotify_free_event,
814*4882a593Smuzhiyun .free_mark = fanotify_free_mark,
815*4882a593Smuzhiyun };
816