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->fd = create_fd(group, event);
111 static ssize_t copy_event_to_user(struct fsnotify_group *group,
112 struct fsnotify_event *event,
115 struct fanotify_event_metadata fanotify_event_metadata;
118 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
120 ret = fill_event_metadata(group, &fanotify_event_metadata, event);
124 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
127 return FAN_EVENT_METADATA_LEN;
130 /* intofiy userspace file descriptor functions */
131 static unsigned int fanotify_poll(struct file *file, poll_table *wait)
133 struct fsnotify_group *group = file->private_data;
136 poll_wait(file, &group->notification_waitq, wait);
137 mutex_lock(&group->notification_mutex);
138 if (!fsnotify_notify_queue_is_empty(group))
139 ret = POLLIN | POLLRDNORM;
140 mutex_unlock(&group->notification_mutex);
145 static ssize_t fanotify_read(struct file *file, char __user *buf,
146 size_t count, loff_t *pos)
148 struct fsnotify_group *group;
149 struct fsnotify_event *kevent;
155 group = file->private_data;
157 pr_debug("%s: group=%p\n", __func__, group);
160 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
162 mutex_lock(&group->notification_mutex);
163 kevent = get_one_event(group, count);
164 mutex_unlock(&group->notification_mutex);
167 ret = PTR_ERR(kevent);
170 ret = copy_event_to_user(group, kevent, buf);
171 fsnotify_put_event(kevent);
180 if (file->f_flags & O_NONBLOCK)
183 if (signal_pending(current))
192 finish_wait(&group->notification_waitq, &wait);
193 if (start != buf && ret != -EFAULT)
198 static int fanotify_release(struct inode *ignored, struct file *file)
200 struct fsnotify_group *group = file->private_data;
202 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
204 /* matches the fanotify_init->fsnotify_alloc_group */
205 fsnotify_put_group(group);
210 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
212 struct fsnotify_group *group;
213 struct fsnotify_event_holder *holder;
218 group = file->private_data;
220 p = (void __user *) arg;
224 mutex_lock(&group->notification_mutex);
225 list_for_each_entry(holder, &group->notification_list, event_list)
226 send_len += FAN_EVENT_METADATA_LEN;
227 mutex_unlock(&group->notification_mutex);
228 ret = put_user(send_len, (int __user *) p);
235 static const struct file_operations fanotify_fops = {
236 .poll = fanotify_poll,
237 .read = fanotify_read,
239 .release = fanotify_release,
240 .unlocked_ioctl = fanotify_ioctl,
241 .compat_ioctl = fanotify_ioctl,
244 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
246 kmem_cache_free(fanotify_mark_cache, fsn_mark);
249 static int fanotify_find_path(int dfd, const char __user *filename,
250 struct path *path, unsigned int flags)
254 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
255 dfd, filename, flags);
257 if (filename == NULL) {
262 file = fget_light(dfd, &fput_needed);
267 if ((flags & FAN_MARK_ONLYDIR) &&
268 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
269 fput_light(file, fput_needed);
273 *path = file->f_path;
275 fput_light(file, fput_needed);
277 unsigned int lookup_flags = 0;
279 if (!(flags & FAN_MARK_DONT_FOLLOW))
280 lookup_flags |= LOOKUP_FOLLOW;
281 if (flags & FAN_MARK_ONLYDIR)
282 lookup_flags |= LOOKUP_DIRECTORY;
284 ret = user_path_at(dfd, filename, lookup_flags, path);
289 /* you can only watch an inode if you have read permissions on it */
290 ret = inode_permission(path->dentry->d_inode, MAY_READ);
297 static int fanotify_remove_mark(struct fsnotify_group *group,
301 struct fsnotify_mark *fsn_mark;
304 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
307 fsn_mark = fsnotify_find_mark(group, inode);
311 spin_lock(&fsn_mark->lock);
312 fsn_mark->mask &= ~mask;
313 new_mask = fsn_mark->mask;
314 spin_unlock(&fsn_mark->lock);
317 fsnotify_destroy_mark(fsn_mark);
319 fsnotify_recalc_inode_mask(inode);
321 fsnotify_recalc_group_mask(group);
323 /* matches the fsnotify_find_mark() */
324 fsnotify_put_mark(fsn_mark);
329 static int fanotify_add_mark(struct fsnotify_group *group,
333 struct fsnotify_mark *fsn_mark;
334 __u32 old_mask, new_mask;
337 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
340 fsn_mark = fsnotify_find_mark(group, inode);
342 struct fsnotify_mark *new_fsn_mark;
345 new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
349 fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
350 ret = fsnotify_add_mark(new_fsn_mark, group, inode, 0);
352 fanotify_free_mark(new_fsn_mark);
356 fsn_mark = new_fsn_mark;
361 spin_lock(&fsn_mark->lock);
362 old_mask = fsn_mark->mask;
363 fsn_mark->mask |= mask;
364 new_mask = fsn_mark->mask;
365 spin_unlock(&fsn_mark->lock);
367 /* we made changes to a mask, update the group mask and the inode mask
368 * so things happen quickly. */
369 if (old_mask != new_mask) {
370 /* more bits in old than in new? */
371 int dropped = (old_mask & ~new_mask);
372 /* more bits in this mark than the inode's mask? */
373 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
374 /* more bits in this mark than the group? */
375 int do_group = (new_mask & ~group->mask);
377 /* update the inode with this new mark */
378 if (dropped || do_inode)
379 fsnotify_recalc_inode_mask(inode);
381 /* update the group mask with the new mask */
382 if (dropped || do_group)
383 fsnotify_recalc_group_mask(group);
386 /* match the init or the find.... */
387 fsnotify_put_mark(fsn_mark);
392 static int fanotify_update_mark(struct fsnotify_group *group,
393 struct inode *inode, int flags,
396 pr_debug("%s: group=%p inode=%p flags=%x mask=%x\n", __func__,
397 group, inode, flags, mask);
399 if (flags & FAN_MARK_ADD)
400 fanotify_add_mark(group, inode, mask);
401 else if (flags & FAN_MARK_REMOVE)
402 fanotify_remove_mark(group, inode, mask);
409 static bool fanotify_mark_validate_input(int flags,
412 pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask);
414 /* are flags valid of this operation? */
415 if (!fanotify_mark_flags_valid(flags))
417 /* is the mask valid? */
418 if (!fanotify_mask_valid(mask))
423 /* fanotify syscalls */
424 SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
425 unsigned int, priority)
427 struct fsnotify_group *group;
430 pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
431 __func__, flags, event_f_flags, priority);
438 if (!capable(CAP_SYS_ADMIN))
441 if (flags & ~FAN_ALL_INIT_FLAGS)
444 f_flags = (O_RDONLY | FMODE_NONOTIFY);
445 if (flags & FAN_CLOEXEC)
446 f_flags |= O_CLOEXEC;
447 if (flags & FAN_NONBLOCK)
448 f_flags |= O_NONBLOCK;
450 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
451 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
453 return PTR_ERR(group);
455 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
462 fsnotify_put_group(group);
466 SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
468 const char __user * pathname)
471 struct fsnotify_group *group;
474 int ret, fput_needed;
476 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
477 __func__, fanotify_fd, flags, dfd, pathname, mask);
479 /* we only use the lower 32 bits as of right now. */
480 if (mask & ((__u64)0xffffffff << 32))
483 if (!fanotify_mark_validate_input(flags, mask))
486 filp = fget_light(fanotify_fd, &fput_needed);
490 /* verify that this is indeed an fanotify instance */
492 if (unlikely(filp->f_op != &fanotify_fops))
495 ret = fanotify_find_path(dfd, pathname, &path, flags);
499 /* inode held in place by reference to path; group by fget on fd */
500 inode = path.dentry->d_inode;
501 group = filp->private_data;
503 /* create/update an inode mark */
504 ret = fanotify_update_mark(group, inode, flags, mask);
508 fput_light(filp, fput_needed);
512 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
513 asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
514 long dfd, long pathname)
516 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
518 (const char __user *) pathname);
520 SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
524 * fanotify_user_setup - Our initialization function. Note that we cannnot return
525 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
526 * must result in panic().
528 static int __init fanotify_user_setup(void)
530 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
534 device_initcall(fanotify_user_setup);