Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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