fanotify_user.c 23.7 KB
Newer Older
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>
E
Eric Paris 已提交
9
#include <linux/mount.h>
10
#include <linux/namei.h>
E
Eric Paris 已提交
11
#include <linux/poll.h>
12 13
#include <linux/security.h>
#include <linux/syscalls.h>
T
Tejun Heo 已提交
14
#include <linux/slab.h>
15
#include <linux/types.h>
E
Eric Paris 已提交
16
#include <linux/uaccess.h>
17
#include <linux/compat.h>
18
#include <linux/sched/signal.h>
E
Eric Paris 已提交
19 20

#include <asm/ioctls.h>
21

22
#include "../../mount.h"
23
#include "../fdinfo.h"
24
#include "fanotify.h"
25

26
#define FANOTIFY_DEFAULT_MAX_EVENTS	16384
27
#define FANOTIFY_DEFAULT_MAX_MARKS	8192
28
#define FANOTIFY_DEFAULT_MAX_LISTENERS	128
29

30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * All flags that may be specified in parameter event_f_flags of fanotify_init.
 *
 * Internal and external open flags are stored together in field f_flags of
 * struct file. Only external open flags shall be allowed in event_f_flags.
 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
 * excluded.
 */
#define	FANOTIFY_INIT_ALL_EVENT_F_BITS				( \
		O_ACCMODE	| O_APPEND	| O_NONBLOCK	| \
		__O_SYNC	| O_DSYNC	| O_CLOEXEC     | \
		O_LARGEFILE	| O_NOATIME	)

43
extern const struct fsnotify_ops fanotify_fsnotify_ops;
44

45
struct kmem_cache *fanotify_mark_cache __read_mostly;
46
struct kmem_cache *fanotify_event_cachep __read_mostly;
47
struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
48

E
Eric Paris 已提交
49 50 51 52 53
/*
 * Get an fsnotify notification event if one exists and is small
 * enough to fit in "count". Return an error pointer if the count
 * is not large enough.
 *
54
 * Called with the group->notification_lock held.
E
Eric Paris 已提交
55 56 57 58
 */
static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
					    size_t count)
{
J
Jan Kara 已提交
59
	assert_spin_locked(&group->notification_lock);
E
Eric Paris 已提交
60 61 62 63 64 65 66 67 68

	pr_debug("%s: group=%p count=%zd\n", __func__, group, count);

	if (fsnotify_notify_queue_is_empty(group))
		return NULL;

	if (FAN_EVENT_METADATA_LEN > count)
		return ERR_PTR(-EINVAL);

69
	/* held the notification_lock the whole time, so this is the
E
Eric Paris 已提交
70
	 * same event we peeked above */
71
	return fsnotify_remove_first_event(group);
E
Eric Paris 已提交
72 73
}

74
static int create_fd(struct fsnotify_group *group,
75 76
		     struct fanotify_event_info *event,
		     struct file **file)
E
Eric Paris 已提交
77 78 79 80
{
	int client_fd;
	struct file *new_file;

81
	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
E
Eric Paris 已提交
82

83
	client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
E
Eric Paris 已提交
84 85 86 87 88 89 90 91 92
	if (client_fd < 0)
		return client_fd;

	/*
	 * we need a new file handle for the userspace program so it can read even if it was
	 * originally opened O_WRONLY.
	 */
	/* it's possible this event was an overflow event.  in that case dentry and mnt
	 * are NULL;  That's fine, just don't call dentry open */
93 94
	if (event->path.dentry && event->path.mnt)
		new_file = dentry_open(&event->path,
95
				       group->fanotify_data.f_flags | FMODE_NONOTIFY,
E
Eric Paris 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
				       current_cred());
	else
		new_file = ERR_PTR(-EOVERFLOW);
	if (IS_ERR(new_file)) {
		/*
		 * we still send an event even if we can't open the file.  this
		 * can happen when say tasks are gone and we try to open their
		 * /proc files or we try to open a WRONLY file like in sysfs
		 * we just send the errno to userspace since there isn't much
		 * else we can do.
		 */
		put_unused_fd(client_fd);
		client_fd = PTR_ERR(new_file);
	} else {
110
		*file = new_file;
E
Eric Paris 已提交
111 112
	}

113
	return client_fd;
E
Eric Paris 已提交
114 115
}

