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 20afc894c7SJan Kara static bool fanotify_path_equal(struct path *p1, struct path *p2) 21afc894c7SJan Kara { 22afc894c7SJan Kara return p1->mnt == p2->mnt && p1->dentry == p2->dentry; 23afc894c7SJan Kara } 24afc894c7SJan Kara 25afc894c7SJan Kara static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1, 26afc894c7SJan Kara __kernel_fsid_t *fsid2) 27afc894c7SJan Kara { 286def1a1dSNathan Chancellor return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1]; 29afc894c7SJan Kara } 30afc894c7SJan Kara 31afc894c7SJan Kara static bool fanotify_fh_equal(struct fanotify_fh *fh1, 32afc894c7SJan Kara struct fanotify_fh *fh2) 33afc894c7SJan Kara { 34afc894c7SJan Kara if (fh1->type != fh2->type || fh1->len != fh2->len) 35afc894c7SJan Kara return false; 36afc894c7SJan Kara 37afc894c7SJan Kara /* Do not merge events if we failed to encode fh */ 38afc894c7SJan Kara if (fh1->type == FILEID_INVALID) 39afc894c7SJan Kara return false; 40afc894c7SJan Kara 41afc894c7SJan Kara return !fh1->len || 42afc894c7SJan Kara !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len); 43afc894c7SJan Kara } 44afc894c7SJan Kara 457088f357SJan Kara static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1, 467088f357SJan Kara struct fanotify_fid_event *ffe2) 477088f357SJan Kara { 487088f357SJan Kara /* Do not merge fid events without object fh */ 497088f357SJan Kara if (!ffe1->object_fh.len) 507088f357SJan Kara return false; 517088f357SJan Kara 527088f357SJan Kara return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) && 537088f357SJan Kara fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh); 547088f357SJan Kara } 557088f357SJan Kara 56cacfb956SAmir Goldstein static bool fanotify_name_event_equal(struct fanotify_name_event *fne1, 57cacfb956SAmir Goldstein struct fanotify_name_event *fne2) 58cacfb956SAmir Goldstein { 59cacfb956SAmir Goldstein /* 60cacfb956SAmir Goldstein * Do not merge name events without dir fh. 61cacfb956SAmir Goldstein * FAN_DIR_MODIFY does not encode object fh, so it may be empty. 62cacfb956SAmir Goldstein */ 63cacfb956SAmir Goldstein if (!fne1->dir_fh.len) 64cacfb956SAmir Goldstein return false; 65cacfb956SAmir Goldstein 66cacfb956SAmir Goldstein if (fne1->name_len != fne2->name_len || 67cacfb956SAmir Goldstein !fanotify_fh_equal(&fne1->dir_fh, &fne2->dir_fh)) 68cacfb956SAmir Goldstein return false; 69cacfb956SAmir Goldstein 70cacfb956SAmir Goldstein return !memcmp(fne1->name, fne2->name, fne1->name_len); 71cacfb956SAmir Goldstein } 72cacfb956SAmir Goldstein 73ab3c4da0SFabian Frederick static bool fanotify_should_merge(struct fsnotify_event *old_fsn, 747053aee2SJan Kara struct fsnotify_event *new_fsn) 757053aee2SJan Kara { 7633913997SAmir Goldstein struct fanotify_event *old, *new; 777053aee2SJan Kara 787053aee2SJan Kara pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); 797053aee2SJan Kara old = FANOTIFY_E(old_fsn); 807053aee2SJan Kara new = FANOTIFY_E(new_fsn); 817053aee2SJan Kara 827088f357SJan Kara if (old_fsn->objectid != new_fsn->objectid || 837088f357SJan Kara old->type != new->type || old->pid != new->pid) 84e9e0c890SAmir Goldstein return false; 85e9e0c890SAmir Goldstein 867088f357SJan Kara switch (old->type) { 877088f357SJan Kara case FANOTIFY_EVENT_TYPE_PATH: 88afc894c7SJan Kara return fanotify_path_equal(fanotify_event_path(old), 89afc894c7SJan Kara fanotify_event_path(new)); 907088f357SJan Kara case FANOTIFY_EVENT_TYPE_FID: 91e7fce6d9SAmir Goldstein /* 92e7fce6d9SAmir Goldstein * We want to merge many dirent events in the same dir (i.e. 93e7fce6d9SAmir Goldstein * creates/unlinks/renames), but we do not want to merge dirent 94e7fce6d9SAmir Goldstein * events referring to subdirs with dirent events referring to 95e7fce6d9SAmir Goldstein * non subdirs, otherwise, user won't be able to tell from a 96e7fce6d9SAmir Goldstein * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+ 97e7fce6d9SAmir Goldstein * unlink pair or rmdir+create pair of events. 98e7fce6d9SAmir Goldstein */ 99afc894c7SJan Kara if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR)) 100afc894c7SJan Kara return false; 101afc894c7SJan Kara 1027088f357SJan Kara return fanotify_fid_event_equal(FANOTIFY_FE(old), 1037088f357SJan Kara FANOTIFY_FE(new)); 104cacfb956SAmir Goldstein case FANOTIFY_EVENT_TYPE_FID_NAME: 105cacfb956SAmir Goldstein return fanotify_name_event_equal(FANOTIFY_NE(old), 106cacfb956SAmir Goldstein FANOTIFY_NE(new)); 1077088f357SJan Kara default: 1087088f357SJan Kara WARN_ON_ONCE(1); 109e9e0c890SAmir Goldstein } 110e9e0c890SAmir Goldstein 111767cd46cSEric Paris return false; 112767cd46cSEric Paris } 113767cd46cSEric Paris 114f70ab54cSEric Paris /* and the list better be locked by something too! */ 11583c0e1b4SJan Kara static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) 116767cd46cSEric Paris { 1177053aee2SJan Kara struct fsnotify_event *test_event; 11833913997SAmir Goldstein struct fanotify_event *new; 119767cd46cSEric Paris 120767cd46cSEric Paris pr_debug("%s: list=%p event=%p\n", __func__, list, event); 121a0a92d26SAmir Goldstein new = FANOTIFY_E(event); 122767cd46cSEric Paris 12313116dfdSJan Kara /* 12413116dfdSJan Kara * Don't merge a permission event with any other event so that we know 12513116dfdSJan Kara * the event structure we have created in fanotify_handle_event() is the 12613116dfdSJan Kara * one we should check for permission response. 12713116dfdSJan Kara */ 128a0a92d26SAmir Goldstein if (fanotify_is_perm_event(new->mask)) 12983c0e1b4SJan Kara return 0; 13013116dfdSJan Kara 1317053aee2SJan Kara list_for_each_entry_reverse(test_event, list, list) { 132ab3c4da0SFabian Frederick if (fanotify_should_merge(test_event, event)) { 133a0a92d26SAmir Goldstein FANOTIFY_E(test_event)->mask |= new->mask; 13483c0e1b4SJan Kara return 1; 1359dced01aSEric Paris } 1366c71100dSKinglong Mee } 1376c71100dSKinglong Mee 1386c71100dSKinglong Mee return 0; 1396c71100dSKinglong Mee } 1409dced01aSEric Paris 141fabf7f29SJan Kara /* 142fabf7f29SJan Kara * Wait for response to permission event. The function also takes care of 143fabf7f29SJan Kara * freeing the permission event (or offloads that in case the wait is canceled 144fabf7f29SJan Kara * by a signal). The function returns 0 in case access got allowed by userspace, 145fabf7f29SJan Kara * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case 146fabf7f29SJan Kara * the wait got interrupted by a signal. 147fabf7f29SJan Kara */ 148f083441bSJan Kara static int fanotify_get_response(struct fsnotify_group *group, 14933913997SAmir Goldstein struct fanotify_perm_event *event, 15005f0e387SJan Kara struct fsnotify_iter_info *iter_info) 1519e66e423SEric Paris { 1529e66e423SEric Paris int ret; 1539e66e423SEric Paris 1549e66e423SEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 1559e66e423SEric Paris 156b5190579SJan Kara ret = wait_event_killable(group->fanotify_data.access_waitq, 15740873284SJan Kara event->state == FAN_EVENT_ANSWERED); 158fabf7f29SJan Kara /* Signal pending? */ 159fabf7f29SJan Kara if (ret < 0) { 160fabf7f29SJan Kara spin_lock(&group->notification_lock); 161fabf7f29SJan Kara /* Event reported to userspace and no answer yet? */ 162fabf7f29SJan Kara if (event->state == FAN_EVENT_REPORTED) { 163fabf7f29SJan Kara /* Event will get freed once userspace answers to it */ 164fabf7f29SJan Kara event->state = FAN_EVENT_CANCELED; 165fabf7f29SJan Kara spin_unlock(&group->notification_lock); 166fabf7f29SJan Kara return ret; 167fabf7f29SJan Kara } 168fabf7f29SJan Kara /* Event not yet reported? Just remove it. */ 169fabf7f29SJan Kara if (event->state == FAN_EVENT_INIT) 170fabf7f29SJan Kara fsnotify_remove_queued_event(group, &event->fae.fse); 171fabf7f29SJan Kara /* 172fabf7f29SJan Kara * Event may be also answered in case signal delivery raced 173fabf7f29SJan Kara * with wakeup. In that case we have nothing to do besides 174fabf7f29SJan Kara * freeing the event and reporting error. 175fabf7f29SJan Kara */ 176fabf7f29SJan Kara spin_unlock(&group->notification_lock); 177fabf7f29SJan Kara goto out; 178fabf7f29SJan Kara } 1799e66e423SEric Paris 1809e66e423SEric Paris /* userspace responded, convert to something usable */ 181de8cd83eSSteve Grubb switch (event->response & ~FAN_AUDIT) { 1829e66e423SEric Paris case FAN_ALLOW: 1839e66e423SEric Paris ret = 0; 1849e66e423SEric Paris break; 1859e66e423SEric Paris case FAN_DENY: 1869e66e423SEric Paris default: 1879e66e423SEric Paris ret = -EPERM; 1889e66e423SEric Paris } 189de8cd83eSSteve Grubb 190de8cd83eSSteve Grubb /* Check if the response should be audited */ 191de8cd83eSSteve Grubb if (event->response & FAN_AUDIT) 192de8cd83eSSteve Grubb audit_fanotify(event->response & ~FAN_AUDIT); 193de8cd83eSSteve Grubb 194b2d87909SEric Paris pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, 195b2d87909SEric Paris group, event, ret); 196fabf7f29SJan Kara out: 197fabf7f29SJan Kara fsnotify_destroy_event(group, &event->fae.fse); 198b2d87909SEric Paris 1999e66e423SEric Paris return ret; 2009e66e423SEric Paris } 2019e66e423SEric Paris 2022d10b230SMatthew Bobrowski /* 2032d10b230SMatthew Bobrowski * This function returns a mask for an event that only contains the flags 2042d10b230SMatthew Bobrowski * that have been specifically requested by the user. Flags that may have 2052d10b230SMatthew Bobrowski * been included within the event mask, but have not been explicitly 2062d10b230SMatthew Bobrowski * requested by the user, will not be present in the returned mask. 2072d10b230SMatthew Bobrowski */ 20883b535d2SAmir Goldstein static u32 fanotify_group_event_mask(struct fsnotify_group *group, 20983b535d2SAmir Goldstein struct fsnotify_iter_info *iter_info, 2105b0457adSAmir Goldstein u32 event_mask, const void *data, 2115b0457adSAmir Goldstein int data_type) 2121c529063SEric Paris { 21354a307baSAmir Goldstein __u32 marks_mask = 0, marks_ignored_mask = 0; 214*0badfa02SAmir Goldstein __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS | 215*0badfa02SAmir Goldstein FANOTIFY_EVENT_FLAGS; 216aa93bdc5SAmir Goldstein const struct path *path = fsnotify_data_path(data, data_type); 217837a3934SAmir Goldstein struct fsnotify_mark *mark; 218837a3934SAmir Goldstein int type; 2191968f5eeSEric Paris 220837a3934SAmir Goldstein pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", 221837a3934SAmir Goldstein __func__, iter_info->report_mask, event_mask, data, data_type); 2221968f5eeSEric Paris 22383b535d2SAmir Goldstein if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { 22483b535d2SAmir Goldstein /* Do we have path to open a file descriptor? */ 225aa93bdc5SAmir Goldstein if (!path) 2262d10b230SMatthew Bobrowski return 0; 22783b535d2SAmir Goldstein /* Path type events are only relevant for files and dirs */ 22883b535d2SAmir Goldstein if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry)) 2292d10b230SMatthew Bobrowski return 0; 23083b535d2SAmir Goldstein } 231e1c048baSEric Paris 232837a3934SAmir Goldstein fsnotify_foreach_obj_type(type) { 233837a3934SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 234837a3934SAmir Goldstein continue; 235837a3934SAmir Goldstein mark = iter_info->marks[type]; 2362f02fd3fSAmir Goldstein 2372f02fd3fSAmir Goldstein /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */ 2382f02fd3fSAmir Goldstein marks_ignored_mask |= mark->ignored_mask; 2392f02fd3fSAmir Goldstein 2401968f5eeSEric Paris /* 24155bf882cSAmir Goldstein * If the event is on dir and this mark doesn't care about 24255bf882cSAmir Goldstein * events on dir, don't send it! 24355bf882cSAmir Goldstein */ 24455bf882cSAmir Goldstein if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR)) 24555bf882cSAmir Goldstein continue; 24655bf882cSAmir Goldstein 24755bf882cSAmir Goldstein /* 248b469e7e4SAmir Goldstein * If the event is for a child and this mark doesn't care about 249b469e7e4SAmir Goldstein * events on a child, don't send it! 2501968f5eeSEric Paris */ 251b469e7e4SAmir Goldstein if (event_mask & FS_EVENT_ON_CHILD && 252b469e7e4SAmir Goldstein (type != FSNOTIFY_OBJ_TYPE_INODE || 253b469e7e4SAmir Goldstein !(mark->mask & FS_EVENT_ON_CHILD))) 254837a3934SAmir Goldstein continue; 25554a307baSAmir Goldstein 256837a3934SAmir Goldstein marks_mask |= mark->mask; 2571968f5eeSEric Paris } 2581968f5eeSEric Paris 259e7fce6d9SAmir Goldstein test_mask = event_mask & marks_mask & ~marks_ignored_mask; 260e7fce6d9SAmir Goldstein 261e7fce6d9SAmir Goldstein /* 2629e2ba2c3SAmir Goldstein * For dirent modification events (create/delete/move) that do not carry 2639e2ba2c3SAmir Goldstein * the child entry name information, we report FAN_ONDIR for mkdir/rmdir 2649e2ba2c3SAmir Goldstein * so user can differentiate them from creat/unlink. 265e7fce6d9SAmir Goldstein * 266e7fce6d9SAmir Goldstein * For backward compatibility and consistency, do not report FAN_ONDIR 267e7fce6d9SAmir Goldstein * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR 268*0badfa02SAmir Goldstein * to user in fid mode for all event types. 269*0badfa02SAmir Goldstein * 270*0badfa02SAmir Goldstein * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to 271*0badfa02SAmir Goldstein * fanotify_alloc_event() when group is reporting fid as indication 272*0badfa02SAmir Goldstein * that event happened on child. 273e7fce6d9SAmir Goldstein */ 274e7fce6d9SAmir Goldstein if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { 275*0badfa02SAmir Goldstein /* Do not report event flags without any event */ 276*0badfa02SAmir Goldstein if (!(test_mask & ~FANOTIFY_EVENT_FLAGS)) 277e7fce6d9SAmir Goldstein return 0; 278e7fce6d9SAmir Goldstein } else { 279*0badfa02SAmir Goldstein user_mask &= ~FANOTIFY_EVENT_FLAGS; 280e7fce6d9SAmir Goldstein } 281e7fce6d9SAmir Goldstein 282e7fce6d9SAmir Goldstein return test_mask & user_mask; 2831c529063SEric Paris } 2841c529063SEric Paris 285afc894c7SJan Kara static void fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode, 286afc894c7SJan Kara gfp_t gfp) 287e9e0c890SAmir Goldstein { 288afc894c7SJan Kara int dwords, type, bytes = 0; 289afc894c7SJan Kara char *ext_buf = NULL; 290afc894c7SJan Kara void *buf = fh->buf; 291afc894c7SJan Kara int err; 292e9e0c890SAmir Goldstein 293cacfb956SAmir Goldstein if (!inode) 294cacfb956SAmir Goldstein goto out; 295cacfb956SAmir Goldstein 296e9e0c890SAmir Goldstein dwords = 0; 297e9e0c890SAmir Goldstein err = -ENOENT; 29877115225SAmir Goldstein type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL); 299e9e0c890SAmir Goldstein if (!dwords) 300e9e0c890SAmir Goldstein goto out_err; 301e9e0c890SAmir Goldstein 302e9e0c890SAmir Goldstein bytes = dwords << 2; 303e9e0c890SAmir Goldstein if (bytes > FANOTIFY_INLINE_FH_LEN) { 304e9e0c890SAmir Goldstein /* Treat failure to allocate fh as failure to allocate event */ 305e9e0c890SAmir Goldstein err = -ENOMEM; 306afc894c7SJan Kara ext_buf = kmalloc(bytes, gfp); 307afc894c7SJan Kara if (!ext_buf) 308e9e0c890SAmir Goldstein goto out_err; 309afc894c7SJan Kara 310afc894c7SJan Kara *fanotify_fh_ext_buf_ptr(fh) = ext_buf; 311afc894c7SJan Kara buf = ext_buf; 312e9e0c890SAmir Goldstein } 313e9e0c890SAmir Goldstein 314afc894c7SJan Kara type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL); 315e9e0c890SAmir Goldstein err = -EINVAL; 316e9e0c890SAmir Goldstein if (!type || type == FILEID_INVALID || bytes != dwords << 2) 317e9e0c890SAmir Goldstein goto out_err; 318e9e0c890SAmir Goldstein 319afc894c7SJan Kara fh->type = type; 320afc894c7SJan Kara fh->len = bytes; 321e9e0c890SAmir Goldstein 322afc894c7SJan Kara return; 323e9e0c890SAmir Goldstein 324e9e0c890SAmir Goldstein out_err: 325afc894c7SJan Kara pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n", 326afc894c7SJan Kara type, bytes, err); 327afc894c7SJan Kara kfree(ext_buf); 328afc894c7SJan Kara *fanotify_fh_ext_buf_ptr(fh) = NULL; 329cacfb956SAmir Goldstein out: 330afc894c7SJan Kara /* Report the event without a file identifier on encode error */ 331afc894c7SJan Kara fh->type = FILEID_INVALID; 332afc894c7SJan Kara fh->len = 0; 333e9e0c890SAmir Goldstein } 334e9e0c890SAmir Goldstein 33583b535d2SAmir Goldstein /* 33683b535d2SAmir Goldstein * The inode to use as identifier when reporting fid depends on the event. 33783b535d2SAmir Goldstein * Report the modified directory inode on dirent modification events. 33883b535d2SAmir Goldstein * Report the "victim" inode otherwise. 33983b535d2SAmir Goldstein * For example: 34083b535d2SAmir Goldstein * FS_ATTRIB reports the child inode even if reported on a watched parent. 34183b535d2SAmir Goldstein * FS_CREATE reports the modified dir inode and not the created inode. 34283b535d2SAmir Goldstein */ 343b54cecf5SAmir Goldstein static struct inode *fanotify_fid_inode(u32 event_mask, const void *data, 344b54cecf5SAmir Goldstein int data_type, struct inode *dir) 34583b535d2SAmir Goldstein { 34683b535d2SAmir Goldstein if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) 347b54cecf5SAmir Goldstein return dir; 348aa93bdc5SAmir Goldstein 349cbcf47adSAmir Goldstein return fsnotify_data_inode(data, data_type); 35083b535d2SAmir Goldstein } 35183b535d2SAmir Goldstein 3529c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_path_event(const struct path *path, 3539c61f3b5SAmir Goldstein gfp_t gfp) 3549c61f3b5SAmir Goldstein { 3559c61f3b5SAmir Goldstein struct fanotify_path_event *pevent; 3569c61f3b5SAmir Goldstein 3579c61f3b5SAmir Goldstein pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp); 3589c61f3b5SAmir Goldstein if (!pevent) 3599c61f3b5SAmir Goldstein return NULL; 3609c61f3b5SAmir Goldstein 3619c61f3b5SAmir Goldstein pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH; 3629c61f3b5SAmir Goldstein pevent->path = *path; 3639c61f3b5SAmir Goldstein path_get(path); 3649c61f3b5SAmir Goldstein 3659c61f3b5SAmir Goldstein return &pevent->fae; 3669c61f3b5SAmir Goldstein } 3679c61f3b5SAmir Goldstein 3689c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, 3699c61f3b5SAmir Goldstein gfp_t gfp) 3709c61f3b5SAmir Goldstein { 3719c61f3b5SAmir Goldstein struct fanotify_perm_event *pevent; 3729c61f3b5SAmir Goldstein 3739c61f3b5SAmir Goldstein pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); 3749c61f3b5SAmir Goldstein if (!pevent) 3759c61f3b5SAmir Goldstein return NULL; 3769c61f3b5SAmir Goldstein 3779c61f3b5SAmir Goldstein pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM; 3789c61f3b5SAmir Goldstein pevent->response = 0; 3799c61f3b5SAmir Goldstein pevent->state = FAN_EVENT_INIT; 3809c61f3b5SAmir Goldstein pevent->path = *path; 3819c61f3b5SAmir Goldstein path_get(path); 3829c61f3b5SAmir Goldstein 3839c61f3b5SAmir Goldstein return &pevent->fae; 3849c61f3b5SAmir Goldstein } 3859c61f3b5SAmir Goldstein 3869c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id, 3879c61f3b5SAmir Goldstein __kernel_fsid_t *fsid, 3889c61f3b5SAmir Goldstein gfp_t gfp) 3899c61f3b5SAmir Goldstein { 3909c61f3b5SAmir Goldstein struct fanotify_fid_event *ffe; 3919c61f3b5SAmir Goldstein 3929c61f3b5SAmir Goldstein ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp); 3939c61f3b5SAmir Goldstein if (!ffe) 3949c61f3b5SAmir Goldstein return NULL; 3959c61f3b5SAmir Goldstein 3969c61f3b5SAmir Goldstein ffe->fae.type = FANOTIFY_EVENT_TYPE_FID; 3979c61f3b5SAmir Goldstein ffe->fsid = *fsid; 3989c61f3b5SAmir Goldstein fanotify_encode_fh(&ffe->object_fh, id, gfp); 3999c61f3b5SAmir Goldstein 4009c61f3b5SAmir Goldstein return &ffe->fae; 4019c61f3b5SAmir Goldstein } 4029c61f3b5SAmir Goldstein 4039c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_name_event(struct inode *id, 4049c61f3b5SAmir Goldstein __kernel_fsid_t *fsid, 4059c61f3b5SAmir Goldstein const struct qstr *file_name, 4069c61f3b5SAmir Goldstein gfp_t gfp) 4079c61f3b5SAmir Goldstein { 4089c61f3b5SAmir Goldstein struct fanotify_name_event *fne; 4099c61f3b5SAmir Goldstein 4109c61f3b5SAmir Goldstein fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp); 4119c61f3b5SAmir Goldstein if (!fne) 4129c61f3b5SAmir Goldstein return NULL; 4139c61f3b5SAmir Goldstein 4149c61f3b5SAmir Goldstein fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME; 4159c61f3b5SAmir Goldstein fne->fsid = *fsid; 4169c61f3b5SAmir Goldstein fanotify_encode_fh(&fne->dir_fh, id, gfp); 4179c61f3b5SAmir Goldstein fne->name_len = file_name->len; 4189c61f3b5SAmir Goldstein strcpy(fne->name, file_name->name); 4199c61f3b5SAmir Goldstein 4209c61f3b5SAmir Goldstein return &fne->fae; 4219c61f3b5SAmir Goldstein } 4229c61f3b5SAmir Goldstein 423b8a6c3a2SAmir Goldstein static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, 424b54cecf5SAmir Goldstein u32 mask, const void *data, 425b54cecf5SAmir Goldstein int data_type, struct inode *dir, 426cacfb956SAmir Goldstein const struct qstr *file_name, 42777115225SAmir Goldstein __kernel_fsid_t *fsid) 428f083441bSJan Kara { 42933913997SAmir Goldstein struct fanotify_event *event = NULL; 430d46eb14bSShakeel Butt gfp_t gfp = GFP_KERNEL_ACCOUNT; 431b54cecf5SAmir Goldstein struct inode *id = fanotify_fid_inode(mask, data, data_type, dir); 432aa93bdc5SAmir Goldstein const struct path *path = fsnotify_data_path(data, data_type); 43308b95c33SAmir Goldstein bool name_event = false; 4341f5eaa90SJan Kara 4351f5eaa90SJan Kara /* 4361f5eaa90SJan Kara * For queues with unlimited length lost events are not expected and 4371f5eaa90SJan Kara * can possibly have security implications. Avoid losing events when 438ec165450SShakeel Butt * memory is short. For the limited size queues, avoid OOM killer in the 439ec165450SShakeel Butt * target monitoring memcg as it may have security repercussion. 4401f5eaa90SJan Kara */ 4411f5eaa90SJan Kara if (group->max_events == UINT_MAX) 4421f5eaa90SJan Kara gfp |= __GFP_NOFAIL; 443ec165450SShakeel Butt else 444ec165450SShakeel Butt gfp |= __GFP_RETRY_MAYFAIL; 445f083441bSJan Kara 446d46eb14bSShakeel Butt /* Whoever is interested in the event, pays for the allocation. */ 447d46eb14bSShakeel Butt memalloc_use_memcg(group->memcg); 448d46eb14bSShakeel Butt 4496685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 4509c61f3b5SAmir Goldstein event = fanotify_alloc_perm_event(path, gfp); 45108b95c33SAmir Goldstein } else if (name_event && file_name) { 4529c61f3b5SAmir Goldstein event = fanotify_alloc_name_event(id, fsid, file_name, gfp); 4539c61f3b5SAmir Goldstein } else if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { 4549c61f3b5SAmir Goldstein event = fanotify_alloc_fid_event(id, fsid, gfp); 4557088f357SJan Kara } else { 4569c61f3b5SAmir Goldstein event = fanotify_alloc_path_event(path, gfp); 4577088f357SJan Kara } 4587088f357SJan Kara 4599c61f3b5SAmir Goldstein if (!event) 4609c61f3b5SAmir Goldstein goto out; 4619c61f3b5SAmir Goldstein 462f367a62aSAmir Goldstein /* 463f367a62aSAmir Goldstein * Use the victim inode instead of the watching inode as the id for 464f367a62aSAmir Goldstein * event queue, so event reported on parent is merged with event 465f367a62aSAmir Goldstein * reported on child when both directory and child watches exist. 466f367a62aSAmir Goldstein */ 467b8a6c3a2SAmir Goldstein fanotify_init_event(event, (unsigned long)id, mask); 468d0a6a87eSAmir Goldstein if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) 469d0a6a87eSAmir Goldstein event->pid = get_pid(task_pid(current)); 470d0a6a87eSAmir Goldstein else 471d0a6a87eSAmir Goldstein event->pid = get_pid(task_tgid(current)); 4727088f357SJan Kara 473d46eb14bSShakeel Butt out: 474d46eb14bSShakeel Butt memalloc_unuse_memcg(); 475f083441bSJan Kara return event; 476f083441bSJan Kara } 477f083441bSJan Kara 47877115225SAmir Goldstein /* 47977115225SAmir Goldstein * Get cached fsid of the filesystem containing the object from any connector. 48077115225SAmir Goldstein * All connectors are supposed to have the same fsid, but we do not verify that 48177115225SAmir Goldstein * here. 48277115225SAmir Goldstein */ 48377115225SAmir Goldstein static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info) 48477115225SAmir Goldstein { 48577115225SAmir Goldstein int type; 48677115225SAmir Goldstein __kernel_fsid_t fsid = {}; 48777115225SAmir Goldstein 48877115225SAmir Goldstein fsnotify_foreach_obj_type(type) { 489b1da6a51SJan Kara struct fsnotify_mark_connector *conn; 490b1da6a51SJan Kara 49177115225SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 49277115225SAmir Goldstein continue; 49377115225SAmir Goldstein 494b1da6a51SJan Kara conn = READ_ONCE(iter_info->marks[type]->connector); 495b1da6a51SJan Kara /* Mark is just getting destroyed or created? */ 496b1da6a51SJan Kara if (!conn) 497b1da6a51SJan Kara continue; 498c285a2f0SAmir Goldstein if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID)) 499c285a2f0SAmir Goldstein continue; 500c285a2f0SAmir Goldstein /* Pairs with smp_wmb() in fsnotify_add_mark_list() */ 501c285a2f0SAmir Goldstein smp_rmb(); 502b1da6a51SJan Kara fsid = conn->fsid; 50377115225SAmir Goldstein if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1])) 50477115225SAmir Goldstein continue; 50577115225SAmir Goldstein return fsid; 50677115225SAmir Goldstein } 50777115225SAmir Goldstein 50877115225SAmir Goldstein return fsid; 50977115225SAmir Goldstein } 51077115225SAmir Goldstein 511b54cecf5SAmir Goldstein static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, 512b54cecf5SAmir Goldstein const void *data, int data_type, 513b54cecf5SAmir Goldstein struct inode *dir, 514e43e9c33SAl Viro const struct qstr *file_name, u32 cookie, 5159385a84dSJan Kara struct fsnotify_iter_info *iter_info) 5167053aee2SJan Kara { 5177053aee2SJan Kara int ret = 0; 51833913997SAmir Goldstein struct fanotify_event *event; 5197053aee2SJan Kara struct fsnotify_event *fsn_event; 52077115225SAmir Goldstein __kernel_fsid_t fsid = {}; 5217053aee2SJan Kara 5227053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); 5237053aee2SJan Kara BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); 524235328d1SAmir Goldstein BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB); 5257053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 5267053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); 5277053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN != FS_OPEN); 528235328d1SAmir Goldstein BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO); 529235328d1SAmir Goldstein BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM); 530235328d1SAmir Goldstein BUILD_BUG_ON(FAN_CREATE != FS_CREATE); 531235328d1SAmir Goldstein BUILD_BUG_ON(FAN_DELETE != FS_DELETE); 532235328d1SAmir Goldstein BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF); 533235328d1SAmir Goldstein BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF); 5347053aee2SJan Kara BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); 5357053aee2SJan Kara BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); 5367053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); 5377053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); 5387053aee2SJan Kara BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); 5399b076f1cSMatthew Bobrowski BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC); 54066917a31SMatthew Bobrowski BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); 5417053aee2SJan Kara 542f1793699SAmir Goldstein BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19); 543bdd5a46fSAmir Goldstein 54483b535d2SAmir Goldstein mask = fanotify_group_event_mask(group, iter_info, mask, data, 54583b535d2SAmir Goldstein data_type); 5462d10b230SMatthew Bobrowski if (!mask) 54783c4c4b0SJan Kara return 0; 54883c4c4b0SJan Kara 549b54cecf5SAmir Goldstein pr_debug("%s: group=%p mask=%x\n", __func__, group, mask); 5507053aee2SJan Kara 5516685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 552f37650f1SMiklos Szeredi /* 553f37650f1SMiklos Szeredi * fsnotify_prepare_user_wait() fails if we race with mark 554f37650f1SMiklos Szeredi * deletion. Just let the operation pass in that case. 555f37650f1SMiklos Szeredi */ 556f37650f1SMiklos Szeredi if (!fsnotify_prepare_user_wait(iter_info)) 557f37650f1SMiklos Szeredi return 0; 558f37650f1SMiklos Szeredi } 559f37650f1SMiklos Szeredi 560b1da6a51SJan Kara if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { 56177115225SAmir Goldstein fsid = fanotify_get_fsid(iter_info); 562b1da6a51SJan Kara /* Racing with mark destruction or creation? */ 563b1da6a51SJan Kara if (!fsid.val[0] && !fsid.val[1]) 564b1da6a51SJan Kara return 0; 565b1da6a51SJan Kara } 56677115225SAmir Goldstein 567b54cecf5SAmir Goldstein event = fanotify_alloc_event(group, mask, data, data_type, dir, 568cacfb956SAmir Goldstein file_name, &fsid); 569f37650f1SMiklos Szeredi ret = -ENOMEM; 5707b1f6417SJan Kara if (unlikely(!event)) { 5717b1f6417SJan Kara /* 5727b1f6417SJan Kara * We don't queue overflow events for permission events as 5737b1f6417SJan Kara * there the access is denied and so no event is in fact lost. 5747b1f6417SJan Kara */ 5757b1f6417SJan Kara if (!fanotify_is_perm_event(mask)) 5767b1f6417SJan Kara fsnotify_queue_overflow(group); 577f37650f1SMiklos Szeredi goto finish; 5787b1f6417SJan Kara } 5797053aee2SJan Kara 5807053aee2SJan Kara fsn_event = &event->fse; 5818ba8fa91SJan Kara ret = fsnotify_add_event(group, fsn_event, fanotify_merge); 58283c0e1b4SJan Kara if (ret) { 583482ef06cSJan Kara /* Permission events shouldn't be merged */ 58423c9deebSAmir Goldstein BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS); 5857053aee2SJan Kara /* Our event wasn't used in the end. Free it. */ 5867053aee2SJan Kara fsnotify_destroy_event(group, fsn_event); 587482ef06cSJan Kara 588f37650f1SMiklos Szeredi ret = 0; 5896685df31SMiklos Szeredi } else if (fanotify_is_perm_event(mask)) { 5907088f357SJan Kara ret = fanotify_get_response(group, FANOTIFY_PERM(event), 59105f0e387SJan Kara iter_info); 59285816794SJan Kara } 593f37650f1SMiklos Szeredi finish: 5946685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) 595f37650f1SMiklos Szeredi fsnotify_finish_user_wait(iter_info); 5966685df31SMiklos Szeredi 5977053aee2SJan Kara return ret; 5987053aee2SJan Kara } 5997053aee2SJan Kara 6004afeff85SEric Paris static void fanotify_free_group_priv(struct fsnotify_group *group) 6014afeff85SEric Paris { 6024afeff85SEric Paris struct user_struct *user; 6034afeff85SEric Paris 6044afeff85SEric Paris user = group->fanotify_data.user; 6054afeff85SEric Paris atomic_dec(&user->fanotify_listeners); 6064afeff85SEric Paris free_uid(user); 6074afeff85SEric Paris } 6084afeff85SEric Paris 6097088f357SJan Kara static void fanotify_free_path_event(struct fanotify_event *event) 6107088f357SJan Kara { 6117088f357SJan Kara path_put(fanotify_event_path(event)); 6127088f357SJan Kara kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event)); 6137088f357SJan Kara } 6147088f357SJan Kara 6157088f357SJan Kara static void fanotify_free_perm_event(struct fanotify_event *event) 6167088f357SJan Kara { 6177088f357SJan Kara path_put(fanotify_event_path(event)); 6187088f357SJan Kara kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event)); 6197088f357SJan Kara } 6207088f357SJan Kara 6217088f357SJan Kara static void fanotify_free_fid_event(struct fanotify_event *event) 6227088f357SJan Kara { 6237088f357SJan Kara struct fanotify_fid_event *ffe = FANOTIFY_FE(event); 6247088f357SJan Kara 6257088f357SJan Kara if (fanotify_fh_has_ext_buf(&ffe->object_fh)) 6267088f357SJan Kara kfree(fanotify_fh_ext_buf(&ffe->object_fh)); 6277088f357SJan Kara kmem_cache_free(fanotify_fid_event_cachep, ffe); 6287088f357SJan Kara } 6297088f357SJan Kara 630cacfb956SAmir Goldstein static void fanotify_free_name_event(struct fanotify_event *event) 631cacfb956SAmir Goldstein { 632cacfb956SAmir Goldstein struct fanotify_name_event *fne = FANOTIFY_NE(event); 633cacfb956SAmir Goldstein 634cacfb956SAmir Goldstein if (fanotify_fh_has_ext_buf(&fne->dir_fh)) 635cacfb956SAmir Goldstein kfree(fanotify_fh_ext_buf(&fne->dir_fh)); 636cacfb956SAmir Goldstein kfree(fne); 637cacfb956SAmir Goldstein } 638cacfb956SAmir Goldstein 6397053aee2SJan Kara static void fanotify_free_event(struct fsnotify_event *fsn_event) 6407053aee2SJan Kara { 64133913997SAmir Goldstein struct fanotify_event *event; 6427053aee2SJan Kara 6437053aee2SJan Kara event = FANOTIFY_E(fsn_event); 644d0a6a87eSAmir Goldstein put_pid(event->pid); 6457088f357SJan Kara switch (event->type) { 6467088f357SJan Kara case FANOTIFY_EVENT_TYPE_PATH: 6477088f357SJan Kara fanotify_free_path_event(event); 6487088f357SJan Kara break; 6497088f357SJan Kara case FANOTIFY_EVENT_TYPE_PATH_PERM: 6507088f357SJan Kara fanotify_free_perm_event(event); 6517088f357SJan Kara break; 6527088f357SJan Kara case FANOTIFY_EVENT_TYPE_FID: 6537088f357SJan Kara fanotify_free_fid_event(event); 6547088f357SJan Kara break; 655cacfb956SAmir Goldstein case FANOTIFY_EVENT_TYPE_FID_NAME: 656cacfb956SAmir Goldstein fanotify_free_name_event(event); 657cacfb956SAmir Goldstein break; 658b8a6c3a2SAmir Goldstein case FANOTIFY_EVENT_TYPE_OVERFLOW: 659b8a6c3a2SAmir Goldstein kfree(event); 660b8a6c3a2SAmir Goldstein break; 6617088f357SJan Kara default: 6627088f357SJan Kara WARN_ON_ONCE(1); 663f083441bSJan Kara } 6647053aee2SJan Kara } 6657053aee2SJan Kara 666054c636eSJan Kara static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) 667054c636eSJan Kara { 668054c636eSJan Kara kmem_cache_free(fanotify_mark_cache, fsn_mark); 669054c636eSJan Kara } 670054c636eSJan Kara 671ff0b16a9SEric Paris const struct fsnotify_ops fanotify_fsnotify_ops = { 672ff0b16a9SEric Paris .handle_event = fanotify_handle_event, 6734afeff85SEric Paris .free_group_priv = fanotify_free_group_priv, 6747053aee2SJan Kara .free_event = fanotify_free_event, 675054c636eSJan Kara .free_mark = fanotify_free_mark, 676ff0b16a9SEric Paris }; 677