rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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/types.h> |
| 12 | #include <linux/wait.h> |
| 13 | |
| 14 | #include "fanotify.h" |
| 15 | |
| 16 | static bool should_merge(struct fsnotify_event *old_fsn, |
| 17 | struct fsnotify_event *new_fsn) |
| 18 | { |
| 19 | struct fanotify_event_info *old, *new; |
| 20 | |
| 21 | pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); |
| 22 | old = FANOTIFY_E(old_fsn); |
| 23 | new = FANOTIFY_E(new_fsn); |
| 24 | |
| 25 | if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid && |
| 26 | old->path.mnt == new->path.mnt && |
| 27 | old->path.dentry == new->path.dentry) |
| 28 | return true; |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | /* and the list better be locked by something too! */ |
| 33 | static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) |
| 34 | { |
| 35 | struct fsnotify_event *test_event; |
| 36 | |
| 37 | pr_debug("%s: list=%p event=%p\n", __func__, list, event); |
| 38 | |
| 39 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 40 | /* |
| 41 | * Don't merge a permission event with any other event so that we know |
| 42 | * the event structure we have created in fanotify_handle_event() is the |
| 43 | * one we should check for permission response. |
| 44 | */ |
| 45 | if (event->mask & FAN_ALL_PERM_EVENTS) |
| 46 | return 0; |
| 47 | #endif |
| 48 | |
| 49 | list_for_each_entry_reverse(test_event, list, list) { |
| 50 | if (should_merge(test_event, event)) { |
| 51 | test_event->mask |= event->mask; |
| 52 | return 1; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 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) { |
| 72 | case FAN_ALLOW: |
| 73 | ret = 0; |
| 74 | break; |
| 75 | case FAN_DENY: |
| 76 | default: |
| 77 | ret = -EPERM; |
| 78 | } |
| 79 | event->response = 0; |
| 80 | |
| 81 | pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, |
| 82 | group, event, ret); |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark, |
| 89 | struct fsnotify_mark *vfsmnt_mark, |
| 90 | u32 event_mask, |
| 91 | const void *data, int data_type) |
| 92 | { |
| 93 | __u32 marks_mask = 0, marks_ignored_mask = 0; |
| 94 | const struct path *path = data; |
| 95 | |
| 96 | pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p" |
| 97 | " data_type=%d\n", __func__, inode_mark, vfsmnt_mark, |
| 98 | event_mask, data, data_type); |
| 99 | |
| 100 | /* if we don't have enough info to send an event to userspace say no */ |
| 101 | if (data_type != FSNOTIFY_EVENT_PATH) |
| 102 | return false; |
| 103 | |
| 104 | /* sorry, fanotify only gives a damn about files and dirs */ |
| 105 | if (!d_is_reg(path->dentry) && |
| 106 | !d_can_lookup(path->dentry)) |
| 107 | return false; |
| 108 | |
| 109 | /* |
| 110 | * if the event is for a child and this inode doesn't care about |
| 111 | * events on the child, don't send it! |
| 112 | */ |
| 113 | if (inode_mark && |
| 114 | (!(event_mask & FS_EVENT_ON_CHILD) || |
| 115 | (inode_mark->mask & FS_EVENT_ON_CHILD))) { |
| 116 | marks_mask |= inode_mark->mask; |
| 117 | marks_ignored_mask |= inode_mark->ignored_mask; |
| 118 | } |
| 119 | |
| 120 | if (vfsmnt_mark) { |
| 121 | marks_mask |= vfsmnt_mark->mask; |
| 122 | marks_ignored_mask |= vfsmnt_mark->ignored_mask; |
| 123 | } |
| 124 | |
| 125 | if (d_is_dir(path->dentry) && |
| 126 | !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) |
| 127 | return false; |
| 128 | |
| 129 | if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask & |
| 130 | ~marks_ignored_mask) |
| 131 | return true; |
| 132 | |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | struct fanotify_event_info *fanotify_alloc_event(struct inode *inode, u32 mask, |
| 137 | const struct path *path) |
| 138 | { |
| 139 | struct fanotify_event_info *event; |
| 140 | |
| 141 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 142 | if (mask & FAN_ALL_PERM_EVENTS) { |
| 143 | struct fanotify_perm_event_info *pevent; |
| 144 | |
| 145 | pevent = kmem_cache_alloc(fanotify_perm_event_cachep, |
| 146 | GFP_KERNEL); |
| 147 | if (!pevent) |
| 148 | return NULL; |
| 149 | event = &pevent->fae; |
| 150 | pevent->response = 0; |
| 151 | goto init; |
| 152 | } |
| 153 | #endif |
| 154 | event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL); |
| 155 | if (!event) |
| 156 | return NULL; |
| 157 | init: __maybe_unused |
| 158 | fsnotify_init_event(&event->fse, inode, mask); |
| 159 | event->tgid = get_pid(task_tgid(current)); |
| 160 | if (path) { |
| 161 | event->path = *path; |
| 162 | path_get(&event->path); |
| 163 | } else { |
| 164 | event->path.mnt = NULL; |
| 165 | event->path.dentry = NULL; |
| 166 | } |
| 167 | return event; |
| 168 | } |
| 169 | |
| 170 | static int fanotify_handle_event(struct fsnotify_group *group, |
| 171 | struct inode *inode, |
| 172 | struct fsnotify_mark *inode_mark, |
| 173 | struct fsnotify_mark *fanotify_mark, |
| 174 | u32 mask, const void *data, int data_type, |
| 175 | const unsigned char *file_name, u32 cookie, |
| 176 | struct fsnotify_iter_info *iter_info) |
| 177 | { |
| 178 | int ret = 0; |
| 179 | struct fanotify_event_info *event; |
| 180 | struct fsnotify_event *fsn_event; |
| 181 | |
| 182 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); |
| 183 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); |
| 184 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 185 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 186 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); |
| 187 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); |
| 188 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 189 | BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); |
| 190 | BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); |
| 191 | BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); |
| 192 | |
| 193 | if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data, |
| 194 | data_type)) |
| 195 | return 0; |
| 196 | |
| 197 | pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, |
| 198 | mask); |
| 199 | |
| 200 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 201 | if (mask & FAN_ALL_PERM_EVENTS) { |
| 202 | /* |
| 203 | * fsnotify_prepare_user_wait() fails if we race with mark |
| 204 | * deletion. Just let the operation pass in that case. |
| 205 | */ |
| 206 | if (!fsnotify_prepare_user_wait(iter_info)) |
| 207 | return 0; |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | event = fanotify_alloc_event(inode, mask, data); |
| 212 | ret = -ENOMEM; |
| 213 | if (unlikely(!event)) |
| 214 | goto finish; |
| 215 | |
| 216 | fsn_event = &event->fse; |
| 217 | ret = fsnotify_add_event(group, fsn_event, fanotify_merge); |
| 218 | if (ret) { |
| 219 | /* Permission events shouldn't be merged */ |
| 220 | BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS); |
| 221 | /* Our event wasn't used in the end. Free it. */ |
| 222 | fsnotify_destroy_event(group, fsn_event); |
| 223 | |
| 224 | ret = 0; |
| 225 | goto finish; |
| 226 | } |
| 227 | |
| 228 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 229 | if (mask & FAN_ALL_PERM_EVENTS) { |
| 230 | ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event), |
| 231 | iter_info); |
| 232 | fsnotify_destroy_event(group, fsn_event); |
| 233 | } |
| 234 | finish: |
| 235 | if (mask & FAN_ALL_PERM_EVENTS) |
| 236 | fsnotify_finish_user_wait(iter_info); |
| 237 | #else |
| 238 | finish: |
| 239 | #endif |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | static void fanotify_free_group_priv(struct fsnotify_group *group) |
| 244 | { |
| 245 | struct user_struct *user; |
| 246 | |
| 247 | user = group->fanotify_data.user; |
| 248 | atomic_dec(&user->fanotify_listeners); |
| 249 | free_uid(user); |
| 250 | } |
| 251 | |
| 252 | static void fanotify_free_event(struct fsnotify_event *fsn_event) |
| 253 | { |
| 254 | struct fanotify_event_info *event; |
| 255 | |
| 256 | event = FANOTIFY_E(fsn_event); |
| 257 | path_put(&event->path); |
| 258 | put_pid(event->tgid); |
| 259 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 260 | if (fsn_event->mask & FAN_ALL_PERM_EVENTS) { |
| 261 | kmem_cache_free(fanotify_perm_event_cachep, |
| 262 | FANOTIFY_PE(fsn_event)); |
| 263 | return; |
| 264 | } |
| 265 | #endif |
| 266 | kmem_cache_free(fanotify_event_cachep, event); |
| 267 | } |
| 268 | |
| 269 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 270 | { |
| 271 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 272 | } |
| 273 | |
| 274 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
| 275 | .handle_event = fanotify_handle_event, |
| 276 | .free_group_priv = fanotify_free_group_priv, |
| 277 | .free_event = fanotify_free_event, |
| 278 | .free_mark = fanotify_free_mark, |
| 279 | }; |