116
static int fill_event_metadata(struct fsnotify_group *group,
117 118 119
			       struct fanotify_event_metadata *metadata,
			       struct fsnotify_event *fsn_event,
			       struct file **file)
E
Eric Paris 已提交
120
{
121
	int ret = 0;
122
	struct fanotify_event_info *event;
123

E
Eric Paris 已提交
124
	pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
125
		 group, metadata, fsn_event);
E
Eric Paris 已提交
126

127
	*file = NULL;
128
	event = container_of(fsn_event, struct fanotify_event_info, fse);
E
Eric Paris 已提交
129
	metadata->event_len = FAN_EVENT_METADATA_LEN;
130
	metadata->metadata_len = FAN_EVENT_METADATA_LEN;
E
Eric Paris 已提交
131
	metadata->vers = FANOTIFY_METADATA_VERSION;
132
	metadata->reserved = 0;
133
	metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
134
	metadata->pid = pid_vnr(event->tgid);
135
	if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
136 137
		metadata->fd = FAN_NOFD;
	else {
138
		metadata->fd = create_fd(group, event, file);
139 140 141
		if (metadata->fd < 0)
			ret = metadata->fd;
	}
E
Eric Paris 已提交
142

143
	return ret;
E
Eric Paris 已提交
144 145
}

146 147
static struct fanotify_perm_event_info *dequeue_event(
				struct fsnotify_group *group, int fd)
148
{
149
	struct fanotify_perm_event_info *event, *return_e = NULL;
150

151
	spin_lock(&group->notification_lock);
152 153 154
	list_for_each_entry(event, &group->fanotify_data.access_list,
			    fae.fse.list) {
		if (event->fd != fd)
155 156
			continue;

157 158
		list_del_init(&event->fae.fse.list);
		return_e = event;
159 160
		break;
	}
161
	spin_unlock(&group->notification_lock);
162

163
	pr_debug("%s: found return_re=%p\n", __func__, return_e);
164

165
	return return_e;
166 167 168 169 170
}

static int process_access_response(struct fsnotify_group *group,
				   struct fanotify_response *response_struct)
{
171 172 173
	struct fanotify_perm_event_info *event;
	int fd = response_struct->fd;
	int response = response_struct->response;
174 175 176 177 178

	pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
		 fd, response);
	/*
	 * make sure the response is valid, if invalid we do nothing and either
L
Lucas De Marchi 已提交
179
	 * userspace can send a valid response or we will clean it up after the
180 181
	 * timeout
	 */
182
	switch (response & ~FAN_AUDIT) {
183 184 185 186 187 188 189 190 191 192
	case FAN_ALLOW:
	case FAN_DENY:
		break;
	default:
		return -EINVAL;
	}

	if (fd < 0)
		return -EINVAL;

193 194 195
	if ((response & FAN_AUDIT) && !group->fanotify_data.audit)
		return -EINVAL;

196 197
	event = dequeue_event(group, fd);
	if (!event)
198 199
		return -ENOENT;

200
	event->response = response;
201 202 203 204 205
	wake_up(&group->fanotify_data.access_waitq);

	return 0;
}

E
Eric Paris 已提交
206 207 208 209 210
static ssize_t copy_event_to_user(struct fsnotify_group *group,
				  struct fsnotify_event *event,
				  char __user *buf)
{
	struct fanotify_event_metadata fanotify_event_metadata;
211
	struct file *f;
212
	int fd, ret;
E
Eric Paris 已提交
213 214 215

	pr_debug("%s: group=%p event=%p\n", __func__, group, event);

216
	ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
217
	if (ret < 0)
218
		return ret;
219

220
	fd = fanotify_event_metadata.fd;
221
	ret = -EFAULT;
222 223
	if (copy_to_user(buf, &fanotify_event_metadata,
			 fanotify_event_metadata.event_len))
224 225
		goto out_close_fd;

226
	if (fanotify_is_perm_event(event->mask))
227
		FANOTIFY_PE(event)->fd = fd;
E
Eric Paris 已提交
228

229 230
	if (fd != FAN_NOFD)
		fd_install(fd, f);
231
	return fanotify_event_metadata.event_len;
232 233

out_close_fd:
234 235 236 237
	if (fd != FAN_NOFD) {
		put_unused_fd(fd);
		fput(f);
	}
238
	return ret;
E
Eric Paris 已提交
239 240 241
}

