Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1 | #include <linux/fanotify.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 2 | #include <linux/fcntl.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 3 | #include <linux/file.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 4 | #include <linux/fs.h> |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 5 | #include <linux/anon_inodes.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 6 | #include <linux/fsnotify_backend.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 7 | #include <linux/init.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 8 | #include <linux/mount.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 9 | #include <linux/namei.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 10 | #include <linux/poll.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 11 | #include <linux/security.h> |
| 12 | #include <linux/syscalls.h> |
Tejun Heo | e4e047a | 2010-05-20 01:36:28 +1000 | [diff] [blame] | 13 | #include <linux/slab.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 14 | #include <linux/types.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 16 | #include <linux/compat.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 17 | |
| 18 | #include <asm/ioctls.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 19 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 20 | #include "../../mount.h" |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 21 | #include "../fdinfo.h" |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 22 | |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 23 | #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 24 | #define FANOTIFY_DEFAULT_MAX_MARKS 8192 |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 25 | #define FANOTIFY_DEFAULT_MAX_LISTENERS 128 |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 26 | |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 27 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 28 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 29 | static struct kmem_cache *fanotify_mark_cache __read_mostly; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 30 | static struct kmem_cache *fanotify_response_event_cache __read_mostly; |
| 31 | |
| 32 | struct fanotify_response_event { |
| 33 | struct list_head list; |
| 34 | __s32 fd; |
| 35 | struct fsnotify_event *event; |
| 36 | }; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 37 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 38 | /* |
| 39 | * Get an fsnotify notification event if one exists and is small |
| 40 | * enough to fit in "count". Return an error pointer if the count |
| 41 | * is not large enough. |
| 42 | * |
| 43 | * Called with the group->notification_mutex held. |
| 44 | */ |
| 45 | static struct fsnotify_event *get_one_event(struct fsnotify_group *group, |
| 46 | size_t count) |
| 47 | { |
| 48 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 49 | |
| 50 | pr_debug("%s: group=%p count=%zd\n", __func__, group, count); |
| 51 | |
| 52 | if (fsnotify_notify_queue_is_empty(group)) |
| 53 | return NULL; |
| 54 | |
| 55 | if (FAN_EVENT_METADATA_LEN > count) |
| 56 | return ERR_PTR(-EINVAL); |
| 57 | |
| 58 | /* held the notification_mutex the whole time, so this is the |
| 59 | * same event we peeked above */ |
| 60 | return fsnotify_remove_notify_event(group); |
| 61 | } |
| 62 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 63 | static int create_fd(struct fsnotify_group *group, |
| 64 | struct fsnotify_event *event, |
| 65 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 66 | { |
| 67 | int client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 68 | struct file *new_file; |
| 69 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 70 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 71 | |
| 72 | client_fd = get_unused_fd(); |
| 73 | if (client_fd < 0) |
| 74 | return client_fd; |
| 75 | |
Linus Torvalds | 2069601 | 2010-08-12 14:23:04 -0700 | [diff] [blame] | 76 | if (event->data_type != FSNOTIFY_EVENT_PATH) { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 77 | WARN_ON(1); |
| 78 | put_unused_fd(client_fd); |
| 79 | return -EINVAL; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * we need a new file handle for the userspace program so it can read even if it was |
| 84 | * originally opened O_WRONLY. |
| 85 | */ |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 86 | /* it's possible this event was an overflow event. in that case dentry and mnt |
| 87 | * are NULL; That's fine, just don't call dentry open */ |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 88 | if (event->path.dentry && event->path.mnt) |
| 89 | new_file = dentry_open(&event->path, |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 90 | group->fanotify_data.f_flags | FMODE_NONOTIFY, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 91 | current_cred()); |
| 92 | else |
| 93 | new_file = ERR_PTR(-EOVERFLOW); |
| 94 | if (IS_ERR(new_file)) { |
| 95 | /* |
| 96 | * we still send an event even if we can't open the file. this |
| 97 | * can happen when say tasks are gone and we try to open their |
| 98 | * /proc files or we try to open a WRONLY file like in sysfs |
| 99 | * we just send the errno to userspace since there isn't much |
| 100 | * else we can do. |
| 101 | */ |
| 102 | put_unused_fd(client_fd); |
| 103 | client_fd = PTR_ERR(new_file); |
| 104 | } else { |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 105 | *file = new_file; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 108 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 111 | static int fill_event_metadata(struct fsnotify_group *group, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 112 | struct fanotify_event_metadata *metadata, |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 113 | struct fsnotify_event *event, |
| 114 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 115 | { |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 116 | int ret = 0; |
| 117 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 118 | pr_debug("%s: group=%p metadata=%p event=%p\n", __func__, |
| 119 | group, metadata, event); |
| 120 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 121 | *file = NULL; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 122 | metadata->event_len = FAN_EVENT_METADATA_LEN; |
Eric Paris | 7d13162 | 2010-12-07 15:27:57 -0500 | [diff] [blame] | 123 | metadata->metadata_len = FAN_EVENT_METADATA_LEN; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 124 | metadata->vers = FANOTIFY_METADATA_VERSION; |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 125 | metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS; |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 126 | metadata->pid = pid_vnr(event->tgid); |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 127 | if (unlikely(event->mask & FAN_Q_OVERFLOW)) |
| 128 | metadata->fd = FAN_NOFD; |
| 129 | else { |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 130 | metadata->fd = create_fd(group, event, file); |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 131 | if (metadata->fd < 0) |
| 132 | ret = metadata->fd; |
| 133 | } |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 134 | |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 135 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 138 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 139 | static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group, |
| 140 | __s32 fd) |
| 141 | { |
| 142 | struct fanotify_response_event *re, *return_re = NULL; |
| 143 | |
| 144 | mutex_lock(&group->fanotify_data.access_mutex); |
| 145 | list_for_each_entry(re, &group->fanotify_data.access_list, list) { |
| 146 | if (re->fd != fd) |
| 147 | continue; |
| 148 | |
| 149 | list_del_init(&re->list); |
| 150 | return_re = re; |
| 151 | break; |
| 152 | } |
| 153 | mutex_unlock(&group->fanotify_data.access_mutex); |
| 154 | |
| 155 | pr_debug("%s: found return_re=%p\n", __func__, return_re); |
| 156 | |
| 157 | return return_re; |
| 158 | } |
| 159 | |
| 160 | static int process_access_response(struct fsnotify_group *group, |
| 161 | struct fanotify_response *response_struct) |
| 162 | { |
| 163 | struct fanotify_response_event *re; |
| 164 | __s32 fd = response_struct->fd; |
| 165 | __u32 response = response_struct->response; |
| 166 | |
| 167 | pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group, |
| 168 | fd, response); |
| 169 | /* |
| 170 | * make sure the response is valid, if invalid we do nothing and either |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 171 | * userspace can send a valid response or we will clean it up after the |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 172 | * timeout |
| 173 | */ |
| 174 | switch (response) { |
| 175 | case FAN_ALLOW: |
| 176 | case FAN_DENY: |
| 177 | break; |
| 178 | default: |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | if (fd < 0) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | re = dequeue_re(group, fd); |
| 186 | if (!re) |
| 187 | return -ENOENT; |
| 188 | |
| 189 | re->event->response = response; |
| 190 | |
| 191 | wake_up(&group->fanotify_data.access_waitq); |
| 192 | |
| 193 | kmem_cache_free(fanotify_response_event_cache, re); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int prepare_for_access_response(struct fsnotify_group *group, |
| 199 | struct fsnotify_event *event, |
| 200 | __s32 fd) |
| 201 | { |
| 202 | struct fanotify_response_event *re; |
| 203 | |
| 204 | if (!(event->mask & FAN_ALL_PERM_EVENTS)) |
| 205 | return 0; |
| 206 | |
| 207 | re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL); |
| 208 | if (!re) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | re->event = event; |
| 212 | re->fd = fd; |
| 213 | |
| 214 | mutex_lock(&group->fanotify_data.access_mutex); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 215 | |
Lino Sanfilippo | 09e5f14 | 2010-11-19 10:58:07 +0100 | [diff] [blame] | 216 | if (atomic_read(&group->fanotify_data.bypass_perm)) { |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 217 | mutex_unlock(&group->fanotify_data.access_mutex); |
| 218 | kmem_cache_free(fanotify_response_event_cache, re); |
| 219 | event->response = FAN_ALLOW; |
| 220 | return 0; |
| 221 | } |
| 222 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 223 | list_add_tail(&re->list, &group->fanotify_data.access_list); |
| 224 | mutex_unlock(&group->fanotify_data.access_mutex); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 229 | #else |
| 230 | static int prepare_for_access_response(struct fsnotify_group *group, |
| 231 | struct fsnotify_event *event, |
| 232 | __s32 fd) |
| 233 | { |
| 234 | return 0; |
| 235 | } |
| 236 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 237 | #endif |
| 238 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 239 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
| 240 | struct fsnotify_event *event, |
| 241 | char __user *buf) |
| 242 | { |
| 243 | struct fanotify_event_metadata fanotify_event_metadata; |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 244 | struct file *f; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 245 | int fd, ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 246 | |
| 247 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 248 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 249 | ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f); |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 250 | if (ret < 0) |
| 251 | goto out; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 252 | |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 253 | fd = fanotify_event_metadata.fd; |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 254 | ret = -EFAULT; |
| 255 | if (copy_to_user(buf, &fanotify_event_metadata, |
| 256 | fanotify_event_metadata.event_len)) |
| 257 | goto out_close_fd; |
| 258 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 259 | ret = prepare_for_access_response(group, event, fd); |
| 260 | if (ret) |
| 261 | goto out_close_fd; |
| 262 | |
Al Viro | 3587b1b | 2012-11-18 19:19:00 +0000 | [diff] [blame] | 263 | if (fd != FAN_NOFD) |
| 264 | fd_install(fd, f); |
Eric Paris | 7d13162 | 2010-12-07 15:27:57 -0500 | [diff] [blame] | 265 | return fanotify_event_metadata.event_len; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 266 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 267 | out_close_fd: |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 268 | if (fd != FAN_NOFD) { |
| 269 | put_unused_fd(fd); |
| 270 | fput(f); |
| 271 | } |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 272 | out: |
| 273 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 274 | if (event->mask & FAN_ALL_PERM_EVENTS) { |
| 275 | event->response = FAN_DENY; |
| 276 | wake_up(&group->fanotify_data.access_waitq); |
| 277 | } |
| 278 | #endif |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 279 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* intofiy userspace file descriptor functions */ |
| 283 | static unsigned int fanotify_poll(struct file *file, poll_table *wait) |
| 284 | { |
| 285 | struct fsnotify_group *group = file->private_data; |
| 286 | int ret = 0; |
| 287 | |
| 288 | poll_wait(file, &group->notification_waitq, wait); |
| 289 | mutex_lock(&group->notification_mutex); |
| 290 | if (!fsnotify_notify_queue_is_empty(group)) |
| 291 | ret = POLLIN | POLLRDNORM; |
| 292 | mutex_unlock(&group->notification_mutex); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static ssize_t fanotify_read(struct file *file, char __user *buf, |
| 298 | size_t count, loff_t *pos) |
| 299 | { |
| 300 | struct fsnotify_group *group; |
| 301 | struct fsnotify_event *kevent; |
| 302 | char __user *start; |
| 303 | int ret; |
| 304 | DEFINE_WAIT(wait); |
| 305 | |
| 306 | start = buf; |
| 307 | group = file->private_data; |
| 308 | |
| 309 | pr_debug("%s: group=%p\n", __func__, group); |
| 310 | |
| 311 | while (1) { |
| 312 | prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE); |
| 313 | |
| 314 | mutex_lock(&group->notification_mutex); |
| 315 | kevent = get_one_event(group, count); |
| 316 | mutex_unlock(&group->notification_mutex); |
| 317 | |
| 318 | if (kevent) { |
| 319 | ret = PTR_ERR(kevent); |
| 320 | if (IS_ERR(kevent)) |
| 321 | break; |
| 322 | ret = copy_event_to_user(group, kevent, buf); |
| 323 | fsnotify_put_event(kevent); |
| 324 | if (ret < 0) |
| 325 | break; |
| 326 | buf += ret; |
| 327 | count -= ret; |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | ret = -EAGAIN; |
| 332 | if (file->f_flags & O_NONBLOCK) |
| 333 | break; |
Lino Sanfilippo | 1a5cea7 | 2010-10-29 12:06:42 +0200 | [diff] [blame] | 334 | ret = -ERESTARTSYS; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 335 | if (signal_pending(current)) |
| 336 | break; |
| 337 | |
| 338 | if (start != buf) |
| 339 | break; |
| 340 | |
| 341 | schedule(); |
| 342 | } |
| 343 | |
| 344 | finish_wait(&group->notification_waitq, &wait); |
| 345 | if (start != buf && ret != -EFAULT) |
| 346 | ret = buf - start; |
| 347 | return ret; |
| 348 | } |
| 349 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 350 | static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 351 | { |
| 352 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 353 | struct fanotify_response response = { .fd = -1, .response = -1 }; |
| 354 | struct fsnotify_group *group; |
| 355 | int ret; |
| 356 | |
| 357 | group = file->private_data; |
| 358 | |
| 359 | if (count > sizeof(response)) |
| 360 | count = sizeof(response); |
| 361 | |
| 362 | pr_debug("%s: group=%p count=%zu\n", __func__, group, count); |
| 363 | |
| 364 | if (copy_from_user(&response, buf, count)) |
| 365 | return -EFAULT; |
| 366 | |
| 367 | ret = process_access_response(group, &response); |
| 368 | if (ret < 0) |
| 369 | count = ret; |
| 370 | |
| 371 | return count; |
| 372 | #else |
| 373 | return -EINVAL; |
| 374 | #endif |
| 375 | } |
| 376 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 377 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 378 | { |
| 379 | struct fsnotify_group *group = file->private_data; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 380 | |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 381 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Andrew Morton | 19ba54f | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 382 | struct fanotify_response_event *re, *lre; |
| 383 | |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 384 | mutex_lock(&group->fanotify_data.access_mutex); |
| 385 | |
Lino Sanfilippo | 09e5f14 | 2010-11-19 10:58:07 +0100 | [diff] [blame] | 386 | atomic_inc(&group->fanotify_data.bypass_perm); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 387 | |
| 388 | list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) { |
| 389 | pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group, |
| 390 | re, re->event); |
| 391 | |
| 392 | list_del_init(&re->list); |
| 393 | re->event->response = FAN_ALLOW; |
| 394 | |
| 395 | kmem_cache_free(fanotify_response_event_cache, re); |
| 396 | } |
| 397 | mutex_unlock(&group->fanotify_data.access_mutex); |
| 398 | |
| 399 | wake_up(&group->fanotify_data.access_waitq); |
| 400 | #endif |
Eric Paris | 0a6b6bd | 2011-10-14 17:43:39 -0400 | [diff] [blame] | 401 | |
| 402 | if (file->f_flags & FASYNC) |
| 403 | fsnotify_fasync(-1, file, 0); |
| 404 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 405 | /* matches the fanotify_init->fsnotify_alloc_group */ |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 406 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 411 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 412 | { |
| 413 | struct fsnotify_group *group; |
| 414 | struct fsnotify_event_holder *holder; |
| 415 | void __user *p; |
| 416 | int ret = -ENOTTY; |
| 417 | size_t send_len = 0; |
| 418 | |
| 419 | group = file->private_data; |
| 420 | |
| 421 | p = (void __user *) arg; |
| 422 | |
| 423 | switch (cmd) { |
| 424 | case FIONREAD: |
| 425 | mutex_lock(&group->notification_mutex); |
| 426 | list_for_each_entry(holder, &group->notification_list, event_list) |
| 427 | send_len += FAN_EVENT_METADATA_LEN; |
| 428 | mutex_unlock(&group->notification_mutex); |
| 429 | ret = put_user(send_len, (int __user *) p); |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 436 | static const struct file_operations fanotify_fops = { |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 437 | .show_fdinfo = fanotify_show_fdinfo, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 438 | .poll = fanotify_poll, |
| 439 | .read = fanotify_read, |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 440 | .write = fanotify_write, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 441 | .fasync = NULL, |
| 442 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 443 | .unlocked_ioctl = fanotify_ioctl, |
| 444 | .compat_ioctl = fanotify_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 445 | .llseek = noop_llseek, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 446 | }; |
| 447 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 448 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 449 | { |
| 450 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 451 | } |
| 452 | |
| 453 | static int fanotify_find_path(int dfd, const char __user *filename, |
| 454 | struct path *path, unsigned int flags) |
| 455 | { |
| 456 | int ret; |
| 457 | |
| 458 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, |
| 459 | dfd, filename, flags); |
| 460 | |
| 461 | if (filename == NULL) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 462 | struct fd f = fdget(dfd); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 463 | |
| 464 | ret = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 465 | if (!f.file) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 466 | goto out; |
| 467 | |
| 468 | ret = -ENOTDIR; |
| 469 | if ((flags & FAN_MARK_ONLYDIR) && |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 470 | !(S_ISDIR(file_inode(f.file)->i_mode))) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 471 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 472 | goto out; |
| 473 | } |
| 474 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 475 | *path = f.file->f_path; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 476 | path_get(path); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 477 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 478 | } else { |
| 479 | unsigned int lookup_flags = 0; |
| 480 | |
| 481 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 482 | lookup_flags |= LOOKUP_FOLLOW; |
| 483 | if (flags & FAN_MARK_ONLYDIR) |
| 484 | lookup_flags |= LOOKUP_DIRECTORY; |
| 485 | |
| 486 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 487 | if (ret) |
| 488 | goto out; |
| 489 | } |
| 490 | |
| 491 | /* you can only watch an inode if you have read permissions on it */ |
| 492 | ret = inode_permission(path->dentry->d_inode, MAY_READ); |
| 493 | if (ret) |
| 494 | path_put(path); |
| 495 | out: |
| 496 | return ret; |
| 497 | } |
| 498 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 499 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
| 500 | __u32 mask, |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 501 | unsigned int flags, |
| 502 | int *destroy) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 503 | { |
| 504 | __u32 oldmask; |
| 505 | |
| 506 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 507 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 508 | oldmask = fsn_mark->mask; |
| 509 | fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask)); |
| 510 | } else { |
| 511 | oldmask = fsn_mark->ignored_mask; |
| 512 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask)); |
| 513 | } |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 514 | spin_unlock(&fsn_mark->lock); |
| 515 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 516 | *destroy = !(oldmask & ~mask); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 517 | |
| 518 | return mask & oldmask; |
| 519 | } |
| 520 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 521 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 522 | struct vfsmount *mnt, __u32 mask, |
| 523 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 524 | { |
| 525 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 526 | __u32 removed; |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 527 | int destroy_mark; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 528 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 529 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 530 | if (!fsn_mark) |
| 531 | return -ENOENT; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 532 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 533 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
| 534 | &destroy_mark); |
| 535 | if (destroy_mark) |
Lino Sanfilippo | e2a2994 | 2011-06-14 17:29:51 +0200 | [diff] [blame] | 536 | fsnotify_destroy_mark(fsn_mark, group); |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 537 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 538 | fsnotify_put_mark(fsn_mark); |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 539 | if (removed & real_mount(mnt)->mnt_fsnotify_mask) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 540 | fsnotify_recalc_vfsmount_mask(mnt); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 541 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 546 | struct inode *inode, __u32 mask, |
| 547 | unsigned int flags) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 548 | { |
| 549 | struct fsnotify_mark *fsn_mark = NULL; |
| 550 | __u32 removed; |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 551 | int destroy_mark; |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 552 | |
| 553 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 554 | if (!fsn_mark) |
| 555 | return -ENOENT; |
| 556 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 557 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
| 558 | &destroy_mark); |
| 559 | if (destroy_mark) |
Lino Sanfilippo | e2a2994 | 2011-06-14 17:29:51 +0200 | [diff] [blame] | 560 | fsnotify_destroy_mark(fsn_mark, group); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 561 | /* matches the fsnotify_find_inode_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 562 | fsnotify_put_mark(fsn_mark); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 563 | if (removed & inode->i_fsnotify_mask) |
| 564 | fsnotify_recalc_inode_mask(inode); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 565 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 569 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 570 | __u32 mask, |
| 571 | unsigned int flags) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 572 | { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 573 | __u32 oldmask = -1; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 574 | |
| 575 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 576 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 577 | oldmask = fsn_mark->mask; |
| 578 | fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask)); |
| 579 | } else { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 580 | __u32 tmask = fsn_mark->ignored_mask | mask; |
| 581 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask); |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 582 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 583 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 584 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 585 | |
| 586 | if (!(flags & FAN_MARK_ONDIR)) { |
| 587 | __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR; |
| 588 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask); |
| 589 | } |
| 590 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 591 | spin_unlock(&fsn_mark->lock); |
| 592 | |
| 593 | return mask & ~oldmask; |
| 594 | } |
| 595 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 596 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 597 | struct vfsmount *mnt, __u32 mask, |
| 598 | unsigned int flags) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 599 | { |
| 600 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 601 | __u32 added; |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 602 | int ret = 0; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 603 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 604 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 605 | if (!fsn_mark) { |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 606 | if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks) |
| 607 | return -ENOSPC; |
| 608 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 609 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 610 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 611 | return -ENOMEM; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 612 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 613 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 614 | ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 615 | if (ret) |
| 616 | goto err; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 617 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 618 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 619 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 620 | if (added & ~real_mount(mnt)->mnt_fsnotify_mask) |
Eric Paris | 43709a2 | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 621 | fsnotify_recalc_vfsmount_mask(mnt); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 622 | err: |
| 623 | fsnotify_put_mark(fsn_mark); |
| 624 | return ret; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 625 | } |
| 626 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 627 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 628 | struct inode *inode, __u32 mask, |
| 629 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 630 | { |
| 631 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 632 | __u32 added; |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 633 | int ret = 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 634 | |
| 635 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 636 | |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 637 | /* |
| 638 | * If some other task has this inode open for write we should not add |
| 639 | * an ignored mark, unless that ignored mark is supposed to survive |
| 640 | * modification changes anyway. |
| 641 | */ |
| 642 | if ((flags & FAN_MARK_IGNORED_MASK) && |
| 643 | !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && |
| 644 | (atomic_read(&inode->i_writecount) > 0)) |
| 645 | return 0; |
| 646 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 647 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 648 | if (!fsn_mark) { |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 649 | if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks) |
| 650 | return -ENOSPC; |
| 651 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 652 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 653 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 654 | return -ENOMEM; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 655 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 656 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 657 | ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 658 | if (ret) |
| 659 | goto err; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 660 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 661 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 662 | |
Eric Paris | 43709a2 | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 663 | if (added & ~inode->i_fsnotify_mask) |
| 664 | fsnotify_recalc_inode_mask(inode); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 665 | err: |
| 666 | fsnotify_put_mark(fsn_mark); |
| 667 | return ret; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 668 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 669 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 670 | /* fanotify syscalls */ |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 671 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 672 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 673 | struct fsnotify_group *group; |
| 674 | int f_flags, fd; |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 675 | struct user_struct *user; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 676 | |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 677 | pr_debug("%s: flags=%d event_f_flags=%d\n", |
| 678 | __func__, flags, event_f_flags); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 679 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 680 | if (!capable(CAP_SYS_ADMIN)) |
Andreas Gruenbacher | a2f13ad | 2010-08-24 12:58:54 +0200 | [diff] [blame] | 681 | return -EPERM; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 682 | |
| 683 | if (flags & ~FAN_ALL_INIT_FLAGS) |
| 684 | return -EINVAL; |
| 685 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 686 | user = get_current_user(); |
| 687 | if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) { |
| 688 | free_uid(user); |
| 689 | return -EMFILE; |
| 690 | } |
| 691 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 692 | f_flags = O_RDWR | FMODE_NONOTIFY; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 693 | if (flags & FAN_CLOEXEC) |
| 694 | f_flags |= O_CLOEXEC; |
| 695 | if (flags & FAN_NONBLOCK) |
| 696 | f_flags |= O_NONBLOCK; |
| 697 | |
| 698 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 699 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 700 | if (IS_ERR(group)) { |
| 701 | free_uid(user); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 702 | return PTR_ERR(group); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 703 | } |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 704 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 705 | group->fanotify_data.user = user; |
| 706 | atomic_inc(&user->fanotify_listeners); |
| 707 | |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 708 | group->fanotify_data.f_flags = event_f_flags; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 709 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 710 | mutex_init(&group->fanotify_data.access_mutex); |
| 711 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
| 712 | INIT_LIST_HEAD(&group->fanotify_data.access_list); |
Lino Sanfilippo | 09e5f14 | 2010-11-19 10:58:07 +0100 | [diff] [blame] | 713 | atomic_set(&group->fanotify_data.bypass_perm, 0); |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 714 | #endif |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 715 | switch (flags & FAN_ALL_CLASS_BITS) { |
| 716 | case FAN_CLASS_NOTIF: |
| 717 | group->priority = FS_PRIO_0; |
| 718 | break; |
| 719 | case FAN_CLASS_CONTENT: |
| 720 | group->priority = FS_PRIO_1; |
| 721 | break; |
| 722 | case FAN_CLASS_PRE_CONTENT: |
| 723 | group->priority = FS_PRIO_2; |
| 724 | break; |
| 725 | default: |
| 726 | fd = -EINVAL; |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 727 | goto out_destroy_group; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 728 | } |
Eric Paris | cb2d429 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 729 | |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 730 | if (flags & FAN_UNLIMITED_QUEUE) { |
| 731 | fd = -EPERM; |
| 732 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 733 | goto out_destroy_group; |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 734 | group->max_events = UINT_MAX; |
| 735 | } else { |
| 736 | group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS; |
| 737 | } |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 738 | |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 739 | if (flags & FAN_UNLIMITED_MARKS) { |
| 740 | fd = -EPERM; |
| 741 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 742 | goto out_destroy_group; |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 743 | group->fanotify_data.max_marks = UINT_MAX; |
| 744 | } else { |
| 745 | group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS; |
| 746 | } |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 747 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 748 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 749 | if (fd < 0) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 750 | goto out_destroy_group; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 751 | |
| 752 | return fd; |
| 753 | |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 754 | out_destroy_group: |
| 755 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 756 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 757 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 758 | |
Al Viro | 4a0fd5b | 2013-01-21 15:16:58 -0500 | [diff] [blame] | 759 | SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, |
| 760 | __u64, mask, int, dfd, |
| 761 | const char __user *, pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 762 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 763 | struct inode *inode = NULL; |
| 764 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 765 | struct fsnotify_group *group; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 766 | struct fd f; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 767 | struct path path; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 768 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 769 | |
| 770 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 771 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 772 | |
| 773 | /* we only use the lower 32 bits as of right now. */ |
| 774 | if (mask & ((__u64)0xffffffff << 32)) |
| 775 | return -EINVAL; |
| 776 | |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 777 | if (flags & ~FAN_ALL_MARK_FLAGS) |
| 778 | return -EINVAL; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 779 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 780 | case FAN_MARK_ADD: /* fallthrough */ |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 781 | case FAN_MARK_REMOVE: |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 782 | if (!mask) |
| 783 | return -EINVAL; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 784 | case FAN_MARK_FLUSH: |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 785 | break; |
| 786 | default: |
| 787 | return -EINVAL; |
| 788 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 789 | |
| 790 | if (mask & FAN_ONDIR) { |
| 791 | flags |= FAN_MARK_ONDIR; |
| 792 | mask &= ~FAN_ONDIR; |
| 793 | } |
| 794 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 795 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 796 | if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD)) |
| 797 | #else |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 798 | if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD)) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 799 | #endif |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 800 | return -EINVAL; |
| 801 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 802 | f = fdget(fanotify_fd); |
| 803 | if (unlikely(!f.file)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 804 | return -EBADF; |
| 805 | |
| 806 | /* verify that this is indeed an fanotify instance */ |
| 807 | ret = -EINVAL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 808 | if (unlikely(f.file->f_op != &fanotify_fops)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 809 | goto fput_and_out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 810 | group = f.file->private_data; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 811 | |
| 812 | /* |
| 813 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not |
| 814 | * allowed to set permissions events. |
| 815 | */ |
| 816 | ret = -EINVAL; |
| 817 | if (mask & FAN_ALL_PERM_EVENTS && |
| 818 | group->priority == FS_PRIO_0) |
| 819 | goto fput_and_out; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 820 | |
| 821 | ret = fanotify_find_path(dfd, pathname, &path, flags); |
| 822 | if (ret) |
| 823 | goto fput_and_out; |
| 824 | |
| 825 | /* inode held in place by reference to path; group by fget on fd */ |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 826 | if (!(flags & FAN_MARK_MOUNT)) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 827 | inode = path.dentry->d_inode; |
| 828 | else |
| 829 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 830 | |
| 831 | /* create/update an inode mark */ |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 832 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 833 | case FAN_MARK_ADD: |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 834 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 835 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 836 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 837 | ret = fanotify_add_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 838 | break; |
| 839 | case FAN_MARK_REMOVE: |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 840 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 841 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 842 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 843 | ret = fanotify_remove_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 844 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 845 | case FAN_MARK_FLUSH: |
| 846 | if (flags & FAN_MARK_MOUNT) |
| 847 | fsnotify_clear_vfsmount_marks_by_group(group); |
| 848 | else |
| 849 | fsnotify_clear_inode_marks_by_group(group); |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 850 | break; |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 851 | default: |
| 852 | ret = -EINVAL; |
| 853 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 854 | |
| 855 | path_put(&path); |
| 856 | fput_and_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 857 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 858 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 859 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 860 | |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 861 | #ifdef CONFIG_COMPAT |
| 862 | COMPAT_SYSCALL_DEFINE6(fanotify_mark, |
| 863 | int, fanotify_fd, unsigned int, flags, |
| 864 | __u32, mask0, __u32, mask1, int, dfd, |
| 865 | const char __user *, pathname) |
| 866 | { |
| 867 | return sys_fanotify_mark(fanotify_fd, flags, |
| 868 | #ifdef __BIG_ENDIAN |
| 869 | ((__u64)mask1 << 32) | mask0, |
| 870 | #else |
| 871 | ((__u64)mask0 << 32) | mask1, |
| 872 | #endif |
| 873 | dfd, pathname); |
| 874 | } |
| 875 | #endif |
| 876 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 877 | /* |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 878 | * fanotify_user_setup - Our initialization function. Note that we cannot return |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 879 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 880 | * must result in panic(). |
| 881 | */ |
| 882 | static int __init fanotify_user_setup(void) |
| 883 | { |
| 884 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 885 | fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event, |
| 886 | SLAB_PANIC); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | device_initcall(fanotify_user_setup); |