fanotify_user.c 23.6 KB
Newer Older
1
#include <linux/fanotify.h>
2
#include <linux/fcntl.h>
3
#include <linux/file.h>
4
#include <linux/fs.h>
5
#include <linux/anon_inodes.h>
6
#include <linux/fsnotify_backend.h>
7
#include <linux/init.h>
E
Eric Paris 已提交
8
#include <linux/mount.h>
9
#include <linux/namei.h>
E
Eric Paris 已提交
10
#include <linux/poll.h>
11 12
#include <linux/security.h>
#include <linux/syscalls.h>
T
Tejun Heo 已提交
13
#include <linux/slab.h>
14
#include <linux/types.h>
E
Eric Paris 已提交
15
#include <linux/uaccess.h>
16
#include <linux/compat.h>
17
#include <linux/sched/signal.h>
E
Eric Paris 已提交
18 19

#include <asm/ioctls.h>
20

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

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

29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * 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	)

42
extern const struct fsnotify_ops fanotify_fsnotify_ops;
43

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

E
Eric Paris 已提交
48 49 50 51 52
/*
 * 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.
 *
53
 * Called with the group->notification_lock held.
E
Eric Paris 已提交
54 55 56 57
 */
static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
					    size_t count)
{
J
Jan Kara 已提交
58
	assert_spin_locked(&group->notification_lock);
E
Eric Paris 已提交
59 60 61 62 63 64 65 66 67

	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);

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

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

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

82
	client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
E
Eric Paris 已提交
83 84 85 86 87 88 89 90 91
	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 */
92 93
	if (event->path.dentry && event->path.mnt)
		new_file = dentry_open(&event->path,
94
				       group->fanotify_data.f_flags | FMODE_NONOTIFY,
E
Eric Paris 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108
				       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 {
109
		*file = new_file;
E
Eric Paris 已提交
110 111
	}

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

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

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

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

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

145
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
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 182 183 184 185 186 187 188 189 190 191 192
	 * timeout
	 */
	switch (response) {
	case FAN_ALLOW:
	case FAN_DENY:
		break;
	default:
		return -EINVAL;
	}

	if (fd < 0)
		return -EINVAL;

193 194
	event = dequeue_event(group, fd);
	if (!event)
195 196
		return -ENOENT;

197
	event->response = response;
198 199 200 201 202 203
	wake_up(&group->fanotify_data.access_waitq);

	return 0;
}
#endif

E
Eric Paris 已提交
204 205 206 207 208
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;
209
	struct file *f;
210
	int fd, ret;
E
Eric Paris 已提交
211 212 213

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

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

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

224
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
225 226
	if (event->mask & FAN_ALL_PERM_EVENTS)
		FANOTIFY_PE(event)->fd = fd;
227
#endif
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 242 243 244 245 246 247
}

/* intofiy userspace file descriptor functions */
static unsigned int fanotify_poll(struct file *file, poll_table *wait)
{
	struct fsnotify_group *group = file->private_data;
	int ret = 0;

	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 (!(kevent->mask & FAN_ALL_PERM_EVENTS)) {
313
			fsnotify_destroy_event(group, kevent);
314 315
		} else {
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
316
			if (ret <= 0) {
317 318
				FANOTIFY_PE(kevent)->response = FAN_DENY;
				wake_up(&group->fanotify_data.access_waitq);
319 320 321 322 323
			} else {
				spin_lock(&group->notification_lock);
				list_add_tail(&kevent->list,
					&group->fanotify_data.access_list);
				spin_unlock(&group->notification_lock);
324 325 326
			}
#endif
		}
327 328
		if (ret < 0)
			break;
329 330
		buf += ret;
		count -= ret;
E
Eric Paris 已提交
331
	}
332
	remove_wait_queue(&group->notification_waitq, &wait);
E
Eric Paris 已提交
333 334 335 336 337 338

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

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
	struct fanotify_response response = { .fd = -1, .response = -1 };
	struct fsnotify_group *group;
	int ret;

	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;
#else
	return -EINVAL;
#endif
}

366 367 368 369
static int fanotify_release(struct inode *ignored, struct file *file)
{
	struct fsnotify_group *group = file->private_data;

370
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
371
	struct fanotify_perm_event_info *event, *next;
372
	struct fsnotify_event *fsn_event;
373

374
	/*
375 376 377
	 * 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.
378
	 */
379
	fsnotify_group_stop_queueing(group);
380

381 382 383 384
	/*
	 * Process all permission events on access_list and notification queue
	 * and simulate reply from userspace.
	 */
385
	spin_lock(&group->notification_lock);
386 387 388 389
	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);
390

391 392
		list_del_init(&event->fae.fse.list);
		event->response = FAN_ALLOW;
393 394
	}