/* intofiy userspace file descriptor functions */
A
Al Viro 已提交
242
static __poll_t fanotify_poll(struct file *file, poll_table *wait)
E
Eric Paris 已提交
243 244
{
	struct fsnotify_group *group = file->private_data;
A
Al Viro 已提交
245
	__poll_t ret = 0;
E
Eric Paris 已提交
246 247

	poll_wait(file, &group->notification_waitq, wait);
248
	spin_lock(&group->notification_lock);
E
Eric Paris 已提交
249 250
	if (!fsnotify_notify_queue_is_empty(group))
		ret = POLLIN | POLLRDNORM;
251
	spin_unlock(&group->notification_lock);
E
Eric Paris 已提交
252 253 254 255 256 257 258 259 260 261 262

	return ret;
}

static ssize_t fanotify_read(struct file *file, char __user *buf,
			     size_t count, loff_t *pos)
{
	struct fsnotify_group *group;
	struct fsnotify_event *kevent;
	char __user *start;
	int ret;
263
	DEFINE_WAIT_FUNC(wait, woken_wake_function);
E
Eric Paris 已提交
264 265 266 267 268 269

	start = buf;
	group = file->private_data;

	pr_debug("%s: group=%p\n", __func__, group);

270
	add_wait_queue(&group->notification_waitq, &wait);
E
Eric Paris 已提交
271
	while (1) {
272
		spin_lock(&group->notification_lock);
E
Eric Paris 已提交
273
		kevent = get_one_event(group, count);
274
		spin_unlock(&group->notification_lock);
E
Eric Paris 已提交
275

276
		if (IS_ERR(kevent)) {
E
Eric Paris 已提交
277
			ret = PTR_ERR(kevent);
278 279 280 281 282 283
			break;
		}

		if (!kevent) {
			ret = -EAGAIN;
			if (file->f_flags & O_NONBLOCK)
E
Eric Paris 已提交
284
				break;
285 286 287 288 289 290

			ret = -ERESTARTSYS;
			if (signal_pending(current))
				break;

			if (start != buf)
E
Eric Paris 已提交
291
				break;
292 293

			wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
E
Eric Paris 已提交
294 295 296
			continue;
		}

297
		ret = copy_event_to_user(group, kevent, buf);
298 299 300 301 302 303 304 305 306 307
		if (unlikely(ret == -EOPENSTALE)) {
			/*
			 * We cannot report events with stale fd so drop it.
			 * Setting ret to 0 will continue the event loop and
			 * do the right thing if there are no more events to
			 * read (i.e. return bytes read, -EAGAIN or wait).
			 */
			ret = 0;
		}

308 309 310 311
		/*
		 * Permission events get queued to wait for response.  Other
		 * events can be destroyed now.
		 */
312
		if (!fanotify_is_perm_event(kevent->mask)) {
313
			fsnotify_destroy_event(group, kevent);
314
		} else {
315
			if (ret <= 0) {
316 317
				FANOTIFY_PE(kevent)->response = FAN_DENY;
				wake_up(&group->fanotify_data.access_waitq);
318 319 320 321 322
			} else {
				spin_lock(&group->notification_lock);
				list_add_tail(&kevent->list,
					&group->fanotify_data.access_list);
				spin_unlock(&group->notification_lock);
323 324
			}
		}
325 326
		if (ret < 0)
			break;
327 328
		buf += ret;
		count -= ret;
E
Eric Paris 已提交
329
	}
330
	remove_wait_queue(&group->notification_waitq, &wait);
E
Eric Paris 已提交
331 332 333 334 335 336

	if (start != buf && ret != -EFAULT)
		ret = buf - start;
	return ret;
}

