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