1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 233d3dfffSAndreas Gruenbacher #include <linux/fanotify.h> 3ff0b16a9SEric Paris #include <linux/fdtable.h> 4ff0b16a9SEric Paris #include <linux/fsnotify_backend.h> 5ff0b16a9SEric Paris #include <linux/init.h> 69e66e423SEric Paris #include <linux/jiffies.h> 7ff0b16a9SEric Paris #include <linux/kernel.h> /* UINT_MAX */ 81c529063SEric Paris #include <linux/mount.h> 99e66e423SEric Paris #include <linux/sched.h> 105b825c3aSIngo Molnar #include <linux/sched/user.h> 117a36094dSEric W. Biederman #include <linux/sched/signal.h> 12ff0b16a9SEric Paris #include <linux/types.h> 139e66e423SEric Paris #include <linux/wait.h> 14de8cd83eSSteve Grubb #include <linux/audit.h> 15d46eb14bSShakeel Butt #include <linux/sched/mm.h> 16e9e0c890SAmir Goldstein #include <linux/statfs.h> 17ff0b16a9SEric Paris 187053aee2SJan Kara #include "fanotify.h" 19767cd46cSEric Paris 207053aee2SJan Kara static bool should_merge(struct fsnotify_event *old_fsn, 217053aee2SJan Kara struct fsnotify_event *new_fsn) 227053aee2SJan Kara { 2333913997SAmir Goldstein struct fanotify_event *old, *new; 247053aee2SJan Kara 257053aee2SJan Kara pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); 267053aee2SJan Kara old = FANOTIFY_E(old_fsn); 277053aee2SJan Kara new = FANOTIFY_E(new_fsn); 287053aee2SJan Kara 29e9e0c890SAmir Goldstein if (old_fsn->inode != new_fsn->inode || old->pid != new->pid || 30e9e0c890SAmir Goldstein old->fh_type != new->fh_type || old->fh_len != new->fh_len) 31e9e0c890SAmir Goldstein return false; 32e9e0c890SAmir Goldstein 33e9e0c890SAmir Goldstein if (fanotify_event_has_path(old)) { 34e9e0c890SAmir Goldstein return old->path.mnt == new->path.mnt && 35e9e0c890SAmir Goldstein old->path.dentry == new->path.dentry; 36e9e0c890SAmir Goldstein } else if (fanotify_event_has_fid(old)) { 37e7fce6d9SAmir Goldstein /* 38e7fce6d9SAmir Goldstein * We want to merge many dirent events in the same dir (i.e. 39e7fce6d9SAmir Goldstein * creates/unlinks/renames), but we do not want to merge dirent 40e7fce6d9SAmir Goldstein * events referring to subdirs with dirent events referring to 41e7fce6d9SAmir Goldstein * non subdirs, otherwise, user won't be able to tell from a 42e7fce6d9SAmir Goldstein * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+ 43e7fce6d9SAmir Goldstein * unlink pair or rmdir+create pair of events. 44e7fce6d9SAmir Goldstein */ 45e7fce6d9SAmir Goldstein return (old->mask & FS_ISDIR) == (new->mask & FS_ISDIR) && 46e7fce6d9SAmir Goldstein fanotify_fid_equal(&old->fid, &new->fid, old->fh_len); 47e9e0c890SAmir Goldstein } 48e9e0c890SAmir Goldstein 49e9e0c890SAmir Goldstein /* Do not merge events if we failed to encode fid */ 50767cd46cSEric Paris return false; 51767cd46cSEric Paris } 52767cd46cSEric Paris 53f70ab54cSEric Paris /* and the list better be locked by something too! */ 5483c0e1b4SJan Kara static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) 55767cd46cSEric Paris { 567053aee2SJan Kara struct fsnotify_event *test_event; 5733913997SAmir Goldstein struct fanotify_event *new; 58767cd46cSEric Paris 59767cd46cSEric Paris pr_debug("%s: list=%p event=%p\n", __func__, list, event); 60a0a92d26SAmir Goldstein new = FANOTIFY_E(event); 61767cd46cSEric Paris 6213116dfdSJan Kara /* 6313116dfdSJan Kara * Don't merge a permission event with any other event so that we know 6413116dfdSJan Kara * the event structure we have created in fanotify_handle_event() is the 6513116dfdSJan Kara * one we should check for permission response. 6613116dfdSJan Kara */ 67a0a92d26SAmir Goldstein if (fanotify_is_perm_event(new->mask)) 6883c0e1b4SJan Kara return 0; 6913116dfdSJan Kara 707053aee2SJan Kara list_for_each_entry_reverse(test_event, list, list) { 717053aee2SJan Kara if (should_merge(test_event, event)) { 72a0a92d26SAmir Goldstein FANOTIFY_E(test_event)->mask |= new->mask; 7383c0e1b4SJan Kara return 1; 749dced01aSEric Paris } 756c71100dSKinglong Mee } 766c71100dSKinglong Mee 776c71100dSKinglong Mee return 0; 786c71100dSKinglong Mee } 799dced01aSEric Paris 80fabf7f29SJan Kara /* 81fabf7f29SJan Kara * Wait for response to permission event. The function also takes care of 82fabf7f29SJan Kara * freeing the permission event (or offloads that in case the wait is canceled 83fabf7f29SJan Kara * by a signal). The function returns 0 in case access got allowed by userspace, 84fabf7f29SJan Kara * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case 85fabf7f29SJan Kara * the wait got interrupted by a signal. 86fabf7f29SJan Kara */ 87f083441bSJan Kara static int fanotify_get_response(struct fsnotify_group *group, 8833913997SAmir Goldstein struct fanotify_perm_event *event, 8905f0e387SJan Kara struct fsnotify_iter_info *iter_info) 909e66e423SEric Paris { 919e66e423SEric Paris int ret; 929e66e423SEric Paris 939e66e423SEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 949e66e423SEric Paris 95b5190579SJan Kara ret = wait_event_killable(group->fanotify_data.access_waitq, 9640873284SJan Kara event->state == FAN_EVENT_ANSWERED); 97fabf7f29SJan Kara /* Signal pending? */ 98fabf7f29SJan Kara if (ret < 0) { 99fabf7f29SJan Kara spin_lock(&group->notification_lock); 100fabf7f29SJan Kara /* Event reported to userspace and no answer yet? */ 101fabf7f29SJan Kara if (event->state == FAN_EVENT_REPORTED) { 102fabf7f29SJan Kara /* Event will get freed once userspace answers to it */ 103fabf7f29SJan Kara event->state = FAN_EVENT_CANCELED; 104fabf7f29SJan Kara spin_unlock(&group->notification_lock); 105fabf7f29SJan Kara return ret; 106fabf7f29SJan Kara } 107fabf7f29SJan Kara /* Event not yet reported? Just remove it. */ 108fabf7f29SJan Kara if (event->state == FAN_EVENT_INIT) 109fabf7f29SJan Kara fsnotify_remove_queued_event(group, &event->fae.fse); 110fabf7f29SJan Kara /* 111fabf7f29SJan Kara * Event may be also answered in case signal delivery raced 112fabf7f29SJan Kara * with wakeup. In that case we have nothing to do besides 113fabf7f29SJan Kara * freeing the event and reporting error. 114fabf7f29SJan Kara */ 115fabf7f29SJan Kara spin_unlock(&group->notification_lock); 116fabf7f29SJan Kara goto out; 117fabf7f29SJan Kara } 1189e66e423SEric Paris 1199e66e423SEric Paris /* userspace responded, convert to something usable */ 120de8cd83eSSteve Grubb switch (event->response & ~FAN_AUDIT) { 1219e66e423SEric Paris case FAN_ALLOW: 1229e66e423SEric Paris ret = 0; 1239e66e423SEric Paris break; 1249e66e423SEric Paris case FAN_DENY: 1259e66e423SEric Paris default: 1269e66e423SEric Paris ret = -EPERM; 1279e66e423SEric Paris } 128de8cd83eSSteve Grubb 129de8cd83eSSteve Grubb /* Check if the response should be audited */ 130de8cd83eSSteve Grubb if (event->response & FAN_AUDIT) 131de8cd83eSSteve Grubb audit_fanotify(event->response & ~FAN_AUDIT); 132de8cd83eSSteve Grubb 133b2d87909SEric Paris pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, 134b2d87909SEric Paris group, event, ret); 135fabf7f29SJan Kara out: 136fabf7f29SJan Kara fsnotify_destroy_event(group, &event->fae.fse); 137b2d87909SEric Paris 1389e66e423SEric Paris return ret; 1399e66e423SEric Paris } 1409e66e423SEric Paris 1412d10b230SMatthew Bobrowski /* 1422d10b230SMatthew Bobrowski * This function returns a mask for an event that only contains the flags 1432d10b230SMatthew Bobrowski * that have been specifically requested by the user. Flags that may have 1442d10b230SMatthew Bobrowski * been included within the event mask, but have not been explicitly 1452d10b230SMatthew Bobrowski * requested by the user, will not be present in the returned mask. 1462d10b230SMatthew Bobrowski */ 14783b535d2SAmir Goldstein static u32 fanotify_group_event_mask(struct fsnotify_group *group, 14883b535d2SAmir Goldstein struct fsnotify_iter_info *iter_info, 1495b0457adSAmir Goldstein u32 event_mask, const void *data, 1505b0457adSAmir Goldstein int data_type) 1511c529063SEric Paris { 15254a307baSAmir Goldstein __u32 marks_mask = 0, marks_ignored_mask = 0; 153e7fce6d9SAmir Goldstein __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS; 1543cd5eca8SAl Viro const struct path *path = data; 155837a3934SAmir Goldstein struct fsnotify_mark *mark; 156837a3934SAmir Goldstein int type; 1571968f5eeSEric Paris 158837a3934SAmir Goldstein pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", 159837a3934SAmir Goldstein __func__, iter_info->report_mask, event_mask, data, data_type); 1601968f5eeSEric Paris 16183b535d2SAmir Goldstein if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { 16283b535d2SAmir Goldstein /* Do we have path to open a file descriptor? */ 1632069601bSLinus Torvalds if (data_type != FSNOTIFY_EVENT_PATH) 1642d10b230SMatthew Bobrowski return 0; 16583b535d2SAmir Goldstein /* Path type events are only relevant for files and dirs */ 16683b535d2SAmir Goldstein if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry)) 1672d10b230SMatthew Bobrowski return 0; 16883b535d2SAmir Goldstein } 169e1c048baSEric Paris 170837a3934SAmir Goldstein fsnotify_foreach_obj_type(type) { 171837a3934SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 172837a3934SAmir Goldstein continue; 173837a3934SAmir Goldstein mark = iter_info->marks[type]; 1741968f5eeSEric Paris /* 175b469e7e4SAmir Goldstein * If the event is for a child and this mark doesn't care about 176b469e7e4SAmir Goldstein * events on a child, don't send it! 1771968f5eeSEric Paris */ 178b469e7e4SAmir Goldstein if (event_mask & FS_EVENT_ON_CHILD && 179b469e7e4SAmir Goldstein (type != FSNOTIFY_OBJ_TYPE_INODE || 180b469e7e4SAmir Goldstein !(mark->mask & FS_EVENT_ON_CHILD))) 181837a3934SAmir Goldstein continue; 18254a307baSAmir Goldstein 183837a3934SAmir Goldstein marks_mask |= mark->mask; 184837a3934SAmir Goldstein marks_ignored_mask |= mark->ignored_mask; 1851968f5eeSEric Paris } 1861968f5eeSEric Paris 187e7fce6d9SAmir Goldstein test_mask = event_mask & marks_mask & ~marks_ignored_mask; 188e7fce6d9SAmir Goldstein 189e7fce6d9SAmir Goldstein /* 190e7fce6d9SAmir Goldstein * dirent modification events (create/delete/move) do not carry the 191e7fce6d9SAmir Goldstein * child entry name/inode information. Instead, we report FAN_ONDIR 192e7fce6d9SAmir Goldstein * for mkdir/rmdir so user can differentiate them from creat/unlink. 193e7fce6d9SAmir Goldstein * 194e7fce6d9SAmir Goldstein * For backward compatibility and consistency, do not report FAN_ONDIR 195e7fce6d9SAmir Goldstein * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR 196e7fce6d9SAmir Goldstein * to user in FAN_REPORT_FID mode for all event types. 197e7fce6d9SAmir Goldstein */ 198e7fce6d9SAmir Goldstein if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { 199e7fce6d9SAmir Goldstein /* Do not report FAN_ONDIR without any event */ 200e7fce6d9SAmir Goldstein if (!(test_mask & ~FAN_ONDIR)) 201e7fce6d9SAmir Goldstein return 0; 202e7fce6d9SAmir Goldstein } else { 203e7fce6d9SAmir Goldstein user_mask &= ~FAN_ONDIR; 204e7fce6d9SAmir Goldstein } 205e7fce6d9SAmir Goldstein 2060321e03cSAmir Goldstein if (event_mask & FS_ISDIR && 20766ba93c0SLino Sanfilippo !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) 2082d10b230SMatthew Bobrowski return 0; 2098fcd6528SEric Paris 210e7fce6d9SAmir Goldstein return test_mask & user_mask; 2111c529063SEric Paris } 2121c529063SEric Paris 213e9e0c890SAmir Goldstein static int fanotify_encode_fid(struct fanotify_event *event, 21477115225SAmir Goldstein struct inode *inode, gfp_t gfp, 21577115225SAmir Goldstein __kernel_fsid_t *fsid) 216e9e0c890SAmir Goldstein { 217e9e0c890SAmir Goldstein struct fanotify_fid *fid = &event->fid; 218e9e0c890SAmir Goldstein int dwords, bytes = 0; 219e9e0c890SAmir Goldstein int err, type; 220e9e0c890SAmir Goldstein 221e9e0c890SAmir Goldstein fid->ext_fh = NULL; 222e9e0c890SAmir Goldstein dwords = 0; 223e9e0c890SAmir Goldstein err = -ENOENT; 22477115225SAmir Goldstein type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL); 225e9e0c890SAmir Goldstein if (!dwords) 226e9e0c890SAmir Goldstein goto out_err; 227e9e0c890SAmir Goldstein 228e9e0c890SAmir Goldstein bytes = dwords << 2; 229e9e0c890SAmir Goldstein if (bytes > FANOTIFY_INLINE_FH_LEN) { 230e9e0c890SAmir Goldstein /* Treat failure to allocate fh as failure to allocate event */ 231e9e0c890SAmir Goldstein err = -ENOMEM; 232e9e0c890SAmir Goldstein fid->ext_fh = kmalloc(bytes, gfp); 233e9e0c890SAmir Goldstein if (!fid->ext_fh) 234e9e0c890SAmir Goldstein goto out_err; 235e9e0c890SAmir Goldstein } 236e9e0c890SAmir Goldstein 23777115225SAmir Goldstein type = exportfs_encode_inode_fh(inode, fanotify_fid_fh(fid, bytes), 23877115225SAmir Goldstein &dwords, NULL); 239e9e0c890SAmir Goldstein err = -EINVAL; 240e9e0c890SAmir Goldstein if (!type || type == FILEID_INVALID || bytes != dwords << 2) 241e9e0c890SAmir Goldstein goto out_err; 242e9e0c890SAmir Goldstein 24377115225SAmir Goldstein fid->fsid = *fsid; 244e9e0c890SAmir Goldstein event->fh_len = bytes; 245e9e0c890SAmir Goldstein 246e9e0c890SAmir Goldstein return type; 247e9e0c890SAmir Goldstein 248e9e0c890SAmir Goldstein out_err: 249e9e0c890SAmir Goldstein pr_warn_ratelimited("fanotify: failed to encode fid (fsid=%x.%x, " 250e9e0c890SAmir Goldstein "type=%d, bytes=%d, err=%i)\n", 25177115225SAmir Goldstein fsid->val[0], fsid->val[1], type, bytes, err); 252e9e0c890SAmir Goldstein kfree(fid->ext_fh); 253e9e0c890SAmir Goldstein fid->ext_fh = NULL; 254e9e0c890SAmir Goldstein event->fh_len = 0; 255e9e0c890SAmir Goldstein 256e9e0c890SAmir Goldstein return FILEID_INVALID; 257e9e0c890SAmir Goldstein } 258e9e0c890SAmir Goldstein 25983b535d2SAmir Goldstein /* 26083b535d2SAmir Goldstein * The inode to use as identifier when reporting fid depends on the event. 26183b535d2SAmir Goldstein * Report the modified directory inode on dirent modification events. 26283b535d2SAmir Goldstein * Report the "victim" inode otherwise. 26383b535d2SAmir Goldstein * For example: 26483b535d2SAmir Goldstein * FS_ATTRIB reports the child inode even if reported on a watched parent. 26583b535d2SAmir Goldstein * FS_CREATE reports the modified dir inode and not the created inode. 26683b535d2SAmir Goldstein */ 26783b535d2SAmir Goldstein static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask, 26883b535d2SAmir Goldstein const void *data, int data_type) 26983b535d2SAmir Goldstein { 27083b535d2SAmir Goldstein if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) 27183b535d2SAmir Goldstein return to_tell; 27283b535d2SAmir Goldstein else if (data_type == FSNOTIFY_EVENT_INODE) 27383b535d2SAmir Goldstein return (struct inode *)data; 27483b535d2SAmir Goldstein else if (data_type == FSNOTIFY_EVENT_PATH) 27583b535d2SAmir Goldstein return d_inode(((struct path *)data)->dentry); 27683b535d2SAmir Goldstein return NULL; 27783b535d2SAmir Goldstein } 27883b535d2SAmir Goldstein 27933913997SAmir Goldstein struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, 2801f5eaa90SJan Kara struct inode *inode, u32 mask, 28183b535d2SAmir Goldstein const void *data, int data_type, 28277115225SAmir Goldstein __kernel_fsid_t *fsid) 283f083441bSJan Kara { 28433913997SAmir Goldstein struct fanotify_event *event = NULL; 285d46eb14bSShakeel Butt gfp_t gfp = GFP_KERNEL_ACCOUNT; 28683b535d2SAmir Goldstein struct inode *id = fanotify_fid_inode(inode, mask, data, data_type); 2871f5eaa90SJan Kara 2881f5eaa90SJan Kara /* 2891f5eaa90SJan Kara * For queues with unlimited length lost events are not expected and 2901f5eaa90SJan Kara * can possibly have security implications. Avoid losing events when 2911f5eaa90SJan Kara * memory is short. 2921f5eaa90SJan Kara */ 2931f5eaa90SJan Kara if (group->max_events == UINT_MAX) 2941f5eaa90SJan Kara gfp |= __GFP_NOFAIL; 295f083441bSJan Kara 296d46eb14bSShakeel Butt /* Whoever is interested in the event, pays for the allocation. */ 297d46eb14bSShakeel Butt memalloc_use_memcg(group->memcg); 298d46eb14bSShakeel Butt 2996685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 30033913997SAmir Goldstein struct fanotify_perm_event *pevent; 301f083441bSJan Kara 3021f5eaa90SJan Kara pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); 303f083441bSJan Kara if (!pevent) 304d46eb14bSShakeel Butt goto out; 305f083441bSJan Kara event = &pevent->fae; 306f083441bSJan Kara pevent->response = 0; 30740873284SJan Kara pevent->state = FAN_EVENT_INIT; 308f083441bSJan Kara goto init; 309f083441bSJan Kara } 3101f5eaa90SJan Kara event = kmem_cache_alloc(fanotify_event_cachep, gfp); 311f083441bSJan Kara if (!event) 312d46eb14bSShakeel Butt goto out; 313f083441bSJan Kara init: __maybe_unused 314a0a92d26SAmir Goldstein fsnotify_init_event(&event->fse, inode); 315a0a92d26SAmir Goldstein event->mask = mask; 316d0a6a87eSAmir Goldstein if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) 317d0a6a87eSAmir Goldstein event->pid = get_pid(task_pid(current)); 318d0a6a87eSAmir Goldstein else 319d0a6a87eSAmir Goldstein event->pid = get_pid(task_tgid(current)); 320e9e0c890SAmir Goldstein event->fh_len = 0; 32183b535d2SAmir Goldstein if (id && FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { 322e9e0c890SAmir Goldstein /* Report the event without a file identifier on encode error */ 32383b535d2SAmir Goldstein event->fh_type = fanotify_encode_fid(event, id, gfp, fsid); 32483b535d2SAmir Goldstein } else if (data_type == FSNOTIFY_EVENT_PATH) { 325e9e0c890SAmir Goldstein event->fh_type = FILEID_ROOT; 32683b535d2SAmir Goldstein event->path = *((struct path *)data); 327f083441bSJan Kara path_get(&event->path); 328f083441bSJan Kara } else { 329e9e0c890SAmir Goldstein event->fh_type = FILEID_INVALID; 330f083441bSJan Kara event->path.mnt = NULL; 331f083441bSJan Kara event->path.dentry = NULL; 332f083441bSJan Kara } 333d46eb14bSShakeel Butt out: 334d46eb14bSShakeel Butt memalloc_unuse_memcg(); 335f083441bSJan Kara return event; 336f083441bSJan Kara } 337f083441bSJan Kara 33877115225SAmir Goldstein /* 33977115225SAmir Goldstein * Get cached fsid of the filesystem containing the object from any connector. 34077115225SAmir Goldstein * All connectors are supposed to have the same fsid, but we do not verify that 34177115225SAmir Goldstein * here. 34277115225SAmir Goldstein */ 34377115225SAmir Goldstein static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info) 34477115225SAmir Goldstein { 34577115225SAmir Goldstein int type; 34677115225SAmir Goldstein __kernel_fsid_t fsid = {}; 34777115225SAmir Goldstein 34877115225SAmir Goldstein fsnotify_foreach_obj_type(type) { 34977115225SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 35077115225SAmir Goldstein continue; 35177115225SAmir Goldstein 35277115225SAmir Goldstein fsid = iter_info->marks[type]->connector->fsid; 35377115225SAmir Goldstein if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1])) 35477115225SAmir Goldstein continue; 35577115225SAmir Goldstein return fsid; 35677115225SAmir Goldstein } 35777115225SAmir Goldstein 35877115225SAmir Goldstein return fsid; 35977115225SAmir Goldstein } 36077115225SAmir Goldstein 3617053aee2SJan Kara static int fanotify_handle_event(struct fsnotify_group *group, 3627053aee2SJan Kara struct inode *inode, 3633cd5eca8SAl Viro u32 mask, const void *data, int data_type, 364*e43e9c33SAl Viro const struct qstr *file_name, u32 cookie, 3659385a84dSJan Kara struct fsnotify_iter_info *iter_info) 3667053aee2SJan Kara { 3677053aee2SJan Kara int ret = 0; 36833913997SAmir Goldstein struct fanotify_event *event; 3697053aee2SJan Kara struct fsnotify_event *fsn_event; 37077115225SAmir Goldstein __kernel_fsid_t fsid = {}; 3717053aee2SJan Kara 3727053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); 3737053aee2SJan Kara BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); 374235328d1SAmir Goldstein BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB); 3757053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 3767053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); 3777053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN != FS_OPEN); 378235328d1SAmir Goldstein BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO); 379235328d1SAmir Goldstein BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM); 380235328d1SAmir Goldstein BUILD_BUG_ON(FAN_CREATE != FS_CREATE); 381235328d1SAmir Goldstein BUILD_BUG_ON(FAN_DELETE != FS_DELETE); 382235328d1SAmir Goldstein BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF); 383235328d1SAmir Goldstein BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF); 3847053aee2SJan Kara BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); 3857053aee2SJan Kara BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); 3867053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); 3877053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); 3887053aee2SJan Kara BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); 3899b076f1cSMatthew Bobrowski BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC); 39066917a31SMatthew Bobrowski BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); 3917053aee2SJan Kara 392235328d1SAmir Goldstein BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19); 393bdd5a46fSAmir Goldstein 39483b535d2SAmir Goldstein mask = fanotify_group_event_mask(group, iter_info, mask, data, 39583b535d2SAmir Goldstein data_type); 3962d10b230SMatthew Bobrowski if (!mask) 39783c4c4b0SJan Kara return 0; 39883c4c4b0SJan Kara 3997053aee2SJan Kara pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, 4007053aee2SJan Kara mask); 4017053aee2SJan Kara 4026685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 403f37650f1SMiklos Szeredi /* 404f37650f1SMiklos Szeredi * fsnotify_prepare_user_wait() fails if we race with mark 405f37650f1SMiklos Szeredi * deletion. Just let the operation pass in that case. 406f37650f1SMiklos Szeredi */ 407f37650f1SMiklos Szeredi if (!fsnotify_prepare_user_wait(iter_info)) 408f37650f1SMiklos Szeredi return 0; 409f37650f1SMiklos Szeredi } 410f37650f1SMiklos Szeredi 41177115225SAmir Goldstein if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) 41277115225SAmir Goldstein fsid = fanotify_get_fsid(iter_info); 41377115225SAmir Goldstein 41483b535d2SAmir Goldstein event = fanotify_alloc_event(group, inode, mask, data, data_type, 41583b535d2SAmir Goldstein &fsid); 416f37650f1SMiklos Szeredi ret = -ENOMEM; 4177b1f6417SJan Kara if (unlikely(!event)) { 4187b1f6417SJan Kara /* 4197b1f6417SJan Kara * We don't queue overflow events for permission events as 4207b1f6417SJan Kara * there the access is denied and so no event is in fact lost. 4217b1f6417SJan Kara */ 4227b1f6417SJan Kara if (!fanotify_is_perm_event(mask)) 4237b1f6417SJan Kara fsnotify_queue_overflow(group); 424f37650f1SMiklos Szeredi goto finish; 4257b1f6417SJan Kara } 4267053aee2SJan Kara 4277053aee2SJan Kara fsn_event = &event->fse; 4288ba8fa91SJan Kara ret = fsnotify_add_event(group, fsn_event, fanotify_merge); 42983c0e1b4SJan Kara if (ret) { 430482ef06cSJan Kara /* Permission events shouldn't be merged */ 43123c9deebSAmir Goldstein BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS); 4327053aee2SJan Kara /* Our event wasn't used in the end. Free it. */ 4337053aee2SJan Kara fsnotify_destroy_event(group, fsn_event); 434482ef06cSJan Kara 435f37650f1SMiklos Szeredi ret = 0; 4366685df31SMiklos Szeredi } else if (fanotify_is_perm_event(mask)) { 43705f0e387SJan Kara ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event), 43805f0e387SJan Kara iter_info); 43985816794SJan Kara } 440f37650f1SMiklos Szeredi finish: 4416685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) 442f37650f1SMiklos Szeredi fsnotify_finish_user_wait(iter_info); 4436685df31SMiklos Szeredi 4447053aee2SJan Kara return ret; 4457053aee2SJan Kara } 4467053aee2SJan Kara 4474afeff85SEric Paris static void fanotify_free_group_priv(struct fsnotify_group *group) 4484afeff85SEric Paris { 4494afeff85SEric Paris struct user_struct *user; 4504afeff85SEric Paris 4514afeff85SEric Paris user = group->fanotify_data.user; 4524afeff85SEric Paris atomic_dec(&user->fanotify_listeners); 4534afeff85SEric Paris free_uid(user); 4544afeff85SEric Paris } 4554afeff85SEric Paris 4567053aee2SJan Kara static void fanotify_free_event(struct fsnotify_event *fsn_event) 4577053aee2SJan Kara { 45833913997SAmir Goldstein struct fanotify_event *event; 4597053aee2SJan Kara 4607053aee2SJan Kara event = FANOTIFY_E(fsn_event); 461e9e0c890SAmir Goldstein if (fanotify_event_has_path(event)) 4627053aee2SJan Kara path_put(&event->path); 463e9e0c890SAmir Goldstein else if (fanotify_event_has_ext_fh(event)) 464e9e0c890SAmir Goldstein kfree(event->fid.ext_fh); 465d0a6a87eSAmir Goldstein put_pid(event->pid); 466a0a92d26SAmir Goldstein if (fanotify_is_perm_event(event->mask)) { 467f083441bSJan Kara kmem_cache_free(fanotify_perm_event_cachep, 468f083441bSJan Kara FANOTIFY_PE(fsn_event)); 469f083441bSJan Kara return; 470f083441bSJan Kara } 4717053aee2SJan Kara kmem_cache_free(fanotify_event_cachep, event); 4727053aee2SJan Kara } 4737053aee2SJan Kara 474054c636eSJan Kara static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) 475054c636eSJan Kara { 476054c636eSJan Kara kmem_cache_free(fanotify_mark_cache, fsn_mark); 477054c636eSJan Kara } 478054c636eSJan Kara 479ff0b16a9SEric Paris const struct fsnotify_ops fanotify_fsnotify_ops = { 480ff0b16a9SEric Paris .handle_event = fanotify_handle_event, 4814afeff85SEric Paris .free_group_priv = fanotify_free_group_priv, 4827053aee2SJan Kara .free_event = fanotify_free_event, 483054c636eSJan Kara .free_mark = fanotify_free_mark, 484ff0b16a9SEric Paris }; 485