337 338 339 340 341 342
static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
	struct fanotify_response response = { .fd = -1, .response = -1 };
	struct fsnotify_group *group;
	int ret;

343 344 345
	if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
		return -EINVAL;

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	group = file->private_data;

	if (count > sizeof(response))
		count = sizeof(response);

	pr_debug("%s: group=%p count=%zu\n", __func__, group, count);

	if (copy_from_user(&response, buf, count))
		return -EFAULT;

	ret = process_access_response(group, &response);
	if (ret < 0)
		count = ret;

	return count;
}

363 364 365
static int fanotify_release(struct inode *ignored, struct file *file)
{
	struct fsnotify_group *group = file->private_data;
366
	struct fanotify_perm_event_info *event, *next;
367
	struct fsnotify_event *fsn_event;
368

369
	/*
370 371 372
	 * Stop new events from arriving in the notification queue. since
	 * userspace cannot use fanotify fd anymore, no event can enter or
	 * leave access_list by now either.
373
	 */
374
	fsnotify_group_stop_queueing(group);
375

376 377 378 379
	/*
	 * Process all permission events on access_list and notification queue
	 * and simulate reply from userspace.
	 */
380
	spin_lock(&group->notification_lock);
381 382 383 384
	list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
				 fae.fse.list) {
		pr_debug("%s: found group=%p event=%p\n", __func__, group,
			 event);
385

386 387
		list_del_init(&event->fae.fse.list);
		event->response = FAN_ALLOW;
388 389
	}

390
	/*
391 392 393
	 * Destroy all non-permission events. For permission events just
	 * dequeue them and set the response. They will be freed once the
	 * response is consumed and fanotify_get_response() returns.
394
	 */
395 396
	while (!fsnotify_notify_queue_is_empty(group)) {
		fsn_event = fsnotify_remove_first_event(group);
397
		if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
398
			spin_unlock(&group->notification_lock);
399
			fsnotify_destroy_event(group, fsn_event);
400
			spin_lock(&group->notification_lock);
401
		} else {
402
			FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
403
		}
404
	}
405
	spin_unlock(&group->notification_lock);
406 407

	/* Response for all permission events it set, wakeup waiters */
408
	wake_up(&group->fanotify_data.access_waitq);
409

410
	/* matches the fanotify_init->fsnotify_alloc_group */
411
	fsnotify_destroy_group(group);
412 413 414 415

	return 0;
}

E
Eric Paris 已提交
416 417 418
static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct fsnotify_group *group;
419
	struct fsnotify_event *fsn_event;
E
Eric Paris 已提交
420 421 422 423 424 425 426 427 428 429
	void __user *p;
	int ret = -ENOTTY;
	size_t send_len = 0;

	group = file->private_data;

	p = (void __user *) arg;

	switch (cmd) {
	case FIONREAD:
430
		spin_lock(&group->notification_lock);
431
		list_for_each_entry(fsn_event, &group->notification_list, list)
E
Eric Paris 已提交
432
			send_len += FAN_EVENT_METADATA_LEN;
433
		spin_unlock(&group->notification_lock);
E
Eric Paris 已提交
434 435 436 437 438 439 440
		ret = put_user(send_len, (int __user *) p);
		break;
	}

	return ret;
}

441
static const struct file_operations fanotify_fops = {
442
	.show_fdinfo	= fanotify_show_fdinfo,
E
Eric Paris 已提交
443 444
	.poll		= fanotify_poll,
	.read		= fanotify_read,
445
	.write		= fanotify_write,
446 447
	.fasync		= NULL,
	.release	= fanotify_release,
E
Eric Paris 已提交
448 449
	.unlocked_ioctl	= fanotify_ioctl,
	.compat_ioctl	= fanotify_ioctl,
450
	.llseek		= noop_llseek,
451 452
};

