Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/fanotify.h>
0003 #include <linux/fcntl.h>
0004 #include <linux/fdtable.h>
0005 #include <linux/file.h>
0006 #include <linux/fs.h>
0007 #include <linux/anon_inodes.h>
0008 #include <linux/fsnotify_backend.h>
0009 #include <linux/init.h>
0010 #include <linux/mount.h>
0011 #include <linux/namei.h>
0012 #include <linux/poll.h>
0013 #include <linux/security.h>
0014 #include <linux/syscalls.h>
0015 #include <linux/slab.h>
0016 #include <linux/types.h>
0017 #include <linux/uaccess.h>
0018 #include <linux/compat.h>
0019 #include <linux/sched/signal.h>
0020 #include <linux/memcontrol.h>
0021 #include <linux/statfs.h>
0022 #include <linux/exportfs.h>
0023 
0024 #include <asm/ioctls.h>
0025 
0026 #include "../../mount.h"
0027 #include "../fdinfo.h"
0028 #include "fanotify.h"
0029 
0030 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
0031 #define FANOTIFY_OLD_DEFAULT_MAX_MARKS  8192
0032 #define FANOTIFY_DEFAULT_MAX_GROUPS 128
0033 #define FANOTIFY_DEFAULT_FEE_POOL_SIZE  32
0034 
0035 /*
0036  * Legacy fanotify marks limits (8192) is per group and we introduced a tunable
0037  * limit of marks per user, similar to inotify.  Effectively, the legacy limit
0038  * of fanotify marks per user is <max marks per group> * <max groups per user>.
0039  * This default limit (1M) also happens to match the increased limit of inotify
0040  * max_user_watches since v5.10.
0041  */
0042 #define FANOTIFY_DEFAULT_MAX_USER_MARKS \
0043     (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS)
0044 
0045 /*
0046  * Most of the memory cost of adding an inode mark is pinning the marked inode.
0047  * The size of the filesystem inode struct is not uniform across filesystems,
0048  * so double the size of a VFS inode is used as a conservative approximation.
0049  */
0050 #define INODE_MARK_COST (2 * sizeof(struct inode))
0051 
0052 /* configurable via /proc/sys/fs/fanotify/ */
0053 static int fanotify_max_queued_events __read_mostly;
0054 
0055 #ifdef CONFIG_SYSCTL
0056 
0057 #include <linux/sysctl.h>
0058 
0059 static long ft_zero = 0;
0060 static long ft_int_max = INT_MAX;
0061 
0062 static struct ctl_table fanotify_table[] = {
0063     {
0064         .procname   = "max_user_groups",
0065         .data   = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS],
0066         .maxlen     = sizeof(long),
0067         .mode       = 0644,
0068         .proc_handler   = proc_doulongvec_minmax,
0069         .extra1     = &ft_zero,
0070         .extra2     = &ft_int_max,
0071     },
0072     {
0073         .procname   = "max_user_marks",
0074         .data   = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS],
0075         .maxlen     = sizeof(long),
0076         .mode       = 0644,
0077         .proc_handler   = proc_doulongvec_minmax,
0078         .extra1     = &ft_zero,
0079         .extra2     = &ft_int_max,
0080     },
0081     {
0082         .procname   = "max_queued_events",
0083         .data       = &fanotify_max_queued_events,
0084         .maxlen     = sizeof(int),
0085         .mode       = 0644,
0086         .proc_handler   = proc_dointvec_minmax,
0087         .extra1     = SYSCTL_ZERO
0088     },
0089     { }
0090 };
0091 
0092 static void __init fanotify_sysctls_init(void)
0093 {
0094     register_sysctl("fs/fanotify", fanotify_table);
0095 }
0096 #else
0097 #define fanotify_sysctls_init() do { } while (0)
0098 #endif /* CONFIG_SYSCTL */
0099 
0100 /*
0101  * All flags that may be specified in parameter event_f_flags of fanotify_init.
0102  *
0103  * Internal and external open flags are stored together in field f_flags of
0104  * struct file. Only external open flags shall be allowed in event_f_flags.
0105  * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
0106  * excluded.
0107  */
0108 #define FANOTIFY_INIT_ALL_EVENT_F_BITS              ( \
0109         O_ACCMODE   | O_APPEND  | O_NONBLOCK    | \
0110         __O_SYNC    | O_DSYNC   | O_CLOEXEC     | \
0111         O_LARGEFILE | O_NOATIME )
0112 
0113 extern const struct fsnotify_ops fanotify_fsnotify_ops;
0114 
0115 struct kmem_cache *fanotify_mark_cache __read_mostly;
0116 struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
0117 struct kmem_cache *fanotify_path_event_cachep __read_mostly;
0118 struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
0119 
0120 #define FANOTIFY_EVENT_ALIGN 4
0121 #define FANOTIFY_FID_INFO_HDR_LEN \
0122     (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
0123 #define FANOTIFY_PIDFD_INFO_HDR_LEN \
0124     sizeof(struct fanotify_event_info_pidfd)
0125 #define FANOTIFY_ERROR_INFO_LEN \
0126     (sizeof(struct fanotify_event_info_error))
0127 
0128 static int fanotify_fid_info_len(int fh_len, int name_len)
0129 {
0130     int info_len = fh_len;
0131 
0132     if (name_len)
0133         info_len += name_len + 1;
0134 
0135     return roundup(FANOTIFY_FID_INFO_HDR_LEN + info_len,
0136                FANOTIFY_EVENT_ALIGN);
0137 }
0138 
0139 /* FAN_RENAME may have one or two dir+name info records */
0140 static int fanotify_dir_name_info_len(struct fanotify_event *event)
0141 {
0142     struct fanotify_info *info = fanotify_event_info(event);
0143     int dir_fh_len = fanotify_event_dir_fh_len(event);
0144     int dir2_fh_len = fanotify_event_dir2_fh_len(event);
0145     int info_len = 0;
0146 
0147     if (dir_fh_len)
0148         info_len += fanotify_fid_info_len(dir_fh_len,
0149                           info->name_len);
0150     if (dir2_fh_len)
0151         info_len += fanotify_fid_info_len(dir2_fh_len,
0152                           info->name2_len);
0153 
0154     return info_len;
0155 }
0156 
0157 static size_t fanotify_event_len(unsigned int info_mode,
0158                  struct fanotify_event *event)
0159 {
0160     size_t event_len = FAN_EVENT_METADATA_LEN;
0161     int fh_len;
0162     int dot_len = 0;
0163 
0164     if (!info_mode)
0165         return event_len;
0166 
0167     if (fanotify_is_error_event(event->mask))
0168         event_len += FANOTIFY_ERROR_INFO_LEN;
0169 
0170     if (fanotify_event_has_any_dir_fh(event)) {
0171         event_len += fanotify_dir_name_info_len(event);
0172     } else if ((info_mode & FAN_REPORT_NAME) &&
0173            (event->mask & FAN_ONDIR)) {
0174         /*
0175          * With group flag FAN_REPORT_NAME, if name was not recorded in
0176          * event on a directory, we will report the name ".".
0177          */
0178         dot_len = 1;
0179     }
0180 
0181     if (info_mode & FAN_REPORT_PIDFD)
0182         event_len += FANOTIFY_PIDFD_INFO_HDR_LEN;
0183 
0184     if (fanotify_event_has_object_fh(event)) {
0185         fh_len = fanotify_event_object_fh_len(event);
0186         event_len += fanotify_fid_info_len(fh_len, dot_len);
0187     }
0188 
0189     return event_len;
0190 }
0191 
0192 /*
0193  * Remove an hashed event from merge hash table.
0194  */
0195 static void fanotify_unhash_event(struct fsnotify_group *group,
0196                   struct fanotify_event *event)
0197 {
0198     assert_spin_locked(&group->notification_lock);
0199 
0200     pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
0201          group, event, fanotify_event_hash_bucket(group, event));
0202 
0203     if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list)))
0204         return;
0205 
0206     hlist_del_init(&event->merge_list);
0207 }
0208 
0209 /*
0210  * Get an fanotify notification event if one exists and is small
0211  * enough to fit in "count". Return an error pointer if the count
0212  * is not large enough. When permission event is dequeued, its state is
0213  * updated accordingly.
0214  */
0215 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
0216                         size_t count)
0217 {
0218     size_t event_size;
0219     struct fanotify_event *event = NULL;
0220     struct fsnotify_event *fsn_event;
0221     unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
0222 
0223     pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
0224 
0225     spin_lock(&group->notification_lock);
0226     fsn_event = fsnotify_peek_first_event(group);
0227     if (!fsn_event)
0228         goto out;
0229 
0230     event = FANOTIFY_E(fsn_event);
0231     event_size = fanotify_event_len(info_mode, event);
0232 
0233     if (event_size > count) {
0234         event = ERR_PTR(-EINVAL);
0235         goto out;
0236     }
0237 
0238     /*
0239      * Held the notification_lock the whole time, so this is the
0240      * same event we peeked above.
0241      */
0242     fsnotify_remove_first_event(group);
0243     if (fanotify_is_perm_event(event->mask))
0244         FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
0245     if (fanotify_is_hashed_event(event->mask))
0246         fanotify_unhash_event(group, event);
0247 out:
0248     spin_unlock(&group->notification_lock);
0249     return event;
0250 }
0251 
0252 static int create_fd(struct fsnotify_group *group, struct path *path,
0253              struct file **file)
0254 {
0255     int client_fd;
0256     struct file *new_file;
0257 
0258     client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
0259     if (client_fd < 0)
0260         return client_fd;
0261 
0262     /*
0263      * we need a new file handle for the userspace program so it can read even if it was
0264      * originally opened O_WRONLY.
0265      */
0266     new_file = dentry_open(path,
0267                    group->fanotify_data.f_flags | __FMODE_NONOTIFY,
0268                    current_cred());
0269     if (IS_ERR(new_file)) {
0270         /*
0271          * we still send an event even if we can't open the file.  this
0272          * can happen when say tasks are gone and we try to open their
0273          * /proc files or we try to open a WRONLY file like in sysfs
0274          * we just send the errno to userspace since there isn't much
0275          * else we can do.
0276          */
0277         put_unused_fd(client_fd);
0278         client_fd = PTR_ERR(new_file);
0279     } else {
0280         *file = new_file;
0281     }
0282 
0283     return client_fd;
0284 }
0285 
0286 /*
0287  * Finish processing of permission event by setting it to ANSWERED state and
0288  * drop group->notification_lock.
0289  */
0290 static void finish_permission_event(struct fsnotify_group *group,
0291                     struct fanotify_perm_event *event,
0292                     unsigned int response)
0293                     __releases(&group->notification_lock)
0294 {
0295     bool destroy = false;
0296 
0297     assert_spin_locked(&group->notification_lock);
0298     event->response = response;
0299     if (event->state == FAN_EVENT_CANCELED)
0300         destroy = true;
0301     else
0302         event->state = FAN_EVENT_ANSWERED;
0303     spin_unlock(&group->notification_lock);
0304     if (destroy)
0305         fsnotify_destroy_event(group, &event->fae.fse);
0306 }
0307 
0308 static int process_access_response(struct fsnotify_group *group,
0309                    struct fanotify_response *response_struct)
0310 {
0311     struct fanotify_perm_event *event;
0312     int fd = response_struct->fd;
0313     int response = response_struct->response;
0314 
0315     pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
0316          fd, response);
0317     /*
0318      * make sure the response is valid, if invalid we do nothing and either
0319      * userspace can send a valid response or we will clean it up after the
0320      * timeout
0321      */
0322     switch (response & ~FAN_AUDIT) {
0323     case FAN_ALLOW:
0324     case FAN_DENY:
0325         break;
0326     default:
0327         return -EINVAL;
0328     }
0329 
0330     if (fd < 0)
0331         return -EINVAL;
0332 
0333     if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
0334         return -EINVAL;
0335 
0336     spin_lock(&group->notification_lock);
0337     list_for_each_entry(event, &group->fanotify_data.access_list,
0338                 fae.fse.list) {
0339         if (event->fd != fd)
0340             continue;
0341 
0342         list_del_init(&event->fae.fse.list);
0343         finish_permission_event(group, event, response);
0344         wake_up(&group->fanotify_data.access_waitq);
0345         return 0;
0346     }
0347     spin_unlock(&group->notification_lock);
0348 
0349     return -ENOENT;
0350 }
0351 
0352 static size_t copy_error_info_to_user(struct fanotify_event *event,
0353                       char __user *buf, int count)
0354 {
0355     struct fanotify_event_info_error info = { };
0356     struct fanotify_error_event *fee = FANOTIFY_EE(event);
0357 
0358     info.hdr.info_type = FAN_EVENT_INFO_TYPE_ERROR;
0359     info.hdr.len = FANOTIFY_ERROR_INFO_LEN;
0360 
0361     if (WARN_ON(count < info.hdr.len))
0362         return -EFAULT;
0363 
0364     info.error = fee->error;
0365     info.error_count = fee->err_count;
0366 
0367     if (copy_to_user(buf, &info, sizeof(info)))
0368         return -EFAULT;
0369 
0370     return info.hdr.len;
0371 }
0372 
0373 static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
0374                  int info_type, const char *name,
0375                  size_t name_len,
0376                  char __user *buf, size_t count)
0377 {
0378     struct fanotify_event_info_fid info = { };
0379     struct file_handle handle = { };
0380     unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
0381     size_t fh_len = fh ? fh->len : 0;
0382     size_t info_len = fanotify_fid_info_len(fh_len, name_len);
0383     size_t len = info_len;
0384 
0385     pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
0386          __func__, fh_len, name_len, info_len, count);
0387 
0388     if (WARN_ON_ONCE(len < sizeof(info) || len > count))
0389         return -EFAULT;
0390 
0391     /*
0392      * Copy event info fid header followed by variable sized file handle
0393      * and optionally followed by variable sized filename.
0394      */
0395     switch (info_type) {
0396     case FAN_EVENT_INFO_TYPE_FID:
0397     case FAN_EVENT_INFO_TYPE_DFID:
0398         if (WARN_ON_ONCE(name_len))
0399             return -EFAULT;
0400         break;
0401     case FAN_EVENT_INFO_TYPE_DFID_NAME:
0402     case FAN_EVENT_INFO_TYPE_OLD_DFID_NAME:
0403     case FAN_EVENT_INFO_TYPE_NEW_DFID_NAME:
0404         if (WARN_ON_ONCE(!name || !name_len))
0405             return -EFAULT;
0406         break;
0407     default:
0408         return -EFAULT;
0409     }
0410 
0411     info.hdr.info_type = info_type;
0412     info.hdr.len = len;
0413     info.fsid = *fsid;
0414     if (copy_to_user(buf, &info, sizeof(info)))
0415         return -EFAULT;
0416 
0417     buf += sizeof(info);
0418     len -= sizeof(info);
0419     if (WARN_ON_ONCE(len < sizeof(handle)))
0420         return -EFAULT;
0421 
0422     handle.handle_type = fh->type;
0423     handle.handle_bytes = fh_len;
0424 
0425     /* Mangle handle_type for bad file_handle */
0426     if (!fh_len)
0427         handle.handle_type = FILEID_INVALID;
0428 
0429     if (copy_to_user(buf, &handle, sizeof(handle)))
0430         return -EFAULT;
0431 
0432     buf += sizeof(handle);
0433     len -= sizeof(handle);
0434     if (WARN_ON_ONCE(len < fh_len))
0435         return -EFAULT;
0436 
0437     /*
0438      * For an inline fh and inline file name, copy through stack to exclude
0439      * the copy from usercopy hardening protections.
0440      */
0441     fh_buf = fanotify_fh_buf(fh);
0442     if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
0443         memcpy(bounce, fh_buf, fh_len);
0444         fh_buf = bounce;
0445     }
0446     if (copy_to_user(buf, fh_buf, fh_len))
0447         return -EFAULT;
0448 
0449     buf += fh_len;
0450     len -= fh_len;
0451 
0452     if (name_len) {
0453         /* Copy the filename with terminating null */
0454         name_len++;
0455         if (WARN_ON_ONCE(len < name_len))
0456             return -EFAULT;
0457 
0458         if (copy_to_user(buf, name, name_len))
0459             return -EFAULT;
0460 
0461         buf += name_len;
0462         len -= name_len;
0463     }
0464 
0465     /* Pad with 0's */
0466     WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
0467     if (len > 0 && clear_user(buf, len))
0468         return -EFAULT;
0469 
0470     return info_len;
0471 }
0472 
0473 static int copy_pidfd_info_to_user(int pidfd,
0474                    char __user *buf,
0475                    size_t count)
0476 {
0477     struct fanotify_event_info_pidfd info = { };
0478     size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN;
0479 
0480     if (WARN_ON_ONCE(info_len > count))
0481         return -EFAULT;
0482 
0483     info.hdr.info_type = FAN_EVENT_INFO_TYPE_PIDFD;
0484     info.hdr.len = info_len;
0485     info.pidfd = pidfd;
0486 
0487     if (copy_to_user(buf, &info, info_len))
0488         return -EFAULT;
0489 
0490     return info_len;
0491 }
0492 
0493 static int copy_info_records_to_user(struct fanotify_event *event,
0494                      struct fanotify_info *info,
0495                      unsigned int info_mode, int pidfd,
0496                      char __user *buf, size_t count)
0497 {
0498     int ret, total_bytes = 0, info_type = 0;
0499     unsigned int fid_mode = info_mode & FANOTIFY_FID_BITS;
0500     unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
0501 
0502     /*
0503      * Event info records order is as follows:
0504      * 1. dir fid + name
0505      * 2. (optional) new dir fid + new name
0506      * 3. (optional) child fid
0507      */
0508     if (fanotify_event_has_dir_fh(event)) {
0509         info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
0510                          FAN_EVENT_INFO_TYPE_DFID;
0511 
0512         /* FAN_RENAME uses special info types */
0513         if (event->mask & FAN_RENAME)
0514             info_type = FAN_EVENT_INFO_TYPE_OLD_DFID_NAME;
0515 
0516         ret = copy_fid_info_to_user(fanotify_event_fsid(event),
0517                         fanotify_info_dir_fh(info),
0518                         info_type,
0519                         fanotify_info_name(info),
0520                         info->name_len, buf, count);
0521         if (ret < 0)
0522             return ret;
0523 
0524         buf += ret;
0525         count -= ret;
0526         total_bytes += ret;
0527     }
0528 
0529     /* New dir fid+name may be reported in addition to old dir fid+name */
0530     if (fanotify_event_has_dir2_fh(event)) {
0531         info_type = FAN_EVENT_INFO_TYPE_NEW_DFID_NAME;
0532         ret = copy_fid_info_to_user(fanotify_event_fsid(event),
0533                         fanotify_info_dir2_fh(info),
0534                         info_type,
0535                         fanotify_info_name2(info),
0536                         info->name2_len, buf, count);
0537         if (ret < 0)
0538             return ret;
0539 
0540         buf += ret;
0541         count -= ret;
0542         total_bytes += ret;
0543     }
0544 
0545     if (fanotify_event_has_object_fh(event)) {
0546         const char *dot = NULL;
0547         int dot_len = 0;
0548 
0549         if (fid_mode == FAN_REPORT_FID || info_type) {
0550             /*
0551              * With only group flag FAN_REPORT_FID only type FID is
0552              * reported. Second info record type is always FID.
0553              */
0554             info_type = FAN_EVENT_INFO_TYPE_FID;
0555         } else if ((fid_mode & FAN_REPORT_NAME) &&
0556                (event->mask & FAN_ONDIR)) {
0557             /*
0558              * With group flag FAN_REPORT_NAME, if name was not
0559              * recorded in an event on a directory, report the name
0560              * "." with info type DFID_NAME.
0561              */
0562             info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
0563             dot = ".";
0564             dot_len = 1;
0565         } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
0566                (event->mask & FAN_ONDIR)) {
0567             /*
0568              * With group flag FAN_REPORT_DIR_FID, a single info
0569              * record has type DFID for directory entry modification
0570              * event and for event on a directory.
0571              */
0572             info_type = FAN_EVENT_INFO_TYPE_DFID;
0573         } else {
0574             /*
0575              * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
0576              * a single info record has type FID for event on a
0577              * non-directory, when there is no directory to report.
0578              * For example, on FAN_DELETE_SELF event.
0579              */
0580             info_type = FAN_EVENT_INFO_TYPE_FID;
0581         }
0582 
0583         ret = copy_fid_info_to_user(fanotify_event_fsid(event),
0584                         fanotify_event_object_fh(event),
0585                         info_type, dot, dot_len,
0586                         buf, count);
0587         if (ret < 0)
0588             return ret;
0589 
0590         buf += ret;
0591         count -= ret;
0592         total_bytes += ret;
0593     }
0594 
0595     if (pidfd_mode) {
0596         ret = copy_pidfd_info_to_user(pidfd, buf, count);
0597         if (ret < 0)
0598             return ret;
0599 
0600         buf += ret;
0601         count -= ret;
0602         total_bytes += ret;
0603     }
0604 
0605     if (fanotify_is_error_event(event->mask)) {
0606         ret = copy_error_info_to_user(event, buf, count);
0607         if (ret < 0)
0608             return ret;
0609         buf += ret;
0610         count -= ret;
0611         total_bytes += ret;
0612     }
0613 
0614     return total_bytes;
0615 }
0616 
0617 static ssize_t copy_event_to_user(struct fsnotify_group *group,
0618                   struct fanotify_event *event,
0619                   char __user *buf, size_t count)
0620 {
0621     struct fanotify_event_metadata metadata;
0622     struct path *path = fanotify_event_path(event);
0623     struct fanotify_info *info = fanotify_event_info(event);
0624     unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
0625     unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
0626     struct file *f = NULL;
0627     int ret, pidfd = FAN_NOPIDFD, fd = FAN_NOFD;
0628 
0629     pr_debug("%s: group=%p event=%p\n", __func__, group, event);
0630 
0631     metadata.event_len = fanotify_event_len(info_mode, event);
0632     metadata.metadata_len = FAN_EVENT_METADATA_LEN;
0633     metadata.vers = FANOTIFY_METADATA_VERSION;
0634     metadata.reserved = 0;
0635     metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
0636     metadata.pid = pid_vnr(event->pid);
0637     /*
0638      * For an unprivileged listener, event->pid can be used to identify the
0639      * events generated by the listener process itself, without disclosing
0640      * the pids of other processes.
0641      */
0642     if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
0643         task_tgid(current) != event->pid)
0644         metadata.pid = 0;
0645 
0646     /*
0647      * For now, fid mode is required for an unprivileged listener and
0648      * fid mode does not report fd in events.  Keep this check anyway
0649      * for safety in case fid mode requirement is relaxed in the future
0650      * to allow unprivileged listener to get events with no fd and no fid.
0651      */
0652     if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
0653         path && path->mnt && path->dentry) {
0654         fd = create_fd(group, path, &f);
0655         if (fd < 0)
0656             return fd;
0657     }
0658     metadata.fd = fd;
0659 
0660     if (pidfd_mode) {
0661         /*
0662          * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual
0663          * exclusion is ever lifted. At the time of incoporating pidfd
0664          * support within fanotify, the pidfd API only supported the
0665          * creation of pidfds for thread-group leaders.
0666          */
0667         WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID));
0668 
0669         /*
0670          * The PIDTYPE_TGID check for an event->pid is performed
0671          * preemptively in an attempt to catch out cases where the event
0672          * listener reads events after the event generating process has
0673          * already terminated. Report FAN_NOPIDFD to the event listener
0674          * in those cases, with all other pidfd creation errors being
0675          * reported as FAN_EPIDFD.
0676          */
0677         if (metadata.pid == 0 ||
0678             !pid_has_task(event->pid, PIDTYPE_TGID)) {
0679             pidfd = FAN_NOPIDFD;
0680         } else {
0681             pidfd = pidfd_create(event->pid, 0);
0682             if (pidfd < 0)
0683                 pidfd = FAN_EPIDFD;
0684         }
0685     }
0686 
0687     ret = -EFAULT;
0688     /*
0689      * Sanity check copy size in case get_one_event() and
0690      * event_len sizes ever get out of sync.
0691      */
0692     if (WARN_ON_ONCE(metadata.event_len > count))
0693         goto out_close_fd;
0694 
0695     if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
0696         goto out_close_fd;
0697 
0698     buf += FAN_EVENT_METADATA_LEN;
0699     count -= FAN_EVENT_METADATA_LEN;
0700 
0701     if (fanotify_is_perm_event(event->mask))
0702         FANOTIFY_PERM(event)->fd = fd;
0703 
0704     if (info_mode) {
0705         ret = copy_info_records_to_user(event, info, info_mode, pidfd,
0706                         buf, count);
0707         if (ret < 0)
0708             goto out_close_fd;
0709     }
0710 
0711     if (f)
0712         fd_install(fd, f);
0713 
0714     return metadata.event_len;
0715 
0716 out_close_fd:
0717     if (fd != FAN_NOFD) {
0718         put_unused_fd(fd);
0719         fput(f);
0720     }
0721 
0722     if (pidfd >= 0)
0723         close_fd(pidfd);
0724 
0725     return ret;
0726 }
0727 
0728 /* intofiy userspace file descriptor functions */
0729 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
0730 {
0731     struct fsnotify_group *group = file->private_data;
0732     __poll_t ret = 0;
0733 
0734     poll_wait(file, &group->notification_waitq, wait);
0735     spin_lock(&group->notification_lock);
0736     if (!fsnotify_notify_queue_is_empty(group))
0737         ret = EPOLLIN | EPOLLRDNORM;
0738     spin_unlock(&group->notification_lock);
0739 
0740     return ret;
0741 }
0742 
0743 static ssize_t fanotify_read(struct file *file, char __user *buf,
0744                  size_t count, loff_t *pos)
0745 {
0746     struct fsnotify_group *group;
0747     struct fanotify_event *event;
0748     char __user *start;
0749     int ret;
0750     DEFINE_WAIT_FUNC(wait, woken_wake_function);
0751 
0752     start = buf;
0753     group = file->private_data;
0754 
0755     pr_debug("%s: group=%p\n", __func__, group);
0756 
0757     add_wait_queue(&group->notification_waitq, &wait);
0758     while (1) {
0759         /*
0760          * User can supply arbitrarily large buffer. Avoid softlockups
0761          * in case there are lots of available events.
0762          */
0763         cond_resched();
0764         event = get_one_event(group, count);
0765         if (IS_ERR(event)) {
0766             ret = PTR_ERR(event);
0767             break;
0768         }
0769 
0770         if (!event) {
0771             ret = -EAGAIN;
0772             if (file->f_flags & O_NONBLOCK)
0773                 break;
0774 
0775             ret = -ERESTARTSYS;
0776             if (signal_pending(current))
0777                 break;
0778 
0779             if (start != buf)
0780                 break;
0781 
0782             wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
0783             continue;
0784         }
0785 
0786         ret = copy_event_to_user(group, event, buf, count);
0787         if (unlikely(ret == -EOPENSTALE)) {
0788             /*
0789              * We cannot report events with stale fd so drop it.
0790              * Setting ret to 0 will continue the event loop and
0791              * do the right thing if there are no more events to
0792              * read (i.e. return bytes read, -EAGAIN or wait).
0793              */
0794             ret = 0;
0795         }
0796 
0797         /*
0798          * Permission events get queued to wait for response.  Other
0799          * events can be destroyed now.
0800          */
0801         if (!fanotify_is_perm_event(event->mask)) {
0802             fsnotify_destroy_event(group, &event->fse);
0803         } else {
0804             if (ret <= 0) {
0805                 spin_lock(&group->notification_lock);
0806                 finish_permission_event(group,
0807                     FANOTIFY_PERM(event), FAN_DENY);
0808                 wake_up(&group->fanotify_data.access_waitq);
0809             } else {
0810                 spin_lock(&group->notification_lock);
0811                 list_add_tail(&event->fse.list,
0812                     &group->fanotify_data.access_list);
0813                 spin_unlock(&group->notification_lock);
0814             }
0815         }
0816         if (ret < 0)
0817             break;
0818         buf += ret;
0819         count -= ret;
0820     }
0821     remove_wait_queue(&group->notification_waitq, &wait);
0822 
0823     if (start != buf && ret != -EFAULT)
0824         ret = buf - start;
0825     return ret;
0826 }
0827 
0828 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
0829 {
0830     struct fanotify_response response = { .fd = -1, .response = -1 };
0831     struct fsnotify_group *group;
0832     int ret;
0833 
0834     if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
0835         return -EINVAL;
0836 
0837     group = file->private_data;
0838 
0839     if (count < sizeof(response))
0840         return -EINVAL;
0841 
0842     count = sizeof(response);
0843 
0844     pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
0845 
0846     if (copy_from_user(&response, buf, count))
0847         return -EFAULT;
0848 
0849     ret = process_access_response(group, &response);
0850     if (ret < 0)
0851         count = ret;
0852 
0853     return count;
0854 }
0855 
0856 static int fanotify_release(struct inode *ignored, struct file *file)
0857 {
0858     struct fsnotify_group *group = file->private_data;
0859     struct fsnotify_event *fsn_event;
0860 
0861     /*
0862      * Stop new events from arriving in the notification queue. since
0863      * userspace cannot use fanotify fd anymore, no event can enter or
0864      * leave access_list by now either.
0865      */
0866     fsnotify_group_stop_queueing(group);
0867 
0868     /*
0869      * Process all permission events on access_list and notification queue
0870      * and simulate reply from userspace.
0871      */
0872     spin_lock(&group->notification_lock);
0873     while (!list_empty(&group->fanotify_data.access_list)) {
0874         struct fanotify_perm_event *event;
0875 
0876         event = list_first_entry(&group->fanotify_data.access_list,
0877                 struct fanotify_perm_event, fae.fse.list);
0878         list_del_init(&event->fae.fse.list);
0879         finish_permission_event(group, event, FAN_ALLOW);
0880         spin_lock(&group->notification_lock);
0881     }
0882 
0883     /*
0884      * Destroy all non-permission events. For permission events just
0885      * dequeue them and set the response. They will be freed once the
0886      * response is consumed and fanotify_get_response() returns.
0887      */
0888     while ((fsn_event = fsnotify_remove_first_event(group))) {
0889         struct fanotify_event *event = FANOTIFY_E(fsn_event);
0890 
0891         if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
0892             spin_unlock(&group->notification_lock);
0893             fsnotify_destroy_event(group, fsn_event);
0894         } else {
0895             finish_permission_event(group, FANOTIFY_PERM(event),
0896                         FAN_ALLOW);
0897         }
0898         spin_lock(&group->notification_lock);
0899     }
0900     spin_unlock(&group->notification_lock);
0901 
0902     /* Response for all permission events it set, wakeup waiters */
0903     wake_up(&group->fanotify_data.access_waitq);
0904 
0905     /* matches the fanotify_init->fsnotify_alloc_group */
0906     fsnotify_destroy_group(group);
0907 
0908     return 0;
0909 }
0910 
0911 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0912 {
0913     struct fsnotify_group *group;
0914     struct fsnotify_event *fsn_event;
0915     void __user *p;
0916     int ret = -ENOTTY;
0917     size_t send_len = 0;
0918 
0919     group = file->private_data;
0920 
0921     p = (void __user *) arg;
0922 
0923     switch (cmd) {
0924     case FIONREAD:
0925         spin_lock(&group->notification_lock);
0926         list_for_each_entry(fsn_event, &group->notification_list, list)
0927             send_len += FAN_EVENT_METADATA_LEN;
0928         spin_unlock(&group->notification_lock);
0929         ret = put_user(send_len, (int __user *) p);
0930         break;
0931     }
0932 
0933     return ret;
0934 }
0935 
0936 static const struct file_operations fanotify_fops = {
0937     .show_fdinfo    = fanotify_show_fdinfo,
0938     .poll       = fanotify_poll,
0939     .read       = fanotify_read,
0940     .write      = fanotify_write,
0941     .fasync     = NULL,
0942     .release    = fanotify_release,
0943     .unlocked_ioctl = fanotify_ioctl,
0944     .compat_ioctl   = compat_ptr_ioctl,
0945     .llseek     = noop_llseek,
0946 };
0947 
0948 static int fanotify_find_path(int dfd, const char __user *filename,
0949                   struct path *path, unsigned int flags, __u64 mask,
0950                   unsigned int obj_type)
0951 {
0952     int ret;
0953 
0954     pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
0955          dfd, filename, flags);
0956 
0957     if (filename == NULL) {
0958         struct fd f = fdget(dfd);
0959 
0960         ret = -EBADF;
0961         if (!f.file)
0962             goto out;
0963 
0964         ret = -ENOTDIR;
0965         if ((flags & FAN_MARK_ONLYDIR) &&
0966             !(S_ISDIR(file_inode(f.file)->i_mode))) {
0967             fdput(f);
0968             goto out;
0969         }
0970 
0971         *path = f.file->f_path;
0972         path_get(path);
0973         fdput(f);
0974     } else {
0975         unsigned int lookup_flags = 0;
0976 
0977         if (!(flags & FAN_MARK_DONT_FOLLOW))
0978             lookup_flags |= LOOKUP_FOLLOW;
0979         if (flags & FAN_MARK_ONLYDIR)
0980             lookup_flags |= LOOKUP_DIRECTORY;
0981 
0982         ret = user_path_at(dfd, filename, lookup_flags, path);
0983         if (ret)
0984             goto out;
0985     }
0986 
0987     /* you can only watch an inode if you have read permissions on it */
0988     ret = path_permission(path, MAY_READ);
0989     if (ret) {
0990         path_put(path);
0991         goto out;
0992     }
0993 
0994     ret = security_path_notify(path, mask, obj_type);
0995     if (ret)
0996         path_put(path);
0997 
0998 out:
0999     return ret;
1000 }
1001 
1002 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
1003                         __u32 mask, unsigned int flags,
1004                         __u32 umask, int *destroy)
1005 {
1006     __u32 oldmask, newmask;
1007 
1008     /* umask bits cannot be removed by user */
1009     mask &= ~umask;
1010     spin_lock(&fsn_mark->lock);
1011     oldmask = fsnotify_calc_mask(fsn_mark);
1012     if (!(flags & FANOTIFY_MARK_IGNORE_BITS)) {
1013         fsn_mark->mask &= ~mask;
1014     } else {
1015         fsn_mark->ignore_mask &= ~mask;
1016     }
1017     newmask = fsnotify_calc_mask(fsn_mark);
1018     /*
1019      * We need to keep the mark around even if remaining mask cannot
1020      * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
1021      * changes to the mask.
1022      * Destroy mark when only umask bits remain.
1023      */
1024     *destroy = !((fsn_mark->mask | fsn_mark->ignore_mask) & ~umask);
1025     spin_unlock(&fsn_mark->lock);
1026 
1027     return oldmask & ~newmask;
1028 }
1029 
1030 static int fanotify_remove_mark(struct fsnotify_group *group,
1031                 fsnotify_connp_t *connp, __u32 mask,
1032                 unsigned int flags, __u32 umask)
1033 {
1034     struct fsnotify_mark *fsn_mark = NULL;
1035     __u32 removed;
1036     int destroy_mark;
1037 
1038     fsnotify_group_lock(group);
1039     fsn_mark = fsnotify_find_mark(connp, group);
1040     if (!fsn_mark) {
1041         fsnotify_group_unlock(group);
1042         return -ENOENT;
1043     }
1044 
1045     removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
1046                          umask, &destroy_mark);
1047     if (removed & fsnotify_conn_mask(fsn_mark->connector))
1048         fsnotify_recalc_mask(fsn_mark->connector);
1049     if (destroy_mark)
1050         fsnotify_detach_mark(fsn_mark);
1051     fsnotify_group_unlock(group);
1052     if (destroy_mark)
1053         fsnotify_free_mark(fsn_mark);
1054 
1055     /* matches the fsnotify_find_mark() */
1056     fsnotify_put_mark(fsn_mark);
1057     return 0;
1058 }
1059 
1060 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
1061                      struct vfsmount *mnt, __u32 mask,
1062                      unsigned int flags, __u32 umask)
1063 {
1064     return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
1065                     mask, flags, umask);
1066 }
1067 
1068 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
1069                    struct super_block *sb, __u32 mask,
1070                    unsigned int flags, __u32 umask)
1071 {
1072     return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
1073                     flags, umask);
1074 }
1075 
1076 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
1077                       struct inode *inode, __u32 mask,
1078                       unsigned int flags, __u32 umask)
1079 {
1080     return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
1081                     flags, umask);
1082 }
1083 
1084 static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
1085                        unsigned int fan_flags)
1086 {
1087     bool want_iref = !(fan_flags & FAN_MARK_EVICTABLE);
1088     unsigned int ignore = fan_flags & FANOTIFY_MARK_IGNORE_BITS;
1089     bool recalc = false;
1090 
1091     /*
1092      * When using FAN_MARK_IGNORE for the first time, mark starts using
1093      * independent event flags in ignore mask.  After that, trying to
1094      * update the ignore mask with the old FAN_MARK_IGNORED_MASK API
1095      * will result in EEXIST error.
1096      */
1097     if (ignore == FAN_MARK_IGNORE)
1098         fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS;
1099 
1100     /*
1101      * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to
1102      * the removal of the FS_MODIFY bit in calculated mask if it was set
1103      * because of an ignore mask that is now going to survive FS_MODIFY.
1104      */
1105     if (ignore && (fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1106         !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) {
1107         fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
1108         if (!(fsn_mark->mask & FS_MODIFY))
1109             recalc = true;
1110     }
1111 
1112     if (fsn_mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE ||
1113         want_iref == !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
1114         return recalc;
1115 
1116     /*
1117      * NO_IREF may be removed from a mark, but not added.
1118      * When removed, fsnotify_recalc_mask() will take the inode ref.
1119      */
1120     WARN_ON_ONCE(!want_iref);
1121     fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF;
1122 
1123     return true;
1124 }
1125 
1126 static bool fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
1127                       __u32 mask, unsigned int fan_flags)
1128 {
1129     bool recalc;
1130 
1131     spin_lock(&fsn_mark->lock);
1132     if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS))
1133         fsn_mark->mask |= mask;
1134     else
1135         fsn_mark->ignore_mask |= mask;
1136 
1137     recalc = fsnotify_calc_mask(fsn_mark) &
1138         ~fsnotify_conn_mask(fsn_mark->connector);
1139 
1140     recalc |= fanotify_mark_update_flags(fsn_mark, fan_flags);
1141     spin_unlock(&fsn_mark->lock);
1142 
1143     return recalc;
1144 }
1145 
1146 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
1147                            fsnotify_connp_t *connp,
1148                            unsigned int obj_type,
1149                            unsigned int fan_flags,
1150                            __kernel_fsid_t *fsid)
1151 {
1152     struct ucounts *ucounts = group->fanotify_data.ucounts;
1153     struct fsnotify_mark *mark;
1154     int ret;
1155 
1156     /*
1157      * Enforce per user marks limits per user in all containing user ns.
1158      * A group with FAN_UNLIMITED_MARKS does not contribute to mark count
1159      * in the limited groups account.
1160      */
1161     if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) &&
1162         !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))
1163         return ERR_PTR(-ENOSPC);
1164 
1165     mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
1166     if (!mark) {
1167         ret = -ENOMEM;
1168         goto out_dec_ucounts;
1169     }
1170 
1171     fsnotify_init_mark(mark, group);
1172     if (fan_flags & FAN_MARK_EVICTABLE)
1173         mark->flags |= FSNOTIFY_MARK_FLAG_NO_IREF;
1174 
1175     ret = fsnotify_add_mark_locked(mark, connp, obj_type, 0, fsid);
1176     if (ret) {
1177         fsnotify_put_mark(mark);
1178         goto out_dec_ucounts;
1179     }
1180 
1181     return mark;
1182 
1183 out_dec_ucounts:
1184     if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1185         dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS);
1186     return ERR_PTR(ret);
1187 }
1188 
1189 static int fanotify_group_init_error_pool(struct fsnotify_group *group)
1190 {
1191     if (mempool_initialized(&group->fanotify_data.error_events_pool))
1192         return 0;
1193 
1194     return mempool_init_kmalloc_pool(&group->fanotify_data.error_events_pool,
1195                      FANOTIFY_DEFAULT_FEE_POOL_SIZE,
1196                      sizeof(struct fanotify_error_event));
1197 }
1198 
1199 static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
1200                           unsigned int fan_flags)
1201 {
1202     /*
1203      * Non evictable mark cannot be downgraded to evictable mark.
1204      */
1205     if (fan_flags & FAN_MARK_EVICTABLE &&
1206         !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
1207         return -EEXIST;
1208 
1209     /*
1210      * New ignore mask semantics cannot be downgraded to old semantics.
1211      */
1212     if (fan_flags & FAN_MARK_IGNORED_MASK &&
1213         fsn_mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
1214         return -EEXIST;
1215 
1216     /*
1217      * An ignore mask that survives modify could never be downgraded to not
1218      * survive modify.  With new FAN_MARK_IGNORE semantics we make that rule
1219      * explicit and return an error when trying to update the ignore mask
1220      * without the original FAN_MARK_IGNORED_SURV_MODIFY value.
1221      */
1222     if (fan_flags & FAN_MARK_IGNORE &&
1223         !(fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1224         fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
1225         return -EEXIST;
1226 
1227     return 0;
1228 }
1229 
1230 static int fanotify_add_mark(struct fsnotify_group *group,
1231                  fsnotify_connp_t *connp, unsigned int obj_type,
1232                  __u32 mask, unsigned int fan_flags,
1233                  __kernel_fsid_t *fsid)
1234 {
1235     struct fsnotify_mark *fsn_mark;
1236     bool recalc;
1237     int ret = 0;
1238 
1239     fsnotify_group_lock(group);
1240     fsn_mark = fsnotify_find_mark(connp, group);
1241     if (!fsn_mark) {
1242         fsn_mark = fanotify_add_new_mark(group, connp, obj_type,
1243                          fan_flags, fsid);
1244         if (IS_ERR(fsn_mark)) {
1245             fsnotify_group_unlock(group);
1246             return PTR_ERR(fsn_mark);
1247         }
1248     }
1249 
1250     /*
1251      * Check if requested mark flags conflict with an existing mark flags.
1252      */
1253     ret = fanotify_may_update_existing_mark(fsn_mark, fan_flags);
1254     if (ret)
1255         goto out;
1256 
1257     /*
1258      * Error events are pre-allocated per group, only if strictly
1259      * needed (i.e. FAN_FS_ERROR was requested).
1260      */
1261     if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS) &&
1262         (mask & FAN_FS_ERROR)) {
1263         ret = fanotify_group_init_error_pool(group);
1264         if (ret)
1265             goto out;
1266     }
1267 
1268     recalc = fanotify_mark_add_to_mask(fsn_mark, mask, fan_flags);
1269     if (recalc)
1270         fsnotify_recalc_mask(fsn_mark->connector);
1271 
1272 out:
1273     fsnotify_group_unlock(group);
1274 
1275     fsnotify_put_mark(fsn_mark);
1276     return ret;
1277 }
1278 
1279 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
1280                       struct vfsmount *mnt, __u32 mask,
1281                       unsigned int flags, __kernel_fsid_t *fsid)
1282 {
1283     return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
1284                  FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
1285 }
1286 
1287 static int fanotify_add_sb_mark(struct fsnotify_group *group,
1288                 struct super_block *sb, __u32 mask,
1289                 unsigned int flags, __kernel_fsid_t *fsid)
1290 {
1291     return fanotify_add_mark(group, &sb->s_fsnotify_marks,
1292                  FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
1293 }
1294 
1295 static int fanotify_add_inode_mark(struct fsnotify_group *group,
1296                    struct inode *inode, __u32 mask,
1297                    unsigned int flags, __kernel_fsid_t *fsid)
1298 {
1299     pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
1300 
1301     /*
1302      * If some other task has this inode open for write we should not add
1303      * an ignore mask, unless that ignore mask is supposed to survive
1304      * modification changes anyway.
1305      */
1306     if ((flags & FANOTIFY_MARK_IGNORE_BITS) &&
1307         !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1308         inode_is_open_for_write(inode))
1309         return 0;
1310 
1311     return fanotify_add_mark(group, &inode->i_fsnotify_marks,
1312                  FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
1313 }
1314 
1315 static struct fsnotify_event *fanotify_alloc_overflow_event(void)
1316 {
1317     struct fanotify_event *oevent;
1318 
1319     oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
1320     if (!oevent)
1321         return NULL;
1322 
1323     fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
1324     oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
1325 
1326     return &oevent->fse;
1327 }
1328 
1329 static struct hlist_head *fanotify_alloc_merge_hash(void)
1330 {
1331     struct hlist_head *hash;
1332 
1333     hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS,
1334                GFP_KERNEL_ACCOUNT);
1335     if (!hash)
1336         return NULL;
1337 
1338     __hash_init(hash, FANOTIFY_HTABLE_SIZE);
1339 
1340     return hash;
1341 }
1342 
1343 /* fanotify syscalls */
1344 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
1345 {
1346     struct fsnotify_group *group;
1347     int f_flags, fd;
1348     unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
1349     unsigned int class = flags & FANOTIFY_CLASS_BITS;
1350     unsigned int internal_flags = 0;
1351 
1352     pr_debug("%s: flags=%x event_f_flags=%x\n",
1353          __func__, flags, event_f_flags);
1354 
1355     if (!capable(CAP_SYS_ADMIN)) {
1356         /*
1357          * An unprivileged user can setup an fanotify group with
1358          * limited functionality - an unprivileged group is limited to
1359          * notification events with file handles and it cannot use
1360          * unlimited queue/marks.
1361          */
1362         if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode)
1363             return -EPERM;
1364 
1365         /*
1366          * Setting the internal flag FANOTIFY_UNPRIV on the group
1367          * prevents setting mount/filesystem marks on this group and
1368          * prevents reporting pid and open fd in events.
1369          */
1370         internal_flags |= FANOTIFY_UNPRIV;
1371     }
1372 
1373 #ifdef CONFIG_AUDITSYSCALL
1374     if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
1375 #else
1376     if (flags & ~FANOTIFY_INIT_FLAGS)
1377 #endif
1378         return -EINVAL;
1379 
1380     /*
1381      * A pidfd can only be returned for a thread-group leader; thus
1382      * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually
1383      * exclusive.
1384      */
1385     if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID))
1386         return -EINVAL;
1387 
1388     if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
1389         return -EINVAL;
1390 
1391     switch (event_f_flags & O_ACCMODE) {
1392     case O_RDONLY:
1393     case O_RDWR:
1394     case O_WRONLY:
1395         break;
1396     default:
1397         return -EINVAL;
1398     }
1399 
1400     if (fid_mode && class != FAN_CLASS_NOTIF)
1401         return -EINVAL;
1402 
1403     /*
1404      * Child name is reported with parent fid so requires dir fid.
1405      * We can report both child fid and dir fid with or without name.
1406      */
1407     if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
1408         return -EINVAL;
1409 
1410     /*
1411      * FAN_REPORT_TARGET_FID requires FAN_REPORT_NAME and FAN_REPORT_FID
1412      * and is used as an indication to report both dir and child fid on all
1413      * dirent events.
1414      */
1415     if ((fid_mode & FAN_REPORT_TARGET_FID) &&
1416         (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
1417         return -EINVAL;
1418 
1419     f_flags = O_RDWR | __FMODE_NONOTIFY;
1420     if (flags & FAN_CLOEXEC)
1421         f_flags |= O_CLOEXEC;
1422     if (flags & FAN_NONBLOCK)
1423         f_flags |= O_NONBLOCK;
1424 
1425     /* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
1426     group = fsnotify_alloc_group(&fanotify_fsnotify_ops,
1427                      FSNOTIFY_GROUP_USER | FSNOTIFY_GROUP_NOFS);
1428     if (IS_ERR(group)) {
1429         return PTR_ERR(group);
1430     }
1431 
1432     /* Enforce groups limits per user in all containing user ns */
1433     group->fanotify_data.ucounts = inc_ucount(current_user_ns(),
1434                           current_euid(),
1435                           UCOUNT_FANOTIFY_GROUPS);
1436     if (!group->fanotify_data.ucounts) {
1437         fd = -EMFILE;
1438         goto out_destroy_group;
1439     }
1440 
1441     group->fanotify_data.flags = flags | internal_flags;
1442     group->memcg = get_mem_cgroup_from_mm(current->mm);
1443 
1444     group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
1445     if (!group->fanotify_data.merge_hash) {
1446         fd = -ENOMEM;
1447         goto out_destroy_group;
1448     }
1449 
1450     group->overflow_event = fanotify_alloc_overflow_event();
1451     if (unlikely(!group->overflow_event)) {
1452         fd = -ENOMEM;
1453         goto out_destroy_group;
1454     }
1455 
1456     if (force_o_largefile())
1457         event_f_flags |= O_LARGEFILE;
1458     group->fanotify_data.f_flags = event_f_flags;
1459     init_waitqueue_head(&group->fanotify_data.access_waitq);
1460     INIT_LIST_HEAD(&group->fanotify_data.access_list);
1461     switch (class) {
1462     case FAN_CLASS_NOTIF:
1463         group->priority = FS_PRIO_0;
1464         break;
1465     case FAN_CLASS_CONTENT:
1466         group->priority = FS_PRIO_1;
1467         break;
1468     case FAN_CLASS_PRE_CONTENT:
1469         group->priority = FS_PRIO_2;
1470         break;
1471     default:
1472         fd = -EINVAL;
1473         goto out_destroy_group;
1474     }
1475 
1476     if (flags & FAN_UNLIMITED_QUEUE) {
1477         fd = -EPERM;
1478         if (!capable(CAP_SYS_ADMIN))
1479             goto out_destroy_group;
1480         group->max_events = UINT_MAX;
1481     } else {
1482         group->max_events = fanotify_max_queued_events;
1483     }
1484 
1485     if (flags & FAN_UNLIMITED_MARKS) {
1486         fd = -EPERM;
1487         if (!capable(CAP_SYS_ADMIN))
1488             goto out_destroy_group;
1489     }
1490 
1491     if (flags & FAN_ENABLE_AUDIT) {
1492         fd = -EPERM;
1493         if (!capable(CAP_AUDIT_WRITE))
1494             goto out_destroy_group;
1495     }
1496 
1497     fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1498     if (fd < 0)
1499         goto out_destroy_group;
1500 
1501     return fd;
1502 
1503 out_destroy_group:
1504     fsnotify_destroy_group(group);
1505     return fd;
1506 }
1507 
1508 static int fanotify_test_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
1509 {
1510     __kernel_fsid_t root_fsid;
1511     int err;
1512 
1513     /*
1514      * Make sure dentry is not of a filesystem with zero fsid (e.g. fuse).
1515      */
1516     err = vfs_get_fsid(dentry, fsid);
1517     if (err)
1518         return err;
1519 
1520     if (!fsid->val[0] && !fsid->val[1])
1521         return -ENODEV;
1522 
1523     /*
1524      * Make sure dentry is not of a filesystem subvolume (e.g. btrfs)
1525      * which uses a different fsid than sb root.
1526      */
1527     err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid);
1528     if (err)
1529         return err;
1530 
1531     if (root_fsid.val[0] != fsid->val[0] ||
1532         root_fsid.val[1] != fsid->val[1])
1533         return -EXDEV;
1534 
1535     return 0;
1536 }
1537 
1538 /* Check if filesystem can encode a unique fid */
1539 static int fanotify_test_fid(struct dentry *dentry)
1540 {
1541     /*
1542      * We need to make sure that the file system supports at least
1543      * encoding a file handle so user can use name_to_handle_at() to
1544      * compare fid returned with event to the file handle of watched
1545      * objects. However, name_to_handle_at() requires that the
1546      * filesystem also supports decoding file handles.
1547      */
1548     if (!dentry->d_sb->s_export_op ||
1549         !dentry->d_sb->s_export_op->fh_to_dentry)
1550         return -EOPNOTSUPP;
1551 
1552     return 0;
1553 }
1554 
1555 static int fanotify_events_supported(struct fsnotify_group *group,
1556                      struct path *path, __u64 mask,
1557                      unsigned int flags)
1558 {
1559     unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1560     /* Strict validation of events in non-dir inode mask with v5.17+ APIs */
1561     bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) ||
1562                  (mask & FAN_RENAME) ||
1563                  (flags & FAN_MARK_IGNORE);
1564 
1565     /*
1566      * Some filesystems such as 'proc' acquire unusual locks when opening
1567      * files. For them fanotify permission events have high chances of
1568      * deadlocking the system - open done when reporting fanotify event
1569      * blocks on this "unusual" lock while another process holding the lock
1570      * waits for fanotify permission event to be answered. Just disallow
1571      * permission events for such filesystems.
1572      */
1573     if (mask & FANOTIFY_PERM_EVENTS &&
1574         path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1575         return -EINVAL;
1576 
1577     /*
1578      * We shouldn't have allowed setting dirent events and the directory
1579      * flags FAN_ONDIR and FAN_EVENT_ON_CHILD in mask of non-dir inode,
1580      * but because we always allowed it, error only when using new APIs.
1581      */
1582     if (strict_dir_events && mark_type == FAN_MARK_INODE &&
1583         !d_is_dir(path->dentry) && (mask & FANOTIFY_DIRONLY_EVENT_BITS))
1584         return -ENOTDIR;
1585 
1586     return 0;
1587 }
1588 
1589 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1590                 int dfd, const char  __user *pathname)
1591 {
1592     struct inode *inode = NULL;
1593     struct vfsmount *mnt = NULL;
1594     struct fsnotify_group *group;
1595     struct fd f;
1596     struct path path;
1597     __kernel_fsid_t __fsid, *fsid = NULL;
1598     u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1599     unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1600     unsigned int mark_cmd = flags & FANOTIFY_MARK_CMD_BITS;
1601     unsigned int ignore = flags & FANOTIFY_MARK_IGNORE_BITS;
1602     unsigned int obj_type, fid_mode;
1603     u32 umask = 0;
1604     int ret;
1605 
1606     pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1607          __func__, fanotify_fd, flags, dfd, pathname, mask);
1608 
1609     /* we only use the lower 32 bits as of right now. */
1610     if (upper_32_bits(mask))
1611         return -EINVAL;
1612 
1613     if (flags & ~FANOTIFY_MARK_FLAGS)
1614         return -EINVAL;
1615 
1616     switch (mark_type) {
1617     case FAN_MARK_INODE:
1618         obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1619         break;
1620     case FAN_MARK_MOUNT:
1621         obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1622         break;
1623     case FAN_MARK_FILESYSTEM:
1624         obj_type = FSNOTIFY_OBJ_TYPE_SB;
1625         break;
1626     default:
1627         return -EINVAL;
1628     }
1629 
1630     switch (mark_cmd) {
1631     case FAN_MARK_ADD:
1632     case FAN_MARK_REMOVE:
1633         if (!mask)
1634             return -EINVAL;
1635         break;
1636     case FAN_MARK_FLUSH:
1637         if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1638             return -EINVAL;
1639         break;
1640     default:
1641         return -EINVAL;
1642     }
1643 
1644     if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1645         valid_mask |= FANOTIFY_PERM_EVENTS;
1646 
1647     if (mask & ~valid_mask)
1648         return -EINVAL;
1649 
1650 
1651     /* We don't allow FAN_MARK_IGNORE & FAN_MARK_IGNORED_MASK together */
1652     if (ignore == (FAN_MARK_IGNORE | FAN_MARK_IGNORED_MASK))
1653         return -EINVAL;
1654 
1655     /*
1656      * Event flags (FAN_ONDIR, FAN_EVENT_ON_CHILD) have no effect with
1657      * FAN_MARK_IGNORED_MASK.
1658      */
1659     if (ignore == FAN_MARK_IGNORED_MASK) {
1660         mask &= ~FANOTIFY_EVENT_FLAGS;
1661         umask = FANOTIFY_EVENT_FLAGS;
1662     }
1663 
1664     f = fdget(fanotify_fd);
1665     if (unlikely(!f.file))
1666         return -EBADF;
1667 
1668     /* verify that this is indeed an fanotify instance */
1669     ret = -EINVAL;
1670     if (unlikely(f.file->f_op != &fanotify_fops))
1671         goto fput_and_out;
1672     group = f.file->private_data;
1673 
1674     /*
1675      * An unprivileged user is not allowed to setup mount nor filesystem
1676      * marks.  This also includes setting up such marks by a group that
1677      * was initialized by an unprivileged user.
1678      */
1679     ret = -EPERM;
1680     if ((!capable(CAP_SYS_ADMIN) ||
1681          FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) &&
1682         mark_type != FAN_MARK_INODE)
1683         goto fput_and_out;
1684 
1685     /*
1686      * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF.  These are not
1687      * allowed to set permissions events.
1688      */
1689     ret = -EINVAL;
1690     if (mask & FANOTIFY_PERM_EVENTS &&
1691         group->priority == FS_PRIO_0)
1692         goto fput_and_out;
1693 
1694     if (mask & FAN_FS_ERROR &&
1695         mark_type != FAN_MARK_FILESYSTEM)
1696         goto fput_and_out;
1697 
1698     /*
1699      * Evictable is only relevant for inode marks, because only inode object
1700      * can be evicted on memory pressure.
1701      */
1702     if (flags & FAN_MARK_EVICTABLE &&
1703          mark_type != FAN_MARK_INODE)
1704         goto fput_and_out;
1705 
1706     /*
1707      * Events that do not carry enough information to report
1708      * event->fd require a group that supports reporting fid.  Those
1709      * events are not supported on a mount mark, because they do not
1710      * carry enough information (i.e. path) to be filtered by mount
1711      * point.
1712      */
1713     fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
1714     if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_EVENT_FLAGS) &&
1715         (!fid_mode || mark_type == FAN_MARK_MOUNT))
1716         goto fput_and_out;
1717 
1718     /*
1719      * FAN_RENAME uses special info type records to report the old and
1720      * new parent+name.  Reporting only old and new parent id is less
1721      * useful and was not implemented.
1722      */
1723     if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME))
1724         goto fput_and_out;
1725 
1726     if (mark_cmd == FAN_MARK_FLUSH) {
1727         ret = 0;
1728         if (mark_type == FAN_MARK_MOUNT)
1729             fsnotify_clear_vfsmount_marks_by_group(group);
1730         else if (mark_type == FAN_MARK_FILESYSTEM)
1731             fsnotify_clear_sb_marks_by_group(group);
1732         else
1733             fsnotify_clear_inode_marks_by_group(group);
1734         goto fput_and_out;
1735     }
1736 
1737     ret = fanotify_find_path(dfd, pathname, &path, flags,
1738             (mask & ALL_FSNOTIFY_EVENTS), obj_type);
1739     if (ret)
1740         goto fput_and_out;
1741 
1742     if (mark_cmd == FAN_MARK_ADD) {
1743         ret = fanotify_events_supported(group, &path, mask, flags);
1744         if (ret)
1745             goto path_put_and_out;
1746     }
1747 
1748     if (fid_mode) {
1749         ret = fanotify_test_fsid(path.dentry, &__fsid);
1750         if (ret)
1751             goto path_put_and_out;
1752 
1753         ret = fanotify_test_fid(path.dentry);
1754         if (ret)
1755             goto path_put_and_out;
1756 
1757         fsid = &__fsid;
1758     }
1759 
1760     /* inode held in place by reference to path; group by fget on fd */
1761     if (mark_type == FAN_MARK_INODE)
1762         inode = path.dentry->d_inode;
1763     else
1764         mnt = path.mnt;
1765 
1766     ret = mnt ? -EINVAL : -EISDIR;
1767     /* FAN_MARK_IGNORE requires SURV_MODIFY for sb/mount/dir marks */
1768     if (mark_cmd == FAN_MARK_ADD && ignore == FAN_MARK_IGNORE &&
1769         (mnt || S_ISDIR(inode->i_mode)) &&
1770         !(flags & FAN_MARK_IGNORED_SURV_MODIFY))
1771         goto path_put_and_out;
1772 
1773     /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1774     if (mnt || !S_ISDIR(inode->i_mode)) {
1775         mask &= ~FAN_EVENT_ON_CHILD;
1776         umask = FAN_EVENT_ON_CHILD;
1777         /*
1778          * If group needs to report parent fid, register for getting
1779          * events with parent/name info for non-directory.
1780          */
1781         if ((fid_mode & FAN_REPORT_DIR_FID) &&
1782             (flags & FAN_MARK_ADD) && !ignore)
1783             mask |= FAN_EVENT_ON_CHILD;
1784     }
1785 
1786     /* create/update an inode mark */
1787     switch (mark_cmd) {
1788     case FAN_MARK_ADD:
1789         if (mark_type == FAN_MARK_MOUNT)
1790             ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1791                              flags, fsid);
1792         else if (mark_type == FAN_MARK_FILESYSTEM)
1793             ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1794                            flags, fsid);
1795         else
1796             ret = fanotify_add_inode_mark(group, inode, mask,
1797                               flags, fsid);
1798         break;
1799     case FAN_MARK_REMOVE:
1800         if (mark_type == FAN_MARK_MOUNT)
1801             ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1802                                 flags, umask);
1803         else if (mark_type == FAN_MARK_FILESYSTEM)
1804             ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1805                               flags, umask);
1806         else
1807             ret = fanotify_remove_inode_mark(group, inode, mask,
1808                              flags, umask);
1809         break;
1810     default:
1811         ret = -EINVAL;
1812     }
1813 
1814 path_put_and_out:
1815     path_put(&path);
1816 fput_and_out:
1817     fdput(f);
1818     return ret;
1819 }
1820 
1821 #ifndef CONFIG_ARCH_SPLIT_ARG64
1822 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1823                   __u64, mask, int, dfd,
1824                   const char  __user *, pathname)
1825 {
1826     return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1827 }
1828 #endif
1829 
1830 #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1831 SYSCALL32_DEFINE6(fanotify_mark,
1832                 int, fanotify_fd, unsigned int, flags,
1833                 SC_ARG64(mask), int, dfd,
1834                 const char  __user *, pathname)
1835 {
1836     return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1837                 dfd, pathname);
1838 }
1839 #endif
1840 
1841 /*
1842  * fanotify_user_setup - Our initialization function.  Note that we cannot return
1843  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
1844  * must result in panic().
1845  */
1846 static int __init fanotify_user_setup(void)
1847 {
1848     struct sysinfo si;
1849     int max_marks;
1850 
1851     si_meminfo(&si);
1852     /*
1853      * Allow up to 1% of addressable memory to be accounted for per user
1854      * marks limited to the range [8192, 1048576]. mount and sb marks are
1855      * a lot cheaper than inode marks, but there is no reason for a user
1856      * to have many of those, so calculate by the cost of inode marks.
1857      */
1858     max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
1859             INODE_MARK_COST;
1860     max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS,
1861                      FANOTIFY_DEFAULT_MAX_USER_MARKS);
1862 
1863     BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS);
1864     BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 12);
1865     BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 11);
1866 
1867     fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1868                      SLAB_PANIC|SLAB_ACCOUNT);
1869     fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1870                            SLAB_PANIC);
1871     fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1872                         SLAB_PANIC);
1873     if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1874         fanotify_perm_event_cachep =
1875             KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1876     }
1877 
1878     fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1879     init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
1880                     FANOTIFY_DEFAULT_MAX_GROUPS;
1881     init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks;
1882     fanotify_sysctls_init();
1883 
1884     return 0;
1885 }
1886 device_initcall(fanotify_user_setup);