]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/notify/fanotify/fanotify_user.c
bf306d4f72f73561095d4f7ec10627967cfd3e61
[linux.git] / fs / notify / fanotify / fanotify_user.c
1 #include <linux/fanotify.h>
2 #include <linux/fcntl.h>
3 #include <linux/file.h>
4 #include <linux/fs.h>
5 #include <linux/anon_inodes.h>
6 #include <linux/fsnotify_backend.h>
7 #include <linux/init.h>
8 #include <linux/mount.h>
9 #include <linux/namei.h>
10 #include <linux/poll.h>
11 #include <linux/security.h>
12 #include <linux/syscalls.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/uaccess.h>
16 #include <linux/compat.h>
17 #include <linux/sched/signal.h>
18
19 #include <asm/ioctls.h>
20
21 #include "../../mount.h"
22 #include "../fdinfo.h"
23 #include "fanotify.h"
24
25 #define FANOTIFY_DEFAULT_MAX_EVENTS     16384
26 #define FANOTIFY_DEFAULT_MAX_MARKS      8192
27 #define FANOTIFY_DEFAULT_MAX_LISTENERS  128
28
29 /*
30  * All flags that may be specified in parameter event_f_flags of fanotify_init.
31  *
32  * Internal and external open flags are stored together in field f_flags of
33  * struct file. Only external open flags shall be allowed in event_f_flags.
34  * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
35  * excluded.
36  */
37 #define FANOTIFY_INIT_ALL_EVENT_F_BITS                          ( \
38                 O_ACCMODE       | O_APPEND      | O_NONBLOCK    | \
39                 __O_SYNC        | O_DSYNC       | O_CLOEXEC     | \
40                 O_LARGEFILE     | O_NOATIME     )
41
42 extern const struct fsnotify_ops fanotify_fsnotify_ops;
43
44 struct kmem_cache *fanotify_mark_cache __read_mostly;
45 struct kmem_cache *fanotify_event_cachep __read_mostly;
46 struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
47
48 /*
49  * Get an fsnotify notification event if one exists and is small
50  * enough to fit in "count". Return an error pointer if the count
51  * is not large enough.
52  *
53  * Called with the group->notification_lock held.
54  */
55 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
56                                             size_t count)
57 {
58         assert_spin_locked(&group->notification_lock);
59
60         pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
61
62         if (fsnotify_notify_queue_is_empty(group))
63                 return NULL;
64
65         if (FAN_EVENT_METADATA_LEN > count)
66                 return ERR_PTR(-EINVAL);
67
68         /* held the notification_lock the whole time, so this is the
69          * same event we peeked above */
70         return fsnotify_remove_first_event(group);
71 }
72
73 static int create_fd(struct fsnotify_group *group,
74                      struct fanotify_event_info *event,
75                      struct file **file)
76 {
77         int client_fd;
78         struct file *new_file;
79
80         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
81
82         client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
83         if (client_fd < 0)
84                 return client_fd;
85
86         /*
87          * we need a new file handle for the userspace program so it can read even if it was
88          * originally opened O_WRONLY.
89          */
90         /* it's possible this event was an overflow event.  in that case dentry and mnt
91          * are NULL;  That's fine, just don't call dentry open */
92         if (event->path.dentry && event->path.mnt)
93                 new_file = dentry_open(&event->path,
94                                        group->fanotify_data.f_flags | FMODE_NONOTIFY,
95                                        current_cred());
96         else
97                 new_file = ERR_PTR(-EOVERFLOW);
98         if (IS_ERR(new_file)) {
99                 /*
100                  * we still send an event even if we can't open the file.  this
101                  * can happen when say tasks are gone and we try to open their
102                  * /proc files or we try to open a WRONLY file like in sysfs
103                  * we just send the errno to userspace since there isn't much
104                  * else we can do.
105                  */
106                 put_unused_fd(client_fd);
107                 client_fd = PTR_ERR(new_file);
108         } else {
109                 *file = new_file;
110         }
111
112         return client_fd;
113 }
114
115 static int fill_event_metadata(struct fsnotify_group *group,
116                                struct fanotify_event_metadata *metadata,
117                                struct fsnotify_event *fsn_event,
118                                struct file **file)
119 {
120         int ret = 0;
121         struct fanotify_event_info *event;
122
123         pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
124                  group, metadata, fsn_event);
125
126         *file = NULL;
127         event = container_of(fsn_event, struct fanotify_event_info, fse);
128         metadata->event_len = FAN_EVENT_METADATA_LEN;
129         metadata->metadata_len = FAN_EVENT_METADATA_LEN;
130         metadata->vers = FANOTIFY_METADATA_VERSION;
131         metadata->reserved = 0;
132         metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
133         metadata->pid = pid_vnr(event->tgid);
134         if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
135                 metadata->fd = FAN_NOFD;
136         else {
137                 metadata->fd = create_fd(group, event, file);
138                 if (metadata->fd < 0)
139                         ret = metadata->fd;
140         }
141
142         return ret;
143 }
144
145 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
146 static struct fanotify_perm_event_info *dequeue_event(
147                                 struct fsnotify_group *group, int fd)
148 {
149         struct fanotify_perm_event_info *event, *return_e = NULL;
150
151         spin_lock(&group->notification_lock);
152         list_for_each_entry(event, &group->fanotify_data.access_list,
153                             fae.fse.list) {
154                 if (event->fd != fd)
155                         continue;
156
157                 list_del_init(&event->fae.fse.list);
158                 return_e = event;
159                 break;
160         }
161         spin_unlock(&group->notification_lock);
162
163         pr_debug("%s: found return_re=%p\n", __func__, return_e);
164
165         return return_e;
166 }
167
168 static int process_access_response(struct fsnotify_group *group,
169                                    struct fanotify_response *response_struct)
170 {
171         struct fanotify_perm_event_info *event;
172         int fd = response_struct->fd;
173         int response = response_struct->response;
174
175         pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
176                  fd, response);
177         /*
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
180          * timeout
181          */
182         switch (response) {
183         case FAN_ALLOW:
184         case FAN_DENY:
185                 break;
186         default:
187                 return -EINVAL;
188         }
189
190         if (fd < 0)
191                 return -EINVAL;
192
193         event = dequeue_event(group, fd);
194         if (!event)
195                 return -ENOENT;
196
197         event->response = response;
198         wake_up(&group->fanotify_data.access_waitq);
199
200         return 0;
201 }
202 #endif
203
204 static ssize_t copy_event_to_user(struct fsnotify_group *group,
205                                   struct fsnotify_event *event,
206                                   char __user *buf)
207 {
208         struct fanotify_event_metadata fanotify_event_metadata;
209         struct file *f;
210         int fd, ret;
211
212         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
213
214         ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
215         if (ret < 0)
216                 return ret;
217
218         fd = fanotify_event_metadata.fd;
219         ret = -EFAULT;
220         if (copy_to_user(buf, &fanotify_event_metadata,
221                          fanotify_event_metadata.event_len))
222                 goto out_close_fd;
223
224 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
225         if (event->mask & FAN_ALL_PERM_EVENTS)
226                 FANOTIFY_PE(event)->fd = fd;
227 #endif
228
229         if (fd != FAN_NOFD)
230                 fd_install(fd, f);
231         return fanotify_event_metadata.event_len;
232
233 out_close_fd:
234         if (fd != FAN_NOFD) {
235                 put_unused_fd(fd);
236                 fput(f);
237         }
238         return ret;
239 }
240
241 /* intofiy userspace file descriptor functions */
242 static unsigned int fanotify_poll(struct file *file, poll_table *wait)
243 {
244         struct fsnotify_group *group = file->private_data;
245         int ret = 0;
246
247         poll_wait(file, &group->notification_waitq, wait);
248         spin_lock(&group->notification_lock);
249         if (!fsnotify_notify_queue_is_empty(group))
250                 ret = POLLIN | POLLRDNORM;
251         spin_unlock(&group->notification_lock);
252
253         return ret;
254 }
255
256 static ssize_t fanotify_read(struct file *file, char __user *buf,
257                              size_t count, loff_t *pos)
258 {
259         struct fsnotify_group *group;
260         struct fsnotify_event *kevent;
261         char __user *start;
262         int ret;
263         DEFINE_WAIT_FUNC(wait, woken_wake_function);
264
265         start = buf;
266         group = file->private_data;
267
268         pr_debug("%s: group=%p\n", __func__, group);
269
270         add_wait_queue(&group->notification_waitq, &wait);
271         while (1) {
272                 spin_lock(&group->notification_lock);
273                 kevent = get_one_event(group, count);
274                 spin_unlock(&group->notification_lock);
275
276                 if (IS_ERR(kevent)) {
277                         ret = PTR_ERR(kevent);
278                         break;
279                 }
280
281                 if (!kevent) {
282                         ret = -EAGAIN;
283                         if (file->f_flags & O_NONBLOCK)
284                                 break;
285
286                         ret = -ERESTARTSYS;
287                         if (signal_pending(current))
288                                 break;
289
290                         if (start != buf)
291                                 break;
292
293                         wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
294                         continue;
295                 }
296
297                 ret = copy_event_to_user(group, kevent, buf);
298                 /*
299                  * Permission events get queued to wait for response.  Other
300                  * events can be destroyed now.
301                  */
302                 if (!(kevent->mask & FAN_ALL_PERM_EVENTS)) {
303                         fsnotify_destroy_event(group, kevent);
304                         if (ret < 0)
305                                 break;
306                 } else {
307 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
308                         if (ret < 0) {
309                                 FANOTIFY_PE(kevent)->response = FAN_DENY;
310                                 wake_up(&group->fanotify_data.access_waitq);
311                                 break;
312                         }
313                         spin_lock(&group->notification_lock);
314                         list_add_tail(&kevent->list,
315                                       &group->fanotify_data.access_list);
316                         spin_unlock(&group->notification_lock);
317 #endif
318                 }
319                 buf += ret;
320                 count -= ret;
321         }
322         remove_wait_queue(&group->notification_waitq, &wait);
323
324         if (start != buf && ret != -EFAULT)
325                 ret = buf - start;
326         return ret;
327 }
328
329 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
330 {
331 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
332         struct fanotify_response response = { .fd = -1, .response = -1 };
333         struct fsnotify_group *group;
334         int ret;
335
336         group = file->private_data;
337
338         if (count > sizeof(response))
339                 count = sizeof(response);
340
341         pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
342
343         if (copy_from_user(&response, buf, count))
344                 return -EFAULT;
345
346         ret = process_access_response(group, &response);
347         if (ret < 0)
348                 count = ret;
349
350         return count;
351 #else
352         return -EINVAL;
353 #endif
354 }
355
356 static int fanotify_release(struct inode *ignored, struct file *file)
357 {
358         struct fsnotify_group *group = file->private_data;
359
360 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
361         struct fanotify_perm_event_info *event, *next;
362         struct fsnotify_event *fsn_event;
363
364         /*
365          * Stop new events from arriving in the notification queue. since
366          * userspace cannot use fanotify fd anymore, no event can enter or
367          * leave access_list by now either.
368          */
369         fsnotify_group_stop_queueing(group);
370
371         /*
372          * Process all permission events on access_list and notification queue
373          * and simulate reply from userspace.
374          */
375         spin_lock(&group->notification_lock);
376         list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
377                                  fae.fse.list) {
378                 pr_debug("%s: found group=%p event=%p\n", __func__, group,
379                          event);
380
381                 list_del_init(&event->fae.fse.list);
382                 event->response = FAN_ALLOW;
383         }
384
385         /*
386          * Destroy all non-permission events. For permission events just
387          * dequeue them and set the response. They will be freed once the
388          * response is consumed and fanotify_get_response() returns.
389          */
390         while (!fsnotify_notify_queue_is_empty(group)) {
391                 fsn_event = fsnotify_remove_first_event(group);
392                 if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
393                         spin_unlock(&group->notification_lock);
394                         fsnotify_destroy_event(group, fsn_event);
395                         spin_lock(&group->notification_lock);
396                 } else
397                         FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
398         }
399         spin_unlock(&group->notification_lock);
400
401         /* Response for all permission events it set, wakeup waiters */
402         wake_up(&group->fanotify_data.access_waitq);
403 #endif
404
405         /* matches the fanotify_init->fsnotify_alloc_group */
406         fsnotify_destroy_group(group);
407
408         return 0;
409 }
410
411 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
412 {
413         struct fsnotify_group *group;
414         struct fsnotify_event *fsn_event;
415         void __user *p;
416         int ret = -ENOTTY;
417         size_t send_len = 0;
418
419         group = file->private_data;
420
421         p = (void __user *) arg;
422
423         switch (cmd) {
424         case FIONREAD:
425                 spin_lock(&group->notification_lock);
426                 list_for_each_entry(fsn_event, &group->notification_list, list)
427                         send_len += FAN_EVENT_METADATA_LEN;
428                 spin_unlock(&group->notification_lock);
429                 ret = put_user(send_len, (int __user *) p);
430                 break;
431         }
432
433         return ret;
434 }
435
436 static const struct file_operations fanotify_fops = {
437         .show_fdinfo    = fanotify_show_fdinfo,
438         .poll           = fanotify_poll,
439         .read           = fanotify_read,
440         .write          = fanotify_write,
441         .fasync         = NULL,
442         .release        = fanotify_release,
443         .unlocked_ioctl = fanotify_ioctl,
444         .compat_ioctl   = fanotify_ioctl,
445         .llseek         = noop_llseek,
446 };
447
448 static int fanotify_find_path(int dfd, const char __user *filename,
449                               struct path *path, unsigned int flags)
450 {
451         int ret;
452
453         pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
454                  dfd, filename, flags);
455
456         if (filename == NULL) {
457                 struct fd f = fdget(dfd);
458
459                 ret = -EBADF;
460                 if (!f.file)
461                         goto out;
462
463                 ret = -ENOTDIR;
464                 if ((flags & FAN_MARK_ONLYDIR) &&
465                     !(S_ISDIR(file_inode(f.file)->i_mode))) {
466                         fdput(f);
467                         goto out;
468                 }
469
470                 *path = f.file->f_path;
471                 path_get(path);
472                 fdput(f);
473         } else {
474                 unsigned int lookup_flags = 0;
475
476                 if (!(flags & FAN_MARK_DONT_FOLLOW))
477                         lookup_flags |= LOOKUP_FOLLOW;
478                 if (flags & FAN_MARK_ONLYDIR)
479                         lookup_flags |= LOOKUP_DIRECTORY;
480
481                 ret = user_path_at(dfd, filename, lookup_flags, path);
482                 if (ret)
483                         goto out;
484         }
485
486         /* you can only watch an inode if you have read permissions on it */
487         ret = inode_permission(path->dentry->d_inode, MAY_READ);
488         if (ret)
489                 path_put(path);
490 out:
491         return ret;
492 }
493
494 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
495                                             __u32 mask,
496                                             unsigned int flags,
497                                             int *destroy)
498 {
499         __u32 oldmask = 0;
500
501         spin_lock(&fsn_mark->lock);
502         if (!(flags & FAN_MARK_IGNORED_MASK)) {
503                 __u32 tmask = fsn_mark->mask & ~mask;
504
505                 if (flags & FAN_MARK_ONDIR)
506                         tmask &= ~FAN_ONDIR;
507
508                 oldmask = fsn_mark->mask;
509                 fsn_mark->mask = tmask;
510         } else {
511                 __u32 tmask = fsn_mark->ignored_mask & ~mask;
512                 if (flags & FAN_MARK_ONDIR)
513                         tmask &= ~FAN_ONDIR;
514                 fsn_mark->ignored_mask = tmask;
515         }
516         *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
517         spin_unlock(&fsn_mark->lock);
518
519         return mask & oldmask;
520 }
521
522 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
523                                          struct vfsmount *mnt, __u32 mask,
524                                          unsigned int flags)
525 {
526         struct fsnotify_mark *fsn_mark = NULL;
527         __u32 removed;
528         int destroy_mark;
529
530         mutex_lock(&group->mark_mutex);
531         fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
532                                       group);
533         if (!fsn_mark) {
534                 mutex_unlock(&group->mark_mutex);
535                 return -ENOENT;
536         }
537
538         removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
539                                                  &destroy_mark);
540         if (removed & real_mount(mnt)->mnt_fsnotify_mask)
541                 fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
542         if (destroy_mark)
543                 fsnotify_detach_mark(fsn_mark);
544         mutex_unlock(&group->mark_mutex);
545         if (destroy_mark)
546                 fsnotify_free_mark(fsn_mark);
547
548         fsnotify_put_mark(fsn_mark);
549         return 0;
550 }
551
552 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
553                                       struct inode *inode, __u32 mask,
554                                       unsigned int flags)
555 {
556         struct fsnotify_mark *fsn_mark = NULL;
557         __u32 removed;
558         int destroy_mark;
559
560         mutex_lock(&group->mark_mutex);
561         fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
562         if (!fsn_mark) {
563                 mutex_unlock(&group->mark_mutex);
564                 return -ENOENT;
565         }
566
567         removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
568                                                  &destroy_mark);
569         if (removed & inode->i_fsnotify_mask)
570                 fsnotify_recalc_mask(inode->i_fsnotify_marks);
571         if (destroy_mark)
572                 fsnotify_detach_mark(fsn_mark);
573         mutex_unlock(&group->mark_mutex);
574         if (destroy_mark)
575                 fsnotify_free_mark(fsn_mark);
576
577         /* matches the fsnotify_find_mark() */
578         fsnotify_put_mark(fsn_mark);
579
580         return 0;
581 }
582
583 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
584                                        __u32 mask,
585                                        unsigned int flags)
586 {
587         __u32 oldmask = -1;
588
589         spin_lock(&fsn_mark->lock);
590         if (!(flags & FAN_MARK_IGNORED_MASK)) {
591                 __u32 tmask = fsn_mark->mask | mask;
592
593                 if (flags & FAN_MARK_ONDIR)
594                         tmask |= FAN_ONDIR;
595
596                 oldmask = fsn_mark->mask;
597                 fsn_mark->mask = tmask;
598         } else {
599                 __u32 tmask = fsn_mark->ignored_mask | mask;
600                 if (flags & FAN_MARK_ONDIR)
601                         tmask |= FAN_ONDIR;
602
603                 fsn_mark->ignored_mask = tmask;
604                 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
605                         fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
606         }
607         spin_unlock(&fsn_mark->lock);
608
609         return mask & ~oldmask;
610 }
611
612 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
613                                                    struct inode *inode,
614                                                    struct vfsmount *mnt)
615 {
616         struct fsnotify_mark *mark;
617         int ret;
618
619         if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
620                 return ERR_PTR(-ENOSPC);
621
622         mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
623         if (!mark)
624                 return ERR_PTR(-ENOMEM);
625
626         fsnotify_init_mark(mark, group);
627         ret = fsnotify_add_mark_locked(mark, inode, mnt, 0);
628         if (ret) {
629                 fsnotify_put_mark(mark);
630                 return ERR_PTR(ret);
631         }
632
633         return mark;
634 }
635
636
637 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
638                                       struct vfsmount *mnt, __u32 mask,
639                                       unsigned int flags)
640 {
641         struct fsnotify_mark *fsn_mark;
642         __u32 added;
643
644         mutex_lock(&group->mark_mutex);
645         fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
646                                       group);
647         if (!fsn_mark) {
648                 fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
649                 if (IS_ERR(fsn_mark)) {
650                         mutex_unlock(&group->mark_mutex);
651                         return PTR_ERR(fsn_mark);
652                 }
653         }
654         added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
655         if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
656                 fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
657         mutex_unlock(&group->mark_mutex);
658
659         fsnotify_put_mark(fsn_mark);
660         return 0;
661 }
662
663 static int fanotify_add_inode_mark(struct fsnotify_group *group,
664                                    struct inode *inode, __u32 mask,
665                                    unsigned int flags)
666 {
667         struct fsnotify_mark *fsn_mark;
668         __u32 added;
669
670         pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
671
672         /*
673          * If some other task has this inode open for write we should not add
674          * an ignored mark, unless that ignored mark is supposed to survive
675          * modification changes anyway.
676          */
677         if ((flags & FAN_MARK_IGNORED_MASK) &&
678             !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
679             (atomic_read(&inode->i_writecount) > 0))
680                 return 0;
681
682         mutex_lock(&group->mark_mutex);
683         fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
684         if (!fsn_mark) {
685                 fsn_mark = fanotify_add_new_mark(group, inode, NULL);
686                 if (IS_ERR(fsn_mark)) {
687                         mutex_unlock(&group->mark_mutex);
688                         return PTR_ERR(fsn_mark);
689                 }
690         }
691         added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
692         if (added & ~inode->i_fsnotify_mask)
693                 fsnotify_recalc_mask(inode->i_fsnotify_marks);
694         mutex_unlock(&group->mark_mutex);
695
696         fsnotify_put_mark(fsn_mark);
697         return 0;
698 }
699
700 /* fanotify syscalls */
701 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
702 {
703         struct fsnotify_group *group;
704         int f_flags, fd;
705         struct user_struct *user;
706         struct fanotify_event_info *oevent;
707
708         pr_debug("%s: flags=%d event_f_flags=%d\n",
709                 __func__, flags, event_f_flags);
710
711         if (!capable(CAP_SYS_ADMIN))
712                 return -EPERM;
713
714         if (flags & ~FAN_ALL_INIT_FLAGS)
715                 return -EINVAL;
716
717         if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
718                 return -EINVAL;
719
720         switch (event_f_flags & O_ACCMODE) {
721         case O_RDONLY:
722         case O_RDWR:
723         case O_WRONLY:
724                 break;
725         default:
726                 return -EINVAL;
727         }
728
729         user = get_current_user();
730         if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
731                 free_uid(user);
732                 return -EMFILE;
733         }
734
735         f_flags = O_RDWR | FMODE_NONOTIFY;
736         if (flags & FAN_CLOEXEC)
737                 f_flags |= O_CLOEXEC;
738         if (flags & FAN_NONBLOCK)
739                 f_flags |= O_NONBLOCK;
740
741         /* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
742         group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
743         if (IS_ERR(group)) {
744                 free_uid(user);
745                 return PTR_ERR(group);
746         }
747
748         group->fanotify_data.user = user;
749         atomic_inc(&user->fanotify_listeners);
750
751         oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
752         if (unlikely(!oevent)) {
753                 fd = -ENOMEM;
754                 goto out_destroy_group;
755         }
756         group->overflow_event = &oevent->fse;
757
758         if (force_o_largefile())
759                 event_f_flags |= O_LARGEFILE;
760         group->fanotify_data.f_flags = event_f_flags;
761 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
762         init_waitqueue_head(&group->fanotify_data.access_waitq);
763         INIT_LIST_HEAD(&group->fanotify_data.access_list);
764 #endif
765         switch (flags & FAN_ALL_CLASS_BITS) {
766         case FAN_CLASS_NOTIF:
767                 group->priority = FS_PRIO_0;
768                 break;
769         case FAN_CLASS_CONTENT:
770                 group->priority = FS_PRIO_1;
771                 break;
772         case FAN_CLASS_PRE_CONTENT:
773                 group->priority = FS_PRIO_2;
774                 break;
775         default:
776                 fd = -EINVAL;
777                 goto out_destroy_group;
778         }
779
780         if (flags & FAN_UNLIMITED_QUEUE) {
781                 fd = -EPERM;
782                 if (!capable(CAP_SYS_ADMIN))
783                         goto out_destroy_group;
784                 group->max_events = UINT_MAX;
785         } else {
786                 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
787         }
788
789         if (flags & FAN_UNLIMITED_MARKS) {
790                 fd = -EPERM;
791                 if (!capable(CAP_SYS_ADMIN))
792                         goto out_destroy_group;
793                 group->fanotify_data.max_marks = UINT_MAX;
794         } else {
795                 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
796         }
797
798         fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
799         if (fd < 0)
800                 goto out_destroy_group;
801
802         return fd;
803
804 out_destroy_group:
805         fsnotify_destroy_group(group);
806         return fd;
807 }
808
809 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
810                               __u64, mask, int, dfd,
811                               const char  __user *, pathname)
812 {
813         struct inode *inode = NULL;
814         struct vfsmount *mnt = NULL;
815         struct fsnotify_group *group;
816         struct fd f;
817         struct path path;
818         int ret;
819
820         pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
821                  __func__, fanotify_fd, flags, dfd, pathname, mask);
822
823         /* we only use the lower 32 bits as of right now. */
824         if (mask & ((__u64)0xffffffff << 32))
825                 return -EINVAL;
826
827         if (flags & ~FAN_ALL_MARK_FLAGS)
828                 return -EINVAL;
829         switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
830         case FAN_MARK_ADD:              /* fallthrough */
831         case FAN_MARK_REMOVE:
832                 if (!mask)
833                         return -EINVAL;
834                 break;
835         case FAN_MARK_FLUSH:
836                 if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
837                         return -EINVAL;
838                 break;
839         default:
840                 return -EINVAL;
841         }
842
843         if (mask & FAN_ONDIR) {
844                 flags |= FAN_MARK_ONDIR;
845                 mask &= ~FAN_ONDIR;
846         }
847
848 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
849         if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
850 #else
851         if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
852 #endif
853                 return -EINVAL;
854
855         f = fdget(fanotify_fd);
856         if (unlikely(!f.file))
857                 return -EBADF;
858
859         /* verify that this is indeed an fanotify instance */
860         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          * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF.  These are not
867          * allowed to set permissions events.
868          */
869         ret = -EINVAL;
870         if (mask & FAN_ALL_PERM_EVENTS &&
871             group->priority == FS_PRIO_0)
872                 goto fput_and_out;
873
874         if (flags & FAN_MARK_FLUSH) {
875                 ret = 0;
876                 if (flags & FAN_MARK_MOUNT)
877                         fsnotify_clear_vfsmount_marks_by_group(group);
878                 else
879                         fsnotify_clear_inode_marks_by_group(group);
880                 goto fput_and_out;
881         }
882
883         ret = fanotify_find_path(dfd, pathname, &path, flags);
884         if (ret)
885                 goto fput_and_out;
886
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;
890         else
891                 mnt = path.mnt;
892
893         /* create/update an inode mark */
894         switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
895         case FAN_MARK_ADD:
896                 if (flags & FAN_MARK_MOUNT)
897                         ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
898                 else
899                         ret = fanotify_add_inode_mark(group, inode, mask, flags);
900                 break;
901         case FAN_MARK_REMOVE:
902                 if (flags & FAN_MARK_MOUNT)
903                         ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
904                 else
905                         ret = fanotify_remove_inode_mark(group, inode, mask, flags);
906                 break;
907         default:
908                 ret = -EINVAL;
909         }
910
911         path_put(&path);
912 fput_and_out:
913         fdput(f);
914         return ret;
915 }
916
917 #ifdef CONFIG_COMPAT
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)
922 {
923         return sys_fanotify_mark(fanotify_fd, flags,
924 #ifdef __BIG_ENDIAN
925                                 ((__u64)mask0 << 32) | mask1,
926 #else
927                                 ((__u64)mask1 << 32) | mask0,
928 #endif
929                                  dfd, pathname);
930 }
931 #endif
932
933 /*
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().
937  */
938 static int __init fanotify_user_setup(void)
939 {
940         fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
941         fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
942 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
943         fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
944                                                 SLAB_PANIC);
945 #endif
946
947         return 0;
948 }
949 device_initcall(fanotify_user_setup);