453 454 455 456 457 458 459 460 461
static int fanotify_find_path(int dfd, const char __user *filename,
			      struct path *path, unsigned int flags)
{
	int ret;

	pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
		 dfd, filename, flags);

	if (filename == NULL) {
462
		struct fd f = fdget(dfd);
463 464

		ret = -EBADF;
465
		if (!f.file)
466 467 468 469
			goto out;

		ret = -ENOTDIR;
		if ((flags & FAN_MARK_ONLYDIR) &&
A
Al Viro 已提交
470
		    !(S_ISDIR(file_inode(f.file)->i_mode))) {
471
			fdput(f);
472 473 474
			goto out;
		}

475
		*path = f.file->f_path;
476
		path_get(path);
477
		fdput(f);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	} else {
		unsigned int lookup_flags = 0;

		if (!(flags & FAN_MARK_DONT_FOLLOW))
			lookup_flags |= LOOKUP_FOLLOW;
		if (flags & FAN_MARK_ONLYDIR)
			lookup_flags |= LOOKUP_DIRECTORY;

		ret = user_path_at(dfd, filename, lookup_flags, path);
		if (ret)
			goto out;
	}

	/* you can only watch an inode if you have read permissions on it */
	ret = inode_permission(path->dentry->d_inode, MAY_READ);
	if (ret)
		path_put(path);
out:
	return ret;
}

499 500
static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
					    __u32 mask,
501 502
					    unsigned int flags,
					    int *destroy)
503
{
504
	__u32 oldmask = 0;
505 506

	spin_lock(&fsn_mark->lock);
507
	if (!(flags & FAN_MARK_IGNORED_MASK)) {
508 509 510 511 512
		__u32 tmask = fsn_mark->mask & ~mask;

		if (flags & FAN_MARK_ONDIR)
			tmask &= ~FAN_ONDIR;

513
		oldmask = fsn_mark->mask;
514
		fsn_mark->mask = tmask;
515
	} else {
516
		__u32 tmask = fsn_mark->ignored_mask & ~mask;
517 518
		if (flags & FAN_MARK_ONDIR)
			tmask &= ~FAN_ONDIR;
519
		fsn_mark->ignored_mask = tmask;
520
	}
521
	*destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
522 523 524 525 526
	spin_unlock(&fsn_mark->lock);

	return mask & oldmask;
}

527
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
528 529
					 struct vfsmount *mnt, __u32 mask,
					 unsigned int flags)
530 531
{
	struct fsnotify_mark *fsn_mark = NULL;
532
	__u32 removed;
533
	int destroy_mark;
534

535
	mutex_lock(&group->mark_mutex);
536 537
	fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
				      group);
538 539
	if (!fsn_mark) {
		mutex_unlock(&group->mark_mutex);
540
		return -ENOENT;
541
	}
542

543 544
	removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
						 &destroy_mark);
545
	if (removed & real_mount(mnt)->mnt_fsnotify_mask)
546
		fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
547
	if (destroy_mark)
548
		fsnotify_detach_mark(fsn_mark);
549
	mutex_unlock(&group->mark_mutex);
550 551
	if (destroy_mark)
		fsnotify_free_mark(fsn_mark);
552

553 554 555
	fsnotify_put_mark(fsn_mark);
	return 0;
}
556

557
static int fanotify_remove_inode_mark(struct fsnotify_group *group,
558 559
				      struct inode *inode, __u32 mask,
				      unsigned int flags)
560 561 562
{
	struct fsnotify_mark *fsn_mark = NULL;
	__u32 removed;
563
	int destroy_mark;
564

565
	mutex_lock(&group->mark_mutex);
566
	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
567 568
	if (!fsn_mark) {
		mutex_unlock(&group->mark_mutex);
569
		return -ENOENT;
570
	}
571

572 573
	removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
						 &destroy_mark);
574
	if (removed & inode->i_fsnotify_mask)
575
		fsnotify_recalc_mask(inode->i_fsnotify_marks);
576
	if (destroy_mark)
577
		fsnotify_detach_mark(fsn_mark);
578
	mutex_unlock(&group->mark_mutex);
