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