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> 16ff0b16a9SEric Paris 177053aee2SJan Kara #include "fanotify.h" 18767cd46cSEric Paris 197053aee2SJan Kara static bool should_merge(struct fsnotify_event *old_fsn, 207053aee2SJan Kara struct fsnotify_event *new_fsn) 217053aee2SJan Kara { 227053aee2SJan Kara struct fanotify_event_info *old, *new; 237053aee2SJan Kara 247053aee2SJan Kara pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); 257053aee2SJan Kara old = FANOTIFY_E(old_fsn); 267053aee2SJan Kara new = FANOTIFY_E(new_fsn); 277053aee2SJan Kara 28d0a6a87eSAmir Goldstein if (old_fsn->inode == new_fsn->inode && old->pid == new->pid && 297053aee2SJan Kara old->path.mnt == new->path.mnt && 307053aee2SJan Kara old->path.dentry == new->path.dentry) 31767cd46cSEric Paris return true; 32767cd46cSEric Paris return false; 33767cd46cSEric Paris } 34767cd46cSEric Paris 35f70ab54cSEric Paris /* and the list better be locked by something too! */ 3683c0e1b4SJan Kara static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) 37767cd46cSEric Paris { 387053aee2SJan Kara struct fsnotify_event *test_event; 39767cd46cSEric Paris 40767cd46cSEric Paris pr_debug("%s: list=%p event=%p\n", __func__, list, event); 41767cd46cSEric Paris 4213116dfdSJan Kara /* 4313116dfdSJan Kara * Don't merge a permission event with any other event so that we know 4413116dfdSJan Kara * the event structure we have created in fanotify_handle_event() is the 4513116dfdSJan Kara * one we should check for permission response. 4613116dfdSJan Kara */ 476685df31SMiklos Szeredi if (fanotify_is_perm_event(event->mask)) 4883c0e1b4SJan Kara return 0; 4913116dfdSJan Kara 507053aee2SJan Kara list_for_each_entry_reverse(test_event, list, list) { 517053aee2SJan Kara if (should_merge(test_event, event)) { 529dced01aSEric Paris test_event->mask |= event->mask; 5383c0e1b4SJan Kara return 1; 549dced01aSEric Paris } 556c71100dSKinglong Mee } 566c71100dSKinglong Mee 576c71100dSKinglong Mee return 0; 586c71100dSKinglong Mee } 599dced01aSEric Paris 60f083441bSJan Kara static int fanotify_get_response(struct fsnotify_group *group, 6105f0e387SJan Kara struct fanotify_perm_event_info *event, 6205f0e387SJan Kara struct fsnotify_iter_info *iter_info) 639e66e423SEric Paris { 649e66e423SEric Paris int ret; 659e66e423SEric Paris 669e66e423SEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 679e66e423SEric Paris 6896d41019SJan Kara wait_event(group->fanotify_data.access_waitq, event->response); 699e66e423SEric Paris 709e66e423SEric Paris /* userspace responded, convert to something usable */ 71de8cd83eSSteve Grubb switch (event->response & ~FAN_AUDIT) { 729e66e423SEric Paris case FAN_ALLOW: 739e66e423SEric Paris ret = 0; 749e66e423SEric Paris break; 759e66e423SEric Paris case FAN_DENY: 769e66e423SEric Paris default: 779e66e423SEric Paris ret = -EPERM; 789e66e423SEric Paris } 79de8cd83eSSteve Grubb 80de8cd83eSSteve Grubb /* Check if the response should be audited */ 81de8cd83eSSteve Grubb if (event->response & FAN_AUDIT) 82de8cd83eSSteve Grubb audit_fanotify(event->response & ~FAN_AUDIT); 83de8cd83eSSteve Grubb 849e66e423SEric Paris event->response = 0; 859e66e423SEric Paris 86b2d87909SEric Paris pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, 87b2d87909SEric Paris group, event, ret); 88b2d87909SEric Paris 899e66e423SEric Paris return ret; 909e66e423SEric Paris } 919e66e423SEric Paris 922d10b230SMatthew Bobrowski /* 932d10b230SMatthew Bobrowski * This function returns a mask for an event that only contains the flags 942d10b230SMatthew Bobrowski * that have been specifically requested by the user. Flags that may have 952d10b230SMatthew Bobrowski * been included within the event mask, but have not been explicitly 962d10b230SMatthew Bobrowski * requested by the user, will not be present in the returned mask. 972d10b230SMatthew Bobrowski */ 982d10b230SMatthew Bobrowski static u32 fanotify_group_event_mask(struct fsnotify_iter_info *iter_info, 995b0457adSAmir Goldstein u32 event_mask, const void *data, 1005b0457adSAmir Goldstein int data_type) 1011c529063SEric Paris { 10254a307baSAmir Goldstein __u32 marks_mask = 0, marks_ignored_mask = 0; 1033cd5eca8SAl Viro const struct path *path = data; 104837a3934SAmir Goldstein struct fsnotify_mark *mark; 105837a3934SAmir Goldstein int type; 1061968f5eeSEric Paris 107837a3934SAmir Goldstein pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", 108837a3934SAmir Goldstein __func__, iter_info->report_mask, event_mask, data, data_type); 1091968f5eeSEric Paris 1102d10b230SMatthew Bobrowski /* If we don't have enough info to send an event to userspace say no */ 1112069601bSLinus Torvalds if (data_type != FSNOTIFY_EVENT_PATH) 1122d10b230SMatthew Bobrowski return 0; 1131c529063SEric Paris 1142d10b230SMatthew Bobrowski /* Sorry, fanotify only gives a damn about files and dirs */ 115e36cb0b8SDavid Howells if (!d_is_reg(path->dentry) && 11654f2a2f4SDavid Howells !d_can_lookup(path->dentry)) 1172d10b230SMatthew Bobrowski return 0; 118e1c048baSEric Paris 119837a3934SAmir Goldstein fsnotify_foreach_obj_type(type) { 120837a3934SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 121837a3934SAmir Goldstein continue; 122837a3934SAmir Goldstein mark = iter_info->marks[type]; 1231968f5eeSEric Paris /* 124b469e7e4SAmir Goldstein * If the event is for a child and this mark doesn't care about 125b469e7e4SAmir Goldstein * events on a child, don't send it! 1261968f5eeSEric Paris */ 127b469e7e4SAmir Goldstein if (event_mask & FS_EVENT_ON_CHILD && 128b469e7e4SAmir Goldstein (type != FSNOTIFY_OBJ_TYPE_INODE || 129b469e7e4SAmir Goldstein !(mark->mask & FS_EVENT_ON_CHILD))) 130837a3934SAmir Goldstein continue; 13154a307baSAmir Goldstein 132837a3934SAmir Goldstein marks_mask |= mark->mask; 133837a3934SAmir Goldstein marks_ignored_mask |= mark->ignored_mask; 1341968f5eeSEric Paris } 1351968f5eeSEric Paris 136e36cb0b8SDavid Howells if (d_is_dir(path->dentry) && 13766ba93c0SLino Sanfilippo !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) 1382d10b230SMatthew Bobrowski return 0; 1398fcd6528SEric Paris 1402d10b230SMatthew Bobrowski return event_mask & FANOTIFY_OUTGOING_EVENTS & marks_mask & 1412d10b230SMatthew Bobrowski ~marks_ignored_mask; 1421c529063SEric Paris } 1431c529063SEric Paris 1441f5eaa90SJan Kara struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, 1451f5eaa90SJan Kara struct inode *inode, u32 mask, 1463cd5eca8SAl Viro const struct path *path) 147f083441bSJan Kara { 148d46eb14bSShakeel Butt struct fanotify_event_info *event = NULL; 149d46eb14bSShakeel Butt gfp_t gfp = GFP_KERNEL_ACCOUNT; 1501f5eaa90SJan Kara 1511f5eaa90SJan Kara /* 1521f5eaa90SJan Kara * For queues with unlimited length lost events are not expected and 1531f5eaa90SJan Kara * can possibly have security implications. Avoid losing events when 1541f5eaa90SJan Kara * memory is short. 1551f5eaa90SJan Kara */ 1561f5eaa90SJan Kara if (group->max_events == UINT_MAX) 1571f5eaa90SJan Kara gfp |= __GFP_NOFAIL; 158f083441bSJan Kara 159d46eb14bSShakeel Butt /* Whoever is interested in the event, pays for the allocation. */ 160d46eb14bSShakeel Butt memalloc_use_memcg(group->memcg); 161d46eb14bSShakeel Butt 1626685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 163f083441bSJan Kara struct fanotify_perm_event_info *pevent; 164f083441bSJan Kara 1651f5eaa90SJan Kara pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); 166f083441bSJan Kara if (!pevent) 167d46eb14bSShakeel Butt goto out; 168f083441bSJan Kara event = &pevent->fae; 169f083441bSJan Kara pevent->response = 0; 170f083441bSJan Kara goto init; 171f083441bSJan Kara } 1721f5eaa90SJan Kara event = kmem_cache_alloc(fanotify_event_cachep, gfp); 173f083441bSJan Kara if (!event) 174d46eb14bSShakeel Butt goto out; 175f083441bSJan Kara init: __maybe_unused 176f083441bSJan Kara fsnotify_init_event(&event->fse, inode, mask); 177d0a6a87eSAmir Goldstein if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) 178d0a6a87eSAmir Goldstein event->pid = get_pid(task_pid(current)); 179d0a6a87eSAmir Goldstein else 180d0a6a87eSAmir Goldstein event->pid = get_pid(task_tgid(current)); 181f083441bSJan Kara if (path) { 182f083441bSJan Kara event->path = *path; 183f083441bSJan Kara path_get(&event->path); 184f083441bSJan Kara } else { 185f083441bSJan Kara event->path.mnt = NULL; 186f083441bSJan Kara event->path.dentry = NULL; 187f083441bSJan Kara } 188d46eb14bSShakeel Butt out: 189d46eb14bSShakeel Butt memalloc_unuse_memcg(); 190f083441bSJan Kara return event; 191f083441bSJan Kara } 192f083441bSJan Kara 1937053aee2SJan Kara static int fanotify_handle_event(struct fsnotify_group *group, 1947053aee2SJan Kara struct inode *inode, 1953cd5eca8SAl Viro u32 mask, const void *data, int data_type, 1969385a84dSJan Kara const unsigned char *file_name, u32 cookie, 1979385a84dSJan Kara struct fsnotify_iter_info *iter_info) 1987053aee2SJan Kara { 1997053aee2SJan Kara int ret = 0; 2007053aee2SJan Kara struct fanotify_event_info *event; 2017053aee2SJan Kara struct fsnotify_event *fsn_event; 2027053aee2SJan Kara 2037053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); 2047053aee2SJan Kara BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); 2057053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 2067053aee2SJan Kara BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); 2077053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN != FS_OPEN); 2087053aee2SJan Kara BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); 2097053aee2SJan Kara BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); 2107053aee2SJan Kara BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); 2117053aee2SJan Kara BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); 2127053aee2SJan Kara BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); 2139b076f1cSMatthew Bobrowski BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC); 214*66917a31SMatthew Bobrowski BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); 2157053aee2SJan Kara 216*66917a31SMatthew Bobrowski BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 12); 217bdd5a46fSAmir Goldstein 2182d10b230SMatthew Bobrowski mask = fanotify_group_event_mask(iter_info, mask, data, data_type); 2192d10b230SMatthew Bobrowski if (!mask) 22083c4c4b0SJan Kara return 0; 22183c4c4b0SJan Kara 2227053aee2SJan Kara pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, 2237053aee2SJan Kara mask); 2247053aee2SJan Kara 2256685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) { 226f37650f1SMiklos Szeredi /* 227f37650f1SMiklos Szeredi * fsnotify_prepare_user_wait() fails if we race with mark 228f37650f1SMiklos Szeredi * deletion. Just let the operation pass in that case. 229f37650f1SMiklos Szeredi */ 230f37650f1SMiklos Szeredi if (!fsnotify_prepare_user_wait(iter_info)) 231f37650f1SMiklos Szeredi return 0; 232f37650f1SMiklos Szeredi } 233f37650f1SMiklos Szeredi 2341f5eaa90SJan Kara event = fanotify_alloc_event(group, inode, mask, data); 235f37650f1SMiklos Szeredi ret = -ENOMEM; 2367b1f6417SJan Kara if (unlikely(!event)) { 2377b1f6417SJan Kara /* 2387b1f6417SJan Kara * We don't queue overflow events for permission events as 2397b1f6417SJan Kara * there the access is denied and so no event is in fact lost. 2407b1f6417SJan Kara */ 2417b1f6417SJan Kara if (!fanotify_is_perm_event(mask)) 2427b1f6417SJan Kara fsnotify_queue_overflow(group); 243f37650f1SMiklos Szeredi goto finish; 2447b1f6417SJan Kara } 2457053aee2SJan Kara 2467053aee2SJan Kara fsn_event = &event->fse; 2478ba8fa91SJan Kara ret = fsnotify_add_event(group, fsn_event, fanotify_merge); 24883c0e1b4SJan Kara if (ret) { 249482ef06cSJan Kara /* Permission events shouldn't be merged */ 25023c9deebSAmir Goldstein BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS); 2517053aee2SJan Kara /* Our event wasn't used in the end. Free it. */ 2527053aee2SJan Kara fsnotify_destroy_event(group, fsn_event); 253482ef06cSJan Kara 254f37650f1SMiklos Szeredi ret = 0; 2556685df31SMiklos Szeredi } else if (fanotify_is_perm_event(mask)) { 25605f0e387SJan Kara ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event), 25705f0e387SJan Kara iter_info); 25885816794SJan Kara fsnotify_destroy_event(group, fsn_event); 25985816794SJan Kara } 260f37650f1SMiklos Szeredi finish: 2616685df31SMiklos Szeredi if (fanotify_is_perm_event(mask)) 262f37650f1SMiklos Szeredi fsnotify_finish_user_wait(iter_info); 2636685df31SMiklos Szeredi 2647053aee2SJan Kara return ret; 2657053aee2SJan Kara } 2667053aee2SJan Kara 2674afeff85SEric Paris static void fanotify_free_group_priv(struct fsnotify_group *group) 2684afeff85SEric Paris { 2694afeff85SEric Paris struct user_struct *user; 2704afeff85SEric Paris 2714afeff85SEric Paris user = group->fanotify_data.user; 2724afeff85SEric Paris atomic_dec(&user->fanotify_listeners); 2734afeff85SEric Paris free_uid(user); 2744afeff85SEric Paris } 2754afeff85SEric Paris 2767053aee2SJan Kara static void fanotify_free_event(struct fsnotify_event *fsn_event) 2777053aee2SJan Kara { 2787053aee2SJan Kara struct fanotify_event_info *event; 2797053aee2SJan Kara 2807053aee2SJan Kara event = FANOTIFY_E(fsn_event); 2817053aee2SJan Kara path_put(&event->path); 282d0a6a87eSAmir Goldstein put_pid(event->pid); 2836685df31SMiklos Szeredi if (fanotify_is_perm_event(fsn_event->mask)) { 284f083441bSJan Kara kmem_cache_free(fanotify_perm_event_cachep, 285f083441bSJan Kara FANOTIFY_PE(fsn_event)); 286f083441bSJan Kara return; 287f083441bSJan Kara } 2887053aee2SJan Kara kmem_cache_free(fanotify_event_cachep, event); 2897053aee2SJan Kara } 2907053aee2SJan Kara 291054c636eSJan Kara static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) 292054c636eSJan Kara { 293054c636eSJan Kara kmem_cache_free(fanotify_mark_cache, fsn_mark); 294054c636eSJan Kara } 295054c636eSJan Kara 296ff0b16a9SEric Paris const struct fsnotify_ops fanotify_fsnotify_ops = { 297ff0b16a9SEric Paris .handle_event = fanotify_handle_event, 2984afeff85SEric Paris .free_group_priv = fanotify_free_group_priv, 2997053aee2SJan Kara .free_event = fanotify_free_event, 300054c636eSJan Kara .free_mark = fanotify_free_mark, 301ff0b16a9SEric Paris }; 302