1 #include <linux/fcntl.h>
2 #include <linux/file.h>
4 #include <linux/anon_inodes.h>
5 #include <linux/fsnotify_backend.h>
6 #include <linux/init.h>
7 #include <linux/namei.h>
8 #include <linux/security.h>
9 #include <linux/syscalls.h>
10 #include <linux/types.h>
14 static struct kmem_cache *fanotify_mark_cache __read_mostly;
16 static int fanotify_release(struct inode *ignored, struct file *file)
18 struct fsnotify_group *group = file->private_data;
20 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
22 /* matches the fanotify_init->fsnotify_alloc_group */
23 fsnotify_put_group(group);
28 static const struct file_operations fanotify_fops = {
32 .release = fanotify_release,
33 .unlocked_ioctl = NULL,
37 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
39 kmem_cache_free(fanotify_mark_cache, fsn_mark);
42 static int fanotify_find_path(int dfd, const char __user *filename,
43 struct path *path, unsigned int flags)
47 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
48 dfd, filename, flags);
50 if (filename == NULL) {
55 file = fget_light(dfd, &fput_needed);
60 if ((flags & FAN_MARK_ONLYDIR) &&
61 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
62 fput_light(file, fput_needed);
68 fput_light(file, fput_needed);
70 unsigned int lookup_flags = 0;
72 if (!(flags & FAN_MARK_DONT_FOLLOW))
73 lookup_flags |= LOOKUP_FOLLOW;
74 if (flags & FAN_MARK_ONLYDIR)
75 lookup_flags |= LOOKUP_DIRECTORY;
77 ret = user_path_at(dfd, filename, lookup_flags, path);
82 /* you can only watch an inode if you have read permissions on it */
83 ret = inode_permission(path->dentry->d_inode, MAY_READ);
90 static int fanotify_remove_mark(struct fsnotify_group *group,
94 struct fsnotify_mark *fsn_mark;
97 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
100 fsn_mark = fsnotify_find_mark(group, inode);
104 spin_lock(&fsn_mark->lock);
105 fsn_mark->mask &= ~mask;
106 new_mask = fsn_mark->mask;
107 spin_unlock(&fsn_mark->lock);
110 fsnotify_destroy_mark(fsn_mark);
112 fsnotify_recalc_inode_mask(inode);
114 fsnotify_recalc_group_mask(group);
116 /* matches the fsnotify_find_mark() */
117 fsnotify_put_mark(fsn_mark);
122 static int fanotify_add_mark(struct fsnotify_group *group,
126 struct fsnotify_mark *fsn_mark;
127 __u32 old_mask, new_mask;
130 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
133 fsn_mark = fsnotify_find_mark(group, inode);
135 struct fsnotify_mark *new_fsn_mark;
138 new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
142 fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
143 ret = fsnotify_add_mark(new_fsn_mark, group, inode, 0);
145 fanotify_free_mark(new_fsn_mark);
149 fsn_mark = new_fsn_mark;
154 spin_lock(&fsn_mark->lock);
155 old_mask = fsn_mark->mask;
156 fsn_mark->mask |= mask;
157 new_mask = fsn_mark->mask;
158 spin_unlock(&fsn_mark->lock);
160 /* we made changes to a mask, update the group mask and the inode mask
161 * so things happen quickly. */
162 if (old_mask != new_mask) {
163 /* more bits in old than in new? */
164 int dropped = (old_mask & ~new_mask);
165 /* more bits in this mark than the inode's mask? */
166 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
167 /* more bits in this mark than the group? */
168 int do_group = (new_mask & ~group->mask);
170 /* update the inode with this new mark */
171 if (dropped || do_inode)
172 fsnotify_recalc_inode_mask(inode);
174 /* update the group mask with the new mask */
175 if (dropped || do_group)
176 fsnotify_recalc_group_mask(group);
179 /* match the init or the find.... */
180 fsnotify_put_mark(fsn_mark);
185 static int fanotify_update_mark(struct fsnotify_group *group,
186 struct inode *inode, int flags,
189 pr_debug("%s: group=%p inode=%p flags=%x mask=%x\n", __func__,
190 group, inode, flags, mask);
192 if (flags & FAN_MARK_ADD)
193 fanotify_add_mark(group, inode, mask);
194 else if (flags & FAN_MARK_REMOVE)
195 fanotify_remove_mark(group, inode, mask);
202 static bool fanotify_mark_validate_input(int flags,
205 pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask);
207 /* are flags valid of this operation? */
208 if (!fanotify_mark_flags_valid(flags))
210 /* is the mask valid? */
211 if (!fanotify_mask_valid(mask))
216 /* fanotify syscalls */
217 SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
218 unsigned int, priority)
220 struct fsnotify_group *group;
223 pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
224 __func__, flags, event_f_flags, priority);
231 if (!capable(CAP_SYS_ADMIN))
234 if (flags & ~FAN_ALL_INIT_FLAGS)
237 f_flags = (O_RDONLY | FMODE_NONOTIFY);
238 if (flags & FAN_CLOEXEC)
239 f_flags |= O_CLOEXEC;
240 if (flags & FAN_NONBLOCK)
241 f_flags |= O_NONBLOCK;
243 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
244 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
246 return PTR_ERR(group);
248 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
255 fsnotify_put_group(group);
259 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
260 __u64, mask, int, dfd, const char __user *, pathname)
263 struct fsnotify_group *group;
266 int ret, fput_needed;
268 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
269 __func__, fanotify_fd, flags, dfd, pathname, mask);
271 /* we only use the lower 32 bits as of right now. */
272 if (mask & ((__u64)0xffffffff << 32))
275 if (!fanotify_mark_validate_input(flags, mask))
278 filp = fget_light(fanotify_fd, &fput_needed);
282 /* verify that this is indeed an fanotify instance */
284 if (unlikely(filp->f_op != &fanotify_fops))
287 ret = fanotify_find_path(dfd, pathname, &path, flags);
291 /* inode held in place by reference to path; group by fget on fd */
292 inode = path.dentry->d_inode;
293 group = filp->private_data;
295 /* create/update an inode mark */
296 ret = fanotify_update_mark(group, inode, flags, mask);
300 fput_light(filp, fput_needed);
305 * fanotify_user_setup - Our initialization function. Note that we cannnot return
306 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
307 * must result in panic().
309 static int __init fanotify_user_setup(void)
311 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
315 device_initcall(fanotify_user_setup);