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