395
	/*
396 397 398
	 * 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.
399
	 */
400 401
	while (!fsnotify_notify_queue_is_empty(group)) {
		fsn_event = fsnotify_remove_first_event(group);
402
		if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
403
			spin_unlock(&group->notification_lock);
404
			fsnotify_destroy_event(group, fsn_event);
405
			spin_lock(&group->notification_lock);
406
		} else
407 408
			FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
	}
409
	spin_unlock(&group->notification_lock);
410 411

	/* Response for all permission events it set, wakeup waiters */
412 413
	wake_up(&group->fanotify_data.access_waitq);
#endif
414

415
	/* matches the fanotify_init->fsnotify_alloc_group */
416
	fsnotify_destroy_group(group);
417 418 419 420

	return 0;
}

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

	group = file->private_data;

	p = (void __user *) arg;

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

	return ret;
}

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

458 459 460 461 462 463 464 465 466
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) {
467
		struct fd f = fdget(dfd);
468 469

		ret = -EBADF;
470
		if (!f.file)
471 472 473 474
			goto out;

		ret = -ENOTDIR;
		if ((flags & FAN_MARK_ONLYDIR) &&
A
Al Viro 已提交
475
		    !(S_ISDIR(file_inode(f.file)->i_mode))) {
476
			fdput(f);
477 478 479
			goto out;
		}

480
		*path = f.file->f_path;
481
		path_get(path);
482
		fdput(f);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	} 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;
}

504 505
static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
					    __u32 mask,
506 507
					    unsigned int flags,
					    int *destroy)
508
{
509
	__u32 oldmask = 0;
510 511

	spin_lock(&fsn_mark->lock);
512
	if (!(flags & FAN_MARK_IGNORED_MASK)) {
513 514 515 516 517
		__u32 tmask = fsn_mark->mask & ~mask;

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

518
		oldmask = fsn_mark->mask;
519
		fsn_mark->mask = tmask;
520
	} else {
521
		__u32 tmask = fsn_mark->ignored_mask & ~mask;
522 523
		if (flags & FAN_MARK_ONDIR)
			tmask &= ~FAN_ONDIR;
524
		fsn_mark->ignored_mask = tmask;
525
	}
526
	*destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
527 528 529 530 531
	spin_unlock(&fsn_mark->lock);

	return mask & oldmask;
}

532
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
533 534
					 struct vfsmount *mnt, __u32 mask,
					 unsigned int flags)
535 536
{
	struct fsnotify_mark *fsn_mark = NULL;
537
	__u32 removed;
538
	int destroy_mark;
539

540
	mutex_lock(&group->mark_mutex);
541 542
	fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
				      group);
543 544
	if (!fsn_mark) {
		mutex_unlock(&group->mark_mutex);
545
		return -ENOENT;
546
	}
547

548 549
	removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
						 &destroy_mark);
550
	if (removed & real_mount(mnt)->mnt_fsnotify_mask)
551
		fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
552
	if (destroy_mark)
553
		fsnotify_detach_mark(fsn_mark);
554
	mutex_unlock(&group->mark_mutex);
555 556
	if (destroy_mark)
		fsnotify_free_mark(fsn_mark);
557

558 559 560
	fsnotify_put_mark(fsn_mark);
	return 0;
}
561

562
static int fanotify_remove_inode_mark(struct fsnotify_group *group,
563 564
				      struct inode *inode, __u32 mask,
				      unsigned int flags)
565 566 567
{
	struct fsnotify_mark *fsn_mark = NULL;
	__u32 removed;
568
	int destroy_mark;
569

570
	mutex_lock(&group->mark_mutex);
571
	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
572 573
	if (!fsn_mark) {
		mutex_unlock(&group->mark_mutex);
574
		return -ENOENT;
575
	}
576

577 578
	removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
						 &destroy_mark);
579
	if (removed & inode->i_fsnotify_mask)
580
		fsnotify_recalc_mask(inode->i_fsnotify_marks);
581
	if (destroy_mark)
582
		fsnotify_detach_mark(fsn_mark);
583
	mutex_unlock(&group->mark_mutex);
584 585
	if (destroy_mark)
		fsnotify_free_mark(fsn_mark);
586

587
	/* matches the fsnotify_find_mark() */
588
	fsnotify_put_mark(fsn_mark);
589

590 591 592
	return 0;
}

593 594 595
static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
				       __u32 mask,
				       unsigned int flags)
596
{
597
	__u32 oldmask = -1;
598 599

	spin_lock(&fsn_mark->lock);
600
	if (!(flags & FAN_MARK_IGNORED_MASK)) {
601 602 603 604 605
		__u32 tmask = fsn_mark->mask | mask;

		if (flags & FAN_MARK_ONDIR)
			tmask |= FAN_ONDIR;

606
		oldmask = fsn_mark->mask;
607
		fsn_mark->mask = tmask;
608
	} else {
609
		__u32 tmask = fsn_mark->ignored_mask | mask;
610 611 612
		if (flags & FAN_MARK_ONDIR)
			tmask |= FAN_ONDIR;

613
		fsn_mark->ignored_mask = tmask;
614 615
		if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
			fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
616
	}
617 618 619 620 621
	spin_unlock(&fsn_mark->lock);

	return mask & ~oldmask;
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635
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);

636
	fsnotify_init_mark(mark, group);
637
	ret = fsnotify_add_mark_locked(mark, inode, mnt, 0);
638 639 640 641 642 643 644 645 646
	if (ret) {
		fsnotify_put_mark(mark);
		return ERR_PTR(ret);
	}

	return mark;
}


647
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
648 649
				      struct vfsmount *mnt, __u32 mask,
				      unsigned int flags)
650 651
{
	struct fsnotify_mark *fsn_mark;
652
	__u32 added;
653

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

669
	fsnotify_put_mark(fsn_mark);
670
	return 0;
671 672
}

673
static int fanotify_add_inode_mark(struct fsnotify_group *group,
674 675
				   struct inode *inode, __u32 mask,
				   unsigned int flags)
676 677
{
	struct fsnotify_mark *fsn_mark;
678
	__u32 added;
679 680

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

682 683 684 685 686 687 688 689 690 691
	/*
	 * 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;

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

706
	fsnotify_put_mark(fsn_mark);
707
	return 0;
708
}
709

710
/* fanotify syscalls */
711
SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
712
{
713 714
	struct fsnotify_group *group;
	int f_flags, fd;
715
	struct user_struct *user;
716
	struct fanotify_event_info *oevent;
717

718 719
	pr_debug("%s: flags=%d event_f_flags=%d\n",
		__func__, flags, event_f_flags);
720 721

	if (!capable(CAP_SYS_ADMIN))
722
		return -EPERM;
723 724 725 726

	if (flags & ~FAN_ALL_INIT_FLAGS)
		return -EINVAL;

727 728 729 730 731 732 733 734 735 736 737 738
	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;
	}

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

745
	f_flags = O_RDWR | FMODE_NONOTIFY;
746 747 748 749 750 751 752
	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);
753 754
	if (IS_ERR(group)) {
		free_uid(user);
755
		return PTR_ERR(group);
756
	}
757

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

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

768 769
	if (force_o_largefile())
		event_f_flags |= O_LARGEFILE;
770
	group->fanotify_data.f_flags = event_f_flags;
E
Eric Paris 已提交
771 772 773 774
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
	init_waitqueue_head(&group->fanotify_data.access_waitq);
	INIT_LIST_HEAD(&group->fanotify_data.access_list);
#endif
775 776 777 778 779 780 781 782 783 784 785 786
	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;
787
		goto out_destroy_group;
788
	}
E
Eric Paris 已提交
789

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

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

808 809
	fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
	if (fd < 0)
810
		goto out_destroy_group;
811 812 813

	return fd;

814 815
out_destroy_group:
	fsnotify_destroy_group(group);
816
	return fd;
817
}
818

819 820 821
SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
			      __u64, mask, int, dfd,
			      const char  __user *, pathname)
822
{
823 824
	struct inode *inode = NULL;
	struct vfsmount *mnt = NULL;
825
	struct fsnotify_group *group;
826
	struct fd f;
827
	struct path path;
828
	int ret;
829 830 831 832 833 834 835 836

	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;

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

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

858 859 860
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
	if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
#else
861
	if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
862
#endif
863 864
		return -EINVAL;

865 866
	f = fdget(fanotify_fd);
	if (unlikely(!f.file))
867 868 869 870
		return -EBADF;

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

	/*
	 * 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;
883

884 885 886 887 888 889 890 891 892
	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;
	}

893 894 895 896 897
	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 */
898
	if (!(flags & FAN_MARK_MOUNT))
899 900 901
		inode = path.dentry->d_inode;
	else
		mnt = path.mnt;
902 903

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

	path_put(&path);
fput_and_out:
923
	fdput(f);
924 925 926
	return ret;
}

927 928 929 930 931 932 933 934 935
#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 已提交
936 937
#else
				((__u64)mask1 << 32) | mask0,
938 939 940 941 942
#endif
				 dfd, pathname);
}
#endif

943
/*
944
 * fanotify_user_setup - Our initialization function.  Note that we cannot return
945 946 947 948 949 950
 * 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);
951
	fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
952 953 954 955
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
	fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
						SLAB_PANIC);
#endif
956 957

	return 0;
958
}
959
device_initcall(fanotify_user_setup);