Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1 | #include <linux/fanotify.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 2 | #include <linux/fcntl.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 3 | #include <linux/file.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 4 | #include <linux/fs.h> |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 5 | #include <linux/anon_inodes.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 6 | #include <linux/fsnotify_backend.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 7 | #include <linux/init.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 8 | #include <linux/mount.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 9 | #include <linux/namei.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 10 | #include <linux/poll.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 11 | #include <linux/security.h> |
| 12 | #include <linux/syscalls.h> |
Tejun Heo | e4e047a | 2010-05-20 01:36:28 +1000 | [diff] [blame] | 13 | #include <linux/slab.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 14 | #include <linux/types.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
| 16 | |
| 17 | #include <asm/ioctls.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 18 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 19 | #include "../../mount.h" |
| 20 | |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 21 | #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 22 | #define FANOTIFY_DEFAULT_MAX_MARKS 8192 |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 23 | #define FANOTIFY_DEFAULT_MAX_LISTENERS 128 |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 24 | |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 25 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 26 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 27 | static struct kmem_cache *fanotify_mark_cache __read_mostly; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 28 | static struct kmem_cache *fanotify_response_event_cache __read_mostly; |
| 29 | |
| 30 | struct fanotify_response_event { |
| 31 | struct list_head list; |
| 32 | __s32 fd; |
| 33 | struct fsnotify_event *event; |
| 34 | }; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 35 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 36 | /* |
| 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 | */ |
| 43 | static 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 Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 61 | static int create_fd(struct fsnotify_group *group, |
| 62 | struct fsnotify_event *event, |
| 63 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 64 | { |
| 65 | int client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 66 | struct file *new_file; |
| 67 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 68 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 69 | |
| 70 | client_fd = get_unused_fd(); |
| 71 | if (client_fd < 0) |
| 72 | return client_fd; |
| 73 | |
Linus Torvalds | 2069601 | 2010-08-12 14:23:04 -0700 | [diff] [blame] | 74 | if (event->data_type != FSNOTIFY_EVENT_PATH) { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 75 | 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 Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 84 | /* 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 Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 86 | if (event->path.dentry && event->path.mnt) |
| 87 | new_file = dentry_open(&event->path, |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 88 | group->fanotify_data.f_flags | FMODE_NONOTIFY, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 89 | 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 Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 103 | *file = new_file; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 106 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 109 | static int fill_event_metadata(struct fsnotify_group *group, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 110 | struct fanotify_event_metadata *metadata, |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 111 | struct fsnotify_event *event, |
| 112 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 113 | { |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 114 | int ret = 0; |
| 115 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 116 | pr_debug("%s: group=%p metadata=%p event=%p\n", __func__, |
| 117 | group, metadata, event); |
| 118 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 119 | *file = NULL; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 120 | metadata->event_len = FAN_EVENT_METADATA_LEN; |
Eric Paris | 7d13162 | 2010-12-07 15:27:57 -0500 | [diff] [blame] | 121 | metadata->metadata_len = FAN_EVENT_METADATA_LEN; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 122 | metadata->vers = FANOTIFY_METADATA_VERSION; |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 123 | metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS; |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 124 | metadata->pid = pid_vnr(event->tgid); |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 125 | if (unlikely(event->mask & FAN_Q_OVERFLOW)) |
| 126 | metadata->fd = FAN_NOFD; |
| 127 | else { |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 128 | metadata->fd = create_fd(group, event, file); |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 129 | if (metadata->fd < 0) |
| 130 | ret = metadata->fd; |
| 131 | } |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 132 | |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 133 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 136 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 137 | static 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 | |
| 158 | static 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 Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 169 | * userspace can send a valid response or we will clean it up after the |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 170 | * 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 | |
| 196 | static 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 Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 213 | |
Lino Sanfilippo | 09e5f14 | 2010-11-19 10:58:07 +0100 | [diff] [blame] | 214 | if (atomic_read(&group->fanotify_data.bypass_perm)) { |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 215 | 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 Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 221 | 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 Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 227 | #else |
| 228 | static int prepare_for_access_response(struct fsnotify_group *group, |
| 229 | struct fsnotify_event *event, |
| 230 | __s32 fd) |
| 231 | { |
| 232 | return 0; |
| 233 | } |
| 234 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 235 | #endif |
| 236 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 237 | static 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 Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 242 | struct file *f; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 243 | int fd, ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 244 | |
| 245 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 246 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 247 | ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f); |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 248 | if (ret < 0) |
| 249 | goto out; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 250 | |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 251 | fd = fanotify_event_metadata.fd; |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 252 | ret = -EFAULT; |
| 253 | if (copy_to_user(buf, &fanotify_event_metadata, |
| 254 | fanotify_event_metadata.event_len)) |
| 255 | goto out_close_fd; |
| 256 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 257 | ret = prepare_for_access_response(group, event, fd); |
| 258 | if (ret) |
| 259 | goto out_close_fd; |
| 260 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 261 | fd_install(fd, f); |
Eric Paris | 7d13162 | 2010-12-07 15:27:57 -0500 | [diff] [blame] | 262 | return fanotify_event_metadata.event_len; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 263 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 264 | out_close_fd: |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 265 | if (fd != FAN_NOFD) { |
| 266 | put_unused_fd(fd); |
| 267 | fput(f); |
| 268 | } |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 269 | out: |
| 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 Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 276 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* intofiy userspace file descriptor functions */ |
| 280 | static 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 | |
| 294 | static 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 Sanfilippo | 1a5cea7 | 2010-10-29 12:06:42 +0200 | [diff] [blame] | 331 | ret = -ERESTARTSYS; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 332 | 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 Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 347 | static 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 Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 374 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 375 | { |
| 376 | struct fsnotify_group *group = file->private_data; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 377 | |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 378 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Andrew Morton | 19ba54f | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 379 | struct fanotify_response_event *re, *lre; |
| 380 | |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 381 | mutex_lock(&group->fanotify_data.access_mutex); |
| 382 | |
Lino Sanfilippo | 09e5f14 | 2010-11-19 10:58:07 +0100 | [diff] [blame] | 383 | atomic_inc(&group->fanotify_data.bypass_perm); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 384 | |
| 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 Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 398 | /* matches the fanotify_init->fsnotify_alloc_group */ |
| 399 | fsnotify_put_group(group); |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 404 | static 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 Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 429 | static const struct file_operations fanotify_fops = { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 430 | .poll = fanotify_poll, |
| 431 | .read = fanotify_read, |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 432 | .write = fanotify_write, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 433 | .fasync = NULL, |
| 434 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 435 | .unlocked_ioctl = fanotify_ioctl, |
| 436 | .compat_ioctl = fanotify_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 437 | .llseek = noop_llseek, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 438 | }; |
| 439 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 440 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 441 | { |
| 442 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 443 | } |
| 444 | |
| 445 | static 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) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 454 | struct fd f = fdget(dfd); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 455 | |
| 456 | ret = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 457 | if (!f.file) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 458 | goto out; |
| 459 | |
| 460 | ret = -ENOTDIR; |
| 461 | if ((flags & FAN_MARK_ONLYDIR) && |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 462 | !(S_ISDIR(f.file->f_path.dentry->d_inode->i_mode))) { |
| 463 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 464 | goto out; |
| 465 | } |
| 466 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 467 | *path = f.file->f_path; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 468 | path_get(path); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 469 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 470 | } else { |
| 471 | unsigned int lookup_flags = 0; |
| 472 | |
| 473 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 474 | lookup_flags |= LOOKUP_FOLLOW; |
| 475 | if (flags & FAN_MARK_ONLYDIR) |
| 476 | lookup_flags |= LOOKUP_DIRECTORY; |
| 477 | |
| 478 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 479 | if (ret) |
| 480 | goto out; |
| 481 | } |
| 482 | |
| 483 | /* you can only watch an inode if you have read permissions on it */ |
| 484 | ret = inode_permission(path->dentry->d_inode, MAY_READ); |
| 485 | if (ret) |
| 486 | path_put(path); |
| 487 | out: |
| 488 | return ret; |
| 489 | } |
| 490 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 491 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
| 492 | __u32 mask, |
| 493 | unsigned int flags) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 494 | { |
| 495 | __u32 oldmask; |
| 496 | |
| 497 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 498 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 499 | oldmask = fsn_mark->mask; |
| 500 | fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask)); |
| 501 | } else { |
| 502 | oldmask = fsn_mark->ignored_mask; |
| 503 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask)); |
| 504 | } |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 505 | spin_unlock(&fsn_mark->lock); |
| 506 | |
| 507 | if (!(oldmask & ~mask)) |
| 508 | fsnotify_destroy_mark(fsn_mark); |
| 509 | |
| 510 | return mask & oldmask; |
| 511 | } |
| 512 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 513 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 514 | struct vfsmount *mnt, __u32 mask, |
| 515 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 516 | { |
| 517 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 518 | __u32 removed; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 519 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 520 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 521 | if (!fsn_mark) |
| 522 | return -ENOENT; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 523 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 524 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 525 | fsnotify_put_mark(fsn_mark); |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 526 | if (removed & real_mount(mnt)->mnt_fsnotify_mask) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 527 | fsnotify_recalc_vfsmount_mask(mnt); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 528 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 533 | struct inode *inode, __u32 mask, |
| 534 | unsigned int flags) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 535 | { |
| 536 | struct fsnotify_mark *fsn_mark = NULL; |
| 537 | __u32 removed; |
| 538 | |
| 539 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 540 | if (!fsn_mark) |
| 541 | return -ENOENT; |
| 542 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 543 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 544 | /* matches the fsnotify_find_inode_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 545 | fsnotify_put_mark(fsn_mark); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 546 | if (removed & inode->i_fsnotify_mask) |
| 547 | fsnotify_recalc_inode_mask(inode); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 548 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 552 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 553 | __u32 mask, |
| 554 | unsigned int flags) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 555 | { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 556 | __u32 oldmask = -1; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 557 | |
| 558 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 559 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 560 | oldmask = fsn_mark->mask; |
| 561 | fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask)); |
| 562 | } else { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 563 | __u32 tmask = fsn_mark->ignored_mask | mask; |
| 564 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask); |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 565 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 566 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 567 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 568 | |
| 569 | if (!(flags & FAN_MARK_ONDIR)) { |
| 570 | __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR; |
| 571 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask); |
| 572 | } |
| 573 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 574 | spin_unlock(&fsn_mark->lock); |
| 575 | |
| 576 | return mask & ~oldmask; |
| 577 | } |
| 578 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 579 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 580 | struct vfsmount *mnt, __u32 mask, |
| 581 | unsigned int flags) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 582 | { |
| 583 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 584 | __u32 added; |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 585 | int ret = 0; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 586 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 587 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 588 | if (!fsn_mark) { |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 589 | if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks) |
| 590 | return -ENOSPC; |
| 591 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 592 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 593 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 594 | return -ENOMEM; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 595 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 596 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 597 | ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 598 | if (ret) |
| 599 | goto err; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 600 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 601 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 602 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 603 | if (added & ~real_mount(mnt)->mnt_fsnotify_mask) |
Eric Paris | 43709a2 | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 604 | fsnotify_recalc_vfsmount_mask(mnt); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 605 | err: |
| 606 | fsnotify_put_mark(fsn_mark); |
| 607 | return ret; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 608 | } |
| 609 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 610 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 611 | struct inode *inode, __u32 mask, |
| 612 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 613 | { |
| 614 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 615 | __u32 added; |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 616 | int ret = 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 617 | |
| 618 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 619 | |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 620 | /* |
| 621 | * If some other task has this inode open for write we should not add |
| 622 | * an ignored mark, unless that ignored mark is supposed to survive |
| 623 | * modification changes anyway. |
| 624 | */ |
| 625 | if ((flags & FAN_MARK_IGNORED_MASK) && |
| 626 | !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && |
| 627 | (atomic_read(&inode->i_writecount) > 0)) |
| 628 | return 0; |
| 629 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 630 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 631 | if (!fsn_mark) { |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 632 | if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks) |
| 633 | return -ENOSPC; |
| 634 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 635 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 636 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 637 | return -ENOMEM; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 638 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 639 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 640 | ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 641 | if (ret) |
| 642 | goto err; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 643 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 644 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 645 | |
Eric Paris | 43709a2 | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 646 | if (added & ~inode->i_fsnotify_mask) |
| 647 | fsnotify_recalc_inode_mask(inode); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 648 | err: |
| 649 | fsnotify_put_mark(fsn_mark); |
| 650 | return ret; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 651 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 652 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 653 | /* fanotify syscalls */ |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 654 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 655 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 656 | struct fsnotify_group *group; |
| 657 | int f_flags, fd; |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 658 | struct user_struct *user; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 659 | |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 660 | pr_debug("%s: flags=%d event_f_flags=%d\n", |
| 661 | __func__, flags, event_f_flags); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 662 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 663 | if (!capable(CAP_SYS_ADMIN)) |
Andreas Gruenbacher | a2f13ad | 2010-08-24 12:58:54 +0200 | [diff] [blame] | 664 | return -EPERM; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 665 | |
| 666 | if (flags & ~FAN_ALL_INIT_FLAGS) |
| 667 | return -EINVAL; |
| 668 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 669 | user = get_current_user(); |
| 670 | if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) { |
| 671 | free_uid(user); |
| 672 | return -EMFILE; |
| 673 | } |
| 674 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 675 | f_flags = O_RDWR | FMODE_NONOTIFY; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 676 | if (flags & FAN_CLOEXEC) |
| 677 | f_flags |= O_CLOEXEC; |
| 678 | if (flags & FAN_NONBLOCK) |
| 679 | f_flags |= O_NONBLOCK; |
| 680 | |
| 681 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 682 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 683 | if (IS_ERR(group)) { |
| 684 | free_uid(user); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 685 | return PTR_ERR(group); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 686 | } |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 687 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 688 | group->fanotify_data.user = user; |
| 689 | atomic_inc(&user->fanotify_listeners); |
| 690 | |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 691 | group->fanotify_data.f_flags = event_f_flags; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 692 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 693 | mutex_init(&group->fanotify_data.access_mutex); |
| 694 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
| 695 | INIT_LIST_HEAD(&group->fanotify_data.access_list); |
Lino Sanfilippo | 09e5f14 | 2010-11-19 10:58:07 +0100 | [diff] [blame] | 696 | atomic_set(&group->fanotify_data.bypass_perm, 0); |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 697 | #endif |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 698 | switch (flags & FAN_ALL_CLASS_BITS) { |
| 699 | case FAN_CLASS_NOTIF: |
| 700 | group->priority = FS_PRIO_0; |
| 701 | break; |
| 702 | case FAN_CLASS_CONTENT: |
| 703 | group->priority = FS_PRIO_1; |
| 704 | break; |
| 705 | case FAN_CLASS_PRE_CONTENT: |
| 706 | group->priority = FS_PRIO_2; |
| 707 | break; |
| 708 | default: |
| 709 | fd = -EINVAL; |
| 710 | goto out_put_group; |
| 711 | } |
Eric Paris | cb2d429 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 712 | |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 713 | if (flags & FAN_UNLIMITED_QUEUE) { |
| 714 | fd = -EPERM; |
| 715 | if (!capable(CAP_SYS_ADMIN)) |
| 716 | goto out_put_group; |
| 717 | group->max_events = UINT_MAX; |
| 718 | } else { |
| 719 | group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS; |
| 720 | } |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 721 | |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 722 | if (flags & FAN_UNLIMITED_MARKS) { |
| 723 | fd = -EPERM; |
| 724 | if (!capable(CAP_SYS_ADMIN)) |
| 725 | goto out_put_group; |
| 726 | group->fanotify_data.max_marks = UINT_MAX; |
| 727 | } else { |
| 728 | group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS; |
| 729 | } |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 730 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 731 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 732 | if (fd < 0) |
| 733 | goto out_put_group; |
| 734 | |
| 735 | return fd; |
| 736 | |
| 737 | out_put_group: |
| 738 | fsnotify_put_group(group); |
| 739 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 740 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 741 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 742 | SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags, |
| 743 | __u64 mask, int dfd, |
| 744 | const char __user * pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 745 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 746 | struct inode *inode = NULL; |
| 747 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 748 | struct fsnotify_group *group; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 749 | struct fd f; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 750 | struct path path; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 751 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 752 | |
| 753 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 754 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 755 | |
| 756 | /* we only use the lower 32 bits as of right now. */ |
| 757 | if (mask & ((__u64)0xffffffff << 32)) |
| 758 | return -EINVAL; |
| 759 | |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 760 | if (flags & ~FAN_ALL_MARK_FLAGS) |
| 761 | return -EINVAL; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 762 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 763 | case FAN_MARK_ADD: /* fallthrough */ |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 764 | case FAN_MARK_REMOVE: |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 765 | if (!mask) |
| 766 | return -EINVAL; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 767 | case FAN_MARK_FLUSH: |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 768 | break; |
| 769 | default: |
| 770 | return -EINVAL; |
| 771 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 772 | |
| 773 | if (mask & FAN_ONDIR) { |
| 774 | flags |= FAN_MARK_ONDIR; |
| 775 | mask &= ~FAN_ONDIR; |
| 776 | } |
| 777 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 778 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 779 | if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD)) |
| 780 | #else |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 781 | if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD)) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 782 | #endif |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 783 | return -EINVAL; |
| 784 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 785 | f = fdget(fanotify_fd); |
| 786 | if (unlikely(!f.file)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 787 | return -EBADF; |
| 788 | |
| 789 | /* verify that this is indeed an fanotify instance */ |
| 790 | ret = -EINVAL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 791 | if (unlikely(f.file->f_op != &fanotify_fops)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 792 | goto fput_and_out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 793 | group = f.file->private_data; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 794 | |
| 795 | /* |
| 796 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not |
| 797 | * allowed to set permissions events. |
| 798 | */ |
| 799 | ret = -EINVAL; |
| 800 | if (mask & FAN_ALL_PERM_EVENTS && |
| 801 | group->priority == FS_PRIO_0) |
| 802 | goto fput_and_out; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 803 | |
| 804 | ret = fanotify_find_path(dfd, pathname, &path, flags); |
| 805 | if (ret) |
| 806 | goto fput_and_out; |
| 807 | |
| 808 | /* inode held in place by reference to path; group by fget on fd */ |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 809 | if (!(flags & FAN_MARK_MOUNT)) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 810 | inode = path.dentry->d_inode; |
| 811 | else |
| 812 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 813 | |
| 814 | /* create/update an inode mark */ |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 815 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 816 | case FAN_MARK_ADD: |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 817 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 818 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 819 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 820 | ret = fanotify_add_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 821 | break; |
| 822 | case FAN_MARK_REMOVE: |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 823 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 824 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 825 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 826 | ret = fanotify_remove_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 827 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 828 | case FAN_MARK_FLUSH: |
| 829 | if (flags & FAN_MARK_MOUNT) |
| 830 | fsnotify_clear_vfsmount_marks_by_group(group); |
| 831 | else |
| 832 | fsnotify_clear_inode_marks_by_group(group); |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 833 | break; |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 834 | default: |
| 835 | ret = -EINVAL; |
| 836 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 837 | |
| 838 | path_put(&path); |
| 839 | fput_and_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 840 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 841 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 842 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 843 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 844 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 845 | asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask, |
| 846 | long dfd, long pathname) |
| 847 | { |
| 848 | return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags, |
| 849 | mask, (int) dfd, |
| 850 | (const char __user *) pathname); |
| 851 | } |
| 852 | SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark); |
| 853 | #endif |
| 854 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 855 | /* |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 856 | * fanotify_user_setup - Our initialization function. Note that we cannot return |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 857 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 858 | * must result in panic(). |
| 859 | */ |
| 860 | static int __init fanotify_user_setup(void) |
| 861 | { |
| 862 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 863 | fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event, |
| 864 | SLAB_PANIC); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | device_initcall(fanotify_user_setup); |