Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1 | #include <linux/fanotify.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 2 | #include <linux/fdtable.h> |
| 3 | #include <linux/fsnotify_backend.h> |
| 4 | #include <linux/init.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 5 | #include <linux/jiffies.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 6 | #include <linux/kernel.h> /* UINT_MAX */ |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 7 | #include <linux/mount.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 8 | #include <linux/sched.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 9 | #include <linux/types.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 10 | #include <linux/wait.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 11 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 12 | #include "fanotify.h" |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 13 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 14 | static bool should_merge(struct fsnotify_event *old_fsn, |
| 15 | struct fsnotify_event *new_fsn) |
| 16 | { |
| 17 | struct fanotify_event_info *old, *new; |
| 18 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 19 | pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn); |
| 20 | old = FANOTIFY_E(old_fsn); |
| 21 | new = FANOTIFY_E(new_fsn); |
| 22 | |
| 23 | if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid && |
| 24 | old->path.mnt == new->path.mnt && |
| 25 | old->path.dentry == new->path.dentry) |
| 26 | return true; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 27 | return false; |
| 28 | } |
| 29 | |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 30 | /* and the list better be locked by something too! */ |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 31 | static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 32 | { |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 33 | struct fsnotify_event *test_event; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 34 | |
| 35 | pr_debug("%s: list=%p event=%p\n", __func__, list, event); |
| 36 | |
Jan Kara | 13116df | 2014-01-28 18:29:24 +0100 | [diff] [blame] | 37 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 38 | /* |
| 39 | * Don't merge a permission event with any other event so that we know |
| 40 | * the event structure we have created in fanotify_handle_event() is the |
| 41 | * one we should check for permission response. |
| 42 | */ |
| 43 | if (event->mask & FAN_ALL_PERM_EVENTS) |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 44 | return 0; |
Jan Kara | 13116df | 2014-01-28 18:29:24 +0100 | [diff] [blame] | 45 | #endif |
| 46 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 47 | list_for_each_entry_reverse(test_event, list, list) { |
| 48 | if (should_merge(test_event, event)) { |
Kinglong Mee | 6c71100 | 2017-02-09 20:45:22 +0800 | [diff] [blame^] | 49 | test_event->mask |= event->mask; |
| 50 | return 1; |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 51 | } |
| 52 | } |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 53 | |
Kinglong Mee | 6c71100 | 2017-02-09 20:45:22 +0800 | [diff] [blame^] | 54 | return 0; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 57 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 58 | static int fanotify_get_response(struct fsnotify_group *group, |
| 59 | struct fanotify_perm_event_info *event) |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 60 | { |
| 61 | int ret; |
| 62 | |
| 63 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 64 | |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 65 | wait_event(group->fanotify_data.access_waitq, event->response); |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 66 | |
| 67 | /* userspace responded, convert to something usable */ |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 68 | switch (event->response) { |
| 69 | case FAN_ALLOW: |
| 70 | ret = 0; |
| 71 | break; |
| 72 | case FAN_DENY: |
| 73 | default: |
| 74 | ret = -EPERM; |
| 75 | } |
| 76 | event->response = 0; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 77 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 78 | pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, |
| 79 | group, event, ret); |
| 80 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | #endif |
| 84 | |
Jan Kara | 83c4c4b | 2014-01-21 15:48:15 -0800 | [diff] [blame] | 85 | static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark, |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 86 | struct fsnotify_mark *vfsmnt_mark, |
Jan Kara | 83c4c4b | 2014-01-21 15:48:15 -0800 | [diff] [blame] | 87 | u32 event_mask, |
Al Viro | 3cd5eca | 2016-11-20 20:19:09 -0500 | [diff] [blame] | 88 | const void *data, int data_type) |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 89 | { |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 90 | __u32 marks_mask, marks_ignored_mask; |
Al Viro | 3cd5eca | 2016-11-20 20:19:09 -0500 | [diff] [blame] | 91 | const struct path *path = data; |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 92 | |
Jan Kara | 83c4c4b | 2014-01-21 15:48:15 -0800 | [diff] [blame] | 93 | pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p" |
| 94 | " data_type=%d\n", __func__, inode_mark, vfsmnt_mark, |
| 95 | event_mask, data, data_type); |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 96 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 97 | /* 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] | 98 | if (data_type != FSNOTIFY_EVENT_PATH) |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 99 | return false; |
| 100 | |
Eric Paris | e1c048b | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 101 | /* sorry, fanotify only gives a damn about files and dirs */ |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 102 | if (!d_is_reg(path->dentry) && |
David Howells | 54f2a2f | 2015-01-29 12:02:36 +0000 | [diff] [blame] | 103 | !d_can_lookup(path->dentry)) |
Eric Paris | e1c048b | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 104 | return false; |
| 105 | |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 106 | if (inode_mark && vfsmnt_mark) { |
| 107 | marks_mask = (vfsmnt_mark->mask | inode_mark->mask); |
| 108 | marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask); |
| 109 | } else if (inode_mark) { |
| 110 | /* |
| 111 | * if the event is for a child and this inode doesn't care about |
| 112 | * events on the child, don't send it! |
| 113 | */ |
| 114 | if ((event_mask & FS_EVENT_ON_CHILD) && |
| 115 | !(inode_mark->mask & FS_EVENT_ON_CHILD)) |
| 116 | return false; |
| 117 | marks_mask = inode_mark->mask; |
| 118 | marks_ignored_mask = inode_mark->ignored_mask; |
| 119 | } else if (vfsmnt_mark) { |
| 120 | marks_mask = vfsmnt_mark->mask; |
| 121 | marks_ignored_mask = vfsmnt_mark->ignored_mask; |
| 122 | } else { |
| 123 | BUG(); |
| 124 | } |
| 125 | |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 126 | if (d_is_dir(path->dentry) && |
Lino Sanfilippo | 66ba93c | 2015-02-10 14:08:27 -0800 | [diff] [blame] | 127 | !(marks_mask & FS_ISDIR & ~marks_ignored_mask)) |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 128 | return false; |
| 129 | |
Suzuki K. Poulose | b3c1030 | 2015-03-12 16:26:08 -0700 | [diff] [blame] | 130 | if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask & |
| 131 | ~marks_ignored_mask) |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 132 | return true; |
| 133 | |
| 134 | return false; |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 137 | struct fanotify_event_info *fanotify_alloc_event(struct inode *inode, u32 mask, |
Al Viro | 3cd5eca | 2016-11-20 20:19:09 -0500 | [diff] [blame] | 138 | const struct path *path) |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 139 | { |
| 140 | struct fanotify_event_info *event; |
| 141 | |
| 142 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 143 | if (mask & FAN_ALL_PERM_EVENTS) { |
| 144 | struct fanotify_perm_event_info *pevent; |
| 145 | |
| 146 | pevent = kmem_cache_alloc(fanotify_perm_event_cachep, |
| 147 | GFP_KERNEL); |
| 148 | if (!pevent) |
| 149 | return NULL; |
| 150 | event = &pevent->fae; |
| 151 | pevent->response = 0; |
| 152 | goto init; |
| 153 | } |
| 154 | #endif |
| 155 | event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL); |
| 156 | if (!event) |
| 157 | return NULL; |
| 158 | init: __maybe_unused |
| 159 | fsnotify_init_event(&event->fse, inode, mask); |
| 160 | event->tgid = get_pid(task_tgid(current)); |
| 161 | if (path) { |
| 162 | event->path = *path; |
| 163 | path_get(&event->path); |
| 164 | } else { |
| 165 | event->path.mnt = NULL; |
| 166 | event->path.dentry = NULL; |
| 167 | } |
| 168 | return event; |
| 169 | } |
| 170 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 171 | static int fanotify_handle_event(struct fsnotify_group *group, |
| 172 | struct inode *inode, |
| 173 | struct fsnotify_mark *inode_mark, |
| 174 | struct fsnotify_mark *fanotify_mark, |
Al Viro | 3cd5eca | 2016-11-20 20:19:09 -0500 | [diff] [blame] | 175 | u32 mask, const void *data, int data_type, |
Jan Kara | 45a22f4 | 2014-02-17 13:09:50 +0100 | [diff] [blame] | 176 | const unsigned char *file_name, u32 cookie) |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 177 | { |
| 178 | int ret = 0; |
| 179 | struct fanotify_event_info *event; |
| 180 | struct fsnotify_event *fsn_event; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 181 | |
| 182 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); |
| 183 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); |
| 184 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 185 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 186 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); |
| 187 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); |
| 188 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 189 | BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); |
| 190 | BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); |
| 191 | BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR); |
| 192 | |
Jan Kara | 83c4c4b | 2014-01-21 15:48:15 -0800 | [diff] [blame] | 193 | if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data, |
| 194 | data_type)) |
| 195 | return 0; |
| 196 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 197 | pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode, |
| 198 | mask); |
| 199 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 200 | event = fanotify_alloc_event(inode, mask, data); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 201 | if (unlikely(!event)) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | fsn_event = &event->fse; |
Jan Kara | 8ba8fa91 | 2014-08-06 16:03:26 -0700 | [diff] [blame] | 205 | ret = fsnotify_add_event(group, fsn_event, fanotify_merge); |
Jan Kara | 83c0e1b | 2014-01-28 18:53:22 +0100 | [diff] [blame] | 206 | if (ret) { |
Jan Kara | 482ef06 | 2014-02-21 19:07:54 +0100 | [diff] [blame] | 207 | /* Permission events shouldn't be merged */ |
| 208 | BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 209 | /* Our event wasn't used in the end. Free it. */ |
| 210 | fsnotify_destroy_event(group, fsn_event); |
Jan Kara | 482ef06 | 2014-02-21 19:07:54 +0100 | [diff] [blame] | 211 | |
| 212 | return 0; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Jan Kara | 8581679 | 2014-01-28 21:38:06 +0100 | [diff] [blame] | 216 | if (mask & FAN_ALL_PERM_EVENTS) { |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 217 | ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event)); |
Jan Kara | 8581679 | 2014-01-28 21:38:06 +0100 | [diff] [blame] | 218 | fsnotify_destroy_event(group, fsn_event); |
| 219 | } |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 220 | #endif |
| 221 | return ret; |
| 222 | } |
| 223 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 224 | static void fanotify_free_group_priv(struct fsnotify_group *group) |
| 225 | { |
| 226 | struct user_struct *user; |
| 227 | |
| 228 | user = group->fanotify_data.user; |
| 229 | atomic_dec(&user->fanotify_listeners); |
| 230 | free_uid(user); |
| 231 | } |
| 232 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 233 | static void fanotify_free_event(struct fsnotify_event *fsn_event) |
| 234 | { |
| 235 | struct fanotify_event_info *event; |
| 236 | |
| 237 | event = FANOTIFY_E(fsn_event); |
| 238 | path_put(&event->path); |
| 239 | put_pid(event->tgid); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 240 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 241 | if (fsn_event->mask & FAN_ALL_PERM_EVENTS) { |
| 242 | kmem_cache_free(fanotify_perm_event_cachep, |
| 243 | FANOTIFY_PE(fsn_event)); |
| 244 | return; |
| 245 | } |
| 246 | #endif |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 247 | kmem_cache_free(fanotify_event_cachep, event); |
| 248 | } |
| 249 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 250 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
| 251 | .handle_event = fanotify_handle_event, |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 252 | .free_group_priv = fanotify_free_group_priv, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 253 | .free_event = fanotify_free_event, |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 254 | }; |