fanotify_user.c 24.3 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>
19
#include <linux/memcontrol.h>
E
Eric Paris 已提交
20 21

#include <asm/ioctls.h>
22

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

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

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

44
extern const struct fsnotify_ops fanotify_fsnotify_ops;
45

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

166
	return return_e;
167 168 169 170 171
}

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

	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 已提交
180
	 * userspace can send a valid response or we will clean it up after the
181 182
	 * timeout
	 */
183
	switch (response & ~FAN_AUDIT) {
184 185 186 187 188 189 190 191 192 193
	case FAN_ALLOW:
	case FAN_DENY:
		break;
	default:
		return -EINVAL;
	}

	if (fd < 0)
		return -EINVAL;

194
	if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
195 196
		return -EINVAL;

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

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

	return 0;
}

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

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

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

221
	fd = fanotify_event_metadata.fd;
222
	ret = -EFAULT;
223 224 225 226 227 228
	/*
	 * Sanity check copy size in case get_one_event() and
	 * fill_event_metadata() event_len sizes ever get out of sync.
	 */
	if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count))
		goto out_close_fd;
229 230
	if (copy_to_user(buf, &fanotify_event_metadata,
			 fanotify_event_metadata.event_len))
231 232
		goto out_close_fd;

233
	if (fanotify_is_perm_event(FANOTIFY_E(event)->mask))
234
		FANOTIFY_PE(event)->fd = fd;
E
Eric Paris 已提交
235

236 237
	if (fd != FAN_NOFD)
		fd_install(fd, f);
238
	return fanotify_event_metadata.event_len;
239 240

out_close_fd:
241 242 243 244
	if (fd != FAN_NOFD) {
		put_unused_fd(fd);
		fput(f);
	}
245
	return ret;
E
Eric Paris 已提交
246 247 248
}

/* intofiy userspace file descriptor functions */
A
Al Viro 已提交
249
static __poll_t fanotify_poll(struct file *file, poll_table *wait)
E
Eric Paris 已提交
250 251
{
	struct fsnotify_group *group = file->private_data;
A
Al Viro 已提交
252
	__poll_t ret = 0;
E
Eric Paris 已提交
253 254

	poll_wait(file, &group->notification_waitq, wait);
255
	spin_lock(&group->notification_lock);
E
Eric Paris 已提交
256
	if (!fsnotify_notify_queue_is_empty(group))
257
		ret = EPOLLIN | EPOLLRDNORM;
258
	spin_unlock(&group->notification_lock);
E
Eric Paris 已提交
259 260 261 262 263 264 265 266 267 268 269

	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;
270
	DEFINE_WAIT_FUNC(wait, woken_wake_function);
E
Eric Paris 已提交
271 272 273 274 275 276

	start = buf;
	group = file->private_data;

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

277
	add_wait_queue(&group->notification_waitq, &wait);
E
Eric Paris 已提交
278
	while (1) {
279
		spin_lock(&group->notification_lock);
E
Eric Paris 已提交
280
		kevent = get_one_event(group, count);
281
		spin_unlock(&group->notification_lock);
E
Eric Paris 已提交
282

283
		if (IS_ERR(kevent)) {
E
Eric Paris 已提交
284
			ret = PTR_ERR(kevent);
285 286 287 288 289 290
			break;
		}

		if (!kevent) {
			ret = -EAGAIN;
			if (file->f_flags & O_NONBLOCK)
E
Eric Paris 已提交
291
				break;
292 293 294 295 296 297

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

			if (start != buf)
E
Eric Paris 已提交
298
				break;
299 300

			wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
E
Eric Paris 已提交
301 302 303
			continue;
		}

304
		ret = copy_event_to_user(group, kevent, buf, count);
305 306 307 308 309 310 311 312 313 314
		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;
		}

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

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

344 345 346 347 348 349
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;

350 351 352
	if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
		return -EINVAL;

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	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;
}

370 371 372
static int fanotify_release(struct inode *ignored, struct file *file)
{
	struct fsnotify_group *group = file->private_data;
373
	struct fanotify_perm_event *event, *next;
374
	struct fsnotify_event *fsn_event;
375

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

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

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

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

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

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

	return 0;
}

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

	group = file->private_data;

	p = (void __user *) arg;

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

	return ret;
}

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

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

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

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

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

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

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

	return mask & oldmask;
}

