blob: da01091f93ebf8a04ea069418635f9f8b715ba4c [file] [log] [blame]
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05001#include <linux/fanotify.h>
Eric Paris11637e42009-12-17 21:24:25 -05002#include <linux/fcntl.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05003#include <linux/file.h>
Eric Paris11637e42009-12-17 21:24:25 -05004#include <linux/fs.h>
Eric Paris52c923d2009-12-17 21:24:26 -05005#include <linux/anon_inodes.h>
Eric Paris11637e42009-12-17 21:24:25 -05006#include <linux/fsnotify_backend.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05007#include <linux/init.h>
Eric Parisa1014f12009-12-17 21:24:26 -05008#include <linux/mount.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05009#include <linux/namei.h>
Eric Parisa1014f12009-12-17 21:24:26 -050010#include <linux/poll.h>
Eric Paris11637e42009-12-17 21:24:25 -050011#include <linux/security.h>
12#include <linux/syscalls.h>
Tejun Heoe4e047a2010-05-20 01:36:28 +100013#include <linux/slab.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050014#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050015#include <linux/uaccess.h>
16
17#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050018
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050019extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050020
Eric Paris2a3edf82009-12-17 21:24:26 -050021static struct kmem_cache *fanotify_mark_cache __read_mostly;
Eric Parisb2d87902009-12-17 21:24:34 -050022static struct kmem_cache *fanotify_response_event_cache __read_mostly;
23
24struct fanotify_response_event {
25 struct list_head list;
26 __s32 fd;
27 struct fsnotify_event *event;
28};
Eric Paris2a3edf82009-12-17 21:24:26 -050029
Eric Parisa1014f12009-12-17 21:24:26 -050030/*
31 * Get an fsnotify notification event if one exists and is small
32 * enough to fit in "count". Return an error pointer if the count
33 * is not large enough.
34 *
35 * Called with the group->notification_mutex held.
36 */
37static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
38 size_t count)
39{
40 BUG_ON(!mutex_is_locked(&group->notification_mutex));
41
42 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
43
44 if (fsnotify_notify_queue_is_empty(group))
45 return NULL;
46
47 if (FAN_EVENT_METADATA_LEN > count)
48 return ERR_PTR(-EINVAL);
49
50 /* held the notification_mutex the whole time, so this is the
51 * same event we peeked above */
52 return fsnotify_remove_notify_event(group);
53}
54
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050055static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
Eric Parisa1014f12009-12-17 21:24:26 -050056{
57 int client_fd;
58 struct dentry *dentry;
59 struct vfsmount *mnt;
60 struct file *new_file;
61
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050062 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050063
64 client_fd = get_unused_fd();
65 if (client_fd < 0)
66 return client_fd;
67
68 if (event->data_type != FSNOTIFY_EVENT_PATH) {
69 WARN_ON(1);
70 put_unused_fd(client_fd);
71 return -EINVAL;
72 }
73
74 /*
75 * we need a new file handle for the userspace program so it can read even if it was
76 * originally opened O_WRONLY.
77 */
78 dentry = dget(event->path.dentry);
79 mnt = mntget(event->path.mnt);
80 /* it's possible this event was an overflow event. in that case dentry and mnt
81 * are NULL; That's fine, just don't call dentry open */
82 if (dentry && mnt)
83 new_file = dentry_open(dentry, mnt,
84 O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
85 current_cred());
86 else
87 new_file = ERR_PTR(-EOVERFLOW);
88 if (IS_ERR(new_file)) {
89 /*
90 * we still send an event even if we can't open the file. this
91 * can happen when say tasks are gone and we try to open their
92 * /proc files or we try to open a WRONLY file like in sysfs
93 * we just send the errno to userspace since there isn't much
94 * else we can do.
95 */
96 put_unused_fd(client_fd);
97 client_fd = PTR_ERR(new_file);
98 } else {
99 fd_install(client_fd, new_file);
100 }
101
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500102 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500103}
104
105static ssize_t fill_event_metadata(struct fsnotify_group *group,
106 struct fanotify_event_metadata *metadata,
107 struct fsnotify_event *event)
108{
109 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
110 group, metadata, event);
111
112 metadata->event_len = FAN_EVENT_METADATA_LEN;
113 metadata->vers = FANOTIFY_METADATA_VERSION;
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -0500114 metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500115 metadata->pid = pid_vnr(event->tgid);
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500116 metadata->fd = create_fd(group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500117
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500118 return metadata->fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500119}
120
Eric Parisb2d87902009-12-17 21:24:34 -0500121#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
122static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
123 __s32 fd)
124{
125 struct fanotify_response_event *re, *return_re = NULL;
126
127 mutex_lock(&group->fanotify_data.access_mutex);
128 list_for_each_entry(re, &group->fanotify_data.access_list, list) {
129 if (re->fd != fd)
130 continue;
131
132 list_del_init(&re->list);
133 return_re = re;
134 break;
135 }
136 mutex_unlock(&group->fanotify_data.access_mutex);
137
138 pr_debug("%s: found return_re=%p\n", __func__, return_re);
139
140 return return_re;
141}
142
143static int process_access_response(struct fsnotify_group *group,
144 struct fanotify_response *response_struct)
145{
146 struct fanotify_response_event *re;
147 __s32 fd = response_struct->fd;
148 __u32 response = response_struct->response;
149
150 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
151 fd, response);
152 /*
153 * make sure the response is valid, if invalid we do nothing and either
154 * userspace can send a valid responce or we will clean it up after the
155 * timeout
156 */
157 switch (response) {
158 case FAN_ALLOW:
159 case FAN_DENY:
160 break;
161 default:
162 return -EINVAL;
163 }
164
165 if (fd < 0)
166 return -EINVAL;
167
168 re = dequeue_re(group, fd);
169 if (!re)
170 return -ENOENT;
171
172 re->event->response = response;
173
174 wake_up(&group->fanotify_data.access_waitq);
175
176 kmem_cache_free(fanotify_response_event_cache, re);
177
178 return 0;
179}
180
181static int prepare_for_access_response(struct fsnotify_group *group,
182 struct fsnotify_event *event,
183 __s32 fd)
184{
185 struct fanotify_response_event *re;
186
187 if (!(event->mask & FAN_ALL_PERM_EVENTS))
188 return 0;
189
190 re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
191 if (!re)
192 return -ENOMEM;
193
194 re->event = event;
195 re->fd = fd;
196
197 mutex_lock(&group->fanotify_data.access_mutex);
198 list_add_tail(&re->list, &group->fanotify_data.access_list);
199 mutex_unlock(&group->fanotify_data.access_mutex);
200
201 return 0;
202}
203
204static void remove_access_response(struct fsnotify_group *group,
205 struct fsnotify_event *event,
206 __s32 fd)
207{
208 struct fanotify_response_event *re;
209
210 if (!(event->mask & FAN_ALL_PERM_EVENTS))
211 return;
212
213 re = dequeue_re(group, fd);
214 if (!re)
215 return;
216
217 BUG_ON(re->event != event);
218
219 kmem_cache_free(fanotify_response_event_cache, re);
220
221 return;
222}
223#else
224static int prepare_for_access_response(struct fsnotify_group *group,
225 struct fsnotify_event *event,
226 __s32 fd)
227{
228 return 0;
229}
230
231static void remove_access_response(struct fsnotify_group *group,
232 struct fsnotify_event *event,
233 __s32 fd)
234{
Eric Paris8860f062009-12-23 00:10:25 -0500235 return;
Eric Parisb2d87902009-12-17 21:24:34 -0500236}
237#endif
238
Eric Parisa1014f12009-12-17 21:24:26 -0500239static ssize_t copy_event_to_user(struct fsnotify_group *group,
240 struct fsnotify_event *event,
241 char __user *buf)
242{
243 struct fanotify_event_metadata fanotify_event_metadata;
Eric Parisb2d87902009-12-17 21:24:34 -0500244 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500245
246 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
247
Eric Parisb2d87902009-12-17 21:24:34 -0500248 fd = fill_event_metadata(group, &fanotify_event_metadata, event);
249 if (fd < 0)
250 return fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500251
Eric Parisb2d87902009-12-17 21:24:34 -0500252 ret = prepare_for_access_response(group, event, fd);
253 if (ret)
254 goto out_close_fd;
255
256 ret = -EFAULT;
Eric Parisa1014f12009-12-17 21:24:26 -0500257 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
Eric Parisb2d87902009-12-17 21:24:34 -0500258 goto out_kill_access_response;
Eric Parisa1014f12009-12-17 21:24:26 -0500259
260 return FAN_EVENT_METADATA_LEN;
Eric Parisb2d87902009-12-17 21:24:34 -0500261
262out_kill_access_response:
263 remove_access_response(group, event, fd);
264out_close_fd:
265 sys_close(fd);
266 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500267}
268
269/* intofiy userspace file descriptor functions */
270static unsigned int fanotify_poll(struct file *file, poll_table *wait)
271{
272 struct fsnotify_group *group = file->private_data;
273 int ret = 0;
274
275 poll_wait(file, &group->notification_waitq, wait);
276 mutex_lock(&group->notification_mutex);
277 if (!fsnotify_notify_queue_is_empty(group))
278 ret = POLLIN | POLLRDNORM;
279 mutex_unlock(&group->notification_mutex);
280
281 return ret;
282}
283
284static ssize_t fanotify_read(struct file *file, char __user *buf,
285 size_t count, loff_t *pos)
286{
287 struct fsnotify_group *group;
288 struct fsnotify_event *kevent;
289 char __user *start;
290 int ret;
291 DEFINE_WAIT(wait);
292
293 start = buf;
294 group = file->private_data;
295
296 pr_debug("%s: group=%p\n", __func__, group);
297
298 while (1) {
299 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
300
301 mutex_lock(&group->notification_mutex);
302 kevent = get_one_event(group, count);
303 mutex_unlock(&group->notification_mutex);
304
305 if (kevent) {
306 ret = PTR_ERR(kevent);
307 if (IS_ERR(kevent))
308 break;
309 ret = copy_event_to_user(group, kevent, buf);
310 fsnotify_put_event(kevent);
311 if (ret < 0)
312 break;
313 buf += ret;
314 count -= ret;
315 continue;
316 }
317
318 ret = -EAGAIN;
319 if (file->f_flags & O_NONBLOCK)
320 break;
321 ret = -EINTR;
322 if (signal_pending(current))
323 break;
324
325 if (start != buf)
326 break;
327
328 schedule();
329 }
330
331 finish_wait(&group->notification_waitq, &wait);
332 if (start != buf && ret != -EFAULT)
333 ret = buf - start;
334 return ret;
335}
336
Eric Parisb2d87902009-12-17 21:24:34 -0500337static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
338{
339#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
340 struct fanotify_response response = { .fd = -1, .response = -1 };
341 struct fsnotify_group *group;
342 int ret;
343
344 group = file->private_data;
345
346 if (count > sizeof(response))
347 count = sizeof(response);
348
349 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
350
351 if (copy_from_user(&response, buf, count))
352 return -EFAULT;
353
354 ret = process_access_response(group, &response);
355 if (ret < 0)
356 count = ret;
357
358 return count;
359#else
360 return -EINVAL;
361#endif
362}
363
Eric Paris52c923d2009-12-17 21:24:26 -0500364static int fanotify_release(struct inode *ignored, struct file *file)
365{
366 struct fsnotify_group *group = file->private_data;
367
368 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
369
370 /* matches the fanotify_init->fsnotify_alloc_group */
371 fsnotify_put_group(group);
372
373 return 0;
374}
375
Eric Parisa1014f12009-12-17 21:24:26 -0500376static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
377{
378 struct fsnotify_group *group;
379 struct fsnotify_event_holder *holder;
380 void __user *p;
381 int ret = -ENOTTY;
382 size_t send_len = 0;
383
384 group = file->private_data;
385
386 p = (void __user *) arg;
387
388 switch (cmd) {
389 case FIONREAD:
390 mutex_lock(&group->notification_mutex);
391 list_for_each_entry(holder, &group->notification_list, event_list)
392 send_len += FAN_EVENT_METADATA_LEN;
393 mutex_unlock(&group->notification_mutex);
394 ret = put_user(send_len, (int __user *) p);
395 break;
396 }
397
398 return ret;
399}
400
Eric Paris52c923d2009-12-17 21:24:26 -0500401static const struct file_operations fanotify_fops = {
Eric Parisa1014f12009-12-17 21:24:26 -0500402 .poll = fanotify_poll,
403 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500404 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500405 .fasync = NULL,
406 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500407 .unlocked_ioctl = fanotify_ioctl,
408 .compat_ioctl = fanotify_ioctl,
Eric Paris52c923d2009-12-17 21:24:26 -0500409};
410
Eric Paris2a3edf82009-12-17 21:24:26 -0500411static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
412{
413 kmem_cache_free(fanotify_mark_cache, fsn_mark);
414}
415
416static int fanotify_find_path(int dfd, const char __user *filename,
417 struct path *path, unsigned int flags)
418{
419 int ret;
420
421 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
422 dfd, filename, flags);
423
424 if (filename == NULL) {
425 struct file *file;
426 int fput_needed;
427
428 ret = -EBADF;
429 file = fget_light(dfd, &fput_needed);
430 if (!file)
431 goto out;
432
433 ret = -ENOTDIR;
434 if ((flags & FAN_MARK_ONLYDIR) &&
435 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
436 fput_light(file, fput_needed);
437 goto out;
438 }
439
440 *path = file->f_path;
441 path_get(path);
442 fput_light(file, fput_needed);
443 } else {
444 unsigned int lookup_flags = 0;
445
446 if (!(flags & FAN_MARK_DONT_FOLLOW))
447 lookup_flags |= LOOKUP_FOLLOW;
448 if (flags & FAN_MARK_ONLYDIR)
449 lookup_flags |= LOOKUP_DIRECTORY;
450
451 ret = user_path_at(dfd, filename, lookup_flags, path);
452 if (ret)
453 goto out;
454 }
455
456 /* you can only watch an inode if you have read permissions on it */
457 ret = inode_permission(path->dentry->d_inode, MAY_READ);
458 if (ret)
459 path_put(path);
460out:
461 return ret;
462}
463
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500464static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
465 __u32 mask,
466 unsigned int flags)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500467{
468 __u32 oldmask;
469
470 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500471 if (!(flags & FAN_MARK_IGNORED_MASK)) {
472 oldmask = fsn_mark->mask;
473 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
474 } else {
475 oldmask = fsn_mark->ignored_mask;
476 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
477 }
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500478 spin_unlock(&fsn_mark->lock);
479
480 if (!(oldmask & ~mask))
481 fsnotify_destroy_mark(fsn_mark);
482
483 return mask & oldmask;
484}
485
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500486static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500487 struct vfsmount *mnt, __u32 mask,
488 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500489{
490 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500491 __u32 removed;
Eric Paris88826272009-12-17 21:24:28 -0500492
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500493 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
494 if (!fsn_mark)
495 return -ENOENT;
Eric Paris88826272009-12-17 21:24:28 -0500496
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500497 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500498 fsnotify_put_mark(fsn_mark);
499 if (removed & group->mask)
500 fsnotify_recalc_group_mask(group);
501 if (removed & mnt->mnt_fsnotify_mask)
502 fsnotify_recalc_vfsmount_mask(mnt);
Eric Paris88826272009-12-17 21:24:28 -0500503
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500504 return 0;
505}
506
507static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500508 struct inode *inode, __u32 mask,
509 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500510{
511 struct fsnotify_mark *fsn_mark = NULL;
512 __u32 removed;
513
514 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris88826272009-12-17 21:24:28 -0500515 if (!fsn_mark)
516 return -ENOENT;
517
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500518 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Eric Paris5444e292009-12-17 21:24:27 -0500519 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500520 fsnotify_put_mark(fsn_mark);
521
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500522 if (removed & group->mask)
523 fsnotify_recalc_group_mask(group);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500524 if (removed & inode->i_fsnotify_mask)
525 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500526
Eric Paris2a3edf82009-12-17 21:24:26 -0500527 return 0;
528}
529
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500530static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
531 __u32 mask,
532 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500533{
534 __u32 oldmask;
535
536 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500537 if (!(flags & FAN_MARK_IGNORED_MASK)) {
538 oldmask = fsn_mark->mask;
539 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
540 } else {
541 oldmask = fsn_mark->ignored_mask;
542 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
Eric Parisc9778a92009-12-17 21:24:33 -0500543 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
544 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500545 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500546 spin_unlock(&fsn_mark->lock);
547
548 return mask & ~oldmask;
549}
550
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500551static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500552 struct vfsmount *mnt, __u32 mask,
553 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500554{
555 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500556 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500557
Eric Paris88826272009-12-17 21:24:28 -0500558 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
559 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500560 int ret;
561
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500562 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
563 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500564 return -ENOMEM;
Eric Paris88826272009-12-17 21:24:28 -0500565
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500566 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
567 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
Eric Paris88826272009-12-17 21:24:28 -0500568 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500569 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500570 return ret;
Eric Paris88826272009-12-17 21:24:28 -0500571 }
Eric Paris88826272009-12-17 21:24:28 -0500572 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500573 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500574 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500575 if (added) {
576 if (added & ~group->mask)
577 fsnotify_recalc_group_mask(group);
578 if (added & ~mnt->mnt_fsnotify_mask)
579 fsnotify_recalc_vfsmount_mask(mnt);
580 }
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500581 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500582}
583
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500584static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500585 struct inode *inode, __u32 mask,
586 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500587{
588 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500589 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500590
591 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500592
Eric Paris5444e292009-12-17 21:24:27 -0500593 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500594 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500595 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500596
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500597 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
598 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500599 return -ENOMEM;
Eric Paris2a3edf82009-12-17 21:24:26 -0500600
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500601 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
602 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
Eric Paris2a3edf82009-12-17 21:24:26 -0500603 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500604 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500605 return ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500606 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500607 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500608 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500609 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500610 if (added) {
611 if (added & ~group->mask)
612 fsnotify_recalc_group_mask(group);
613 if (added & ~inode->i_fsnotify_mask)
614 fsnotify_recalc_inode_mask(inode);
615 }
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500616 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500617}
Eric Paris2a3edf82009-12-17 21:24:26 -0500618
Eric Paris52c923d2009-12-17 21:24:26 -0500619/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400620SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500621{
Eric Paris52c923d2009-12-17 21:24:26 -0500622 struct fsnotify_group *group;
623 int f_flags, fd;
624
Eric Paris08ae8932010-05-27 09:41:40 -0400625 pr_debug("%s: flags=%d event_f_flags=%d\n",
626 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500627
628 if (event_f_flags)
629 return -EINVAL;
Eric Paris52c923d2009-12-17 21:24:26 -0500630
631 if (!capable(CAP_SYS_ADMIN))
632 return -EACCES;
633
634 if (flags & ~FAN_ALL_INIT_FLAGS)
635 return -EINVAL;
636
Eric Parisb2d87902009-12-17 21:24:34 -0500637 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500638 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
Eric Paris9e66e422009-12-17 21:24:34 -0500648#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
649 mutex_init(&group->fanotify_data.access_mutex);
650 init_waitqueue_head(&group->fanotify_data.access_waitq);
651 INIT_LIST_HEAD(&group->fanotify_data.access_list);
652#endif
Eric Pariscb2d4292009-12-17 21:24:34 -0500653
Eric Paris52c923d2009-12-17 21:24:26 -0500654 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
655 if (fd < 0)
656 goto out_put_group;
657
658 return fd;
659
660out_put_group:
661 fsnotify_put_group(group);
662 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500663}
Eric Parisbbaa4162009-12-17 21:24:26 -0500664
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500665SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
666 __u64 mask, int dfd,
667 const char __user * pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500668{
Eric Paris0ff21db2009-12-17 21:24:29 -0500669 struct inode *inode = NULL;
670 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500671 struct fsnotify_group *group;
672 struct file *filp;
673 struct path path;
674 int ret, fput_needed;
675
676 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
677 __func__, fanotify_fd, flags, dfd, pathname, mask);
678
679 /* we only use the lower 32 bits as of right now. */
680 if (mask & ((__u64)0xffffffff << 32))
681 return -EINVAL;
682
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500683 if (flags & ~FAN_ALL_MARK_FLAGS)
684 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500685 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500686 case FAN_MARK_ADD:
687 case FAN_MARK_REMOVE:
Eric Paris4d926042009-12-17 21:24:34 -0500688 case FAN_MARK_FLUSH:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500689 break;
690 default:
691 return -EINVAL;
692 }
Eric Parisb2d87902009-12-17 21:24:34 -0500693#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
694 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
695#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500696 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500697#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500698 return -EINVAL;
699
700 filp = fget_light(fanotify_fd, &fput_needed);
701 if (unlikely(!filp))
702 return -EBADF;
703
704 /* verify that this is indeed an fanotify instance */
705 ret = -EINVAL;
706 if (unlikely(filp->f_op != &fanotify_fops))
707 goto fput_and_out;
708
709 ret = fanotify_find_path(dfd, pathname, &path, flags);
710 if (ret)
711 goto fput_and_out;
712
713 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500714 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500715 inode = path.dentry->d_inode;
716 else
717 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500718 group = filp->private_data;
719
720 /* create/update an inode mark */
Eric Paris4d926042009-12-17 21:24:34 -0500721 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500722 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500723 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500724 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500725 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500726 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500727 break;
728 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500729 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500730 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500731 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500732 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500733 break;
Eric Paris4d926042009-12-17 21:24:34 -0500734 case FAN_MARK_FLUSH:
735 if (flags & FAN_MARK_MOUNT)
736 fsnotify_clear_vfsmount_marks_by_group(group);
737 else
738 fsnotify_clear_inode_marks_by_group(group);
739 fsnotify_recalc_group_mask(group);
740 break;
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500741 default:
742 ret = -EINVAL;
743 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500744
745 path_put(&path);
746fput_and_out:
747 fput_light(filp, fput_needed);
748 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500749}
Eric Paris2a3edf82009-12-17 21:24:26 -0500750
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500751#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
752asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
753 long dfd, long pathname)
754{
755 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
756 mask, (int) dfd,
757 (const char __user *) pathname);
758}
759SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
760#endif
761
Eric Paris2a3edf82009-12-17 21:24:26 -0500762/*
763 * fanotify_user_setup - Our initialization function. Note that we cannnot return
764 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
765 * must result in panic().
766 */
767static int __init fanotify_user_setup(void)
768{
769 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Eric Parisb2d87902009-12-17 21:24:34 -0500770 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
771 SLAB_PANIC);
Eric Paris2a3edf82009-12-17 21:24:26 -0500772
773 return 0;
774}
775device_initcall(fanotify_user_setup);