Commit | Line | Data |
---|---|---|
33d3dfff | 1 | #include <linux/fanotify.h> |
11637e4b | 2 | #include <linux/fcntl.h> |
2a3edf86 | 3 | #include <linux/file.h> |
11637e4b | 4 | #include <linux/fs.h> |
52c923dd | 5 | #include <linux/anon_inodes.h> |
11637e4b | 6 | #include <linux/fsnotify_backend.h> |
2a3edf86 | 7 | #include <linux/init.h> |
a1014f10 | 8 | #include <linux/mount.h> |
2a3edf86 | 9 | #include <linux/namei.h> |
a1014f10 | 10 | #include <linux/poll.h> |
11637e4b EP |
11 | #include <linux/security.h> |
12 | #include <linux/syscalls.h> | |
e4e047a2 | 13 | #include <linux/slab.h> |
2a3edf86 | 14 | #include <linux/types.h> |
a1014f10 EP |
15 | #include <linux/uaccess.h> |
16 | ||
17 | #include <asm/ioctls.h> | |
11637e4b | 18 | |
2529a0df EP |
19 | #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 |
20 | ||
33d3dfff | 21 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
11637e4b | 22 | |
2a3edf86 | 23 | static struct kmem_cache *fanotify_mark_cache __read_mostly; |
b2d87909 EP |
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 | }; | |
2a3edf86 | 31 | |
a1014f10 EP |
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 | ||
22aa425d | 57 | static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event) |
a1014f10 EP |
58 | { |
59 | int client_fd; | |
60 | struct dentry *dentry; | |
61 | struct vfsmount *mnt; | |
62 | struct file *new_file; | |
63 | ||
22aa425d | 64 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
a1014f10 EP |
65 | |
66 | client_fd = get_unused_fd(); | |
67 | if (client_fd < 0) | |
68 | return client_fd; | |
69 | ||
2069601b | 70 | if (event->data_type != FSNOTIFY_EVENT_PATH) { |
a1014f10 EP |
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 | */ | |
2069601b LT |
80 | dentry = dget(event->path.dentry); |
81 | mnt = mntget(event->path.mnt); | |
a1014f10 EP |
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, | |
80af2588 | 86 | group->fanotify_data.f_flags | FMODE_NONOTIFY, |
a1014f10 EP |
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 | ||
22aa425d | 104 | return client_fd; |
a1014f10 EP |
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; | |
33d3dfff | 116 | metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS; |
32c32632 | 117 | metadata->pid = pid_vnr(event->tgid); |
22aa425d | 118 | metadata->fd = create_fd(group, event); |
a1014f10 | 119 | |
22aa425d | 120 | return metadata->fd; |
a1014f10 EP |
121 | } |
122 | ||
b2d87909 EP |
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); | |
2eebf582 EP |
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 | ||
b2d87909 EP |
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 | { | |
8860f060 | 245 | return; |
b2d87909 EP |
246 | } |
247 | #endif | |
248 | ||
a1014f10 EP |
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; | |
b2d87909 | 254 | int fd, ret; |
a1014f10 EP |
255 | |
256 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); | |
257 | ||
b2d87909 EP |
258 | fd = fill_event_metadata(group, &fanotify_event_metadata, event); |
259 | if (fd < 0) | |
260 | return fd; | |
261 | ||
262 | ret = prepare_for_access_response(group, event, fd); | |
263 | if (ret) | |
264 | goto out_close_fd; | |
a1014f10 | 265 | |
b2d87909 | 266 | ret = -EFAULT; |
a1014f10 | 267 | if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN)) |
b2d87909 | 268 | goto out_kill_access_response; |
a1014f10 EP |
269 | |
270 | return FAN_EVENT_METADATA_LEN; | |
b2d87909 EP |
271 | |
272 | out_kill_access_response: | |
273 | remove_access_response(group, event, fd); | |
274 | out_close_fd: | |
275 | sys_close(fd); | |
276 | return ret; | |
a1014f10 EP |
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 | ||
b2d87909 EP |
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 | ||
52c923dd EP |
374 | static int fanotify_release(struct inode *ignored, struct file *file) |
375 | { | |
376 | struct fsnotify_group *group = file->private_data; | |
2eebf582 | 377 | struct fanotify_response_event *re, *lre; |
52c923dd EP |
378 | |
379 | pr_debug("%s: file=%p group=%p\n", __func__, file, group); | |
380 | ||
2eebf582 EP |
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 | |
52c923dd EP |
399 | /* matches the fanotify_init->fsnotify_alloc_group */ |
400 | fsnotify_put_group(group); | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
a1014f10 EP |
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 | ||
52c923dd | 430 | static const struct file_operations fanotify_fops = { |
a1014f10 EP |
431 | .poll = fanotify_poll, |
432 | .read = fanotify_read, | |
b2d87909 | 433 | .write = fanotify_write, |
52c923dd EP |
434 | .fasync = NULL, |
435 | .release = fanotify_release, | |
a1014f10 EP |
436 | .unlocked_ioctl = fanotify_ioctl, |
437 | .compat_ioctl = fanotify_ioctl, | |
6038f373 | 438 | .llseek = noop_llseek, |
52c923dd EP |
439 | }; |
440 | ||
2a3edf86 EP |
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 | ||
b9e4e3bd EP |
494 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
495 | __u32 mask, | |
496 | unsigned int flags) | |
088b09b0 AG |
497 | { |
498 | __u32 oldmask; | |
499 | ||
500 | spin_lock(&fsn_mark->lock); | |
b9e4e3bd EP |
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 | } | |
088b09b0 AG |
508 | spin_unlock(&fsn_mark->lock); |
509 | ||
510 | if (!(oldmask & ~mask)) | |
511 | fsnotify_destroy_mark(fsn_mark); | |
512 | ||
513 | return mask & oldmask; | |
514 | } | |
515 | ||
f3640192 | 516 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
b9e4e3bd EP |
517 | struct vfsmount *mnt, __u32 mask, |
518 | unsigned int flags) | |
88826276 EP |
519 | { |
520 | struct fsnotify_mark *fsn_mark = NULL; | |
088b09b0 | 521 | __u32 removed; |
88826276 | 522 | |
f3640192 AG |
523 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
524 | if (!fsn_mark) | |
525 | return -ENOENT; | |
88826276 | 526 | |
b9e4e3bd | 527 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
f3640192 | 528 | fsnotify_put_mark(fsn_mark); |
f3640192 AG |
529 | if (removed & mnt->mnt_fsnotify_mask) |
530 | fsnotify_recalc_vfsmount_mask(mnt); | |
531 | ||
532 | return 0; | |
533 | } | |
2a3edf86 | 534 | |
f3640192 | 535 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
b9e4e3bd EP |
536 | struct inode *inode, __u32 mask, |
537 | unsigned int flags) | |
f3640192 AG |
538 | { |
539 | struct fsnotify_mark *fsn_mark = NULL; | |
540 | __u32 removed; | |
541 | ||
542 | fsn_mark = fsnotify_find_inode_mark(group, inode); | |
88826276 EP |
543 | if (!fsn_mark) |
544 | return -ENOENT; | |
545 | ||
b9e4e3bd | 546 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
5444e298 | 547 | /* matches the fsnotify_find_inode_mark() */ |
2a3edf86 | 548 | fsnotify_put_mark(fsn_mark); |
f3640192 AG |
549 | if (removed & inode->i_fsnotify_mask) |
550 | fsnotify_recalc_inode_mask(inode); | |
088b09b0 | 551 | |
2a3edf86 EP |
552 | return 0; |
553 | } | |
554 | ||
b9e4e3bd EP |
555 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
556 | __u32 mask, | |
557 | unsigned int flags) | |
912ee394 AG |
558 | { |
559 | __u32 oldmask; | |
560 | ||
561 | spin_lock(&fsn_mark->lock); | |
b9e4e3bd EP |
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)); | |
c9778a98 EP |
568 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
569 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; | |
b9e4e3bd | 570 | } |
912ee394 AG |
571 | spin_unlock(&fsn_mark->lock); |
572 | ||
573 | return mask & ~oldmask; | |
574 | } | |
575 | ||
52202dfb | 576 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
b9e4e3bd EP |
577 | struct vfsmount *mnt, __u32 mask, |
578 | unsigned int flags) | |
2a3edf86 EP |
579 | { |
580 | struct fsnotify_mark *fsn_mark; | |
912ee394 | 581 | __u32 added; |
2a3edf86 | 582 | |
88826276 EP |
583 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
584 | if (!fsn_mark) { | |
88826276 EP |
585 | int ret; |
586 | ||
912ee394 AG |
587 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
588 | if (!fsn_mark) | |
52202dfb | 589 | return -ENOMEM; |
88826276 | 590 | |
912ee394 AG |
591 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
592 | ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0); | |
88826276 | 593 | if (ret) { |
912ee394 | 594 | fanotify_free_mark(fsn_mark); |
52202dfb | 595 | return ret; |
88826276 | 596 | } |
88826276 | 597 | } |
b9e4e3bd | 598 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
52202dfb | 599 | fsnotify_put_mark(fsn_mark); |
43709a28 EP |
600 | if (added & ~mnt->mnt_fsnotify_mask) |
601 | fsnotify_recalc_vfsmount_mask(mnt); | |
602 | ||
52202dfb | 603 | return 0; |
88826276 EP |
604 | } |
605 | ||
52202dfb | 606 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
b9e4e3bd EP |
607 | struct inode *inode, __u32 mask, |
608 | unsigned int flags) | |
88826276 EP |
609 | { |
610 | struct fsnotify_mark *fsn_mark; | |
912ee394 | 611 | __u32 added; |
88826276 EP |
612 | |
613 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); | |
2a3edf86 | 614 | |
5322a59f EP |
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 | ||
5444e298 | 625 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
2a3edf86 | 626 | if (!fsn_mark) { |
88826276 | 627 | int ret; |
2a3edf86 | 628 | |
912ee394 AG |
629 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
630 | if (!fsn_mark) | |
52202dfb | 631 | return -ENOMEM; |
2a3edf86 | 632 | |
912ee394 AG |
633 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
634 | ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0); | |
2a3edf86 | 635 | if (ret) { |
912ee394 | 636 | fanotify_free_mark(fsn_mark); |
52202dfb | 637 | return ret; |
2a3edf86 | 638 | } |
2a3edf86 | 639 | } |
b9e4e3bd | 640 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
52202dfb | 641 | fsnotify_put_mark(fsn_mark); |
43709a28 EP |
642 | if (added & ~inode->i_fsnotify_mask) |
643 | fsnotify_recalc_inode_mask(inode); | |
52202dfb | 644 | return 0; |
88826276 | 645 | } |
2a3edf86 | 646 | |
52c923dd | 647 | /* fanotify syscalls */ |
08ae8938 | 648 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
11637e4b | 649 | { |
52c923dd EP |
650 | struct fsnotify_group *group; |
651 | int f_flags, fd; | |
652 | ||
08ae8938 EP |
653 | pr_debug("%s: flags=%d event_f_flags=%d\n", |
654 | __func__, flags, event_f_flags); | |
52c923dd | 655 | |
52c923dd | 656 | if (!capable(CAP_SYS_ADMIN)) |
a2f13ad0 | 657 | return -EPERM; |
52c923dd EP |
658 | |
659 | if (flags & ~FAN_ALL_INIT_FLAGS) | |
660 | return -EINVAL; | |
661 | ||
b2d87909 | 662 | f_flags = O_RDWR | FMODE_NONOTIFY; |
52c923dd EP |
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 | ||
80af2588 | 673 | group->fanotify_data.f_flags = event_f_flags; |
9e66e423 EP |
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 | |
4231a235 EP |
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 | } | |
cb2d429f | 693 | |
5dd03f55 EP |
694 | if (flags & FAN_UNLIMITED_QUEUE) { |
695 | fd = -EPERM; | |
696 | if (!capable(CAP_SYS_ADMIN)) | |
697 | goto out_put_group; | |
698 | group->max_events = UINT_MAX; | |
699 | } else { | |
700 | group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS; | |
701 | } | |
2529a0df | 702 | |
52c923dd EP |
703 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
704 | if (fd < 0) | |
705 | goto out_put_group; | |
706 | ||
707 | return fd; | |
708 | ||
709 | out_put_group: | |
710 | fsnotify_put_group(group); | |
711 | return fd; | |
11637e4b | 712 | } |
bbaa4168 | 713 | |
9bbfc964 HC |
714 | SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags, |
715 | __u64 mask, int dfd, | |
716 | const char __user * pathname) | |
bbaa4168 | 717 | { |
0ff21db9 EP |
718 | struct inode *inode = NULL; |
719 | struct vfsmount *mnt = NULL; | |
2a3edf86 EP |
720 | struct fsnotify_group *group; |
721 | struct file *filp; | |
722 | struct path path; | |
723 | int ret, fput_needed; | |
724 | ||
725 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", | |
726 | __func__, fanotify_fd, flags, dfd, pathname, mask); | |
727 | ||
728 | /* we only use the lower 32 bits as of right now. */ | |
729 | if (mask & ((__u64)0xffffffff << 32)) | |
730 | return -EINVAL; | |
731 | ||
88380fe6 AG |
732 | if (flags & ~FAN_ALL_MARK_FLAGS) |
733 | return -EINVAL; | |
4d92604c | 734 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
88380fe6 AG |
735 | case FAN_MARK_ADD: |
736 | case FAN_MARK_REMOVE: | |
4d92604c | 737 | case FAN_MARK_FLUSH: |
88380fe6 AG |
738 | break; |
739 | default: | |
740 | return -EINVAL; | |
741 | } | |
b2d87909 EP |
742 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
743 | if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD)) | |
744 | #else | |
88380fe6 | 745 | if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD)) |
b2d87909 | 746 | #endif |
2a3edf86 EP |
747 | return -EINVAL; |
748 | ||
749 | filp = fget_light(fanotify_fd, &fput_needed); | |
750 | if (unlikely(!filp)) | |
751 | return -EBADF; | |
752 | ||
753 | /* verify that this is indeed an fanotify instance */ | |
754 | ret = -EINVAL; | |
755 | if (unlikely(filp->f_op != &fanotify_fops)) | |
756 | goto fput_and_out; | |
4231a235 EP |
757 | group = filp->private_data; |
758 | ||
759 | /* | |
760 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not | |
761 | * allowed to set permissions events. | |
762 | */ | |
763 | ret = -EINVAL; | |
764 | if (mask & FAN_ALL_PERM_EVENTS && | |
765 | group->priority == FS_PRIO_0) | |
766 | goto fput_and_out; | |
2a3edf86 EP |
767 | |
768 | ret = fanotify_find_path(dfd, pathname, &path, flags); | |
769 | if (ret) | |
770 | goto fput_and_out; | |
771 | ||
772 | /* inode held in place by reference to path; group by fget on fd */ | |
eac8e9e8 | 773 | if (!(flags & FAN_MARK_MOUNT)) |
0ff21db9 EP |
774 | inode = path.dentry->d_inode; |
775 | else | |
776 | mnt = path.mnt; | |
2a3edf86 EP |
777 | |
778 | /* create/update an inode mark */ | |
4d92604c | 779 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
c6223f46 | 780 | case FAN_MARK_ADD: |
eac8e9e8 | 781 | if (flags & FAN_MARK_MOUNT) |
b9e4e3bd | 782 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags); |
0ff21db9 | 783 | else |
b9e4e3bd | 784 | ret = fanotify_add_inode_mark(group, inode, mask, flags); |
c6223f46 AG |
785 | break; |
786 | case FAN_MARK_REMOVE: | |
f3640192 | 787 | if (flags & FAN_MARK_MOUNT) |
b9e4e3bd | 788 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags); |
f3640192 | 789 | else |
b9e4e3bd | 790 | ret = fanotify_remove_inode_mark(group, inode, mask, flags); |
c6223f46 | 791 | break; |
4d92604c EP |
792 | case FAN_MARK_FLUSH: |
793 | if (flags & FAN_MARK_MOUNT) | |
794 | fsnotify_clear_vfsmount_marks_by_group(group); | |
795 | else | |
796 | fsnotify_clear_inode_marks_by_group(group); | |
4d92604c | 797 | break; |
c6223f46 AG |
798 | default: |
799 | ret = -EINVAL; | |
800 | } | |
2a3edf86 EP |
801 | |
802 | path_put(&path); | |
803 | fput_and_out: | |
804 | fput_light(filp, fput_needed); | |
805 | return ret; | |
806 | } | |
807 | ||
9bbfc964 HC |
808 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
809 | asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask, | |
810 | long dfd, long pathname) | |
811 | { | |
812 | return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags, | |
813 | mask, (int) dfd, | |
814 | (const char __user *) pathname); | |
815 | } | |
816 | SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark); | |
817 | #endif | |
818 | ||
2a3edf86 EP |
819 | /* |
820 | * fanotify_user_setup - Our initialization function. Note that we cannnot return | |
821 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here | |
822 | * must result in panic(). | |
823 | */ | |
824 | static int __init fanotify_user_setup(void) | |
825 | { | |
826 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC); | |
b2d87909 EP |
827 | fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event, |
828 | SLAB_PANIC); | |
2a3edf86 EP |
829 | |
830 | return 0; | |
bbaa4168 | 831 | } |
2a3edf86 | 832 | device_initcall(fanotify_user_setup); |