1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fcntl.h>
4 #include <linux/file.h>
5 #include <linux/fs.h>
6 #include <linux/anon_inodes.h>
7 #include <linux/fsnotify_backend.h>
8 #include <linux/init.h>
9 #include <linux/mount.h>
10 #include <linux/namei.h>
11 #include <linux/poll.h>
12 #include <linux/security.h>
13 #include <linux/syscalls.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/uaccess.h>
17 #include <linux/compat.h>
18 #include <linux/sched/signal.h>
19 #include <linux/memcontrol.h>
20 #include <linux/statfs.h>
21 #include <linux/exportfs.h>
22 
23 #include <asm/ioctls.h>
24 
25 #include "../../mount.h"
26 #include "../fdinfo.h"
27 #include "fanotify.h"
28 
29 #define FANOTIFY_DEFAULT_MAX_EVENTS	16384
30 #define FANOTIFY_DEFAULT_MAX_MARKS	8192
31 #define FANOTIFY_DEFAULT_MAX_LISTENERS	128
32 
33 /*
34  * All flags that may be specified in parameter event_f_flags of fanotify_init.
35  *
36  * Internal and external open flags are stored together in field f_flags of
37  * struct file. Only external open flags shall be allowed in event_f_flags.
38  * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
39  * excluded.
40  */
41 #define	FANOTIFY_INIT_ALL_EVENT_F_BITS				( \
42 		O_ACCMODE	| O_APPEND	| O_NONBLOCK	| \
43 		__O_SYNC	| O_DSYNC	| O_CLOEXEC     | \
44 		O_LARGEFILE	| O_NOATIME	)
45 
46 extern const struct fsnotify_ops fanotify_fsnotify_ops;
47 
48 struct kmem_cache *fanotify_mark_cache __read_mostly;
49 struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
50 struct kmem_cache *fanotify_path_event_cachep __read_mostly;
51 struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
52 
53 #define FANOTIFY_EVENT_ALIGN 4
54 #define FANOTIFY_INFO_HDR_LEN \
55 	(sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
56 
57 static int fanotify_fid_info_len(int fh_len, int name_len)
58 {
59 	int info_len = fh_len;
60 
61 	if (name_len)
62 		info_len += name_len + 1;
63 
64 	return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
65 }
66 
67 static int fanotify_event_info_len(struct fanotify_event *event)
68 {
69 	int info_len = 0;
70 	int fh_len = fanotify_event_object_fh_len(event);
71 
72 	if (fh_len)
73 		info_len += fanotify_fid_info_len(fh_len, 0);
74 
75 	if (fanotify_event_name_len(event)) {
76 		struct fanotify_name_event *fne = FANOTIFY_NE(event);
77 
78 		info_len += fanotify_fid_info_len(fne->dir_fh.len,
79 						  fne->name_len);
80 	}
81 
82 	return info_len;
83 }
84 
85 /*
86  * Get an fanotify notification event if one exists and is small
87  * enough to fit in "count". Return an error pointer if the count
88  * is not large enough. When permission event is dequeued, its state is
89  * updated accordingly.
90  */
91 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
92 					    size_t count)
93 {
94 	size_t event_size = FAN_EVENT_METADATA_LEN;
95 	struct fanotify_event *event = NULL;
96 
97 	pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
98 
99 	spin_lock(&group->notification_lock);
100 	if (fsnotify_notify_queue_is_empty(group))
101 		goto out;
102 
103 	if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
104 		event_size += fanotify_event_info_len(
105 			FANOTIFY_E(fsnotify_peek_first_event(group)));
106 	}
107 
108 	if (event_size > count) {
109 		event = ERR_PTR(-EINVAL);
110 		goto out;
111 	}
112 	event = FANOTIFY_E(fsnotify_remove_first_event(group));
113 	if (fanotify_is_perm_event(event->mask))
114 		FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
115 out:
116 	spin_unlock(&group->notification_lock);
117 	return event;
118 }
119 
120 static int create_fd(struct fsnotify_group *group, struct path *path,
121 		     struct file **file)
122 {
123 	int client_fd;
124 	struct file *new_file;
125 
126 	client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
127 	if (client_fd < 0)
128 		return client_fd;
129 
130 	/*
131 	 * we need a new file handle for the userspace program so it can read even if it was
132 	 * originally opened O_WRONLY.
133 	 */
134 	new_file = dentry_open(path,
135 			       group->fanotify_data.f_flags | FMODE_NONOTIFY,
136 			       current_cred());
137 	if (IS_ERR(new_file)) {
138 		/*
139 		 * we still send an event even if we can't open the file.  this
140 		 * can happen when say tasks are gone and we try to open their
141 		 * /proc files or we try to open a WRONLY file like in sysfs
142 		 * we just send the errno to userspace since there isn't much
143 		 * else we can do.
144 		 */
145 		put_unused_fd(client_fd);
146 		client_fd = PTR_ERR(new_file);
147 	} else {
148 		*file = new_file;
149 	}
150 
151 	return client_fd;
152 }
153 
154 /*
155  * Finish processing of permission event by setting it to ANSWERED state and
156  * drop group->notification_lock.
157  */
158 static void finish_permission_event(struct fsnotify_group *group,
159 				    struct fanotify_perm_event *event,
160 				    unsigned int response)
161 				    __releases(&group->notification_lock)
162 {
163 	bool destroy = false;
164 
165 	assert_spin_locked(&group->notification_lock);
166 	event->response = response;
167 	if (event->state == FAN_EVENT_CANCELED)
168 		destroy = true;
169 	else
170 		event->state = FAN_EVENT_ANSWERED;
171 	spin_unlock(&group->notification_lock);
172 	if (destroy)
173 		fsnotify_destroy_event(group, &event->fae.fse);
174 }
175 
176 static int process_access_response(struct fsnotify_group *group,
177 				   struct fanotify_response *response_struct)
178 {
179 	struct fanotify_perm_event *event;
180 	int fd = response_struct->fd;
181 	int response = response_struct->response;
182 
183 	pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
184 		 fd, response);
185 	/*
186 	 * make sure the response is valid, if invalid we do nothing and either
187 	 * userspace can send a valid response or we will clean it up after the
188 	 * timeout
189 	 */
190 	switch (response & ~FAN_AUDIT) {
191 	case FAN_ALLOW:
192 	case FAN_DENY:
193 		break;
194 	default:
195 		return -EINVAL;
196 	}
197 
198 	if (fd < 0)
199 		return -EINVAL;
200 
201 	if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
202 		return -EINVAL;
203 
204 	spin_lock(&group->notification_lock);
205 	list_for_each_entry(event, &group->fanotify_data.access_list,
206 			    fae.fse.list) {
207 		if (event->fd != fd)
208 			continue;
209 
210 		list_del_init(&event->fae.fse.list);
211 		finish_permission_event(group, event, response);
212 		wake_up(&group->fanotify_data.access_waitq);
213 		return 0;
214 	}
215 	spin_unlock(&group->notification_lock);
216 
217 	return -ENOENT;
218 }
219 
220 static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
221 			     const char *name, size_t name_len,
222 			     char __user *buf, size_t count)
223 {
224 	struct fanotify_event_info_fid info = { };
225 	struct file_handle handle = { };
226 	unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
227 	size_t fh_len = fh ? fh->len : 0;
228 	size_t info_len = fanotify_fid_info_len(fh_len, name_len);
229 	size_t len = info_len;
230 
231 	pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
232 		 __func__, fh_len, name_len, info_len, count);
233 
234 	if (!fh_len || (name && !name_len))
235 		return 0;
236 
237 	if (WARN_ON_ONCE(len < sizeof(info) || len > count))
238 		return -EFAULT;
239 
240 	/*
241 	 * Copy event info fid header followed by variable sized file handle
242 	 * and optionally followed by variable sized filename.
243 	 */
244 	info.hdr.info_type = name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
245 					FAN_EVENT_INFO_TYPE_FID;
246 	info.hdr.len = len;
247 	info.fsid = *fsid;
248 	if (copy_to_user(buf, &info, sizeof(info)))
249 		return -EFAULT;
250 
251 	buf += sizeof(info);
252 	len -= sizeof(info);
253 	if (WARN_ON_ONCE(len < sizeof(handle)))
254 		return -EFAULT;
255 
256 	handle.handle_type = fh->type;
257 	handle.handle_bytes = fh_len;
258 	if (copy_to_user(buf, &handle, sizeof(handle)))
259 		return -EFAULT;
260 
261 	buf += sizeof(handle);
262 	len -= sizeof(handle);
263 	if (WARN_ON_ONCE(len < fh_len))
264 		return -EFAULT;
265 
266 	/*
267 	 * For an inline fh and inline file name, copy through stack to exclude
268 	 * the copy from usercopy hardening protections.
269 	 */
270 	fh_buf = fanotify_fh_buf(fh);
271 	if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
272 		memcpy(bounce, fh_buf, fh_len);
273 		fh_buf = bounce;
274 	}
275 	if (copy_to_user(buf, fh_buf, fh_len))
276 		return -EFAULT;
277 
278 	buf += fh_len;
279 	len -= fh_len;
280 
281 	if (name_len) {
282 		/* Copy the filename with terminating null */
283 		name_len++;
284 		if (WARN_ON_ONCE(len < name_len))
285 			return -EFAULT;
286 
287 		if (copy_to_user(buf, name, name_len))
288 			return -EFAULT;
289 
290 		buf += name_len;
291 		len -= name_len;
292 	}
293 
294 	/* Pad with 0's */
295 	WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
296 	if (len > 0 && clear_user(buf, len))
297 		return -EFAULT;
298 
299 	return info_len;
300 }
301 
302 static ssize_t copy_event_to_user(struct fsnotify_group *group,
303 				  struct fanotify_event *event,
304 				  char __user *buf, size_t count)
305 {
306 	struct fanotify_event_metadata metadata;
307 	struct path *path = fanotify_event_path(event);
308 	struct file *f = NULL;
309 	int ret, fd = FAN_NOFD;
310 
311 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
312 
313 	metadata.event_len = FAN_EVENT_METADATA_LEN +
314 					fanotify_event_info_len(event);
315 	metadata.metadata_len = FAN_EVENT_METADATA_LEN;
316 	metadata.vers = FANOTIFY_METADATA_VERSION;
317 	metadata.reserved = 0;
318 	metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
319 	metadata.pid = pid_vnr(event->pid);
320 
321 	if (path && path->mnt && path->dentry) {
322 		fd = create_fd(group, path, &f);
323 		if (fd < 0)
324 			return fd;
325 	}
326 	metadata.fd = fd;
327 
328 	ret = -EFAULT;
329 	/*
330 	 * Sanity check copy size in case get_one_event() and
331 	 * event_len sizes ever get out of sync.
332 	 */
333 	if (WARN_ON_ONCE(metadata.event_len > count))
334 		goto out_close_fd;
335 
336 	if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
337 		goto out_close_fd;
338 
339 	buf += FAN_EVENT_METADATA_LEN;
340 	count -= FAN_EVENT_METADATA_LEN;
341 
342 	if (fanotify_is_perm_event(event->mask))
343 		FANOTIFY_PERM(event)->fd = fd;
344 
345 	if (f)
346 		fd_install(fd, f);
347 
348 	/* Event info records order is: dir fid + name, child fid */
349 	if (fanotify_event_name_len(event)) {
350 		struct fanotify_name_event *fne = FANOTIFY_NE(event);
351 
352 		ret = copy_info_to_user(fanotify_event_fsid(event),
353 					fanotify_event_dir_fh(event),
354 					fne->name, fne->name_len,
355 					buf, count);
356 		if (ret < 0)
357 			return ret;
358 
359 		buf += ret;
360 		count -= ret;
361 	}
362 
363 	if (fanotify_event_object_fh_len(event)) {
364 		ret = copy_info_to_user(fanotify_event_fsid(event),
365 					fanotify_event_object_fh(event),
366 					NULL, 0, buf, count);
367 		if (ret < 0)
368 			return ret;
369 
370 		buf += ret;
371 		count -= ret;
372 	}
373 
374 	return metadata.event_len;
375 
376 out_close_fd:
377 	if (fd != FAN_NOFD) {
378 		put_unused_fd(fd);
379 		fput(f);
380 	}
381 	return ret;
382 }
383 
384 /* intofiy userspace file descriptor functions */
385 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
386 {
387 	struct fsnotify_group *group = file->private_data;
388 	__poll_t ret = 0;
389 
390 	poll_wait(file, &group->notification_waitq, wait);
391 	spin_lock(&group->notification_lock);
392 	if (!fsnotify_notify_queue_is_empty(group))
393 		ret = EPOLLIN | EPOLLRDNORM;
394 	spin_unlock(&group->notification_lock);
395 
396 	return ret;
397 }
398 
399 static ssize_t fanotify_read(struct file *file, char __user *buf,
400 			     size_t count, loff_t *pos)
401 {
402 	struct fsnotify_group *group;
403 	struct fanotify_event *event;
404 	char __user *start;
405 	int ret;
406 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
407 
408 	start = buf;
409 	group = file->private_data;
410 
411 	pr_debug("%s: group=%p\n", __func__, group);
412 
413 	add_wait_queue(&group->notification_waitq, &wait);
414 	while (1) {
415 		event = get_one_event(group, count);
416 		if (IS_ERR(event)) {
417 			ret = PTR_ERR(event);
418 			break;
419 		}
420 
421 		if (!event) {
422 			ret = -EAGAIN;
423 			if (file->f_flags & O_NONBLOCK)
424 				break;
425 
426 			ret = -ERESTARTSYS;
427 			if (signal_pending(current))
428 				break;
429 
430 			if (start != buf)
431 				break;
432 
433 			wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
434 			continue;
435 		}
436 
437 		ret = copy_event_to_user(group, event, buf, count);
438 		if (unlikely(ret == -EOPENSTALE)) {
439 			/*
440 			 * We cannot report events with stale fd so drop it.
441 			 * Setting ret to 0 will continue the event loop and
442 			 * do the right thing if there are no more events to
443 			 * read (i.e. return bytes read, -EAGAIN or wait).
444 			 */
445 			ret = 0;
446 		}
447 
448 		/*
449 		 * Permission events get queued to wait for response.  Other
450 		 * events can be destroyed now.
451 		 */
452 		if (!fanotify_is_perm_event(event->mask)) {
453 			fsnotify_destroy_event(group, &event->fse);
454 		} else {
455 			if (ret <= 0) {
456 				spin_lock(&group->notification_lock);
457 				finish_permission_event(group,
458 					FANOTIFY_PERM(event), FAN_DENY);
459 				wake_up(&group->fanotify_data.access_waitq);
460 			} else {
461 				spin_lock(&group->notification_lock);
462 				list_add_tail(&event->fse.list,
463 					&group->fanotify_data.access_list);
464 				spin_unlock(&group->notification_lock);
465 			}
466 		}
467 		if (ret < 0)
468 			break;
469 		buf += ret;
470 		count -= ret;
471 	}
472 	remove_wait_queue(&group->notification_waitq, &wait);
473 
474 	if (start != buf && ret != -EFAULT)
475 		ret = buf - start;
476 	return ret;
477 }
478 
479 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
480 {
481 	struct fanotify_response response = { .fd = -1, .response = -1 };
482 	struct fsnotify_group *group;
483 	int ret;
484 
485 	if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
486 		return -EINVAL;
487 
488 	group = file->private_data;
489 
490 	if (count < sizeof(response))
491 		return -EINVAL;
492 
493 	count = sizeof(response);
494 
495 	pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
496 
497 	if (copy_from_user(&response, buf, count))
498 		return -EFAULT;
499 
500 	ret = process_access_response(group, &response);
501 	if (ret < 0)
502 		count = ret;
503 
504 	return count;
505 }
506 
507 static int fanotify_release(struct inode *ignored, struct file *file)
508 {
509 	struct fsnotify_group *group = file->private_data;
510 
511 	/*
512 	 * Stop new events from arriving in the notification queue. since
513 	 * userspace cannot use fanotify fd anymore, no event can enter or
514 	 * leave access_list by now either.
515 	 */
516 	fsnotify_group_stop_queueing(group);
517 
518 	/*
519 	 * Process all permission events on access_list and notification queue
520 	 * and simulate reply from userspace.
521 	 */
522 	spin_lock(&group->notification_lock);
523 	while (!list_empty(&group->fanotify_data.access_list)) {
524 		struct fanotify_perm_event *event;
525 
526 		event = list_first_entry(&group->fanotify_data.access_list,
527 				struct fanotify_perm_event, fae.fse.list);
528 		list_del_init(&event->fae.fse.list);
529 		finish_permission_event(group, event, FAN_ALLOW);
530 		spin_lock(&group->notification_lock);
531 	}
532 
533 	/*
534 	 * Destroy all non-permission events. For permission events just
535 	 * dequeue them and set the response. They will be freed once the
536 	 * response is consumed and fanotify_get_response() returns.
537 	 */
538 	while (!fsnotify_notify_queue_is_empty(group)) {
539 		struct fanotify_event *event;
540 
541 		event = FANOTIFY_E(fsnotify_remove_first_event(group));
542 		if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
543 			spin_unlock(&group->notification_lock);
544 			fsnotify_destroy_event(group, &event->fse);
545 		} else {
546 			finish_permission_event(group, FANOTIFY_PERM(event),
547 						FAN_ALLOW);
548 		}
549 		spin_lock(&group->notification_lock);
550 	}
551 	spin_unlock(&group->notification_lock);
552 
553 	/* Response for all permission events it set, wakeup waiters */
554 	wake_up(&group->fanotify_data.access_waitq);
555 
556 	/* matches the fanotify_init->fsnotify_alloc_group */
557 	fsnotify_destroy_group(group);
558 
559 	return 0;
560 }
561 
562 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
563 {
564 	struct fsnotify_group *group;
565 	struct fsnotify_event *fsn_event;
566 	void __user *p;
567 	int ret = -ENOTTY;
568 	size_t send_len = 0;
569 
570 	group = file->private_data;
571 
572 	p = (void __user *) arg;
573 
574 	switch (cmd) {
575 	case FIONREAD:
576 		spin_lock(&group->notification_lock);
577 		list_for_each_entry(fsn_event, &group->notification_list, list)
578 			send_len += FAN_EVENT_METADATA_LEN;
579 		spin_unlock(&group->notification_lock);
580 		ret = put_user(send_len, (int __user *) p);
581 		break;
582 	}
583 
584 	return ret;
585 }
586 
587 static const struct file_operations fanotify_fops = {
588 	.show_fdinfo	= fanotify_show_fdinfo,
589 	.poll		= fanotify_poll,
590 	.read		= fanotify_read,
591 	.write		= fanotify_write,
592 	.fasync		= NULL,
593 	.release	= fanotify_release,
594 	.unlocked_ioctl	= fanotify_ioctl,
595 	.compat_ioctl	= compat_ptr_ioctl,
596 	.llseek		= noop_llseek,
597 };
598 
599 static int fanotify_find_path(int dfd, const char __user *filename,
600 			      struct path *path, unsigned int flags, __u64 mask,
601 			      unsigned int obj_type)
602 {
603 	int ret;
604 
605 	pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
606 		 dfd, filename, flags);
607 
608 	if (filename == NULL) {
609 		struct fd f = fdget(dfd);
610 
611 		ret = -EBADF;
612 		if (!f.file)
613 			goto out;
614 
615 		ret = -ENOTDIR;
616 		if ((flags & FAN_MARK_ONLYDIR) &&
617 		    !(S_ISDIR(file_inode(f.file)->i_mode))) {
618 			fdput(f);
619 			goto out;
620 		}
621 
622 		*path = f.file->f_path;
623 		path_get(path);
624 		fdput(f);
625 	} else {
626 		unsigned int lookup_flags = 0;
627 
628 		if (!(flags & FAN_MARK_DONT_FOLLOW))
629 			lookup_flags |= LOOKUP_FOLLOW;
630 		if (flags & FAN_MARK_ONLYDIR)
631 			lookup_flags |= LOOKUP_DIRECTORY;
632 
633 		ret = user_path_at(dfd, filename, lookup_flags, path);
634 		if (ret)
635 			goto out;
636 	}
637 
638 	/* you can only watch an inode if you have read permissions on it */
639 	ret = inode_permission(path->dentry->d_inode, MAY_READ);
640 	if (ret) {
641 		path_put(path);
642 		goto out;
643 	}
644 
645 	ret = security_path_notify(path, mask, obj_type);
646 	if (ret)
647 		path_put(path);
648 
649 out:
650 	return ret;
651 }
652 
653 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
654 					    __u32 mask,
655 					    unsigned int flags,
656 					    int *destroy)
657 {
658 	__u32 oldmask = 0;
659 
660 	spin_lock(&fsn_mark->lock);
661 	if (!(flags & FAN_MARK_IGNORED_MASK)) {
662 		oldmask = fsn_mark->mask;
663 		fsn_mark->mask &= ~mask;
664 	} else {
665 		fsn_mark->ignored_mask &= ~mask;
666 	}
667 	*destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
668 	spin_unlock(&fsn_mark->lock);
669 
670 	return mask & oldmask;
671 }
672 
673 static int fanotify_remove_mark(struct fsnotify_group *group,
674 				fsnotify_connp_t *connp, __u32 mask,
675 				unsigned int flags)
676 {
677 	struct fsnotify_mark *fsn_mark = NULL;
678 	__u32 removed;
679 	int destroy_mark;
680 
681 	mutex_lock(&group->mark_mutex);
682 	fsn_mark = fsnotify_find_mark(connp, group);
683 	if (!fsn_mark) {
684 		mutex_unlock(&group->mark_mutex);
685 		return -ENOENT;
686 	}
687 
688 	removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
689 						 &destroy_mark);
690 	if (removed & fsnotify_conn_mask(fsn_mark->connector))
691 		fsnotify_recalc_mask(fsn_mark->connector);
692 	if (destroy_mark)
693 		fsnotify_detach_mark(fsn_mark);
694 	mutex_unlock(&group->mark_mutex);
695 	if (destroy_mark)
696 		fsnotify_free_mark(fsn_mark);
697 
698 	/* matches the fsnotify_find_mark() */
699 	fsnotify_put_mark(fsn_mark);
700 	return 0;
701 }
702 
703 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
704 					 struct vfsmount *mnt, __u32 mask,
705 					 unsigned int flags)
706 {
707 	return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
708 				    mask, flags);
709 }
710 
711 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
712 				      struct super_block *sb, __u32 mask,
713 				      unsigned int flags)
714 {
715 	return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
716 }
717 
718 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
719 				      struct inode *inode, __u32 mask,
720 				      unsigned int flags)
721 {
722 	return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
723 				    flags);
724 }
725 
726 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
727 				       __u32 mask,
728 				       unsigned int flags)
729 {
730 	__u32 oldmask = -1;
731 
732 	spin_lock(&fsn_mark->lock);
733 	if (!(flags & FAN_MARK_IGNORED_MASK)) {
734 		oldmask = fsn_mark->mask;
735 		fsn_mark->mask |= mask;
736 	} else {
737 		fsn_mark->ignored_mask |= mask;
738 		if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
739 			fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
740 	}
741 	spin_unlock(&fsn_mark->lock);
742 
743 	return mask & ~oldmask;
744 }
745 
746 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
747 						   fsnotify_connp_t *connp,
748 						   unsigned int type,
749 						   __kernel_fsid_t *fsid)
750 {
751 	struct fsnotify_mark *mark;
752 	int ret;
753 
754 	if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
755 		return ERR_PTR(-ENOSPC);
756 
757 	mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
758 	if (!mark)
759 		return ERR_PTR(-ENOMEM);
760 
761 	fsnotify_init_mark(mark, group);
762 	ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
763 	if (ret) {
764 		fsnotify_put_mark(mark);
765 		return ERR_PTR(ret);
766 	}
767 
768 	return mark;
769 }
770 
771 
772 static int fanotify_add_mark(struct fsnotify_group *group,
773 			     fsnotify_connp_t *connp, unsigned int type,
774 			     __u32 mask, unsigned int flags,
775 			     __kernel_fsid_t *fsid)
776 {
777 	struct fsnotify_mark *fsn_mark;
778 	__u32 added;
779 
780 	mutex_lock(&group->mark_mutex);
781 	fsn_mark = fsnotify_find_mark(connp, group);
782 	if (!fsn_mark) {
783 		fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
784 		if (IS_ERR(fsn_mark)) {
785 			mutex_unlock(&group->mark_mutex);
786 			return PTR_ERR(fsn_mark);
787 		}
788 	}
789 	added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
790 	if (added & ~fsnotify_conn_mask(fsn_mark->connector))
791 		fsnotify_recalc_mask(fsn_mark->connector);
792 	mutex_unlock(&group->mark_mutex);
793 
794 	fsnotify_put_mark(fsn_mark);
795 	return 0;
796 }
797 
798 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
799 				      struct vfsmount *mnt, __u32 mask,
800 				      unsigned int flags, __kernel_fsid_t *fsid)
801 {
802 	return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
803 				 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
804 }
805 
806 static int fanotify_add_sb_mark(struct fsnotify_group *group,
807 				struct super_block *sb, __u32 mask,
808 				unsigned int flags, __kernel_fsid_t *fsid)
809 {
810 	return fanotify_add_mark(group, &sb->s_fsnotify_marks,
811 				 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
812 }
813 
814 static int fanotify_add_inode_mark(struct fsnotify_group *group,
815 				   struct inode *inode, __u32 mask,
816 				   unsigned int flags, __kernel_fsid_t *fsid)
817 {
818 	pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
819 
820 	/*
821 	 * If some other task has this inode open for write we should not add
822 	 * an ignored mark, unless that ignored mark is supposed to survive
823 	 * modification changes anyway.
824 	 */
825 	if ((flags & FAN_MARK_IGNORED_MASK) &&
826 	    !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
827 	    inode_is_open_for_write(inode))
828 		return 0;
829 
830 	return fanotify_add_mark(group, &inode->i_fsnotify_marks,
831 				 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
832 }
833 
834 /* fanotify syscalls */
835 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
836 {
837 	struct fsnotify_group *group;
838 	int f_flags, fd;
839 	struct user_struct *user;
840 	struct fanotify_event *oevent;
841 
842 	pr_debug("%s: flags=%x event_f_flags=%x\n",
843 		 __func__, flags, event_f_flags);
844 
845 	if (!capable(CAP_SYS_ADMIN))
846 		return -EPERM;
847 
848 #ifdef CONFIG_AUDITSYSCALL
849 	if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
850 #else
851 	if (flags & ~FANOTIFY_INIT_FLAGS)
852 #endif
853 		return -EINVAL;
854 
855 	if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
856 		return -EINVAL;
857 
858 	switch (event_f_flags & O_ACCMODE) {
859 	case O_RDONLY:
860 	case O_RDWR:
861 	case O_WRONLY:
862 		break;
863 	default:
864 		return -EINVAL;
865 	}
866 
867 	if ((flags & FAN_REPORT_FID) &&
868 	    (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
869 		return -EINVAL;
870 
871 	user = get_current_user();
872 	if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
873 		free_uid(user);
874 		return -EMFILE;
875 	}
876 
877 	f_flags = O_RDWR | FMODE_NONOTIFY;
878 	if (flags & FAN_CLOEXEC)
879 		f_flags |= O_CLOEXEC;
880 	if (flags & FAN_NONBLOCK)
881 		f_flags |= O_NONBLOCK;
882 
883 	/* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
884 	group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
885 	if (IS_ERR(group)) {
886 		free_uid(user);
887 		return PTR_ERR(group);
888 	}
889 
890 	group->fanotify_data.user = user;
891 	group->fanotify_data.flags = flags;
892 	atomic_inc(&user->fanotify_listeners);
893 	group->memcg = get_mem_cgroup_from_mm(current->mm);
894 
895 	oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL,
896 				      FSNOTIFY_EVENT_NONE, NULL, NULL);
897 	if (unlikely(!oevent)) {
898 		fd = -ENOMEM;
899 		goto out_destroy_group;
900 	}
901 	group->overflow_event = &oevent->fse;
902 
903 	if (force_o_largefile())
904 		event_f_flags |= O_LARGEFILE;
905 	group->fanotify_data.f_flags = event_f_flags;
906 	init_waitqueue_head(&group->fanotify_data.access_waitq);
907 	INIT_LIST_HEAD(&group->fanotify_data.access_list);
908 	switch (flags & FANOTIFY_CLASS_BITS) {
909 	case FAN_CLASS_NOTIF:
910 		group->priority = FS_PRIO_0;
911 		break;
912 	case FAN_CLASS_CONTENT:
913 		group->priority = FS_PRIO_1;
914 		break;
915 	case FAN_CLASS_PRE_CONTENT:
916 		group->priority = FS_PRIO_2;
917 		break;
918 	default:
919 		fd = -EINVAL;
920 		goto out_destroy_group;
921 	}
922 
923 	if (flags & FAN_UNLIMITED_QUEUE) {
924 		fd = -EPERM;
925 		if (!capable(CAP_SYS_ADMIN))
926 			goto out_destroy_group;
927 		group->max_events = UINT_MAX;
928 	} else {
929 		group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
930 	}
931 
932 	if (flags & FAN_UNLIMITED_MARKS) {
933 		fd = -EPERM;
934 		if (!capable(CAP_SYS_ADMIN))
935 			goto out_destroy_group;
936 		group->fanotify_data.max_marks = UINT_MAX;
937 	} else {
938 		group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
939 	}
940 
941 	if (flags & FAN_ENABLE_AUDIT) {
942 		fd = -EPERM;
943 		if (!capable(CAP_AUDIT_WRITE))
944 			goto out_destroy_group;
945 	}
946 
947 	fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
948 	if (fd < 0)
949 		goto out_destroy_group;
950 
951 	return fd;
952 
953 out_destroy_group:
954 	fsnotify_destroy_group(group);
955 	return fd;
956 }
957 
958 /* Check if filesystem can encode a unique fid */
959 static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
960 {
961 	__kernel_fsid_t root_fsid;
962 	int err;
963 
964 	/*
965 	 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
966 	 */
967 	err = vfs_get_fsid(path->dentry, fsid);
968 	if (err)
969 		return err;
970 
971 	if (!fsid->val[0] && !fsid->val[1])
972 		return -ENODEV;
973 
974 	/*
975 	 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
976 	 * which uses a different fsid than sb root.
977 	 */
978 	err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
979 	if (err)
980 		return err;
981 
982 	if (root_fsid.val[0] != fsid->val[0] ||
983 	    root_fsid.val[1] != fsid->val[1])
984 		return -EXDEV;
985 
986 	/*
987 	 * We need to make sure that the file system supports at least
988 	 * encoding a file handle so user can use name_to_handle_at() to
989 	 * compare fid returned with event to the file handle of watched
990 	 * objects. However, name_to_handle_at() requires that the
991 	 * filesystem also supports decoding file handles.
992 	 */
993 	if (!path->dentry->d_sb->s_export_op ||
994 	    !path->dentry->d_sb->s_export_op->fh_to_dentry)
995 		return -EOPNOTSUPP;
996 
997 	return 0;
998 }
999 
1000 static int fanotify_events_supported(struct path *path, __u64 mask)
1001 {
1002 	/*
1003 	 * Some filesystems such as 'proc' acquire unusual locks when opening
1004 	 * files. For them fanotify permission events have high chances of
1005 	 * deadlocking the system - open done when reporting fanotify event
1006 	 * blocks on this "unusual" lock while another process holding the lock
1007 	 * waits for fanotify permission event to be answered. Just disallow
1008 	 * permission events for such filesystems.
1009 	 */
1010 	if (mask & FANOTIFY_PERM_EVENTS &&
1011 	    path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1012 		return -EINVAL;
1013 	return 0;
1014 }
1015 
1016 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1017 			    int dfd, const char  __user *pathname)
1018 {
1019 	struct inode *inode = NULL;
1020 	struct vfsmount *mnt = NULL;
1021 	struct fsnotify_group *group;
1022 	struct fd f;
1023 	struct path path;
1024 	__kernel_fsid_t __fsid, *fsid = NULL;
1025 	u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1026 	unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1027 	unsigned int obj_type;
1028 	int ret;
1029 
1030 	pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1031 		 __func__, fanotify_fd, flags, dfd, pathname, mask);
1032 
1033 	/* we only use the lower 32 bits as of right now. */
1034 	if (mask & ((__u64)0xffffffff << 32))
1035 		return -EINVAL;
1036 
1037 	if (flags & ~FANOTIFY_MARK_FLAGS)
1038 		return -EINVAL;
1039 
1040 	switch (mark_type) {
1041 	case FAN_MARK_INODE:
1042 		obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1043 		break;
1044 	case FAN_MARK_MOUNT:
1045 		obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1046 		break;
1047 	case FAN_MARK_FILESYSTEM:
1048 		obj_type = FSNOTIFY_OBJ_TYPE_SB;
1049 		break;
1050 	default:
1051 		return -EINVAL;
1052 	}
1053 
1054 	switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
1055 	case FAN_MARK_ADD:		/* fallthrough */
1056 	case FAN_MARK_REMOVE:
1057 		if (!mask)
1058 			return -EINVAL;
1059 		break;
1060 	case FAN_MARK_FLUSH:
1061 		if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1062 			return -EINVAL;
1063 		break;
1064 	default:
1065 		return -EINVAL;
1066 	}
1067 
1068 	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1069 		valid_mask |= FANOTIFY_PERM_EVENTS;
1070 
1071 	if (mask & ~valid_mask)
1072 		return -EINVAL;
1073 
1074 	f = fdget(fanotify_fd);
1075 	if (unlikely(!f.file))
1076 		return -EBADF;
1077 
1078 	/* verify that this is indeed an fanotify instance */
1079 	ret = -EINVAL;
1080 	if (unlikely(f.file->f_op != &fanotify_fops))
1081 		goto fput_and_out;
1082 	group = f.file->private_data;
1083 
1084 	/*
1085 	 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF.  These are not
1086 	 * allowed to set permissions events.
1087 	 */
1088 	ret = -EINVAL;
1089 	if (mask & FANOTIFY_PERM_EVENTS &&
1090 	    group->priority == FS_PRIO_0)
1091 		goto fput_and_out;
1092 
1093 	/*
1094 	 * Events with data type inode do not carry enough information to report
1095 	 * event->fd, so we do not allow setting a mask for inode events unless
1096 	 * group supports reporting fid.
1097 	 * inode events are not supported on a mount mark, because they do not
1098 	 * carry enough information (i.e. path) to be filtered by mount point.
1099 	 */
1100 	if (mask & FANOTIFY_INODE_EVENTS &&
1101 	    (!FAN_GROUP_FLAG(group, FAN_REPORT_FID) ||
1102 	     mark_type == FAN_MARK_MOUNT))
1103 		goto fput_and_out;
1104 
1105 	if (flags & FAN_MARK_FLUSH) {
1106 		ret = 0;
1107 		if (mark_type == FAN_MARK_MOUNT)
1108 			fsnotify_clear_vfsmount_marks_by_group(group);
1109 		else if (mark_type == FAN_MARK_FILESYSTEM)
1110 			fsnotify_clear_sb_marks_by_group(group);
1111 		else
1112 			fsnotify_clear_inode_marks_by_group(group);
1113 		goto fput_and_out;
1114 	}
1115 
1116 	ret = fanotify_find_path(dfd, pathname, &path, flags,
1117 			(mask & ALL_FSNOTIFY_EVENTS), obj_type);
1118 	if (ret)
1119 		goto fput_and_out;
1120 
1121 	if (flags & FAN_MARK_ADD) {
1122 		ret = fanotify_events_supported(&path, mask);
1123 		if (ret)
1124 			goto path_put_and_out;
1125 	}
1126 
1127 	if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
1128 		ret = fanotify_test_fid(&path, &__fsid);
1129 		if (ret)
1130 			goto path_put_and_out;
1131 
1132 		fsid = &__fsid;
1133 	}
1134 
1135 	/* inode held in place by reference to path; group by fget on fd */
1136 	if (mark_type == FAN_MARK_INODE)
1137 		inode = path.dentry->d_inode;
1138 	else
1139 		mnt = path.mnt;
1140 
1141 	/* create/update an inode mark */
1142 	switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
1143 	case FAN_MARK_ADD:
1144 		if (mark_type == FAN_MARK_MOUNT)
1145 			ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1146 							 flags, fsid);
1147 		else if (mark_type == FAN_MARK_FILESYSTEM)
1148 			ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1149 						   flags, fsid);
1150 		else
1151 			ret = fanotify_add_inode_mark(group, inode, mask,
1152 						      flags, fsid);
1153 		break;
1154 	case FAN_MARK_REMOVE:
1155 		if (mark_type == FAN_MARK_MOUNT)
1156 			ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1157 							    flags);
1158 		else if (mark_type == FAN_MARK_FILESYSTEM)
1159 			ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1160 						      flags);
1161 		else
1162 			ret = fanotify_remove_inode_mark(group, inode, mask,
1163 							 flags);
1164 		break;
1165 	default:
1166 		ret = -EINVAL;
1167 	}
1168 
1169 path_put_and_out:
1170 	path_put(&path);
1171 fput_and_out:
1172 	fdput(f);
1173 	return ret;
1174 }
1175 
1176 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1177 			      __u64, mask, int, dfd,
1178 			      const char  __user *, pathname)
1179 {
1180 	return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1181 }
1182 
1183 #ifdef CONFIG_COMPAT
1184 COMPAT_SYSCALL_DEFINE6(fanotify_mark,
1185 				int, fanotify_fd, unsigned int, flags,
1186 				__u32, mask0, __u32, mask1, int, dfd,
1187 				const char  __user *, pathname)
1188 {
1189 	return do_fanotify_mark(fanotify_fd, flags,
1190 #ifdef __BIG_ENDIAN
1191 				((__u64)mask0 << 32) | mask1,
1192 #else
1193 				((__u64)mask1 << 32) | mask0,
1194 #endif
1195 				 dfd, pathname);
1196 }
1197 #endif
1198 
1199 /*
1200  * fanotify_user_setup - Our initialization function.  Note that we cannot return
1201  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
1202  * must result in panic().
1203  */
1204 static int __init fanotify_user_setup(void)
1205 {
1206 	BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
1207 	BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1208 
1209 	fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1210 					 SLAB_PANIC|SLAB_ACCOUNT);
1211 	fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1212 					       SLAB_PANIC);
1213 	fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1214 						SLAB_PANIC);
1215 	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1216 		fanotify_perm_event_cachep =
1217 			KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1218 	}
1219 
1220 	return 0;
1221 }
1222 device_initcall(fanotify_user_setup);
1223