526 527 528
static int fanotify_remove_mark(struct fsnotify_group *group,
				fsnotify_connp_t *connp, __u32 mask,
				unsigned int flags)
529 530
{
	struct fsnotify_mark *fsn_mark = NULL;
531
	__u32 removed;
532
	int destroy_mark;
533

534
	mutex_lock(&group->mark_mutex);
535
	fsn_mark = fsnotify_find_mark(connp, group);
536 537
	if (!fsn_mark) {
		mutex_unlock(&group->mark_mutex);
538
		return -ENOENT;
539
	}
540

541 542
	removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
						 &destroy_mark);
543 544
	if (removed & fsnotify_conn_mask(fsn_mark->connector))
		fsnotify_recalc_mask(fsn_mark->connector);
545
	if (destroy_mark)
546
		fsnotify_detach_mark(fsn_mark);
547
	mutex_unlock(&group->mark_mutex);
548 549
	if (destroy_mark)
		fsnotify_free_mark(fsn_mark);
550

551
	/* matches the fsnotify_find_mark() */
552 553 554
	fsnotify_put_mark(fsn_mark);
	return 0;
}
555

556 557 558 559 560 561 562 563
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
					 struct vfsmount *mnt, __u32 mask,
					 unsigned int flags)
{
	return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
				    mask, flags);
}

564 565 566 567 568 569 570
static int fanotify_remove_sb_mark(struct fsnotify_group *group,
				      struct super_block *sb, __u32 mask,
				      unsigned int flags)
{
	return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
}

571
static int fanotify_remove_inode_mark(struct fsnotify_group *group,
572 573
				      struct inode *inode, __u32 mask,
				      unsigned int flags)
574
{
575 576
	return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
				    flags);
577 578
}

579 580 581
static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
				       __u32 mask,
				       unsigned int flags)
582
{
583
	__u32 oldmask = -1;
584 585

	spin_lock(&fsn_mark->lock);
586 587
	if (!(flags & FAN_MARK_IGNORED_MASK)) {
		oldmask = fsn_mark->mask;
588
		fsn_mark->mask |= mask;
589
	} else {
590
		fsn_mark->ignored_mask |= mask;
591 592
		if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
			fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
593
	}
594 595 596 597 598
	spin_unlock(&fsn_mark->lock);

	return mask & ~oldmask;
}

599
static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
600 601
						   fsnotify_connp_t *connp,
						   unsigned int type)
602 603 604 605 606 607 608 609 610 611 612
{
	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);

613
	fsnotify_init_mark(mark, group);
614
	ret = fsnotify_add_mark_locked(mark, connp, type, 0);
615 616 617 618 619 620 621 622 623
	if (ret) {
		fsnotify_put_mark(mark);
		return ERR_PTR(ret);
	}

	return mark;
}


624 625 626
static int fanotify_add_mark(struct fsnotify_group *group,
			     fsnotify_connp_t *connp, unsigned int type,
			     __u32 mask, unsigned int flags)
627 628
{
	struct fsnotify_mark *fsn_mark;
629
	__u32 added;
630

631
	mutex_lock(&group->mark_mutex);
632
	fsn_mark = fsnotify_find_mark(connp, group);
633
	if (!fsn_mark) {
634
		fsn_mark = fanotify_add_new_mark(group, connp, type);
635
		if (IS_ERR(fsn_mark)) {
636
			mutex_unlock(&group->mark_mutex);
637
			return PTR_ERR(fsn_mark);
638
		}
639
	}
640
	added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
641 642
	if (added & ~fsnotify_conn_mask(fsn_mark->connector))
		fsnotify_recalc_mask(fsn_mark->connector);
643
	mutex_unlock(&group->mark_mutex);
644

645
	fsnotify_put_mark(fsn_mark);
646
	return 0;
647 648
}

649 650 651 652 653 654 655 656
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
				      struct vfsmount *mnt, __u32 mask,
				      unsigned int flags)
{
	return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
				 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags);
}

