File Index
Files and directories
Sign in
FILE

fanotify_user.c

File preview with syntax highlighting.

linux-6.19.14/fs/notify/fanotify/fanotify_user.c C
   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 "../fsnotify.h"
  26#include "../fdinfo.h"
  27#include "fanotify.h"
  28
  29#define FANOTIFY_DEFAULT_MAX_EVENTS	16384
  30#define FANOTIFY_OLD_DEFAULT_MAX_MARKS	8192
  31#define FANOTIFY_DEFAULT_MAX_GROUPS	128
  32#define FANOTIFY_DEFAULT_FEE_POOL_SIZE	32
  33
  34/*
  35 * Legacy fanotify marks limits (8192) is per group and we introduced a tunable
  36 * limit of marks per user, similar to inotify.  Effectively, the legacy limit
  37 * of fanotify marks per user is <max marks per group> * <max groups per user>.
  38 * This default limit (1M) also happens to match the increased limit of inotify
  39 * max_user_watches since v5.10.
  40 */
  41#define FANOTIFY_DEFAULT_MAX_USER_MARKS	\
  42	(FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS)
  43
  44/*
  45 * Most of the memory cost of adding an inode mark is pinning the marked inode.
  46 * The size of the filesystem inode struct is not uniform across filesystems,
  47 * so double the size of a VFS inode is used as a conservative approximation.
  48 */
  49#define INODE_MARK_COST	(2 * sizeof(struct inode))
  50
  51/* configurable via /proc/sys/fs/fanotify/ */
  52static int fanotify_max_queued_events __read_mostly;
  53static int perm_group_timeout __read_mostly;
  54
  55#ifdef CONFIG_SYSCTL
  56
  57#include <linux/sysctl.h>
  58
  59static long ft_zero = 0;
  60static long ft_int_max = INT_MAX;
  61
  62static const struct ctl_table fanotify_table[] = {
  63	{
  64		.procname	= "max_user_groups",
  65		.data	= &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS],
  66		.maxlen		= sizeof(long),
  67		.mode		= 0644,
  68		.proc_handler	= proc_doulongvec_minmax,
  69		.extra1		= &ft_zero,
  70		.extra2		= &ft_int_max,
  71	},
  72	{
  73		.procname	= "max_user_marks",
  74		.data	= &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS],
  75		.maxlen		= sizeof(long),
  76		.mode		= 0644,
  77		.proc_handler	= proc_doulongvec_minmax,
  78		.extra1		= &ft_zero,
  79		.extra2		= &ft_int_max,
  80	},
  81	{
  82		.procname	= "max_queued_events",
  83		.data		= &fanotify_max_queued_events,
  84		.maxlen		= sizeof(int),
  85		.mode		= 0644,
  86		.proc_handler	= proc_dointvec_minmax,
  87		.extra1		= SYSCTL_ZERO
  88	},
  89	{
  90		.procname	= "watchdog_timeout",
  91		.data		= &perm_group_timeout,
  92		.maxlen		= sizeof(int),
  93		.mode		= 0644,
  94		.proc_handler	= proc_dointvec_minmax,
  95		.extra1		= SYSCTL_ZERO,
  96	},
  97};
  98
  99static void __init fanotify_sysctls_init(void)
 100{
 101	register_sysctl("fs/fanotify", fanotify_table);
 102}
 103#else
 104#define fanotify_sysctls_init() do { } while (0)
 105#endif /* CONFIG_SYSCTL */
 106
 107static LIST_HEAD(perm_group_list);
 108static DEFINE_SPINLOCK(perm_group_lock);
 109static void perm_group_watchdog(struct work_struct *work);
 110static DECLARE_DELAYED_WORK(perm_group_work, perm_group_watchdog);
 111
 112static void perm_group_watchdog_schedule(void)
 113{
 114	schedule_delayed_work(&perm_group_work, secs_to_jiffies(perm_group_timeout));
 115}
 116
 117static void perm_group_watchdog(struct work_struct *work)
 118{
 119	struct fsnotify_group *group;
 120	struct fanotify_perm_event *event;
 121	struct task_struct *task;
 122	pid_t failed_pid = 0;
 123
 124	guard(spinlock)(&perm_group_lock);
 125	if (list_empty(&perm_group_list))
 126		return;
 127
 128	list_for_each_entry(group, &perm_group_list,
 129			    fanotify_data.perm_grp_list) {
 130		/*
 131		 * Ok to test without lock, racing with an addition is
 132		 * fine, will deal with it next round
 133		 */
 134		if (list_empty(&group->fanotify_data.access_list))
 135			continue;
 136
 137		spin_lock(&group->notification_lock);
 138		list_for_each_entry(event, &group->fanotify_data.access_list,
 139				    fae.fse.list) {
 140			if (likely(event->watchdog_cnt == 0)) {
 141				event->watchdog_cnt = 1;
 142			} else if (event->watchdog_cnt == 1) {
 143				/* Report on event only once */
 144				event->watchdog_cnt = 2;
 145
 146				/* Do not report same pid repeatedly */
 147				if (event->recv_pid == failed_pid)
 148					continue;
 149
 150				failed_pid = event->recv_pid;
 151				rcu_read_lock();
 152				task = find_task_by_pid_ns(event->recv_pid,
 153							   &init_pid_ns);
 154				pr_warn_ratelimited(
 155					"PID %u (%s) failed to respond to fanotify queue for more than %d seconds\n",
 156					event->recv_pid,
 157					task ? task->comm : NULL,
 158					perm_group_timeout);
 159				rcu_read_unlock();
 160			}
 161		}
 162		spin_unlock(&group->notification_lock);
 163	}
 164	perm_group_watchdog_schedule();
 165}
 166
 167static void fanotify_perm_watchdog_group_remove(struct fsnotify_group *group)
 168{
 169	if (!list_empty(&group->fanotify_data.perm_grp_list)) {
 170		/* Perm event watchdog can no longer scan this group. */
 171		spin_lock(&perm_group_lock);
 172		list_del_init(&group->fanotify_data.perm_grp_list);
 173		spin_unlock(&perm_group_lock);
 174	}
 175}
 176
 177static void fanotify_perm_watchdog_group_add(struct fsnotify_group *group)
 178{
 179	if (!perm_group_timeout)
 180		return;
 181
 182	spin_lock(&perm_group_lock);
 183	if (list_empty(&group->fanotify_data.perm_grp_list)) {
 184		/* Add to perm_group_list for monitoring by watchdog. */
 185		if (list_empty(&perm_group_list))
 186			perm_group_watchdog_schedule();
 187		list_add_tail(&group->fanotify_data.perm_grp_list, &perm_group_list);
 188	}
 189	spin_unlock(&perm_group_lock);
 190}
 191
 192/*
 193 * All flags that may be specified in parameter event_f_flags of fanotify_init.
 194 *
 195 * Internal and external open flags are stored together in field f_flags of
 196 * struct file. Only external open flags shall be allowed in event_f_flags.
 197 * Internal flags like FMODE_EXEC shall be excluded.
 198 */
 199#define	FANOTIFY_INIT_ALL_EVENT_F_BITS				( \
 200		O_ACCMODE	| O_APPEND	| O_NONBLOCK	| \
 201		__O_SYNC	| O_DSYNC	| O_CLOEXEC     | \
 202		O_LARGEFILE	| O_NOATIME	)
 203
 204extern const struct fsnotify_ops fanotify_fsnotify_ops;
 205
 206struct kmem_cache *fanotify_mark_cache __ro_after_init;
 207struct kmem_cache *fanotify_fid_event_cachep __ro_after_init;
 208struct kmem_cache *fanotify_path_event_cachep __ro_after_init;
 209struct kmem_cache *fanotify_perm_event_cachep __ro_after_init;
 210struct kmem_cache *fanotify_mnt_event_cachep __ro_after_init;
 211
 212#define FANOTIFY_EVENT_ALIGN 4
 213#define FANOTIFY_FID_INFO_HDR_LEN \
 214	(sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
 215#define FANOTIFY_PIDFD_INFO_LEN \
 216	sizeof(struct fanotify_event_info_pidfd)
 217#define FANOTIFY_ERROR_INFO_LEN \
 218	(sizeof(struct fanotify_event_info_error))
 219#define FANOTIFY_RANGE_INFO_LEN \
 220	(sizeof(struct fanotify_event_info_range))
 221#define FANOTIFY_MNT_INFO_LEN \
 222	(sizeof(struct fanotify_event_info_mnt))
 223
 224static int fanotify_fid_info_len(int fh_len, int name_len)
 225{
 226	int info_len = fh_len;
 227
 228	if (name_len)
 229		info_len += name_len + 1;
 230
 231	return roundup(FANOTIFY_FID_INFO_HDR_LEN + info_len,
 232		       FANOTIFY_EVENT_ALIGN);
 233}
 234
 235/* FAN_RENAME may have one or two dir+name info records */
 236static int fanotify_dir_name_info_len(struct fanotify_event *event)
 237{
 238	struct fanotify_info *info = fanotify_event_info(event);
 239	int dir_fh_len = fanotify_event_dir_fh_len(event);
 240	int dir2_fh_len = fanotify_event_dir2_fh_len(event);
 241	int info_len = 0;
 242
 243	if (dir_fh_len)
 244		info_len += fanotify_fid_info_len(dir_fh_len,
 245						  info->name_len);
 246	if (dir2_fh_len)
 247		info_len += fanotify_fid_info_len(dir2_fh_len,
 248						  info->name2_len);
 249
 250	return info_len;
 251}
 252
 253static size_t fanotify_event_len(unsigned int info_mode,
 254				 struct fanotify_event *event)
 255{
 256	size_t event_len = FAN_EVENT_METADATA_LEN;
 257	int fh_len;
 258	int dot_len = 0;
 259
 260	if (fanotify_is_error_event(event->mask))
 261		event_len += FANOTIFY_ERROR_INFO_LEN;
 262
 263	if (fanotify_event_has_any_dir_fh(event)) {
 264		event_len += fanotify_dir_name_info_len(event);
 265	} else if ((info_mode & FAN_REPORT_NAME) &&
 266		   (event->mask & FAN_ONDIR)) {
 267		/*
 268		 * With group flag FAN_REPORT_NAME, if name was not recorded in
 269		 * event on a directory, we will report the name ".".
 270		 */
 271		dot_len = 1;
 272	}
 273
 274	if (fanotify_event_has_object_fh(event)) {
 275		fh_len = fanotify_event_object_fh_len(event);
 276		event_len += fanotify_fid_info_len(fh_len, dot_len);
 277	}
 278	if (fanotify_is_mnt_event(event->mask))
 279		event_len += FANOTIFY_MNT_INFO_LEN;
 280
 281	if (info_mode & FAN_REPORT_PIDFD)
 282		event_len += FANOTIFY_PIDFD_INFO_LEN;
 283
 284	if (fanotify_event_has_access_range(event))
 285		event_len += FANOTIFY_RANGE_INFO_LEN;
 286
 287	return event_len;
 288}
 289
 290/*
 291 * Remove an hashed event from merge hash table.
 292 */
 293static void fanotify_unhash_event(struct fsnotify_group *group,
 294				  struct fanotify_event *event)
 295{
 296	assert_spin_locked(&group->notification_lock);
 297
 298	pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
 299		 group, event, fanotify_event_hash_bucket(group, event));
 300
 301	if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list)))
 302		return;
 303
 304	hlist_del_init(&event->merge_list);
 305}
 306
 307/*
 308 * Get an fanotify notification event if one exists and is small
 309 * enough to fit in "count". Return an error pointer if the count
 310 * is not large enough. When permission event is dequeued, its state is
 311 * updated accordingly.
 312 */
 313static struct fanotify_event *get_one_event(struct fsnotify_group *group,
 314					    size_t count)
 315{
 316	size_t event_size;
 317	struct fanotify_event *event = NULL;
 318	struct fsnotify_event *fsn_event;
 319	unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
 320
 321	pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
 322
 323	spin_lock(&group->notification_lock);
 324	fsn_event = fsnotify_peek_first_event(group);
 325	if (!fsn_event)
 326		goto out;
 327
 328	event = FANOTIFY_E(fsn_event);
 329	event_size = fanotify_event_len(info_mode, event);
 330
 331	if (event_size > count) {
 332		event = ERR_PTR(-EINVAL);
 333		goto out;
 334	}
 335
 336	/*
 337	 * Held the notification_lock the whole time, so this is the
 338	 * same event we peeked above.
 339	 */
 340	fsnotify_remove_first_event(group);
 341	if (fanotify_is_perm_event(event->mask))
 342		FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
 343	if (fanotify_is_hashed_event(event->mask))
 344		fanotify_unhash_event(group, event);
 345out:
 346	spin_unlock(&group->notification_lock);
 347	return event;
 348}
 349
 350static int create_fd(struct fsnotify_group *group, const struct path *path,
 351		     struct file **file)
 352{
 353	int client_fd;
 354	struct file *new_file;
 355
 356	client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
 357	if (client_fd < 0)
 358		return client_fd;
 359
 360	/*
 361	 * We provide an fd for the userspace program, so it could access the
 362	 * file without generating fanotify events itself.
 363	 */
 364	new_file = dentry_open_nonotify(path, group->fanotify_data.f_flags,
 365					current_cred());
 366	if (IS_ERR(new_file)) {
 367		put_unused_fd(client_fd);
 368		client_fd = PTR_ERR(new_file);
 369	} else {
 370		*file = new_file;
 371	}
 372
 373	return client_fd;
 374}
 375
 376static int process_access_response_info(const char __user *info,
 377					size_t info_len,
 378				struct fanotify_response_info_audit_rule *friar)
 379{
 380	if (info_len != sizeof(*friar))
 381		return -EINVAL;
 382
 383	if (copy_from_user(friar, info, sizeof(*friar)))
 384		return -EFAULT;
 385
 386	if (friar->hdr.type != FAN_RESPONSE_INFO_AUDIT_RULE)
 387		return -EINVAL;
 388	if (friar->hdr.pad != 0)
 389		return -EINVAL;
 390	if (friar->hdr.len != sizeof(*friar))
 391		return -EINVAL;
 392
 393	return info_len;
 394}
 395
 396/*
 397 * Finish processing of permission event by setting it to ANSWERED state and
 398 * drop group->notification_lock.
 399 */
 400static void finish_permission_event(struct fsnotify_group *group,
 401				    struct fanotify_perm_event *event, u32 response,
 402				    struct fanotify_response_info_audit_rule *friar)
 403				    __releases(&group->notification_lock)
 404{
 405	bool destroy = false;
 406
 407	assert_spin_locked(&group->notification_lock);
 408	event->response = response & ~FAN_INFO;
 409	if (response & FAN_INFO)
 410		memcpy(&event->audit_rule, friar, sizeof(*friar));
 411
 412	if (event->state == FAN_EVENT_CANCELED)
 413		destroy = true;
 414	else
 415		event->state = FAN_EVENT_ANSWERED;
 416	spin_unlock(&group->notification_lock);
 417	if (destroy)
 418		fsnotify_destroy_event(group, &event->fae.fse);
 419}
 420
 421static int process_access_response(struct fsnotify_group *group,
 422				   struct fanotify_response *response_struct,
 423				   const char __user *info,
 424				   size_t info_len)
 425{
 426	struct fanotify_perm_event *event;
 427	int fd = response_struct->fd;
 428	u32 response = response_struct->response;
 429	int errno = fanotify_get_response_errno(response);
 430	int ret = info_len;
 431	struct fanotify_response_info_audit_rule friar;
 432
 433	pr_debug("%s: group=%p fd=%d response=%x errno=%d buf=%p size=%zu\n",
 434		 __func__, group, fd, response, errno, info, info_len);
 435	/*
 436	 * make sure the response is valid, if invalid we do nothing and either
 437	 * userspace can send a valid response or we will clean it up after the
 438	 * timeout
 439	 */
 440	if (response & ~FANOTIFY_RESPONSE_VALID_MASK)
 441		return -EINVAL;
 442
 443	switch (response & FANOTIFY_RESPONSE_ACCESS) {
 444	case FAN_ALLOW:
 445		if (errno)
 446			return -EINVAL;
 447		break;
 448	case FAN_DENY:
 449		/* Custom errno is supported only for pre-content groups */
 450		if (errno && group->priority != FSNOTIFY_PRIO_PRE_CONTENT)
 451			return -EINVAL;
 452
 453		/*
 454		 * Limit errno to values expected on open(2)/read(2)/write(2)
 455		 * of regular files.
 456		 */
 457		switch (errno) {
 458		case 0:
 459		case EIO:
 460		case EPERM:
 461		case EBUSY:
 462		case ETXTBSY:
 463		case EAGAIN:
 464		case ENOSPC:
 465		case EDQUOT:
 466			break;
 467		default:
 468			return -EINVAL;
 469		}
 470		break;
 471	default:
 472		return -EINVAL;
 473	}
 474
 475	if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
 476		return -EINVAL;
 477
 478	if (response & FAN_INFO) {
 479		ret = process_access_response_info(info, info_len, &friar);
 480		if (ret < 0)
 481			return ret;
 482		if (fd == FAN_NOFD)
 483			return ret;
 484	} else {
 485		ret = 0;
 486	}
 487
 488	if (fd < 0)
 489		return -EINVAL;
 490
 491	spin_lock(&group->notification_lock);
 492	list_for_each_entry(event, &group->fanotify_data.access_list,
 493			    fae.fse.list) {
 494		if (event->fd != fd)
 495			continue;
 496
 497		list_del_init(&event->fae.fse.list);
 498		finish_permission_event(group, event, response, &friar);
 499		wake_up(&group->fanotify_data.access_waitq);
 500		return ret;
 501	}
 502	spin_unlock(&group->notification_lock);
 503
 504	return -ENOENT;
 505}
 506
 507static size_t copy_mnt_info_to_user(struct fanotify_event *event,
 508				    char __user *buf, int count)
 509{
 510	struct fanotify_event_info_mnt info = { };
 511
 512	info.hdr.info_type = FAN_EVENT_INFO_TYPE_MNT;
 513	info.hdr.len = FANOTIFY_MNT_INFO_LEN;
 514
 515	if (WARN_ON(count < info.hdr.len))
 516		return -EFAULT;
 517
 518	info.mnt_id = FANOTIFY_ME(event)->mnt_id;
 519
 520	if (copy_to_user(buf, &info, sizeof(info)))
 521		return -EFAULT;
 522
 523	return info.hdr.len;
 524}
 525
 526static size_t copy_error_info_to_user(struct fanotify_event *event,
 527				      char __user *buf, int count)
 528{
 529	struct fanotify_event_info_error info = { };
 530	struct fanotify_error_event *fee = FANOTIFY_EE(event);
 531
 532	info.hdr.info_type = FAN_EVENT_INFO_TYPE_ERROR;
 533	info.hdr.len = FANOTIFY_ERROR_INFO_LEN;
 534
 535	if (WARN_ON(count < info.hdr.len))
 536		return -EFAULT;
 537
 538	info.error = fee->error;
 539	info.error_count = fee->err_count;
 540
 541	if (copy_to_user(buf, &info, sizeof(info)))
 542		return -EFAULT;
 543
 544	return info.hdr.len;
 545}
 546
 547static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
 548				 int info_type, const char *name,
 549				 size_t name_len,
 550				 char __user *buf, size_t count)
 551{
 552	struct fanotify_event_info_fid info = { };
 553	struct file_handle handle = { };
 554	unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
 555	size_t fh_len = fh ? fh->len : 0;
 556	size_t info_len = fanotify_fid_info_len(fh_len, name_len);
 557	size_t len = info_len;
 558
 559	pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
 560		 __func__, fh_len, name_len, info_len, count);
 561
 562	if (WARN_ON_ONCE(len < sizeof(info) || len > count))
 563		return -EFAULT;
 564
 565	/*
 566	 * Copy event info fid header followed by variable sized file handle
 567	 * and optionally followed by variable sized filename.
 568	 */
 569	switch (info_type) {
 570	case FAN_EVENT_INFO_TYPE_FID:
 571	case FAN_EVENT_INFO_TYPE_DFID:
 572		if (WARN_ON_ONCE(name_len))
 573			return -EFAULT;
 574		break;
 575	case FAN_EVENT_INFO_TYPE_DFID_NAME:
 576	case FAN_EVENT_INFO_TYPE_OLD_DFID_NAME:
 577	case FAN_EVENT_INFO_TYPE_NEW_DFID_NAME:
 578		if (WARN_ON_ONCE(!name || !name_len))
 579			return -EFAULT;
 580		break;
 581	default:
 582		return -EFAULT;
 583	}
 584
 585	info.hdr.info_type = info_type;
 586	info.hdr.len = len;
 587	info.fsid = *fsid;
 588	if (copy_to_user(buf, &info, sizeof(info)))
 589		return -EFAULT;
 590
 591	buf += sizeof(info);
 592	len -= sizeof(info);
 593	if (WARN_ON_ONCE(len < sizeof(handle)))
 594		return -EFAULT;
 595
 596	handle.handle_type = fh->type;
 597	handle.handle_bytes = fh_len;
 598
 599	/* Mangle handle_type for bad file_handle */
 600	if (!fh_len)
 601		handle.handle_type = FILEID_INVALID;
 602
 603	if (copy_to_user(buf, &handle, sizeof(handle)))
 604		return -EFAULT;
 605
 606	buf += sizeof(handle);
 607	len -= sizeof(handle);
 608	if (WARN_ON_ONCE(len < fh_len))
 609		return -EFAULT;
 610
 611	/*
 612	 * For an inline fh and inline file name, copy through stack to exclude
 613	 * the copy from usercopy hardening protections.
 614	 */
 615	fh_buf = fanotify_fh_buf(fh);
 616	if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
 617		memcpy(bounce, fh_buf, fh_len);
 618		fh_buf = bounce;
 619	}
 620	if (copy_to_user(buf, fh_buf, fh_len))
 621		return -EFAULT;
 622
 623	buf += fh_len;
 624	len -= fh_len;
 625
 626	if (name_len) {
 627		/* Copy the filename with terminating null */
 628		name_len++;
 629		if (WARN_ON_ONCE(len < name_len))
 630			return -EFAULT;
 631
 632		if (copy_to_user(buf, name, name_len))
 633			return -EFAULT;
 634
 635		buf += name_len;
 636		len -= name_len;
 637	}
 638
 639	/* Pad with 0's */
 640	WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
 641	if (len > 0 && clear_user(buf, len))
 642		return -EFAULT;
 643
 644	return info_len;
 645}
 646
 647static int copy_pidfd_info_to_user(int pidfd,
 648				   char __user *buf,
 649				   size_t count)
 650{
 651	struct fanotify_event_info_pidfd info = { };
 652	size_t info_len = FANOTIFY_PIDFD_INFO_LEN;
 653
 654	if (WARN_ON_ONCE(info_len > count))
 655		return -EFAULT;
 656
 657	info.hdr.info_type = FAN_EVENT_INFO_TYPE_PIDFD;
 658	info.hdr.len = info_len;
 659	info.pidfd = pidfd;
 660
 661	if (copy_to_user(buf, &info, info_len))
 662		return -EFAULT;
 663
 664	return info_len;
 665}
 666
 667static size_t copy_range_info_to_user(struct fanotify_event *event,
 668				      char __user *buf, int count)
 669{
 670	struct fanotify_perm_event *pevent = FANOTIFY_PERM(event);
 671	struct fanotify_event_info_range info = { };
 672	size_t info_len = FANOTIFY_RANGE_INFO_LEN;
 673
 674	if (WARN_ON_ONCE(info_len > count))
 675		return -EFAULT;
 676
 677	if (WARN_ON_ONCE(!pevent->ppos))
 678		return -EINVAL;
 679
 680	info.hdr.info_type = FAN_EVENT_INFO_TYPE_RANGE;
 681	info.hdr.len = info_len;
 682	info.offset = *(pevent->ppos);
 683	info.count = pevent->count;
 684
 685	if (copy_to_user(buf, &info, info_len))
 686		return -EFAULT;
 687
 688	return info_len;
 689}
 690
 691static int copy_info_records_to_user(struct fanotify_event *event,
 692				     struct fanotify_info *info,
 693				     unsigned int info_mode, int pidfd,
 694				     char __user *buf, size_t count)
 695{
 696	int ret, total_bytes = 0, info_type = 0;
 697	unsigned int fid_mode = info_mode & FANOTIFY_FID_BITS;
 698	unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
 699
 700	/*
 701	 * Event info records order is as follows:
 702	 * 1. dir fid + name
 703	 * 2. (optional) new dir fid + new name
 704	 * 3. (optional) child fid
 705	 */
 706	if (fanotify_event_has_dir_fh(event)) {
 707		info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
 708					     FAN_EVENT_INFO_TYPE_DFID;
 709
 710		/* FAN_RENAME uses special info types */
 711		if (event->mask & FAN_RENAME)
 712			info_type = FAN_EVENT_INFO_TYPE_OLD_DFID_NAME;
 713
 714		ret = copy_fid_info_to_user(fanotify_event_fsid(event),
 715					    fanotify_info_dir_fh(info),
 716					    info_type,
 717					    fanotify_info_name(info),
 718					    info->name_len, buf, count);
 719		if (ret < 0)
 720			return ret;
 721
 722		buf += ret;
 723		count -= ret;
 724		total_bytes += ret;
 725	}
 726
 727	/* New dir fid+name may be reported in addition to old dir fid+name */
 728	if (fanotify_event_has_dir2_fh(event)) {
 729		info_type = FAN_EVENT_INFO_TYPE_NEW_DFID_NAME;
 730		ret = copy_fid_info_to_user(fanotify_event_fsid(event),
 731					    fanotify_info_dir2_fh(info),
 732					    info_type,
 733					    fanotify_info_name2(info),
 734					    info->name2_len, buf, count);
 735		if (ret < 0)
 736			return ret;
 737
 738		buf += ret;
 739		count -= ret;
 740		total_bytes += ret;
 741	}
 742
 743	if (fanotify_event_has_object_fh(event)) {
 744		const char *dot = NULL;
 745		int dot_len = 0;
 746
 747		if (fid_mode == FAN_REPORT_FID || info_type) {
 748			/*
 749			 * With only group flag FAN_REPORT_FID only type FID is
 750			 * reported. Second info record type is always FID.
 751			 */
 752			info_type = FAN_EVENT_INFO_TYPE_FID;
 753		} else if ((fid_mode & FAN_REPORT_NAME) &&
 754			   (event->mask & FAN_ONDIR)) {
 755			/*
 756			 * With group flag FAN_REPORT_NAME, if name was not
 757			 * recorded in an event on a directory, report the name
 758			 * "." with info type DFID_NAME.
 759			 */
 760			info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
 761			dot = ".";
 762			dot_len = 1;
 763		} else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
 764			   (event->mask & FAN_ONDIR)) {
 765			/*
 766			 * With group flag FAN_REPORT_DIR_FID, a single info
 767			 * record has type DFID for directory entry modification
 768			 * event and for event on a directory.
 769			 */
 770			info_type = FAN_EVENT_INFO_TYPE_DFID;
 771		} else {
 772			/*
 773			 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
 774			 * a single info record has type FID for event on a
 775			 * non-directory, when there is no directory to report.
 776			 * For example, on FAN_DELETE_SELF event.
 777			 */
 778			info_type = FAN_EVENT_INFO_TYPE_FID;
 779		}
 780
 781		ret = copy_fid_info_to_user(fanotify_event_fsid(event),
 782					    fanotify_event_object_fh(event),
 783					    info_type, dot, dot_len,
 784					    buf, count);
 785		if (ret < 0)
 786			return ret;
 787
 788		buf += ret;
 789		count -= ret;
 790		total_bytes += ret;
 791	}
 792
 793	if (pidfd_mode) {
 794		ret = copy_pidfd_info_to_user(pidfd, buf, count);
 795		if (ret < 0)
 796			return ret;
 797
 798		buf += ret;
 799		count -= ret;
 800		total_bytes += ret;
 801	}
 802
 803	if (fanotify_is_error_event(event->mask)) {
 804		ret = copy_error_info_to_user(event, buf, count);
 805		if (ret < 0)
 806			return ret;
 807		buf += ret;
 808		count -= ret;
 809		total_bytes += ret;
 810	}
 811
 812	if (fanotify_event_has_access_range(event)) {
 813		ret = copy_range_info_to_user(event, buf, count);
 814		if (ret < 0)
 815			return ret;
 816		buf += ret;
 817		count -= ret;
 818		total_bytes += ret;
 819	}
 820
 821	if (fanotify_is_mnt_event(event->mask)) {
 822		ret = copy_mnt_info_to_user(event, buf, count);
 823		if (ret < 0)
 824			return ret;
 825		buf += ret;
 826		count -= ret;
 827		total_bytes += ret;
 828	}
 829
 830	return total_bytes;
 831}
 832
 833static ssize_t copy_event_to_user(struct fsnotify_group *group,
 834				  struct fanotify_event *event,
 835				  char __user *buf, size_t count)
 836{
 837	struct fanotify_event_metadata metadata;
 838	const struct path *path = fanotify_event_path(event);
 839	struct fanotify_info *info = fanotify_event_info(event);
 840	unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
 841	unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
 842	struct file *f = NULL, *pidfd_file = NULL;
 843	int ret, pidfd = -ESRCH, fd = -EBADF;
 844
 845	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
 846
 847	metadata.event_len = fanotify_event_len(info_mode, event);
 848	metadata.metadata_len = FAN_EVENT_METADATA_LEN;
 849	metadata.vers = FANOTIFY_METADATA_VERSION;
 850	metadata.reserved = 0;
 851	metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
 852	metadata.pid = pid_vnr(event->pid);
 853	/*
 854	 * For an unprivileged listener, event->pid can be used to identify the
 855	 * events generated by the listener process itself, without disclosing
 856	 * the pids of other processes.
 857	 */
 858	if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
 859	    task_tgid(current) != event->pid)
 860		metadata.pid = 0;
 861
 862	/*
 863	 * For now, fid mode is required for an unprivileged listener and
 864	 * fid mode does not report fd in events.  Keep this check anyway
 865	 * for safety in case fid mode requirement is relaxed in the future
 866	 * to allow unprivileged listener to get events with no fd and no fid.
 867	 */
 868	if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
 869	    path && path->mnt && path->dentry) {
 870		fd = create_fd(group, path, &f);
 871		/*
 872		 * Opening an fd from dentry can fail for several reasons.
 873		 * For example, when tasks are gone and we try to open their
 874		 * /proc files or we try to open a WRONLY file like in sysfs
 875		 * or when trying to open a file that was deleted on the
 876		 * remote network server.
 877		 *
 878		 * For a group with FAN_REPORT_FD_ERROR, we will send the
 879		 * event with the error instead of the open fd, otherwise
 880		 * Userspace may not get the error at all.
 881		 * In any case, userspace will not know which file failed to
 882		 * open, so add a debug print for further investigation.
 883		 */
 884		if (fd < 0) {
 885			pr_debug("fanotify: create_fd(%pd2) failed err=%d\n",
 886				 path->dentry, fd);
 887			if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR)) {
 888				/*
 889				 * Historically, we've handled EOPENSTALE in a
 890				 * special way and silently dropped such
 891				 * events. Now we have to keep it to maintain
 892				 * backward compatibility...
 893				 */
 894				if (fd == -EOPENSTALE)
 895					fd = 0;
 896				return fd;
 897			}
 898		}
 899	}
 900	if (FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR))
 901		metadata.fd = fd;
 902	else
 903		metadata.fd = fd >= 0 ? fd : FAN_NOFD;
 904
 905	if (pidfd_mode) {
 906		/*
 907		 * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual
 908		 * exclusion is ever lifted. At the time of incoporating pidfd
 909		 * support within fanotify, the pidfd API only supported the
 910		 * creation of pidfds for thread-group leaders.
 911		 */
 912		WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID));
 913
 914		/*
 915		 * The PIDTYPE_TGID check for an event->pid is performed
 916		 * preemptively in an attempt to catch out cases where the event
 917		 * listener reads events after the event generating process has
 918		 * already terminated.  Depending on flag FAN_REPORT_FD_ERROR,
 919		 * report either -ESRCH or FAN_NOPIDFD to the event listener in
 920		 * those cases with all other pidfd creation errors reported as
 921		 * the error code itself or as FAN_EPIDFD.
 922		 */
 923		if (metadata.pid && pid_has_task(event->pid, PIDTYPE_TGID))
 924			pidfd = pidfd_prepare(event->pid, 0, &pidfd_file);
 925
 926		if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0)
 927			pidfd = pidfd == -ESRCH ? FAN_NOPIDFD : FAN_EPIDFD;
 928	}
 929
 930	ret = -EFAULT;
 931	/*
 932	 * Sanity check copy size in case get_one_event() and
 933	 * event_len sizes ever get out of sync.
 934	 */
 935	if (WARN_ON_ONCE(metadata.event_len > count))
 936		goto out_close_fd;
 937
 938	if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
 939		goto out_close_fd;
 940
 941	buf += FAN_EVENT_METADATA_LEN;
 942	count -= FAN_EVENT_METADATA_LEN;
 943
 944	ret = copy_info_records_to_user(event, info, info_mode, pidfd,
 945					buf, count);
 946	if (ret < 0)
 947		goto out_close_fd;
 948
 949	if (f)
 950		fd_install(fd, f);
 951
 952	if (pidfd_file)
 953		fd_install(pidfd, pidfd_file);
 954
 955	if (fanotify_is_perm_event(event->mask))
 956		FANOTIFY_PERM(event)->fd = fd;
 957
 958	return metadata.event_len;
 959
 960out_close_fd:
 961	if (f) {
 962		put_unused_fd(fd);
 963		fput(f);
 964	}
 965
 966	if (pidfd_file) {
 967		put_unused_fd(pidfd);
 968		fput(pidfd_file);
 969	}
 970
 971	return ret;
 972}
 973
 974/* intofiy userspace file descriptor functions */
 975static __poll_t fanotify_poll(struct file *file, poll_table *wait)
 976{
 977	struct fsnotify_group *group = file->private_data;
 978	__poll_t ret = 0;
 979
 980	poll_wait(file, &group->notification_waitq, wait);
 981	spin_lock(&group->notification_lock);
 982	if (!fsnotify_notify_queue_is_empty(group))
 983		ret = EPOLLIN | EPOLLRDNORM;
 984	spin_unlock(&group->notification_lock);
 985
 986	return ret;
 987}
 988
 989static ssize_t fanotify_read(struct file *file, char __user *buf,
 990			     size_t count, loff_t *pos)
 991{
 992	struct fsnotify_group *group;
 993	struct fanotify_event *event;
 994	char __user *start;
 995	int ret;
 996	DEFINE_WAIT_FUNC(wait, woken_wake_function);
 997
 998	start = buf;
 999	group = file->private_data;
1000
1001	pr_debug("%s: group=%p\n", __func__, group);
1002
1003	add_wait_queue(&group->notification_waitq, &wait);
1004	while (1) {
1005		/*
1006		 * User can supply arbitrarily large buffer. Avoid softlockups
1007		 * in case there are lots of available events.
1008		 */
1009		cond_resched();
1010		event = get_one_event(group, count);
1011		if (IS_ERR(event)) {
1012			ret = PTR_ERR(event);
1013			break;
1014		}
1015
1016		if (!event) {
1017			ret = -EAGAIN;
1018			if (file->f_flags & O_NONBLOCK)
1019				break;
1020
1021			ret = -ERESTARTSYS;
1022			if (signal_pending(current))
1023				break;
1024
1025			if (start != buf)
1026				break;
1027
1028			wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
1029			continue;
1030		}
1031
1032		ret = copy_event_to_user(group, event, buf, count);
1033
1034		/*
1035		 * Permission events get queued to wait for response.  Other
1036		 * events can be destroyed now.
1037		 */
1038		if (!fanotify_is_perm_event(event->mask)) {
1039			fsnotify_destroy_event(group, &event->fse);
1040		} else {
1041			if (ret <= 0 || FANOTIFY_PERM(event)->fd < 0) {
1042				spin_lock(&group->notification_lock);
1043				finish_permission_event(group,
1044					FANOTIFY_PERM(event), FAN_DENY, NULL);
1045				wake_up(&group->fanotify_data.access_waitq);
1046			} else {
1047				spin_lock(&group->notification_lock);
1048				list_add_tail(&event->fse.list,
1049					&group->fanotify_data.access_list);
1050				FANOTIFY_PERM(event)->recv_pid = current->pid;
1051				spin_unlock(&group->notification_lock);
1052			}
1053		}
1054		if (ret < 0)
1055			break;
1056		buf += ret;
1057		count -= ret;
1058	}
1059	remove_wait_queue(&group->notification_waitq, &wait);
1060
1061	if (start != buf && ret != -EFAULT)
1062		ret = buf - start;
1063	return ret;
1064}
1065
1066static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
1067{
1068	struct fanotify_response response;
1069	struct fsnotify_group *group;
1070	int ret;
1071	const char __user *info_buf = buf + sizeof(struct fanotify_response);
1072	size_t info_len;
1073
1074	if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1075		return -EINVAL;
1076
1077	group = file->private_data;
1078
1079	pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
1080
1081	if (count < sizeof(response))
1082		return -EINVAL;
1083
1084	if (copy_from_user(&response, buf, sizeof(response)))
1085		return -EFAULT;
1086
1087	info_len = count - sizeof(response);
1088
1089	ret = process_access_response(group, &response, info_buf, info_len);
1090	if (ret < 0)
1091		count = ret;
1092	else
1093		count = sizeof(response) + ret;
1094
1095	return count;
1096}
1097
1098static int fanotify_release(struct inode *ignored, struct file *file)
1099{
1100	struct fsnotify_group *group = file->private_data;
1101	struct fsnotify_event *fsn_event;
1102
1103	/*
1104	 * Stop new events from arriving in the notification queue. since
1105	 * userspace cannot use fanotify fd anymore, no event can enter or
1106	 * leave access_list by now either.
1107	 */
1108	fsnotify_group_stop_queueing(group);
1109
1110	fanotify_perm_watchdog_group_remove(group);
1111
1112	/*
1113	 * Process all permission events on access_list and notification queue
1114	 * and simulate reply from userspace.
1115	 */
1116	spin_lock(&group->notification_lock);
1117	while (!list_empty(&group->fanotify_data.access_list)) {
1118		struct fanotify_perm_event *event;
1119
1120		event = list_first_entry(&group->fanotify_data.access_list,
1121				struct fanotify_perm_event, fae.fse.list);
1122		list_del_init(&event->fae.fse.list);
1123		finish_permission_event(group, event, FAN_ALLOW, NULL);
1124		spin_lock(&group->notification_lock);
1125	}
1126
1127	/*
1128	 * Destroy all non-permission events. For permission events just
1129	 * dequeue them and set the response. They will be freed once the
1130	 * response is consumed and fanotify_get_response() returns.
1131	 */
1132	while ((fsn_event = fsnotify_remove_first_event(group))) {
1133		struct fanotify_event *event = FANOTIFY_E(fsn_event);
1134
1135		if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
1136			spin_unlock(&group->notification_lock);
1137			fsnotify_destroy_event(group, fsn_event);
1138		} else {
1139			finish_permission_event(group, FANOTIFY_PERM(event),
1140						FAN_ALLOW, NULL);
1141		}
1142		spin_lock(&group->notification_lock);
1143	}
1144	spin_unlock(&group->notification_lock);
1145
1146	/* Response for all permission events it set, wakeup waiters */
1147	wake_up(&group->fanotify_data.access_waitq);
1148
1149	/* matches the fanotify_init->fsnotify_alloc_group */
1150	fsnotify_destroy_group(group);
1151
1152	return 0;
1153}
1154
1155static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1156{
1157	struct fsnotify_group *group;
1158	struct fsnotify_event *fsn_event;
1159	void __user *p;
1160	int ret = -ENOTTY;
1161	size_t send_len = 0;
1162
1163	group = file->private_data;
1164
1165	p = (void __user *) arg;
1166
1167	switch (cmd) {
1168	case FIONREAD:
1169		spin_lock(&group->notification_lock);
1170		list_for_each_entry(fsn_event, &group->notification_list, list)
1171			send_len += FAN_EVENT_METADATA_LEN;
1172		spin_unlock(&group->notification_lock);
1173		ret = put_user(send_len, (int __user *) p);
1174		break;
1175	}
1176
1177	return ret;
1178}
1179
1180static const struct file_operations fanotify_fops = {
1181	.show_fdinfo	= fanotify_show_fdinfo,
1182	.poll		= fanotify_poll,
1183	.read		= fanotify_read,
1184	.write		= fanotify_write,
1185	.fasync		= NULL,
1186	.release	= fanotify_release,
1187	.unlocked_ioctl	= fanotify_ioctl,
1188	.compat_ioctl	= compat_ptr_ioctl,
1189	.llseek		= noop_llseek,
1190};
1191
1192static int fanotify_find_path(int dfd, const char __user *filename,
1193			      struct path *path, unsigned int flags, __u64 mask,
1194			      unsigned int obj_type)
1195{
1196	int ret;
1197
1198	pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
1199		 dfd, filename, flags);
1200
1201	if (filename == NULL) {
1202		CLASS(fd, f)(dfd);
1203
1204		if (fd_empty(f))
1205			return -EBADF;
1206
1207		if ((flags & FAN_MARK_ONLYDIR) &&
1208		    !(S_ISDIR(file_inode(fd_file(f))->i_mode)))
1209			return -ENOTDIR;
1210
1211		*path = fd_file(f)->f_path;
1212		path_get(path);
1213	} else {
1214		unsigned int lookup_flags = 0;
1215
1216		if (!(flags & FAN_MARK_DONT_FOLLOW))
1217			lookup_flags |= LOOKUP_FOLLOW;
1218		if (flags & FAN_MARK_ONLYDIR)
1219			lookup_flags |= LOOKUP_DIRECTORY;
1220
1221		ret = user_path_at(dfd, filename, lookup_flags, path);
1222		if (ret)
1223			goto out;
1224	}
1225
1226	/* you can only watch an inode if you have read permissions on it */
1227	ret = path_permission(path, MAY_READ);
1228	if (ret) {
1229		path_put(path);
1230		goto out;
1231	}
1232
1233	ret = security_path_notify(path, mask, obj_type);
1234	if (ret)
1235		path_put(path);
1236
1237out:
1238	return ret;
1239}
1240
1241static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
1242					    __u32 mask, unsigned int flags,
1243					    __u32 umask, int *destroy)
1244{
1245	__u32 oldmask, newmask;
1246
1247	/* umask bits cannot be removed by user */
1248	mask &= ~umask;
1249	spin_lock(&fsn_mark->lock);
1250	oldmask = fsnotify_calc_mask(fsn_mark);
1251	if (!(flags & FANOTIFY_MARK_IGNORE_BITS)) {
1252		fsn_mark->mask &= ~mask;
1253	} else {
1254		fsn_mark->ignore_mask &= ~mask;
1255	}
1256	newmask = fsnotify_calc_mask(fsn_mark);
1257	/*
1258	 * We need to keep the mark around even if remaining mask cannot
1259	 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
1260	 * changes to the mask.
1261	 * Destroy mark when only umask bits remain.
1262	 */
1263	*destroy = !((fsn_mark->mask | fsn_mark->ignore_mask) & ~umask);
1264	spin_unlock(&fsn_mark->lock);
1265
1266	return oldmask & ~newmask;
1267}
1268
1269static int fanotify_remove_mark(struct fsnotify_group *group,
1270				void *obj, unsigned int obj_type, __u32 mask,
1271				unsigned int flags, __u32 umask)
1272{
1273	struct fsnotify_mark *fsn_mark = NULL;
1274	__u32 removed;
1275	int destroy_mark;
1276
1277	fsnotify_group_lock(group);
1278	fsn_mark = fsnotify_find_mark(obj, obj_type, group);
1279	if (!fsn_mark) {
1280		fsnotify_group_unlock(group);
1281		return -ENOENT;
1282	}
1283
1284	removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
1285						 umask, &destroy_mark);
1286	if (removed & fsnotify_conn_mask(fsn_mark->connector))
1287		fsnotify_recalc_mask(fsn_mark->connector);
1288	if (destroy_mark)
1289		fsnotify_detach_mark(fsn_mark);
1290	fsnotify_group_unlock(group);
1291	if (destroy_mark)
1292		fsnotify_free_mark(fsn_mark);
1293
1294	/* matches the fsnotify_find_mark() */
1295	fsnotify_put_mark(fsn_mark);
1296	return 0;
1297}
1298
1299static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
1300				       unsigned int fan_flags)
1301{
1302	bool want_iref = !(fan_flags & FAN_MARK_EVICTABLE);
1303	unsigned int ignore = fan_flags & FANOTIFY_MARK_IGNORE_BITS;
1304	bool recalc = false;
1305
1306	/*
1307	 * When using FAN_MARK_IGNORE for the first time, mark starts using
1308	 * independent event flags in ignore mask.  After that, trying to
1309	 * update the ignore mask with the old FAN_MARK_IGNORED_MASK API
1310	 * will result in EEXIST error.
1311	 */
1312	if (ignore == FAN_MARK_IGNORE)
1313		fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS;
1314
1315	/*
1316	 * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to
1317	 * the removal of the FS_MODIFY bit in calculated mask if it was set
1318	 * because of an ignore mask that is now going to survive FS_MODIFY.
1319	 */
1320	if (ignore && (fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1321	    !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) {
1322		fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
1323		if (!(fsn_mark->mask & FS_MODIFY))
1324			recalc = true;
1325	}
1326
1327	if (fsn_mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE ||
1328	    want_iref == !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
1329		return recalc;
1330
1331	/*
1332	 * NO_IREF may be removed from a mark, but not added.
1333	 * When removed, fsnotify_recalc_mask() will take the inode ref.
1334	 */
1335	WARN_ON_ONCE(!want_iref);
1336	fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF;
1337
1338	return true;
1339}
1340
1341static bool fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
1342				      __u32 mask, unsigned int fan_flags)
1343{
1344	bool recalc;
1345
1346	spin_lock(&fsn_mark->lock);
1347	if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS))
1348		fsn_mark->mask |= mask;
1349	else
1350		fsn_mark->ignore_mask |= mask;
1351
1352	recalc = fsnotify_calc_mask(fsn_mark) &
1353		~fsnotify_conn_mask(fsn_mark->connector);
1354
1355	recalc |= fanotify_mark_update_flags(fsn_mark, fan_flags);
1356	spin_unlock(&fsn_mark->lock);
1357
1358	return recalc;
1359}
1360
1361struct fan_fsid {
1362	struct super_block *sb;
1363	__kernel_fsid_t id;
1364	bool weak;
1365};
1366
1367static int fanotify_set_mark_fsid(struct fsnotify_group *group,
1368				  struct fsnotify_mark *mark,
1369				  struct fan_fsid *fsid)
1370{
1371	struct fsnotify_mark_connector *conn;
1372	struct fsnotify_mark *old;
1373	struct super_block *old_sb = NULL;
1374
1375	FANOTIFY_MARK(mark)->fsid = fsid->id;
1376	mark->flags |= FSNOTIFY_MARK_FLAG_HAS_FSID;
1377	if (fsid->weak)
1378		mark->flags |= FSNOTIFY_MARK_FLAG_WEAK_FSID;
1379
1380	/* First mark added will determine if group is single or multi fsid */
1381	if (list_empty(&group->marks_list))
1382		return 0;
1383
1384	/* Find sb of an existing mark */
1385	list_for_each_entry(old, &group->marks_list, g_list) {
1386		conn = READ_ONCE(old->connector);
1387		if (!conn)
1388			continue;
1389		old_sb = fsnotify_connector_sb(conn);
1390		if (old_sb)
1391			break;
1392	}
1393
1394	/* Only detached marks left? */
1395	if (!old_sb)
1396		return 0;
1397
1398	/* Do not allow mixing of marks with weak and strong fsid */
1399	if ((mark->flags ^ old->flags) & FSNOTIFY_MARK_FLAG_WEAK_FSID)
1400		return -EXDEV;
1401
1402	/* Allow mixing of marks with strong fsid from different fs */
1403	if (!fsid->weak)
1404		return 0;
1405
1406	/* Do not allow mixing marks with weak fsid from different fs */
1407	if (old_sb != fsid->sb)
1408		return -EXDEV;
1409
1410	/* Do not allow mixing marks from different btrfs sub-volumes */
1411	if (!fanotify_fsid_equal(&FANOTIFY_MARK(old)->fsid,
1412				 &FANOTIFY_MARK(mark)->fsid))
1413		return -EXDEV;
1414
1415	return 0;
1416}
1417
1418static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
1419						   void *obj,
1420						   unsigned int obj_type,
1421						   unsigned int fan_flags,
1422						   struct fan_fsid *fsid)
1423{
1424	struct ucounts *ucounts = group->fanotify_data.ucounts;
1425	struct fanotify_mark *fan_mark;
1426	struct fsnotify_mark *mark;
1427	int ret;
1428
1429	/*
1430	 * Enforce per user marks limits per user in all containing user ns.
1431	 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count
1432	 * in the limited groups account.
1433	 */
1434	BUILD_BUG_ON(!(FANOTIFY_ADMIN_INIT_FLAGS & FAN_UNLIMITED_MARKS));
1435	if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) &&
1436	    !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))
1437		return ERR_PTR(-ENOSPC);
1438
1439	fan_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
1440	if (!fan_mark) {
1441		ret = -ENOMEM;
1442		goto out_dec_ucounts;
1443	}
1444
1445	mark = &fan_mark->fsn_mark;
1446	fsnotify_init_mark(mark, group);
1447	if (fan_flags & FAN_MARK_EVICTABLE)
1448		mark->flags |= FSNOTIFY_MARK_FLAG_NO_IREF;
1449
1450	/* Cache fsid of filesystem containing the marked object */
1451	if (fsid) {
1452		ret = fanotify_set_mark_fsid(group, mark, fsid);
1453		if (ret)
1454			goto out_put_mark;
1455	} else {
1456		fan_mark->fsid.val[0] = fan_mark->fsid.val[1] = 0;
1457	}
1458
1459	ret = fsnotify_add_mark_locked(mark, obj, obj_type, 0);
1460	if (ret)
1461		goto out_put_mark;
1462
1463	return mark;
1464
1465out_put_mark:
1466	fsnotify_put_mark(mark);
1467out_dec_ucounts:
1468	if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1469		dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS);
1470	return ERR_PTR(ret);
1471}
1472
1473static int fanotify_group_init_error_pool(struct fsnotify_group *group)
1474{
1475	if (mempool_initialized(&group->fanotify_data.error_events_pool))
1476		return 0;
1477
1478	return mempool_init_kmalloc_pool(&group->fanotify_data.error_events_pool,
1479					 FANOTIFY_DEFAULT_FEE_POOL_SIZE,
1480					 sizeof(struct fanotify_error_event));
1481}
1482
1483static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
1484					     __u32 mask, unsigned int fan_flags)
1485{
1486	/*
1487	 * Non evictable mark cannot be downgraded to evictable mark.
1488	 */
1489	if (fan_flags & FAN_MARK_EVICTABLE &&
1490	    !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
1491		return -EEXIST;
1492
1493	/*
1494	 * New ignore mask semantics cannot be downgraded to old semantics.
1495	 */
1496	if (fan_flags & FAN_MARK_IGNORED_MASK &&
1497	    fsn_mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
1498		return -EEXIST;
1499
1500	/*
1501	 * An ignore mask that survives modify could never be downgraded to not
1502	 * survive modify.  With new FAN_MARK_IGNORE semantics we make that rule
1503	 * explicit and return an error when trying to update the ignore mask
1504	 * without the original FAN_MARK_IGNORED_SURV_MODIFY value.
1505	 */
1506	if (fan_flags & FAN_MARK_IGNORE &&
1507	    !(fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1508	    fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
1509		return -EEXIST;
1510
1511	/* For now pre-content events are not generated for directories */
1512	mask |= fsn_mark->mask;
1513	if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR)
1514		return -EEXIST;
1515
1516	return 0;
1517}
1518
1519static int fanotify_add_mark(struct fsnotify_group *group,
1520			     void *obj, unsigned int obj_type,
1521			     __u32 mask, unsigned int fan_flags,
1522			     struct fan_fsid *fsid)
1523{
1524	struct fsnotify_mark *fsn_mark;
1525	bool recalc;
1526	int ret = 0;
1527
1528	fsnotify_group_lock(group);
1529	fsn_mark = fsnotify_find_mark(obj, obj_type, group);
1530	if (!fsn_mark) {
1531		fsn_mark = fanotify_add_new_mark(group, obj, obj_type,
1532						 fan_flags, fsid);
1533		if (IS_ERR(fsn_mark)) {
1534			fsnotify_group_unlock(group);
1535			return PTR_ERR(fsn_mark);
1536		}
1537	}
1538
1539	/*
1540	 * Check if requested mark flags conflict with an existing mark flags.
1541	 */
1542	ret = fanotify_may_update_existing_mark(fsn_mark, mask, fan_flags);
1543	if (ret)
1544		goto out;
1545
1546	/*
1547	 * Error events are pre-allocated per group, only if strictly
1548	 * needed (i.e. FAN_FS_ERROR was requested).
1549	 */
1550	if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS) &&
1551	    (mask & FAN_FS_ERROR)) {
1552		ret = fanotify_group_init_error_pool(group);
1553		if (ret)
1554			goto out;
1555	}
1556
1557	recalc = fanotify_mark_add_to_mask(fsn_mark, mask, fan_flags);
1558	if (recalc)
1559		fsnotify_recalc_mask(fsn_mark->connector);
1560
1561out:
1562	fsnotify_group_unlock(group);
1563
1564	fsnotify_put_mark(fsn_mark);
1565
1566	if (!ret && (mask & FANOTIFY_PERM_EVENTS))
1567		fanotify_perm_watchdog_group_add(group);
1568
1569	return ret;
1570}
1571
1572static struct fsnotify_event *fanotify_alloc_overflow_event(void)
1573{
1574	struct fanotify_event *oevent;
1575
1576	oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
1577	if (!oevent)
1578		return NULL;
1579
1580	fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
1581	oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
1582
1583	return &oevent->fse;
1584}
1585
1586static struct hlist_head *fanotify_alloc_merge_hash(void)
1587{
1588	struct hlist_head *hash;
1589
1590	hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS,
1591		       GFP_KERNEL_ACCOUNT);
1592	if (!hash)
1593		return NULL;
1594
1595	__hash_init(hash, FANOTIFY_HTABLE_SIZE);
1596
1597	return hash;
1598}
1599
1600DEFINE_CLASS(fsnotify_group,
1601	     struct fsnotify_group *,
1602	     if (!IS_ERR_OR_NULL(_T)) fsnotify_destroy_group(_T),
1603	     fsnotify_alloc_group(ops, flags),
1604	     const struct fsnotify_ops *ops, int flags)
1605
1606/* fanotify syscalls */
1607SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
1608{
1609	struct user_namespace *user_ns = current_user_ns();
1610	int f_flags, fd;
1611	unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
1612	unsigned int class = flags & FANOTIFY_CLASS_BITS;
1613	unsigned int internal_flags = 0;
1614
1615	pr_debug("%s: flags=%x event_f_flags=%x\n",
1616		 __func__, flags, event_f_flags);
1617
1618	if (!capable(CAP_SYS_ADMIN)) {
1619		/*
1620		 * An unprivileged user can setup an fanotify group with
1621		 * limited functionality - an unprivileged group is limited to
1622		 * notification events with file handles or mount ids and it
1623		 * cannot use unlimited queue/marks.
1624		 */
1625		if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) ||
1626		    !(flags & (FANOTIFY_FID_BITS | FAN_REPORT_MNT)))
1627			return -EPERM;
1628
1629		/*
1630		 * Setting the internal flag FANOTIFY_UNPRIV on the group
1631		 * prevents setting mount/filesystem marks on this group and
1632		 * prevents reporting pid and open fd in events.
1633		 */
1634		internal_flags |= FANOTIFY_UNPRIV;
1635	}
1636
1637#ifdef CONFIG_AUDITSYSCALL
1638	if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
1639#else
1640	if (flags & ~FANOTIFY_INIT_FLAGS)
1641#endif
1642		return -EINVAL;
1643
1644	/*
1645	 * A pidfd can only be returned for a thread-group leader; thus
1646	 * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually
1647	 * exclusive.
1648	 */
1649	if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID))
1650		return -EINVAL;
1651
1652	/* Don't allow mixing mnt events with inode events for now */
1653	if (flags & FAN_REPORT_MNT) {
1654		if (class != FAN_CLASS_NOTIF)
1655			return -EINVAL;
1656		if (flags & (FANOTIFY_FID_BITS | FAN_REPORT_FD_ERROR))
1657			return -EINVAL;
1658	}
1659
1660	if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
1661		return -EINVAL;
1662
1663	switch (event_f_flags & O_ACCMODE) {
1664	case O_RDONLY:
1665	case O_RDWR:
1666	case O_WRONLY:
1667		break;
1668	default:
1669		return -EINVAL;
1670	}
1671
1672	if (fid_mode && class != FAN_CLASS_NOTIF)
1673		return -EINVAL;
1674
1675	/*
1676	 * Child name is reported with parent fid so requires dir fid.
1677	 * We can report both child fid and dir fid with or without name.
1678	 */
1679	if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
1680		return -EINVAL;
1681
1682	/*
1683	 * FAN_REPORT_TARGET_FID requires FAN_REPORT_NAME and FAN_REPORT_FID
1684	 * and is used as an indication to report both dir and child fid on all
1685	 * dirent events.
1686	 */
1687	if ((fid_mode & FAN_REPORT_TARGET_FID) &&
1688	    (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
1689		return -EINVAL;
1690
1691	f_flags = O_RDWR;
1692	if (flags & FAN_CLOEXEC)
1693		f_flags |= O_CLOEXEC;
1694	if (flags & FAN_NONBLOCK)
1695		f_flags |= O_NONBLOCK;
1696
1697	CLASS(fsnotify_group, group)(&fanotify_fsnotify_ops,
1698				     FSNOTIFY_GROUP_USER);
1699	/* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
1700	if (IS_ERR(group))
1701		return PTR_ERR(group);
1702
1703	/* Enforce groups limits per user in all containing user ns */
1704	group->fanotify_data.ucounts = inc_ucount(user_ns, current_euid(),
1705						  UCOUNT_FANOTIFY_GROUPS);
1706	if (!group->fanotify_data.ucounts)
1707		return -EMFILE;
1708
1709	group->fanotify_data.flags = flags | internal_flags;
1710	group->memcg = get_mem_cgroup_from_mm(current->mm);
1711	group->user_ns = get_user_ns(user_ns);
1712
1713	group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
1714	if (!group->fanotify_data.merge_hash)
1715		return -ENOMEM;
1716
1717	group->overflow_event = fanotify_alloc_overflow_event();
1718	if (unlikely(!group->overflow_event))
1719		return -ENOMEM;
1720
1721	if (force_o_largefile())
1722		event_f_flags |= O_LARGEFILE;
1723	group->fanotify_data.f_flags = event_f_flags;
1724	init_waitqueue_head(&group->fanotify_data.access_waitq);
1725	INIT_LIST_HEAD(&group->fanotify_data.access_list);
1726	INIT_LIST_HEAD(&group->fanotify_data.perm_grp_list);
1727	switch (class) {
1728	case FAN_CLASS_NOTIF:
1729		group->priority = FSNOTIFY_PRIO_NORMAL;
1730		break;
1731	case FAN_CLASS_CONTENT:
1732		group->priority = FSNOTIFY_PRIO_CONTENT;
1733		break;
1734	case FAN_CLASS_PRE_CONTENT:
1735		group->priority = FSNOTIFY_PRIO_PRE_CONTENT;
1736		break;
1737	default:
1738		return -EINVAL;
1739	}
1740
1741	BUILD_BUG_ON(!(FANOTIFY_ADMIN_INIT_FLAGS & FAN_UNLIMITED_QUEUE));
1742	if (flags & FAN_UNLIMITED_QUEUE) {
1743		group->max_events = UINT_MAX;
1744	} else {
1745		group->max_events = fanotify_max_queued_events;
1746	}
1747
1748	if (flags & FAN_ENABLE_AUDIT) {
1749		if (!capable(CAP_AUDIT_WRITE))
1750			return -EPERM;
1751	}
1752
1753	fd = FD_ADD(f_flags,
1754		    anon_inode_getfile_fmode("[fanotify]", &fanotify_fops,
1755					     group, f_flags, FMODE_NONOTIFY));
1756	if (fd >= 0)
1757		retain_and_null_ptr(group);
1758	return fd;
1759}
1760
1761static int fanotify_test_fsid(struct dentry *dentry, unsigned int flags,
1762			      struct fan_fsid *fsid)
1763{
1764	unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1765	__kernel_fsid_t root_fsid;
1766	int err;
1767
1768	/*
1769	 * Make sure dentry is not of a filesystem with zero fsid (e.g. fuse).
1770	 */
1771	err = vfs_get_fsid(dentry, &fsid->id);
1772	if (err)
1773		return err;
1774
1775	fsid->sb = dentry->d_sb;
1776	if (!fsid->id.val[0] && !fsid->id.val[1]) {
1777		err = -ENODEV;
1778		goto weak;
1779	}
1780
1781	/*
1782	 * Make sure dentry is not of a filesystem subvolume (e.g. btrfs)
1783	 * which uses a different fsid than sb root.
1784	 */
1785	err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid);
1786	if (err)
1787		return err;
1788
1789	if (!fanotify_fsid_equal(&root_fsid, &fsid->id)) {
1790		err = -EXDEV;
1791		goto weak;
1792	}
1793
1794	fsid->weak = false;
1795	return 0;
1796
1797weak:
1798	/* Allow weak fsid when marking inodes */
1799	fsid->weak = true;
1800	return (mark_type == FAN_MARK_INODE) ? 0 : err;
1801}
1802
1803/* Check if filesystem can encode a unique fid */
1804static int fanotify_test_fid(struct dentry *dentry, unsigned int flags)
1805{
1806	unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1807	const struct export_operations *nop = dentry->d_sb->s_export_op;
1808
1809	/*
1810	 * We need to make sure that the filesystem supports encoding of
1811	 * file handles so user can use name_to_handle_at() to compare fids
1812	 * reported with events to the file handle of watched objects.
1813	 */
1814	if (!exportfs_can_encode_fid(nop))
1815		return -EOPNOTSUPP;
1816
1817	/*
1818	 * For sb/mount mark, we also need to make sure that the filesystem
1819	 * supports decoding file handles, so user has a way to map back the
1820	 * reported fids to filesystem objects.
1821	 */
1822	if (mark_type != FAN_MARK_INODE && !exportfs_can_decode_fh(nop))
1823		return -EOPNOTSUPP;
1824
1825	return 0;
1826}
1827
1828static int fanotify_events_supported(struct fsnotify_group *group,
1829				     const struct path *path, __u64 mask,
1830				     unsigned int flags)
1831{
1832	unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1833	bool is_dir = d_is_dir(path->dentry);
1834	/* Strict validation of events in non-dir inode mask with v5.17+ APIs */
1835	bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) ||
1836				 (mask & FAN_RENAME) ||
1837				 (flags & FAN_MARK_IGNORE);
1838
1839	/*
1840	 * Filesystems need to opt-into pre-content evnets (a.k.a HSM)
1841	 * and they are only supported on regular files and directories.
1842	 */
1843	if (mask & FANOTIFY_PRE_CONTENT_EVENTS) {
1844		if (!(path->mnt->mnt_sb->s_iflags & SB_I_ALLOW_HSM))
1845			return -EOPNOTSUPP;
1846		if (!is_dir && !d_is_reg(path->dentry))
1847			return -EINVAL;
1848	}
1849
1850	/*
1851	 * Some filesystems such as 'proc' acquire unusual locks when opening
1852	 * files. For them fanotify permission events have high chances of
1853	 * deadlocking the system - open done when reporting fanotify event
1854	 * blocks on this "unusual" lock while another process holding the lock
1855	 * waits for fanotify permission event to be answered. Just disallow
1856	 * permission events for such filesystems.
1857	 */
1858	if (mask & FANOTIFY_PERM_EVENTS &&
1859	    path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1860		return -EINVAL;
1861
1862	/*
1863	 * mount and sb marks are not allowed on kernel internal pseudo fs,
1864	 * like pipe_mnt, because that would subscribe to events on all the
1865	 * anonynous pipes in the system.
1866	 *
1867	 * SB_NOUSER covers all of the internal pseudo fs whose objects are not
1868	 * exposed to user's mount namespace, but there are other SB_KERNMOUNT
1869	 * fs, like nsfs, debugfs, for which the value of allowing sb and mount
1870	 * mark is questionable. For now we leave them alone.
1871	 */
1872	if (mark_type != FAN_MARK_INODE &&
1873	    path->mnt->mnt_sb->s_flags & SB_NOUSER)
1874		return -EINVAL;
1875
1876	/*
1877	 * We shouldn't have allowed setting dirent events and the directory
1878	 * flags FAN_ONDIR and FAN_EVENT_ON_CHILD in mask of non-dir inode,
1879	 * but because we always allowed it, error only when using new APIs.
1880	 */
1881	if (strict_dir_events && mark_type == FAN_MARK_INODE &&
1882	    !is_dir && (mask & FANOTIFY_DIRONLY_EVENT_BITS))
1883		return -ENOTDIR;
1884
1885	return 0;
1886}
1887
1888static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1889			    int dfd, const char  __user *pathname)
1890{
1891	struct inode *inode = NULL;
1892	struct fsnotify_group *group;
1893	struct path path;
1894	struct fan_fsid __fsid, *fsid = NULL;
1895	struct user_namespace *user_ns = NULL;
1896	struct mnt_namespace *mntns;
1897	u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1898	unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1899	unsigned int mark_cmd = flags & FANOTIFY_MARK_CMD_BITS;
1900	unsigned int ignore = flags & FANOTIFY_MARK_IGNORE_BITS;
1901	unsigned int obj_type, fid_mode;
1902	void *obj = NULL;
1903	u32 umask = 0;
1904	int ret;
1905
1906	pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1907		 __func__, fanotify_fd, flags, dfd, pathname, mask);
1908
1909	/* we only use the lower 32 bits as of right now. */
1910	if (upper_32_bits(mask))
1911		return -EINVAL;
1912
1913	if (flags & ~FANOTIFY_MARK_FLAGS)
1914		return -EINVAL;
1915
1916	switch (mark_type) {
1917	case FAN_MARK_INODE:
1918		obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1919		break;
1920	case FAN_MARK_MOUNT:
1921		obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1922		break;
1923	case FAN_MARK_FILESYSTEM:
1924		obj_type = FSNOTIFY_OBJ_TYPE_SB;
1925		break;
1926	case FAN_MARK_MNTNS:
1927		obj_type = FSNOTIFY_OBJ_TYPE_MNTNS;
1928		break;
1929	default:
1930		return -EINVAL;
1931	}
1932
1933	switch (mark_cmd) {
1934	case FAN_MARK_ADD:
1935	case FAN_MARK_REMOVE:
1936		if (!mask)
1937			return -EINVAL;
1938		break;
1939	case FAN_MARK_FLUSH:
1940		if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1941			return -EINVAL;
1942		break;
1943	default:
1944		return -EINVAL;
1945	}
1946
1947	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1948		valid_mask |= FANOTIFY_PERM_EVENTS;
1949
1950	if (mask & ~valid_mask)
1951		return -EINVAL;
1952
1953
1954	/* We don't allow FAN_MARK_IGNORE & FAN_MARK_IGNORED_MASK together */
1955	if (ignore == (FAN_MARK_IGNORE | FAN_MARK_IGNORED_MASK))
1956		return -EINVAL;
1957
1958	/*
1959	 * Event flags (FAN_ONDIR, FAN_EVENT_ON_CHILD) have no effect with
1960	 * FAN_MARK_IGNORED_MASK.
1961	 */
1962	if (ignore == FAN_MARK_IGNORED_MASK) {
1963		mask &= ~FANOTIFY_EVENT_FLAGS;
1964		umask = FANOTIFY_EVENT_FLAGS;
1965	}
1966
1967	CLASS(fd, f)(fanotify_fd);
1968	if (fd_empty(f))
1969		return -EBADF;
1970
1971	/* verify that this is indeed an fanotify instance */
1972	if (unlikely(fd_file(f)->f_op != &fanotify_fops))
1973		return -EINVAL;
1974	group = fd_file(f)->private_data;
1975
1976	/* Only report mount events on mnt namespace */
1977	if (FAN_GROUP_FLAG(group, FAN_REPORT_MNT)) {
1978		if (mask & ~FANOTIFY_MOUNT_EVENTS)
1979			return -EINVAL;
1980		if (mark_type != FAN_MARK_MNTNS)
1981			return -EINVAL;
1982	} else {
1983		if (mask & FANOTIFY_MOUNT_EVENTS)
1984			return -EINVAL;
1985		if (mark_type == FAN_MARK_MNTNS)
1986			return -EINVAL;
1987	}
1988
1989	/*
1990	 * A user is allowed to setup sb/mount/mntns marks only if it is
1991	 * capable in the user ns where the group was created.
1992	 */
1993	if (!ns_capable(group->user_ns, CAP_SYS_ADMIN) &&
1994	    mark_type != FAN_MARK_INODE)
1995		return -EPERM;
1996
1997	/*
1998	 * Permission events are not allowed for FAN_CLASS_NOTIF.
1999	 * Pre-content permission events are not allowed for FAN_CLASS_CONTENT.
2000	 */
2001	if (mask & FANOTIFY_PERM_EVENTS &&
2002	    group->priority == FSNOTIFY_PRIO_NORMAL)
2003		return -EINVAL;
2004	else if (mask & FANOTIFY_PRE_CONTENT_EVENTS &&
2005		 group->priority == FSNOTIFY_PRIO_CONTENT)
2006		return -EINVAL;
2007
2008	if (mask & FAN_FS_ERROR &&
2009	    mark_type != FAN_MARK_FILESYSTEM)
2010		return -EINVAL;
2011
2012	/*
2013	 * Evictable is only relevant for inode marks, because only inode object
2014	 * can be evicted on memory pressure.
2015	 */
2016	if (flags & FAN_MARK_EVICTABLE &&
2017	     mark_type != FAN_MARK_INODE)
2018		return -EINVAL;
2019
2020	/*
2021	 * Events that do not carry enough information to report
2022	 * event->fd require a group that supports reporting fid.  Those
2023	 * events are not supported on a mount mark, because they do not
2024	 * carry enough information (i.e. path) to be filtered by mount
2025	 * point.
2026	 */
2027	fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
2028	if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_MOUNT_EVENTS|FANOTIFY_EVENT_FLAGS) &&
2029	    (!fid_mode || mark_type == FAN_MARK_MOUNT))
2030		return -EINVAL;
2031
2032	/*
2033	 * FAN_RENAME uses special info type records to report the old and
2034	 * new parent+name.  Reporting only old and new parent id is less
2035	 * useful and was not implemented.
2036	 */
2037	if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME))
2038		return -EINVAL;
2039
2040	/* Pre-content events are not currently generated for directories. */
2041	if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR)
2042		return -EINVAL;
2043
2044	if (mark_cmd == FAN_MARK_FLUSH) {
2045		fsnotify_clear_marks_by_group(group, obj_type);
2046		return 0;
2047	}
2048
2049	ret = fanotify_find_path(dfd, pathname, &path, flags,
2050			(mask & ALL_FSNOTIFY_EVENTS), obj_type);
2051	if (ret)
2052		return ret;
2053
2054	if (mark_cmd == FAN_MARK_ADD) {
2055		ret = fanotify_events_supported(group, &path, mask, flags);
2056		if (ret)
2057			goto path_put_and_out;
2058	}
2059
2060	if (fid_mode) {
2061		ret = fanotify_test_fsid(path.dentry, flags, &__fsid);
2062		if (ret)
2063			goto path_put_and_out;
2064
2065		ret = fanotify_test_fid(path.dentry, flags);
2066		if (ret)
2067			goto path_put_and_out;
2068
2069		fsid = &__fsid;
2070	}
2071
2072	/*
2073	 * In addition to being capable in the user ns where group was created,
2074	 * the user also needs to be capable in the user ns associated with
2075	 * the filesystem or in the user ns associated with the mntns
2076	 * (when marking mntns).
2077	 */
2078	if (obj_type == FSNOTIFY_OBJ_TYPE_INODE) {
2079		inode = path.dentry->d_inode;
2080		obj = inode;
2081	} else if (obj_type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
2082		user_ns = path.mnt->mnt_sb->s_user_ns;
2083		obj = path.mnt;
2084	} else if (obj_type == FSNOTIFY_OBJ_TYPE_SB) {
2085		user_ns = path.mnt->mnt_sb->s_user_ns;
2086		obj = path.mnt->mnt_sb;
2087	} else if (obj_type == FSNOTIFY_OBJ_TYPE_MNTNS) {
2088		ret = -EINVAL;
2089		mntns = mnt_ns_from_dentry(path.dentry);
2090		if (!mntns)
2091			goto path_put_and_out;
2092		user_ns = mntns->user_ns;
2093		obj = mntns;
2094	}
2095
2096	ret = -EPERM;
2097	if (user_ns && !ns_capable(user_ns, CAP_SYS_ADMIN))
2098		goto path_put_and_out;
2099
2100	ret = -EINVAL;
2101	if (!obj)
2102		goto path_put_and_out;
2103
2104	/*
2105	 * If some other task has this inode open for write we should not add
2106	 * an ignore mask, unless that ignore mask is supposed to survive
2107	 * modification changes anyway.
2108	 */
2109	if (mark_cmd == FAN_MARK_ADD && (flags & FANOTIFY_MARK_IGNORE_BITS) &&
2110	    !(flags & FAN_MARK_IGNORED_SURV_MODIFY)) {
2111		ret = !inode ? -EINVAL : -EISDIR;
2112		/* FAN_MARK_IGNORE requires SURV_MODIFY for sb/mount/dir marks */
2113		if (ignore == FAN_MARK_IGNORE &&
2114		    (!inode || S_ISDIR(inode->i_mode)))
2115			goto path_put_and_out;
2116
2117		ret = 0;
2118		if (inode && inode_is_open_for_write(inode))
2119			goto path_put_and_out;
2120	}
2121
2122	/* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
2123	if (!inode || !S_ISDIR(inode->i_mode)) {
2124		mask &= ~FAN_EVENT_ON_CHILD;
2125		umask = FAN_EVENT_ON_CHILD;
2126		/*
2127		 * If group needs to report parent fid, register for getting
2128		 * events with parent/name info for non-directory.
2129		 */
2130		if ((fid_mode & FAN_REPORT_DIR_FID) &&
2131		    (flags & FAN_MARK_ADD) && !ignore)
2132			mask |= FAN_EVENT_ON_CHILD;
2133	}
2134
2135	/* create/update an inode mark */
2136	switch (mark_cmd) {
2137	case FAN_MARK_ADD:
2138		ret = fanotify_add_mark(group, obj, obj_type, mask, flags,
2139					fsid);
2140		break;
2141	case FAN_MARK_REMOVE:
2142		ret = fanotify_remove_mark(group, obj, obj_type, mask, flags,
2143					   umask);
2144		break;
2145	default:
2146		ret = -EINVAL;
2147	}
2148
2149path_put_and_out:
2150	path_put(&path);
2151	return ret;
2152}
2153
2154#ifndef CONFIG_ARCH_SPLIT_ARG64
2155SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
2156			      __u64, mask, int, dfd,
2157			      const char  __user *, pathname)
2158{
2159	return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
2160}
2161#endif
2162
2163#if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
2164SYSCALL32_DEFINE6(fanotify_mark,
2165				int, fanotify_fd, unsigned int, flags,
2166				SC_ARG64(mask), int, dfd,
2167				const char  __user *, pathname)
2168{
2169	return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
2170				dfd, pathname);
2171}
2172#endif
2173
2174/*
2175 * fanotify_user_setup - Our initialization function.  Note that we cannot return
2176 * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
2177 * must result in panic().
2178 */
2179static int __init fanotify_user_setup(void)
2180{
2181	struct sysinfo si;
2182	int max_marks;
2183
2184	si_meminfo(&si);
2185	/*
2186	 * Allow up to 1% of addressable memory to be accounted for per user
2187	 * marks limited to the range [8192, 1048576]. mount and sb marks are
2188	 * a lot cheaper than inode marks, but there is no reason for a user
2189	 * to have many of those, so calculate by the cost of inode marks.
2190	 */
2191	max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
2192		    INODE_MARK_COST;
2193	max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS,
2194				     FANOTIFY_DEFAULT_MAX_USER_MARKS);
2195
2196	BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS);
2197	BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 14);
2198	BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 11);
2199
2200	fanotify_mark_cache = KMEM_CACHE(fanotify_mark,
2201					 SLAB_PANIC|SLAB_ACCOUNT);
2202	fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
2203					       SLAB_PANIC);
2204	fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
2205						SLAB_PANIC);
2206	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
2207		fanotify_perm_event_cachep =
2208			KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
2209	}
2210	fanotify_mnt_event_cachep = KMEM_CACHE(fanotify_mnt_event, SLAB_PANIC);
2211
2212	fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
2213	init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
2214					FANOTIFY_DEFAULT_MAX_GROUPS;
2215	init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks;
2216	fanotify_sysctls_init();
2217
2218	return 0;
2219}
2220device_initcall(fanotify_user_setup);
~/ Read-only file preview.