1 #include <linux/fcntl.h>
2 #include <linux/file.h>
4 #include <linux/anon_inodes.h>
5 #include <linux/fsnotify_backend.h>
6 #include <linux/init.h>
7 #include <linux/mount.h>
8 #include <linux/namei.h>
9 #include <linux/poll.h>
10 #include <linux/security.h>
11 #include <linux/syscalls.h>
12 #include <linux/types.h>
13 #include <linux/uaccess.h>
15 #include <asm/ioctls.h>
19 static struct kmem_cache *fanotify_mark_cache __read_mostly;
22 * Get an fsnotify notification event if one exists and is small
23 * enough to fit in "count". Return an error pointer if the count
24 * is not large enough.
26 * Called with the group->notification_mutex held.
28 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
31 BUG_ON(!mutex_is_locked(&group->notification_mutex));
33 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
35 if (fsnotify_notify_queue_is_empty(group))
38 if (FAN_EVENT_METADATA_LEN > count)
39 return ERR_PTR(-EINVAL);
41 /* held the notification_mutex the whole time, so this is the
42 * same event we peeked above */
43 return fsnotify_remove_notify_event(group);
46 static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
49 struct dentry *dentry;
51 struct file *new_file;
53 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
55 client_fd = get_unused_fd();
59 if (event->data_type != FSNOTIFY_EVENT_PATH) {
61 put_unused_fd(client_fd);
66 * we need a new file handle for the userspace program so it can read even if it was
67 * originally opened O_WRONLY.
69 dentry = dget(event->path.dentry);
70 mnt = mntget(event->path.mnt);
71 /* it's possible this event was an overflow event. in that case dentry and mnt
72 * are NULL; That's fine, just don't call dentry open */
74 new_file = dentry_open(dentry, mnt,
75 O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
78 new_file = ERR_PTR(-EOVERFLOW);
79 if (IS_ERR(new_file)) {
81 * we still send an event even if we can't open the file. this
82 * can happen when say tasks are gone and we try to open their
83 * /proc files or we try to open a WRONLY file like in sysfs
84 * we just send the errno to userspace since there isn't much
87 put_unused_fd(client_fd);
88 client_fd = PTR_ERR(new_file);
90 fd_install(client_fd, new_file);
96 static ssize_t fill_event_metadata(struct fsnotify_group *group,
97 struct fanotify_event_metadata *metadata,
98 struct fsnotify_event *event)
100 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
101 group, metadata, event);
103 metadata->event_len = FAN_EVENT_METADATA_LEN;
104 metadata->vers = FANOTIFY_METADATA_VERSION;
105 metadata->mask = fanotify_outgoing_mask(event->mask);
106 metadata->pid = pid_vnr(event->tgid);
107 metadata->fd = create_fd(group, event);
112 static ssize_t copy_event_to_user(struct fsnotify_group *group,
113 struct fsnotify_event *event,
116 struct fanotify_event_metadata fanotify_event_metadata;
119 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
121 ret = fill_event_metadata(group, &fanotify_event_metadata, event);
125 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
128 return FAN_EVENT_METADATA_LEN;
131 /* intofiy userspace file descriptor functions */
132 static unsigned int fanotify_poll(struct file *file, poll_table *wait)
134 struct fsnotify_group *group = file->private_data;
137 poll_wait(file, &group->notification_waitq, wait);
138 mutex_lock(&group->notification_mutex);
139 if (!fsnotify_notify_queue_is_empty(group))
140 ret = POLLIN | POLLRDNORM;
141 mutex_unlock(&group->notification_mutex);
146 static ssize_t fanotify_read(struct file *file, char __user *buf,
147 size_t count, loff_t *pos)
149 struct fsnotify_group *group;
150 struct fsnotify_event *kevent;
156 group = file->private_data;
158 pr_debug("%s: group=%p\n", __func__, group);
161 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
163 mutex_lock(&group->notification_mutex);
164 kevent = get_one_event(group, count);
165 mutex_unlock(&group->notification_mutex);
168 ret = PTR_ERR(kevent);
171 ret = copy_event_to_user(group, kevent, buf);
172 fsnotify_put_event(kevent);
181 if (file->f_flags & O_NONBLOCK)
184 if (signal_pending(current))
193 finish_wait(&group->notification_waitq, &wait);
194 if (start != buf && ret != -EFAULT)
199 static int fanotify_release(struct inode *ignored, struct file *file)
201 struct fsnotify_group *group = file->private_data;
203 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
205 /* matches the fanotify_init->fsnotify_alloc_group */
206 fsnotify_put_group(group);
211 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
213 struct fsnotify_group *group;
214 struct fsnotify_event_holder *holder;
219 group = file->private_data;
221 p = (void __user *) arg;
225 mutex_lock(&group->notification_mutex);
226 list_for_each_entry(holder, &group->notification_list, event_list)
227 send_len += FAN_EVENT_METADATA_LEN;
228 mutex_unlock(&group->notification_mutex);
229 ret = put_user(send_len, (int __user *) p);
236 static const struct file_operations fanotify_fops = {
237 .poll = fanotify_poll,
238 .read = fanotify_read,
240 .release = fanotify_release,
241 .unlocked_ioctl = fanotify_ioctl,
242 .compat_ioctl = fanotify_ioctl,
245 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
247 kmem_cache_free(fanotify_mark_cache, fsn_mark);
250 static int fanotify_find_path(int dfd, const char __user *filename,
251 struct path *path, unsigned int flags)
255 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
256 dfd, filename, flags);
258 if (filename == NULL) {
263 file = fget_light(dfd, &fput_needed);
268 if ((flags & FAN_MARK_ONLYDIR) &&
269 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
270 fput_light(file, fput_needed);
274 *path = file->f_path;
276 fput_light(file, fput_needed);
278 unsigned int lookup_flags = 0;
280 if (!(flags & FAN_MARK_DONT_FOLLOW))
281 lookup_flags |= LOOKUP_FOLLOW;
282 if (flags & FAN_MARK_ONLYDIR)
283 lookup_flags |= LOOKUP_DIRECTORY;
285 ret = user_path_at(dfd, filename, lookup_flags, path);
290 /* you can only watch an inode if you have read permissions on it */
291 ret = inode_permission(path->dentry->d_inode, MAY_READ);
298 static void fanotify_update_object_mask(struct fsnotify_group *group,
300 struct vfsmount *mnt,
301 struct fsnotify_mark *fsn_mark,
305 __u32 old_mask, new_mask;
307 pr_debug("%s: group=%p inode=%p mnt=%p fsn_mark=%p flags=%x mask=%x\n",
308 __func__, group, inode, mnt, fsn_mark, flags, mask);
310 spin_lock(&fsn_mark->lock);
311 old_mask = fsn_mark->mask;
312 if (flags & FAN_MARK_ADD)
313 fsn_mark->mask |= mask;
314 else if (flags & FAN_MARK_REMOVE)
315 fsn_mark->mask &= ~mask;
318 new_mask = fsn_mark->mask;
319 spin_unlock(&fsn_mark->lock);
322 fsnotify_destroy_mark(fsn_mark);
324 /* we made changes to a mask, update the group mask and the object mask
325 * so things happen quickly. */
326 if (old_mask != new_mask) {
327 __u32 dropped, do_object, do_group;
329 /* more bits in old than in new? */
330 dropped = (old_mask & ~new_mask);
331 /* more bits in this fsn_mark than the group? */
332 do_group = (new_mask & ~group->mask);
335 /* more bits in this fsn_mark than the object's mask? */
336 do_object = (new_mask & ~inode->i_fsnotify_mask);
337 /* update the object with this new fsn_mark */
338 if (dropped || do_object)
339 fsnotify_recalc_inode_mask(inode);
341 /* more bits in this fsn_mark than the object's mask? */
342 do_object = (new_mask & ~mnt->mnt_fsnotify_mask);
343 /* update the object with this new fsn_mark */
344 if (dropped || do_object)
345 fsnotify_recalc_vfsmount_mask(mnt);
350 /* update the group mask with the new mask */
351 if (dropped || do_group)
352 fsnotify_recalc_group_mask(group);
356 static int fanotify_remove_mark(struct fsnotify_group *group, struct inode *inode,
357 struct vfsmount *mnt, unsigned int flags, __u32 mask)
359 struct fsnotify_mark *fsn_mark = NULL;
361 BUG_ON(inode && mnt);
362 BUG_ON(!inode && !mnt);
365 fsn_mark = fsnotify_find_inode_mark(group, inode);
367 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
374 fanotify_update_object_mask(group, inode, mnt, fsn_mark, flags, mask);
376 /* matches the fsnotify_find_inode_mark() */
377 fsnotify_put_mark(fsn_mark);
382 static struct fsnotify_mark *fanotify_add_vfsmount_mark(struct fsnotify_group *group,
383 struct vfsmount *mnt)
385 struct fsnotify_mark *fsn_mark;
387 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
389 struct fsnotify_mark *new_fsn_mark;
392 fsn_mark = ERR_PTR(-ENOMEM);
393 new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
397 fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
398 ret = fsnotify_add_mark(new_fsn_mark, group, NULL, mnt, 0);
400 fsn_mark = ERR_PTR(ret);
401 fanotify_free_mark(new_fsn_mark);
405 fsn_mark = new_fsn_mark;
411 static struct fsnotify_mark *fanotify_add_inode_mark(struct fsnotify_group *group,
414 struct fsnotify_mark *fsn_mark;
416 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
418 fsn_mark = fsnotify_find_inode_mark(group, inode);
420 struct fsnotify_mark *new_fsn_mark;
423 fsn_mark = ERR_PTR(-ENOMEM);
424 new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
428 fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
429 ret = fsnotify_add_mark(new_fsn_mark, group, inode, NULL, 0);
431 fsn_mark = ERR_PTR(ret);
432 fanotify_free_mark(new_fsn_mark);
436 fsn_mark = new_fsn_mark;
442 static int fanotify_add_mark(struct fsnotify_group *group, struct inode *inode,
443 struct vfsmount *mnt, unsigned int flags, __u32 mask)
445 struct fsnotify_mark *fsn_mark;
447 pr_debug("%s: group=%p inode=%p mnt=%p flags=%x mask=%x\n",
448 __func__, group, inode, mnt, flags, mask);
450 BUG_ON(inode && mnt);
451 BUG_ON(!inode && !mnt);
454 fsn_mark = fanotify_add_inode_mark(group, inode);
456 fsn_mark = fanotify_add_vfsmount_mark(group, mnt);
460 if (IS_ERR(fsn_mark))
463 fanotify_update_object_mask(group, inode, mnt, fsn_mark, flags, mask);
465 /* match the init or the find.... */
466 fsnotify_put_mark(fsn_mark);
468 return PTR_ERR(fsn_mark);
471 static bool fanotify_mark_validate_input(int flags,
474 pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask);
476 /* are flags valid of this operation? */
477 if (!fanotify_mark_flags_valid(flags))
479 /* is the mask valid? */
480 if (!fanotify_mask_valid(mask))
485 /* fanotify syscalls */
486 SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
487 unsigned int, priority)
489 struct fsnotify_group *group;
492 pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
493 __func__, flags, event_f_flags, priority);
500 if (!capable(CAP_SYS_ADMIN))
503 if (flags & ~FAN_ALL_INIT_FLAGS)
506 f_flags = (O_RDONLY | FMODE_NONOTIFY);
507 if (flags & FAN_CLOEXEC)
508 f_flags |= O_CLOEXEC;
509 if (flags & FAN_NONBLOCK)
510 f_flags |= O_NONBLOCK;
512 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
513 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
515 return PTR_ERR(group);
517 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
524 fsnotify_put_group(group);
528 SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
530 const char __user * pathname)
533 struct fsnotify_group *group;
536 int ret, fput_needed;
538 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
539 __func__, fanotify_fd, flags, dfd, pathname, mask);
541 /* we only use the lower 32 bits as of right now. */
542 if (mask & ((__u64)0xffffffff << 32))
545 if (!fanotify_mark_validate_input(flags, mask))
548 filp = fget_light(fanotify_fd, &fput_needed);
552 /* verify that this is indeed an fanotify instance */
554 if (unlikely(filp->f_op != &fanotify_fops))
557 ret = fanotify_find_path(dfd, pathname, &path, flags);
561 /* inode held in place by reference to path; group by fget on fd */
562 inode = path.dentry->d_inode;
563 group = filp->private_data;
565 /* create/update an inode mark */
566 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
568 ret = fanotify_add_mark(group, inode, NULL, flags, mask);
570 case FAN_MARK_REMOVE:
571 ret = fanotify_remove_mark(group, inode, NULL, flags, mask);
579 fput_light(filp, fput_needed);
583 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
584 asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
585 long dfd, long pathname)
587 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
589 (const char __user *) pathname);
591 SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
595 * fanotify_user_setup - Our initialization function. Note that we cannnot return
596 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
597 * must result in panic().
599 static int __init fanotify_user_setup(void)
601 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
605 device_initcall(fanotify_user_setup);