579 580
	if (destroy_mark)
		fsnotify_free_mark(fsn_mark);
581

582
	/* matches the fsnotify_find_mark() */
583
	fsnotify_put_mark(fsn_mark);
584

585 586 587
	return 0;
}

588 589 590
static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
				       __u32 mask,
				       unsigned int flags)
591
{
592
	__u32 oldmask = -1;
593 594

	spin_lock(&fsn_mark->lock);
595
	if (!(flags & FAN_MARK_IGNORED_MASK)) {
596 597 598 599 600
		__u32 tmask = fsn_mark->mask | mask;

		if (flags & FAN_MARK_ONDIR)
			tmask |= FAN_ONDIR;

601
		oldmask = fsn_mark->mask;
602
		fsn_mark->mask = tmask;
603
	} else {
604
		__u32 tmask = fsn_mark->ignored_mask | mask;
605 606 607
		if (flags & FAN_MARK_ONDIR)
			tmask |= FAN_ONDIR;

608
		fsn_mark->ignored_mask = tmask;
609 610
		if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
			fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
611
	}
612 613 614 615 616
	spin_unlock(&fsn_mark->lock);

	return mask & ~oldmask;
}

617 618 619 620 621 622 623 624 625 626 627 628 629 630
static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
						   struct inode *inode,
						   struct vfsmount *mnt)
{
	struct fsnotify_mark *mark;
	int ret;

	if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
		return ERR_PTR(-ENOSPC);

	mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
	if (!mark)
		return ERR_PTR(-ENOMEM);

631
	fsnotify_init_mark(mark, group);
632
	ret = fsnotify_add_mark_locked(mark, inode, mnt, 0);
633 634 635 636 637 638 639 640 641
	if (ret) {
		fsnotify_put_mark(mark);
		return ERR_PTR(ret);
	}

	return mark;
}


642
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
643 644
				      struct vfsmount *mnt, __u32 mask,
				      unsigned int flags)
645 646
{
	struct fsnotify_mark *fsn_mark;
647
	__u32 added;
648

649
	mutex_lock(&group->mark_mutex);
650 651
	fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
				      group);
652
	if (!fsn_mark) {
653 654
		fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
		if (IS_ERR(fsn_mark)) {
655
			mutex_unlock(&group->mark_mutex);
656
			return PTR_ERR(fsn_mark);
657
		}
658
	}
659
	added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
660
	if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
661
		fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
662
	mutex_unlock(&group->mark_mutex);
663

664
	fsnotify_put_mark(fsn_mark);
665
	return 0;
666 667
}

668
static int fanotify_add_inode_mark(struct fsnotify_group *group,
669 670
				   struct inode *inode, __u32 mask,
				   unsigned int flags)
671 672
{
	struct fsnotify_mark *fsn_mark;
673
	__u32 added;
674 675

	pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
676

677 678 679 680 681 682 683 684 685 686
	/*
	 * If some other task has this inode open for write we should not add
	 * an ignored mark, unless that ignored mark is supposed to survive
	 * modification changes anyway.
	 */
	if ((flags & FAN_MARK_IGNORED_MASK) &&
	    !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
	    (atomic_read(&inode->i_writecount) > 0))
		return 0;

687
	mutex_lock(&group->mark_mutex);
688
	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
689
	if (!fsn_mark) {
690 691
		fsn_mark = fanotify_add_new_mark(group, inode, NULL);
		if (IS_ERR(fsn_mark)) {
692
			mutex_unlock(&group->mark_mutex);
693
			return PTR_ERR(fsn_mark);
694
		}
695
	}
696
	added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
E
Eric Paris 已提交
697
	if (added & ~inode->i_fsnotify_mask)
698
		fsnotify_recalc_mask(inode->i_fsnotify_marks);
699
	mutex_unlock(&group->mark_mutex);
700

701
	fsnotify_put_mark(fsn_mark);
702
	return 0;
703
}
704

