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> |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 17 | #include <linux/stringhash.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 18 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 19 | #include "fanotify.h" |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 20 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 21 | static bool fanotify_path_equal(struct path *p1, struct path *p2) |
| 22 | { |
| 23 | return p1->mnt == p2->mnt && p1->dentry == p2->dentry; |
| 24 | } |
| 25 | |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 26 | static unsigned int fanotify_hash_path(const struct path *path) |
| 27 | { |
| 28 | return hash_ptr(path->dentry, FANOTIFY_EVENT_HASH_BITS) ^ |
| 29 | hash_ptr(path->mnt, FANOTIFY_EVENT_HASH_BITS); |
| 30 | } |
| 31 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 32 | static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1, |
| 33 | __kernel_fsid_t *fsid2) |
| 34 | { |
Nathan Chancellor | 6def1a1 | 2020-03-27 10:10:30 -0700 | [diff] [blame] | 35 | 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] | 36 | } |
| 37 | |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 38 | static unsigned int fanotify_hash_fsid(__kernel_fsid_t *fsid) |
| 39 | { |
| 40 | return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^ |
| 41 | hash_32(fsid->val[1], FANOTIFY_EVENT_HASH_BITS); |
| 42 | } |
| 43 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 44 | static bool fanotify_fh_equal(struct fanotify_fh *fh1, |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 45 | struct fanotify_fh *fh2) |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 46 | { |
| 47 | if (fh1->type != fh2->type || fh1->len != fh2->len) |
| 48 | return false; |
| 49 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 50 | return !fh1->len || |
| 51 | !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len); |
| 52 | } |
| 53 | |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 54 | static unsigned int fanotify_hash_fh(struct fanotify_fh *fh) |
| 55 | { |
| 56 | long salt = (long)fh->type | (long)fh->len << 8; |
| 57 | |
| 58 | /* |
| 59 | * full_name_hash() works long by long, so it handles fh buf optimally. |
| 60 | */ |
| 61 | return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len); |
| 62 | } |
| 63 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 64 | static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1, |
| 65 | struct fanotify_fid_event *ffe2) |
| 66 | { |
| 67 | /* Do not merge fid events without object fh */ |
| 68 | if (!ffe1->object_fh.len) |
| 69 | return false; |
| 70 | |
| 71 | return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) && |
| 72 | fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh); |
| 73 | } |
| 74 | |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 75 | static bool fanotify_info_equal(struct fanotify_info *info1, |
| 76 | struct fanotify_info *info2) |
| 77 | { |
| 78 | if (info1->dir_fh_totlen != info2->dir_fh_totlen || |
| 79 | info1->file_fh_totlen != info2->file_fh_totlen || |
| 80 | info1->name_len != info2->name_len) |
| 81 | return false; |
| 82 | |
| 83 | if (info1->dir_fh_totlen && |
| 84 | !fanotify_fh_equal(fanotify_info_dir_fh(info1), |
| 85 | fanotify_info_dir_fh(info2))) |
| 86 | return false; |
| 87 | |
| 88 | if (info1->file_fh_totlen && |
| 89 | !fanotify_fh_equal(fanotify_info_file_fh(info1), |
| 90 | fanotify_info_file_fh(info2))) |
| 91 | return false; |
| 92 | |
| 93 | return !info1->name_len || |
| 94 | !memcmp(fanotify_info_name(info1), fanotify_info_name(info2), |
| 95 | info1->name_len); |
| 96 | } |
| 97 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 98 | static bool fanotify_name_event_equal(struct fanotify_name_event *fne1, |
| 99 | struct fanotify_name_event *fne2) |
| 100 | { |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 101 | struct fanotify_info *info1 = &fne1->info; |
| 102 | struct fanotify_info *info2 = &fne2->info; |
| 103 | |
Amir Goldstein | 6ad1aad | 2020-07-16 11:42:11 +0300 | [diff] [blame] | 104 | /* Do not merge name events without dir fh */ |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 105 | if (!info1->dir_fh_totlen) |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 106 | return false; |
| 107 | |
Jan Kara | 8aed8ce | 2020-07-28 10:58:07 +0200 | [diff] [blame] | 108 | if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid)) |
| 109 | return false; |
| 110 | |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 111 | return fanotify_info_equal(info1, info2); |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Gabriel Krisman Bertazi | 8a6ae64 | 2021-10-25 16:27:36 -0300 | [diff] [blame^] | 114 | static bool fanotify_error_event_equal(struct fanotify_error_event *fee1, |
| 115 | struct fanotify_error_event *fee2) |
| 116 | { |
| 117 | /* Error events against the same file system are always merged. */ |
| 118 | if (!fanotify_fsid_equal(&fee1->fsid, &fee2->fsid)) |
| 119 | return false; |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
Amir Goldstein | 8988f11 | 2021-03-04 12:48:23 +0200 | [diff] [blame] | 124 | static bool fanotify_should_merge(struct fanotify_event *old, |
| 125 | struct fanotify_event *new) |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 126 | { |
Amir Goldstein | 8988f11 | 2021-03-04 12:48:23 +0200 | [diff] [blame] | 127 | pr_debug("%s: old=%p new=%p\n", __func__, old, new); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 128 | |
Amir Goldstein | 8988f11 | 2021-03-04 12:48:23 +0200 | [diff] [blame] | 129 | if (old->hash != new->hash || |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 130 | old->type != new->type || old->pid != new->pid) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 131 | return false; |
| 132 | |
Amir Goldstein | 103ff6a | 2020-07-16 11:42:10 +0300 | [diff] [blame] | 133 | /* |
| 134 | * We want to merge many dirent events in the same dir (i.e. |
| 135 | * creates/unlinks/renames), but we do not want to merge dirent |
| 136 | * events referring to subdirs with dirent events referring to |
| 137 | * non subdirs, otherwise, user won't be able to tell from a |
| 138 | * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+ |
| 139 | * unlink pair or rmdir+create pair of events. |
| 140 | */ |
| 141 | if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR)) |
| 142 | return false; |
| 143 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 144 | switch (old->type) { |
| 145 | case FANOTIFY_EVENT_TYPE_PATH: |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 146 | return fanotify_path_equal(fanotify_event_path(old), |
| 147 | fanotify_event_path(new)); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 148 | case FANOTIFY_EVENT_TYPE_FID: |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 149 | return fanotify_fid_event_equal(FANOTIFY_FE(old), |
| 150 | FANOTIFY_FE(new)); |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 151 | case FANOTIFY_EVENT_TYPE_FID_NAME: |
| 152 | return fanotify_name_event_equal(FANOTIFY_NE(old), |
| 153 | FANOTIFY_NE(new)); |
Gabriel Krisman Bertazi | 8a6ae64 | 2021-10-25 16:27:36 -0300 | [diff] [blame^] | 154 | case FANOTIFY_EVENT_TYPE_FS_ERROR: |
| 155 | return fanotify_error_event_equal(FANOTIFY_EE(old), |
| 156 | FANOTIFY_EE(new)); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 157 | default: |
| 158 | WARN_ON_ONCE(1); |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 161 | return false; |
| 162 | } |
| 163 | |
Amir Goldstein | b8cd0ee | 2021-03-04 12:48:26 +0200 | [diff] [blame] | 164 | /* Limit event merges to limit CPU overhead per event */ |
| 165 | #define FANOTIFY_MAX_MERGE_EVENTS 128 |
| 166 | |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 167 | /* and the list better be locked by something too! */ |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 168 | static int fanotify_merge(struct fsnotify_group *group, |
| 169 | struct fsnotify_event *event) |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 170 | { |
Amir Goldstein | 8988f11 | 2021-03-04 12:48:23 +0200 | [diff] [blame] | 171 | struct fanotify_event *old, *new = FANOTIFY_E(event); |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 172 | unsigned int bucket = fanotify_event_hash_bucket(group, new); |
| 173 | struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket]; |
Amir Goldstein | b8cd0ee | 2021-03-04 12:48:26 +0200 | [diff] [blame] | 174 | int i = 0; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 175 | |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 176 | pr_debug("%s: group=%p event=%p bucket=%u\n", __func__, |
| 177 | group, event, bucket); |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 178 | |
Jan Kara | 13116df | 2014-01-28 18:29:24 +0100 | [diff] [blame] | 179 | /* |
| 180 | * Don't merge a permission event with any other event so that we know |
| 181 | * the event structure we have created in fanotify_handle_event() is the |
| 182 | * one we should check for permission response. |
| 183 | */ |
Amir Goldstein | a0a92d2 | 2019-01-10 19:04:31 +0200 | [diff] [blame] | 184 | if (fanotify_is_perm_event(new->mask)) |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 185 | return 0; |
Jan Kara | 13116df | 2014-01-28 18:29:24 +0100 | [diff] [blame] | 186 | |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 187 | hlist_for_each_entry(old, hlist, merge_list) { |
Amir Goldstein | b8cd0ee | 2021-03-04 12:48:26 +0200 | [diff] [blame] | 188 | if (++i > FANOTIFY_MAX_MERGE_EVENTS) |
| 189 | break; |
Amir Goldstein | 8988f11 | 2021-03-04 12:48:23 +0200 | [diff] [blame] | 190 | if (fanotify_should_merge(old, new)) { |
| 191 | old->mask |= new->mask; |
Gabriel Krisman Bertazi | 8a6ae64 | 2021-10-25 16:27:36 -0300 | [diff] [blame^] | 192 | |
| 193 | if (fanotify_is_error_event(old->mask)) |
| 194 | FANOTIFY_EE(old)->err_count++; |
| 195 | |
Kinglong Mee | 6c71100 | 2017-02-09 20:45:22 +0800 | [diff] [blame] | 196 | return 1; |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 197 | } |
| 198 | } |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 199 | |
Kinglong Mee | 6c71100 | 2017-02-09 20:45:22 +0800 | [diff] [blame] | 200 | return 0; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 203 | /* |
| 204 | * Wait for response to permission event. The function also takes care of |
| 205 | * freeing the permission event (or offloads that in case the wait is canceled |
| 206 | * by a signal). The function returns 0 in case access got allowed by userspace, |
| 207 | * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case |
| 208 | * the wait got interrupted by a signal. |
| 209 | */ |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 210 | static int fanotify_get_response(struct fsnotify_group *group, |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 211 | struct fanotify_perm_event *event, |
Jan Kara | 05f0e38 | 2016-11-10 17:45:16 +0100 | [diff] [blame] | 212 | struct fsnotify_iter_info *iter_info) |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 213 | { |
| 214 | int ret; |
| 215 | |
| 216 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 217 | |
Jan Kara | b519057 | 2019-02-21 11:47:23 +0100 | [diff] [blame] | 218 | ret = wait_event_killable(group->fanotify_data.access_waitq, |
| 219 | event->state == FAN_EVENT_ANSWERED); |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 220 | /* Signal pending? */ |
| 221 | if (ret < 0) { |
| 222 | spin_lock(&group->notification_lock); |
| 223 | /* Event reported to userspace and no answer yet? */ |
| 224 | if (event->state == FAN_EVENT_REPORTED) { |
| 225 | /* Event will get freed once userspace answers to it */ |
| 226 | event->state = FAN_EVENT_CANCELED; |
| 227 | spin_unlock(&group->notification_lock); |
| 228 | return ret; |
| 229 | } |
| 230 | /* Event not yet reported? Just remove it. */ |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 231 | if (event->state == FAN_EVENT_INIT) { |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 232 | fsnotify_remove_queued_event(group, &event->fae.fse); |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 233 | /* Permission events are not supposed to be hashed */ |
| 234 | WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list)); |
| 235 | } |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Event may be also answered in case signal delivery raced |
| 238 | * with wakeup. In that case we have nothing to do besides |
| 239 | * freeing the event and reporting error. |
| 240 | */ |
| 241 | spin_unlock(&group->notification_lock); |
| 242 | goto out; |
| 243 | } |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 244 | |
| 245 | /* userspace responded, convert to something usable */ |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 246 | switch (event->response & ~FAN_AUDIT) { |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 247 | case FAN_ALLOW: |
| 248 | ret = 0; |
| 249 | break; |
| 250 | case FAN_DENY: |
| 251 | default: |
| 252 | ret = -EPERM; |
| 253 | } |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 254 | |
| 255 | /* Check if the response should be audited */ |
| 256 | if (event->response & FAN_AUDIT) |
| 257 | audit_fanotify(event->response & ~FAN_AUDIT); |
| 258 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 259 | pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, |
| 260 | group, event, ret); |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 261 | out: |
| 262 | fsnotify_destroy_event(group, &event->fae.fse); |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 263 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 264 | return ret; |
| 265 | } |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 266 | |
Matthew Bobrowski | 2d10b23 | 2018-11-08 14:05:49 +1100 | [diff] [blame] | 267 | /* |
| 268 | * This function returns a mask for an event that only contains the flags |
| 269 | * that have been specifically requested by the user. Flags that may have |
| 270 | * been included within the event mask, but have not been explicitly |
| 271 | * requested by the user, will not be present in the returned mask. |
| 272 | */ |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 273 | static u32 fanotify_group_event_mask(struct fsnotify_group *group, |
| 274 | struct fsnotify_iter_info *iter_info, |
| 275 | u32 event_mask, const void *data, |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 276 | int data_type, struct inode *dir) |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 277 | { |
Amir Goldstein | 54a307b | 2018-04-04 23:42:18 +0300 | [diff] [blame] | 278 | __u32 marks_mask = 0, marks_ignored_mask = 0; |
Amir Goldstein | 0badfa0 | 2020-07-16 11:42:09 +0300 | [diff] [blame] | 279 | __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS | |
| 280 | FANOTIFY_EVENT_FLAGS; |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 281 | const struct path *path = fsnotify_data_path(data, data_type); |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 282 | unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 283 | struct fsnotify_mark *mark; |
| 284 | int type; |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 285 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 286 | pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", |
| 287 | __func__, iter_info->report_mask, event_mask, data, data_type); |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 288 | |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 289 | if (!fid_mode) { |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 290 | /* Do we have path to open a file descriptor? */ |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 291 | if (!path) |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 292 | return 0; |
| 293 | /* Path type events are only relevant for files and dirs */ |
| 294 | if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry)) |
| 295 | return 0; |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 296 | } else if (!(fid_mode & FAN_REPORT_FID)) { |
| 297 | /* Do we have a directory inode to report? */ |
| 298 | if (!dir && !(event_mask & FS_ISDIR)) |
| 299 | return 0; |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 300 | } |
Eric Paris | e1c048b | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 301 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 302 | fsnotify_foreach_obj_type(type) { |
| 303 | if (!fsnotify_iter_should_report_type(iter_info, type)) |
| 304 | continue; |
| 305 | mark = iter_info->marks[type]; |
Amir Goldstein | 2f02fd3 | 2020-05-24 10:24:41 +0300 | [diff] [blame] | 306 | |
| 307 | /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */ |
| 308 | marks_ignored_mask |= mark->ignored_mask; |
| 309 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 310 | /* |
Amir Goldstein | 55bf882 | 2020-03-19 17:10:17 +0200 | [diff] [blame] | 311 | * If the event is on dir and this mark doesn't care about |
| 312 | * events on dir, don't send it! |
| 313 | */ |
| 314 | if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR)) |
| 315 | continue; |
| 316 | |
| 317 | /* |
Amir Goldstein | fecc455 | 2020-12-02 14:07:09 +0200 | [diff] [blame] | 318 | * If the event is on a child and this mark is on a parent not |
Amir Goldstein | 497b0c5 | 2020-07-16 11:42:22 +0300 | [diff] [blame] | 319 | * watching children, don't send it! |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 320 | */ |
Amir Goldstein | fecc455 | 2020-12-02 14:07:09 +0200 | [diff] [blame] | 321 | if (type == FSNOTIFY_OBJ_TYPE_PARENT && |
| 322 | !(mark->mask & FS_EVENT_ON_CHILD)) |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 323 | continue; |
Amir Goldstein | 54a307b | 2018-04-04 23:42:18 +0300 | [diff] [blame] | 324 | |
Amir Goldstein | 837a393 | 2018-04-20 16:10:54 -0700 | [diff] [blame] | 325 | marks_mask |= mark->mask; |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 328 | test_mask = event_mask & marks_mask & ~marks_ignored_mask; |
| 329 | |
| 330 | /* |
Amir Goldstein | 9e2ba2c | 2020-03-19 17:10:19 +0200 | [diff] [blame] | 331 | * For dirent modification events (create/delete/move) that do not carry |
| 332 | * the child entry name information, we report FAN_ONDIR for mkdir/rmdir |
| 333 | * so user can differentiate them from creat/unlink. |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 334 | * |
| 335 | * For backward compatibility and consistency, do not report FAN_ONDIR |
| 336 | * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR |
Amir Goldstein | 0badfa0 | 2020-07-16 11:42:09 +0300 | [diff] [blame] | 337 | * to user in fid mode for all event types. |
| 338 | * |
| 339 | * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to |
| 340 | * fanotify_alloc_event() when group is reporting fid as indication |
| 341 | * that event happened on child. |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 342 | */ |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 343 | if (fid_mode) { |
Amir Goldstein | 0badfa0 | 2020-07-16 11:42:09 +0300 | [diff] [blame] | 344 | /* Do not report event flags without any event */ |
| 345 | if (!(test_mask & ~FANOTIFY_EVENT_FLAGS)) |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 346 | return 0; |
| 347 | } else { |
Amir Goldstein | 0badfa0 | 2020-07-16 11:42:09 +0300 | [diff] [blame] | 348 | user_mask &= ~FANOTIFY_EVENT_FLAGS; |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Amir Goldstein | e7fce6d | 2019-01-10 19:04:44 +0200 | [diff] [blame] | 351 | return test_mask & user_mask; |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 354 | /* |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 355 | * Check size needed to encode fanotify_fh. |
| 356 | * |
| 357 | * Return size of encoded fh without fanotify_fh header. |
| 358 | * Return 0 on failure to encode. |
| 359 | */ |
| 360 | static int fanotify_encode_fh_len(struct inode *inode) |
| 361 | { |
| 362 | int dwords = 0; |
| 363 | |
| 364 | if (!inode) |
| 365 | return 0; |
| 366 | |
| 367 | exportfs_encode_inode_fh(inode, NULL, &dwords, NULL); |
| 368 | |
| 369 | return dwords << 2; |
| 370 | } |
| 371 | |
| 372 | /* |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 373 | * Encode fanotify_fh. |
| 374 | * |
| 375 | * Return total size of encoded fh including fanotify_fh header. |
| 376 | * Return 0 on failure to encode. |
| 377 | */ |
| 378 | static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode, |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 379 | unsigned int fh_len, unsigned int *hash, |
| 380 | gfp_t gfp) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 381 | { |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 382 | int dwords, type = 0; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 383 | char *ext_buf = NULL; |
| 384 | void *buf = fh->buf; |
| 385 | int err; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 386 | |
Amir Goldstein | 6ad1aad | 2020-07-16 11:42:11 +0300 | [diff] [blame] | 387 | fh->type = FILEID_ROOT; |
| 388 | fh->len = 0; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 389 | fh->flags = 0; |
Gabriel Krisman Bertazi | 272531a | 2021-10-25 16:27:30 -0300 | [diff] [blame] | 390 | |
| 391 | /* |
| 392 | * Invalid FHs are used by FAN_FS_ERROR for errors not |
| 393 | * linked to any inode. The f_handle won't be reported |
| 394 | * back to userspace. |
| 395 | */ |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 396 | if (!inode) |
Gabriel Krisman Bertazi | 272531a | 2021-10-25 16:27:30 -0300 | [diff] [blame] | 397 | goto out; |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 398 | |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 399 | /* |
| 400 | * !gpf means preallocated variable size fh, but fh_len could |
| 401 | * be zero in that case if encoding fh len failed. |
| 402 | */ |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 403 | err = -ENOENT; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 404 | if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4)) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 405 | goto out_err; |
| 406 | |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 407 | /* No external buffer in a variable size allocated fh */ |
| 408 | if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) { |
| 409 | /* Treat failure to allocate fh as failure to encode fh */ |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 410 | err = -ENOMEM; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 411 | ext_buf = kmalloc(fh_len, gfp); |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 412 | if (!ext_buf) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 413 | goto out_err; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 414 | |
| 415 | *fanotify_fh_ext_buf_ptr(fh) = ext_buf; |
| 416 | buf = ext_buf; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 417 | fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 420 | dwords = fh_len >> 2; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 421 | type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL); |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 422 | err = -EINVAL; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 423 | if (!type || type == FILEID_INVALID || fh_len != dwords << 2) |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 424 | goto out_err; |
| 425 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 426 | fh->type = type; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 427 | fh->len = fh_len; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 428 | |
Gabriel Krisman Bertazi | 272531a | 2021-10-25 16:27:30 -0300 | [diff] [blame] | 429 | out: |
Gabriel Krisman Bertazi | 74fe473 | 2021-10-25 16:27:29 -0300 | [diff] [blame] | 430 | /* |
| 431 | * Mix fh into event merge key. Hash might be NULL in case of |
| 432 | * unhashed FID events (i.e. FAN_FS_ERROR). |
| 433 | */ |
| 434 | if (hash) |
| 435 | *hash ^= fanotify_hash_fh(fh); |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 436 | |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 437 | return FANOTIFY_FH_HDR_LEN + fh_len; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 438 | |
| 439 | out_err: |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 440 | pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n", |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 441 | type, fh_len, err); |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 442 | kfree(ext_buf); |
| 443 | *fanotify_fh_ext_buf_ptr(fh) = NULL; |
| 444 | /* Report the event without a file identifier on encode error */ |
| 445 | fh->type = FILEID_INVALID; |
| 446 | fh->len = 0; |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 447 | return 0; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 450 | /* |
| 451 | * The inode to use as identifier when reporting fid depends on the event. |
| 452 | * Report the modified directory inode on dirent modification events. |
| 453 | * Report the "victim" inode otherwise. |
| 454 | * For example: |
| 455 | * FS_ATTRIB reports the child inode even if reported on a watched parent. |
| 456 | * FS_CREATE reports the modified dir inode and not the created inode. |
| 457 | */ |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame] | 458 | static struct inode *fanotify_fid_inode(u32 event_mask, const void *data, |
| 459 | int data_type, struct inode *dir) |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 460 | { |
| 461 | if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame] | 462 | return dir; |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 463 | |
Amir Goldstein | cbcf47a | 2020-07-08 14:11:38 +0300 | [diff] [blame] | 464 | return fsnotify_data_inode(data, data_type); |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 467 | /* |
| 468 | * The inode to use as identifier when reporting dir fid depends on the event. |
| 469 | * Report the modified directory inode on dirent modification events. |
| 470 | * Report the "victim" inode if "victim" is a directory. |
| 471 | * Report the parent inode if "victim" is not a directory and event is |
| 472 | * reported to parent. |
| 473 | * Otherwise, do not report dir fid. |
| 474 | */ |
| 475 | static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data, |
| 476 | int data_type, struct inode *dir) |
| 477 | { |
| 478 | struct inode *inode = fsnotify_data_inode(data, data_type); |
| 479 | |
| 480 | if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) |
| 481 | return dir; |
| 482 | |
Gabriel Krisman Bertazi | 12f47bf | 2021-10-25 16:27:28 -0300 | [diff] [blame] | 483 | if (inode && S_ISDIR(inode->i_mode)) |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 484 | return inode; |
| 485 | |
| 486 | return dir; |
| 487 | } |
| 488 | |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 489 | static struct fanotify_event *fanotify_alloc_path_event(const struct path *path, |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 490 | unsigned int *hash, |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 491 | gfp_t gfp) |
| 492 | { |
| 493 | struct fanotify_path_event *pevent; |
| 494 | |
| 495 | pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp); |
| 496 | if (!pevent) |
| 497 | return NULL; |
| 498 | |
| 499 | pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH; |
| 500 | pevent->path = *path; |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 501 | *hash ^= fanotify_hash_path(path); |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 502 | path_get(path); |
| 503 | |
| 504 | return &pevent->fae; |
| 505 | } |
| 506 | |
| 507 | static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, |
| 508 | gfp_t gfp) |
| 509 | { |
| 510 | struct fanotify_perm_event *pevent; |
| 511 | |
| 512 | pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); |
| 513 | if (!pevent) |
| 514 | return NULL; |
| 515 | |
| 516 | pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM; |
| 517 | pevent->response = 0; |
| 518 | pevent->state = FAN_EVENT_INIT; |
| 519 | pevent->path = *path; |
| 520 | path_get(path); |
| 521 | |
| 522 | return &pevent->fae; |
| 523 | } |
| 524 | |
| 525 | static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id, |
| 526 | __kernel_fsid_t *fsid, |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 527 | unsigned int *hash, |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 528 | gfp_t gfp) |
| 529 | { |
| 530 | struct fanotify_fid_event *ffe; |
| 531 | |
| 532 | ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp); |
| 533 | if (!ffe) |
| 534 | return NULL; |
| 535 | |
| 536 | ffe->fae.type = FANOTIFY_EVENT_TYPE_FID; |
| 537 | ffe->fsid = *fsid; |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 538 | *hash ^= fanotify_hash_fsid(fsid); |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 539 | fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id), |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 540 | hash, gfp); |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 541 | |
| 542 | return &ffe->fae; |
| 543 | } |
| 544 | |
| 545 | static struct fanotify_event *fanotify_alloc_name_event(struct inode *id, |
| 546 | __kernel_fsid_t *fsid, |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 547 | const struct qstr *name, |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 548 | struct inode *child, |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 549 | unsigned int *hash, |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 550 | gfp_t gfp) |
| 551 | { |
| 552 | struct fanotify_name_event *fne; |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 553 | struct fanotify_info *info; |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 554 | struct fanotify_fh *dfh, *ffh; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 555 | unsigned int dir_fh_len = fanotify_encode_fh_len(id); |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 556 | unsigned int child_fh_len = fanotify_encode_fh_len(child); |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 557 | unsigned int size; |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 558 | |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 559 | size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len; |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 560 | if (child_fh_len) |
| 561 | size += FANOTIFY_FH_HDR_LEN + child_fh_len; |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 562 | if (name) |
| 563 | size += name->len + 1; |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 564 | fne = kmalloc(size, gfp); |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 565 | if (!fne) |
| 566 | return NULL; |
| 567 | |
| 568 | fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME; |
| 569 | fne->fsid = *fsid; |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 570 | *hash ^= fanotify_hash_fsid(fsid); |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 571 | info = &fne->info; |
| 572 | fanotify_info_init(info); |
| 573 | dfh = fanotify_info_dir_fh(info); |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 574 | info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, hash, 0); |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 575 | if (child_fh_len) { |
| 576 | ffh = fanotify_info_file_fh(info); |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 577 | info->file_fh_totlen = fanotify_encode_fh(ffh, child, |
| 578 | child_fh_len, hash, 0); |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 579 | } |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 580 | if (name) { |
| 581 | long salt = name->len; |
| 582 | |
| 583 | fanotify_info_copy_name(info, name); |
| 584 | *hash ^= full_name_hash((void *)salt, name->name, name->len); |
| 585 | } |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 586 | |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 587 | pr_debug("%s: ino=%lu size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n", |
| 588 | __func__, id->i_ino, size, dir_fh_len, child_fh_len, |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 589 | info->name_len, info->name_len, fanotify_info_name(info)); |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 590 | |
| 591 | return &fne->fae; |
| 592 | } |
| 593 | |
Gabriel Krisman Bertazi | 83e9acb | 2021-10-25 16:27:35 -0300 | [diff] [blame] | 594 | static struct fanotify_event *fanotify_alloc_error_event( |
| 595 | struct fsnotify_group *group, |
| 596 | __kernel_fsid_t *fsid, |
Gabriel Krisman Bertazi | 8a6ae64 | 2021-10-25 16:27:36 -0300 | [diff] [blame^] | 597 | const void *data, int data_type, |
| 598 | unsigned int *hash) |
Gabriel Krisman Bertazi | 83e9acb | 2021-10-25 16:27:35 -0300 | [diff] [blame] | 599 | { |
| 600 | struct fs_error_report *report = |
| 601 | fsnotify_data_error_report(data, data_type); |
| 602 | struct fanotify_error_event *fee; |
| 603 | |
| 604 | if (WARN_ON_ONCE(!report)) |
| 605 | return NULL; |
| 606 | |
| 607 | fee = mempool_alloc(&group->fanotify_data.error_events_pool, GFP_NOFS); |
| 608 | if (!fee) |
| 609 | return NULL; |
| 610 | |
| 611 | fee->fae.type = FANOTIFY_EVENT_TYPE_FS_ERROR; |
Gabriel Krisman Bertazi | 8a6ae64 | 2021-10-25 16:27:36 -0300 | [diff] [blame^] | 612 | fee->err_count = 1; |
| 613 | fee->fsid = *fsid; |
| 614 | |
| 615 | *hash ^= fanotify_hash_fsid(fsid); |
Gabriel Krisman Bertazi | 83e9acb | 2021-10-25 16:27:35 -0300 | [diff] [blame] | 616 | |
| 617 | return &fee->fae; |
| 618 | } |
| 619 | |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame] | 620 | static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame] | 621 | u32 mask, const void *data, |
| 622 | int data_type, struct inode *dir, |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 623 | const struct qstr *file_name, |
| 624 | __kernel_fsid_t *fsid) |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 625 | { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 626 | struct fanotify_event *event = NULL; |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 627 | gfp_t gfp = GFP_KERNEL_ACCOUNT; |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame] | 628 | struct inode *id = fanotify_fid_inode(mask, data, data_type, dir); |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 629 | struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir); |
Amir Goldstein | aa93bdc | 2020-03-19 17:10:12 +0200 | [diff] [blame] | 630 | const struct path *path = fsnotify_data_path(data, data_type); |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 631 | unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
Roman Gushchin | b87d8ce | 2020-10-17 16:13:40 -0700 | [diff] [blame] | 632 | struct mem_cgroup *old_memcg; |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 633 | struct inode *child = NULL; |
Amir Goldstein | 08b95c33 | 2020-07-08 14:11:52 +0300 | [diff] [blame] | 634 | bool name_event = false; |
Amir Goldstein | 8988f11 | 2021-03-04 12:48:23 +0200 | [diff] [blame] | 635 | unsigned int hash = 0; |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 636 | bool ondir = mask & FAN_ONDIR; |
| 637 | struct pid *pid; |
Jan Kara | 1f5eaa9 | 2018-02-21 14:10:59 +0100 | [diff] [blame] | 638 | |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 639 | if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) { |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 640 | /* |
| 641 | * With both flags FAN_REPORT_DIR_FID and FAN_REPORT_FID, we |
| 642 | * report the child fid for events reported on a non-dir child |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 643 | * in addition to reporting the parent fid and maybe child name. |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 644 | */ |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 645 | if ((fid_mode & FAN_REPORT_FID) && id != dirid && !ondir) |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 646 | child = id; |
| 647 | |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 648 | id = dirid; |
| 649 | |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 650 | /* |
| 651 | * We record file name only in a group with FAN_REPORT_NAME |
| 652 | * and when we have a directory inode to report. |
| 653 | * |
| 654 | * For directory entry modification event, we record the fid of |
| 655 | * the directory and the name of the modified entry. |
| 656 | * |
| 657 | * For event on non-directory that is reported to parent, we |
| 658 | * record the fid of the parent and the name of the child. |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 659 | * |
| 660 | * Even if not reporting name, we need a variable length |
| 661 | * fanotify_name_event if reporting both parent and child fids. |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 662 | */ |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 663 | if (!(fid_mode & FAN_REPORT_NAME)) { |
| 664 | name_event = !!child; |
| 665 | file_name = NULL; |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 666 | } else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) { |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 667 | name_event = true; |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 668 | } |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 669 | } |
| 670 | |
Jan Kara | 1f5eaa9 | 2018-02-21 14:10:59 +0100 | [diff] [blame] | 671 | /* |
| 672 | * For queues with unlimited length lost events are not expected and |
| 673 | * can possibly have security implications. Avoid losing events when |
Shakeel Butt | ec16545 | 2019-07-11 20:55:52 -0700 | [diff] [blame] | 674 | * memory is short. For the limited size queues, avoid OOM killer in the |
| 675 | * target monitoring memcg as it may have security repercussion. |
Jan Kara | 1f5eaa9 | 2018-02-21 14:10:59 +0100 | [diff] [blame] | 676 | */ |
| 677 | if (group->max_events == UINT_MAX) |
| 678 | gfp |= __GFP_NOFAIL; |
Shakeel Butt | ec16545 | 2019-07-11 20:55:52 -0700 | [diff] [blame] | 679 | else |
| 680 | gfp |= __GFP_RETRY_MAYFAIL; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 681 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 682 | /* Whoever is interested in the event, pays for the allocation. */ |
Roman Gushchin | b87d8ce | 2020-10-17 16:13:40 -0700 | [diff] [blame] | 683 | old_memcg = set_active_memcg(group->memcg); |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 684 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 685 | if (fanotify_is_perm_event(mask)) { |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 686 | event = fanotify_alloc_perm_event(path, gfp); |
Gabriel Krisman Bertazi | 83e9acb | 2021-10-25 16:27:35 -0300 | [diff] [blame] | 687 | } else if (fanotify_is_error_event(mask)) { |
| 688 | event = fanotify_alloc_error_event(group, fsid, data, |
Gabriel Krisman Bertazi | 8a6ae64 | 2021-10-25 16:27:36 -0300 | [diff] [blame^] | 689 | data_type, &hash); |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 690 | } else if (name_event && (file_name || child)) { |
Amir Goldstein | 7e8283a | 2020-07-16 11:42:29 +0300 | [diff] [blame] | 691 | event = fanotify_alloc_name_event(id, fsid, file_name, child, |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 692 | &hash, gfp); |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 693 | } else if (fid_mode) { |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 694 | event = fanotify_alloc_fid_event(id, fsid, &hash, gfp); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 695 | } else { |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 696 | event = fanotify_alloc_path_event(path, &hash, gfp); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Amir Goldstein | 9c61f3b | 2020-03-30 10:55:28 +0300 | [diff] [blame] | 699 | if (!event) |
| 700 | goto out; |
| 701 | |
Amir Goldstein | d0a6a87 | 2018-10-04 00:25:38 +0300 | [diff] [blame] | 702 | if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 703 | pid = get_pid(task_pid(current)); |
Amir Goldstein | d0a6a87 | 2018-10-04 00:25:38 +0300 | [diff] [blame] | 704 | else |
Amir Goldstein | 7e3e5c6 | 2021-03-04 12:48:24 +0200 | [diff] [blame] | 705 | pid = get_pid(task_tgid(current)); |
| 706 | |
| 707 | /* Mix event info, FAN_ONDIR flag and pid into event merge key */ |
| 708 | hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS); |
| 709 | fanotify_init_event(event, hash, mask); |
| 710 | event->pid = pid; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 711 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 712 | out: |
Roman Gushchin | b87d8ce | 2020-10-17 16:13:40 -0700 | [diff] [blame] | 713 | set_active_memcg(old_memcg); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 714 | return event; |
| 715 | } |
| 716 | |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 717 | /* |
| 718 | * Get cached fsid of the filesystem containing the object from any connector. |
| 719 | * All connectors are supposed to have the same fsid, but we do not verify that |
| 720 | * here. |
| 721 | */ |
| 722 | static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info) |
| 723 | { |
| 724 | int type; |
| 725 | __kernel_fsid_t fsid = {}; |
| 726 | |
| 727 | fsnotify_foreach_obj_type(type) { |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 728 | struct fsnotify_mark_connector *conn; |
| 729 | |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 730 | if (!fsnotify_iter_should_report_type(iter_info, type)) |
| 731 | continue; |
| 732 | |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 733 | conn = READ_ONCE(iter_info->marks[type]->connector); |
| 734 | /* Mark is just getting destroyed or created? */ |
| 735 | if (!conn) |
| 736 | continue; |
Amir Goldstein | c285a2f | 2019-06-19 13:34:44 +0300 | [diff] [blame] | 737 | if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID)) |
| 738 | continue; |
| 739 | /* Pairs with smp_wmb() in fsnotify_add_mark_list() */ |
| 740 | smp_rmb(); |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 741 | fsid = conn->fsid; |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 742 | if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1])) |
| 743 | continue; |
| 744 | return fsid; |
| 745 | } |
| 746 | |
| 747 | return fsid; |
| 748 | } |
| 749 | |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 750 | /* |
| 751 | * Add an event to hash table for faster merge. |
| 752 | */ |
| 753 | static void fanotify_insert_event(struct fsnotify_group *group, |
| 754 | struct fsnotify_event *fsn_event) |
| 755 | { |
| 756 | struct fanotify_event *event = FANOTIFY_E(fsn_event); |
| 757 | unsigned int bucket = fanotify_event_hash_bucket(group, event); |
| 758 | struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket]; |
| 759 | |
| 760 | assert_spin_locked(&group->notification_lock); |
| 761 | |
Gabriel Krisman Bertazi | cc53b55 | 2021-10-25 16:27:19 -0300 | [diff] [blame] | 762 | if (!fanotify_is_hashed_event(event->mask)) |
| 763 | return; |
| 764 | |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 765 | pr_debug("%s: group=%p event=%p bucket=%u\n", __func__, |
| 766 | group, event, bucket); |
| 767 | |
| 768 | hlist_add_head(&event->merge_list, hlist); |
| 769 | } |
| 770 | |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame] | 771 | static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, |
| 772 | const void *data, int data_type, |
| 773 | struct inode *dir, |
Al Viro | e43e9c3 | 2019-04-26 13:51:03 -0400 | [diff] [blame] | 774 | const struct qstr *file_name, u32 cookie, |
Jan Kara | 9385a84 | 2016-11-10 17:51:50 +0100 | [diff] [blame] | 775 | struct fsnotify_iter_info *iter_info) |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 776 | { |
| 777 | int ret = 0; |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 778 | struct fanotify_event *event; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 779 | struct fsnotify_event *fsn_event; |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 780 | __kernel_fsid_t fsid = {}; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 781 | |
| 782 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); |
| 783 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 784 | BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 785 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 786 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 787 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 788 | BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO); |
| 789 | BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM); |
| 790 | BUILD_BUG_ON(FAN_CREATE != FS_CREATE); |
| 791 | BUILD_BUG_ON(FAN_DELETE != FS_DELETE); |
| 792 | BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF); |
| 793 | BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 794 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); |
| 795 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 796 | BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); |
| 797 | BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); |
| 798 | BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); |
Matthew Bobrowski | 9b076f1 | 2018-11-08 14:07:14 +1100 | [diff] [blame] | 799 | BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC); |
Matthew Bobrowski | 66917a3 | 2018-11-08 14:12:44 +1100 | [diff] [blame] | 800 | BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); |
Gabriel Krisman Bertazi | 8d11a4f | 2021-10-25 16:27:33 -0300 | [diff] [blame] | 801 | BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 802 | |
Amir Goldstein | f179369 | 2020-05-27 15:54:55 +0300 | [diff] [blame] | 803 | BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19); |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 804 | |
Amir Goldstein | 83b535d | 2019-01-10 19:04:42 +0200 | [diff] [blame] | 805 | mask = fanotify_group_event_mask(group, iter_info, mask, data, |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 806 | data_type, dir); |
Matthew Bobrowski | 2d10b23 | 2018-11-08 14:05:49 +1100 | [diff] [blame] | 807 | if (!mask) |
Jan Kara | 83c4c4b | 2014-01-21 15:48:15 -0800 | [diff] [blame] | 808 | return 0; |
| 809 | |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame] | 810 | pr_debug("%s: group=%p mask=%x\n", __func__, group, mask); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 811 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 812 | if (fanotify_is_perm_event(mask)) { |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 813 | /* |
| 814 | * fsnotify_prepare_user_wait() fails if we race with mark |
| 815 | * deletion. Just let the operation pass in that case. |
| 816 | */ |
| 817 | if (!fsnotify_prepare_user_wait(iter_info)) |
| 818 | return 0; |
| 819 | } |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 820 | |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 821 | if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) { |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 822 | fsid = fanotify_get_fsid(iter_info); |
Jan Kara | b1da6a5 | 2019-04-24 18:39:57 +0200 | [diff] [blame] | 823 | /* Racing with mark destruction or creation? */ |
| 824 | if (!fsid.val[0] && !fsid.val[1]) |
| 825 | return 0; |
| 826 | } |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 827 | |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame] | 828 | event = fanotify_alloc_event(group, mask, data, data_type, dir, |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 829 | file_name, &fsid); |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 830 | ret = -ENOMEM; |
Jan Kara | 7b1f641 | 2018-02-21 15:07:52 +0100 | [diff] [blame] | 831 | if (unlikely(!event)) { |
| 832 | /* |
| 833 | * We don't queue overflow events for permission events as |
| 834 | * there the access is denied and so no event is in fact lost. |
| 835 | */ |
| 836 | if (!fanotify_is_perm_event(mask)) |
| 837 | fsnotify_queue_overflow(group); |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 838 | goto finish; |
Jan Kara | 7b1f641 | 2018-02-21 15:07:52 +0100 | [diff] [blame] | 839 | } |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 840 | |
| 841 | fsn_event = &event->fse; |
Gabriel Krisman Bertazi | 1ad03c3 | 2021-10-25 16:27:24 -0300 | [diff] [blame] | 842 | ret = fsnotify_insert_event(group, fsn_event, fanotify_merge, |
| 843 | fanotify_insert_event); |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 844 | if (ret) { |
Jan Kara | 482ef06 | 2014-02-21 19:07:54 +0100 | [diff] [blame] | 845 | /* Permission events shouldn't be merged */ |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 846 | BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 847 | /* Our event wasn't used in the end. Free it. */ |
| 848 | fsnotify_destroy_event(group, fsn_event); |
Jan Kara | 482ef06 | 2014-02-21 19:07:54 +0100 | [diff] [blame] | 849 | |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 850 | ret = 0; |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 851 | } else if (fanotify_is_perm_event(mask)) { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 852 | ret = fanotify_get_response(group, FANOTIFY_PERM(event), |
Jan Kara | 05f0e38 | 2016-11-10 17:45:16 +0100 | [diff] [blame] | 853 | iter_info); |
Jan Kara | 8581679 | 2014-01-28 21:38:06 +0100 | [diff] [blame] | 854 | } |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 855 | finish: |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 856 | if (fanotify_is_perm_event(mask)) |
Miklos Szeredi | f37650f | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 857 | fsnotify_finish_user_wait(iter_info); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 858 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 859 | return ret; |
| 860 | } |
| 861 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 862 | static void fanotify_free_group_priv(struct fsnotify_group *group) |
| 863 | { |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 864 | kfree(group->fanotify_data.merge_hash); |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 865 | if (group->fanotify_data.ucounts) |
| 866 | dec_ucount(group->fanotify_data.ucounts, |
| 867 | UCOUNT_FANOTIFY_GROUPS); |
Gabriel Krisman Bertazi | 734a1a5 | 2021-10-25 16:27:34 -0300 | [diff] [blame] | 868 | |
| 869 | if (mempool_initialized(&group->fanotify_data.error_events_pool)) |
| 870 | mempool_exit(&group->fanotify_data.error_events_pool); |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 871 | } |
| 872 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 873 | static void fanotify_free_path_event(struct fanotify_event *event) |
| 874 | { |
| 875 | path_put(fanotify_event_path(event)); |
| 876 | kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event)); |
| 877 | } |
| 878 | |
| 879 | static void fanotify_free_perm_event(struct fanotify_event *event) |
| 880 | { |
| 881 | path_put(fanotify_event_path(event)); |
| 882 | kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event)); |
| 883 | } |
| 884 | |
| 885 | static void fanotify_free_fid_event(struct fanotify_event *event) |
| 886 | { |
| 887 | struct fanotify_fid_event *ffe = FANOTIFY_FE(event); |
| 888 | |
| 889 | if (fanotify_fh_has_ext_buf(&ffe->object_fh)) |
| 890 | kfree(fanotify_fh_ext_buf(&ffe->object_fh)); |
| 891 | kmem_cache_free(fanotify_fid_event_cachep, ffe); |
| 892 | } |
| 893 | |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 894 | static void fanotify_free_name_event(struct fanotify_event *event) |
| 895 | { |
Amir Goldstein | f35c415 | 2020-07-16 11:42:18 +0300 | [diff] [blame] | 896 | kfree(FANOTIFY_NE(event)); |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 897 | } |
| 898 | |
Gabriel Krisman Bertazi | 83e9acb | 2021-10-25 16:27:35 -0300 | [diff] [blame] | 899 | static void fanotify_free_error_event(struct fsnotify_group *group, |
| 900 | struct fanotify_event *event) |
| 901 | { |
| 902 | struct fanotify_error_event *fee = FANOTIFY_EE(event); |
| 903 | |
| 904 | mempool_free(fee, &group->fanotify_data.error_events_pool); |
| 905 | } |
| 906 | |
Gabriel Krisman Bertazi | 330ae77 | 2021-10-25 16:27:27 -0300 | [diff] [blame] | 907 | static void fanotify_free_event(struct fsnotify_group *group, |
| 908 | struct fsnotify_event *fsn_event) |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 909 | { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 910 | struct fanotify_event *event; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 911 | |
| 912 | event = FANOTIFY_E(fsn_event); |
Amir Goldstein | d0a6a87 | 2018-10-04 00:25:38 +0300 | [diff] [blame] | 913 | put_pid(event->pid); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 914 | switch (event->type) { |
| 915 | case FANOTIFY_EVENT_TYPE_PATH: |
| 916 | fanotify_free_path_event(event); |
| 917 | break; |
| 918 | case FANOTIFY_EVENT_TYPE_PATH_PERM: |
| 919 | fanotify_free_perm_event(event); |
| 920 | break; |
| 921 | case FANOTIFY_EVENT_TYPE_FID: |
| 922 | fanotify_free_fid_event(event); |
| 923 | break; |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 924 | case FANOTIFY_EVENT_TYPE_FID_NAME: |
| 925 | fanotify_free_name_event(event); |
| 926 | break; |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame] | 927 | case FANOTIFY_EVENT_TYPE_OVERFLOW: |
| 928 | kfree(event); |
| 929 | break; |
Gabriel Krisman Bertazi | 83e9acb | 2021-10-25 16:27:35 -0300 | [diff] [blame] | 930 | case FANOTIFY_EVENT_TYPE_FS_ERROR: |
| 931 | fanotify_free_error_event(group, event); |
| 932 | break; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 933 | default: |
| 934 | WARN_ON_ONCE(1); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 935 | } |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 938 | static void fanotify_freeing_mark(struct fsnotify_mark *mark, |
| 939 | struct fsnotify_group *group) |
| 940 | { |
| 941 | if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS)) |
| 942 | dec_ucount(group->fanotify_data.ucounts, UCOUNT_FANOTIFY_MARKS); |
| 943 | } |
| 944 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 945 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 946 | { |
| 947 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 948 | } |
| 949 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 950 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
| 951 | .handle_event = fanotify_handle_event, |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 952 | .free_group_priv = fanotify_free_group_priv, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 953 | .free_event = fanotify_free_event, |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 954 | .freeing_mark = fanotify_freeing_mark, |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 955 | .free_mark = fanotify_free_mark, |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 956 | }; |