xref: /openbmc/linux/fs/notify/fanotify/fanotify.c (revision 96de2506)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fdtable.h>
4 #include <linux/fsnotify_backend.h>
5 #include <linux/init.h>
6 #include <linux/jiffies.h>
7 #include <linux/kernel.h> /* UINT_MAX */
8 #include <linux/mount.h>
9 #include <linux/sched.h>
10 #include <linux/sched/user.h>
11 #include <linux/sched/signal.h>
12 #include <linux/types.h>
13 #include <linux/wait.h>
14 #include <linux/audit.h>
15 #include <linux/sched/mm.h>
16 
17 #include "fanotify.h"
18 
19 static bool should_merge(struct fsnotify_event *old_fsn,
20 			 struct fsnotify_event *new_fsn)
21 {
22 	struct fanotify_event_info *old, *new;
23 
24 	pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
25 	old = FANOTIFY_E(old_fsn);
26 	new = FANOTIFY_E(new_fsn);
27 
28 	if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
29 	    old->path.mnt == new->path.mnt &&
30 	    old->path.dentry == new->path.dentry)
31 		return true;
32 	return false;
33 }
34 
35 /* and the list better be locked by something too! */
36 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
37 {
38 	struct fsnotify_event *test_event;
39 
40 	pr_debug("%s: list=%p event=%p\n", __func__, list, event);
41 
42 	/*
43 	 * Don't merge a permission event with any other event so that we know
44 	 * the event structure we have created in fanotify_handle_event() is the
45 	 * one we should check for permission response.
46 	 */
47 	if (fanotify_is_perm_event(event->mask))
48 		return 0;
49 
50 	list_for_each_entry_reverse(test_event, list, list) {
51 		if (should_merge(test_event, event)) {
52 			test_event->mask |= event->mask;
53 			return 1;
54 		}
55 	}
56 
57 	return 0;
58 }
59 
60 static int fanotify_get_response(struct fsnotify_group *group,
61 				 struct fanotify_perm_event_info *event,
62 				 struct fsnotify_iter_info *iter_info)
63 {
64 	int ret;
65 
66 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
67 
68 	wait_event(group->fanotify_data.access_waitq, event->response);
69 
70 	/* userspace responded, convert to something usable */
71 	switch (event->response & ~FAN_AUDIT) {
72 	case FAN_ALLOW:
73 		ret = 0;
74 		break;
75 	case FAN_DENY:
76 	default:
77 		ret = -EPERM;
78 	}
79 
80 	/* Check if the response should be audited */
81 	if (event->response & FAN_AUDIT)
82 		audit_fanotify(event->response & ~FAN_AUDIT);
83 
84 	event->response = 0;
85 
86 	pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
87 		 group, event, ret);
88 
89 	return ret;
90 }
91 
92 static bool fanotify_should_send_event(struct fsnotify_iter_info *iter_info,
93 				       u32 event_mask, const void *data,
94 				       int data_type)
95 {
96 	__u32 marks_mask = 0, marks_ignored_mask = 0;
97 	const struct path *path = data;
98 	struct fsnotify_mark *mark;
99 	int type;
100 
101 	pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
102 		 __func__, iter_info->report_mask, event_mask, data, data_type);
103 
104 	/* if we don't have enough info to send an event to userspace say no */
105 	if (data_type != FSNOTIFY_EVENT_PATH)
106 		return false;
107 
108 	/* sorry, fanotify only gives a damn about files and dirs */
109 	if (!d_is_reg(path->dentry) &&
110 	    !d_can_lookup(path->dentry))
111 		return false;
112 
113 	fsnotify_foreach_obj_type(type) {
114 		if (!fsnotify_iter_should_report_type(iter_info, type))
115 			continue;
116 		mark = iter_info->marks[type];
117 		/*
118 		 * if the event is for a child and this inode doesn't care about
119 		 * events on the child, don't send it!
120 		 */
121 		if (type == FSNOTIFY_OBJ_TYPE_INODE &&
122 		    (event_mask & FS_EVENT_ON_CHILD) &&
123 		    !(mark->mask & FS_EVENT_ON_CHILD))
124 			continue;
125 
126 		marks_mask |= mark->mask;
127 		marks_ignored_mask |= mark->ignored_mask;
128 	}
129 
130 	if (d_is_dir(path->dentry) &&
131 	    !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
132 		return false;
133 
134 	if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask &
135 				 ~marks_ignored_mask)
136 		return true;
137 
138 	return false;
139 }
140 
141 struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group,
142 						 struct inode *inode, u32 mask,
143 						 const struct path *path)
144 {
145 	struct fanotify_event_info *event = NULL;
146 	gfp_t gfp = GFP_KERNEL_ACCOUNT;
147 
148 	/*
149 	 * For queues with unlimited length lost events are not expected and
150 	 * can possibly have security implications. Avoid losing events when
151 	 * memory is short.
152 	 */
153 	if (group->max_events == UINT_MAX)
154 		gfp |= __GFP_NOFAIL;
155 
156 	/* Whoever is interested in the event, pays for the allocation. */
157 	memalloc_use_memcg(group->memcg);
158 
159 	if (fanotify_is_perm_event(mask)) {
160 		struct fanotify_perm_event_info *pevent;
161 
162 		pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
163 		if (!pevent)
164 			goto out;
165 		event = &pevent->fae;
166 		pevent->response = 0;
167 		goto init;
168 	}
169 	event = kmem_cache_alloc(fanotify_event_cachep, gfp);
170 	if (!event)
171 		goto out;
172 init: __maybe_unused
173 	fsnotify_init_event(&event->fse, inode, mask);
174 	event->tgid = get_pid(task_tgid(current));
175 	if (path) {
176 		event->path = *path;
177 		path_get(&event->path);
178 	} else {
179 		event->path.mnt = NULL;
180 		event->path.dentry = NULL;
181 	}
182 out:
183 	memalloc_unuse_memcg();
184 	return event;
185 }
186 
187 static int fanotify_handle_event(struct fsnotify_group *group,
188 				 struct inode *inode,
189 				 u32 mask, const void *data, int data_type,
190 				 const unsigned char *file_name, u32 cookie,
191 				 struct fsnotify_iter_info *iter_info)
192 {
193 	int ret = 0;
194 	struct fanotify_event_info *event;
195 	struct fsnotify_event *fsn_event;
196 
197 	BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
198 	BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
199 	BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
200 	BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
201 	BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
202 	BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
203 	BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
204 	BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
205 	BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
206 	BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
207 
208 	if (!fanotify_should_send_event(iter_info, mask, data, data_type))
209 		return 0;
210 
211 	pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
212 		 mask);
213 
214 	if (fanotify_is_perm_event(mask)) {
215 		/*
216 		 * fsnotify_prepare_user_wait() fails if we race with mark
217 		 * deletion.  Just let the operation pass in that case.
218 		 */
219 		if (!fsnotify_prepare_user_wait(iter_info))
220 			return 0;
221 	}
222 
223 	event = fanotify_alloc_event(group, inode, mask, data);
224 	ret = -ENOMEM;
225 	if (unlikely(!event)) {
226 		/*
227 		 * We don't queue overflow events for permission events as
228 		 * there the access is denied and so no event is in fact lost.
229 		 */
230 		if (!fanotify_is_perm_event(mask))
231 			fsnotify_queue_overflow(group);
232 		goto finish;
233 	}
234 
235 	fsn_event = &event->fse;
236 	ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
237 	if (ret) {
238 		/* Permission events shouldn't be merged */
239 		BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS);
240 		/* Our event wasn't used in the end. Free it. */
241 		fsnotify_destroy_event(group, fsn_event);
242 
243 		ret = 0;
244 	} else if (fanotify_is_perm_event(mask)) {
245 		ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
246 					    iter_info);
247 		fsnotify_destroy_event(group, fsn_event);
248 	}
249 finish:
250 	if (fanotify_is_perm_event(mask))
251 		fsnotify_finish_user_wait(iter_info);
252 
253 	return ret;
254 }
255 
256 static void fanotify_free_group_priv(struct fsnotify_group *group)
257 {
258 	struct user_struct *user;
259 
260 	user = group->fanotify_data.user;
261 	atomic_dec(&user->fanotify_listeners);
262 	free_uid(user);
263 }
264 
265 static void fanotify_free_event(struct fsnotify_event *fsn_event)
266 {
267 	struct fanotify_event_info *event;
268 
269 	event = FANOTIFY_E(fsn_event);
270 	path_put(&event->path);
271 	put_pid(event->tgid);
272 	if (fanotify_is_perm_event(fsn_event->mask)) {
273 		kmem_cache_free(fanotify_perm_event_cachep,
274 				FANOTIFY_PE(fsn_event));
275 		return;
276 	}
277 	kmem_cache_free(fanotify_event_cachep, event);
278 }
279 
280 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
281 {
282 	kmem_cache_free(fanotify_mark_cache, fsn_mark);
283 }
284 
285 const struct fsnotify_ops fanotify_fsnotify_ops = {
286 	.handle_event = fanotify_handle_event,
287 	.free_group_priv = fanotify_free_group_priv,
288 	.free_event = fanotify_free_event,
289 	.free_mark = fanotify_free_mark,
290 };
291