blob: 04f2fe47b66a35be8df40127f8b1608419bc46f7 [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
Eric Paris2529a0d2010-10-28 17:21:57 -040019#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
20
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050021extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050022
Eric Paris2a3edf82009-12-17 21:24:26 -050023static struct kmem_cache *fanotify_mark_cache __read_mostly;
Eric Parisb2d87902009-12-17 21:24:34 -050024static struct kmem_cache *fanotify_response_event_cache __read_mostly;
25
26struct fanotify_response_event {
27 struct list_head list;
28 __s32 fd;
29 struct fsnotify_event *event;
30};
Eric Paris2a3edf82009-12-17 21:24:26 -050031
Eric Parisa1014f12009-12-17 21:24:26 -050032/*
33 * Get an fsnotify notification event if one exists and is small
34 * enough to fit in "count". Return an error pointer if the count
35 * is not large enough.
36 *
37 * Called with the group->notification_mutex held.
38 */
39static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
40 size_t count)
41{
42 BUG_ON(!mutex_is_locked(&group->notification_mutex));
43
44 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
45
46 if (fsnotify_notify_queue_is_empty(group))
47 return NULL;
48
49 if (FAN_EVENT_METADATA_LEN > count)
50 return ERR_PTR(-EINVAL);
51
52 /* held the notification_mutex the whole time, so this is the
53 * same event we peeked above */
54 return fsnotify_remove_notify_event(group);
55}
56
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050057static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
Eric Parisa1014f12009-12-17 21:24:26 -050058{
59 int client_fd;
60 struct dentry *dentry;
61 struct vfsmount *mnt;
62 struct file *new_file;
63
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050064 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050065
66 client_fd = get_unused_fd();
67 if (client_fd < 0)
68 return client_fd;
69
Linus Torvalds20696012010-08-12 14:23:04 -070070 if (event->data_type != FSNOTIFY_EVENT_PATH) {
Eric Parisa1014f12009-12-17 21:24:26 -050071 WARN_ON(1);
72 put_unused_fd(client_fd);
73 return -EINVAL;
74 }
75
76 /*
77 * we need a new file handle for the userspace program so it can read even if it was
78 * originally opened O_WRONLY.
79 */
Linus Torvalds20696012010-08-12 14:23:04 -070080 dentry = dget(event->path.dentry);
81 mnt = mntget(event->path.mnt);
Eric Parisa1014f12009-12-17 21:24:26 -050082 /* it's possible this event was an overflow event. in that case dentry and mnt
83 * are NULL; That's fine, just don't call dentry open */
84 if (dentry && mnt)
85 new_file = dentry_open(dentry, mnt,
Eric Paris80af2582010-07-28 10:18:37 -040086 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -050087 current_cred());
88 else
89 new_file = ERR_PTR(-EOVERFLOW);
90 if (IS_ERR(new_file)) {
91 /*
92 * we still send an event even if we can't open the file. this
93 * can happen when say tasks are gone and we try to open their
94 * /proc files or we try to open a WRONLY file like in sysfs
95 * we just send the errno to userspace since there isn't much
96 * else we can do.
97 */
98 put_unused_fd(client_fd);
99 client_fd = PTR_ERR(new_file);
100 } else {
101 fd_install(client_fd, new_file);
102 }
103
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500104 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500105}
106
107static ssize_t fill_event_metadata(struct fsnotify_group *group,
108 struct fanotify_event_metadata *metadata,
109 struct fsnotify_event *event)
110{
111 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
112 group, metadata, event);
113
114 metadata->event_len = FAN_EVENT_METADATA_LEN;
115 metadata->vers = FANOTIFY_METADATA_VERSION;
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -0500116 metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500117 metadata->pid = pid_vnr(event->tgid);
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500118 metadata->fd = create_fd(group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500119
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500120 return metadata->fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500121}
122
Eric Parisb2d87902009-12-17 21:24:34 -0500123#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
124static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
125 __s32 fd)
126{
127 struct fanotify_response_event *re, *return_re = NULL;
128
129 mutex_lock(&group->fanotify_data.access_mutex);
130 list_for_each_entry(re, &group->fanotify_data.access_list, list) {
131 if (re->fd != fd)
132 continue;
133
134 list_del_init(&re->list);
135 return_re = re;
136 break;
137 }
138 mutex_unlock(&group->fanotify_data.access_mutex);
139
140 pr_debug("%s: found return_re=%p\n", __func__, return_re);
141
142 return return_re;
143}
144
145static int process_access_response(struct fsnotify_group *group,
146 struct fanotify_response *response_struct)
147{
148 struct fanotify_response_event *re;
149 __s32 fd = response_struct->fd;
150 __u32 response = response_struct->response;
151
152 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
153 fd, response);
154 /*
155 * make sure the response is valid, if invalid we do nothing and either
156 * userspace can send a valid responce or we will clean it up after the
157 * timeout
158 */
159 switch (response) {
160 case FAN_ALLOW:
161 case FAN_DENY:
162 break;
163 default:
164 return -EINVAL;
165 }
166
167 if (fd < 0)
168 return -EINVAL;
169
170 re = dequeue_re(group, fd);
171 if (!re)
172 return -ENOENT;
173
174 re->event->response = response;
175
176 wake_up(&group->fanotify_data.access_waitq);
177
178 kmem_cache_free(fanotify_response_event_cache, re);
179
180 return 0;
181}
182
183static int prepare_for_access_response(struct fsnotify_group *group,
184 struct fsnotify_event *event,
185 __s32 fd)
186{
187 struct fanotify_response_event *re;
188
189 if (!(event->mask & FAN_ALL_PERM_EVENTS))
190 return 0;
191
192 re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
193 if (!re)
194 return -ENOMEM;
195
196 re->event = event;
197 re->fd = fd;
198
199 mutex_lock(&group->fanotify_data.access_mutex);
Eric Paris2eebf582010-08-18 12:25:50 -0400200
201 if (group->fanotify_data.bypass_perm) {
202 mutex_unlock(&group->fanotify_data.access_mutex);
203 kmem_cache_free(fanotify_response_event_cache, re);
204 event->response = FAN_ALLOW;
205 return 0;
206 }
207
Eric Parisb2d87902009-12-17 21:24:34 -0500208 list_add_tail(&re->list, &group->fanotify_data.access_list);
209 mutex_unlock(&group->fanotify_data.access_mutex);
210
211 return 0;
212}
213
214static void remove_access_response(struct fsnotify_group *group,
215 struct fsnotify_event *event,
216 __s32 fd)
217{
218 struct fanotify_response_event *re;
219
220 if (!(event->mask & FAN_ALL_PERM_EVENTS))
221 return;
222
223 re = dequeue_re(group, fd);
224 if (!re)
225 return;
226
227 BUG_ON(re->event != event);
228
229 kmem_cache_free(fanotify_response_event_cache, re);
230
231 return;
232}
233#else
234static int prepare_for_access_response(struct fsnotify_group *group,
235 struct fsnotify_event *event,
236 __s32 fd)
237{
238 return 0;
239}
240
241static void remove_access_response(struct fsnotify_group *group,
242 struct fsnotify_event *event,
243 __s32 fd)
244{
Eric Paris8860f062009-12-23 00:10:25 -0500245 return;
Eric Parisb2d87902009-12-17 21:24:34 -0500246}
247#endif
248
Eric Parisa1014f12009-12-17 21:24:26 -0500249static ssize_t copy_event_to_user(struct fsnotify_group *group,
250 struct fsnotify_event *event,
251 char __user *buf)
252{
253 struct fanotify_event_metadata fanotify_event_metadata;
Eric Parisb2d87902009-12-17 21:24:34 -0500254 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500255
256 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
257
Eric Parisb2d87902009-12-17 21:24:34 -0500258 fd = fill_event_metadata(group, &fanotify_event_metadata, event);
259 if (fd < 0)
260 return fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500261
Eric Parisb2d87902009-12-17 21:24:34 -0500262 ret = prepare_for_access_response(group, event, fd);
263 if (ret)
264 goto out_close_fd;
265
266 ret = -EFAULT;
Eric Parisa1014f12009-12-17 21:24:26 -0500267 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
Eric Parisb2d87902009-12-17 21:24:34 -0500268 goto out_kill_access_response;
Eric Parisa1014f12009-12-17 21:24:26 -0500269
270 return FAN_EVENT_METADATA_LEN;
Eric Parisb2d87902009-12-17 21:24:34 -0500271
272out_kill_access_response:
273 remove_access_response(group, event, fd);
274out_close_fd:
275 sys_close(fd);
276 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500277}
278
279/* intofiy userspace file descriptor functions */
280static unsigned int fanotify_poll(struct file *file, poll_table *wait)
281{
282 struct fsnotify_group *group = file->private_data;
283 int ret = 0;
284
285 poll_wait(file, &group->notification_waitq, wait);
286 mutex_lock(&group->notification_mutex);
287 if (!fsnotify_notify_queue_is_empty(group))
288 ret = POLLIN | POLLRDNORM;
289 mutex_unlock(&group->notification_mutex);
290
291 return ret;
292}
293
294static ssize_t fanotify_read(struct file *file, char __user *buf,
295 size_t count, loff_t *pos)
296{
297 struct fsnotify_group *group;
298 struct fsnotify_event *kevent;
299 char __user *start;
300 int ret;
301 DEFINE_WAIT(wait);
302
303 start = buf;
304 group = file->private_data;
305
306 pr_debug("%s: group=%p\n", __func__, group);
307
308 while (1) {
309 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
310
311 mutex_lock(&group->notification_mutex);
312 kevent = get_one_event(group, count);
313 mutex_unlock(&group->notification_mutex);
314
315 if (kevent) {
316 ret = PTR_ERR(kevent);
317 if (IS_ERR(kevent))
318 break;
319 ret = copy_event_to_user(group, kevent, buf);
320 fsnotify_put_event(kevent);
321 if (ret < 0)
322 break;
323 buf += ret;
324 count -= ret;
325 continue;
326 }
327
328 ret = -EAGAIN;
329 if (file->f_flags & O_NONBLOCK)
330 break;
331 ret = -EINTR;
332 if (signal_pending(current))
333 break;
334
335 if (start != buf)
336 break;
337
338 schedule();
339 }
340
341 finish_wait(&group->notification_waitq, &wait);
342 if (start != buf && ret != -EFAULT)
343 ret = buf - start;
344 return ret;
345}
346
Eric Parisb2d87902009-12-17 21:24:34 -0500347static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
348{
349#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
350 struct fanotify_response response = { .fd = -1, .response = -1 };
351 struct fsnotify_group *group;
352 int ret;
353
354 group = file->private_data;
355
356 if (count > sizeof(response))
357 count = sizeof(response);
358
359 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
360
361 if (copy_from_user(&response, buf, count))
362 return -EFAULT;
363
364 ret = process_access_response(group, &response);
365 if (ret < 0)
366 count = ret;
367
368 return count;
369#else
370 return -EINVAL;
371#endif
372}
373
Eric Paris52c923d2009-12-17 21:24:26 -0500374static int fanotify_release(struct inode *ignored, struct file *file)
375{
376 struct fsnotify_group *group = file->private_data;
Eric Paris2eebf582010-08-18 12:25:50 -0400377 struct fanotify_response_event *re, *lre;
Eric Paris52c923d2009-12-17 21:24:26 -0500378
379 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
380
Eric Paris2eebf582010-08-18 12:25:50 -0400381#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
382 mutex_lock(&group->fanotify_data.access_mutex);
383
384 group->fanotify_data.bypass_perm = true;
385
386 list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
387 pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
388 re, re->event);
389
390 list_del_init(&re->list);
391 re->event->response = FAN_ALLOW;
392
393 kmem_cache_free(fanotify_response_event_cache, re);
394 }
395 mutex_unlock(&group->fanotify_data.access_mutex);
396
397 wake_up(&group->fanotify_data.access_waitq);
398#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500399 /* matches the fanotify_init->fsnotify_alloc_group */
400 fsnotify_put_group(group);
401
402 return 0;
403}
404
Eric Parisa1014f12009-12-17 21:24:26 -0500405static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
406{
407 struct fsnotify_group *group;
408 struct fsnotify_event_holder *holder;
409 void __user *p;
410 int ret = -ENOTTY;
411 size_t send_len = 0;
412
413 group = file->private_data;
414
415 p = (void __user *) arg;
416
417 switch (cmd) {
418 case FIONREAD:
419 mutex_lock(&group->notification_mutex);
420 list_for_each_entry(holder, &group->notification_list, event_list)
421 send_len += FAN_EVENT_METADATA_LEN;
422 mutex_unlock(&group->notification_mutex);
423 ret = put_user(send_len, (int __user *) p);
424 break;
425 }
426
427 return ret;
428}
429
Eric Paris52c923d2009-12-17 21:24:26 -0500430static const struct file_operations fanotify_fops = {
Eric Parisa1014f12009-12-17 21:24:26 -0500431 .poll = fanotify_poll,
432 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500433 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500434 .fasync = NULL,
435 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500436 .unlocked_ioctl = fanotify_ioctl,
437 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200438 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500439};
440
Eric Paris2a3edf82009-12-17 21:24:26 -0500441static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
442{
443 kmem_cache_free(fanotify_mark_cache, fsn_mark);
444}
445
446static int fanotify_find_path(int dfd, const char __user *filename,
447 struct path *path, unsigned int flags)
448{
449 int ret;
450
451 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
452 dfd, filename, flags);
453
454 if (filename == NULL) {
455 struct file *file;
456 int fput_needed;
457
458 ret = -EBADF;
459 file = fget_light(dfd, &fput_needed);
460 if (!file)
461 goto out;
462
463 ret = -ENOTDIR;
464 if ((flags & FAN_MARK_ONLYDIR) &&
465 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
466 fput_light(file, fput_needed);
467 goto out;
468 }
469
470 *path = file->f_path;
471 path_get(path);
472 fput_light(file, fput_needed);
473 } else {
474 unsigned int lookup_flags = 0;
475
476 if (!(flags & FAN_MARK_DONT_FOLLOW))
477 lookup_flags |= LOOKUP_FOLLOW;
478 if (flags & FAN_MARK_ONLYDIR)
479 lookup_flags |= LOOKUP_DIRECTORY;
480
481 ret = user_path_at(dfd, filename, lookup_flags, path);
482 if (ret)
483 goto out;
484 }
485
486 /* you can only watch an inode if you have read permissions on it */
487 ret = inode_permission(path->dentry->d_inode, MAY_READ);
488 if (ret)
489 path_put(path);
490out:
491 return ret;
492}
493
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500494static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
495 __u32 mask,
496 unsigned int flags)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500497{
498 __u32 oldmask;
499
500 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500501 if (!(flags & FAN_MARK_IGNORED_MASK)) {
502 oldmask = fsn_mark->mask;
503 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
504 } else {
505 oldmask = fsn_mark->ignored_mask;
506 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
507 }
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500508 spin_unlock(&fsn_mark->lock);
509
510 if (!(oldmask & ~mask))
511 fsnotify_destroy_mark(fsn_mark);
512
513 return mask & oldmask;
514}
515
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500516static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500517 struct vfsmount *mnt, __u32 mask,
518 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500519{
520 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500521 __u32 removed;
Eric Paris88826272009-12-17 21:24:28 -0500522
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500523 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
524 if (!fsn_mark)
525 return -ENOENT;
Eric Paris88826272009-12-17 21:24:28 -0500526
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500527 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500528 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500529 if (removed & mnt->mnt_fsnotify_mask)
530 fsnotify_recalc_vfsmount_mask(mnt);
Eric Paris88826272009-12-17 21:24:28 -0500531
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500532 return 0;
533}
534
535static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500536 struct inode *inode, __u32 mask,
537 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500538{
539 struct fsnotify_mark *fsn_mark = NULL;
540 __u32 removed;
541
542 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris88826272009-12-17 21:24:28 -0500543 if (!fsn_mark)
544 return -ENOENT;
545
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500546 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Eric Paris5444e292009-12-17 21:24:27 -0500547 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500548 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500549 if (removed & inode->i_fsnotify_mask)
550 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500551
Eric Paris2a3edf82009-12-17 21:24:26 -0500552 return 0;
553}
554
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500555static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
556 __u32 mask,
557 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500558{
559 __u32 oldmask;
560
561 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500562 if (!(flags & FAN_MARK_IGNORED_MASK)) {
563 oldmask = fsn_mark->mask;
564 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
565 } else {
566 oldmask = fsn_mark->ignored_mask;
567 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
Eric Parisc9778a92009-12-17 21:24:33 -0500568 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
569 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500570 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500571 spin_unlock(&fsn_mark->lock);
572
573 return mask & ~oldmask;
574}
575
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500576static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500577 struct vfsmount *mnt, __u32 mask,
578 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500579{
580 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500581 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500582
Eric Paris88826272009-12-17 21:24:28 -0500583 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
584 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500585 int ret;
586
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500587 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
588 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500589 return -ENOMEM;
Eric Paris88826272009-12-17 21:24:28 -0500590
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500591 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
592 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
Eric Paris88826272009-12-17 21:24:28 -0500593 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500594 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500595 return ret;
Eric Paris88826272009-12-17 21:24:28 -0500596 }
Eric Paris88826272009-12-17 21:24:28 -0500597 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500598 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500599 fsnotify_put_mark(fsn_mark);
Eric Paris43709a22010-07-28 10:18:39 -0400600 if (added & ~mnt->mnt_fsnotify_mask)
601 fsnotify_recalc_vfsmount_mask(mnt);
602
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500603 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500604}
605
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500606static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500607 struct inode *inode, __u32 mask,
608 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500609{
610 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500611 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500612
613 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500614
Eric Paris5322a592010-10-28 17:21:57 -0400615 /*
616 * If some other task has this inode open for write we should not add
617 * an ignored mark, unless that ignored mark is supposed to survive
618 * modification changes anyway.
619 */
620 if ((flags & FAN_MARK_IGNORED_MASK) &&
621 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
622 (atomic_read(&inode->i_writecount) > 0))
623 return 0;
624
Eric Paris5444e292009-12-17 21:24:27 -0500625 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500626 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500627 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500628
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500629 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
630 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500631 return -ENOMEM;
Eric Paris2a3edf82009-12-17 21:24:26 -0500632
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500633 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
634 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
Eric Paris2a3edf82009-12-17 21:24:26 -0500635 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500636 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500637 return ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500638 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500639 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500640 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500641 fsnotify_put_mark(fsn_mark);
Eric Paris43709a22010-07-28 10:18:39 -0400642 if (added & ~inode->i_fsnotify_mask)
643 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500644 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500645}
Eric Paris2a3edf82009-12-17 21:24:26 -0500646
Eric Paris52c923d2009-12-17 21:24:26 -0500647/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400648SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500649{
Eric Paris52c923d2009-12-17 21:24:26 -0500650 struct fsnotify_group *group;
651 int f_flags, fd;
652
Eric Paris08ae8932010-05-27 09:41:40 -0400653 pr_debug("%s: flags=%d event_f_flags=%d\n",
654 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500655
Eric Paris52c923d2009-12-17 21:24:26 -0500656 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200657 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500658
659 if (flags & ~FAN_ALL_INIT_FLAGS)
660 return -EINVAL;
661
Eric Parisb2d87902009-12-17 21:24:34 -0500662 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500663 if (flags & FAN_CLOEXEC)
664 f_flags |= O_CLOEXEC;
665 if (flags & FAN_NONBLOCK)
666 f_flags |= O_NONBLOCK;
667
668 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
669 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
670 if (IS_ERR(group))
671 return PTR_ERR(group);
672
Eric Paris80af2582010-07-28 10:18:37 -0400673 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500674#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
675 mutex_init(&group->fanotify_data.access_mutex);
676 init_waitqueue_head(&group->fanotify_data.access_waitq);
677 INIT_LIST_HEAD(&group->fanotify_data.access_list);
678#endif
Eric Paris4231a232010-10-28 17:21:56 -0400679 switch (flags & FAN_ALL_CLASS_BITS) {
680 case FAN_CLASS_NOTIF:
681 group->priority = FS_PRIO_0;
682 break;
683 case FAN_CLASS_CONTENT:
684 group->priority = FS_PRIO_1;
685 break;
686 case FAN_CLASS_PRE_CONTENT:
687 group->priority = FS_PRIO_2;
688 break;
689 default:
690 fd = -EINVAL;
691 goto out_put_group;
692 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500693
Eric Paris2529a0d2010-10-28 17:21:57 -0400694 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
695
Eric Paris52c923d2009-12-17 21:24:26 -0500696 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
697 if (fd < 0)
698 goto out_put_group;
699
700 return fd;
701
702out_put_group:
703 fsnotify_put_group(group);
704 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500705}
Eric Parisbbaa4162009-12-17 21:24:26 -0500706
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500707SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
708 __u64 mask, int dfd,
709 const char __user * pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500710{
Eric Paris0ff21db2009-12-17 21:24:29 -0500711 struct inode *inode = NULL;
712 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500713 struct fsnotify_group *group;
714 struct file *filp;
715 struct path path;
716 int ret, fput_needed;
717
718 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
719 __func__, fanotify_fd, flags, dfd, pathname, mask);
720
721 /* we only use the lower 32 bits as of right now. */
722 if (mask & ((__u64)0xffffffff << 32))
723 return -EINVAL;
724
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500725 if (flags & ~FAN_ALL_MARK_FLAGS)
726 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500727 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500728 case FAN_MARK_ADD:
729 case FAN_MARK_REMOVE:
Eric Paris4d926042009-12-17 21:24:34 -0500730 case FAN_MARK_FLUSH:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500731 break;
732 default:
733 return -EINVAL;
734 }
Eric Parisb2d87902009-12-17 21:24:34 -0500735#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
736 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
737#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500738 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500739#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500740 return -EINVAL;
741
742 filp = fget_light(fanotify_fd, &fput_needed);
743 if (unlikely(!filp))
744 return -EBADF;
745
746 /* verify that this is indeed an fanotify instance */
747 ret = -EINVAL;
748 if (unlikely(filp->f_op != &fanotify_fops))
749 goto fput_and_out;
Eric Paris4231a232010-10-28 17:21:56 -0400750 group = filp->private_data;
751
752 /*
753 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
754 * allowed to set permissions events.
755 */
756 ret = -EINVAL;
757 if (mask & FAN_ALL_PERM_EVENTS &&
758 group->priority == FS_PRIO_0)
759 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500760
761 ret = fanotify_find_path(dfd, pathname, &path, flags);
762 if (ret)
763 goto fput_and_out;
764
765 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500766 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500767 inode = path.dentry->d_inode;
768 else
769 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500770
771 /* create/update an inode mark */
Eric Paris4d926042009-12-17 21:24:34 -0500772 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500773 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500774 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500775 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500776 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500777 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500778 break;
779 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500780 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500781 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500782 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500783 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500784 break;
Eric Paris4d926042009-12-17 21:24:34 -0500785 case FAN_MARK_FLUSH:
786 if (flags & FAN_MARK_MOUNT)
787 fsnotify_clear_vfsmount_marks_by_group(group);
788 else
789 fsnotify_clear_inode_marks_by_group(group);
Eric Paris4d926042009-12-17 21:24:34 -0500790 break;
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500791 default:
792 ret = -EINVAL;
793 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500794
795 path_put(&path);
796fput_and_out:
797 fput_light(filp, fput_needed);
798 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500799}
Eric Paris2a3edf82009-12-17 21:24:26 -0500800
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500801#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
802asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
803 long dfd, long pathname)
804{
805 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
806 mask, (int) dfd,
807 (const char __user *) pathname);
808}
809SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
810#endif
811
Eric Paris2a3edf82009-12-17 21:24:26 -0500812/*
813 * fanotify_user_setup - Our initialization function. Note that we cannnot return
814 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
815 * must result in panic().
816 */
817static int __init fanotify_user_setup(void)
818{
819 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Eric Parisb2d87902009-12-17 21:24:34 -0500820 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
821 SLAB_PANIC);
Eric Paris2a3edf82009-12-17 21:24:26 -0500822
823 return 0;
824}
825device_initcall(fanotify_user_setup);