657 658 659 660 661 662 663 664
static int fanotify_add_sb_mark(struct fsnotify_group *group,
				      struct super_block *sb, __u32 mask,
				      unsigned int flags)
{
	return fanotify_add_mark(group, &sb->s_fsnotify_marks,
				 FSNOTIFY_OBJ_TYPE_SB, mask, flags);
}

665
static int fanotify_add_inode_mark(struct fsnotify_group *group,
666 667
				   struct inode *inode, __u32 mask,
				   unsigned int flags)
668 669
{
	pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
670

671 672 673 674 675 676 677
	/*
	 * 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) &&
678
	    inode_is_open_for_write(inode))
679 680
		return 0;

681 682
	return fanotify_add_mark(group, &inode->i_fsnotify_marks,
				 FSNOTIFY_OBJ_TYPE_INODE, mask, flags);
683
}
684

685
/* fanotify syscalls */
686
SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
687
{
688 689
	struct fsnotify_group *group;
	int f_flags, fd;
690
	struct user_struct *user;
691
	struct fanotify_event *oevent;
692

693 694
	pr_debug("%s: flags=%x event_f_flags=%x\n",
		 __func__, flags, event_f_flags);
695 696

	if (!capable(CAP_SYS_ADMIN))
697
		return -EPERM;
698

699
#ifdef CONFIG_AUDITSYSCALL
700
	if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
701
#else
702
	if (flags & ~FANOTIFY_INIT_FLAGS)
703
#endif
704 705
		return -EINVAL;

706 707 708 709 710 711 712 713 714 715 716 717
	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;
	}

718 719 720 721 722 723
	user = get_current_user();
	if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
		free_uid(user);
		return -EMFILE;
	}

724
	f_flags = O_RDWR | FMODE_NONOTIFY;
725 726 727 728 729 730 731
	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);
732 733
	if (IS_ERR(group)) {
		free_uid(user);
734
		return PTR_ERR(group);
735
	}
736

737
	group->fanotify_data.user = user;
738
	group->fanotify_data.flags = flags;
739
	atomic_inc(&user->fanotify_listeners);
740
	group->memcg = get_mem_cgroup_from_mm(current->mm);
741

742
	oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL);
743 744 745 746 747 748
	if (unlikely(!oevent)) {
		fd = -ENOMEM;
		goto out_destroy_group;
	}
	group->overflow_event = &oevent->fse;

749 750
	if (force_o_largefile())
		event_f_flags |= O_LARGEFILE;
751
	group->fanotify_data.f_flags = event_f_flags;
E
Eric Paris 已提交
752 753
	init_waitqueue_head(&group->fanotify_data.access_waitq);
	INIT_LIST_HEAD(&group->fanotify_data.access_list);
754
	switch (flags & FANOTIFY_CLASS_BITS) {
755 756 757 758 759 760 761 762 763 764 765
	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;
766
		goto out_destroy_group;
767
	}
E
Eric Paris 已提交
768

769 770 771
	if (flags & FAN_UNLIMITED_QUEUE) {
		fd = -EPERM;
		if (!capable(CAP_SYS_ADMIN))
772
			goto out_destroy_group;
773 774 775 776
		group->max_events = UINT_MAX;
	} else {
		group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
	}
777

778 779 780
	if (flags & FAN_UNLIMITED_MARKS) {
		fd = -EPERM;
		if (!capable(CAP_SYS_ADMIN))
781
			goto out_destroy_group;
782 783 784 785
		group->fanotify_data.max_marks = UINT_MAX;
	} else {
		group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
	}
786

787 788 789 790 791 792
	if (flags & FAN_ENABLE_AUDIT) {
		fd = -EPERM;
		if (!capable(CAP_AUDIT_WRITE))
			goto out_destroy_group;
	}

793 794
	fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
	if (fd < 0)
795
		goto out_destroy_group;
796 797 798

	return fd;

799 800
out_destroy_group:
	fsnotify_destroy_group(group);
801
	return fd;
802
}
803

804 805
static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
			    int dfd, const char  __user *pathname)
