1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fcntl.h>
4 #include <linux/file.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>
20 #include <asm/ioctls.h>
22 #include "../../mount.h"
23 #include "../fdinfo.h"
26 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
27 #define FANOTIFY_DEFAULT_MAX_MARKS 8192
28 #define FANOTIFY_DEFAULT_MAX_LISTENERS 128
31 * All flags that may be specified in parameter event_f_flags of fanotify_init.
33 * Internal and external open flags are stored together in field f_flags of
34 * struct file. Only external open flags shall be allowed in event_f_flags.
35 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
38 #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
39 O_ACCMODE | O_APPEND | O_NONBLOCK | \
40 __O_SYNC | O_DSYNC | O_CLOEXEC | \
41 O_LARGEFILE | O_NOATIME )
43 extern const struct fsnotify_ops fanotify_fsnotify_ops
;
45 struct kmem_cache
*fanotify_mark_cache __read_mostly
;
46 struct kmem_cache
*fanotify_event_cachep __read_mostly
;
47 struct kmem_cache
*fanotify_perm_event_cachep __read_mostly
;
50 * Get an fsnotify notification event if one exists and is small
51 * enough to fit in "count". Return an error pointer if the count
52 * is not large enough.
54 * Called with the group->notification_lock held.
56 static struct fsnotify_event
*get_one_event(struct fsnotify_group
*group
,
59 assert_spin_locked(&group
->notification_lock
);
61 pr_debug("%s: group=%p count=%zd\n", __func__
, group
, count
);
63 if (fsnotify_notify_queue_is_empty(group
))
66 if (FAN_EVENT_METADATA_LEN
> count
)
67 return ERR_PTR(-EINVAL
);
69 /* held the notification_lock the whole time, so this is the
70 * same event we peeked above */
71 return fsnotify_remove_first_event(group
);
74 static int create_fd(struct fsnotify_group
*group
,
75 struct fanotify_event_info
*event
,
79 struct file
*new_file
;
81 pr_debug("%s: group=%p event=%p\n", __func__
, group
, event
);
83 client_fd
= get_unused_fd_flags(group
->fanotify_data
.f_flags
);
88 * we need a new file handle for the userspace program so it can read even if it was
89 * originally opened O_WRONLY.
91 /* it's possible this event was an overflow event. in that case dentry and mnt
92 * are NULL; That's fine, just don't call dentry open */
93 if (event
->path
.dentry
&& event
->path
.mnt
)
94 new_file
= dentry_open(&event
->path
,
95 group
->fanotify_data
.f_flags
| FMODE_NONOTIFY
,
98 new_file
= ERR_PTR(-EOVERFLOW
);
99 if (IS_ERR(new_file
)) {
101 * we still send an event even if we can't open the file. this
102 * can happen when say tasks are gone and we try to open their
103 * /proc files or we try to open a WRONLY file like in sysfs
104 * we just send the errno to userspace since there isn't much
107 put_unused_fd(client_fd
);
108 client_fd
= PTR_ERR(new_file
);
116 static int fill_event_metadata(struct fsnotify_group
*group
,
117 struct fanotify_event_metadata
*metadata
,
118 struct fsnotify_event
*fsn_event
,
122 struct fanotify_event_info
*event
;
124 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__
,
125 group
, metadata
, fsn_event
);
128 event
= container_of(fsn_event
, struct fanotify_event_info
, fse
);
129 metadata
->event_len
= FAN_EVENT_METADATA_LEN
;
130 metadata
->metadata_len
= FAN_EVENT_METADATA_LEN
;
131 metadata
->vers
= FANOTIFY_METADATA_VERSION
;
132 metadata
->reserved
= 0;
133 metadata
->mask
= fsn_event
->mask
& FAN_ALL_OUTGOING_EVENTS
;
134 metadata
->pid
= pid_vnr(event
->tgid
);
135 if (unlikely(fsn_event
->mask
& FAN_Q_OVERFLOW
))
136 metadata
->fd
= FAN_NOFD
;
138 metadata
->fd
= create_fd(group
, event
, file
);
139 if (metadata
->fd
< 0)
146 static struct fanotify_perm_event_info
*dequeue_event(
147 struct fsnotify_group
*group
, int fd
)
149 struct fanotify_perm_event_info
*event
, *return_e
= NULL
;
151 spin_lock(&group
->notification_lock
);
152 list_for_each_entry(event
, &group
->fanotify_data
.access_list
,
157 list_del_init(&event
->fae
.fse
.list
);
161 spin_unlock(&group
->notification_lock
);
163 pr_debug("%s: found return_re=%p\n", __func__
, return_e
);
168 static int process_access_response(struct fsnotify_group
*group
,
169 struct fanotify_response
*response_struct
)
171 struct fanotify_perm_event_info
*event
;
172 int fd
= response_struct
->fd
;
173 int response
= response_struct
->response
;
175 pr_debug("%s: group=%p fd=%d response=%d\n", __func__
, group
,
178 * make sure the response is valid, if invalid we do nothing and either
179 * userspace can send a valid response or we will clean it up after the
193 event
= dequeue_event(group
, fd
);
197 event
->response
= response
;
198 wake_up(&group
->fanotify_data
.access_waitq
);
203 static ssize_t
copy_event_to_user(struct fsnotify_group
*group
,
204 struct fsnotify_event
*event
,
207 struct fanotify_event_metadata fanotify_event_metadata
;
211 pr_debug("%s: group=%p event=%p\n", __func__
, group
, event
);
213 ret
= fill_event_metadata(group
, &fanotify_event_metadata
, event
, &f
);
217 fd
= fanotify_event_metadata
.fd
;
219 if (copy_to_user(buf
, &fanotify_event_metadata
,
220 fanotify_event_metadata
.event_len
))
223 if (fanotify_is_perm_event(event
->mask
))
224 FANOTIFY_PE(event
)->fd
= fd
;
228 return fanotify_event_metadata
.event_len
;
231 if (fd
!= FAN_NOFD
) {
238 /* intofiy userspace file descriptor functions */
239 static unsigned int fanotify_poll(struct file
*file
, poll_table
*wait
)
241 struct fsnotify_group
*group
= file
->private_data
;
244 poll_wait(file
, &group
->notification_waitq
, wait
);
245 spin_lock(&group
->notification_lock
);
246 if (!fsnotify_notify_queue_is_empty(group
))
247 ret
= POLLIN
| POLLRDNORM
;
248 spin_unlock(&group
->notification_lock
);
253 static ssize_t
fanotify_read(struct file
*file
, char __user
*buf
,
254 size_t count
, loff_t
*pos
)
256 struct fsnotify_group
*group
;
257 struct fsnotify_event
*kevent
;
260 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
263 group
= file
->private_data
;
265 pr_debug("%s: group=%p\n", __func__
, group
);
267 add_wait_queue(&group
->notification_waitq
, &wait
);
269 spin_lock(&group
->notification_lock
);
270 kevent
= get_one_event(group
, count
);
271 spin_unlock(&group
->notification_lock
);
273 if (IS_ERR(kevent
)) {
274 ret
= PTR_ERR(kevent
);
280 if (file
->f_flags
& O_NONBLOCK
)
284 if (signal_pending(current
))
290 wait_woken(&wait
, TASK_INTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
294 ret
= copy_event_to_user(group
, kevent
, buf
);
295 if (unlikely(ret
== -EOPENSTALE
)) {
297 * We cannot report events with stale fd so drop it.
298 * Setting ret to 0 will continue the event loop and
299 * do the right thing if there are no more events to
300 * read (i.e. return bytes read, -EAGAIN or wait).
306 * Permission events get queued to wait for response. Other
307 * events can be destroyed now.
309 if (!fanotify_is_perm_event(kevent
->mask
)) {
310 fsnotify_destroy_event(group
, kevent
);
313 FANOTIFY_PE(kevent
)->response
= FAN_DENY
;
314 wake_up(&group
->fanotify_data
.access_waitq
);
316 spin_lock(&group
->notification_lock
);
317 list_add_tail(&kevent
->list
,
318 &group
->fanotify_data
.access_list
);
319 spin_unlock(&group
->notification_lock
);
327 remove_wait_queue(&group
->notification_waitq
, &wait
);
329 if (start
!= buf
&& ret
!= -EFAULT
)
334 static ssize_t
fanotify_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
336 struct fanotify_response response
= { .fd
= -1, .response
= -1 };
337 struct fsnotify_group
*group
;
340 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS
))
343 group
= file
->private_data
;
345 if (count
> sizeof(response
))
346 count
= sizeof(response
);
348 pr_debug("%s: group=%p count=%zu\n", __func__
, group
, count
);
350 if (copy_from_user(&response
, buf
, count
))
353 ret
= process_access_response(group
, &response
);
360 static int fanotify_release(struct inode
*ignored
, struct file
*file
)
362 struct fsnotify_group
*group
= file
->private_data
;
363 struct fanotify_perm_event_info
*event
, *next
;
364 struct fsnotify_event
*fsn_event
;
367 * Stop new events from arriving in the notification queue. since
368 * userspace cannot use fanotify fd anymore, no event can enter or
369 * leave access_list by now either.
371 fsnotify_group_stop_queueing(group
);
374 * Process all permission events on access_list and notification queue
375 * and simulate reply from userspace.
377 spin_lock(&group
->notification_lock
);
378 list_for_each_entry_safe(event
, next
, &group
->fanotify_data
.access_list
,
380 pr_debug("%s: found group=%p event=%p\n", __func__
, group
,
383 list_del_init(&event
->fae
.fse
.list
);
384 event
->response
= FAN_ALLOW
;
388 * Destroy all non-permission events. For permission events just
389 * dequeue them and set the response. They will be freed once the
390 * response is consumed and fanotify_get_response() returns.
392 while (!fsnotify_notify_queue_is_empty(group
)) {
393 fsn_event
= fsnotify_remove_first_event(group
);
394 if (!(fsn_event
->mask
& FAN_ALL_PERM_EVENTS
)) {
395 spin_unlock(&group
->notification_lock
);
396 fsnotify_destroy_event(group
, fsn_event
);
397 spin_lock(&group
->notification_lock
);
399 FANOTIFY_PE(fsn_event
)->response
= FAN_ALLOW
;
402 spin_unlock(&group
->notification_lock
);
404 /* Response for all permission events it set, wakeup waiters */
405 wake_up(&group
->fanotify_data
.access_waitq
);
407 /* matches the fanotify_init->fsnotify_alloc_group */
408 fsnotify_destroy_group(group
);
413 static long fanotify_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
415 struct fsnotify_group
*group
;
416 struct fsnotify_event
*fsn_event
;
421 group
= file
->private_data
;
423 p
= (void __user
*) arg
;
427 spin_lock(&group
->notification_lock
);
428 list_for_each_entry(fsn_event
, &group
->notification_list
, list
)
429 send_len
+= FAN_EVENT_METADATA_LEN
;
430 spin_unlock(&group
->notification_lock
);
431 ret
= put_user(send_len
, (int __user
*) p
);
438 static const struct file_operations fanotify_fops
= {
439 .show_fdinfo
= fanotify_show_fdinfo
,
440 .poll
= fanotify_poll
,
441 .read
= fanotify_read
,
442 .write
= fanotify_write
,
444 .release
= fanotify_release
,
445 .unlocked_ioctl
= fanotify_ioctl
,
446 .compat_ioctl
= fanotify_ioctl
,
447 .llseek
= noop_llseek
,
450 static int fanotify_find_path(int dfd
, const char __user
*filename
,
451 struct path
*path
, unsigned int flags
)
455 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__
,
456 dfd
, filename
, flags
);
458 if (filename
== NULL
) {
459 struct fd f
= fdget(dfd
);
466 if ((flags
& FAN_MARK_ONLYDIR
) &&
467 !(S_ISDIR(file_inode(f
.file
)->i_mode
))) {
472 *path
= f
.file
->f_path
;
476 unsigned int lookup_flags
= 0;
478 if (!(flags
& FAN_MARK_DONT_FOLLOW
))
479 lookup_flags
|= LOOKUP_FOLLOW
;
480 if (flags
& FAN_MARK_ONLYDIR
)
481 lookup_flags
|= LOOKUP_DIRECTORY
;
483 ret
= user_path_at(dfd
, filename
, lookup_flags
, path
);
488 /* you can only watch an inode if you have read permissions on it */
489 ret
= inode_permission(path
->dentry
->d_inode
, MAY_READ
);
496 static __u32
fanotify_mark_remove_from_mask(struct fsnotify_mark
*fsn_mark
,
503 spin_lock(&fsn_mark
->lock
);
504 if (!(flags
& FAN_MARK_IGNORED_MASK
)) {
505 __u32 tmask
= fsn_mark
->mask
& ~mask
;
507 if (flags
& FAN_MARK_ONDIR
)
510 oldmask
= fsn_mark
->mask
;
511 fsn_mark
->mask
= tmask
;
513 __u32 tmask
= fsn_mark
->ignored_mask
& ~mask
;
514 if (flags
& FAN_MARK_ONDIR
)
516 fsn_mark
->ignored_mask
= tmask
;
518 *destroy
= !(fsn_mark
->mask
| fsn_mark
->ignored_mask
);
519 spin_unlock(&fsn_mark
->lock
);
521 return mask
& oldmask
;
524 static int fanotify_remove_vfsmount_mark(struct fsnotify_group
*group
,
525 struct vfsmount
*mnt
, __u32 mask
,
528 struct fsnotify_mark
*fsn_mark
= NULL
;
532 mutex_lock(&group
->mark_mutex
);
533 fsn_mark
= fsnotify_find_mark(&real_mount(mnt
)->mnt_fsnotify_marks
,
536 mutex_unlock(&group
->mark_mutex
);
540 removed
= fanotify_mark_remove_from_mask(fsn_mark
, mask
, flags
,
542 if (removed
& real_mount(mnt
)->mnt_fsnotify_mask
)
543 fsnotify_recalc_mask(real_mount(mnt
)->mnt_fsnotify_marks
);
545 fsnotify_detach_mark(fsn_mark
);
546 mutex_unlock(&group
->mark_mutex
);
548 fsnotify_free_mark(fsn_mark
);
550 fsnotify_put_mark(fsn_mark
);
554 static int fanotify_remove_inode_mark(struct fsnotify_group
*group
,
555 struct inode
*inode
, __u32 mask
,
558 struct fsnotify_mark
*fsn_mark
= NULL
;
562 mutex_lock(&group
->mark_mutex
);
563 fsn_mark
= fsnotify_find_mark(&inode
->i_fsnotify_marks
, group
);
565 mutex_unlock(&group
->mark_mutex
);
569 removed
= fanotify_mark_remove_from_mask(fsn_mark
, mask
, flags
,
571 if (removed
& inode
->i_fsnotify_mask
)
572 fsnotify_recalc_mask(inode
->i_fsnotify_marks
);
574 fsnotify_detach_mark(fsn_mark
);
575 mutex_unlock(&group
->mark_mutex
);
577 fsnotify_free_mark(fsn_mark
);
579 /* matches the fsnotify_find_mark() */
580 fsnotify_put_mark(fsn_mark
);
585 static __u32
fanotify_mark_add_to_mask(struct fsnotify_mark
*fsn_mark
,
591 spin_lock(&fsn_mark
->lock
);
592 if (!(flags
& FAN_MARK_IGNORED_MASK
)) {
593 __u32 tmask
= fsn_mark
->mask
| mask
;
595 if (flags
& FAN_MARK_ONDIR
)
598 oldmask
= fsn_mark
->mask
;
599 fsn_mark
->mask
= tmask
;
601 __u32 tmask
= fsn_mark
->ignored_mask
| mask
;
602 if (flags
& FAN_MARK_ONDIR
)
605 fsn_mark
->ignored_mask
= tmask
;
606 if (flags
& FAN_MARK_IGNORED_SURV_MODIFY
)
607 fsn_mark
->flags
|= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
;
609 spin_unlock(&fsn_mark
->lock
);
611 return mask
& ~oldmask
;
614 static struct fsnotify_mark
*fanotify_add_new_mark(struct fsnotify_group
*group
,
616 struct vfsmount
*mnt
)
618 struct fsnotify_mark
*mark
;
621 if (atomic_read(&group
->num_marks
) > group
->fanotify_data
.max_marks
)
622 return ERR_PTR(-ENOSPC
);
624 mark
= kmem_cache_alloc(fanotify_mark_cache
, GFP_KERNEL
);
626 return ERR_PTR(-ENOMEM
);
628 fsnotify_init_mark(mark
, group
);
629 ret
= fsnotify_add_mark_locked(mark
, inode
, mnt
, 0);
631 fsnotify_put_mark(mark
);
639 static int fanotify_add_vfsmount_mark(struct fsnotify_group
*group
,
640 struct vfsmount
*mnt
, __u32 mask
,
643 struct fsnotify_mark
*fsn_mark
;
646 mutex_lock(&group
->mark_mutex
);
647 fsn_mark
= fsnotify_find_mark(&real_mount(mnt
)->mnt_fsnotify_marks
,
650 fsn_mark
= fanotify_add_new_mark(group
, NULL
, mnt
);
651 if (IS_ERR(fsn_mark
)) {
652 mutex_unlock(&group
->mark_mutex
);
653 return PTR_ERR(fsn_mark
);
656 added
= fanotify_mark_add_to_mask(fsn_mark
, mask
, flags
);
657 if (added
& ~real_mount(mnt
)->mnt_fsnotify_mask
)
658 fsnotify_recalc_mask(real_mount(mnt
)->mnt_fsnotify_marks
);
659 mutex_unlock(&group
->mark_mutex
);
661 fsnotify_put_mark(fsn_mark
);
665 static int fanotify_add_inode_mark(struct fsnotify_group
*group
,
666 struct inode
*inode
, __u32 mask
,
669 struct fsnotify_mark
*fsn_mark
;
672 pr_debug("%s: group=%p inode=%p\n", __func__
, group
, inode
);
675 * If some other task has this inode open for write we should not add
676 * an ignored mark, unless that ignored mark is supposed to survive
677 * modification changes anyway.
679 if ((flags
& FAN_MARK_IGNORED_MASK
) &&
680 !(flags
& FAN_MARK_IGNORED_SURV_MODIFY
) &&
681 (atomic_read(&inode
->i_writecount
) > 0))
684 mutex_lock(&group
->mark_mutex
);
685 fsn_mark
= fsnotify_find_mark(&inode
->i_fsnotify_marks
, group
);
687 fsn_mark
= fanotify_add_new_mark(group
, inode
, NULL
);
688 if (IS_ERR(fsn_mark
)) {
689 mutex_unlock(&group
->mark_mutex
);
690 return PTR_ERR(fsn_mark
);
693 added
= fanotify_mark_add_to_mask(fsn_mark
, mask
, flags
);
694 if (added
& ~inode
->i_fsnotify_mask
)
695 fsnotify_recalc_mask(inode
->i_fsnotify_marks
);
696 mutex_unlock(&group
->mark_mutex
);
698 fsnotify_put_mark(fsn_mark
);
702 /* fanotify syscalls */
703 SYSCALL_DEFINE2(fanotify_init
, unsigned int, flags
, unsigned int, event_f_flags
)
705 struct fsnotify_group
*group
;
707 struct user_struct
*user
;
708 struct fanotify_event_info
*oevent
;
710 pr_debug("%s: flags=%d event_f_flags=%d\n",
711 __func__
, flags
, event_f_flags
);
713 if (!capable(CAP_SYS_ADMIN
))
716 if (flags
& ~FAN_ALL_INIT_FLAGS
)
719 if (event_f_flags
& ~FANOTIFY_INIT_ALL_EVENT_F_BITS
)
722 switch (event_f_flags
& O_ACCMODE
) {
731 user
= get_current_user();
732 if (atomic_read(&user
->fanotify_listeners
) > FANOTIFY_DEFAULT_MAX_LISTENERS
) {
737 f_flags
= O_RDWR
| FMODE_NONOTIFY
;
738 if (flags
& FAN_CLOEXEC
)
739 f_flags
|= O_CLOEXEC
;
740 if (flags
& FAN_NONBLOCK
)
741 f_flags
|= O_NONBLOCK
;
743 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
744 group
= fsnotify_alloc_group(&fanotify_fsnotify_ops
);
747 return PTR_ERR(group
);
750 group
->fanotify_data
.user
= user
;
751 atomic_inc(&user
->fanotify_listeners
);
753 oevent
= fanotify_alloc_event(NULL
, FS_Q_OVERFLOW
, NULL
);
754 if (unlikely(!oevent
)) {
756 goto out_destroy_group
;
758 group
->overflow_event
= &oevent
->fse
;
760 if (force_o_largefile())
761 event_f_flags
|= O_LARGEFILE
;
762 group
->fanotify_data
.f_flags
= event_f_flags
;
763 init_waitqueue_head(&group
->fanotify_data
.access_waitq
);
764 INIT_LIST_HEAD(&group
->fanotify_data
.access_list
);
765 switch (flags
& FAN_ALL_CLASS_BITS
) {
766 case FAN_CLASS_NOTIF
:
767 group
->priority
= FS_PRIO_0
;
769 case FAN_CLASS_CONTENT
:
770 group
->priority
= FS_PRIO_1
;
772 case FAN_CLASS_PRE_CONTENT
:
773 group
->priority
= FS_PRIO_2
;
777 goto out_destroy_group
;
780 if (flags
& FAN_UNLIMITED_QUEUE
) {
782 if (!capable(CAP_SYS_ADMIN
))
783 goto out_destroy_group
;
784 group
->max_events
= UINT_MAX
;
786 group
->max_events
= FANOTIFY_DEFAULT_MAX_EVENTS
;
789 if (flags
& FAN_UNLIMITED_MARKS
) {
791 if (!capable(CAP_SYS_ADMIN
))
792 goto out_destroy_group
;
793 group
->fanotify_data
.max_marks
= UINT_MAX
;
795 group
->fanotify_data
.max_marks
= FANOTIFY_DEFAULT_MAX_MARKS
;
798 fd
= anon_inode_getfd("[fanotify]", &fanotify_fops
, group
, f_flags
);
800 goto out_destroy_group
;
805 fsnotify_destroy_group(group
);
809 SYSCALL_DEFINE5(fanotify_mark
, int, fanotify_fd
, unsigned int, flags
,
810 __u64
, mask
, int, dfd
,
811 const char __user
*, pathname
)
813 struct inode
*inode
= NULL
;
814 struct vfsmount
*mnt
= NULL
;
815 struct fsnotify_group
*group
;
818 u32 valid_mask
= FAN_ALL_EVENTS
| FAN_EVENT_ON_CHILD
;
821 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
822 __func__
, fanotify_fd
, flags
, dfd
, pathname
, mask
);
824 /* we only use the lower 32 bits as of right now. */
825 if (mask
& ((__u64
)0xffffffff << 32))
828 if (flags
& ~FAN_ALL_MARK_FLAGS
)
830 switch (flags
& (FAN_MARK_ADD
| FAN_MARK_REMOVE
| FAN_MARK_FLUSH
)) {
831 case FAN_MARK_ADD
: /* fallthrough */
832 case FAN_MARK_REMOVE
:
837 if (flags
& ~(FAN_MARK_MOUNT
| FAN_MARK_FLUSH
))
844 if (mask
& FAN_ONDIR
) {
845 flags
|= FAN_MARK_ONDIR
;
849 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS
))
850 valid_mask
|= FAN_ALL_PERM_EVENTS
;
852 if (mask
& ~valid_mask
)
855 f
= fdget(fanotify_fd
);
856 if (unlikely(!f
.file
))
859 /* verify that this is indeed an fanotify instance */
861 if (unlikely(f
.file
->f_op
!= &fanotify_fops
))
863 group
= f
.file
->private_data
;
866 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
867 * allowed to set permissions events.
870 if (mask
& FAN_ALL_PERM_EVENTS
&&
871 group
->priority
== FS_PRIO_0
)
874 if (flags
& FAN_MARK_FLUSH
) {
876 if (flags
& FAN_MARK_MOUNT
)
877 fsnotify_clear_vfsmount_marks_by_group(group
);
879 fsnotify_clear_inode_marks_by_group(group
);
883 ret
= fanotify_find_path(dfd
, pathname
, &path
, flags
);
887 /* inode held in place by reference to path; group by fget on fd */
888 if (!(flags
& FAN_MARK_MOUNT
))
889 inode
= path
.dentry
->d_inode
;
893 /* create/update an inode mark */
894 switch (flags
& (FAN_MARK_ADD
| FAN_MARK_REMOVE
)) {
896 if (flags
& FAN_MARK_MOUNT
)
897 ret
= fanotify_add_vfsmount_mark(group
, mnt
, mask
, flags
);
899 ret
= fanotify_add_inode_mark(group
, inode
, mask
, flags
);
901 case FAN_MARK_REMOVE
:
902 if (flags
& FAN_MARK_MOUNT
)
903 ret
= fanotify_remove_vfsmount_mark(group
, mnt
, mask
, flags
);
905 ret
= fanotify_remove_inode_mark(group
, inode
, mask
, flags
);
918 COMPAT_SYSCALL_DEFINE6(fanotify_mark
,
919 int, fanotify_fd
, unsigned int, flags
,
920 __u32
, mask0
, __u32
, mask1
, int, dfd
,
921 const char __user
*, pathname
)
923 return sys_fanotify_mark(fanotify_fd
, flags
,
925 ((__u64
)mask0
<< 32) | mask1
,
927 ((__u64
)mask1
<< 32) | mask0
,
934 * fanotify_user_setup - Our initialization function. Note that we cannot return
935 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
936 * must result in panic().
938 static int __init
fanotify_user_setup(void)
940 fanotify_mark_cache
= KMEM_CACHE(fsnotify_mark
, SLAB_PANIC
);
941 fanotify_event_cachep
= KMEM_CACHE(fanotify_event_info
, SLAB_PANIC
);
942 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS
)) {
943 fanotify_perm_event_cachep
=
944 KMEM_CACHE(fanotify_perm_event_info
, SLAB_PANIC
);
949 device_initcall(fanotify_user_setup
);