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> |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 16 | #include <linux/compat.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 17 | |
| 18 | #include <asm/ioctls.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 19 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 20 | #include "../../mount.h" |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 21 | #include "../fdinfo.h" |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 22 | #include "fanotify.h" |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 23 | |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 24 | #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 25 | #define FANOTIFY_DEFAULT_MAX_MARKS 8192 |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 26 | #define FANOTIFY_DEFAULT_MAX_LISTENERS 128 |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 27 | |
Heinrich Schuchardt | 48149e9 | 2014-06-04 16:05:44 -0700 | [diff] [blame] | 28 | /* |
| 29 | * All flags that may be specified in parameter event_f_flags of fanotify_init. |
| 30 | * |
| 31 | * Internal and external open flags are stored together in field f_flags of |
| 32 | * struct file. Only external open flags shall be allowed in event_f_flags. |
| 33 | * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be |
| 34 | * excluded. |
| 35 | */ |
| 36 | #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ |
| 37 | O_ACCMODE | O_APPEND | O_NONBLOCK | \ |
| 38 | __O_SYNC | O_DSYNC | O_CLOEXEC | \ |
| 39 | O_LARGEFILE | O_NOATIME ) |
| 40 | |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 41 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 42 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 43 | static struct kmem_cache *fanotify_mark_cache __read_mostly; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 44 | struct kmem_cache *fanotify_event_cachep __read_mostly; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 45 | struct kmem_cache *fanotify_perm_event_cachep __read_mostly; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 46 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 47 | /* |
| 48 | * Get an fsnotify notification event if one exists and is small |
| 49 | * enough to fit in "count". Return an error pointer if the count |
| 50 | * is not large enough. |
| 51 | * |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 52 | * Called with the group->notification_lock held. |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 53 | */ |
| 54 | static struct fsnotify_event *get_one_event(struct fsnotify_group *group, |
| 55 | size_t count) |
| 56 | { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 57 | BUG_ON(IS_ENABLED(CONFIG_SMP) && |
| 58 | !spin_is_locked(&group->notification_lock)); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 59 | |
| 60 | pr_debug("%s: group=%p count=%zd\n", __func__, group, count); |
| 61 | |
| 62 | if (fsnotify_notify_queue_is_empty(group)) |
| 63 | return NULL; |
| 64 | |
| 65 | if (FAN_EVENT_METADATA_LEN > count) |
| 66 | return ERR_PTR(-EINVAL); |
| 67 | |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 68 | /* held the notification_lock the whole time, so this is the |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 69 | * same event we peeked above */ |
Jan Kara | 8ba8fa91 | 2014-08-06 16:03:26 -0700 | [diff] [blame] | 70 | return fsnotify_remove_first_event(group); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 73 | static int create_fd(struct fsnotify_group *group, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 74 | struct fanotify_event_info *event, |
| 75 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 76 | { |
| 77 | int client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 78 | struct file *new_file; |
| 79 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 80 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 81 | |
Yann Droneaud | 0b37e09 | 2014-10-09 15:24:40 -0700 | [diff] [blame] | 82 | client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 83 | if (client_fd < 0) |
| 84 | return client_fd; |
| 85 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 86 | /* |
| 87 | * we need a new file handle for the userspace program so it can read even if it was |
| 88 | * originally opened O_WRONLY. |
| 89 | */ |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 90 | /* it's possible this event was an overflow event. in that case dentry and mnt |
| 91 | * are NULL; That's fine, just don't call dentry open */ |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 92 | if (event->path.dentry && event->path.mnt) |
| 93 | new_file = dentry_open(&event->path, |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 94 | group->fanotify_data.f_flags | FMODE_NONOTIFY, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 95 | current_cred()); |
| 96 | else |
| 97 | new_file = ERR_PTR(-EOVERFLOW); |
| 98 | if (IS_ERR(new_file)) { |
| 99 | /* |
| 100 | * we still send an event even if we can't open the file. this |
| 101 | * can happen when say tasks are gone and we try to open their |
| 102 | * /proc files or we try to open a WRONLY file like in sysfs |
| 103 | * we just send the errno to userspace since there isn't much |
| 104 | * else we can do. |
| 105 | */ |
| 106 | put_unused_fd(client_fd); |
| 107 | client_fd = PTR_ERR(new_file); |
| 108 | } else { |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 109 | *file = new_file; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 112 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 115 | static int fill_event_metadata(struct fsnotify_group *group, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 116 | struct fanotify_event_metadata *metadata, |
| 117 | struct fsnotify_event *fsn_event, |
| 118 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 119 | { |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 120 | int ret = 0; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 121 | struct fanotify_event_info *event; |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 122 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 123 | pr_debug("%s: group=%p metadata=%p event=%p\n", __func__, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 124 | group, metadata, fsn_event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 125 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 126 | *file = NULL; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 127 | event = container_of(fsn_event, struct fanotify_event_info, fse); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 128 | metadata->event_len = FAN_EVENT_METADATA_LEN; |
Eric Paris | 7d13162 | 2010-12-07 15:27:57 -0500 | [diff] [blame] | 129 | metadata->metadata_len = FAN_EVENT_METADATA_LEN; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 130 | metadata->vers = FANOTIFY_METADATA_VERSION; |
Dan Carpenter | de1e0c4 | 2013-07-08 15:59:40 -0700 | [diff] [blame] | 131 | metadata->reserved = 0; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 132 | metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS; |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 133 | metadata->pid = pid_vnr(event->tgid); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 134 | if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW)) |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 135 | metadata->fd = FAN_NOFD; |
| 136 | else { |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 137 | metadata->fd = create_fd(group, event, file); |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 138 | if (metadata->fd < 0) |
| 139 | ret = metadata->fd; |
| 140 | } |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 141 | |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 142 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 145 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 146 | static struct fanotify_perm_event_info *dequeue_event( |
| 147 | struct fsnotify_group *group, int fd) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 148 | { |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 149 | struct fanotify_perm_event_info *event, *return_e = NULL; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 150 | |
Jan Kara | 9573f79 | 2014-04-03 14:46:34 -0700 | [diff] [blame] | 151 | spin_lock(&group->fanotify_data.access_lock); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 152 | list_for_each_entry(event, &group->fanotify_data.access_list, |
| 153 | fae.fse.list) { |
| 154 | if (event->fd != fd) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 155 | continue; |
| 156 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 157 | list_del_init(&event->fae.fse.list); |
| 158 | return_e = event; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 159 | break; |
| 160 | } |
Jan Kara | 9573f79 | 2014-04-03 14:46:34 -0700 | [diff] [blame] | 161 | spin_unlock(&group->fanotify_data.access_lock); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 162 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 163 | pr_debug("%s: found return_re=%p\n", __func__, return_e); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 164 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 165 | return return_e; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static int process_access_response(struct fsnotify_group *group, |
| 169 | struct fanotify_response *response_struct) |
| 170 | { |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 171 | struct fanotify_perm_event_info *event; |
| 172 | int fd = response_struct->fd; |
| 173 | int response = response_struct->response; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 174 | |
| 175 | pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group, |
| 176 | fd, response); |
| 177 | /* |
| 178 | * 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] | 179 | * 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] | 180 | * timeout |
| 181 | */ |
| 182 | switch (response) { |
| 183 | case FAN_ALLOW: |
| 184 | case FAN_DENY: |
| 185 | break; |
| 186 | default: |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
| 190 | if (fd < 0) |
| 191 | return -EINVAL; |
| 192 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 193 | event = dequeue_event(group, fd); |
| 194 | if (!event) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 195 | return -ENOENT; |
| 196 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 197 | event->response = response; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 198 | wake_up(&group->fanotify_data.access_waitq); |
| 199 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 200 | return 0; |
| 201 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 202 | #endif |
| 203 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 204 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
| 205 | struct fsnotify_event *event, |
| 206 | char __user *buf) |
| 207 | { |
| 208 | struct fanotify_event_metadata fanotify_event_metadata; |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 209 | struct file *f; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 210 | int fd, ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 211 | |
| 212 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 213 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 214 | ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f); |
Eric Paris | ecf6f5e | 2010-11-08 18:08:14 -0500 | [diff] [blame] | 215 | if (ret < 0) |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 216 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 217 | |
Lino Sanfilippo | fdbf3ce | 2010-11-24 18:26:04 +0100 | [diff] [blame] | 218 | fd = fanotify_event_metadata.fd; |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 219 | ret = -EFAULT; |
| 220 | if (copy_to_user(buf, &fanotify_event_metadata, |
| 221 | fanotify_event_metadata.event_len)) |
| 222 | goto out_close_fd; |
| 223 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 224 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 225 | if (event->mask & FAN_ALL_PERM_EVENTS) |
| 226 | FANOTIFY_PE(event)->fd = fd; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 227 | #endif |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 228 | |
Al Viro | 3587b1b | 2012-11-18 19:19:00 +0000 | [diff] [blame] | 229 | if (fd != FAN_NOFD) |
| 230 | fd_install(fd, f); |
Eric Paris | 7d13162 | 2010-12-07 15:27:57 -0500 | [diff] [blame] | 231 | return fanotify_event_metadata.event_len; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 232 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 233 | out_close_fd: |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 234 | if (fd != FAN_NOFD) { |
| 235 | put_unused_fd(fd); |
| 236 | fput(f); |
| 237 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 238 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* intofiy userspace file descriptor functions */ |
| 242 | static unsigned int fanotify_poll(struct file *file, poll_table *wait) |
| 243 | { |
| 244 | struct fsnotify_group *group = file->private_data; |
| 245 | int ret = 0; |
| 246 | |
| 247 | poll_wait(file, &group->notification_waitq, wait); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 248 | spin_lock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 249 | if (!fsnotify_notify_queue_is_empty(group)) |
| 250 | ret = POLLIN | POLLRDNORM; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 251 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static ssize_t fanotify_read(struct file *file, char __user *buf, |
| 257 | size_t count, loff_t *pos) |
| 258 | { |
| 259 | struct fsnotify_group *group; |
| 260 | struct fsnotify_event *kevent; |
| 261 | char __user *start; |
| 262 | int ret; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 263 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 264 | |
| 265 | start = buf; |
| 266 | group = file->private_data; |
| 267 | |
| 268 | pr_debug("%s: group=%p\n", __func__, group); |
| 269 | |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 270 | add_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 271 | while (1) { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 272 | spin_lock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 273 | kevent = get_one_event(group, count); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 274 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 275 | |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 276 | if (IS_ERR(kevent)) { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 277 | ret = PTR_ERR(kevent); |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | |
| 281 | if (!kevent) { |
| 282 | ret = -EAGAIN; |
| 283 | if (file->f_flags & O_NONBLOCK) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 284 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 285 | |
| 286 | ret = -ERESTARTSYS; |
| 287 | if (signal_pending(current)) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 288 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 289 | |
| 290 | if (start != buf) |
| 291 | break; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 292 | |
| 293 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 294 | continue; |
| 295 | } |
| 296 | |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 297 | ret = copy_event_to_user(group, kevent, buf); |
| 298 | /* |
| 299 | * Permission events get queued to wait for response. Other |
| 300 | * events can be destroyed now. |
| 301 | */ |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 302 | if (!(kevent->mask & FAN_ALL_PERM_EVENTS)) { |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 303 | fsnotify_destroy_event(group, kevent); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 304 | if (ret < 0) |
| 305 | break; |
| 306 | } else { |
| 307 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 308 | if (ret < 0) { |
| 309 | FANOTIFY_PE(kevent)->response = FAN_DENY; |
| 310 | wake_up(&group->fanotify_data.access_waitq); |
| 311 | break; |
| 312 | } |
| 313 | spin_lock(&group->fanotify_data.access_lock); |
| 314 | list_add_tail(&kevent->list, |
| 315 | &group->fanotify_data.access_list); |
| 316 | spin_unlock(&group->fanotify_data.access_lock); |
| 317 | #endif |
| 318 | } |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 319 | buf += ret; |
| 320 | count -= ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 321 | } |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 322 | remove_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 323 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 324 | if (start != buf && ret != -EFAULT) |
| 325 | ret = buf - start; |
| 326 | return ret; |
| 327 | } |
| 328 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 329 | static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 330 | { |
| 331 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 332 | struct fanotify_response response = { .fd = -1, .response = -1 }; |
| 333 | struct fsnotify_group *group; |
| 334 | int ret; |
| 335 | |
| 336 | group = file->private_data; |
| 337 | |
| 338 | if (count > sizeof(response)) |
| 339 | count = sizeof(response); |
| 340 | |
| 341 | pr_debug("%s: group=%p count=%zu\n", __func__, group, count); |
| 342 | |
| 343 | if (copy_from_user(&response, buf, count)) |
| 344 | return -EFAULT; |
| 345 | |
| 346 | ret = process_access_response(group, &response); |
| 347 | if (ret < 0) |
| 348 | count = ret; |
| 349 | |
| 350 | return count; |
| 351 | #else |
| 352 | return -EINVAL; |
| 353 | #endif |
| 354 | } |
| 355 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 356 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 357 | { |
| 358 | struct fsnotify_group *group = file->private_data; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 359 | |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 360 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 361 | struct fanotify_perm_event_info *event, *next; |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 362 | struct fsnotify_event *fsn_event; |
Andrew Morton | 19ba54f | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 363 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 364 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 365 | * Stop new events from arriving in the notification queue. since |
| 366 | * userspace cannot use fanotify fd anymore, no event can enter or |
| 367 | * leave access_list by now either. |
| 368 | */ |
| 369 | fsnotify_group_stop_queueing(group); |
| 370 | |
| 371 | /* |
| 372 | * Process all permission events on access_list and notification queue |
| 373 | * and simulate reply from userspace. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 374 | */ |
Jan Kara | 9573f79 | 2014-04-03 14:46:34 -0700 | [diff] [blame] | 375 | spin_lock(&group->fanotify_data.access_lock); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 376 | list_for_each_entry_safe(event, next, &group->fanotify_data.access_list, |
| 377 | fae.fse.list) { |
| 378 | pr_debug("%s: found group=%p event=%p\n", __func__, group, |
| 379 | event); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 380 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 381 | list_del_init(&event->fae.fse.list); |
| 382 | event->response = FAN_ALLOW; |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 383 | } |
Jan Kara | 9573f79 | 2014-04-03 14:46:34 -0700 | [diff] [blame] | 384 | spin_unlock(&group->fanotify_data.access_lock); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 385 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 386 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 387 | * Destroy all non-permission events. For permission events just |
| 388 | * dequeue them and set the response. They will be freed once the |
| 389 | * response is consumed and fanotify_get_response() returns. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 390 | */ |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 391 | spin_lock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 392 | while (!fsnotify_notify_queue_is_empty(group)) { |
| 393 | fsn_event = fsnotify_remove_first_event(group); |
Jan Kara | 1404ff3 | 2016-10-07 16:56:49 -0700 | [diff] [blame] | 394 | if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 395 | spin_unlock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 396 | fsnotify_destroy_event(group, fsn_event); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 397 | spin_lock(&group->notification_lock); |
Jan Kara | 1404ff3 | 2016-10-07 16:56:49 -0700 | [diff] [blame] | 398 | } else |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 399 | FANOTIFY_PE(fsn_event)->response = FAN_ALLOW; |
| 400 | } |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 401 | spin_unlock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 402 | |
| 403 | /* Response for all permission events it set, wakeup waiters */ |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 404 | wake_up(&group->fanotify_data.access_waitq); |
| 405 | #endif |
Eric Paris | 0a6b6bd | 2011-10-14 17:43:39 -0400 | [diff] [blame] | 406 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 407 | /* matches the fanotify_init->fsnotify_alloc_group */ |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 408 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 413 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 414 | { |
| 415 | struct fsnotify_group *group; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 416 | struct fsnotify_event *fsn_event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 417 | void __user *p; |
| 418 | int ret = -ENOTTY; |
| 419 | size_t send_len = 0; |
| 420 | |
| 421 | group = file->private_data; |
| 422 | |
| 423 | p = (void __user *) arg; |
| 424 | |
| 425 | switch (cmd) { |
| 426 | case FIONREAD: |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 427 | spin_lock(&group->notification_lock); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 428 | list_for_each_entry(fsn_event, &group->notification_list, list) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 429 | send_len += FAN_EVENT_METADATA_LEN; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame^] | 430 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 431 | ret = put_user(send_len, (int __user *) p); |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 438 | static const struct file_operations fanotify_fops = { |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 439 | .show_fdinfo = fanotify_show_fdinfo, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 440 | .poll = fanotify_poll, |
| 441 | .read = fanotify_read, |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 442 | .write = fanotify_write, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 443 | .fasync = NULL, |
| 444 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 445 | .unlocked_ioctl = fanotify_ioctl, |
| 446 | .compat_ioctl = fanotify_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 447 | .llseek = noop_llseek, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 448 | }; |
| 449 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 450 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 451 | { |
| 452 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 453 | } |
| 454 | |
| 455 | static int fanotify_find_path(int dfd, const char __user *filename, |
| 456 | struct path *path, unsigned int flags) |
| 457 | { |
| 458 | int ret; |
| 459 | |
| 460 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, |
| 461 | dfd, filename, flags); |
| 462 | |
| 463 | if (filename == NULL) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 464 | struct fd f = fdget(dfd); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 465 | |
| 466 | ret = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 467 | if (!f.file) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 468 | goto out; |
| 469 | |
| 470 | ret = -ENOTDIR; |
| 471 | if ((flags & FAN_MARK_ONLYDIR) && |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 472 | !(S_ISDIR(file_inode(f.file)->i_mode))) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 473 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 474 | goto out; |
| 475 | } |
| 476 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 477 | *path = f.file->f_path; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 478 | path_get(path); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 479 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 480 | } else { |
| 481 | unsigned int lookup_flags = 0; |
| 482 | |
| 483 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 484 | lookup_flags |= LOOKUP_FOLLOW; |
| 485 | if (flags & FAN_MARK_ONLYDIR) |
| 486 | lookup_flags |= LOOKUP_DIRECTORY; |
| 487 | |
| 488 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 489 | if (ret) |
| 490 | goto out; |
| 491 | } |
| 492 | |
| 493 | /* you can only watch an inode if you have read permissions on it */ |
| 494 | ret = inode_permission(path->dentry->d_inode, MAY_READ); |
| 495 | if (ret) |
| 496 | path_put(path); |
| 497 | out: |
| 498 | return ret; |
| 499 | } |
| 500 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 501 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
| 502 | __u32 mask, |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 503 | unsigned int flags, |
| 504 | int *destroy) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 505 | { |
Lino Sanfilippo | d2c1874 | 2015-02-10 14:08:24 -0800 | [diff] [blame] | 506 | __u32 oldmask = 0; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 507 | |
| 508 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 509 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
Lino Sanfilippo | 66ba93c | 2015-02-10 14:08:27 -0800 | [diff] [blame] | 510 | __u32 tmask = fsn_mark->mask & ~mask; |
| 511 | |
| 512 | if (flags & FAN_MARK_ONDIR) |
| 513 | tmask &= ~FAN_ONDIR; |
| 514 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 515 | oldmask = fsn_mark->mask; |
Lino Sanfilippo | 66ba93c | 2015-02-10 14:08:27 -0800 | [diff] [blame] | 516 | fsnotify_set_mark_mask_locked(fsn_mark, tmask); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 517 | } else { |
Lino Sanfilippo | d2c1874 | 2015-02-10 14:08:24 -0800 | [diff] [blame] | 518 | __u32 tmask = fsn_mark->ignored_mask & ~mask; |
Lino Sanfilippo | 66ba93c | 2015-02-10 14:08:27 -0800 | [diff] [blame] | 519 | if (flags & FAN_MARK_ONDIR) |
| 520 | tmask &= ~FAN_ONDIR; |
Lino Sanfilippo | d2c1874 | 2015-02-10 14:08:24 -0800 | [diff] [blame] | 521 | |
| 522 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 523 | } |
Lino Sanfilippo | a118449 | 2015-02-10 14:08:21 -0800 | [diff] [blame] | 524 | *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 525 | spin_unlock(&fsn_mark->lock); |
| 526 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 527 | return mask & oldmask; |
| 528 | } |
| 529 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 530 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 531 | struct vfsmount *mnt, __u32 mask, |
| 532 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 533 | { |
| 534 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 535 | __u32 removed; |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 536 | int destroy_mark; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 537 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 538 | mutex_lock(&group->mark_mutex); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 539 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 540 | if (!fsn_mark) { |
| 541 | mutex_unlock(&group->mark_mutex); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 542 | return -ENOENT; |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 543 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 544 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 545 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
| 546 | &destroy_mark); |
| 547 | if (destroy_mark) |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 548 | fsnotify_detach_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 549 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 550 | if (destroy_mark) |
| 551 | fsnotify_free_mark(fsn_mark); |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 552 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 553 | fsnotify_put_mark(fsn_mark); |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 554 | if (removed & real_mount(mnt)->mnt_fsnotify_mask) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 555 | fsnotify_recalc_vfsmount_mask(mnt); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 556 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 561 | struct inode *inode, __u32 mask, |
| 562 | unsigned int flags) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 563 | { |
| 564 | struct fsnotify_mark *fsn_mark = NULL; |
| 565 | __u32 removed; |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 566 | int destroy_mark; |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 567 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 568 | mutex_lock(&group->mark_mutex); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 569 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 570 | if (!fsn_mark) { |
| 571 | mutex_unlock(&group->mark_mutex); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 572 | return -ENOENT; |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 573 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 574 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 575 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
| 576 | &destroy_mark); |
| 577 | if (destroy_mark) |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 578 | fsnotify_detach_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 579 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 580 | if (destroy_mark) |
| 581 | fsnotify_free_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 582 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 583 | /* matches the fsnotify_find_inode_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 584 | fsnotify_put_mark(fsn_mark); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 585 | if (removed & inode->i_fsnotify_mask) |
| 586 | fsnotify_recalc_inode_mask(inode); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 587 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 591 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 592 | __u32 mask, |
| 593 | unsigned int flags) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 594 | { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 595 | __u32 oldmask = -1; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 596 | |
| 597 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 598 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
Lino Sanfilippo | 66ba93c | 2015-02-10 14:08:27 -0800 | [diff] [blame] | 599 | __u32 tmask = fsn_mark->mask | mask; |
| 600 | |
| 601 | if (flags & FAN_MARK_ONDIR) |
| 602 | tmask |= FAN_ONDIR; |
| 603 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 604 | oldmask = fsn_mark->mask; |
Lino Sanfilippo | 66ba93c | 2015-02-10 14:08:27 -0800 | [diff] [blame] | 605 | fsnotify_set_mark_mask_locked(fsn_mark, tmask); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 606 | } else { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 607 | __u32 tmask = fsn_mark->ignored_mask | mask; |
Lino Sanfilippo | 66ba93c | 2015-02-10 14:08:27 -0800 | [diff] [blame] | 608 | if (flags & FAN_MARK_ONDIR) |
| 609 | tmask |= FAN_ONDIR; |
| 610 | |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 611 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask); |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 612 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 613 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 614 | } |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 615 | spin_unlock(&fsn_mark->lock); |
| 616 | |
| 617 | return mask & ~oldmask; |
| 618 | } |
| 619 | |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 620 | static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, |
| 621 | struct inode *inode, |
| 622 | struct vfsmount *mnt) |
| 623 | { |
| 624 | struct fsnotify_mark *mark; |
| 625 | int ret; |
| 626 | |
| 627 | if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks) |
| 628 | return ERR_PTR(-ENOSPC); |
| 629 | |
| 630 | mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 631 | if (!mark) |
| 632 | return ERR_PTR(-ENOMEM); |
| 633 | |
| 634 | fsnotify_init_mark(mark, fanotify_free_mark); |
| 635 | ret = fsnotify_add_mark_locked(mark, group, inode, mnt, 0); |
| 636 | if (ret) { |
| 637 | fsnotify_put_mark(mark); |
| 638 | return ERR_PTR(ret); |
| 639 | } |
| 640 | |
| 641 | return mark; |
| 642 | } |
| 643 | |
| 644 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 645 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 646 | struct vfsmount *mnt, __u32 mask, |
| 647 | unsigned int flags) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 648 | { |
| 649 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 650 | __u32 added; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 651 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 652 | mutex_lock(&group->mark_mutex); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 653 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 654 | if (!fsn_mark) { |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 655 | fsn_mark = fanotify_add_new_mark(group, NULL, mnt); |
| 656 | if (IS_ERR(fsn_mark)) { |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 657 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 658 | return PTR_ERR(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 659 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 660 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 661 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 662 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 663 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 664 | if (added & ~real_mount(mnt)->mnt_fsnotify_mask) |
Eric Paris | 43709a2 | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 665 | fsnotify_recalc_vfsmount_mask(mnt); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 666 | |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 667 | fsnotify_put_mark(fsn_mark); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 668 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 669 | } |
| 670 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 671 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 672 | struct inode *inode, __u32 mask, |
| 673 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 674 | { |
| 675 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 676 | __u32 added; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 677 | |
| 678 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 679 | |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 680 | /* |
| 681 | * If some other task has this inode open for write we should not add |
| 682 | * an ignored mark, unless that ignored mark is supposed to survive |
| 683 | * modification changes anyway. |
| 684 | */ |
| 685 | if ((flags & FAN_MARK_IGNORED_MASK) && |
| 686 | !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && |
| 687 | (atomic_read(&inode->i_writecount) > 0)) |
| 688 | return 0; |
| 689 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 690 | mutex_lock(&group->mark_mutex); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 691 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 692 | if (!fsn_mark) { |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 693 | fsn_mark = fanotify_add_new_mark(group, inode, NULL); |
| 694 | if (IS_ERR(fsn_mark)) { |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 695 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 696 | return PTR_ERR(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 697 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 698 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 699 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 700 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 701 | |
Eric Paris | 43709a2 | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 702 | if (added & ~inode->i_fsnotify_mask) |
| 703 | fsnotify_recalc_inode_mask(inode); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 704 | |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 705 | fsnotify_put_mark(fsn_mark); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 706 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 707 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 708 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 709 | /* fanotify syscalls */ |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 710 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 711 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 712 | struct fsnotify_group *group; |
| 713 | int f_flags, fd; |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 714 | struct user_struct *user; |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 715 | struct fanotify_event_info *oevent; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 716 | |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 717 | pr_debug("%s: flags=%d event_f_flags=%d\n", |
| 718 | __func__, flags, event_f_flags); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 719 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 720 | if (!capable(CAP_SYS_ADMIN)) |
Andreas Gruenbacher | a2f13ad | 2010-08-24 12:58:54 +0200 | [diff] [blame] | 721 | return -EPERM; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 722 | |
| 723 | if (flags & ~FAN_ALL_INIT_FLAGS) |
| 724 | return -EINVAL; |
| 725 | |
Heinrich Schuchardt | 48149e9 | 2014-06-04 16:05:44 -0700 | [diff] [blame] | 726 | if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS) |
| 727 | return -EINVAL; |
| 728 | |
| 729 | switch (event_f_flags & O_ACCMODE) { |
| 730 | case O_RDONLY: |
| 731 | case O_RDWR: |
| 732 | case O_WRONLY: |
| 733 | break; |
| 734 | default: |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 738 | user = get_current_user(); |
| 739 | if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) { |
| 740 | free_uid(user); |
| 741 | return -EMFILE; |
| 742 | } |
| 743 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 744 | f_flags = O_RDWR | FMODE_NONOTIFY; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 745 | if (flags & FAN_CLOEXEC) |
| 746 | f_flags |= O_CLOEXEC; |
| 747 | if (flags & FAN_NONBLOCK) |
| 748 | f_flags |= O_NONBLOCK; |
| 749 | |
| 750 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 751 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 752 | if (IS_ERR(group)) { |
| 753 | free_uid(user); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 754 | return PTR_ERR(group); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 755 | } |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 756 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 757 | group->fanotify_data.user = user; |
| 758 | atomic_inc(&user->fanotify_listeners); |
| 759 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 760 | oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL); |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 761 | if (unlikely(!oevent)) { |
| 762 | fd = -ENOMEM; |
| 763 | goto out_destroy_group; |
| 764 | } |
| 765 | group->overflow_event = &oevent->fse; |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 766 | |
Will Woods | 1e2ee49 | 2014-05-06 12:50:10 -0700 | [diff] [blame] | 767 | if (force_o_largefile()) |
| 768 | event_f_flags |= O_LARGEFILE; |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 769 | group->fanotify_data.f_flags = event_f_flags; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 770 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
Jan Kara | 9573f79 | 2014-04-03 14:46:34 -0700 | [diff] [blame] | 771 | spin_lock_init(&group->fanotify_data.access_lock); |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 772 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
| 773 | INIT_LIST_HEAD(&group->fanotify_data.access_list); |
| 774 | #endif |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 775 | switch (flags & FAN_ALL_CLASS_BITS) { |
| 776 | case FAN_CLASS_NOTIF: |
| 777 | group->priority = FS_PRIO_0; |
| 778 | break; |
| 779 | case FAN_CLASS_CONTENT: |
| 780 | group->priority = FS_PRIO_1; |
| 781 | break; |
| 782 | case FAN_CLASS_PRE_CONTENT: |
| 783 | group->priority = FS_PRIO_2; |
| 784 | break; |
| 785 | default: |
| 786 | fd = -EINVAL; |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 787 | goto out_destroy_group; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 788 | } |
Eric Paris | cb2d429 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 789 | |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 790 | if (flags & FAN_UNLIMITED_QUEUE) { |
| 791 | fd = -EPERM; |
| 792 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 793 | goto out_destroy_group; |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 794 | group->max_events = UINT_MAX; |
| 795 | } else { |
| 796 | group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS; |
| 797 | } |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 798 | |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 799 | if (flags & FAN_UNLIMITED_MARKS) { |
| 800 | fd = -EPERM; |
| 801 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 802 | goto out_destroy_group; |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 803 | group->fanotify_data.max_marks = UINT_MAX; |
| 804 | } else { |
| 805 | group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS; |
| 806 | } |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 807 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 808 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 809 | if (fd < 0) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 810 | goto out_destroy_group; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 811 | |
| 812 | return fd; |
| 813 | |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 814 | out_destroy_group: |
| 815 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 816 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 817 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 818 | |
Al Viro | 4a0fd5b | 2013-01-21 15:16:58 -0500 | [diff] [blame] | 819 | SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, |
| 820 | __u64, mask, int, dfd, |
| 821 | const char __user *, pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 822 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 823 | struct inode *inode = NULL; |
| 824 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 825 | struct fsnotify_group *group; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 826 | struct fd f; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 827 | struct path path; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 828 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 829 | |
| 830 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 831 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 832 | |
| 833 | /* we only use the lower 32 bits as of right now. */ |
| 834 | if (mask & ((__u64)0xffffffff << 32)) |
| 835 | return -EINVAL; |
| 836 | |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 837 | if (flags & ~FAN_ALL_MARK_FLAGS) |
| 838 | return -EINVAL; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 839 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 840 | case FAN_MARK_ADD: /* fallthrough */ |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 841 | case FAN_MARK_REMOVE: |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 842 | if (!mask) |
| 843 | return -EINVAL; |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 844 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 845 | case FAN_MARK_FLUSH: |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 846 | if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH)) |
| 847 | return -EINVAL; |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 848 | break; |
| 849 | default: |
| 850 | return -EINVAL; |
| 851 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 852 | |
| 853 | if (mask & FAN_ONDIR) { |
| 854 | flags |= FAN_MARK_ONDIR; |
| 855 | mask &= ~FAN_ONDIR; |
| 856 | } |
| 857 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 858 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 859 | if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD)) |
| 860 | #else |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 861 | if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD)) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 862 | #endif |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 863 | return -EINVAL; |
| 864 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 865 | f = fdget(fanotify_fd); |
| 866 | if (unlikely(!f.file)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 867 | return -EBADF; |
| 868 | |
| 869 | /* verify that this is indeed an fanotify instance */ |
| 870 | ret = -EINVAL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 871 | if (unlikely(f.file->f_op != &fanotify_fops)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 872 | goto fput_and_out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 873 | group = f.file->private_data; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 874 | |
| 875 | /* |
| 876 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not |
| 877 | * allowed to set permissions events. |
| 878 | */ |
| 879 | ret = -EINVAL; |
| 880 | if (mask & FAN_ALL_PERM_EVENTS && |
| 881 | group->priority == FS_PRIO_0) |
| 882 | goto fput_and_out; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 883 | |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 884 | if (flags & FAN_MARK_FLUSH) { |
| 885 | ret = 0; |
| 886 | if (flags & FAN_MARK_MOUNT) |
| 887 | fsnotify_clear_vfsmount_marks_by_group(group); |
| 888 | else |
| 889 | fsnotify_clear_inode_marks_by_group(group); |
| 890 | goto fput_and_out; |
| 891 | } |
| 892 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 893 | ret = fanotify_find_path(dfd, pathname, &path, flags); |
| 894 | if (ret) |
| 895 | goto fput_and_out; |
| 896 | |
| 897 | /* 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] | 898 | if (!(flags & FAN_MARK_MOUNT)) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 899 | inode = path.dentry->d_inode; |
| 900 | else |
| 901 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 902 | |
| 903 | /* create/update an inode mark */ |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 904 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 905 | case FAN_MARK_ADD: |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 906 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 907 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 908 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 909 | ret = fanotify_add_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 910 | break; |
| 911 | case FAN_MARK_REMOVE: |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 912 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 913 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 914 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 915 | ret = fanotify_remove_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 916 | break; |
| 917 | default: |
| 918 | ret = -EINVAL; |
| 919 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 920 | |
| 921 | path_put(&path); |
| 922 | fput_and_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 923 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 924 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 925 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 926 | |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 927 | #ifdef CONFIG_COMPAT |
| 928 | COMPAT_SYSCALL_DEFINE6(fanotify_mark, |
| 929 | int, fanotify_fd, unsigned int, flags, |
| 930 | __u32, mask0, __u32, mask1, int, dfd, |
| 931 | const char __user *, pathname) |
| 932 | { |
| 933 | return sys_fanotify_mark(fanotify_fd, flags, |
| 934 | #ifdef __BIG_ENDIAN |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 935 | ((__u64)mask0 << 32) | mask1, |
Heiko Carstens | 592f6b8 | 2014-01-27 17:07:19 -0800 | [diff] [blame] | 936 | #else |
| 937 | ((__u64)mask1 << 32) | mask0, |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 938 | #endif |
| 939 | dfd, pathname); |
| 940 | } |
| 941 | #endif |
| 942 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 943 | /* |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 944 | * fanotify_user_setup - Our initialization function. Note that we cannot return |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 945 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 946 | * must result in panic(). |
| 947 | */ |
| 948 | static int __init fanotify_user_setup(void) |
| 949 | { |
| 950 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 951 | fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 952 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 953 | fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info, |
| 954 | SLAB_PANIC); |
| 955 | #endif |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | device_initcall(fanotify_user_setup); |