Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 2 | #include <linux/fanotify.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 3 | #include <linux/fdtable.h> |
| 4 | #include <linux/fsnotify_backend.h> |
| 5 | #include <linux/init.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 6 | #include <linux/jiffies.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 7 | #include <linux/kernel.h> /* UINT_MAX */ |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 8 | #include <linux/mount.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 9 | #include <linux/sched.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 10 | #include <linux/sched/user.h> |
Eric W. Biederman | 7a36094 | 2017-09-26 12:45:33 -0500 | [diff] [blame] | 11 | #include <linux/sched/signal.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 12 | #include <linux/types.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 13 | #include <linux/wait.h> |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 14 | #include <linux/audit.h> |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 15 | #include <linux/sched/mm.h> |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 16 | #include <linux/statfs.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 17 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 18 | #include "fanotify.h" |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 19 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 20 | static bool fanotify_path_equal(struct path *p1, struct path *p2) |
| 21 | { |
| 22 | return p1->mnt == p2->mnt && p1->dentry == p2->dentry; |
| 23 | } |
| 24 | |
| 25 | static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1, |
| 26 | __kernel_fsid_t *fsid2) |
| 27 | { |
Nathan Chancellor | 6def1a1 | 2020-03-27 10:10:30 -0700 | [diff] [blame] | 28 | return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1]; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static bool fanotify_fh_equal(struct fanotify_fh *fh1, |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 32 | struct fanotify_fh *fh2) |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 33 | { |
| 34 | if (fh1->type != fh2->type || fh1->len != fh2->len) |
| 35 | return false; |
| 36 | |
| 37 | /* Do not merge events if we failed to encode fh */ |
| 38 | if (fh1->type == FILEID_INVALID) |
| 39 | return false; |
| 40 | |
| 41 | return !fh1->len || |
| 42 | !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len); |
| 43 | } |
| 44 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 45 | static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1, |
| 46 | struct fanotify_fid_event *ffe2) |
| 47 | { |
| 48 | /* Do not merge fid events without object fh */ |
| 49 | if (!ffe1->object_fh.len) |
| 50 | return false; |
| 51 | |
| 52 | return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) && |
| 53 | fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh); |
| 54 | } |
| 55 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 56 | static bool fanotify_name_event_equal(struct fanotify_name_event *fne1, |
| 57 | struct fanotify_name_event *fne2) |
| 58 | { |
| 59 | /* |
| 60 | * Do not merge name events without dir fh. |
| 61 | * FAN_DIR_MODIFY does not encode object fh, so it may be empty. |
| 62 | */ |
| 63 | if (!fne1->dir_fh.len) |
| 64 | return false; |
| 65 | |
| 66 | if (fne1->name_len != fne2->name_len || |
| 67 | !fanotify_fh_equal(&fne1->dir_fh, &fne2->dir_fh)) |
| 68 | return false; |
| 69 | |
| 70 | return !memcmp(fne1->name, fne2->name, fne1->name_len); |
| 71 | } |
| 72 | |
Fabian Frederick | ab3c4da0 | 2020-05-12 20:17:15 +0200 | [diff] [blame] | 73 | static bool fanotify_should_merge(struct fsnotify_event *old_fsn, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 74 | struct fsnotify_event *new_fsn) |
| 75 | { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 76 | struct fanotify_event *old, *new; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 77 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 78 | pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); |
| 79 | old = FANOTIFY_E(old_fsn); |
| 80 | new = FANOTIFY_E(new_fsn); |
| 81 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 82 | if (old_fsn->objectid != new_fsn->objectid || |
| 83 | old->type != new->type || old->pid != new->pid) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 84 | return false; |
| 85 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 86 | switch (old->type) { |
| 87 | case FANOTIFY_EVENT_TYPE_PATH: |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 88 | return fanotify_path_equal(fanotify_event_path(old), |
| 89 | fanotify_event_path(new)); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 90 | case FANOTIFY_EVENT_TYPE_FID: |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 91 | /* |
| 92 | * We want to merge many dirent events in the same dir (i.e. |
| 93 | * creates/unlinks/renames), but we do not want to merge dirent |
| 94 | * events referring to subdirs with dirent events referring to |
| 95 | * non subdirs, otherwise, user won't be able to tell from a |
| 96 | * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+ |
| 97 | * unlink pair or rmdir+create pair of events. |
| 98 | */ |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 99 | if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR)) |
| 100 | return false; |
| 101 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 102 | return fanotify_fid_event_equal(FANOTIFY_FE(old), |
| 103 | FANOTIFY_FE(new)); |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 104 | case FANOTIFY_EVENT_TYPE_FID_NAME: |
| 105 | return fanotify_name_event_equal(FANOTIFY_NE(old), |
| 106 | FANOTIFY_NE(new)); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 107 | default: |
| 108 | WARN_ON_ONCE(1); |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 114 | /* and the list better be locked by something too! */ |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 115 | static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 116 | { |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 117 | struct fsnotify_event *test_event; |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 118 | struct fanotify_event *new; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 119 | |
| 120 | pr_debug("%s: list=%p event=%p\n", __func__, list, event); |
Amir Goldstein | a0a92d2 | 2019-01-10 19:04:31 +0200 | [diff] [blame] | 121 | new = FANOTIFY_E(event); |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 122 | |
Jan Kara | 13116df | 2014-01-28 18:29:24 +0100 | [diff] [blame] | 123 | /* |
| 124 | * Don't merge a permission event with any other event so that we know |
| 125 | * the event structure we have created in fanotify_handle_event() is the |
| 126 | * one we should check for permission response. |
| 127 | */ |
Amir Goldstein | a0a92d2 | 2019-01-10 19:04:31 +0200 | [diff] [blame] | 128 | if (fanotify_is_perm_event(new->mask)) |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 129 | return 0; |
Jan Kara | 13116df | 2014-01-28 18:29:24 +0100 | [diff] [blame] | 130 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 131 | list_for_each_entry_reverse(test_event, list, list) { |
Fabian Frederick | ab3c4da0 | 2020-05-12 20:17:15 +0200 | [diff] [blame] | 132 | if (fanotify_should_merge(test_event, event)) { |
Amir Goldstein | a0a92d2 | 2019-01-10 19:04:31 +0200 | [diff] [blame] | 133 | FANOTIFY_E(test_event)->mask |= new->mask; |
Kinglong Mee | 6c71100 | 2017-02-09 20:45:22 +0800 | [diff] [blame] | 134 | return 1; |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 135 | } |
| 136 | } |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 137 | |
Kinglong Mee | 6c71100 | 2017-02-09 20:45:22 +0800 | [diff] [blame] | 138 | return 0; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 141 | /* |
| 142 | * Wait for response to permission event. The function also takes care of |
| 143 | * freeing the permission event (or offloads that in case the wait is canceled |
| 144 | * by a signal). The function returns 0 in case access got allowed by userspace, |
| 145 | * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case |
| 146 | * the wait got interrupted by a signal. |
| 147 | */ |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 148 | static int fanotify_get_response(struct fsnotify_group *group, |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 149 | struct fanotify_perm_event *event, |
Jan Kara | 05f0e38 | 2016-11-10 17:45:16 +0100 | [diff] [blame] | 150 | struct fsnotify_iter_info *iter_info) |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 151 | { |
| 152 | int ret; |
| 153 | |
| 154 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 155 | |
Jan Kara | b519057 | 2019-02-21 11:47:23 +0100 | [diff] [blame] | 156 | ret = wait_event_killable(group->fanotify_data.access_waitq, |
| 157 | event->state == FAN_EVENT_ANSWERED); |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 158 | /* Signal pending? */ |
| 159 | if (ret < 0) { |
| 160 | spin_lock(&group->notification_lock); |
| 161 | /* Event reported to userspace and no answer yet? */ |
| 162 | if (event->state == FAN_EVENT_REPORTED) { |
| 163 | /* Event will get freed once userspace answers to it */ |
| 164 | event->state = FAN_EVENT_CANCELED; |
| 165 | spin_unlock(&group->notification_lock); |
| 166 | return ret; |
| 167 | } |
| 168 | /* Event not yet reported? Just remove it. */ |
| 169 | if (event->state == FAN_EVENT_INIT) |
| 170 | fsnotify_remove_queued_event(group, &event->fae.fse); |
| 171 | /* |
| 172 | * Event may be also answered in case signal delivery raced |
| 173 | * with wakeup. In that case we have nothing to do besides |
| 174 | * freeing the event and reporting error. |
| 175 | */ |
| 176 | spin_unlock(&group->notification_lock); |
| 177 | goto out; |
| 178 | } |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 179 | |
| 180 | /* userspace responded, convert to something usable */ |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 181 | switch (event->response & ~FAN_AUDIT) { |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 182 | case FAN_ALLOW: |
| 183 | ret = 0; |
| 184 | break; |
| 185 | case FAN_DENY: |
| 186 | default: |
| 187 | ret = -EPERM; |
| 188 | } |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 189 | |
| 190 | /* Check if the response should be audited */ |
| 191 | if (event->response & FAN_AUDIT) |
| 192 | audit_fanotify(event->response & ~FAN_AUDIT); |
| 193 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 194 | pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, |
| 195 | group, event, ret); |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 196 | out: |
| 197 | fsnotify_destroy_event(group, &event->fae.fse); |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 198 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 199 | return ret; |
| 200 | } |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 201 | |
Matthew Bobrowski | 2d10b23 | 2018-11-08 14:05:49 +1100 | [diff] [blame] | 202 | /* |
| 203 | * This function returns a mask for an event that only contains the flags |
| 204 | * that have been specifically requested by the user. Flags that may have |
| 205 | * been included within the event mask, but have not been explicitly |
| 206 | * requested by the user, will not be present in the returned mask. |
| 207 | */ |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 208 | static u32 fanotify_group_event_mask(struct fsnotify_group *group, |
| 209 | struct fsnotify_iter_info *iter_info, |
| 210 | u32 event_mask, const void *data, |
| 211 | int data_type) |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 212 | { |
Amir Goldstein | 54a307b | 2018-04-04 23:42:18 +0300 | [diff] [blame] | 213 | __u32 marks_mask = 0, marks_ignored_mask = 0; |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 214 | __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS; |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 215 | const struct path *path = fsnotify_data_path(data, data_type); |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 216 | struct fsnotify_mark *mark; |
| 217 | int type; |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 218 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 219 | pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", |
| 220 | __func__, iter_info->report_mask, event_mask, data, data_type); |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 221 | |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 222 | if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { |
| 223 | /* Do we have path to open a file descriptor? */ |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 224 | if (!path) |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 225 | return 0; |
| 226 | /* Path type events are only relevant for files and dirs */ |
| 227 | if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry)) |
| 228 | return 0; |
| 229 | } |
Eric Paris | e1c048b | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 230 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 231 | fsnotify_foreach_obj_type(type) { |
| 232 | if (!fsnotify_iter_should_report_type(iter_info, type)) |
| 233 | continue; |
| 234 | mark = iter_info->marks[type]; |
Amir Goldstein | 2f02fd3 | 2020-05-24 10:24:41 +0300 | [diff] [blame] | 235 | |
| 236 | /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */ |
| 237 | marks_ignored_mask |= mark->ignored_mask; |
| 238 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 239 | /* |
Amir Goldstein | 55bf882 | 2020-03-19 17:10:17 +0200 | [diff] [blame] | 240 | * If the event is on dir and this mark doesn't care about |
| 241 | * events on dir, don't send it! |
| 242 | */ |
| 243 | if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR)) |
| 244 | continue; |
| 245 | |
| 246 | /* |
Amir Goldstein | b469e7e | 2018-10-30 20:29:53 +0200 | [diff] [blame] | 247 | * If the event is for a child and this mark doesn't care about |
| 248 | * events on a child, don't send it! |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 249 | */ |
Amir Goldstein | b469e7e | 2018-10-30 20:29:53 +0200 | [diff] [blame] | 250 | if (event_mask & FS_EVENT_ON_CHILD && |
| 251 | (type != FSNOTIFY_OBJ_TYPE_INODE || |
| 252 | !(mark->mask & FS_EVENT_ON_CHILD))) |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 253 | continue; |
Amir Goldstein | 54a307b | 2018-04-04 23:42:18 +0300 | [diff] [blame] | 254 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 255 | marks_mask |= mark->mask; |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 258 | test_mask = event_mask & marks_mask & ~marks_ignored_mask; |
| 259 | |
| 260 | /* |
Amir Goldstein | 9e2ba2c | 2020-03-19 17:10:19 +0200 | [diff] [blame] | 261 | * For dirent modification events (create/delete/move) that do not carry |
| 262 | * the child entry name information, we report FAN_ONDIR for mkdir/rmdir |
| 263 | * so user can differentiate them from creat/unlink. |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 264 | * |
| 265 | * For backward compatibility and consistency, do not report FAN_ONDIR |
| 266 | * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR |
| 267 | * to user in FAN_REPORT_FID mode for all event types. |
| 268 | */ |
| 269 | if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { |
| 270 | /* Do not report FAN_ONDIR without any event */ |
| 271 | if (!(test_mask & ~FAN_ONDIR)) |
| 272 | return 0; |
| 273 | } else { |
| 274 | user_mask &= ~FAN_ONDIR; |
| 275 | } |
| 276 | |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 277 | return test_mask & user_mask; |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 278 | } |
| 279 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 280 | static void fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode, |
| 281 | gfp_t gfp) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 282 | { |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 283 | int dwords, type, bytes = 0; |
| 284 | char *ext_buf = NULL; |
| 285 | void *buf = fh->buf; |
| 286 | int err; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 287 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 288 | if (!inode) |
| 289 | goto out; |
| 290 | |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 291 | dwords = 0; |
| 292 | err = -ENOENT; |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 293 | type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL); |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 294 | if (!dwords) |
| 295 | goto out_err; |
| 296 | |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 297 | bytes = dwords << 2; |
| 298 | if (bytes > FANOTIFY_INLINE_FH_LEN) { |
| 299 | /* Treat failure to allocate fh as failure to allocate event */ |
| 300 | err = -ENOMEM; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 301 | ext_buf = kmalloc(bytes, gfp); |
| 302 | if (!ext_buf) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 303 | goto out_err; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 304 | |
| 305 | *fanotify_fh_ext_buf_ptr(fh) = ext_buf; |
| 306 | buf = ext_buf; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 309 | type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL); |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 310 | err = -EINVAL; |
| 311 | if (!type || type == FILEID_INVALID || bytes != dwords << 2) |
| 312 | goto out_err; |
| 313 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 314 | fh->type = type; |
| 315 | fh->len = bytes; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 316 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 317 | return; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 318 | |
| 319 | out_err: |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 320 | pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n", |
| 321 | type, bytes, err); |
| 322 | kfree(ext_buf); |
| 323 | *fanotify_fh_ext_buf_ptr(fh) = NULL; |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 324 | out: |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 325 | /* Report the event without a file identifier on encode error */ |
| 326 | fh->type = FILEID_INVALID; |
| 327 | fh->len = 0; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 330 | /* |
| 331 | * The inode to use as identifier when reporting fid depends on the event. |
| 332 | * Report the modified directory inode on dirent modification events. |
| 333 | * Report the "victim" inode otherwise. |
| 334 | * For example: |
| 335 | * FS_ATTRIB reports the child inode even if reported on a watched parent. |
| 336 | * FS_CREATE reports the modified dir inode and not the created inode. |
| 337 | */ |
| 338 | static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask, |
| 339 | const void *data, int data_type) |
| 340 | { |
| 341 | if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) |
| 342 | return to_tell; |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 343 | |
Amir Goldstein | cbcf47a | 2020-07-08 14:11:38 +0300 | [diff] [blame] | 344 | return fsnotify_data_inode(data, data_type); |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame^] | 347 | static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, |
| 348 | struct inode *inode, u32 mask, |
| 349 | const void *data, int data_type, |
| 350 | const struct qstr *file_name, |
| 351 | __kernel_fsid_t *fsid) |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 352 | { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 353 | struct fanotify_event *event = NULL; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 354 | struct fanotify_fid_event *ffe = NULL; |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 355 | struct fanotify_name_event *fne = NULL; |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 356 | gfp_t gfp = GFP_KERNEL_ACCOUNT; |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 357 | struct inode *id = fanotify_fid_inode(inode, mask, data, data_type); |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 358 | const struct path *path = fsnotify_data_path(data, data_type); |
Jan Kara | 1f5eaa9 | 2018-02-21 14:10:59 +0100 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * For queues with unlimited length lost events are not expected and |
| 362 | * can possibly have security implications. Avoid losing events when |
Shakeel Butt | ec16545 | 2019-07-11 20:55:52 -0700 | [diff] [blame] | 363 | * memory is short. For the limited size queues, avoid OOM killer in the |
| 364 | * target monitoring memcg as it may have security repercussion. |
Jan Kara | 1f5eaa9 | 2018-02-21 14:10:59 +0100 | [diff] [blame] | 365 | */ |
| 366 | if (group->max_events == UINT_MAX) |
| 367 | gfp |= __GFP_NOFAIL; |
Shakeel Butt | ec16545 | 2019-07-11 20:55:52 -0700 | [diff] [blame] | 368 | else |
| 369 | gfp |= __GFP_RETRY_MAYFAIL; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 370 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 371 | /* Whoever is interested in the event, pays for the allocation. */ |
| 372 | memalloc_use_memcg(group->memcg); |
| 373 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 374 | if (fanotify_is_perm_event(mask)) { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 375 | struct fanotify_perm_event *pevent; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 376 | |
Jan Kara | 1f5eaa9 | 2018-02-21 14:10:59 +0100 | [diff] [blame] | 377 | pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 378 | if (!pevent) |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 379 | goto out; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 380 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 381 | event = &pevent->fae; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 382 | event->type = FANOTIFY_EVENT_TYPE_PATH_PERM; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 383 | pevent->response = 0; |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 384 | pevent->state = FAN_EVENT_INIT; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 385 | goto init; |
| 386 | } |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 387 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 388 | /* |
| 389 | * For FAN_DIR_MODIFY event, we report the fid of the directory and |
| 390 | * the name of the modified entry. |
| 391 | * Allocate an fanotify_name_event struct and copy the name. |
| 392 | */ |
| 393 | if (mask & FAN_DIR_MODIFY && !(WARN_ON_ONCE(!file_name))) { |
| 394 | fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp); |
| 395 | if (!fne) |
| 396 | goto out; |
| 397 | |
| 398 | event = &fne->fae; |
| 399 | event->type = FANOTIFY_EVENT_TYPE_FID_NAME; |
| 400 | fne->name_len = file_name->len; |
| 401 | strcpy(fne->name, file_name->name); |
| 402 | goto init; |
| 403 | } |
| 404 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 405 | if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { |
| 406 | ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp); |
| 407 | if (!ffe) |
| 408 | goto out; |
| 409 | |
| 410 | event = &ffe->fae; |
| 411 | event->type = FANOTIFY_EVENT_TYPE_FID; |
| 412 | } else { |
| 413 | struct fanotify_path_event *pevent; |
| 414 | |
| 415 | pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp); |
| 416 | if (!pevent) |
| 417 | goto out; |
| 418 | |
| 419 | event = &pevent->fae; |
| 420 | event->type = FANOTIFY_EVENT_TYPE_PATH; |
| 421 | } |
| 422 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 423 | init: |
Amir Goldstein | f367a62 | 2020-03-19 17:10:16 +0200 | [diff] [blame] | 424 | /* |
| 425 | * Use the victim inode instead of the watching inode as the id for |
| 426 | * event queue, so event reported on parent is merged with event |
| 427 | * reported on child when both directory and child watches exist. |
| 428 | */ |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame^] | 429 | fanotify_init_event(event, (unsigned long)id, mask); |
Amir Goldstein | d0a6a87 | 2018-10-04 00:25:38 +0300 | [diff] [blame] | 430 | if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) |
| 431 | event->pid = get_pid(task_pid(current)); |
| 432 | else |
| 433 | event->pid = get_pid(task_tgid(current)); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 434 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 435 | if (fsid && fanotify_event_fsid(event)) |
| 436 | *fanotify_event_fsid(event) = *fsid; |
| 437 | |
| 438 | if (fanotify_event_object_fh(event)) |
| 439 | fanotify_encode_fh(fanotify_event_object_fh(event), id, gfp); |
| 440 | |
| 441 | if (fanotify_event_dir_fh(event)) |
| 442 | fanotify_encode_fh(fanotify_event_dir_fh(event), id, gfp); |
| 443 | |
| 444 | if (fanotify_event_has_path(event)) { |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame^] | 445 | *fanotify_event_path(event) = *path; |
| 446 | path_get(path); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 447 | } |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 448 | out: |
| 449 | memalloc_unuse_memcg(); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 450 | return event; |
| 451 | } |
| 452 | |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 453 | /* |
| 454 | * Get cached fsid of the filesystem containing the object from any connector. |
| 455 | * All connectors are supposed to have the same fsid, but we do not verify that |
| 456 | * here. |
| 457 | */ |
| 458 | static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info) |
| 459 | { |
| 460 | int type; |
| 461 | __kernel_fsid_t fsid = {}; |
| 462 | |
| 463 | fsnotify_foreach_obj_type(type) { |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 464 | struct fsnotify_mark_connector *conn; |
| 465 | |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 466 | if (!fsnotify_iter_should_report_type(iter_info, type)) |
| 467 | continue; |
| 468 | |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 469 | conn = READ_ONCE(iter_info->marks[type]->connector); |
| 470 | /* Mark is just getting destroyed or created? */ |
| 471 | if (!conn) |
| 472 | continue; |
Amir Goldstein | c285a2f | 2019-06-19 13:34:44 +0300 | [diff] [blame] | 473 | if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID)) |
| 474 | continue; |
| 475 | /* Pairs with smp_wmb() in fsnotify_add_mark_list() */ |
| 476 | smp_rmb(); |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 477 | fsid = conn->fsid; |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 478 | if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1])) |
| 479 | continue; |
| 480 | return fsid; |
| 481 | } |
| 482 | |
| 483 | return fsid; |
| 484 | } |
| 485 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 486 | static int fanotify_handle_event(struct fsnotify_group *group, |
| 487 | struct inode *inode, |
Al Viro | 3cd5eca | 2016-11-20 20:19:09 -0500 | [diff] [blame] | 488 | u32 mask, const void *data, int data_type, |
Al Viro | e43e9c3 | 2019-04-26 13:51:03 -0400 | [diff] [blame] | 489 | const struct qstr *file_name, u32 cookie, |
Jan Kara | 9385a84 | 2016-11-10 17:51:50 +0100 | [diff] [blame] | 490 | struct fsnotify_iter_info *iter_info) |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 491 | { |
| 492 | int ret = 0; |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 493 | struct fanotify_event *event; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 494 | struct fsnotify_event *fsn_event; |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 495 | __kernel_fsid_t fsid = {}; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 496 | |
| 497 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); |
| 498 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 499 | BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 500 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 501 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 502 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 503 | BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO); |
| 504 | BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM); |
| 505 | BUILD_BUG_ON(FAN_CREATE != FS_CREATE); |
| 506 | BUILD_BUG_ON(FAN_DELETE != FS_DELETE); |
Amir Goldstein | 9e2ba2c | 2020-03-19 17:10:19 +0200 | [diff] [blame] | 507 | BUILD_BUG_ON(FAN_DIR_MODIFY != FS_DIR_MODIFY); |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 508 | BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF); |
| 509 | BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 510 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); |
| 511 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 512 | BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); |
| 513 | BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); |
| 514 | BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); |
Matthew Bobrowski | 9b076f1 | 2018-11-08 14:07:14 +1100 | [diff] [blame] | 515 | BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC); |
Matthew Bobrowski | 66917a3 | 2018-11-08 14:12:44 +1100 | [diff] [blame] | 516 | BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 517 | |
Amir Goldstein | f179369 | 2020-05-27 15:54:55 +0300 | [diff] [blame] | 518 | BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19); |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 519 | |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 520 | mask = fanotify_group_event_mask(group, iter_info, mask, data, |
| 521 | data_type); |
Matthew Bobrowski | 2d10b23 | 2018-11-08 14:05:49 +1100 | [diff] [blame] | 522 | if (!mask) |
Jan Kara | 83c4c4b | 2014-01-21 15:48:15 -0800 | [diff] [blame] | 523 | return 0; |
| 524 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 525 | pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, |
| 526 | mask); |
| 527 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 528 | if (fanotify_is_perm_event(mask)) { |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 529 | /* |
| 530 | * fsnotify_prepare_user_wait() fails if we race with mark |
| 531 | * deletion. Just let the operation pass in that case. |
| 532 | */ |
| 533 | if (!fsnotify_prepare_user_wait(iter_info)) |
| 534 | return 0; |
| 535 | } |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 536 | |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 537 | if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 538 | fsid = fanotify_get_fsid(iter_info); |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 539 | /* Racing with mark destruction or creation? */ |
| 540 | if (!fsid.val[0] && !fsid.val[1]) |
| 541 | return 0; |
| 542 | } |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 543 | |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 544 | event = fanotify_alloc_event(group, inode, mask, data, data_type, |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 545 | file_name, &fsid); |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 546 | ret = -ENOMEM; |
Jan Kara | 7b1f641 | 2018-02-21 15:07:52 +0100 | [diff] [blame] | 547 | if (unlikely(!event)) { |
| 548 | /* |
| 549 | * We don't queue overflow events for permission events as |
| 550 | * there the access is denied and so no event is in fact lost. |
| 551 | */ |
| 552 | if (!fanotify_is_perm_event(mask)) |
| 553 | fsnotify_queue_overflow(group); |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 554 | goto finish; |
Jan Kara | 7b1f641 | 2018-02-21 15:07:52 +0100 | [diff] [blame] | 555 | } |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 556 | |
| 557 | fsn_event = &event->fse; |
Jan Kara | 8ba8fa91 | 2014-08-06 16:03:26 -0700 | [diff] [blame] | 558 | ret = fsnotify_add_event(group, fsn_event, fanotify_merge); |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 559 | if (ret) { |
Jan Kara | 482ef06 | 2014-02-21 19:07:54 +0100 | [diff] [blame] | 560 | /* Permission events shouldn't be merged */ |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 561 | BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 562 | /* Our event wasn't used in the end. Free it. */ |
| 563 | fsnotify_destroy_event(group, fsn_event); |
Jan Kara | 482ef06 | 2014-02-21 19:07:54 +0100 | [diff] [blame] | 564 | |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 565 | ret = 0; |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 566 | } else if (fanotify_is_perm_event(mask)) { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 567 | ret = fanotify_get_response(group, FANOTIFY_PERM(event), |
Jan Kara | 05f0e38 | 2016-11-10 17:45:16 +0100 | [diff] [blame] | 568 | iter_info); |
Jan Kara | 8581679 | 2014-01-28 21:38:06 +0100 | [diff] [blame] | 569 | } |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 570 | finish: |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 571 | if (fanotify_is_perm_event(mask)) |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 572 | fsnotify_finish_user_wait(iter_info); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 573 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 574 | return ret; |
| 575 | } |
| 576 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 577 | static void fanotify_free_group_priv(struct fsnotify_group *group) |
| 578 | { |
| 579 | struct user_struct *user; |
| 580 | |
| 581 | user = group->fanotify_data.user; |
| 582 | atomic_dec(&user->fanotify_listeners); |
| 583 | free_uid(user); |
| 584 | } |
| 585 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 586 | static void fanotify_free_path_event(struct fanotify_event *event) |
| 587 | { |
| 588 | path_put(fanotify_event_path(event)); |
| 589 | kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event)); |
| 590 | } |
| 591 | |
| 592 | static void fanotify_free_perm_event(struct fanotify_event *event) |
| 593 | { |
| 594 | path_put(fanotify_event_path(event)); |
| 595 | kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event)); |
| 596 | } |
| 597 | |
| 598 | static void fanotify_free_fid_event(struct fanotify_event *event) |
| 599 | { |
| 600 | struct fanotify_fid_event *ffe = FANOTIFY_FE(event); |
| 601 | |
| 602 | if (fanotify_fh_has_ext_buf(&ffe->object_fh)) |
| 603 | kfree(fanotify_fh_ext_buf(&ffe->object_fh)); |
| 604 | kmem_cache_free(fanotify_fid_event_cachep, ffe); |
| 605 | } |
| 606 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 607 | static void fanotify_free_name_event(struct fanotify_event *event) |
| 608 | { |
| 609 | struct fanotify_name_event *fne = FANOTIFY_NE(event); |
| 610 | |
| 611 | if (fanotify_fh_has_ext_buf(&fne->dir_fh)) |
| 612 | kfree(fanotify_fh_ext_buf(&fne->dir_fh)); |
| 613 | kfree(fne); |
| 614 | } |
| 615 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 616 | static void fanotify_free_event(struct fsnotify_event *fsn_event) |
| 617 | { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 618 | struct fanotify_event *event; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 619 | |
| 620 | event = FANOTIFY_E(fsn_event); |
Amir Goldstein | d0a6a87 | 2018-10-04 00:25:38 +0300 | [diff] [blame] | 621 | put_pid(event->pid); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 622 | switch (event->type) { |
| 623 | case FANOTIFY_EVENT_TYPE_PATH: |
| 624 | fanotify_free_path_event(event); |
| 625 | break; |
| 626 | case FANOTIFY_EVENT_TYPE_PATH_PERM: |
| 627 | fanotify_free_perm_event(event); |
| 628 | break; |
| 629 | case FANOTIFY_EVENT_TYPE_FID: |
| 630 | fanotify_free_fid_event(event); |
| 631 | break; |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 632 | case FANOTIFY_EVENT_TYPE_FID_NAME: |
| 633 | fanotify_free_name_event(event); |
| 634 | break; |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame^] | 635 | case FANOTIFY_EVENT_TYPE_OVERFLOW: |
| 636 | kfree(event); |
| 637 | break; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 638 | default: |
| 639 | WARN_ON_ONCE(1); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 640 | } |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 643 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 644 | { |
| 645 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 646 | } |
| 647 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 648 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
| 649 | .handle_event = fanotify_handle_event, |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 650 | .free_group_priv = fanotify_free_group_priv, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 651 | .free_event = fanotify_free_event, |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 652 | .free_mark = fanotify_free_mark, |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 653 | }; |