]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - fs/notify/fanotify/fanotify_user.c
fanotify: allow userspace to override max marks
[net-next-2.6.git] / fs / notify / fanotify / fanotify_user.c
index bbcb98e7fcc611d692e9d85c4c74dd742d417507..f9216102b4261f997b6838be271110f4a3238a6e 100644 (file)
@@ -16,6 +16,9 @@
 
 #include <asm/ioctls.h>
 
+#define FANOTIFY_DEFAULT_MAX_EVENTS    16384
+#define FANOTIFY_DEFAULT_MAX_MARKS     8192
+
 extern const struct fsnotify_ops fanotify_fsnotify_ops;
 
 static struct kmem_cache *fanotify_mark_cache __read_mostly;
@@ -582,6 +585,9 @@ static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
        if (!fsn_mark) {
                int ret;
 
+               if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
+                       return -ENOSPC;
+
                fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
                if (!fsn_mark)
                        return -ENOMEM;
@@ -610,10 +616,23 @@ static int fanotify_add_inode_mark(struct fsnotify_group *group,
 
        pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
 
+       /*
+        * If some other task has this inode open for write we should not add
+        * an ignored mark, unless that ignored mark is supposed to survive
+        * modification changes anyway.
+        */
+       if ((flags & FAN_MARK_IGNORED_MASK) &&
+           !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
+           (atomic_read(&inode->i_writecount) > 0))
+               return 0;
+
        fsn_mark = fsnotify_find_inode_mark(group, inode);
        if (!fsn_mark) {
                int ret;
 
+               if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
+                       return -ENOSPC;
+
                fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
                if (!fsn_mark)
                        return -ENOMEM;
@@ -664,6 +683,38 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
        init_waitqueue_head(&group->fanotify_data.access_waitq);
        INIT_LIST_HEAD(&group->fanotify_data.access_list);
 #endif
+       switch (flags & FAN_ALL_CLASS_BITS) {
+       case FAN_CLASS_NOTIF:
+               group->priority = FS_PRIO_0;
+               break;
+       case FAN_CLASS_CONTENT:
+               group->priority = FS_PRIO_1;
+               break;
+       case FAN_CLASS_PRE_CONTENT:
+               group->priority = FS_PRIO_2;
+               break;
+       default:
+               fd = -EINVAL;
+               goto out_put_group;
+       }
+
+       if (flags & FAN_UNLIMITED_QUEUE) {
+               fd = -EPERM;
+               if (!capable(CAP_SYS_ADMIN))
+                       goto out_put_group;
+               group->max_events = UINT_MAX;
+       } else {
+               group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
+       }
+
+       if (flags & FAN_UNLIMITED_MARKS) {
+               fd = -EPERM;
+               if (!capable(CAP_SYS_ADMIN))
+                       goto out_put_group;
+               group->fanotify_data.max_marks = UINT_MAX;
+       } else {
+               group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
+       }
 
        fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
        if (fd < 0)
@@ -719,6 +770,16 @@ SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
        ret = -EINVAL;
        if (unlikely(filp->f_op != &fanotify_fops))
                goto fput_and_out;
+       group = filp->private_data;
+
+       /*
+        * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF.  These are not
+        * allowed to set permissions events.
+        */
+       ret = -EINVAL;
+       if (mask & FAN_ALL_PERM_EVENTS &&
+           group->priority == FS_PRIO_0)
+               goto fput_and_out;
 
        ret = fanotify_find_path(dfd, pathname, &path, flags);
        if (ret)
@@ -729,7 +790,6 @@ SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
                inode = path.dentry->d_inode;
        else
                mnt = path.mnt;
-       group = filp->private_data;
 
        /* create/update an inode mark */
        switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {