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> 11*7a36094dSEric 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> 15ff0b16a9SEric Paris 167053aee2SJan Kara #include "fanotify.h" 17767cd46cSEric Paris 187053aee2SJan Kara static bool should_merge(struct fsnotify_event *old_fsn, 197053aee2SJan Kara struct fsnotify_event *new_fsn) 207053aee2SJan Kara { 217053aee2SJan Kara struct fanotify_event_info *old, *new; 227053aee2SJan Kara 237053aee2SJan Kara pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); 247053aee2SJan Kara old = FANOTIFY_E(old_fsn); 257053aee2SJan Kara new = FANOTIFY_E(new_fsn); 267053aee2SJan Kara 277053aee2SJan Kara if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid && 287053aee2SJan Kara old->path.mnt == new->path.mnt && 297053aee2SJan Kara old->path.dentry == new->path.dentry) 30767cd46cSEric Paris return true; 31767cd46cSEric Paris return false; 32767cd46cSEric Paris } 33767cd46cSEric Paris 34f70ab54cSEric Paris /* and the list better be locked by something too! */ 3583c0e1b4SJan Kara static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) 36767cd46cSEric Paris { 377053aee2SJan Kara struct fsnotify_event *test_event; 38767cd46cSEric Paris 39767cd46cSEric Paris pr_debug("%s: list=%p event=%p\n", __func__, list, event); 40767cd46cSEric Paris 4113116dfdSJan Kara /* 4213116dfdSJan Kara * Don't merge a permission event with any other event so that we know 4313116dfdSJan Kara * the event structure we have created in fanotify_handle_event() is the 4413116dfdSJan Kara * one we should check for permission response. 4513116dfdSJan Kara */ 466685df31SMiklos Szeredi if (fanotify_is_perm_event(event->mask)) 4783c0e1b4SJan Kara return 0; 4813116dfdSJan Kara 497053aee2SJan Kara list_for_each_entry_reverse(test_event, list, list) { 507053aee2SJan Kara if (should_merge(test_event, event)) { 519dced01aSEric Paris test_event->mask |= event->mask; 5283c0e1b4SJan Kara return 1; 539dced01aSEric Paris } 546c71100dSKinglong Mee } 556c71100dSKinglong Mee 566c71100dSKinglong Mee return 0; 576c71100dSKinglong Mee } 589dced01aSEric Paris 59f083441bSJan Kara static int fanotify_get_response(struct fsnotify_group *group, 6005f0e387SJan Kara struct fanotify_perm_event_info *event, 6105f0e387SJan Kara struct fsnotify_iter_info *iter_info) 629e66e423SEric Paris { 639e66e423SEric Paris int ret; 649e66e423SEric Paris 659e66e423SEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 669e66e423SEric Paris 6796d41019SJan Kara wait_event(group->fanotify_data.access_waitq, event->response); 689e66e423SEric Paris 699e66e423SEric Paris /* userspace responded, convert to something usable */ 70de8cd83eSSteve Grubb switch (event->response & ~FAN_AUDIT) { 719e66e423SEric Paris case FAN_ALLOW: 729e66e423SEric Paris ret = 0; 739e66e423SEric Paris break; 749e66e423SEric Paris case FAN_DENY: 759e66e423SEric Paris default: 769e66e423SEric Paris ret = -EPERM; 779e66e423SEric Paris } 78de8cd83eSSteve Grubb 79de8cd83eSSteve Grubb /* Check if the response should be audited */ 80de8cd83eSSteve Grubb if (event->response & FAN_AUDIT) 81de8cd83eSSteve Grubb audit_fanotify(event->response & ~FAN_AUDIT); 82de8cd83eSSteve Grubb 839e66e423SEric Paris event->response = 0; 849e66e423SEric Paris 85b2d87909SEric Paris pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, 86b2d87909SEric Paris group, event, ret); 87b2d87909SEric Paris 889e66e423SEric Paris return ret; 899e66e423SEric Paris } 909e66e423SEric Paris 915b0457adSAmir Goldstein static bool fanotify_should_send_event(struct fsnotify_iter_info *iter_info, 925b0457adSAmir Goldstein u32 event_mask, const void *data, 935b0457adSAmir Goldstein int data_type) 941c529063SEric Paris { 9554a307baSAmir Goldstein __u32 marks_mask = 0, marks_ignored_mask = 0; 963cd5eca8SAl Viro const struct path *path = data; 97837a3934SAmir Goldstein struct fsnotify_mark *mark; 98837a3934SAmir Goldstein int type; 991968f5eeSEric Paris 100837a3934SAmir Goldstein pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", 101837a3934SAmir Goldstein __func__, iter_info->report_mask, event_mask, data, data_type); 1021968f5eeSEric Paris 1031c529063SEric Paris /* if we don't have enough info to send an event to userspace say no */ 1042069601bSLinus Torvalds if (data_type != FSNOTIFY_EVENT_PATH) 1051c529063SEric Paris return false; 1061c529063SEric Paris 107e1c048baSEric Paris /* sorry, fanotify only gives a damn about files and dirs */ 108e36cb0b8SDavid Howells if (!d_is_reg(path->dentry) && 10954f2a2f4SDavid Howells !d_can_lookup(path->dentry)) 110e1c048baSEric Paris return false; 111e1c048baSEric Paris 112837a3934SAmir Goldstein fsnotify_foreach_obj_type(type) { 113837a3934SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 114837a3934SAmir Goldstein continue; 115837a3934SAmir Goldstein mark = iter_info->marks[type]; 1161968f5eeSEric Paris /* 1171968f5eeSEric Paris * if the event is for a child and this inode doesn't care about 1181968f5eeSEric Paris * events on the child, don't send it! 1191968f5eeSEric Paris */ 120837a3934SAmir Goldstein if (type == FSNOTIFY_OBJ_TYPE_INODE && 121837a3934SAmir Goldstein (event_mask & FS_EVENT_ON_CHILD) && 122837a3934SAmir Goldstein !(mark->mask & FS_EVENT_ON_CHILD)) 123837a3934SAmir Goldstein continue; 12454a307baSAmir Goldstein 125837a3934SAmir Goldstein marks_mask |= mark->mask; 126837a3934SAmir Goldstein marks_ignored_mask |= mark->ignored_mask; 1271968f5eeSEric Paris } 1281968f5eeSEric Paris 129e36cb0b8SDavid Howells if (d_is_dir(path->dentry) && 13066ba93c0SLino Sanfilippo !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) 1318fcd6528SEric Paris return false; 1328fcd6528SEric Paris 133b3c1030dSSuzuki K. Poulose if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask & 134b3c1030dSSuzuki K. Poulose ~marks_ignored_mask) 1351968f5eeSEric Paris return true; 1361968f5eeSEric Paris 1371968f5eeSEric Paris return false; 1381c529063SEric Paris } 1391c529063SEric Paris 1401f5eaa90SJan Kara struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, 1411f5eaa90SJan Kara struct inode *inode, u32 mask, 1423cd5eca8SAl Viro const struct path *path) 143f083441bSJan Kara { 144f083441bSJan Kara struct fanotify_event_info *event; 1451f5eaa90SJan Kara gfp_t gfp = GFP_KERNEL; 1461f5eaa90SJan Kara 1471f5eaa90SJan Kara /* 1481f5eaa90SJan Kara * For queues with unlimited length lost events are not expected and 1491f5eaa90SJan Kara * can possibly have security implications. Avoid losing events when 1501f5eaa90SJan Kara * memory is short. 1511f5eaa90SJan Kara */ 1521f5eaa90SJan Kara if (group->max_events == UINT_MAX) 1531f5eaa90SJan Kara gfp |= __GFP_NOFAIL; 154f083441bSJan Kara 1556685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 156f083441bSJan Kara struct fanotify_perm_event_info *pevent; 157f083441bSJan Kara 1581f5eaa90SJan Kara pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); 159f083441bSJan Kara if (!pevent) 160f083441bSJan Kara return NULL; 161f083441bSJan Kara event = &pevent->fae; 162f083441bSJan Kara pevent->response = 0; 163f083441bSJan Kara goto init; 164f083441bSJan Kara } 1651f5eaa90SJan Kara event = kmem_cache_alloc(fanotify_event_cachep, gfp); 166f083441bSJan Kara if (!event) 167f083441bSJan Kara return NULL; 168f083441bSJan Kara init: __maybe_unused 169f083441bSJan Kara fsnotify_init_event(&event->fse, inode, mask); 170f083441bSJan Kara event->tgid = get_pid(task_tgid(current)); 171f083441bSJan Kara if (path) { 172f083441bSJan Kara event->path = *path; 173f083441bSJan Kara path_get(&event->path); 174f083441bSJan Kara } else { 175f083441bSJan Kara event->path.mnt = NULL; 176f083441bSJan Kara event->path.dentry = NULL; 177f083441bSJan Kara } 178f083441bSJan Kara return event; 179f083441bSJan Kara } 180f083441bSJan Kara 1817053aee2SJan Kara static int fanotify_handle_event(struct fsnotify_group *group, 1827053aee2SJan Kara struct inode *inode, 1833cd5eca8SAl Viro u32 mask, const void *data, int data_type, 1849385a84dSJan Kara const unsigned char *file_name, u32 cookie, 1859385a84dSJan Kara struct fsnotify_iter_info *iter_info) 1867053aee2SJan Kara { 1877053aee2SJan Kara int ret = 0; 1887053aee2SJan Kara struct fanotify_event_info *event; 1897053aee2SJan Kara struct fsnotify_event *fsn_event; 1907053aee2SJan Kara 1917053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); 1927053aee2SJan Kara BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); 1937053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 1947053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); 1957053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN != FS_OPEN); 1967053aee2SJan Kara BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); 1977053aee2SJan Kara BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); 1987053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); 1997053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); 2007053aee2SJan Kara BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); 2017053aee2SJan Kara 2025b0457adSAmir Goldstein if (!fanotify_should_send_event(iter_info, mask, data, data_type)) 20383c4c4b0SJan Kara return 0; 20483c4c4b0SJan Kara 2057053aee2SJan Kara pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, 2067053aee2SJan Kara mask); 2077053aee2SJan Kara 2086685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 209f37650f1SMiklos Szeredi /* 210f37650f1SMiklos Szeredi * fsnotify_prepare_user_wait() fails if we race with mark 211f37650f1SMiklos Szeredi * deletion. Just let the operation pass in that case. 212f37650f1SMiklos Szeredi */ 213f37650f1SMiklos Szeredi if (!fsnotify_prepare_user_wait(iter_info)) 214f37650f1SMiklos Szeredi return 0; 215f37650f1SMiklos Szeredi } 216f37650f1SMiklos Szeredi 2171f5eaa90SJan Kara event = fanotify_alloc_event(group, inode, mask, data); 218f37650f1SMiklos Szeredi ret = -ENOMEM; 2197b1f6417SJan Kara if (unlikely(!event)) { 2207b1f6417SJan Kara /* 2217b1f6417SJan Kara * We don't queue overflow events for permission events as 2227b1f6417SJan Kara * there the access is denied and so no event is in fact lost. 2237b1f6417SJan Kara */ 2247b1f6417SJan Kara if (!fanotify_is_perm_event(mask)) 2257b1f6417SJan Kara fsnotify_queue_overflow(group); 226f37650f1SMiklos Szeredi goto finish; 2277b1f6417SJan Kara } 2287053aee2SJan Kara 2297053aee2SJan Kara fsn_event = &event->fse; 2308ba8fa91SJan Kara ret = fsnotify_add_event(group, fsn_event, fanotify_merge); 23183c0e1b4SJan Kara if (ret) { 232482ef06cSJan Kara /* Permission events shouldn't be merged */ 233482ef06cSJan Kara BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS); 2347053aee2SJan Kara /* Our event wasn't used in the end. Free it. */ 2357053aee2SJan Kara fsnotify_destroy_event(group, fsn_event); 236482ef06cSJan Kara 237f37650f1SMiklos Szeredi ret = 0; 2386685df31SMiklos Szeredi } else if (fanotify_is_perm_event(mask)) { 23905f0e387SJan Kara ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event), 24005f0e387SJan Kara iter_info); 24185816794SJan Kara fsnotify_destroy_event(group, fsn_event); 24285816794SJan Kara } 243f37650f1SMiklos Szeredi finish: 2446685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) 245f37650f1SMiklos Szeredi fsnotify_finish_user_wait(iter_info); 2466685df31SMiklos Szeredi 2477053aee2SJan Kara return ret; 2487053aee2SJan Kara } 2497053aee2SJan Kara 2504afeff85SEric Paris static void fanotify_free_group_priv(struct fsnotify_group *group) 2514afeff85SEric Paris { 2524afeff85SEric Paris struct user_struct *user; 2534afeff85SEric Paris 2544afeff85SEric Paris user = group->fanotify_data.user; 2554afeff85SEric Paris atomic_dec(&user->fanotify_listeners); 2564afeff85SEric Paris free_uid(user); 2574afeff85SEric Paris } 2584afeff85SEric Paris 2597053aee2SJan Kara static void fanotify_free_event(struct fsnotify_event *fsn_event) 2607053aee2SJan Kara { 2617053aee2SJan Kara struct fanotify_event_info *event; 2627053aee2SJan Kara 2637053aee2SJan Kara event = FANOTIFY_E(fsn_event); 2647053aee2SJan Kara path_put(&event->path); 2657053aee2SJan Kara put_pid(event->tgid); 2666685df31SMiklos Szeredi if (fanotify_is_perm_event(fsn_event->mask)) { 267f083441bSJan Kara kmem_cache_free(fanotify_perm_event_cachep, 268f083441bSJan Kara FANOTIFY_PE(fsn_event)); 269f083441bSJan Kara return; 270f083441bSJan Kara } 2717053aee2SJan Kara kmem_cache_free(fanotify_event_cachep, event); 2727053aee2SJan Kara } 2737053aee2SJan Kara 274054c636eSJan Kara static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) 275054c636eSJan Kara { 276054c636eSJan Kara kmem_cache_free(fanotify_mark_cache, fsn_mark); 277054c636eSJan Kara } 278054c636eSJan Kara 279ff0b16a9SEric Paris const struct fsnotify_ops fanotify_fsnotify_ops = { 280ff0b16a9SEric Paris .handle_event = fanotify_handle_event, 2814afeff85SEric Paris .free_group_priv = fanotify_free_group_priv, 2827053aee2SJan Kara .free_event = fanotify_free_event, 283054c636eSJan Kara .free_mark = fanotify_free_mark, 284ff0b16a9SEric Paris }; 285