1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/fanotify.h> 3 #include <linux/fdtable.h> 4 #include <linux/fsnotify_backend.h> 5 #include <linux/init.h> 6 #include <linux/jiffies.h> 7 #include <linux/kernel.h> /* UINT_MAX */ 8 #include <linux/mount.h> 9 #include <linux/sched.h> 10 #include <linux/sched/user.h> 11 #include <linux/sched/signal.h> 12 #include <linux/types.h> 13 #include <linux/wait.h> 14 #include <linux/audit.h> 15 #include <linux/sched/mm.h> 16 17 #include "fanotify.h" 18 19 static bool should_merge(struct fsnotify_event *old_fsn, 20 struct fsnotify_event *new_fsn) 21 { 22 struct fanotify_event_info *old, *new; 23 24 pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); 25 old = FANOTIFY_E(old_fsn); 26 new = FANOTIFY_E(new_fsn); 27 28 if (old_fsn->inode == new_fsn->inode && old->pid == new->pid && 29 old->path.mnt == new->path.mnt && 30 old->path.dentry == new->path.dentry) 31 return true; 32 return false; 33 } 34 35 /* and the list better be locked by something too! */ 36 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) 37 { 38 struct fsnotify_event *test_event; 39 40 pr_debug("%s: list=%p event=%p\n", __func__, list, event); 41 42 /* 43 * Don't merge a permission event with any other event so that we know 44 * the event structure we have created in fanotify_handle_event() is the 45 * one we should check for permission response. 46 */ 47 if (fanotify_is_perm_event(event->mask)) 48 return 0; 49 50 list_for_each_entry_reverse(test_event, list, list) { 51 if (should_merge(test_event, event)) { 52 test_event->mask |= event->mask; 53 return 1; 54 } 55 } 56 57 return 0; 58 } 59 60 static int fanotify_get_response(struct fsnotify_group *group, 61 struct fanotify_perm_event_info *event, 62 struct fsnotify_iter_info *iter_info) 63 { 64 int ret; 65 66 pr_debug("%s: group=%p event=%p\n", __func__, group, event); 67 68 wait_event(group->fanotify_data.access_waitq, event->response); 69 70 /* userspace responded, convert to something usable */ 71 switch (event->response & ~FAN_AUDIT) { 72 case FAN_ALLOW: 73 ret = 0; 74 break; 75 case FAN_DENY: 76 default: 77 ret = -EPERM; 78 } 79 80 /* Check if the response should be audited */ 81 if (event->response & FAN_AUDIT) 82 audit_fanotify(event->response & ~FAN_AUDIT); 83 84 event->response = 0; 85 86 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, 87 group, event, ret); 88 89 return ret; 90 } 91 92 /* 93 * This function returns a mask for an event that only contains the flags 94 * that have been specifically requested by the user. Flags that may have 95 * been included within the event mask, but have not been explicitly 96 * requested by the user, will not be present in the returned mask. 97 */ 98 static u32 fanotify_group_event_mask(struct fsnotify_iter_info *iter_info, 99 u32 event_mask, const void *data, 100 int data_type) 101 { 102 __u32 marks_mask = 0, marks_ignored_mask = 0; 103 const struct path *path = data; 104 struct fsnotify_mark *mark; 105 int type; 106 107 pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", 108 __func__, iter_info->report_mask, event_mask, data, data_type); 109 110 /* If we don't have enough info to send an event to userspace say no */ 111 if (data_type != FSNOTIFY_EVENT_PATH) 112 return 0; 113 114 /* Sorry, fanotify only gives a damn about files and dirs */ 115 if (!d_is_reg(path->dentry) && 116 !d_can_lookup(path->dentry)) 117 return 0; 118 119 fsnotify_foreach_obj_type(type) { 120 if (!fsnotify_iter_should_report_type(iter_info, type)) 121 continue; 122 mark = iter_info->marks[type]; 123 /* 124 * If the event is for a child and this mark doesn't care about 125 * events on a child, don't send it! 126 */ 127 if (event_mask & FS_EVENT_ON_CHILD && 128 (type != FSNOTIFY_OBJ_TYPE_INODE || 129 !(mark->mask & FS_EVENT_ON_CHILD))) 130 continue; 131 132 marks_mask |= mark->mask; 133 marks_ignored_mask |= mark->ignored_mask; 134 } 135 136 if (d_is_dir(path->dentry) && 137 !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) 138 return 0; 139 140 return event_mask & FANOTIFY_OUTGOING_EVENTS & marks_mask & 141 ~marks_ignored_mask; 142 } 143 144 struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, 145 struct inode *inode, u32 mask, 146 const struct path *path) 147 { 148 struct fanotify_event_info *event = NULL; 149 gfp_t gfp = GFP_KERNEL_ACCOUNT; 150 151 /* 152 * For queues with unlimited length lost events are not expected and 153 * can possibly have security implications. Avoid losing events when 154 * memory is short. 155 */ 156 if (group->max_events == UINT_MAX) 157 gfp |= __GFP_NOFAIL; 158 159 /* Whoever is interested in the event, pays for the allocation. */ 160 memalloc_use_memcg(group->memcg); 161 162 if (fanotify_is_perm_event(mask)) { 163 struct fanotify_perm_event_info *pevent; 164 165 pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); 166 if (!pevent) 167 goto out; 168 event = &pevent->fae; 169 pevent->response = 0; 170 goto init; 171 } 172 event = kmem_cache_alloc(fanotify_event_cachep, gfp); 173 if (!event) 174 goto out; 175 init: __maybe_unused 176 fsnotify_init_event(&event->fse, inode, mask); 177 if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) 178 event->pid = get_pid(task_pid(current)); 179 else 180 event->pid = get_pid(task_tgid(current)); 181 if (path) { 182 event->path = *path; 183 path_get(&event->path); 184 } else { 185 event->path.mnt = NULL; 186 event->path.dentry = NULL; 187 } 188 out: 189 memalloc_unuse_memcg(); 190 return event; 191 } 192 193 static int fanotify_handle_event(struct fsnotify_group *group, 194 struct inode *inode, 195 u32 mask, const void *data, int data_type, 196 const unsigned char *file_name, u32 cookie, 197 struct fsnotify_iter_info *iter_info) 198 { 199 int ret = 0; 200 struct fanotify_event_info *event; 201 struct fsnotify_event *fsn_event; 202 203 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); 204 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); 205 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 206 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); 207 BUILD_BUG_ON(FAN_OPEN != FS_OPEN); 208 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); 209 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); 210 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); 211 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); 212 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); 213 BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC); 214 BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); 215 216 BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 12); 217 218 mask = fanotify_group_event_mask(iter_info, mask, data, data_type); 219 if (!mask) 220 return 0; 221 222 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, 223 mask); 224 225 if (fanotify_is_perm_event(mask)) { 226 /* 227 * fsnotify_prepare_user_wait() fails if we race with mark 228 * deletion. Just let the operation pass in that case. 229 */ 230 if (!fsnotify_prepare_user_wait(iter_info)) 231 return 0; 232 } 233 234 event = fanotify_alloc_event(group, inode, mask, data); 235 ret = -ENOMEM; 236 if (unlikely(!event)) { 237 /* 238 * We don't queue overflow events for permission events as 239 * there the access is denied and so no event is in fact lost. 240 */ 241 if (!fanotify_is_perm_event(mask)) 242 fsnotify_queue_overflow(group); 243 goto finish; 244 } 245 246 fsn_event = &event->fse; 247 ret = fsnotify_add_event(group, fsn_event, fanotify_merge); 248 if (ret) { 249 /* Permission events shouldn't be merged */ 250 BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS); 251 /* Our event wasn't used in the end. Free it. */ 252 fsnotify_destroy_event(group, fsn_event); 253 254 ret = 0; 255 } else if (fanotify_is_perm_event(mask)) { 256 ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event), 257 iter_info); 258 fsnotify_destroy_event(group, fsn_event); 259 } 260 finish: 261 if (fanotify_is_perm_event(mask)) 262 fsnotify_finish_user_wait(iter_info); 263 264 return ret; 265 } 266 267 static void fanotify_free_group_priv(struct fsnotify_group *group) 268 { 269 struct user_struct *user; 270 271 user = group->fanotify_data.user; 272 atomic_dec(&user->fanotify_listeners); 273 free_uid(user); 274 } 275 276 static void fanotify_free_event(struct fsnotify_event *fsn_event) 277 { 278 struct fanotify_event_info *event; 279 280 event = FANOTIFY_E(fsn_event); 281 path_put(&event->path); 282 put_pid(event->pid); 283 if (fanotify_is_perm_event(fsn_event->mask)) { 284 kmem_cache_free(fanotify_perm_event_cachep, 285 FANOTIFY_PE(fsn_event)); 286 return; 287 } 288 kmem_cache_free(fanotify_event_cachep, event); 289 } 290 291 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) 292 { 293 kmem_cache_free(fanotify_mark_cache, fsn_mark); 294 } 295 296 const struct fsnotify_ops fanotify_fsnotify_ops = { 297 .handle_event = fanotify_handle_event, 298 .free_group_priv = fanotify_free_group_priv, 299 .free_event = fanotify_free_event, 300 .free_mark = fanotify_free_mark, 301 }; 302