blob: cf9c3000982534b3f382b08ff966f9b3a594367c [file] [log] [blame]
Eric Paris11637e42009-12-17 21:24:25 -05001#include <linux/fcntl.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05002#include <linux/file.h>
Eric Paris11637e42009-12-17 21:24:25 -05003#include <linux/fs.h>
Eric Paris52c923d2009-12-17 21:24:26 -05004#include <linux/anon_inodes.h>
Eric Paris11637e42009-12-17 21:24:25 -05005#include <linux/fsnotify_backend.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05006#include <linux/init.h>
Eric Parisa1014f12009-12-17 21:24:26 -05007#include <linux/mount.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05008#include <linux/namei.h>
Eric Parisa1014f12009-12-17 21:24:26 -05009#include <linux/poll.h>
Eric Paris11637e42009-12-17 21:24:25 -050010#include <linux/security.h>
11#include <linux/syscalls.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050012#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050013#include <linux/uaccess.h>
14
15#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050016
17#include "fanotify.h"
18
Eric Paris2a3edf82009-12-17 21:24:26 -050019static struct kmem_cache *fanotify_mark_cache __read_mostly;
20
Eric Parisa1014f12009-12-17 21:24:26 -050021/*
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.
25 *
26 * Called with the group->notification_mutex held.
27 */
28static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
29 size_t count)
30{
31 BUG_ON(!mutex_is_locked(&group->notification_mutex));
32
33 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
34
35 if (fsnotify_notify_queue_is_empty(group))
36 return NULL;
37
38 if (FAN_EVENT_METADATA_LEN > count)
39 return ERR_PTR(-EINVAL);
40
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);
44}
45
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050046static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
Eric Parisa1014f12009-12-17 21:24:26 -050047{
48 int client_fd;
49 struct dentry *dentry;
50 struct vfsmount *mnt;
51 struct file *new_file;
52
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050053 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050054
55 client_fd = get_unused_fd();
56 if (client_fd < 0)
57 return client_fd;
58
59 if (event->data_type != FSNOTIFY_EVENT_PATH) {
60 WARN_ON(1);
61 put_unused_fd(client_fd);
62 return -EINVAL;
63 }
64
65 /*
66 * we need a new file handle for the userspace program so it can read even if it was
67 * originally opened O_WRONLY.
68 */
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 */
73 if (dentry && mnt)
74 new_file = dentry_open(dentry, mnt,
75 O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
76 current_cred());
77 else
78 new_file = ERR_PTR(-EOVERFLOW);
79 if (IS_ERR(new_file)) {
80 /*
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
85 * else we can do.
86 */
87 put_unused_fd(client_fd);
88 client_fd = PTR_ERR(new_file);
89 } else {
90 fd_install(client_fd, new_file);
91 }
92
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050093 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050094}
95
96static ssize_t fill_event_metadata(struct fsnotify_group *group,
97 struct fanotify_event_metadata *metadata,
98 struct fsnotify_event *event)
99{
100 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
101 group, metadata, event);
102
103 metadata->event_len = FAN_EVENT_METADATA_LEN;
104 metadata->vers = FANOTIFY_METADATA_VERSION;
105 metadata->mask = fanotify_outgoing_mask(event->mask);
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500106 metadata->fd = create_fd(group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500107
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500108 return metadata->fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500109}
110
111static ssize_t copy_event_to_user(struct fsnotify_group *group,
112 struct fsnotify_event *event,
113 char __user *buf)
114{
115 struct fanotify_event_metadata fanotify_event_metadata;
116 int ret;
117
118 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
119
120 ret = fill_event_metadata(group, &fanotify_event_metadata, event);
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500121 if (ret < 0)
Eric Parisa1014f12009-12-17 21:24:26 -0500122 return ret;
123
124 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
125 return -EFAULT;
126
127 return FAN_EVENT_METADATA_LEN;
128}
129
130/* intofiy userspace file descriptor functions */
131static unsigned int fanotify_poll(struct file *file, poll_table *wait)
132{
133 struct fsnotify_group *group = file->private_data;
134 int ret = 0;
135
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);
141
142 return ret;
143}
144
145static ssize_t fanotify_read(struct file *file, char __user *buf,
146 size_t count, loff_t *pos)
147{
148 struct fsnotify_group *group;
149 struct fsnotify_event *kevent;
150 char __user *start;
151 int ret;
152 DEFINE_WAIT(wait);
153
154 start = buf;
155 group = file->private_data;
156
157 pr_debug("%s: group=%p\n", __func__, group);
158
159 while (1) {
160 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
161
162 mutex_lock(&group->notification_mutex);
163 kevent = get_one_event(group, count);
164 mutex_unlock(&group->notification_mutex);
165
166 if (kevent) {
167 ret = PTR_ERR(kevent);
168 if (IS_ERR(kevent))
169 break;
170 ret = copy_event_to_user(group, kevent, buf);
171 fsnotify_put_event(kevent);
172 if (ret < 0)
173 break;
174 buf += ret;
175 count -= ret;
176 continue;
177 }
178
179 ret = -EAGAIN;
180 if (file->f_flags & O_NONBLOCK)
181 break;
182 ret = -EINTR;
183 if (signal_pending(current))
184 break;
185
186 if (start != buf)
187 break;
188
189 schedule();
190 }
191
192 finish_wait(&group->notification_waitq, &wait);
193 if (start != buf && ret != -EFAULT)
194 ret = buf - start;
195 return ret;
196}
197
Eric Paris52c923d2009-12-17 21:24:26 -0500198static int fanotify_release(struct inode *ignored, struct file *file)
199{
200 struct fsnotify_group *group = file->private_data;
201
202 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
203
204 /* matches the fanotify_init->fsnotify_alloc_group */
205 fsnotify_put_group(group);
206
207 return 0;
208}
209
Eric Parisa1014f12009-12-17 21:24:26 -0500210static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
211{
212 struct fsnotify_group *group;
213 struct fsnotify_event_holder *holder;
214 void __user *p;
215 int ret = -ENOTTY;
216 size_t send_len = 0;
217
218 group = file->private_data;
219
220 p = (void __user *) arg;
221
222 switch (cmd) {
223 case FIONREAD:
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);
229 break;
230 }
231
232 return ret;
233}
234
Eric Paris52c923d2009-12-17 21:24:26 -0500235static const struct file_operations fanotify_fops = {
Eric Parisa1014f12009-12-17 21:24:26 -0500236 .poll = fanotify_poll,
237 .read = fanotify_read,
Eric Paris52c923d2009-12-17 21:24:26 -0500238 .fasync = NULL,
239 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500240 .unlocked_ioctl = fanotify_ioctl,
241 .compat_ioctl = fanotify_ioctl,
Eric Paris52c923d2009-12-17 21:24:26 -0500242};
243
Eric Paris2a3edf82009-12-17 21:24:26 -0500244static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
245{
246 kmem_cache_free(fanotify_mark_cache, fsn_mark);
247}
248
249static int fanotify_find_path(int dfd, const char __user *filename,
250 struct path *path, unsigned int flags)
251{
252 int ret;
253
254 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
255 dfd, filename, flags);
256
257 if (filename == NULL) {
258 struct file *file;
259 int fput_needed;
260
261 ret = -EBADF;
262 file = fget_light(dfd, &fput_needed);
263 if (!file)
264 goto out;
265
266 ret = -ENOTDIR;
267 if ((flags & FAN_MARK_ONLYDIR) &&
268 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
269 fput_light(file, fput_needed);
270 goto out;
271 }
272
273 *path = file->f_path;
274 path_get(path);
275 fput_light(file, fput_needed);
276 } else {
277 unsigned int lookup_flags = 0;
278
279 if (!(flags & FAN_MARK_DONT_FOLLOW))
280 lookup_flags |= LOOKUP_FOLLOW;
281 if (flags & FAN_MARK_ONLYDIR)
282 lookup_flags |= LOOKUP_DIRECTORY;
283
284 ret = user_path_at(dfd, filename, lookup_flags, path);
285 if (ret)
286 goto out;
287 }
288
289 /* you can only watch an inode if you have read permissions on it */
290 ret = inode_permission(path->dentry->d_inode, MAY_READ);
291 if (ret)
292 path_put(path);
293out:
294 return ret;
295}
296
297static int fanotify_remove_mark(struct fsnotify_group *group,
298 struct inode *inode,
299 __u32 mask)
300{
301 struct fsnotify_mark *fsn_mark;
302 __u32 new_mask;
303
304 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
305 group, inode, mask);
306
307 fsn_mark = fsnotify_find_mark(group, inode);
308 if (!fsn_mark)
309 return -ENOENT;
310
311 spin_lock(&fsn_mark->lock);
312 fsn_mark->mask &= ~mask;
313 new_mask = fsn_mark->mask;
314 spin_unlock(&fsn_mark->lock);
315
316 if (!new_mask)
317 fsnotify_destroy_mark(fsn_mark);
318 else
319 fsnotify_recalc_inode_mask(inode);
320
321 fsnotify_recalc_group_mask(group);
322
323 /* matches the fsnotify_find_mark() */
324 fsnotify_put_mark(fsn_mark);
325
326 return 0;
327}
328
329static int fanotify_add_mark(struct fsnotify_group *group,
330 struct inode *inode,
331 __u32 mask)
332{
333 struct fsnotify_mark *fsn_mark;
334 __u32 old_mask, new_mask;
335 int ret;
336
337 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
338 group, inode, mask);
339
340 fsn_mark = fsnotify_find_mark(group, inode);
341 if (!fsn_mark) {
342 struct fsnotify_mark *new_fsn_mark;
343
344 ret = -ENOMEM;
345 new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
346 if (!new_fsn_mark)
347 goto out;
348
349 fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
350 ret = fsnotify_add_mark(new_fsn_mark, group, inode, 0);
351 if (ret) {
352 fanotify_free_mark(new_fsn_mark);
353 goto out;
354 }
355
356 fsn_mark = new_fsn_mark;
357 }
358
359 ret = 0;
360
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);
366
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);
376
377 /* update the inode with this new mark */
378 if (dropped || do_inode)
379 fsnotify_recalc_inode_mask(inode);
380
381 /* update the group mask with the new mask */
382 if (dropped || do_group)
383 fsnotify_recalc_group_mask(group);
384 }
385
386 /* match the init or the find.... */
387 fsnotify_put_mark(fsn_mark);
388out:
389 return ret;
390}
391
392static int fanotify_update_mark(struct fsnotify_group *group,
393 struct inode *inode, int flags,
394 __u32 mask)
395{
396 pr_debug("%s: group=%p inode=%p flags=%x mask=%x\n", __func__,
397 group, inode, flags, mask);
398
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);
403 else
404 BUG();
405
406 return 0;
407}
408
409static bool fanotify_mark_validate_input(int flags,
410 __u32 mask)
411{
412 pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask);
413
414 /* are flags valid of this operation? */
415 if (!fanotify_mark_flags_valid(flags))
416 return false;
417 /* is the mask valid? */
418 if (!fanotify_mask_valid(mask))
419 return false;
420 return true;
421}
422
Eric Paris52c923d2009-12-17 21:24:26 -0500423/* fanotify syscalls */
Eric Paris11637e42009-12-17 21:24:25 -0500424SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
425 unsigned int, priority)
426{
Eric Paris52c923d2009-12-17 21:24:26 -0500427 struct fsnotify_group *group;
428 int f_flags, fd;
429
430 pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
431 __func__, flags, event_f_flags, priority);
432
433 if (event_f_flags)
434 return -EINVAL;
435 if (priority)
436 return -EINVAL;
437
438 if (!capable(CAP_SYS_ADMIN))
439 return -EACCES;
440
441 if (flags & ~FAN_ALL_INIT_FLAGS)
442 return -EINVAL;
443
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;
449
450 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
451 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
452 if (IS_ERR(group))
453 return PTR_ERR(group);
454
455 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
456 if (fd < 0)
457 goto out_put_group;
458
459 return fd;
460
461out_put_group:
462 fsnotify_put_group(group);
463 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500464}
Eric Parisbbaa4162009-12-17 21:24:26 -0500465
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500466SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
467 __u64 mask, int dfd,
468 const char __user * pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500469{
Eric Paris2a3edf82009-12-17 21:24:26 -0500470 struct inode *inode;
471 struct fsnotify_group *group;
472 struct file *filp;
473 struct path path;
474 int ret, fput_needed;
475
476 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
477 __func__, fanotify_fd, flags, dfd, pathname, mask);
478
479 /* we only use the lower 32 bits as of right now. */
480 if (mask & ((__u64)0xffffffff << 32))
481 return -EINVAL;
482
483 if (!fanotify_mark_validate_input(flags, mask))
484 return -EINVAL;
485
486 filp = fget_light(fanotify_fd, &fput_needed);
487 if (unlikely(!filp))
488 return -EBADF;
489
490 /* verify that this is indeed an fanotify instance */
491 ret = -EINVAL;
492 if (unlikely(filp->f_op != &fanotify_fops))
493 goto fput_and_out;
494
495 ret = fanotify_find_path(dfd, pathname, &path, flags);
496 if (ret)
497 goto fput_and_out;
498
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;
502
503 /* create/update an inode mark */
504 ret = fanotify_update_mark(group, inode, flags, mask);
505
506 path_put(&path);
507fput_and_out:
508 fput_light(filp, fput_needed);
509 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500510}
Eric Paris2a3edf82009-12-17 21:24:26 -0500511
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500512#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
513asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
514 long dfd, long pathname)
515{
516 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
517 mask, (int) dfd,
518 (const char __user *) pathname);
519}
520SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
521#endif
522
Eric Paris2a3edf82009-12-17 21:24:26 -0500523/*
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().
527 */
528static int __init fanotify_user_setup(void)
529{
530 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
531
532 return 0;
533}
534device_initcall(fanotify_user_setup);