blob: 7c869fa23ec6a9349fd9fe2559b1f0b83e27bd5d [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>
Eric Paris2a3edf82009-12-17 21:24:26 -050013#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050014#include <linux/uaccess.h>
15
16#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050017
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050018extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050019
Eric Paris2a3edf82009-12-17 21:24:26 -050020static struct kmem_cache *fanotify_mark_cache __read_mostly;
Eric Parisb2d87902009-12-17 21:24:34 -050021static struct kmem_cache *fanotify_response_event_cache __read_mostly;
22
23struct fanotify_response_event {
24 struct list_head list;
25 __s32 fd;
26 struct fsnotify_event *event;
27};
Eric Paris2a3edf82009-12-17 21:24:26 -050028
Eric Parisa1014f12009-12-17 21:24:26 -050029/*
30 * Get an fsnotify notification event if one exists and is small
31 * enough to fit in "count". Return an error pointer if the count
32 * is not large enough.
33 *
34 * Called with the group->notification_mutex held.
35 */
36static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
37 size_t count)
38{
39 BUG_ON(!mutex_is_locked(&group->notification_mutex));
40
41 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
42
43 if (fsnotify_notify_queue_is_empty(group))
44 return NULL;
45
46 if (FAN_EVENT_METADATA_LEN > count)
47 return ERR_PTR(-EINVAL);
48
49 /* held the notification_mutex the whole time, so this is the
50 * same event we peeked above */
51 return fsnotify_remove_notify_event(group);
52}
53
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050054static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
Eric Parisa1014f12009-12-17 21:24:26 -050055{
56 int client_fd;
57 struct dentry *dentry;
58 struct vfsmount *mnt;
59 struct file *new_file;
60
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050061 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050062
63 client_fd = get_unused_fd();
64 if (client_fd < 0)
65 return client_fd;
66
67 if (event->data_type != FSNOTIFY_EVENT_PATH) {
68 WARN_ON(1);
69 put_unused_fd(client_fd);
70 return -EINVAL;
71 }
72
73 /*
74 * we need a new file handle for the userspace program so it can read even if it was
75 * originally opened O_WRONLY.
76 */
77 dentry = dget(event->path.dentry);
78 mnt = mntget(event->path.mnt);
79 /* it's possible this event was an overflow event. in that case dentry and mnt
80 * are NULL; That's fine, just don't call dentry open */
81 if (dentry && mnt)
82 new_file = dentry_open(dentry, mnt,
83 O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
84 current_cred());
85 else
86 new_file = ERR_PTR(-EOVERFLOW);
87 if (IS_ERR(new_file)) {
88 /*
89 * we still send an event even if we can't open the file. this
90 * can happen when say tasks are gone and we try to open their
91 * /proc files or we try to open a WRONLY file like in sysfs
92 * we just send the errno to userspace since there isn't much
93 * else we can do.
94 */
95 put_unused_fd(client_fd);
96 client_fd = PTR_ERR(new_file);
97 } else {
98 fd_install(client_fd, new_file);
99 }
100
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500101 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500102}
103
104static ssize_t fill_event_metadata(struct fsnotify_group *group,
105 struct fanotify_event_metadata *metadata,
106 struct fsnotify_event *event)
107{
108 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
109 group, metadata, event);
110
111 metadata->event_len = FAN_EVENT_METADATA_LEN;
112 metadata->vers = FANOTIFY_METADATA_VERSION;
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -0500113 metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500114 metadata->pid = pid_vnr(event->tgid);
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500115 metadata->fd = create_fd(group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500116
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500117 return metadata->fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500118}
119
Eric Parisb2d87902009-12-17 21:24:34 -0500120#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
121static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
122 __s32 fd)
123{
124 struct fanotify_response_event *re, *return_re = NULL;
125
126 mutex_lock(&group->fanotify_data.access_mutex);
127 list_for_each_entry(re, &group->fanotify_data.access_list, list) {
128 if (re->fd != fd)
129 continue;
130
131 list_del_init(&re->list);
132 return_re = re;
133 break;
134 }
135 mutex_unlock(&group->fanotify_data.access_mutex);
136
137 pr_debug("%s: found return_re=%p\n", __func__, return_re);
138
139 return return_re;
140}
141
142static int process_access_response(struct fsnotify_group *group,
143 struct fanotify_response *response_struct)
144{
145 struct fanotify_response_event *re;
146 __s32 fd = response_struct->fd;
147 __u32 response = response_struct->response;
148
149 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
150 fd, response);
151 /*
152 * make sure the response is valid, if invalid we do nothing and either
153 * userspace can send a valid responce or we will clean it up after the
154 * timeout
155 */
156 switch (response) {
157 case FAN_ALLOW:
158 case FAN_DENY:
159 break;
160 default:
161 return -EINVAL;
162 }
163
164 if (fd < 0)
165 return -EINVAL;
166
167 re = dequeue_re(group, fd);
168 if (!re)
169 return -ENOENT;
170
171 re->event->response = response;
172
173 wake_up(&group->fanotify_data.access_waitq);
174
175 kmem_cache_free(fanotify_response_event_cache, re);
176
177 return 0;
178}
179
180static int prepare_for_access_response(struct fsnotify_group *group,
181 struct fsnotify_event *event,
182 __s32 fd)
183{
184 struct fanotify_response_event *re;
185
186 if (!(event->mask & FAN_ALL_PERM_EVENTS))
187 return 0;
188
189 re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
190 if (!re)
191 return -ENOMEM;
192
193 re->event = event;
194 re->fd = fd;
195
196 mutex_lock(&group->fanotify_data.access_mutex);
197 list_add_tail(&re->list, &group->fanotify_data.access_list);
198 mutex_unlock(&group->fanotify_data.access_mutex);
199
200 return 0;
201}
202
203static void remove_access_response(struct fsnotify_group *group,
204 struct fsnotify_event *event,
205 __s32 fd)
206{
207 struct fanotify_response_event *re;
208
209 if (!(event->mask & FAN_ALL_PERM_EVENTS))
210 return;
211
212 re = dequeue_re(group, fd);
213 if (!re)
214 return;
215
216 BUG_ON(re->event != event);
217
218 kmem_cache_free(fanotify_response_event_cache, re);
219
220 return;
221}
222#else
223static int prepare_for_access_response(struct fsnotify_group *group,
224 struct fsnotify_event *event,
225 __s32 fd)
226{
227 return 0;
228}
229
230static void remove_access_response(struct fsnotify_group *group,
231 struct fsnotify_event *event,
232 __s32 fd)
233{
Eric Paris8860f062009-12-23 00:10:25 -0500234 return;
Eric Parisb2d87902009-12-17 21:24:34 -0500235}
236#endif
237
Eric Parisa1014f12009-12-17 21:24:26 -0500238static ssize_t copy_event_to_user(struct fsnotify_group *group,
239 struct fsnotify_event *event,
240 char __user *buf)
241{
242 struct fanotify_event_metadata fanotify_event_metadata;
Eric Parisb2d87902009-12-17 21:24:34 -0500243 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500244
245 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
246
Eric Parisb2d87902009-12-17 21:24:34 -0500247 fd = fill_event_metadata(group, &fanotify_event_metadata, event);
248 if (fd < 0)
249 return fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500250
Eric Parisb2d87902009-12-17 21:24:34 -0500251 ret = prepare_for_access_response(group, event, fd);
252 if (ret)
253 goto out_close_fd;
254
255 ret = -EFAULT;
Eric Parisa1014f12009-12-17 21:24:26 -0500256 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
Eric Parisb2d87902009-12-17 21:24:34 -0500257 goto out_kill_access_response;
Eric Parisa1014f12009-12-17 21:24:26 -0500258
259 return FAN_EVENT_METADATA_LEN;
Eric Parisb2d87902009-12-17 21:24:34 -0500260
261out_kill_access_response:
262 remove_access_response(group, event, fd);
263out_close_fd:
264 sys_close(fd);
265 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500266}
267
268/* intofiy userspace file descriptor functions */
269static unsigned int fanotify_poll(struct file *file, poll_table *wait)
270{
271 struct fsnotify_group *group = file->private_data;
272 int ret = 0;
273
274 poll_wait(file, &group->notification_waitq, wait);
275 mutex_lock(&group->notification_mutex);
276 if (!fsnotify_notify_queue_is_empty(group))
277 ret = POLLIN | POLLRDNORM;
278 mutex_unlock(&group->notification_mutex);
279
280 return ret;
281}
282
283static ssize_t fanotify_read(struct file *file, char __user *buf,
284 size_t count, loff_t *pos)
285{
286 struct fsnotify_group *group;
287 struct fsnotify_event *kevent;
288 char __user *start;
289 int ret;
290 DEFINE_WAIT(wait);
291
292 start = buf;
293 group = file->private_data;
294
295 pr_debug("%s: group=%p\n", __func__, group);
296
297 while (1) {
298 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
299
300 mutex_lock(&group->notification_mutex);
301 kevent = get_one_event(group, count);
302 mutex_unlock(&group->notification_mutex);
303
304 if (kevent) {
305 ret = PTR_ERR(kevent);
306 if (IS_ERR(kevent))
307 break;
308 ret = copy_event_to_user(group, kevent, buf);
309 fsnotify_put_event(kevent);
310 if (ret < 0)
311 break;
312 buf += ret;
313 count -= ret;
314 continue;
315 }
316
317 ret = -EAGAIN;
318 if (file->f_flags & O_NONBLOCK)
319 break;
320 ret = -EINTR;
321 if (signal_pending(current))
322 break;
323
324 if (start != buf)
325 break;
326
327 schedule();
328 }
329
330 finish_wait(&group->notification_waitq, &wait);
331 if (start != buf && ret != -EFAULT)
332 ret = buf - start;
333 return ret;
334}
335
Eric Parisb2d87902009-12-17 21:24:34 -0500336static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
337{
338#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
339 struct fanotify_response response = { .fd = -1, .response = -1 };
340 struct fsnotify_group *group;
341 int ret;
342
343 group = file->private_data;
344
345 if (count > sizeof(response))
346 count = sizeof(response);
347
348 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
349
350 if (copy_from_user(&response, buf, count))
351 return -EFAULT;
352
353 ret = process_access_response(group, &response);
354 if (ret < 0)
355 count = ret;
356
357 return count;
358#else
359 return -EINVAL;
360#endif
361}
362
Eric Paris52c923d2009-12-17 21:24:26 -0500363static int fanotify_release(struct inode *ignored, struct file *file)
364{
365 struct fsnotify_group *group = file->private_data;
366
367 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
368
369 /* matches the fanotify_init->fsnotify_alloc_group */
370 fsnotify_put_group(group);
371
372 return 0;
373}
374
Eric Parisa1014f12009-12-17 21:24:26 -0500375static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
376{
377 struct fsnotify_group *group;
378 struct fsnotify_event_holder *holder;
379 void __user *p;
380 int ret = -ENOTTY;
381 size_t send_len = 0;
382
383 group = file->private_data;
384
385 p = (void __user *) arg;
386
387 switch (cmd) {
388 case FIONREAD:
389 mutex_lock(&group->notification_mutex);
390 list_for_each_entry(holder, &group->notification_list, event_list)
391 send_len += FAN_EVENT_METADATA_LEN;
392 mutex_unlock(&group->notification_mutex);
393 ret = put_user(send_len, (int __user *) p);
394 break;
395 }
396
397 return ret;
398}
399
Eric Paris52c923d2009-12-17 21:24:26 -0500400static const struct file_operations fanotify_fops = {
Eric Parisa1014f12009-12-17 21:24:26 -0500401 .poll = fanotify_poll,
402 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500403 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500404 .fasync = NULL,
405 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500406 .unlocked_ioctl = fanotify_ioctl,
407 .compat_ioctl = fanotify_ioctl,
Eric Paris52c923d2009-12-17 21:24:26 -0500408};
409
Eric Paris2a3edf82009-12-17 21:24:26 -0500410static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
411{
412 kmem_cache_free(fanotify_mark_cache, fsn_mark);
413}
414
415static int fanotify_find_path(int dfd, const char __user *filename,
416 struct path *path, unsigned int flags)
417{
418 int ret;
419
420 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
421 dfd, filename, flags);
422
423 if (filename == NULL) {
424 struct file *file;
425 int fput_needed;
426
427 ret = -EBADF;
428 file = fget_light(dfd, &fput_needed);
429 if (!file)
430 goto out;
431
432 ret = -ENOTDIR;
433 if ((flags & FAN_MARK_ONLYDIR) &&
434 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
435 fput_light(file, fput_needed);
436 goto out;
437 }
438
439 *path = file->f_path;
440 path_get(path);
441 fput_light(file, fput_needed);
442 } else {
443 unsigned int lookup_flags = 0;
444
445 if (!(flags & FAN_MARK_DONT_FOLLOW))
446 lookup_flags |= LOOKUP_FOLLOW;
447 if (flags & FAN_MARK_ONLYDIR)
448 lookup_flags |= LOOKUP_DIRECTORY;
449
450 ret = user_path_at(dfd, filename, lookup_flags, path);
451 if (ret)
452 goto out;
453 }
454
455 /* you can only watch an inode if you have read permissions on it */
456 ret = inode_permission(path->dentry->d_inode, MAY_READ);
457 if (ret)
458 path_put(path);
459out:
460 return ret;
461}
462
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500463static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
464 __u32 mask,
465 unsigned int flags)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500466{
467 __u32 oldmask;
468
469 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500470 if (!(flags & FAN_MARK_IGNORED_MASK)) {
471 oldmask = fsn_mark->mask;
472 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
473 } else {
474 oldmask = fsn_mark->ignored_mask;
475 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
476 }
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500477 spin_unlock(&fsn_mark->lock);
478
479 if (!(oldmask & ~mask))
480 fsnotify_destroy_mark(fsn_mark);
481
482 return mask & oldmask;
483}
484
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500485static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500486 struct vfsmount *mnt, __u32 mask,
487 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500488{
489 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500490 __u32 removed;
Eric Paris88826272009-12-17 21:24:28 -0500491
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500492 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
493 if (!fsn_mark)
494 return -ENOENT;
Eric Paris88826272009-12-17 21:24:28 -0500495
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500496 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500497 fsnotify_put_mark(fsn_mark);
498 if (removed & group->mask)
499 fsnotify_recalc_group_mask(group);
500 if (removed & mnt->mnt_fsnotify_mask)
501 fsnotify_recalc_vfsmount_mask(mnt);
Eric Paris88826272009-12-17 21:24:28 -0500502
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500503 return 0;
504}
505
506static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500507 struct inode *inode, __u32 mask,
508 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500509{
510 struct fsnotify_mark *fsn_mark = NULL;
511 __u32 removed;
512
513 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris88826272009-12-17 21:24:28 -0500514 if (!fsn_mark)
515 return -ENOENT;
516
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500517 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Eric Paris5444e292009-12-17 21:24:27 -0500518 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500519 fsnotify_put_mark(fsn_mark);
520
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500521 if (removed & group->mask)
522 fsnotify_recalc_group_mask(group);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500523 if (removed & inode->i_fsnotify_mask)
524 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500525
Eric Paris2a3edf82009-12-17 21:24:26 -0500526 return 0;
527}
528
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500529static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
530 __u32 mask,
531 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500532{
533 __u32 oldmask;
534
535 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500536 if (!(flags & FAN_MARK_IGNORED_MASK)) {
537 oldmask = fsn_mark->mask;
538 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
539 } else {
540 oldmask = fsn_mark->ignored_mask;
541 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
Eric Parisc9778a92009-12-17 21:24:33 -0500542 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
543 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500544 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500545 spin_unlock(&fsn_mark->lock);
546
547 return mask & ~oldmask;
548}
549
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500550static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500551 struct vfsmount *mnt, __u32 mask,
552 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500553{
554 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500555 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500556
Eric Paris88826272009-12-17 21:24:28 -0500557 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
558 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500559 int ret;
560
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500561 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
562 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500563 return -ENOMEM;
Eric Paris88826272009-12-17 21:24:28 -0500564
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500565 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
566 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
Eric Paris88826272009-12-17 21:24:28 -0500567 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500568 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500569 return ret;
Eric Paris88826272009-12-17 21:24:28 -0500570 }
Eric Paris88826272009-12-17 21:24:28 -0500571 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500572 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500573 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500574 if (added) {
575 if (added & ~group->mask)
576 fsnotify_recalc_group_mask(group);
577 if (added & ~mnt->mnt_fsnotify_mask)
578 fsnotify_recalc_vfsmount_mask(mnt);
579 }
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500580 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500581}
582
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500583static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500584 struct inode *inode, __u32 mask,
585 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500586{
587 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500588 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500589
590 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500591
Eric Paris5444e292009-12-17 21:24:27 -0500592 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500593 if (!fsn_mark) {
Eric Paris88826272009-12-17 21:24:28 -0500594 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500595
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500596 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
597 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500598 return -ENOMEM;
Eric Paris2a3edf82009-12-17 21:24:26 -0500599
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500600 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
601 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
Eric Paris2a3edf82009-12-17 21:24:26 -0500602 if (ret) {
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500603 fanotify_free_mark(fsn_mark);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500604 return ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500605 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500606 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500607 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500608 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500609 if (added) {
610 if (added & ~group->mask)
611 fsnotify_recalc_group_mask(group);
612 if (added & ~inode->i_fsnotify_mask)
613 fsnotify_recalc_inode_mask(inode);
614 }
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500615 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500616}
Eric Paris2a3edf82009-12-17 21:24:26 -0500617
Eric Paris52c923d2009-12-17 21:24:26 -0500618/* fanotify syscalls */
Eric Paris11637e42009-12-17 21:24:25 -0500619SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
620 unsigned int, priority)
621{
Eric Paris52c923d2009-12-17 21:24:26 -0500622 struct fsnotify_group *group;
623 int f_flags, fd;
624
625 pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
626 __func__, flags, event_f_flags, priority);
627
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 Pariscb2d4292009-12-17 21:24:34 -0500648 group->priority = priority;
Eric Paris9e66e422009-12-17 21:24:34 -0500649#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
650 mutex_init(&group->fanotify_data.access_mutex);
651 init_waitqueue_head(&group->fanotify_data.access_waitq);
652 INIT_LIST_HEAD(&group->fanotify_data.access_list);
653#endif
Eric Pariscb2d4292009-12-17 21:24:34 -0500654
Eric Paris52c923d2009-12-17 21:24:26 -0500655 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
656 if (fd < 0)
657 goto out_put_group;
658
659 return fd;
660
661out_put_group:
662 fsnotify_put_group(group);
663 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500664}
Eric Parisbbaa4162009-12-17 21:24:26 -0500665
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500666SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
667 __u64 mask, int dfd,
668 const char __user * pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500669{
Eric Paris0ff21db2009-12-17 21:24:29 -0500670 struct inode *inode = NULL;
671 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500672 struct fsnotify_group *group;
673 struct file *filp;
674 struct path path;
675 int ret, fput_needed;
676
677 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
678 __func__, fanotify_fd, flags, dfd, pathname, mask);
679
680 /* we only use the lower 32 bits as of right now. */
681 if (mask & ((__u64)0xffffffff << 32))
682 return -EINVAL;
683
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500684 if (flags & ~FAN_ALL_MARK_FLAGS)
685 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500686 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500687 case FAN_MARK_ADD:
688 case FAN_MARK_REMOVE:
Eric Paris4d926042009-12-17 21:24:34 -0500689 case FAN_MARK_FLUSH:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500690 break;
691 default:
692 return -EINVAL;
693 }
Eric Parisb2d87902009-12-17 21:24:34 -0500694#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
695 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
696#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500697 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500698#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500699 return -EINVAL;
700
701 filp = fget_light(fanotify_fd, &fput_needed);
702 if (unlikely(!filp))
703 return -EBADF;
704
705 /* verify that this is indeed an fanotify instance */
706 ret = -EINVAL;
707 if (unlikely(filp->f_op != &fanotify_fops))
708 goto fput_and_out;
709
710 ret = fanotify_find_path(dfd, pathname, &path, flags);
711 if (ret)
712 goto fput_and_out;
713
714 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500715 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500716 inode = path.dentry->d_inode;
717 else
718 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500719 group = filp->private_data;
720
721 /* create/update an inode mark */
Eric Paris4d926042009-12-17 21:24:34 -0500722 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500723 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500724 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500725 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500726 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500727 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500728 break;
729 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500730 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500731 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500732 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500733 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500734 break;
Eric Paris4d926042009-12-17 21:24:34 -0500735 case FAN_MARK_FLUSH:
736 if (flags & FAN_MARK_MOUNT)
737 fsnotify_clear_vfsmount_marks_by_group(group);
738 else
739 fsnotify_clear_inode_marks_by_group(group);
740 fsnotify_recalc_group_mask(group);
741 break;
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500742 default:
743 ret = -EINVAL;
744 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500745
746 path_put(&path);
747fput_and_out:
748 fput_light(filp, fput_needed);
749 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500750}
Eric Paris2a3edf82009-12-17 21:24:26 -0500751
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500752#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
753asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
754 long dfd, long pathname)
755{
756 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
757 mask, (int) dfd,
758 (const char __user *) pathname);
759}
760SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
761#endif
762
Eric Paris2a3edf82009-12-17 21:24:26 -0500763/*
764 * fanotify_user_setup - Our initialization function. Note that we cannnot return
765 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
766 * must result in panic().
767 */
768static int __init fanotify_user_setup(void)
769{
770 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Eric Parisb2d87902009-12-17 21:24:34 -0500771 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
772 SLAB_PANIC);
Eric Paris2a3edf82009-12-17 21:24:26 -0500773
774 return 0;
775}
776device_initcall(fanotify_user_setup);