705
/* fanotify syscalls */
706
SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
707
{
708 709
	struct fsnotify_group *group;
	int f_flags, fd;
710
	struct user_struct *user;
711
	struct fanotify_event_info *oevent;
712

713 714
	pr_debug("%s: flags=%d event_f_flags=%d\n",
		__func__, flags, event_f_flags);
715 716

	if (!capable(CAP_SYS_ADMIN))
717
		return -EPERM;
718

719 720 721
#ifdef CONFIG_AUDITSYSCALL
	if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
#else
722
	if (flags & ~FAN_ALL_INIT_FLAGS)
723
#endif
724 725
		return -EINVAL;

726 727 728 729 730 731 732 733 734 735 736 737
	if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
		return -EINVAL;

	switch (event_f_flags & O_ACCMODE) {
	case O_RDONLY:
	case O_RDWR:
	case O_WRONLY:
		break;
	default:
		return -EINVAL;
	}

738 739 740 741 742 743
	user = get_current_user();
	if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
		free_uid(user);
		return -EMFILE;
	}

744
	f_flags = O_RDWR | FMODE_NONOTIFY;
745 746 747 748 749 750 751
	if (flags & FAN_CLOEXEC)
		f_flags |= O_CLOEXEC;
	if (flags & FAN_NONBLOCK)
		f_flags |= O_NONBLOCK;

	/* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
	group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
752 753
	if (IS_ERR(group)) {
		free_uid(user);
754
		return PTR_ERR(group);
755
	}
756

757 758 759
	group->fanotify_data.user = user;
	atomic_inc(&user->fanotify_listeners);

760
	oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
761 762 763 764 765 766
	if (unlikely(!oevent)) {
		fd = -ENOMEM;
		goto out_destroy_group;
	}
	group->overflow_event = &oevent->fse;

767 768
	if (force_o_largefile())
		event_f_flags |= O_LARGEFILE;
769
	group->fanotify_data.f_flags = event_f_flags;
E
Eric Paris 已提交
770 771
	init_waitqueue_head(&group->fanotify_data.access_waitq);
	INIT_LIST_HEAD(&group->fanotify_data.access_list);
772 773 774 775 776 777 778 779 780 781 782 783
	switch (flags & FAN_ALL_CLASS_BITS) {
	case FAN_CLASS_NOTIF:
		group->priority = FS_PRIO_0;
		break;
	case FAN_CLASS_CONTENT:
		group->priority = FS_PRIO_1;
		break;
	case FAN_CLASS_PRE_CONTENT:
		group->priority = FS_PRIO_2;
		break;
	default:
		fd = -EINVAL;
784
		goto out_destroy_group;
785
	}
E
Eric Paris 已提交
786

787 788 789
	if (flags & FAN_UNLIMITED_QUEUE) {
		fd = -EPERM;
		if (!capable(CAP_SYS_ADMIN))
790
			goto out_destroy_group;
791 792 793 794
		group->max_events = UINT_MAX;
	} else {
		group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
	}
795

796 797 798
	if (flags & FAN_UNLIMITED_MARKS) {
		fd = -EPERM;
		if (!capable(CAP_SYS_ADMIN))
799
			goto out_destroy_group;
800 801 802 803
		group->fanotify_data.max_marks = UINT_MAX;
	} else {
		group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
	}
804

805 806 807 808 809 810 811
	if (flags & FAN_ENABLE_AUDIT) {
		fd = -EPERM;
		if (!capable(CAP_AUDIT_WRITE))
			goto out_destroy_group;
		group->fanotify_data.audit = true;
	}

812 813
	fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
	if (fd < 0)
814
		goto out_destroy_group;
815 816 817

	return fd;

818 819
out_destroy_group:
	fsnotify_destroy_group(group);
820
	return fd;
821
}
822

823 824 825
SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
			      __u64, mask, int, dfd,
			      const char  __user *, pathname)
826
{
827 828
	struct inode *inode = NULL;
	struct vfsmount *mnt = NULL;
829
	struct fsnotify_group *group;
830
	struct fd f;
831
	struct path path;
832
	u32 valid_mask = FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD;
833
	int ret;
834 835 836 837 838 839 840 841

	pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
		 __func__, fanotify_fd, flags, dfd, pathname, mask);

	/* we only use the lower 32 bits as of right now. */
	if (mask & ((__u64)0xffffffff << 32))
		return -EINVAL;

