blob: ea48693940f1b7eabe3ffd992b2df253f20e9506 [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
Al Viroc63181e2011-11-25 02:35:16 -050019#include "../../mount.h"
20
Eric Paris2529a0d2010-10-28 17:21:57 -040021#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Eric Parise7099d82010-10-28 17:21:57 -040022#define FANOTIFY_DEFAULT_MAX_MARKS 8192
Eric Paris4afeff82010-10-28 17:21:58 -040023#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
Eric Paris2529a0d2010-10-28 17:21:57 -040024
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050025extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050026
Eric Paris2a3edf82009-12-17 21:24:26 -050027static struct kmem_cache *fanotify_mark_cache __read_mostly;
Eric Parisb2d87902009-12-17 21:24:34 -050028static struct kmem_cache *fanotify_response_event_cache __read_mostly;
29
30struct fanotify_response_event {
31 struct list_head list;
32 __s32 fd;
33 struct fsnotify_event *event;
34};
Eric Paris2a3edf82009-12-17 21:24:26 -050035
Eric Parisa1014f12009-12-17 21:24:26 -050036/*
37 * Get an fsnotify notification event if one exists and is small
38 * enough to fit in "count". Return an error pointer if the count
39 * is not large enough.
40 *
41 * Called with the group->notification_mutex held.
42 */
43static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
44 size_t count)
45{
46 BUG_ON(!mutex_is_locked(&group->notification_mutex));
47
48 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
49
50 if (fsnotify_notify_queue_is_empty(group))
51 return NULL;
52
53 if (FAN_EVENT_METADATA_LEN > count)
54 return ERR_PTR(-EINVAL);
55
56 /* held the notification_mutex the whole time, so this is the
57 * same event we peeked above */
58 return fsnotify_remove_notify_event(group);
59}
60
Al Viro352e3b22012-08-19 12:30:45 -040061static int create_fd(struct fsnotify_group *group,
62 struct fsnotify_event *event,
63 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -050064{
65 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050066 struct file *new_file;
67
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050068 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050069
70 client_fd = get_unused_fd();
71 if (client_fd < 0)
72 return client_fd;
73
Linus Torvalds20696012010-08-12 14:23:04 -070074 if (event->data_type != FSNOTIFY_EVENT_PATH) {
Eric Parisa1014f12009-12-17 21:24:26 -050075 WARN_ON(1);
76 put_unused_fd(client_fd);
77 return -EINVAL;
78 }
79
80 /*
81 * we need a new file handle for the userspace program so it can read even if it was
82 * originally opened O_WRONLY.
83 */
Eric Parisa1014f12009-12-17 21:24:26 -050084 /* it's possible this event was an overflow event. in that case dentry and mnt
85 * are NULL; That's fine, just don't call dentry open */
Al Viro765927b2012-06-26 21:58:53 +040086 if (event->path.dentry && event->path.mnt)
87 new_file = dentry_open(&event->path,
Eric Paris80af2582010-07-28 10:18:37 -040088 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -050089 current_cred());
90 else
91 new_file = ERR_PTR(-EOVERFLOW);
92 if (IS_ERR(new_file)) {
93 /*
94 * we still send an event even if we can't open the file. this
95 * can happen when say tasks are gone and we try to open their
96 * /proc files or we try to open a WRONLY file like in sysfs
97 * we just send the errno to userspace since there isn't much
98 * else we can do.
99 */
100 put_unused_fd(client_fd);
101 client_fd = PTR_ERR(new_file);
102 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400103 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500104 }
105
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500106 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500107}
108
Eric Parisecf6f5e2010-11-08 18:08:14 -0500109static int fill_event_metadata(struct fsnotify_group *group,
Eric Parisa1014f12009-12-17 21:24:26 -0500110 struct fanotify_event_metadata *metadata,
Al Viro352e3b22012-08-19 12:30:45 -0400111 struct fsnotify_event *event,
112 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500113{
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100114 int ret = 0;
115
Eric Parisa1014f12009-12-17 21:24:26 -0500116 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
117 group, metadata, event);
118
Al Viro352e3b22012-08-19 12:30:45 -0400119 *file = NULL;
Eric Parisa1014f12009-12-17 21:24:26 -0500120 metadata->event_len = FAN_EVENT_METADATA_LEN;
Eric Paris7d131622010-12-07 15:27:57 -0500121 metadata->metadata_len = FAN_EVENT_METADATA_LEN;
Eric Parisa1014f12009-12-17 21:24:26 -0500122 metadata->vers = FANOTIFY_METADATA_VERSION;
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -0500123 metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500124 metadata->pid = pid_vnr(event->tgid);
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100125 if (unlikely(event->mask & FAN_Q_OVERFLOW))
126 metadata->fd = FAN_NOFD;
127 else {
Al Viro352e3b22012-08-19 12:30:45 -0400128 metadata->fd = create_fd(group, event, file);
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100129 if (metadata->fd < 0)
130 ret = metadata->fd;
131 }
Eric Parisa1014f12009-12-17 21:24:26 -0500132
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100133 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500134}
135
Eric Parisb2d87902009-12-17 21:24:34 -0500136#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
137static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
138 __s32 fd)
139{
140 struct fanotify_response_event *re, *return_re = NULL;
141
142 mutex_lock(&group->fanotify_data.access_mutex);
143 list_for_each_entry(re, &group->fanotify_data.access_list, list) {
144 if (re->fd != fd)
145 continue;
146
147 list_del_init(&re->list);
148 return_re = re;
149 break;
150 }
151 mutex_unlock(&group->fanotify_data.access_mutex);
152
153 pr_debug("%s: found return_re=%p\n", __func__, return_re);
154
155 return return_re;
156}
157
158static int process_access_response(struct fsnotify_group *group,
159 struct fanotify_response *response_struct)
160{
161 struct fanotify_response_event *re;
162 __s32 fd = response_struct->fd;
163 __u32 response = response_struct->response;
164
165 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
166 fd, response);
167 /*
168 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300169 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500170 * timeout
171 */
172 switch (response) {
173 case FAN_ALLOW:
174 case FAN_DENY:
175 break;
176 default:
177 return -EINVAL;
178 }
179
180 if (fd < 0)
181 return -EINVAL;
182
183 re = dequeue_re(group, fd);
184 if (!re)
185 return -ENOENT;
186
187 re->event->response = response;
188
189 wake_up(&group->fanotify_data.access_waitq);
190
191 kmem_cache_free(fanotify_response_event_cache, re);
192
193 return 0;
194}
195
196static int prepare_for_access_response(struct fsnotify_group *group,
197 struct fsnotify_event *event,
198 __s32 fd)
199{
200 struct fanotify_response_event *re;
201
202 if (!(event->mask & FAN_ALL_PERM_EVENTS))
203 return 0;
204
205 re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
206 if (!re)
207 return -ENOMEM;
208
209 re->event = event;
210 re->fd = fd;
211
212 mutex_lock(&group->fanotify_data.access_mutex);
Eric Paris2eebf582010-08-18 12:25:50 -0400213
Lino Sanfilippo09e5f142010-11-19 10:58:07 +0100214 if (atomic_read(&group->fanotify_data.bypass_perm)) {
Eric Paris2eebf582010-08-18 12:25:50 -0400215 mutex_unlock(&group->fanotify_data.access_mutex);
216 kmem_cache_free(fanotify_response_event_cache, re);
217 event->response = FAN_ALLOW;
218 return 0;
219 }
220
Eric Parisb2d87902009-12-17 21:24:34 -0500221 list_add_tail(&re->list, &group->fanotify_data.access_list);
222 mutex_unlock(&group->fanotify_data.access_mutex);
223
224 return 0;
225}
226
Eric Parisb2d87902009-12-17 21:24:34 -0500227#else
228static int prepare_for_access_response(struct fsnotify_group *group,
229 struct fsnotify_event *event,
230 __s32 fd)
231{
232 return 0;
233}
234
Eric Parisb2d87902009-12-17 21:24:34 -0500235#endif
236
Eric Parisa1014f12009-12-17 21:24:26 -0500237static ssize_t copy_event_to_user(struct fsnotify_group *group,
238 struct fsnotify_event *event,
239 char __user *buf)
240{
241 struct fanotify_event_metadata fanotify_event_metadata;
Al Viro352e3b22012-08-19 12:30:45 -0400242 struct file *f;
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
Al Viro352e3b22012-08-19 12:30:45 -0400247 ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
Eric Parisecf6f5e2010-11-08 18:08:14 -0500248 if (ret < 0)
249 goto out;
Eric Parisa1014f12009-12-17 21:24:26 -0500250
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100251 fd = fanotify_event_metadata.fd;
Al Viro352e3b22012-08-19 12:30:45 -0400252 ret = -EFAULT;
253 if (copy_to_user(buf, &fanotify_event_metadata,
254 fanotify_event_metadata.event_len))
255 goto out_close_fd;
256
Eric Parisb2d87902009-12-17 21:24:34 -0500257 ret = prepare_for_access_response(group, event, fd);
258 if (ret)
259 goto out_close_fd;
260
Al Viro352e3b22012-08-19 12:30:45 -0400261 fd_install(fd, f);
Eric Paris7d131622010-12-07 15:27:57 -0500262 return fanotify_event_metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500263
Eric Parisb2d87902009-12-17 21:24:34 -0500264out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400265 if (fd != FAN_NOFD) {
266 put_unused_fd(fd);
267 fput(f);
268 }
Eric Parisecf6f5e2010-11-08 18:08:14 -0500269out:
270#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
271 if (event->mask & FAN_ALL_PERM_EVENTS) {
272 event->response = FAN_DENY;
273 wake_up(&group->fanotify_data.access_waitq);
274 }
275#endif
Eric Parisb2d87902009-12-17 21:24:34 -0500276 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;
Lino Sanfilippo1a5cea72010-10-29 12:06:42 +0200331 ret = -ERESTARTSYS;
Eric Parisa1014f12009-12-17 21:24:26 -0500332 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 Paris52c923d2009-12-17 21:24:26 -0500377
Eric Paris2eebf582010-08-18 12:25:50 -0400378#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Andrew Morton19ba54f2010-10-28 17:21:59 -0400379 struct fanotify_response_event *re, *lre;
380
Eric Paris2eebf582010-08-18 12:25:50 -0400381 mutex_lock(&group->fanotify_data.access_mutex);
382
Lino Sanfilippo09e5f142010-11-19 10:58:07 +0100383 atomic_inc(&group->fanotify_data.bypass_perm);
Eric Paris2eebf582010-08-18 12:25:50 -0400384
385 list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
386 pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
387 re, re->event);
388
389 list_del_init(&re->list);
390 re->event->response = FAN_ALLOW;
391
392 kmem_cache_free(fanotify_response_event_cache, re);
393 }
394 mutex_unlock(&group->fanotify_data.access_mutex);
395
396 wake_up(&group->fanotify_data.access_waitq);
397#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500398 /* matches the fanotify_init->fsnotify_alloc_group */
399 fsnotify_put_group(group);
400
401 return 0;
402}
403
Eric Parisa1014f12009-12-17 21:24:26 -0500404static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
405{
406 struct fsnotify_group *group;
407 struct fsnotify_event_holder *holder;
408 void __user *p;
409 int ret = -ENOTTY;
410 size_t send_len = 0;
411
412 group = file->private_data;
413
414 p = (void __user *) arg;
415
416 switch (cmd) {
417 case FIONREAD:
418 mutex_lock(&group->notification_mutex);
419 list_for_each_entry(holder, &group->notification_list, event_list)
420 send_len += FAN_EVENT_METADATA_LEN;
421 mutex_unlock(&group->notification_mutex);
422 ret = put_user(send_len, (int __user *) p);
423 break;
424 }
425
426 return ret;
427}
428
Eric Paris52c923d2009-12-17 21:24:26 -0500429static const struct file_operations fanotify_fops = {
Eric Parisa1014f12009-12-17 21:24:26 -0500430 .poll = fanotify_poll,
431 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500432 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500433 .fasync = NULL,
434 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500435 .unlocked_ioctl = fanotify_ioctl,
436 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200437 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500438};
439
Eric Paris2a3edf82009-12-17 21:24:26 -0500440static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
441{
442 kmem_cache_free(fanotify_mark_cache, fsn_mark);
443}
444
445static int fanotify_find_path(int dfd, const char __user *filename,
446 struct path *path, unsigned int flags)
447{
448 int ret;
449
450 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
451 dfd, filename, flags);
452
453 if (filename == NULL) {
454 struct file *file;
455 int fput_needed;
456
457 ret = -EBADF;
458 file = fget_light(dfd, &fput_needed);
459 if (!file)
460 goto out;
461
462 ret = -ENOTDIR;
463 if ((flags & FAN_MARK_ONLYDIR) &&
464 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
465 fput_light(file, fput_needed);
466 goto out;
467 }
468
469 *path = file->f_path;
470 path_get(path);
471 fput_light(file, fput_needed);
472 } else {
473 unsigned int lookup_flags = 0;
474
475 if (!(flags & FAN_MARK_DONT_FOLLOW))
476 lookup_flags |= LOOKUP_FOLLOW;
477 if (flags & FAN_MARK_ONLYDIR)
478 lookup_flags |= LOOKUP_DIRECTORY;
479
480 ret = user_path_at(dfd, filename, lookup_flags, path);
481 if (ret)
482 goto out;
483 }
484
485 /* you can only watch an inode if you have read permissions on it */
486 ret = inode_permission(path->dentry->d_inode, MAY_READ);
487 if (ret)
488 path_put(path);
489out:
490 return ret;
491}
492
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500493static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
494 __u32 mask,
495 unsigned int flags)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500496{
497 __u32 oldmask;
498
499 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500500 if (!(flags & FAN_MARK_IGNORED_MASK)) {
501 oldmask = fsn_mark->mask;
502 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
503 } else {
504 oldmask = fsn_mark->ignored_mask;
505 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
506 }
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500507 spin_unlock(&fsn_mark->lock);
508
509 if (!(oldmask & ~mask))
510 fsnotify_destroy_mark(fsn_mark);
511
512 return mask & oldmask;
513}
514
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500515static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500516 struct vfsmount *mnt, __u32 mask,
517 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500518{
519 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500520 __u32 removed;
Eric Paris88826272009-12-17 21:24:28 -0500521
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500522 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
523 if (!fsn_mark)
524 return -ENOENT;
Eric Paris88826272009-12-17 21:24:28 -0500525
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500526 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500527 fsnotify_put_mark(fsn_mark);
Al Viroc63181e2011-11-25 02:35:16 -0500528 if (removed & real_mount(mnt)->mnt_fsnotify_mask)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500529 fsnotify_recalc_vfsmount_mask(mnt);
Eric Paris88826272009-12-17 21:24:28 -0500530
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500531 return 0;
532}
533
534static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500535 struct inode *inode, __u32 mask,
536 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500537{
538 struct fsnotify_mark *fsn_mark = NULL;
539 __u32 removed;
540
541 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris88826272009-12-17 21:24:28 -0500542 if (!fsn_mark)
543 return -ENOENT;
544
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500545 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
Eric Paris5444e292009-12-17 21:24:27 -0500546 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500547 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500548 if (removed & inode->i_fsnotify_mask)
549 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500550
Eric Paris2a3edf82009-12-17 21:24:26 -0500551 return 0;
552}
553
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500554static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
555 __u32 mask,
556 unsigned int flags)
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500557{
Eric Paris192ca4d2010-10-28 17:21:59 -0400558 __u32 oldmask = -1;
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500559
560 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500561 if (!(flags & FAN_MARK_IGNORED_MASK)) {
562 oldmask = fsn_mark->mask;
563 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
564 } else {
Eric Paris192ca4d2010-10-28 17:21:59 -0400565 __u32 tmask = fsn_mark->ignored_mask | mask;
566 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
Eric Parisc9778a92009-12-17 21:24:33 -0500567 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
568 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500569 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400570
571 if (!(flags & FAN_MARK_ONDIR)) {
572 __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR;
573 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
574 }
575
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500576 spin_unlock(&fsn_mark->lock);
577
578 return mask & ~oldmask;
579}
580
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500581static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500582 struct vfsmount *mnt, __u32 mask,
583 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500584{
585 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500586 __u32 added;
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100587 int ret = 0;
Eric Paris2a3edf82009-12-17 21:24:26 -0500588
Eric Paris88826272009-12-17 21:24:28 -0500589 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
590 if (!fsn_mark) {
Eric Parise7099d82010-10-28 17:21:57 -0400591 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
592 return -ENOSPC;
593
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500594 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
595 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500596 return -ENOMEM;
Eric Paris88826272009-12-17 21:24:28 -0500597
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500598 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
599 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100600 if (ret)
601 goto err;
Eric Paris88826272009-12-17 21:24:28 -0500602 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500603 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100604
Al Viroc63181e2011-11-25 02:35:16 -0500605 if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
Eric Paris43709a22010-07-28 10:18:39 -0400606 fsnotify_recalc_vfsmount_mask(mnt);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100607err:
608 fsnotify_put_mark(fsn_mark);
609 return ret;
Eric Paris88826272009-12-17 21:24:28 -0500610}
611
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500612static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500613 struct inode *inode, __u32 mask,
614 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500615{
616 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500617 __u32 added;
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100618 int ret = 0;
Eric Paris88826272009-12-17 21:24:28 -0500619
620 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500621
Eric Paris5322a592010-10-28 17:21:57 -0400622 /*
623 * If some other task has this inode open for write we should not add
624 * an ignored mark, unless that ignored mark is supposed to survive
625 * modification changes anyway.
626 */
627 if ((flags & FAN_MARK_IGNORED_MASK) &&
628 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
629 (atomic_read(&inode->i_writecount) > 0))
630 return 0;
631
Eric Paris5444e292009-12-17 21:24:27 -0500632 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500633 if (!fsn_mark) {
Eric Parise7099d82010-10-28 17:21:57 -0400634 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
635 return -ENOSPC;
636
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500637 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
638 if (!fsn_mark)
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500639 return -ENOMEM;
Eric Paris2a3edf82009-12-17 21:24:26 -0500640
Andreas Gruenbacher912ee392009-12-17 21:24:28 -0500641 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
642 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100643 if (ret)
644 goto err;
Eric Paris2a3edf82009-12-17 21:24:26 -0500645 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500646 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100647
Eric Paris43709a22010-07-28 10:18:39 -0400648 if (added & ~inode->i_fsnotify_mask)
649 fsnotify_recalc_inode_mask(inode);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100650err:
651 fsnotify_put_mark(fsn_mark);
652 return ret;
Eric Paris88826272009-12-17 21:24:28 -0500653}
Eric Paris2a3edf82009-12-17 21:24:26 -0500654
Eric Paris52c923d2009-12-17 21:24:26 -0500655/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400656SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500657{
Eric Paris52c923d2009-12-17 21:24:26 -0500658 struct fsnotify_group *group;
659 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400660 struct user_struct *user;
Eric Paris52c923d2009-12-17 21:24:26 -0500661
Eric Paris08ae8932010-05-27 09:41:40 -0400662 pr_debug("%s: flags=%d event_f_flags=%d\n",
663 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500664
Eric Paris52c923d2009-12-17 21:24:26 -0500665 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200666 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500667
668 if (flags & ~FAN_ALL_INIT_FLAGS)
669 return -EINVAL;
670
Eric Paris4afeff82010-10-28 17:21:58 -0400671 user = get_current_user();
672 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
673 free_uid(user);
674 return -EMFILE;
675 }
676
Eric Parisb2d87902009-12-17 21:24:34 -0500677 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500678 if (flags & FAN_CLOEXEC)
679 f_flags |= O_CLOEXEC;
680 if (flags & FAN_NONBLOCK)
681 f_flags |= O_NONBLOCK;
682
683 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
684 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris263791982010-11-23 23:48:26 -0500685 if (IS_ERR(group)) {
686 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500687 return PTR_ERR(group);
Eric Paris263791982010-11-23 23:48:26 -0500688 }
Eric Paris52c923d2009-12-17 21:24:26 -0500689
Eric Paris4afeff82010-10-28 17:21:58 -0400690 group->fanotify_data.user = user;
691 atomic_inc(&user->fanotify_listeners);
692
Eric Paris80af2582010-07-28 10:18:37 -0400693 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500694#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
695 mutex_init(&group->fanotify_data.access_mutex);
696 init_waitqueue_head(&group->fanotify_data.access_waitq);
697 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Lino Sanfilippo09e5f142010-11-19 10:58:07 +0100698 atomic_set(&group->fanotify_data.bypass_perm, 0);
Eric Paris9e66e422009-12-17 21:24:34 -0500699#endif
Eric Paris4231a232010-10-28 17:21:56 -0400700 switch (flags & FAN_ALL_CLASS_BITS) {
701 case FAN_CLASS_NOTIF:
702 group->priority = FS_PRIO_0;
703 break;
704 case FAN_CLASS_CONTENT:
705 group->priority = FS_PRIO_1;
706 break;
707 case FAN_CLASS_PRE_CONTENT:
708 group->priority = FS_PRIO_2;
709 break;
710 default:
711 fd = -EINVAL;
712 goto out_put_group;
713 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500714
Eric Paris5dd03f52010-10-28 17:21:57 -0400715 if (flags & FAN_UNLIMITED_QUEUE) {
716 fd = -EPERM;
717 if (!capable(CAP_SYS_ADMIN))
718 goto out_put_group;
719 group->max_events = UINT_MAX;
720 } else {
721 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
722 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400723
Eric Parisac7e22d2010-10-28 17:21:58 -0400724 if (flags & FAN_UNLIMITED_MARKS) {
725 fd = -EPERM;
726 if (!capable(CAP_SYS_ADMIN))
727 goto out_put_group;
728 group->fanotify_data.max_marks = UINT_MAX;
729 } else {
730 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
731 }
Eric Parise7099d82010-10-28 17:21:57 -0400732
Eric Paris52c923d2009-12-17 21:24:26 -0500733 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
734 if (fd < 0)
735 goto out_put_group;
736
737 return fd;
738
739out_put_group:
740 fsnotify_put_group(group);
741 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500742}
Eric Parisbbaa4162009-12-17 21:24:26 -0500743
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500744SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
745 __u64 mask, int dfd,
746 const char __user * pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500747{
Eric Paris0ff21db2009-12-17 21:24:29 -0500748 struct inode *inode = NULL;
749 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500750 struct fsnotify_group *group;
751 struct file *filp;
752 struct path path;
753 int ret, fput_needed;
754
755 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
756 __func__, fanotify_fd, flags, dfd, pathname, mask);
757
758 /* we only use the lower 32 bits as of right now. */
759 if (mask & ((__u64)0xffffffff << 32))
760 return -EINVAL;
761
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500762 if (flags & ~FAN_ALL_MARK_FLAGS)
763 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500764 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100765 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500766 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100767 if (!mask)
768 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500769 case FAN_MARK_FLUSH:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500770 break;
771 default:
772 return -EINVAL;
773 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400774
775 if (mask & FAN_ONDIR) {
776 flags |= FAN_MARK_ONDIR;
777 mask &= ~FAN_ONDIR;
778 }
779
Eric Parisb2d87902009-12-17 21:24:34 -0500780#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
781 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
782#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500783 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500784#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500785 return -EINVAL;
786
787 filp = fget_light(fanotify_fd, &fput_needed);
788 if (unlikely(!filp))
789 return -EBADF;
790
791 /* verify that this is indeed an fanotify instance */
792 ret = -EINVAL;
793 if (unlikely(filp->f_op != &fanotify_fops))
794 goto fput_and_out;
Eric Paris4231a232010-10-28 17:21:56 -0400795 group = filp->private_data;
796
797 /*
798 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
799 * allowed to set permissions events.
800 */
801 ret = -EINVAL;
802 if (mask & FAN_ALL_PERM_EVENTS &&
803 group->priority == FS_PRIO_0)
804 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500805
806 ret = fanotify_find_path(dfd, pathname, &path, flags);
807 if (ret)
808 goto fput_and_out;
809
810 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500811 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500812 inode = path.dentry->d_inode;
813 else
814 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500815
816 /* create/update an inode mark */
Eric Paris4d926042009-12-17 21:24:34 -0500817 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500818 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500819 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500820 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500821 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500822 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500823 break;
824 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500825 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500826 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500827 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500828 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500829 break;
Eric Paris4d926042009-12-17 21:24:34 -0500830 case FAN_MARK_FLUSH:
831 if (flags & FAN_MARK_MOUNT)
832 fsnotify_clear_vfsmount_marks_by_group(group);
833 else
834 fsnotify_clear_inode_marks_by_group(group);
Eric Paris4d926042009-12-17 21:24:34 -0500835 break;
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500836 default:
837 ret = -EINVAL;
838 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500839
840 path_put(&path);
841fput_and_out:
842 fput_light(filp, fput_needed);
843 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500844}
Eric Paris2a3edf82009-12-17 21:24:26 -0500845
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500846#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
847asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
848 long dfd, long pathname)
849{
850 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
851 mask, (int) dfd,
852 (const char __user *) pathname);
853}
854SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
855#endif
856
Eric Paris2a3edf82009-12-17 21:24:26 -0500857/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100858 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500859 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
860 * must result in panic().
861 */
862static int __init fanotify_user_setup(void)
863{
864 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Eric Parisb2d87902009-12-17 21:24:34 -0500865 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
866 SLAB_PANIC);
Eric Paris2a3edf82009-12-17 21:24:26 -0500867
868 return 0;
869}
870device_initcall(fanotify_user_setup);