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