842 843
	if (flags & ~FAN_ALL_MARK_FLAGS)
		return -EINVAL;
E
Eric Paris 已提交
844
	switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
845
	case FAN_MARK_ADD:		/* fallthrough */
846
	case FAN_MARK_REMOVE:
847 848
		if (!mask)
			return -EINVAL;
849
		break;
E
Eric Paris 已提交
850
	case FAN_MARK_FLUSH:
851 852
		if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
			return -EINVAL;
853 854 855 856
		break;
	default:
		return -EINVAL;
	}
857 858 859 860 861 862

	if (mask & FAN_ONDIR) {
		flags |= FAN_MARK_ONDIR;
		mask &= ~FAN_ONDIR;
	}

863 864 865 866
	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
		valid_mask |= FAN_ALL_PERM_EVENTS;

	if (mask & ~valid_mask)
867 868
		return -EINVAL;

869 870
	f = fdget(fanotify_fd);
	if (unlikely(!f.file))
871 872 873 874
		return -EBADF;

	/* verify that this is indeed an fanotify instance */
	ret = -EINVAL;
875
	if (unlikely(f.file->f_op != &fanotify_fops))
876
		goto fput_and_out;
877
	group = f.file->private_data;
878 879 880 881 882 883 884 885 886

	/*
	 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF.  These are not
	 * allowed to set permissions events.
	 */
	ret = -EINVAL;
	if (mask & FAN_ALL_PERM_EVENTS &&
	    group->priority == FS_PRIO_0)
		goto fput_and_out;
887

888 889 890 891 892 893 894 895 896
	if (flags & FAN_MARK_FLUSH) {
		ret = 0;
		if (flags & FAN_MARK_MOUNT)
			fsnotify_clear_vfsmount_marks_by_group(group);
		else
			fsnotify_clear_inode_marks_by_group(group);
		goto fput_and_out;
	}

897 898 899 900 901
	ret = fanotify_find_path(dfd, pathname, &path, flags);
	if (ret)
		goto fput_and_out;

	/* inode held in place by reference to path; group by fget on fd */
902
	if (!(flags & FAN_MARK_MOUNT))
903 904 905
		inode = path.dentry->d_inode;
	else
		mnt = path.mnt;
906 907

	/* create/update an inode mark */
908
	switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
909
	case FAN_MARK_ADD:
910
		if (flags & FAN_MARK_MOUNT)
911
			ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
912
		else
913
			ret = fanotify_add_inode_mark(group, inode, mask, flags);
914 915
		break;
	case FAN_MARK_REMOVE:
916
		if (flags & FAN_MARK_MOUNT)
917
			ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
918
		else
919
			ret = fanotify_remove_inode_mark(group, inode, mask, flags);
920 921 922 923
		break;
	default:
		ret = -EINVAL;
	}
924 925 926

	path_put(&path);
fput_and_out:
927
	fdput(f);
928 929 930
	return ret;
}

931 932 933 934 935 936 937 938 939
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE6(fanotify_mark,
				int, fanotify_fd, unsigned int, flags,
				__u32, mask0, __u32, mask1, int, dfd,
				const char  __user *, pathname)
{
	return sys_fanotify_mark(fanotify_fd, flags,
#ifdef __BIG_ENDIAN
				((__u64)mask0 << 32) | mask1,
H
Heiko Carstens 已提交
940 941
#else
				((__u64)mask1 << 32) | mask0,
942 943 944 945 946
#endif
				 dfd, pathname);
}
#endif

947
/*
948
 * fanotify_user_setup - Our initialization function.  Note that we cannot return
949 950 951 952 953 954
 * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
 * must result in panic().
 */
static int __init fanotify_user_setup(void)
{
	fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
955
	fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
956 957 958 959
	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
		fanotify_perm_event_cachep =
			KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
	}
960 961

	return 0;
962
}
963
device_initcall(fanotify_user_setup);