806
{
807 808
	struct inode *inode = NULL;
	struct vfsmount *mnt = NULL;
809
	struct fsnotify_group *group;
810
	struct fd f;
811
	struct path path;
812
	u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
813
	unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
814
	int ret;
815 816 817 818 819 820 821 822

	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;

823
	if (flags & ~FANOTIFY_MARK_FLAGS)
824
		return -EINVAL;
825 826 827 828 829 830 831 832 833 834

	switch (mark_type) {
	case FAN_MARK_INODE:
	case FAN_MARK_MOUNT:
	case FAN_MARK_FILESYSTEM:
		break;
	default:
		return -EINVAL;
	}

E
Eric Paris 已提交
835
	switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
836
	case FAN_MARK_ADD:		/* fallthrough */
837
	case FAN_MARK_REMOVE:
838 839
		if (!mask)
			return -EINVAL;
840
		break;
E
Eric Paris 已提交
841
	case FAN_MARK_FLUSH:
842
		if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
843
			return -EINVAL;
844 845 846 847
		break;
	default:
		return -EINVAL;
	}
848

849
	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
850
		valid_mask |= FANOTIFY_PERM_EVENTS;
851 852

	if (mask & ~valid_mask)
853 854
		return -EINVAL;

855 856
	f = fdget(fanotify_fd);
	if (unlikely(!f.file))
857 858 859 860
		return -EBADF;

	/* verify that this is indeed an fanotify instance */
	ret = -EINVAL;
861
	if (unlikely(f.file->f_op != &fanotify_fops))
862
		goto fput_and_out;
863
	group = f.file->private_data;
864 865 866 867 868 869

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

874 875
	if (flags & FAN_MARK_FLUSH) {
		ret = 0;
876
		if (mark_type == FAN_MARK_MOUNT)
877
			fsnotify_clear_vfsmount_marks_by_group(group);
878 879
		else if (mark_type == FAN_MARK_FILESYSTEM)
			fsnotify_clear_sb_marks_by_group(group);
880 881 882 883 884
		else
			fsnotify_clear_inode_marks_by_group(group);
		goto fput_and_out;
	}

885 886 887 888 889
	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 */
890
	if (mark_type == FAN_MARK_INODE)
891 892 893
		inode = path.dentry->d_inode;
	else
		mnt = path.mnt;
894 895

	/* create/update an inode mark */
896
	switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
897
	case FAN_MARK_ADD:
898
		if (mark_type == FAN_MARK_MOUNT)
899
			ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
900 901
		else if (mark_type == FAN_MARK_FILESYSTEM)
			ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, flags);
902
		else
903
			ret = fanotify_add_inode_mark(group, inode, mask, flags);
904 905
		break;
	case FAN_MARK_REMOVE:
906
		if (mark_type == FAN_MARK_MOUNT)
907
			ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
908 909
		else if (mark_type == FAN_MARK_FILESYSTEM)
			ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, flags);
910
		else
911
			ret = fanotify_remove_inode_mark(group, inode, mask, flags);
912 913 914 915
		break;
	default:
		ret = -EINVAL;
	}
916 917 918

	path_put(&path);
fput_and_out:
919
	fdput(f);
920 921 922
	return ret;
}

923 924 925 926 927 928 929
SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
			      __u64, mask, int, dfd,
			      const char  __user *, pathname)
{
	return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
}

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)
{
936
	return do_fanotify_mark(fanotify_fd, flags,
937 938
#ifdef __BIG_ENDIAN
				((__u64)mask0 << 32) | mask1,
H
Heiko Carstens 已提交
939 940
#else
				((__u64)mask1 << 32) | mask0,
941 942 943 944 945
#endif
				 dfd, pathname);
}
#endif

946
/*
947
 * fanotify_user_setup - Our initialization function.  Note that we cannot return
948 949 950 951 952
 * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
 * must result in panic().
 */
static int __init fanotify_user_setup(void)
{
953
	BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7);
954 955
	BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);

956 957
	fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
					 SLAB_PANIC|SLAB_ACCOUNT);
958
	fanotify_event_cachep = KMEM_CACHE(fanotify_event, SLAB_PANIC);
959 960
	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
		fanotify_perm_event_cachep =
961
			KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
962
	}
963 964

	return 0;
965
}
966
device_initcall(fanotify_user_setup);