16162207e8865a3a8b67126a075a31bf04bab747
[linux-2.6-microblaze.git] / fs / notify / fanotify / fanotify_user.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 "../../mount.h"
26 #include "../fdinfo.h"
27 #include "fanotify.h"
28
29 #define FANOTIFY_DEFAULT_MAX_EVENTS     16384
30 #define FANOTIFY_DEFAULT_MAX_MARKS      8192
31 #define FANOTIFY_DEFAULT_MAX_LISTENERS  128
32
33 /*
34  * All flags that may be specified in parameter event_f_flags of fanotify_init.
35  *
36  * Internal and external open flags are stored together in field f_flags of
37  * struct file. Only external open flags shall be allowed in event_f_flags.
38  * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
39  * excluded.
40  */
41 #define FANOTIFY_INIT_ALL_EVENT_F_BITS                          ( \
42                 O_ACCMODE       | O_APPEND      | O_NONBLOCK    | \
43                 __O_SYNC        | O_DSYNC       | O_CLOEXEC     | \
44                 O_LARGEFILE     | O_NOATIME     )
45
46 extern const struct fsnotify_ops fanotify_fsnotify_ops;
47
48 struct kmem_cache *fanotify_mark_cache __read_mostly;
49 struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
50 struct kmem_cache *fanotify_path_event_cachep __read_mostly;
51 struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
52
53 #define FANOTIFY_EVENT_ALIGN 4
54 #define FANOTIFY_INFO_HDR_LEN \
55         (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
56
57 static int fanotify_fid_info_len(int fh_len, int name_len)
58 {
59         int info_len = fh_len;
60
61         if (name_len)
62                 info_len += name_len + 1;
63
64         return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
65 }
66
67 static int fanotify_event_info_len(unsigned int fid_mode,
68                                    struct fanotify_event *event)
69 {
70         struct fanotify_info *info = fanotify_event_info(event);
71         int dir_fh_len = fanotify_event_dir_fh_len(event);
72         int fh_len = fanotify_event_object_fh_len(event);
73         int info_len = 0;
74         int dot_len = 0;
75
76         if (dir_fh_len) {
77                 info_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
78         } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) {
79                 /*
80                  * With group flag FAN_REPORT_NAME, if name was not recorded in
81                  * event on a directory, we will report the name ".".
82                  */
83                 dot_len = 1;
84         }
85
86         if (fh_len)
87                 info_len += fanotify_fid_info_len(fh_len, dot_len);
88
89         return info_len;
90 }
91
92 /*
93  * Get an fanotify notification event if one exists and is small
94  * enough to fit in "count". Return an error pointer if the count
95  * is not large enough. When permission event is dequeued, its state is
96  * updated accordingly.
97  */
98 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
99                                             size_t count)
100 {
101         size_t event_size = FAN_EVENT_METADATA_LEN;
102         struct fanotify_event *event = NULL;
103         struct fsnotify_event *fsn_event;
104         unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
105
106         pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
107
108         spin_lock(&group->notification_lock);
109         fsn_event = fsnotify_peek_first_event(group);
110         if (!fsn_event)
111                 goto out;
112
113         event = FANOTIFY_E(fsn_event);
114         if (fid_mode)
115                 event_size += fanotify_event_info_len(fid_mode, event);
116
117         if (event_size > count) {
118                 event = ERR_PTR(-EINVAL);
119                 goto out;
120         }
121
122         /*
123          * Held the notification_lock the whole time, so this is the
124          * same event we peeked above.
125          */
126         fsnotify_remove_first_event(group);
127         if (fanotify_is_perm_event(event->mask))
128                 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
129 out:
130         spin_unlock(&group->notification_lock);
131         return event;
132 }
133
134 static int create_fd(struct fsnotify_group *group, struct path *path,
135                      struct file **file)
136 {
137         int client_fd;
138         struct file *new_file;
139
140         client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
141         if (client_fd < 0)
142                 return client_fd;
143
144         /*
145          * we need a new file handle for the userspace program so it can read even if it was
146          * originally opened O_WRONLY.
147          */
148         new_file = dentry_open(path,
149                                group->fanotify_data.f_flags | FMODE_NONOTIFY,
150                                current_cred());
151         if (IS_ERR(new_file)) {
152                 /*
153                  * we still send an event even if we can't open the file.  this
154                  * can happen when say tasks are gone and we try to open their
155                  * /proc files or we try to open a WRONLY file like in sysfs
156                  * we just send the errno to userspace since there isn't much
157                  * else we can do.
158                  */
159                 put_unused_fd(client_fd);
160                 client_fd = PTR_ERR(new_file);
161         } else {
162                 *file = new_file;
163         }
164
165         return client_fd;
166 }
167
168 /*
169  * Finish processing of permission event by setting it to ANSWERED state and
170  * drop group->notification_lock.
171  */
172 static void finish_permission_event(struct fsnotify_group *group,
173                                     struct fanotify_perm_event *event,
174                                     unsigned int response)
175                                     __releases(&group->notification_lock)
176 {
177         bool destroy = false;
178
179         assert_spin_locked(&group->notification_lock);
180         event->response = response;
181         if (event->state == FAN_EVENT_CANCELED)
182                 destroy = true;
183         else
184                 event->state = FAN_EVENT_ANSWERED;
185         spin_unlock(&group->notification_lock);
186         if (destroy)
187                 fsnotify_destroy_event(group, &event->fae.fse);
188 }
189
190 static int process_access_response(struct fsnotify_group *group,
191                                    struct fanotify_response *response_struct)
192 {
193         struct fanotify_perm_event *event;
194         int fd = response_struct->fd;
195         int response = response_struct->response;
196
197         pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
198                  fd, response);
199         /*
200          * make sure the response is valid, if invalid we do nothing and either
201          * userspace can send a valid response or we will clean it up after the
202          * timeout
203          */
204         switch (response & ~FAN_AUDIT) {
205         case FAN_ALLOW:
206         case FAN_DENY:
207                 break;
208         default:
209                 return -EINVAL;
210         }
211
212         if (fd < 0)
213                 return -EINVAL;
214
215         if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
216                 return -EINVAL;
217
218         spin_lock(&group->notification_lock);
219         list_for_each_entry(event, &group->fanotify_data.access_list,
220                             fae.fse.list) {
221                 if (event->fd != fd)
222                         continue;
223
224                 list_del_init(&event->fae.fse.list);
225                 finish_permission_event(group, event, response);
226                 wake_up(&group->fanotify_data.access_waitq);
227                 return 0;
228         }
229         spin_unlock(&group->notification_lock);
230
231         return -ENOENT;
232 }
233
234 static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
235                              int info_type, const char *name, size_t name_len,
236                              char __user *buf, size_t count)
237 {
238         struct fanotify_event_info_fid info = { };
239         struct file_handle handle = { };
240         unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
241         size_t fh_len = fh ? fh->len : 0;
242         size_t info_len = fanotify_fid_info_len(fh_len, name_len);
243         size_t len = info_len;
244
245         pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
246                  __func__, fh_len, name_len, info_len, count);
247
248         if (!fh_len)
249                 return 0;
250
251         if (WARN_ON_ONCE(len < sizeof(info) || len > count))
252                 return -EFAULT;
253
254         /*
255          * Copy event info fid header followed by variable sized file handle
256          * and optionally followed by variable sized filename.
257          */
258         switch (info_type) {
259         case FAN_EVENT_INFO_TYPE_FID:
260         case FAN_EVENT_INFO_TYPE_DFID:
261                 if (WARN_ON_ONCE(name_len))
262                         return -EFAULT;
263                 break;
264         case FAN_EVENT_INFO_TYPE_DFID_NAME:
265                 if (WARN_ON_ONCE(!name || !name_len))
266                         return -EFAULT;
267                 break;
268         default:
269                 return -EFAULT;
270         }
271
272         info.hdr.info_type = info_type;
273         info.hdr.len = len;
274         info.fsid = *fsid;
275         if (copy_to_user(buf, &info, sizeof(info)))
276                 return -EFAULT;
277
278         buf += sizeof(info);
279         len -= sizeof(info);
280         if (WARN_ON_ONCE(len < sizeof(handle)))
281                 return -EFAULT;
282
283         handle.handle_type = fh->type;
284         handle.handle_bytes = fh_len;
285         if (copy_to_user(buf, &handle, sizeof(handle)))
286                 return -EFAULT;
287
288         buf += sizeof(handle);
289         len -= sizeof(handle);
290         if (WARN_ON_ONCE(len < fh_len))
291                 return -EFAULT;
292
293         /*
294          * For an inline fh and inline file name, copy through stack to exclude
295          * the copy from usercopy hardening protections.
296          */
297         fh_buf = fanotify_fh_buf(fh);
298         if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
299                 memcpy(bounce, fh_buf, fh_len);
300                 fh_buf = bounce;
301         }
302         if (copy_to_user(buf, fh_buf, fh_len))
303                 return -EFAULT;
304
305         buf += fh_len;
306         len -= fh_len;
307
308         if (name_len) {
309                 /* Copy the filename with terminating null */
310                 name_len++;
311                 if (WARN_ON_ONCE(len < name_len))
312                         return -EFAULT;
313
314                 if (copy_to_user(buf, name, name_len))
315                         return -EFAULT;
316
317                 buf += name_len;
318                 len -= name_len;
319         }
320
321         /* Pad with 0's */
322         WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
323         if (len > 0 && clear_user(buf, len))
324                 return -EFAULT;
325
326         return info_len;
327 }
328
329 static ssize_t copy_event_to_user(struct fsnotify_group *group,
330                                   struct fanotify_event *event,
331                                   char __user *buf, size_t count)
332 {
333         struct fanotify_event_metadata metadata;
334         struct path *path = fanotify_event_path(event);
335         struct fanotify_info *info = fanotify_event_info(event);
336         unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
337         struct file *f = NULL;
338         int ret, fd = FAN_NOFD;
339         int info_type = 0;
340
341         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
342
343         metadata.event_len = FAN_EVENT_METADATA_LEN +
344                                 fanotify_event_info_len(fid_mode, event);
345         metadata.metadata_len = FAN_EVENT_METADATA_LEN;
346         metadata.vers = FANOTIFY_METADATA_VERSION;
347         metadata.reserved = 0;
348         metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
349         metadata.pid = pid_vnr(event->pid);
350
351         if (path && path->mnt && path->dentry) {
352                 fd = create_fd(group, path, &f);
353                 if (fd < 0)
354                         return fd;
355         }
356         metadata.fd = fd;
357
358         ret = -EFAULT;
359         /*
360          * Sanity check copy size in case get_one_event() and
361          * event_len sizes ever get out of sync.
362          */
363         if (WARN_ON_ONCE(metadata.event_len > count))
364                 goto out_close_fd;
365
366         if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
367                 goto out_close_fd;
368
369         buf += FAN_EVENT_METADATA_LEN;
370         count -= FAN_EVENT_METADATA_LEN;
371
372         if (fanotify_is_perm_event(event->mask))
373                 FANOTIFY_PERM(event)->fd = fd;
374
375         if (f)
376                 fd_install(fd, f);
377
378         /* Event info records order is: dir fid + name, child fid */
379         if (fanotify_event_dir_fh_len(event)) {
380                 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
381                                              FAN_EVENT_INFO_TYPE_DFID;
382                 ret = copy_info_to_user(fanotify_event_fsid(event),
383                                         fanotify_info_dir_fh(info),
384                                         info_type, fanotify_info_name(info),
385                                         info->name_len, buf, count);
386                 if (ret < 0)
387                         return ret;
388
389                 buf += ret;
390                 count -= ret;
391         }
392
393         if (fanotify_event_object_fh_len(event)) {
394                 const char *dot = NULL;
395                 int dot_len = 0;
396
397                 if (fid_mode == FAN_REPORT_FID || info_type) {
398                         /*
399                          * With only group flag FAN_REPORT_FID only type FID is
400                          * reported. Second info record type is always FID.
401                          */
402                         info_type = FAN_EVENT_INFO_TYPE_FID;
403                 } else if ((fid_mode & FAN_REPORT_NAME) &&
404                            (event->mask & FAN_ONDIR)) {
405                         /*
406                          * With group flag FAN_REPORT_NAME, if name was not
407                          * recorded in an event on a directory, report the
408                          * name "." with info type DFID_NAME.
409                          */
410                         info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
411                         dot = ".";
412                         dot_len = 1;
413                 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
414                            (event->mask & FAN_ONDIR)) {
415                         /*
416                          * With group flag FAN_REPORT_DIR_FID, a single info
417                          * record has type DFID for directory entry modification
418                          * event and for event on a directory.
419                          */
420                         info_type = FAN_EVENT_INFO_TYPE_DFID;
421                 } else {
422                         /*
423                          * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
424                          * a single info record has type FID for event on a
425                          * non-directory, when there is no directory to report.
426                          * For example, on FAN_DELETE_SELF event.
427                          */
428                         info_type = FAN_EVENT_INFO_TYPE_FID;
429                 }
430
431                 ret = copy_info_to_user(fanotify_event_fsid(event),
432                                         fanotify_event_object_fh(event),
433                                         info_type, dot, dot_len, buf, count);
434                 if (ret < 0)
435                         return ret;
436
437                 buf += ret;
438                 count -= ret;
439         }
440
441         return metadata.event_len;
442
443 out_close_fd:
444         if (fd != FAN_NOFD) {
445                 put_unused_fd(fd);
446                 fput(f);
447         }
448         return ret;
449 }
450
451 /* intofiy userspace file descriptor functions */
452 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
453 {
454         struct fsnotify_group *group = file->private_data;
455         __poll_t ret = 0;
456
457         poll_wait(file, &group->notification_waitq, wait);
458         spin_lock(&group->notification_lock);
459         if (!fsnotify_notify_queue_is_empty(group))
460                 ret = EPOLLIN | EPOLLRDNORM;
461         spin_unlock(&group->notification_lock);
462
463         return ret;
464 }
465
466 static ssize_t fanotify_read(struct file *file, char __user *buf,
467                              size_t count, loff_t *pos)
468 {
469         struct fsnotify_group *group;
470         struct fanotify_event *event;
471         char __user *start;
472         int ret;
473         DEFINE_WAIT_FUNC(wait, woken_wake_function);
474
475         start = buf;
476         group = file->private_data;
477
478         pr_debug("%s: group=%p\n", __func__, group);
479
480         add_wait_queue(&group->notification_waitq, &wait);
481         while (1) {
482                 /*
483                  * User can supply arbitrarily large buffer. Avoid softlockups
484                  * in case there are lots of available events.
485                  */
486                 cond_resched();
487                 event = get_one_event(group, count);
488                 if (IS_ERR(event)) {
489                         ret = PTR_ERR(event);
490                         break;
491                 }
492
493                 if (!event) {
494                         ret = -EAGAIN;
495                         if (file->f_flags & O_NONBLOCK)
496                                 break;
497
498                         ret = -ERESTARTSYS;
499                         if (signal_pending(current))
500                                 break;
501
502                         if (start != buf)
503                                 break;
504
505                         wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
506                         continue;
507                 }
508
509                 ret = copy_event_to_user(group, event, buf, count);
510                 if (unlikely(ret == -EOPENSTALE)) {
511                         /*
512                          * We cannot report events with stale fd so drop it.
513                          * Setting ret to 0 will continue the event loop and
514                          * do the right thing if there are no more events to
515                          * read (i.e. return bytes read, -EAGAIN or wait).
516                          */
517                         ret = 0;
518                 }
519
520                 /*
521                  * Permission events get queued to wait for response.  Other
522                  * events can be destroyed now.
523                  */
524                 if (!fanotify_is_perm_event(event->mask)) {
525                         fsnotify_destroy_event(group, &event->fse);
526                 } else {
527                         if (ret <= 0) {
528                                 spin_lock(&group->notification_lock);
529                                 finish_permission_event(group,
530                                         FANOTIFY_PERM(event), FAN_DENY);
531                                 wake_up(&group->fanotify_data.access_waitq);
532                         } else {
533                                 spin_lock(&group->notification_lock);
534                                 list_add_tail(&event->fse.list,
535                                         &group->fanotify_data.access_list);
536                                 spin_unlock(&group->notification_lock);
537                         }
538                 }
539                 if (ret < 0)
540                         break;
541                 buf += ret;
542                 count -= ret;
543         }
544         remove_wait_queue(&group->notification_waitq, &wait);
545
546         if (start != buf && ret != -EFAULT)
547                 ret = buf - start;
548         return ret;
549 }
550
551 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
552 {
553         struct fanotify_response response = { .fd = -1, .response = -1 };
554         struct fsnotify_group *group;
555         int ret;
556
557         if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
558                 return -EINVAL;
559
560         group = file->private_data;
561
562         if (count < sizeof(response))
563                 return -EINVAL;
564
565         count = sizeof(response);
566
567         pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
568
569         if (copy_from_user(&response, buf, count))
570                 return -EFAULT;
571
572         ret = process_access_response(group, &response);
573         if (ret < 0)
574                 count = ret;
575
576         return count;
577 }
578
579 static int fanotify_release(struct inode *ignored, struct file *file)
580 {
581         struct fsnotify_group *group = file->private_data;
582         struct fsnotify_event *fsn_event;
583
584         /*
585          * Stop new events from arriving in the notification queue. since
586          * userspace cannot use fanotify fd anymore, no event can enter or
587          * leave access_list by now either.
588          */
589         fsnotify_group_stop_queueing(group);
590
591         /*
592          * Process all permission events on access_list and notification queue
593          * and simulate reply from userspace.
594          */
595         spin_lock(&group->notification_lock);
596         while (!list_empty(&group->fanotify_data.access_list)) {
597                 struct fanotify_perm_event *event;
598
599                 event = list_first_entry(&group->fanotify_data.access_list,
600                                 struct fanotify_perm_event, fae.fse.list);
601                 list_del_init(&event->fae.fse.list);
602                 finish_permission_event(group, event, FAN_ALLOW);
603                 spin_lock(&group->notification_lock);
604         }
605
606         /*
607          * Destroy all non-permission events. For permission events just
608          * dequeue them and set the response. They will be freed once the
609          * response is consumed and fanotify_get_response() returns.
610          */
611         while ((fsn_event = fsnotify_remove_first_event(group))) {
612                 struct fanotify_event *event = FANOTIFY_E(fsn_event);
613
614                 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
615                         spin_unlock(&group->notification_lock);
616                         fsnotify_destroy_event(group, fsn_event);
617                 } else {
618                         finish_permission_event(group, FANOTIFY_PERM(event),
619                                                 FAN_ALLOW);
620                 }
621                 spin_lock(&group->notification_lock);
622         }
623         spin_unlock(&group->notification_lock);
624
625         /* Response for all permission events it set, wakeup waiters */
626         wake_up(&group->fanotify_data.access_waitq);
627
628         /* matches the fanotify_init->fsnotify_alloc_group */
629         fsnotify_destroy_group(group);
630
631         return 0;
632 }
633
634 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
635 {
636         struct fsnotify_group *group;
637         struct fsnotify_event *fsn_event;
638         void __user *p;
639         int ret = -ENOTTY;
640         size_t send_len = 0;
641
642         group = file->private_data;
643
644         p = (void __user *) arg;
645
646         switch (cmd) {
647         case FIONREAD:
648                 spin_lock(&group->notification_lock);
649                 list_for_each_entry(fsn_event, &group->notification_list, list)
650                         send_len += FAN_EVENT_METADATA_LEN;
651                 spin_unlock(&group->notification_lock);
652                 ret = put_user(send_len, (int __user *) p);
653                 break;
654         }
655
656         return ret;
657 }
658
659 static const struct file_operations fanotify_fops = {
660         .show_fdinfo    = fanotify_show_fdinfo,
661         .poll           = fanotify_poll,
662         .read           = fanotify_read,
663         .write          = fanotify_write,
664         .fasync         = NULL,
665         .release        = fanotify_release,
666         .unlocked_ioctl = fanotify_ioctl,
667         .compat_ioctl   = compat_ptr_ioctl,
668         .llseek         = noop_llseek,
669 };
670
671 static int fanotify_find_path(int dfd, const char __user *filename,
672                               struct path *path, unsigned int flags, __u64 mask,
673                               unsigned int obj_type)
674 {
675         int ret;
676
677         pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
678                  dfd, filename, flags);
679
680         if (filename == NULL) {
681                 struct fd f = fdget(dfd);
682
683                 ret = -EBADF;
684                 if (!f.file)
685                         goto out;
686
687                 ret = -ENOTDIR;
688                 if ((flags & FAN_MARK_ONLYDIR) &&
689                     !(S_ISDIR(file_inode(f.file)->i_mode))) {
690                         fdput(f);
691                         goto out;
692                 }
693
694                 *path = f.file->f_path;
695                 path_get(path);
696                 fdput(f);
697         } else {
698                 unsigned int lookup_flags = 0;
699
700                 if (!(flags & FAN_MARK_DONT_FOLLOW))
701                         lookup_flags |= LOOKUP_FOLLOW;
702                 if (flags & FAN_MARK_ONLYDIR)
703                         lookup_flags |= LOOKUP_DIRECTORY;
704
705                 ret = user_path_at(dfd, filename, lookup_flags, path);
706                 if (ret)
707                         goto out;
708         }
709
710         /* you can only watch an inode if you have read permissions on it */
711         ret = path_permission(path, MAY_READ);
712         if (ret) {
713                 path_put(path);
714                 goto out;
715         }
716
717         ret = security_path_notify(path, mask, obj_type);
718         if (ret)
719                 path_put(path);
720
721 out:
722         return ret;
723 }
724
725 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
726                                             __u32 mask, unsigned int flags,
727                                             __u32 umask, int *destroy)
728 {
729         __u32 oldmask = 0;
730
731         /* umask bits cannot be removed by user */
732         mask &= ~umask;
733         spin_lock(&fsn_mark->lock);
734         if (!(flags & FAN_MARK_IGNORED_MASK)) {
735                 oldmask = fsn_mark->mask;
736                 fsn_mark->mask &= ~mask;
737         } else {
738                 fsn_mark->ignored_mask &= ~mask;
739         }
740         /*
741          * We need to keep the mark around even if remaining mask cannot
742          * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
743          * changes to the mask.
744          * Destroy mark when only umask bits remain.
745          */
746         *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
747         spin_unlock(&fsn_mark->lock);
748
749         return mask & oldmask;
750 }
751
752 static int fanotify_remove_mark(struct fsnotify_group *group,
753                                 fsnotify_connp_t *connp, __u32 mask,
754                                 unsigned int flags, __u32 umask)
755 {
756         struct fsnotify_mark *fsn_mark = NULL;
757         __u32 removed;
758         int destroy_mark;
759
760         mutex_lock(&group->mark_mutex);
761         fsn_mark = fsnotify_find_mark(connp, group);
762         if (!fsn_mark) {
763                 mutex_unlock(&group->mark_mutex);
764                 return -ENOENT;
765         }
766
767         removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
768                                                  umask, &destroy_mark);
769         if (removed & fsnotify_conn_mask(fsn_mark->connector))
770                 fsnotify_recalc_mask(fsn_mark->connector);
771         if (destroy_mark)
772                 fsnotify_detach_mark(fsn_mark);
773         mutex_unlock(&group->mark_mutex);
774         if (destroy_mark)
775                 fsnotify_free_mark(fsn_mark);
776
777         /* matches the fsnotify_find_mark() */
778         fsnotify_put_mark(fsn_mark);
779         return 0;
780 }
781
782 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
783                                          struct vfsmount *mnt, __u32 mask,
784                                          unsigned int flags, __u32 umask)
785 {
786         return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
787                                     mask, flags, umask);
788 }
789
790 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
791                                    struct super_block *sb, __u32 mask,
792                                    unsigned int flags, __u32 umask)
793 {
794         return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
795                                     flags, umask);
796 }
797
798 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
799                                       struct inode *inode, __u32 mask,
800                                       unsigned int flags, __u32 umask)
801 {
802         return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
803                                     flags, umask);
804 }
805
806 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
807                                        __u32 mask,
808                                        unsigned int flags)
809 {
810         __u32 oldmask = -1;
811
812         spin_lock(&fsn_mark->lock);
813         if (!(flags & FAN_MARK_IGNORED_MASK)) {
814                 oldmask = fsn_mark->mask;
815                 fsn_mark->mask |= mask;
816         } else {
817                 fsn_mark->ignored_mask |= mask;
818                 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
819                         fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
820         }
821         spin_unlock(&fsn_mark->lock);
822
823         return mask & ~oldmask;
824 }
825
826 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
827                                                    fsnotify_connp_t *connp,
828                                                    unsigned int type,
829                                                    __kernel_fsid_t *fsid)
830 {
831         struct fsnotify_mark *mark;
832         int ret;
833
834         if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
835                 return ERR_PTR(-ENOSPC);
836
837         mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
838         if (!mark)
839                 return ERR_PTR(-ENOMEM);
840
841         fsnotify_init_mark(mark, group);
842         ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
843         if (ret) {
844                 fsnotify_put_mark(mark);
845                 return ERR_PTR(ret);
846         }
847
848         return mark;
849 }
850
851
852 static int fanotify_add_mark(struct fsnotify_group *group,
853                              fsnotify_connp_t *connp, unsigned int type,
854                              __u32 mask, unsigned int flags,
855                              __kernel_fsid_t *fsid)
856 {
857         struct fsnotify_mark *fsn_mark;
858         __u32 added;
859
860         mutex_lock(&group->mark_mutex);
861         fsn_mark = fsnotify_find_mark(connp, group);
862         if (!fsn_mark) {
863                 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
864                 if (IS_ERR(fsn_mark)) {
865                         mutex_unlock(&group->mark_mutex);
866                         return PTR_ERR(fsn_mark);
867                 }
868         }
869         added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
870         if (added & ~fsnotify_conn_mask(fsn_mark->connector))
871                 fsnotify_recalc_mask(fsn_mark->connector);
872         mutex_unlock(&group->mark_mutex);
873
874         fsnotify_put_mark(fsn_mark);
875         return 0;
876 }
877
878 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
879                                       struct vfsmount *mnt, __u32 mask,
880                                       unsigned int flags, __kernel_fsid_t *fsid)
881 {
882         return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
883                                  FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
884 }
885
886 static int fanotify_add_sb_mark(struct fsnotify_group *group,
887                                 struct super_block *sb, __u32 mask,
888                                 unsigned int flags, __kernel_fsid_t *fsid)
889 {
890         return fanotify_add_mark(group, &sb->s_fsnotify_marks,
891                                  FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
892 }
893
894 static int fanotify_add_inode_mark(struct fsnotify_group *group,
895                                    struct inode *inode, __u32 mask,
896                                    unsigned int flags, __kernel_fsid_t *fsid)
897 {
898         pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
899
900         /*
901          * If some other task has this inode open for write we should not add
902          * an ignored mark, unless that ignored mark is supposed to survive
903          * modification changes anyway.
904          */
905         if ((flags & FAN_MARK_IGNORED_MASK) &&
906             !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
907             inode_is_open_for_write(inode))
908                 return 0;
909
910         return fanotify_add_mark(group, &inode->i_fsnotify_marks,
911                                  FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
912 }
913
914 static struct fsnotify_event *fanotify_alloc_overflow_event(void)
915 {
916         struct fanotify_event *oevent;
917
918         oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
919         if (!oevent)
920                 return NULL;
921
922         fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
923         oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
924
925         return &oevent->fse;
926 }
927
928 /* fanotify syscalls */
929 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
930 {
931         struct fsnotify_group *group;
932         int f_flags, fd;
933         struct user_struct *user;
934         unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
935         unsigned int class = flags & FANOTIFY_CLASS_BITS;
936
937         pr_debug("%s: flags=%x event_f_flags=%x\n",
938                  __func__, flags, event_f_flags);
939
940         if (!capable(CAP_SYS_ADMIN))
941                 return -EPERM;
942
943 #ifdef CONFIG_AUDITSYSCALL
944         if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
945 #else
946         if (flags & ~FANOTIFY_INIT_FLAGS)
947 #endif
948                 return -EINVAL;
949
950         if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
951                 return -EINVAL;
952
953         switch (event_f_flags & O_ACCMODE) {
954         case O_RDONLY:
955         case O_RDWR:
956         case O_WRONLY:
957                 break;
958         default:
959                 return -EINVAL;
960         }
961
962         if (fid_mode && class != FAN_CLASS_NOTIF)
963                 return -EINVAL;
964
965         /*
966          * Child name is reported with parent fid so requires dir fid.
967          * We can report both child fid and dir fid with or without name.
968          */
969         if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
970                 return -EINVAL;
971
972         user = get_current_user();
973         if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
974                 free_uid(user);
975                 return -EMFILE;
976         }
977
978         f_flags = O_RDWR | FMODE_NONOTIFY;
979         if (flags & FAN_CLOEXEC)
980                 f_flags |= O_CLOEXEC;
981         if (flags & FAN_NONBLOCK)
982                 f_flags |= O_NONBLOCK;
983
984         /* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
985         group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
986         if (IS_ERR(group)) {
987                 free_uid(user);
988                 return PTR_ERR(group);
989         }
990
991         group->fanotify_data.user = user;
992         group->fanotify_data.flags = flags;
993         atomic_inc(&user->fanotify_listeners);
994         group->memcg = get_mem_cgroup_from_mm(current->mm);
995
996         group->overflow_event = fanotify_alloc_overflow_event();
997         if (unlikely(!group->overflow_event)) {
998                 fd = -ENOMEM;
999                 goto out_destroy_group;
1000         }
1001
1002         if (force_o_largefile())
1003                 event_f_flags |= O_LARGEFILE;
1004         group->fanotify_data.f_flags = event_f_flags;
1005         init_waitqueue_head(&group->fanotify_data.access_waitq);
1006         INIT_LIST_HEAD(&group->fanotify_data.access_list);
1007         switch (class) {
1008         case FAN_CLASS_NOTIF:
1009                 group->priority = FS_PRIO_0;
1010                 break;
1011         case FAN_CLASS_CONTENT:
1012                 group->priority = FS_PRIO_1;
1013                 break;
1014         case FAN_CLASS_PRE_CONTENT:
1015                 group->priority = FS_PRIO_2;
1016                 break;
1017         default:
1018                 fd = -EINVAL;
1019                 goto out_destroy_group;
1020         }
1021
1022         if (flags & FAN_UNLIMITED_QUEUE) {
1023                 fd = -EPERM;
1024                 if (!capable(CAP_SYS_ADMIN))
1025                         goto out_destroy_group;
1026                 group->max_events = UINT_MAX;
1027         } else {
1028                 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1029         }
1030
1031         if (flags & FAN_UNLIMITED_MARKS) {
1032                 fd = -EPERM;
1033                 if (!capable(CAP_SYS_ADMIN))
1034                         goto out_destroy_group;
1035                 group->fanotify_data.max_marks = UINT_MAX;
1036         } else {
1037                 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
1038         }
1039
1040         if (flags & FAN_ENABLE_AUDIT) {
1041                 fd = -EPERM;
1042                 if (!capable(CAP_AUDIT_WRITE))
1043                         goto out_destroy_group;
1044         }
1045
1046         fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1047         if (fd < 0)
1048                 goto out_destroy_group;
1049
1050         return fd;
1051
1052 out_destroy_group:
1053         fsnotify_destroy_group(group);
1054         return fd;
1055 }
1056
1057 /* Check if filesystem can encode a unique fid */
1058 static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
1059 {
1060         __kernel_fsid_t root_fsid;
1061         int err;
1062
1063         /*
1064          * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
1065          */
1066         err = vfs_get_fsid(path->dentry, fsid);
1067         if (err)
1068                 return err;
1069
1070         if (!fsid->val[0] && !fsid->val[1])
1071                 return -ENODEV;
1072
1073         /*
1074          * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
1075          * which uses a different fsid than sb root.
1076          */
1077         err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
1078         if (err)
1079                 return err;
1080
1081         if (root_fsid.val[0] != fsid->val[0] ||
1082             root_fsid.val[1] != fsid->val[1])
1083                 return -EXDEV;
1084
1085         /*
1086          * We need to make sure that the file system supports at least
1087          * encoding a file handle so user can use name_to_handle_at() to
1088          * compare fid returned with event to the file handle of watched
1089          * objects. However, name_to_handle_at() requires that the
1090          * filesystem also supports decoding file handles.
1091          */
1092         if (!path->dentry->d_sb->s_export_op ||
1093             !path->dentry->d_sb->s_export_op->fh_to_dentry)
1094                 return -EOPNOTSUPP;
1095
1096         return 0;
1097 }
1098
1099 static int fanotify_events_supported(struct path *path, __u64 mask)
1100 {
1101         /*
1102          * Some filesystems such as 'proc' acquire unusual locks when opening
1103          * files. For them fanotify permission events have high chances of
1104          * deadlocking the system - open done when reporting fanotify event
1105          * blocks on this "unusual" lock while another process holding the lock
1106          * waits for fanotify permission event to be answered. Just disallow
1107          * permission events for such filesystems.
1108          */
1109         if (mask & FANOTIFY_PERM_EVENTS &&
1110             path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1111                 return -EINVAL;
1112         return 0;
1113 }
1114
1115 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1116                             int dfd, const char  __user *pathname)
1117 {
1118         struct inode *inode = NULL;
1119         struct vfsmount *mnt = NULL;
1120         struct fsnotify_group *group;
1121         struct fd f;
1122         struct path path;
1123         __kernel_fsid_t __fsid, *fsid = NULL;
1124         u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1125         unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1126         bool ignored = flags & FAN_MARK_IGNORED_MASK;
1127         unsigned int obj_type, fid_mode;
1128         u32 umask = 0;
1129         int ret;
1130
1131         pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1132                  __func__, fanotify_fd, flags, dfd, pathname, mask);
1133
1134         /* we only use the lower 32 bits as of right now. */
1135         if (mask & ((__u64)0xffffffff << 32))
1136                 return -EINVAL;
1137
1138         if (flags & ~FANOTIFY_MARK_FLAGS)
1139                 return -EINVAL;
1140
1141         switch (mark_type) {
1142         case FAN_MARK_INODE:
1143                 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1144                 break;
1145         case FAN_MARK_MOUNT:
1146                 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1147                 break;
1148         case FAN_MARK_FILESYSTEM:
1149                 obj_type = FSNOTIFY_OBJ_TYPE_SB;
1150                 break;
1151         default:
1152                 return -EINVAL;
1153         }
1154
1155         switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
1156         case FAN_MARK_ADD:
1157         case FAN_MARK_REMOVE:
1158                 if (!mask)
1159                         return -EINVAL;
1160                 break;
1161         case FAN_MARK_FLUSH:
1162                 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1163                         return -EINVAL;
1164                 break;
1165         default:
1166                 return -EINVAL;
1167         }
1168
1169         if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1170                 valid_mask |= FANOTIFY_PERM_EVENTS;
1171
1172         if (mask & ~valid_mask)
1173                 return -EINVAL;
1174
1175         /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1176         if (ignored)
1177                 mask &= ~FANOTIFY_EVENT_FLAGS;
1178
1179         f = fdget(fanotify_fd);
1180         if (unlikely(!f.file))
1181                 return -EBADF;
1182
1183         /* verify that this is indeed an fanotify instance */
1184         ret = -EINVAL;
1185         if (unlikely(f.file->f_op != &fanotify_fops))
1186                 goto fput_and_out;
1187         group = f.file->private_data;
1188
1189         /*
1190          * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF.  These are not
1191          * allowed to set permissions events.
1192          */
1193         ret = -EINVAL;
1194         if (mask & FANOTIFY_PERM_EVENTS &&
1195             group->priority == FS_PRIO_0)
1196                 goto fput_and_out;
1197
1198         /*
1199          * Events with data type inode do not carry enough information to report
1200          * event->fd, so we do not allow setting a mask for inode events unless
1201          * group supports reporting fid.
1202          * inode events are not supported on a mount mark, because they do not
1203          * carry enough information (i.e. path) to be filtered by mount point.
1204          */
1205         fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
1206         if (mask & FANOTIFY_INODE_EVENTS &&
1207             (!fid_mode || mark_type == FAN_MARK_MOUNT))
1208                 goto fput_and_out;
1209
1210         if (flags & FAN_MARK_FLUSH) {
1211                 ret = 0;
1212                 if (mark_type == FAN_MARK_MOUNT)
1213                         fsnotify_clear_vfsmount_marks_by_group(group);
1214                 else if (mark_type == FAN_MARK_FILESYSTEM)
1215                         fsnotify_clear_sb_marks_by_group(group);
1216                 else
1217                         fsnotify_clear_inode_marks_by_group(group);
1218                 goto fput_and_out;
1219         }
1220
1221         ret = fanotify_find_path(dfd, pathname, &path, flags,
1222                         (mask & ALL_FSNOTIFY_EVENTS), obj_type);
1223         if (ret)
1224                 goto fput_and_out;
1225
1226         if (flags & FAN_MARK_ADD) {
1227                 ret = fanotify_events_supported(&path, mask);
1228                 if (ret)
1229                         goto path_put_and_out;
1230         }
1231
1232         if (fid_mode) {
1233                 ret = fanotify_test_fid(&path, &__fsid);
1234                 if (ret)
1235                         goto path_put_and_out;
1236
1237                 fsid = &__fsid;
1238         }
1239
1240         /* inode held in place by reference to path; group by fget on fd */
1241         if (mark_type == FAN_MARK_INODE)
1242                 inode = path.dentry->d_inode;
1243         else
1244                 mnt = path.mnt;
1245
1246         /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1247         if (mnt || !S_ISDIR(inode->i_mode)) {
1248                 mask &= ~FAN_EVENT_ON_CHILD;
1249                 umask = FAN_EVENT_ON_CHILD;
1250                 /*
1251                  * If group needs to report parent fid, register for getting
1252                  * events with parent/name info for non-directory.
1253                  */
1254                 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1255                     (flags & FAN_MARK_ADD) && !ignored)
1256                         mask |= FAN_EVENT_ON_CHILD;
1257         }
1258
1259         /* create/update an inode mark */
1260         switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
1261         case FAN_MARK_ADD:
1262                 if (mark_type == FAN_MARK_MOUNT)
1263                         ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1264                                                          flags, fsid);
1265                 else if (mark_type == FAN_MARK_FILESYSTEM)
1266                         ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1267                                                    flags, fsid);
1268                 else
1269                         ret = fanotify_add_inode_mark(group, inode, mask,
1270                                                       flags, fsid);
1271                 break;
1272         case FAN_MARK_REMOVE:
1273                 if (mark_type == FAN_MARK_MOUNT)
1274                         ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1275                                                             flags, umask);
1276                 else if (mark_type == FAN_MARK_FILESYSTEM)
1277                         ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1278                                                       flags, umask);
1279                 else
1280                         ret = fanotify_remove_inode_mark(group, inode, mask,
1281                                                          flags, umask);
1282                 break;
1283         default:
1284                 ret = -EINVAL;
1285         }
1286
1287 path_put_and_out:
1288         path_put(&path);
1289 fput_and_out:
1290         fdput(f);
1291         return ret;
1292 }
1293
1294 #ifndef CONFIG_ARCH_SPLIT_ARG64
1295 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1296                               __u64, mask, int, dfd,
1297                               const char  __user *, pathname)
1298 {
1299         return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1300 }
1301 #endif
1302
1303 #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1304 SYSCALL32_DEFINE6(fanotify_mark,
1305                                 int, fanotify_fd, unsigned int, flags,
1306                                 SC_ARG64(mask), int, dfd,
1307                                 const char  __user *, pathname)
1308 {
1309         return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1310                                 dfd, pathname);
1311 }
1312 #endif
1313
1314 /*
1315  * fanotify_user_setup - Our initialization function.  Note that we cannot return
1316  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
1317  * must result in panic().
1318  */
1319 static int __init fanotify_user_setup(void)
1320 {
1321         BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
1322         BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1323
1324         fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1325                                          SLAB_PANIC|SLAB_ACCOUNT);
1326         fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1327                                                SLAB_PANIC);
1328         fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1329                                                 SLAB_PANIC);
1330         if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1331                 fanotify_perm_event_cachep =
1332                         KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1333         }
1334
1335         return 0;
1336 }
1337 device_initcall(fanotify_user_setup);