Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 1 | #include <linux/fcntl.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 2 | #include <linux/file.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 3 | #include <linux/fs.h> |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 4 | #include <linux/anon_inodes.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 5 | #include <linux/fsnotify_backend.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 6 | #include <linux/init.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 7 | #include <linux/mount.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 8 | #include <linux/namei.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 9 | #include <linux/poll.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 10 | #include <linux/security.h> |
| 11 | #include <linux/syscalls.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 12 | #include <linux/types.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
| 14 | |
| 15 | #include <asm/ioctls.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 16 | |
| 17 | #include "fanotify.h" |
| 18 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 19 | static struct kmem_cache *fanotify_mark_cache __read_mostly; |
| 20 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 21 | /* |
| 22 | * Get an fsnotify notification event if one exists and is small |
| 23 | * enough to fit in "count". Return an error pointer if the count |
| 24 | * is not large enough. |
| 25 | * |
| 26 | * Called with the group->notification_mutex held. |
| 27 | */ |
| 28 | static struct fsnotify_event *get_one_event(struct fsnotify_group *group, |
| 29 | size_t count) |
| 30 | { |
| 31 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 32 | |
| 33 | pr_debug("%s: group=%p count=%zd\n", __func__, group, count); |
| 34 | |
| 35 | if (fsnotify_notify_queue_is_empty(group)) |
| 36 | return NULL; |
| 37 | |
| 38 | if (FAN_EVENT_METADATA_LEN > count) |
| 39 | return ERR_PTR(-EINVAL); |
| 40 | |
| 41 | /* held the notification_mutex the whole time, so this is the |
| 42 | * same event we peeked above */ |
| 43 | return fsnotify_remove_notify_event(group); |
| 44 | } |
| 45 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 46 | static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 47 | { |
| 48 | int client_fd; |
| 49 | struct dentry *dentry; |
| 50 | struct vfsmount *mnt; |
| 51 | struct file *new_file; |
| 52 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 53 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 54 | |
| 55 | client_fd = get_unused_fd(); |
| 56 | if (client_fd < 0) |
| 57 | return client_fd; |
| 58 | |
| 59 | if (event->data_type != FSNOTIFY_EVENT_PATH) { |
| 60 | WARN_ON(1); |
| 61 | put_unused_fd(client_fd); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * we need a new file handle for the userspace program so it can read even if it was |
| 67 | * originally opened O_WRONLY. |
| 68 | */ |
| 69 | dentry = dget(event->path.dentry); |
| 70 | mnt = mntget(event->path.mnt); |
| 71 | /* it's possible this event was an overflow event. in that case dentry and mnt |
| 72 | * are NULL; That's fine, just don't call dentry open */ |
| 73 | if (dentry && mnt) |
| 74 | new_file = dentry_open(dentry, mnt, |
| 75 | O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY, |
| 76 | current_cred()); |
| 77 | else |
| 78 | new_file = ERR_PTR(-EOVERFLOW); |
| 79 | if (IS_ERR(new_file)) { |
| 80 | /* |
| 81 | * we still send an event even if we can't open the file. this |
| 82 | * can happen when say tasks are gone and we try to open their |
| 83 | * /proc files or we try to open a WRONLY file like in sysfs |
| 84 | * we just send the errno to userspace since there isn't much |
| 85 | * else we can do. |
| 86 | */ |
| 87 | put_unused_fd(client_fd); |
| 88 | client_fd = PTR_ERR(new_file); |
| 89 | } else { |
| 90 | fd_install(client_fd, new_file); |
| 91 | } |
| 92 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 93 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static ssize_t fill_event_metadata(struct fsnotify_group *group, |
| 97 | struct fanotify_event_metadata *metadata, |
| 98 | struct fsnotify_event *event) |
| 99 | { |
| 100 | pr_debug("%s: group=%p metadata=%p event=%p\n", __func__, |
| 101 | group, metadata, event); |
| 102 | |
| 103 | metadata->event_len = FAN_EVENT_METADATA_LEN; |
| 104 | metadata->vers = FANOTIFY_METADATA_VERSION; |
| 105 | metadata->mask = fanotify_outgoing_mask(event->mask); |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 106 | metadata->pid = pid_vnr(event->tgid); |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 107 | metadata->fd = create_fd(group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 108 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 109 | return metadata->fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
| 113 | struct fsnotify_event *event, |
| 114 | char __user *buf) |
| 115 | { |
| 116 | struct fanotify_event_metadata fanotify_event_metadata; |
| 117 | int ret; |
| 118 | |
| 119 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 120 | |
| 121 | ret = fill_event_metadata(group, &fanotify_event_metadata, event); |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 122 | if (ret < 0) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 123 | return ret; |
| 124 | |
| 125 | if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN)) |
| 126 | return -EFAULT; |
| 127 | |
| 128 | return FAN_EVENT_METADATA_LEN; |
| 129 | } |
| 130 | |
| 131 | /* intofiy userspace file descriptor functions */ |
| 132 | static unsigned int fanotify_poll(struct file *file, poll_table *wait) |
| 133 | { |
| 134 | struct fsnotify_group *group = file->private_data; |
| 135 | int ret = 0; |
| 136 | |
| 137 | poll_wait(file, &group->notification_waitq, wait); |
| 138 | mutex_lock(&group->notification_mutex); |
| 139 | if (!fsnotify_notify_queue_is_empty(group)) |
| 140 | ret = POLLIN | POLLRDNORM; |
| 141 | mutex_unlock(&group->notification_mutex); |
| 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static ssize_t fanotify_read(struct file *file, char __user *buf, |
| 147 | size_t count, loff_t *pos) |
| 148 | { |
| 149 | struct fsnotify_group *group; |
| 150 | struct fsnotify_event *kevent; |
| 151 | char __user *start; |
| 152 | int ret; |
| 153 | DEFINE_WAIT(wait); |
| 154 | |
| 155 | start = buf; |
| 156 | group = file->private_data; |
| 157 | |
| 158 | pr_debug("%s: group=%p\n", __func__, group); |
| 159 | |
| 160 | while (1) { |
| 161 | prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE); |
| 162 | |
| 163 | mutex_lock(&group->notification_mutex); |
| 164 | kevent = get_one_event(group, count); |
| 165 | mutex_unlock(&group->notification_mutex); |
| 166 | |
| 167 | if (kevent) { |
| 168 | ret = PTR_ERR(kevent); |
| 169 | if (IS_ERR(kevent)) |
| 170 | break; |
| 171 | ret = copy_event_to_user(group, kevent, buf); |
| 172 | fsnotify_put_event(kevent); |
| 173 | if (ret < 0) |
| 174 | break; |
| 175 | buf += ret; |
| 176 | count -= ret; |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | ret = -EAGAIN; |
| 181 | if (file->f_flags & O_NONBLOCK) |
| 182 | break; |
| 183 | ret = -EINTR; |
| 184 | if (signal_pending(current)) |
| 185 | break; |
| 186 | |
| 187 | if (start != buf) |
| 188 | break; |
| 189 | |
| 190 | schedule(); |
| 191 | } |
| 192 | |
| 193 | finish_wait(&group->notification_waitq, &wait); |
| 194 | if (start != buf && ret != -EFAULT) |
| 195 | ret = buf - start; |
| 196 | return ret; |
| 197 | } |
| 198 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 199 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 200 | { |
| 201 | struct fsnotify_group *group = file->private_data; |
| 202 | |
| 203 | pr_debug("%s: file=%p group=%p\n", __func__, file, group); |
| 204 | |
| 205 | /* matches the fanotify_init->fsnotify_alloc_group */ |
| 206 | fsnotify_put_group(group); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 211 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 212 | { |
| 213 | struct fsnotify_group *group; |
| 214 | struct fsnotify_event_holder *holder; |
| 215 | void __user *p; |
| 216 | int ret = -ENOTTY; |
| 217 | size_t send_len = 0; |
| 218 | |
| 219 | group = file->private_data; |
| 220 | |
| 221 | p = (void __user *) arg; |
| 222 | |
| 223 | switch (cmd) { |
| 224 | case FIONREAD: |
| 225 | mutex_lock(&group->notification_mutex); |
| 226 | list_for_each_entry(holder, &group->notification_list, event_list) |
| 227 | send_len += FAN_EVENT_METADATA_LEN; |
| 228 | mutex_unlock(&group->notification_mutex); |
| 229 | ret = put_user(send_len, (int __user *) p); |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 236 | static const struct file_operations fanotify_fops = { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 237 | .poll = fanotify_poll, |
| 238 | .read = fanotify_read, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 239 | .fasync = NULL, |
| 240 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 241 | .unlocked_ioctl = fanotify_ioctl, |
| 242 | .compat_ioctl = fanotify_ioctl, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 243 | }; |
| 244 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 245 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 246 | { |
| 247 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 248 | } |
| 249 | |
| 250 | static int fanotify_find_path(int dfd, const char __user *filename, |
| 251 | struct path *path, unsigned int flags) |
| 252 | { |
| 253 | int ret; |
| 254 | |
| 255 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, |
| 256 | dfd, filename, flags); |
| 257 | |
| 258 | if (filename == NULL) { |
| 259 | struct file *file; |
| 260 | int fput_needed; |
| 261 | |
| 262 | ret = -EBADF; |
| 263 | file = fget_light(dfd, &fput_needed); |
| 264 | if (!file) |
| 265 | goto out; |
| 266 | |
| 267 | ret = -ENOTDIR; |
| 268 | if ((flags & FAN_MARK_ONLYDIR) && |
| 269 | !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) { |
| 270 | fput_light(file, fput_needed); |
| 271 | goto out; |
| 272 | } |
| 273 | |
| 274 | *path = file->f_path; |
| 275 | path_get(path); |
| 276 | fput_light(file, fput_needed); |
| 277 | } else { |
| 278 | unsigned int lookup_flags = 0; |
| 279 | |
| 280 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 281 | lookup_flags |= LOOKUP_FOLLOW; |
| 282 | if (flags & FAN_MARK_ONLYDIR) |
| 283 | lookup_flags |= LOOKUP_DIRECTORY; |
| 284 | |
| 285 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 286 | if (ret) |
| 287 | goto out; |
| 288 | } |
| 289 | |
| 290 | /* you can only watch an inode if you have read permissions on it */ |
| 291 | ret = inode_permission(path->dentry->d_inode, MAY_READ); |
| 292 | if (ret) |
| 293 | path_put(path); |
| 294 | out: |
| 295 | return ret; |
| 296 | } |
| 297 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 298 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, __u32 mask) |
| 299 | { |
| 300 | __u32 oldmask; |
| 301 | |
| 302 | spin_lock(&fsn_mark->lock); |
| 303 | oldmask = fsn_mark->mask; |
| 304 | fsn_mark->mask = oldmask & ~mask; |
| 305 | spin_unlock(&fsn_mark->lock); |
| 306 | |
| 307 | if (!(oldmask & ~mask)) |
| 308 | fsnotify_destroy_mark(fsn_mark); |
| 309 | |
| 310 | return mask & oldmask; |
| 311 | } |
| 312 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 313 | static int fanotify_remove_mark(struct fsnotify_group *group, struct inode *inode, |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 314 | struct vfsmount *mnt, __u32 mask) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 315 | { |
| 316 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 317 | __u32 removed; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 318 | |
| 319 | BUG_ON(inode && mnt); |
| 320 | BUG_ON(!inode && !mnt); |
| 321 | |
| 322 | if (inode) |
| 323 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
| 324 | else if (mnt) |
| 325 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 326 | else |
| 327 | BUG(); |
| 328 | |
| 329 | if (!fsn_mark) |
| 330 | return -ENOENT; |
| 331 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 332 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 333 | /* matches the fsnotify_find_inode_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 334 | fsnotify_put_mark(fsn_mark); |
| 335 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 336 | if (removed & group->mask) |
| 337 | fsnotify_recalc_group_mask(group); |
| 338 | if (inode) { |
| 339 | if (removed & inode->i_fsnotify_mask) |
| 340 | fsnotify_recalc_inode_mask(inode); |
| 341 | } else if (mnt) { |
| 342 | if (removed & mnt->mnt_fsnotify_mask) |
| 343 | fsnotify_recalc_vfsmount_mask(mnt); |
| 344 | } |
| 345 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 349 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, __u32 mask) |
| 350 | { |
| 351 | __u32 oldmask; |
| 352 | |
| 353 | spin_lock(&fsn_mark->lock); |
| 354 | oldmask = fsn_mark->mask; |
| 355 | fsn_mark->mask = oldmask | mask; |
| 356 | spin_unlock(&fsn_mark->lock); |
| 357 | |
| 358 | return mask & ~oldmask; |
| 359 | } |
| 360 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 361 | static struct fsnotify_mark *fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 362 | struct vfsmount *mnt, __u32 mask) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 363 | { |
| 364 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 365 | __u32 added; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 366 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 367 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 368 | if (!fsn_mark) { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 369 | int ret; |
| 370 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 371 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 372 | if (!fsn_mark) |
| 373 | return ERR_PTR(-ENOMEM); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 374 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 375 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 376 | ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 377 | if (ret) { |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 378 | fanotify_free_mark(fsn_mark); |
| 379 | return ERR_PTR(ret); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 380 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 381 | } |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 382 | added = fanotify_mark_add_to_mask(fsn_mark, mask); |
| 383 | if (added) { |
| 384 | if (added & ~group->mask) |
| 385 | fsnotify_recalc_group_mask(group); |
| 386 | if (added & ~mnt->mnt_fsnotify_mask) |
| 387 | fsnotify_recalc_vfsmount_mask(mnt); |
| 388 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 389 | return fsn_mark; |
| 390 | } |
| 391 | |
| 392 | static struct fsnotify_mark *fanotify_add_inode_mark(struct fsnotify_group *group, |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 393 | struct inode *inode, __u32 mask) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 394 | { |
| 395 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 396 | __u32 added; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 397 | |
| 398 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 399 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 400 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 401 | if (!fsn_mark) { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 402 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 403 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 404 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 405 | if (!fsn_mark) |
| 406 | return ERR_PTR(-ENOMEM); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 407 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 408 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 409 | ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 410 | if (ret) { |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 411 | fanotify_free_mark(fsn_mark); |
| 412 | return ERR_PTR(ret); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 413 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 414 | } |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 415 | added = fanotify_mark_add_to_mask(fsn_mark, mask); |
| 416 | if (added) { |
| 417 | if (added & ~group->mask) |
| 418 | fsnotify_recalc_group_mask(group); |
| 419 | if (added & ~inode->i_fsnotify_mask) |
| 420 | fsnotify_recalc_inode_mask(inode); |
| 421 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 422 | return fsn_mark; |
| 423 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 424 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 425 | static int fanotify_add_mark(struct fsnotify_group *group, struct inode *inode, |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 426 | struct vfsmount *mnt, __u32 mask) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 427 | { |
| 428 | struct fsnotify_mark *fsn_mark; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 429 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 430 | pr_debug("%s: group=%p inode=%p mnt=%p mask=%x\n", |
| 431 | __func__, group, inode, mnt, mask); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 432 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 433 | BUG_ON(inode && mnt); |
| 434 | BUG_ON(!inode && !mnt); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 435 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 436 | if (inode) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 437 | fsn_mark = fanotify_add_inode_mark(group, inode, mask); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 438 | else if (mnt) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 439 | fsn_mark = fanotify_add_vfsmount_mark(group, mnt, mask); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 440 | else |
| 441 | BUG(); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 442 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 443 | if (IS_ERR(fsn_mark)) |
| 444 | goto out; |
| 445 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 446 | /* match the init or the find.... */ |
| 447 | fsnotify_put_mark(fsn_mark); |
| 448 | out: |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 449 | return PTR_ERR(fsn_mark); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 450 | } |
| 451 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 452 | static bool fanotify_mark_validate_input(int flags, |
| 453 | __u32 mask) |
| 454 | { |
| 455 | pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask); |
| 456 | |
| 457 | /* are flags valid of this operation? */ |
| 458 | if (!fanotify_mark_flags_valid(flags)) |
| 459 | return false; |
| 460 | /* is the mask valid? */ |
| 461 | if (!fanotify_mask_valid(mask)) |
| 462 | return false; |
| 463 | return true; |
| 464 | } |
| 465 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 466 | /* fanotify syscalls */ |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 467 | SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags, |
| 468 | unsigned int, priority) |
| 469 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 470 | struct fsnotify_group *group; |
| 471 | int f_flags, fd; |
| 472 | |
| 473 | pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n", |
| 474 | __func__, flags, event_f_flags, priority); |
| 475 | |
| 476 | if (event_f_flags) |
| 477 | return -EINVAL; |
| 478 | if (priority) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | if (!capable(CAP_SYS_ADMIN)) |
| 482 | return -EACCES; |
| 483 | |
| 484 | if (flags & ~FAN_ALL_INIT_FLAGS) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | f_flags = (O_RDONLY | FMODE_NONOTIFY); |
| 488 | if (flags & FAN_CLOEXEC) |
| 489 | f_flags |= O_CLOEXEC; |
| 490 | if (flags & FAN_NONBLOCK) |
| 491 | f_flags |= O_NONBLOCK; |
| 492 | |
| 493 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 494 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
| 495 | if (IS_ERR(group)) |
| 496 | return PTR_ERR(group); |
| 497 | |
| 498 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 499 | if (fd < 0) |
| 500 | goto out_put_group; |
| 501 | |
| 502 | return fd; |
| 503 | |
| 504 | out_put_group: |
| 505 | fsnotify_put_group(group); |
| 506 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 507 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 508 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 509 | SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags, |
| 510 | __u64 mask, int dfd, |
| 511 | const char __user * pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 512 | { |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 513 | struct inode *inode; |
| 514 | struct fsnotify_group *group; |
| 515 | struct file *filp; |
| 516 | struct path path; |
| 517 | int ret, fput_needed; |
| 518 | |
| 519 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 520 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 521 | |
| 522 | /* we only use the lower 32 bits as of right now. */ |
| 523 | if (mask & ((__u64)0xffffffff << 32)) |
| 524 | return -EINVAL; |
| 525 | |
| 526 | if (!fanotify_mark_validate_input(flags, mask)) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | filp = fget_light(fanotify_fd, &fput_needed); |
| 530 | if (unlikely(!filp)) |
| 531 | return -EBADF; |
| 532 | |
| 533 | /* verify that this is indeed an fanotify instance */ |
| 534 | ret = -EINVAL; |
| 535 | if (unlikely(filp->f_op != &fanotify_fops)) |
| 536 | goto fput_and_out; |
| 537 | |
| 538 | ret = fanotify_find_path(dfd, pathname, &path, flags); |
| 539 | if (ret) |
| 540 | goto fput_and_out; |
| 541 | |
| 542 | /* inode held in place by reference to path; group by fget on fd */ |
| 543 | inode = path.dentry->d_inode; |
| 544 | group = filp->private_data; |
| 545 | |
| 546 | /* create/update an inode mark */ |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 547 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { |
| 548 | case FAN_MARK_ADD: |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 549 | ret = fanotify_add_mark(group, inode, NULL, mask); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 550 | break; |
| 551 | case FAN_MARK_REMOVE: |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 552 | ret = fanotify_remove_mark(group, inode, NULL, mask); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 553 | break; |
| 554 | default: |
| 555 | ret = -EINVAL; |
| 556 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 557 | |
| 558 | path_put(&path); |
| 559 | fput_and_out: |
| 560 | fput_light(filp, fput_needed); |
| 561 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 562 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 563 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 564 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 565 | asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask, |
| 566 | long dfd, long pathname) |
| 567 | { |
| 568 | return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags, |
| 569 | mask, (int) dfd, |
| 570 | (const char __user *) pathname); |
| 571 | } |
| 572 | SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark); |
| 573 | #endif |
| 574 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 575 | /* |
| 576 | * fanotify_user_setup - Our initialization function. Note that we cannnot return |
| 577 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 578 | * must result in panic(). |
| 579 | */ |
| 580 | static int __init fanotify_user_setup(void) |
| 581 | { |
| 582 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC); |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | device_initcall(fanotify_user_setup); |