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