blob: 1c09e6321c5e0908aae9fe46bbe63b5e2acbca02 [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
Linus Torvalds20696012010-08-12 14:23:04 -070068 if (event->data_type != FSNOTIFY_EVENT_PATH) {
Eric Parisa1014f12009-12-17 21:24:26 -050069 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 */
Linus Torvalds20696012010-08-12 14:23:04 -070078 dentry = dget(event->path.dentry);
79 mnt = mntget(event->path.mnt);
Eric Parisa1014f12009-12-17 21:24:26 -050080 /* 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,
Eric Paris80af2582010-07-28 10:18:37 -040084 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -050085 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);
Eric Paris2eebf582010-08-18 12:25:50 -0400198
199 if (group->fanotify_data.bypass_perm) {
200 mutex_unlock(&group->fanotify_data.access_mutex);
201 kmem_cache_free(fanotify_response_event_cache, re);
202 event->response = FAN_ALLOW;
203 return 0;
204 }
205
Eric Parisb2d87902009-12-17 21:24:34 -0500206 list_add_tail(&re->list, &group->fanotify_data.access_list);
207 mutex_unlock(&group->fanotify_data.access_mutex);
208
209 return 0;
210}
211
212static void remove_access_response(struct fsnotify_group *group,
213 struct fsnotify_event *event,
214 __s32 fd)
215{
216 struct fanotify_response_event *re;
217
218 if (!(event->mask & FAN_ALL_PERM_EVENTS))
219 return;
220
221 re = dequeue_re(group, fd);
222 if (!re)
223 return;
224
225 BUG_ON(re->event != event);
226
227 kmem_cache_free(fanotify_response_event_cache, re);
228
229 return;
230}
231#else
232static int prepare_for_access_response(struct fsnotify_group *group,
233 struct fsnotify_event *event,
234 __s32 fd)
235{
236 return 0;
237}
238
239static void remove_access_response(struct fsnotify_group *group,
240 struct fsnotify_event *event,
241 __s32 fd)
242{
Eric Paris8860f062009-12-23 00:10:25 -0500243 return;
Eric Parisb2d87902009-12-17 21:24:34 -0500244}
245#endif
246
Eric Parisa1014f12009-12-17 21:24:26 -0500247static ssize_t copy_event_to_user(struct fsnotify_group *group,
248 struct fsnotify_event *event,
249 char __user *buf)
250{
251 struct fanotify_event_metadata fanotify_event_metadata;
Eric Parisb2d87902009-12-17 21:24:34 -0500252 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500253
254 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
255
Eric Parisb2d87902009-12-17 21:24:34 -0500256 fd = fill_event_metadata(group, &fanotify_event_metadata, event);
257 if (fd < 0)
258 return fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500259
Eric Parisb2d87902009-12-17 21:24:34 -0500260 ret = prepare_for_access_response(group, event, fd);
261 if (ret)
262 goto out_close_fd;
263
264 ret = -EFAULT;
Eric Parisa1014f12009-12-17 21:24:26 -0500265 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
Eric Parisb2d87902009-12-17 21:24:34 -0500266 goto out_kill_access_response;
Eric Parisa1014f12009-12-17 21:24:26 -0500267
268 return FAN_EVENT_METADATA_LEN;
Eric Parisb2d87902009-12-17 21:24:34 -0500269
270out_kill_access_response:
271 remove_access_response(group, event, fd);
272out_close_fd:
273 sys_close(fd);
274 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500275}
276
277/* intofiy userspace file descriptor functions */
278static unsigned int fanotify_poll(struct file *file, poll_table *wait)
279{
280 struct fsnotify_group *group = file->private_data;
281 int ret = 0;
282
283 poll_wait(file, &group->notification_waitq, wait);
284 mutex_lock(&group->notification_mutex);
285 if (!fsnotify_notify_queue_is_empty(group))
286 ret = POLLIN | POLLRDNORM;
287 mutex_unlock(&group->notification_mutex);
288
289 return ret;
290}
291
292static ssize_t fanotify_read(struct file *file, char __user *buf,
293 size_t count, loff_t *pos)
294{
295 struct fsnotify_group *group;
296 struct fsnotify_event *kevent;
297 char __user *start;
298 int ret;
299 DEFINE_WAIT(wait);
300
301 start = buf;
302 group = file->private_data;
303
304 pr_debug("%s: group=%p\n", __func__, group);
305
306 while (1) {
307 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
308
309 mutex_lock(&group->notification_mutex);
310 kevent = get_one_event(group, count);
311 mutex_unlock(&group->notification_mutex);
312
313 if (kevent) {
314 ret = PTR_ERR(kevent);
315 if (IS_ERR(kevent))
316 break;
317 ret = copy_event_to_user(group, kevent, buf);
318 fsnotify_put_event(kevent);
319 if (ret < 0)
320 break;
321 buf += ret;
322 count -= ret;
323 continue;
324 }
325
326 ret = -EAGAIN;
327 if (file->f_flags & O_NONBLOCK)
328 break;
329 ret = -EINTR;
330 if (signal_pending(current))
331 break;
332
333 if (start != buf)
334 break;
335
336 schedule();
337 }
338
339 finish_wait(&group->notification_waitq, &wait);
340 if (start != buf && ret != -EFAULT)
341 ret = buf - start;
342 return ret;
343}
344
Eric Parisb2d87902009-12-17 21:24:34 -0500345static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
346{
347#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
348 struct fanotify_response response = { .fd = -1, .response = -1 };
349 struct fsnotify_group *group;
350 int ret;
351
352 group = file->private_data;
353
354 if (count > sizeof(response))
355 count = sizeof(response);
356
357 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
358
359 if (copy_from_user(&response, buf, count))
360 return -EFAULT;
361
362 ret = process_access_response(group, &response);
363 if (ret < 0)
364 count = ret;
365
366 return count;
367#else
368 return -EINVAL;
369#endif
370}
371
Eric Paris52c923d2009-12-17 21:24:26 -0500372static int fanotify_release(struct inode *ignored, struct file *file)
373{
374 struct fsnotify_group *group = file->private_data;
Eric Paris2eebf582010-08-18 12:25:50 -0400375 struct fanotify_response_event *re, *lre;
Eric Paris52c923d2009-12-17 21:24:26 -0500376
377 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
378
Eric Paris2eebf582010-08-18 12:25:50 -0400379#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
380 mutex_lock(&group->fanotify_data.access_mutex);
381
382 group->fanotify_data.bypass_perm = true;
383
384 list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
385 pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
386 re, re->event);
387
388 list_del_init(&re->list);
389 re->event->response = FAN_ALLOW;
390
391 kmem_cache_free(fanotify_response_event_cache, re);
392 }
393 mutex_unlock(&group->fanotify_data.access_mutex);
394
395 wake_up(&group->fanotify_data.access_waitq);
396#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500397 /* matches the fanotify_init->fsnotify_alloc_group */
398 fsnotify_put_group(group);
399
400 return 0;
401}
402
Eric Parisa1014f12009-12-17 21:24:26 -0500403static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
404{
405 struct fsnotify_group *group;
406 struct fsnotify_event_holder *holder;
407 void __user *p;
408 int ret = -ENOTTY;
409 size_t send_len = 0;
410
411 group = file->private_data;
412
413 p = (void __user *) arg;
414
415 switch (cmd) {
416 case FIONREAD:
417 mutex_lock(&group->notification_mutex);
418 list_for_each_entry(holder, &group->notification_list, event_list)
419 send_len += FAN_EVENT_METADATA_LEN;
420 mutex_unlock(&group->notification_mutex);
421 ret = put_user(send_len, (int __user *) p);
422 break;
423 }
424
425 return ret;
426}
427
Eric Paris52c923d2009-12-17 21:24:26 -0500428static const struct file_operations fanotify_fops = {
Eric Parisa1014f12009-12-17 21:24:26 -0500429 .poll = fanotify_poll,
430 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500431 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500432 .fasync = NULL,
433 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500434 .unlocked_ioctl = fanotify_ioctl,
435 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200436 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500437};
438
Eric Paris2a3edf82009-12-17 21:24:26 -0500439static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
440{
441 kmem_cache_free(fanotify_mark_cache, fsn_mark);
442}
443
444static int fanotify_find_path(int dfd, const char __user *filename,
445 struct path *path, unsigned int flags)
446{
447 int ret;
448
449 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
450 dfd, filename, flags);
451
452 if (filename == NULL) {
453 struct file *file;
454 int fput_needed;
455
456 ret = -EBADF;
457 file = fget_light(dfd, &fput_needed);
458 if (!file)
459 goto out;
460
461 ret = -ENOTDIR;
462 if ((flags & FAN_MARK_ONLYDIR) &&
463 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
464 fput_light(file, fput_needed);
465 goto out;
466 }
467
468 *path = file->f_path;
469 path_get(path);
470 fput_light(file, fput_needed);
471 } else {
472 unsigned int lookup_flags = 0;
473
474 if (!(flags & FAN_MARK_DONT_FOLLOW))
475 lookup_flags |= LOOKUP_FOLLOW;
476 if (flags & FAN_MARK_ONLYDIR)
477 lookup_flags |= LOOKUP_DIRECTORY;
478
479 ret = user_path_at(dfd, filename, lookup_flags, path);
480 if (ret)
481 goto out;
482 }
483
484 /* you can only watch an inode if you have read permissions on it */
485 ret = inode_permission(path->dentry->d_inode, MAY_READ);
486 if (ret)
487 path_put(path);
488out:
489 return ret;
490}
491
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500492static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
493 __u32 mask,
494 unsigned int flags)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500495{
496 __u32 oldmask;
497
498 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500499 if (!(flags & FAN_MARK_IGNORED_MASK)) {
500 oldmask = fsn_mark->mask;
501 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
502 } else {
503 oldmask = fsn_mark->ignored_mask;
504 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
505 }
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500506 spin_unlock(&fsn_mark->lock);
507
508 if (!(oldmask & ~mask))
509 fsnotify_destroy_mark(fsn_mark);
510
511 return mask & oldmask;
512}
513
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500514static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500515 struct vfsmount *mnt, __u32 mask,
516 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500517{
518 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500519 __u32 removed;
Eric Paris88826272009-12-17 21:24:28 -0500520
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500521 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
522 if (!fsn_mark)
523 return -ENOENT;
Eric Paris88826272009-12-17 21:24:28 -0500524
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500525 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500526 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500527 if (removed & mnt->mnt_fsnotify_mask)
528 fsnotify_recalc_vfsmount_mask(mnt);
Eric Paris88826272009-12-17 21:24:28 -0500529
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500530 return 0;
531}
532
533static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500534 struct inode *inode, __u32 mask,
535 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500536{
537 struct fsnotify_mark *fsn_mark = NULL;
538 __u32 removed;
539
540 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris88826272009-12-17 21:24:28 -0500541 if (!fsn_mark)
542 return -ENOENT;
543
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500544 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Eric Paris5444e292009-12-17 21:24:27 -0500545 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500546 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500547 if (removed & inode->i_fsnotify_mask)
548 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500549
Eric Paris2a3edf82009-12-17 21:24:26 -0500550 return 0;
551}
552
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500553static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
554 __u32 mask,
555 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500556{
557 __u32 oldmask;
558
559 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500560 if (!(flags & FAN_MARK_IGNORED_MASK)) {
561 oldmask = fsn_mark->mask;
562 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
563 } else {
564 oldmask = fsn_mark->ignored_mask;
565 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
Eric Parisc9778a92009-12-17 21:24:33 -0500566 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
567 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500568 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500569 spin_unlock(&fsn_mark->lock);
570
571 return mask & ~oldmask;
572}
573
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500574static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500575 struct vfsmount *mnt, __u32 mask,
576 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500577{
578 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500579 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500580
Eric Paris88826272009-12-17 21:24:28 -0500581 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
582 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500583 int ret;
584
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500585 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
586 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500587 return -ENOMEM;
Eric Paris88826272009-12-17 21:24:28 -0500588
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500589 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
590 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
Eric Paris88826272009-12-17 21:24:28 -0500591 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500592 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500593 return ret;
Eric Paris88826272009-12-17 21:24:28 -0500594 }
Eric Paris88826272009-12-17 21:24:28 -0500595 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500596 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500597 fsnotify_put_mark(fsn_mark);
Eric Paris43709a22010-07-28 10:18:39 -0400598 if (added & ~mnt->mnt_fsnotify_mask)
599 fsnotify_recalc_vfsmount_mask(mnt);
600
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500601 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500602}
603
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500604static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500605 struct inode *inode, __u32 mask,
606 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500607{
608 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500609 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500610
611 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500612
Eric Paris5444e292009-12-17 21:24:27 -0500613 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500614 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500615 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500616
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500617 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
618 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500619 return -ENOMEM;
Eric Paris2a3edf82009-12-17 21:24:26 -0500620
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500621 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
622 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
Eric Paris2a3edf82009-12-17 21:24:26 -0500623 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500624 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500625 return ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500626 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500627 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500628 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500629 fsnotify_put_mark(fsn_mark);
Eric Paris43709a22010-07-28 10:18:39 -0400630 if (added & ~inode->i_fsnotify_mask)
631 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500632 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500633}
Eric Paris2a3edf82009-12-17 21:24:26 -0500634
Eric Paris52c923d2009-12-17 21:24:26 -0500635/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400636SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500637{
Eric Paris52c923d2009-12-17 21:24:26 -0500638 struct fsnotify_group *group;
639 int f_flags, fd;
640
Eric Paris08ae8932010-05-27 09:41:40 -0400641 pr_debug("%s: flags=%d event_f_flags=%d\n",
642 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500643
Eric Paris52c923d2009-12-17 21:24:26 -0500644 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200645 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500646
647 if (flags & ~FAN_ALL_INIT_FLAGS)
648 return -EINVAL;
649
Eric Parisb2d87902009-12-17 21:24:34 -0500650 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500651 if (flags & FAN_CLOEXEC)
652 f_flags |= O_CLOEXEC;
653 if (flags & FAN_NONBLOCK)
654 f_flags |= O_NONBLOCK;
655
656 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
657 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
658 if (IS_ERR(group))
659 return PTR_ERR(group);
660
Eric Paris80af2582010-07-28 10:18:37 -0400661 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500662#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
663 mutex_init(&group->fanotify_data.access_mutex);
664 init_waitqueue_head(&group->fanotify_data.access_waitq);
665 INIT_LIST_HEAD(&group->fanotify_data.access_list);
666#endif
Eric Paris4231a232010-10-28 17:21:56 -0400667 switch (flags & FAN_ALL_CLASS_BITS) {
668 case FAN_CLASS_NOTIF:
669 group->priority = FS_PRIO_0;
670 break;
671 case FAN_CLASS_CONTENT:
672 group->priority = FS_PRIO_1;
673 break;
674 case FAN_CLASS_PRE_CONTENT:
675 group->priority = FS_PRIO_2;
676 break;
677 default:
678 fd = -EINVAL;
679 goto out_put_group;
680 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500681
Eric Paris52c923d2009-12-17 21:24:26 -0500682 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
683 if (fd < 0)
684 goto out_put_group;
685
686 return fd;
687
688out_put_group:
689 fsnotify_put_group(group);
690 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500691}
Eric Parisbbaa4162009-12-17 21:24:26 -0500692
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500693SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
694 __u64 mask, int dfd,
695 const char __user * pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500696{
Eric Paris0ff21db2009-12-17 21:24:29 -0500697 struct inode *inode = NULL;
698 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500699 struct fsnotify_group *group;
700 struct file *filp;
701 struct path path;
702 int ret, fput_needed;
703
704 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
705 __func__, fanotify_fd, flags, dfd, pathname, mask);
706
707 /* we only use the lower 32 bits as of right now. */
708 if (mask & ((__u64)0xffffffff << 32))
709 return -EINVAL;
710
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500711 if (flags & ~FAN_ALL_MARK_FLAGS)
712 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500713 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500714 case FAN_MARK_ADD:
715 case FAN_MARK_REMOVE:
Eric Paris4d926042009-12-17 21:24:34 -0500716 case FAN_MARK_FLUSH:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500717 break;
718 default:
719 return -EINVAL;
720 }
Eric Parisb2d87902009-12-17 21:24:34 -0500721#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
722 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
723#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500724 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500725#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500726 return -EINVAL;
727
728 filp = fget_light(fanotify_fd, &fput_needed);
729 if (unlikely(!filp))
730 return -EBADF;
731
732 /* verify that this is indeed an fanotify instance */
733 ret = -EINVAL;
734 if (unlikely(filp->f_op != &fanotify_fops))
735 goto fput_and_out;
Eric Paris4231a232010-10-28 17:21:56 -0400736 group = filp->private_data;
737
738 /*
739 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
740 * allowed to set permissions events.
741 */
742 ret = -EINVAL;
743 if (mask & FAN_ALL_PERM_EVENTS &&
744 group->priority == FS_PRIO_0)
745 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500746
747 ret = fanotify_find_path(dfd, pathname, &path, flags);
748 if (ret)
749 goto fput_and_out;
750
751 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500752 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500753 inode = path.dentry->d_inode;
754 else
755 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500756
757 /* create/update an inode mark */
Eric Paris4d926042009-12-17 21:24:34 -0500758 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500759 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500760 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500761 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500762 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500763 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500764 break;
765 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500766 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500767 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500768 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500769 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500770 break;
Eric Paris4d926042009-12-17 21:24:34 -0500771 case FAN_MARK_FLUSH:
772 if (flags & FAN_MARK_MOUNT)
773 fsnotify_clear_vfsmount_marks_by_group(group);
774 else
775 fsnotify_clear_inode_marks_by_group(group);
Eric Paris4d926042009-12-17 21:24:34 -0500776 break;
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500777 default:
778 ret = -EINVAL;
779 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500780
781 path_put(&path);
782fput_and_out:
783 fput_light(filp, fput_needed);
784 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500785}
Eric Paris2a3edf82009-12-17 21:24:26 -0500786
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500787#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
788asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
789 long dfd, long pathname)
790{
791 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
792 mask, (int) dfd,
793 (const char __user *) pathname);
794}
795SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
796#endif
797
Eric Paris2a3edf82009-12-17 21:24:26 -0500798/*
799 * fanotify_user_setup - Our initialization function. Note that we cannnot return
800 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
801 * must result in panic().
802 */
803static int __init fanotify_user_setup(void)
804{
805 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Eric Parisb2d87902009-12-17 21:24:34 -0500806 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
807 SLAB_PANIC);
Eric Paris2a3edf82009-12-17 21:24:26 -0500808
809 return 0;
810}
811device_initcall(fanotify_user_setup);