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; |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 134 | metadata->mask = fsn_event->mask & FANOTIFY_OUTGOING_EVENTS; |
Amir Goldstein | d0a6a87 | 2018-10-04 00:25:38 +0300 | [diff] [blame] | 135 | metadata->pid = pid_vnr(event->pid); |
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, |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 209 | char __user *buf, size_t count) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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; |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 223 | /* |
| 224 | * Sanity check copy size in case get_one_event() and |
| 225 | * fill_event_metadata() event_len sizes ever get out of sync. |
| 226 | */ |
| 227 | if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count)) |
| 228 | goto out_close_fd; |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 229 | if (copy_to_user(buf, &fanotify_event_metadata, |
| 230 | fanotify_event_metadata.event_len)) |
| 231 | goto out_close_fd; |
| 232 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 233 | if (fanotify_is_perm_event(event->mask)) |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 234 | FANOTIFY_PE(event)->fd = fd; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 235 | |
Al Viro | 3587b1b | 2012-11-18 19:19:00 +0000 | [diff] [blame] | 236 | if (fd != FAN_NOFD) |
| 237 | fd_install(fd, f); |
Eric Paris | 7d13162 | 2010-12-07 15:27:57 -0500 | [diff] [blame] | 238 | return fanotify_event_metadata.event_len; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 239 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 240 | out_close_fd: |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 241 | if (fd != FAN_NOFD) { |
| 242 | put_unused_fd(fd); |
| 243 | fput(f); |
| 244 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 245 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* intofiy userspace file descriptor functions */ |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 249 | static __poll_t fanotify_poll(struct file *file, poll_table *wait) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 250 | { |
| 251 | struct fsnotify_group *group = file->private_data; |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 252 | __poll_t ret = 0; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 253 | |
| 254 | poll_wait(file, &group->notification_waitq, wait); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 255 | spin_lock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 256 | if (!fsnotify_notify_queue_is_empty(group)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 257 | ret = EPOLLIN | EPOLLRDNORM; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 258 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static ssize_t fanotify_read(struct file *file, char __user *buf, |
| 264 | size_t count, loff_t *pos) |
| 265 | { |
| 266 | struct fsnotify_group *group; |
| 267 | struct fsnotify_event *kevent; |
| 268 | char __user *start; |
| 269 | int ret; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 270 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 271 | |
| 272 | start = buf; |
| 273 | group = file->private_data; |
| 274 | |
| 275 | pr_debug("%s: group=%p\n", __func__, group); |
| 276 | |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 277 | add_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 278 | while (1) { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 279 | spin_lock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 280 | kevent = get_one_event(group, count); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 281 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 282 | |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 283 | if (IS_ERR(kevent)) { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 284 | ret = PTR_ERR(kevent); |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | |
| 288 | if (!kevent) { |
| 289 | ret = -EAGAIN; |
| 290 | if (file->f_flags & O_NONBLOCK) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 291 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 292 | |
| 293 | ret = -ERESTARTSYS; |
| 294 | if (signal_pending(current)) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 295 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 296 | |
| 297 | if (start != buf) |
| 298 | break; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 299 | |
| 300 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 301 | continue; |
| 302 | } |
| 303 | |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 304 | ret = copy_event_to_user(group, kevent, buf, count); |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 305 | if (unlikely(ret == -EOPENSTALE)) { |
| 306 | /* |
| 307 | * We cannot report events with stale fd so drop it. |
| 308 | * Setting ret to 0 will continue the event loop and |
| 309 | * do the right thing if there are no more events to |
| 310 | * read (i.e. return bytes read, -EAGAIN or wait). |
| 311 | */ |
| 312 | ret = 0; |
| 313 | } |
| 314 | |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 315 | /* |
| 316 | * Permission events get queued to wait for response. Other |
| 317 | * events can be destroyed now. |
| 318 | */ |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 319 | if (!fanotify_is_perm_event(kevent->mask)) { |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 320 | fsnotify_destroy_event(group, kevent); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 321 | } else { |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 322 | if (ret <= 0) { |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 323 | FANOTIFY_PE(kevent)->response = FAN_DENY; |
| 324 | wake_up(&group->fanotify_data.access_waitq); |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 325 | } else { |
| 326 | spin_lock(&group->notification_lock); |
| 327 | list_add_tail(&kevent->list, |
| 328 | &group->fanotify_data.access_list); |
| 329 | spin_unlock(&group->notification_lock); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 330 | } |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 331 | } |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 332 | if (ret < 0) |
| 333 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 334 | buf += ret; |
| 335 | count -= ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 336 | } |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 337 | remove_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 338 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 339 | if (start != buf && ret != -EFAULT) |
| 340 | ret = buf - start; |
| 341 | return ret; |
| 342 | } |
| 343 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 344 | static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 345 | { |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 346 | struct fanotify_response response = { .fd = -1, .response = -1 }; |
| 347 | struct fsnotify_group *group; |
| 348 | int ret; |
| 349 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 350 | if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
| 351 | return -EINVAL; |
| 352 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 353 | group = file->private_data; |
| 354 | |
| 355 | if (count > sizeof(response)) |
| 356 | count = sizeof(response); |
| 357 | |
| 358 | pr_debug("%s: group=%p count=%zu\n", __func__, group, count); |
| 359 | |
| 360 | if (copy_from_user(&response, buf, count)) |
| 361 | return -EFAULT; |
| 362 | |
| 363 | ret = process_access_response(group, &response); |
| 364 | if (ret < 0) |
| 365 | count = ret; |
| 366 | |
| 367 | return count; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 370 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 371 | { |
| 372 | struct fsnotify_group *group = file->private_data; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 373 | struct fanotify_perm_event_info *event, *next; |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 374 | struct fsnotify_event *fsn_event; |
Andrew Morton | 19ba54f | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 375 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 376 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 377 | * Stop new events from arriving in the notification queue. since |
| 378 | * userspace cannot use fanotify fd anymore, no event can enter or |
| 379 | * leave access_list by now either. |
| 380 | */ |
| 381 | fsnotify_group_stop_queueing(group); |
| 382 | |
| 383 | /* |
| 384 | * Process all permission events on access_list and notification queue |
| 385 | * and simulate reply from userspace. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 386 | */ |
Jan Kara | 073f655 | 2016-10-07 16:56:55 -0700 | [diff] [blame] | 387 | spin_lock(&group->notification_lock); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 388 | list_for_each_entry_safe(event, next, &group->fanotify_data.access_list, |
| 389 | fae.fse.list) { |
| 390 | pr_debug("%s: found group=%p event=%p\n", __func__, group, |
| 391 | event); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 392 | |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 393 | list_del_init(&event->fae.fse.list); |
| 394 | event->response = FAN_ALLOW; |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 395 | } |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 396 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 397 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 398 | * Destroy all non-permission events. For permission events just |
| 399 | * dequeue them and set the response. They will be freed once the |
| 400 | * response is consumed and fanotify_get_response() returns. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 401 | */ |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 402 | while (!fsnotify_notify_queue_is_empty(group)) { |
| 403 | fsn_event = fsnotify_remove_first_event(group); |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 404 | if (!(fsn_event->mask & FANOTIFY_PERM_EVENTS)) { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 405 | spin_unlock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 406 | fsnotify_destroy_event(group, fsn_event); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 407 | spin_lock(&group->notification_lock); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 408 | } else { |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 409 | FANOTIFY_PE(fsn_event)->response = FAN_ALLOW; |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 410 | } |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 411 | } |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 412 | spin_unlock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 413 | |
| 414 | /* Response for all permission events it set, wakeup waiters */ |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 415 | wake_up(&group->fanotify_data.access_waitq); |
Eric Paris | 0a6b6bd | 2011-10-14 17:43:39 -0400 | [diff] [blame] | 416 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 417 | /* matches the fanotify_init->fsnotify_alloc_group */ |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 418 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 423 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 424 | { |
| 425 | struct fsnotify_group *group; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 426 | struct fsnotify_event *fsn_event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 427 | void __user *p; |
| 428 | int ret = -ENOTTY; |
| 429 | size_t send_len = 0; |
| 430 | |
| 431 | group = file->private_data; |
| 432 | |
| 433 | p = (void __user *) arg; |
| 434 | |
| 435 | switch (cmd) { |
| 436 | case FIONREAD: |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 437 | spin_lock(&group->notification_lock); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 438 | list_for_each_entry(fsn_event, &group->notification_list, list) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 439 | send_len += FAN_EVENT_METADATA_LEN; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 440 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 441 | ret = put_user(send_len, (int __user *) p); |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 448 | static const struct file_operations fanotify_fops = { |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 449 | .show_fdinfo = fanotify_show_fdinfo, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 450 | .poll = fanotify_poll, |
| 451 | .read = fanotify_read, |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 452 | .write = fanotify_write, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 453 | .fasync = NULL, |
| 454 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 455 | .unlocked_ioctl = fanotify_ioctl, |
| 456 | .compat_ioctl = fanotify_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 457 | .llseek = noop_llseek, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 458 | }; |
| 459 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 460 | static int fanotify_find_path(int dfd, const char __user *filename, |
| 461 | struct path *path, unsigned int flags) |
| 462 | { |
| 463 | int ret; |
| 464 | |
| 465 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, |
| 466 | dfd, filename, flags); |
| 467 | |
| 468 | if (filename == NULL) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 469 | struct fd f = fdget(dfd); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 470 | |
| 471 | ret = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 472 | if (!f.file) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 473 | goto out; |
| 474 | |
| 475 | ret = -ENOTDIR; |
| 476 | if ((flags & FAN_MARK_ONLYDIR) && |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 477 | !(S_ISDIR(file_inode(f.file)->i_mode))) { |
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 | goto out; |
| 480 | } |
| 481 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 482 | *path = f.file->f_path; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 483 | path_get(path); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 484 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 485 | } else { |
| 486 | unsigned int lookup_flags = 0; |
| 487 | |
| 488 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 489 | lookup_flags |= LOOKUP_FOLLOW; |
| 490 | if (flags & FAN_MARK_ONLYDIR) |
| 491 | lookup_flags |= LOOKUP_DIRECTORY; |
| 492 | |
| 493 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 494 | if (ret) |
| 495 | goto out; |
| 496 | } |
| 497 | |
| 498 | /* you can only watch an inode if you have read permissions on it */ |
| 499 | ret = inode_permission(path->dentry->d_inode, MAY_READ); |
| 500 | if (ret) |
| 501 | path_put(path); |
| 502 | out: |
| 503 | return ret; |
| 504 | } |
| 505 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 506 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
| 507 | __u32 mask, |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 508 | unsigned int flags, |
| 509 | int *destroy) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 510 | { |
Lino Sanfilippo | d2c1874 | 2015-02-10 14:08:24 -0800 | [diff] [blame] | 511 | __u32 oldmask = 0; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 512 | |
| 513 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 514 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 515 | oldmask = fsn_mark->mask; |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 516 | fsn_mark->mask &= ~mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 517 | } else { |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 518 | fsn_mark->ignored_mask &= ~mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 519 | } |
Lino Sanfilippo | a118449 | 2015-02-10 14:08:21 -0800 | [diff] [blame] | 520 | *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 521 | spin_unlock(&fsn_mark->lock); |
| 522 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 523 | return mask & oldmask; |
| 524 | } |
| 525 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 526 | static int fanotify_remove_mark(struct fsnotify_group *group, |
| 527 | fsnotify_connp_t *connp, __u32 mask, |
| 528 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 529 | { |
| 530 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 531 | __u32 removed; |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 532 | int destroy_mark; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 533 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 534 | mutex_lock(&group->mark_mutex); |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 535 | fsn_mark = fsnotify_find_mark(connp, group); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 536 | if (!fsn_mark) { |
| 537 | mutex_unlock(&group->mark_mutex); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 538 | return -ENOENT; |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 539 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 540 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 541 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
| 542 | &destroy_mark); |
Amir Goldstein | 3ac70bf | 2018-06-23 17:54:50 +0300 | [diff] [blame] | 543 | if (removed & fsnotify_conn_mask(fsn_mark->connector)) |
| 544 | fsnotify_recalc_mask(fsn_mark->connector); |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 545 | if (destroy_mark) |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 546 | fsnotify_detach_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 547 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 548 | if (destroy_mark) |
| 549 | fsnotify_free_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 550 | |
Jan Kara | b1362ed | 2016-12-21 16:28:45 +0100 | [diff] [blame] | 551 | /* matches the fsnotify_find_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 552 | fsnotify_put_mark(fsn_mark); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 556 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
| 557 | struct vfsmount *mnt, __u32 mask, |
| 558 | unsigned int flags) |
| 559 | { |
| 560 | return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, |
| 561 | mask, flags); |
| 562 | } |
| 563 | |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 564 | static int fanotify_remove_sb_mark(struct fsnotify_group *group, |
| 565 | struct super_block *sb, __u32 mask, |
| 566 | unsigned int flags) |
| 567 | { |
| 568 | return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags); |
| 569 | } |
| 570 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 571 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
| 572 | struct inode *inode, __u32 mask, |
| 573 | unsigned int flags) |
| 574 | { |
| 575 | return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask, |
| 576 | flags); |
| 577 | } |
| 578 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 579 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 580 | __u32 mask, |
| 581 | unsigned int flags) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 582 | { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 583 | __u32 oldmask = -1; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 584 | |
| 585 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 586 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 587 | oldmask = fsn_mark->mask; |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 588 | fsn_mark->mask |= mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 589 | } else { |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 590 | fsn_mark->ignored_mask |= mask; |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 591 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 592 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 593 | } |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 594 | spin_unlock(&fsn_mark->lock); |
| 595 | |
| 596 | return mask & ~oldmask; |
| 597 | } |
| 598 | |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 599 | static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, |
Amir Goldstein | b812a9f | 2018-06-23 17:54:48 +0300 | [diff] [blame] | 600 | fsnotify_connp_t *connp, |
| 601 | unsigned int type) |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 602 | { |
| 603 | struct fsnotify_mark *mark; |
| 604 | int ret; |
| 605 | |
| 606 | if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks) |
| 607 | return ERR_PTR(-ENOSPC); |
| 608 | |
| 609 | mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 610 | if (!mark) |
| 611 | return ERR_PTR(-ENOMEM); |
| 612 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 613 | fsnotify_init_mark(mark, group); |
Amir Goldstein | b812a9f | 2018-06-23 17:54:48 +0300 | [diff] [blame] | 614 | ret = fsnotify_add_mark_locked(mark, connp, type, 0); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 615 | if (ret) { |
| 616 | fsnotify_put_mark(mark); |
| 617 | return ERR_PTR(ret); |
| 618 | } |
| 619 | |
| 620 | return mark; |
| 621 | } |
| 622 | |
| 623 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 624 | static int fanotify_add_mark(struct fsnotify_group *group, |
| 625 | fsnotify_connp_t *connp, unsigned int type, |
| 626 | __u32 mask, unsigned int flags) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 627 | { |
| 628 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 629 | __u32 added; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 630 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 631 | mutex_lock(&group->mark_mutex); |
Amir Goldstein | b812a9f | 2018-06-23 17:54:48 +0300 | [diff] [blame] | 632 | fsn_mark = fsnotify_find_mark(connp, group); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 633 | if (!fsn_mark) { |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 634 | fsn_mark = fanotify_add_new_mark(group, connp, type); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 635 | if (IS_ERR(fsn_mark)) { |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 636 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 637 | return PTR_ERR(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 638 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 639 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 640 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Amir Goldstein | 3ac70bf | 2018-06-23 17:54:50 +0300 | [diff] [blame] | 641 | if (added & ~fsnotify_conn_mask(fsn_mark->connector)) |
| 642 | fsnotify_recalc_mask(fsn_mark->connector); |
Jan Kara | c974764 | 2016-12-14 13:53:46 +0100 | [diff] [blame] | 643 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 644 | |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 645 | fsnotify_put_mark(fsn_mark); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 646 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 647 | } |
| 648 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 649 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
| 650 | struct vfsmount *mnt, __u32 mask, |
| 651 | unsigned int flags) |
| 652 | { |
| 653 | return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, |
| 654 | FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags); |
| 655 | } |
| 656 | |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 657 | static int fanotify_add_sb_mark(struct fsnotify_group *group, |
| 658 | struct super_block *sb, __u32 mask, |
| 659 | unsigned int flags) |
| 660 | { |
| 661 | return fanotify_add_mark(group, &sb->s_fsnotify_marks, |
| 662 | FSNOTIFY_OBJ_TYPE_SB, mask, flags); |
| 663 | } |
| 664 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 665 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 666 | struct inode *inode, __u32 mask, |
| 667 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 668 | { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 669 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 670 | |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 671 | /* |
| 672 | * If some other task has this inode open for write we should not add |
| 673 | * an ignored mark, unless that ignored mark is supposed to survive |
| 674 | * modification changes anyway. |
| 675 | */ |
| 676 | if ((flags & FAN_MARK_IGNORED_MASK) && |
| 677 | !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && |
Nikolay Borisov | ac9498d | 2018-12-11 10:27:23 +0200 | [diff] [blame] | 678 | inode_is_open_for_write(inode)) |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 679 | return 0; |
| 680 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 681 | return fanotify_add_mark(group, &inode->i_fsnotify_marks, |
| 682 | FSNOTIFY_OBJ_TYPE_INODE, mask, flags); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 683 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 684 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 685 | /* fanotify syscalls */ |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 686 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 687 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 688 | struct fsnotify_group *group; |
| 689 | int f_flags, fd; |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 690 | struct user_struct *user; |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 691 | struct fanotify_event_info *oevent; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 692 | |
Amir Goldstein | 96a71f2 | 2018-09-21 21:20:30 +0300 | [diff] [blame] | 693 | pr_debug("%s: flags=%x event_f_flags=%x\n", |
| 694 | __func__, flags, event_f_flags); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 695 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 696 | if (!capable(CAP_SYS_ADMIN)) |
Andreas Gruenbacher | a2f13ad | 2010-08-24 12:58:54 +0200 | [diff] [blame] | 697 | return -EPERM; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 698 | |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 699 | #ifdef CONFIG_AUDITSYSCALL |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 700 | if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT)) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 701 | #else |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 702 | if (flags & ~FANOTIFY_INIT_FLAGS) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 703 | #endif |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 704 | return -EINVAL; |
| 705 | |
Heinrich Schuchardt | 48149e9 | 2014-06-04 16:05:44 -0700 | [diff] [blame] | 706 | if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS) |
| 707 | return -EINVAL; |
| 708 | |
| 709 | switch (event_f_flags & O_ACCMODE) { |
| 710 | case O_RDONLY: |
| 711 | case O_RDWR: |
| 712 | case O_WRONLY: |
| 713 | break; |
| 714 | default: |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 718 | user = get_current_user(); |
| 719 | if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) { |
| 720 | free_uid(user); |
| 721 | return -EMFILE; |
| 722 | } |
| 723 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 724 | f_flags = O_RDWR | FMODE_NONOTIFY; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 725 | if (flags & FAN_CLOEXEC) |
| 726 | f_flags |= O_CLOEXEC; |
| 727 | if (flags & FAN_NONBLOCK) |
| 728 | f_flags |= O_NONBLOCK; |
| 729 | |
| 730 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 731 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 732 | if (IS_ERR(group)) { |
| 733 | free_uid(user); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 734 | return PTR_ERR(group); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 735 | } |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 736 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 737 | group->fanotify_data.user = user; |
Amir Goldstein | 96a71f2 | 2018-09-21 21:20:30 +0300 | [diff] [blame] | 738 | group->fanotify_data.flags = flags; |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 739 | atomic_inc(&user->fanotify_listeners); |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 740 | group->memcg = get_mem_cgroup_from_mm(current->mm); |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 741 | |
Jan Kara | 1f5eaa9 | 2018-02-21 14:10:59 +0100 | [diff] [blame] | 742 | oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL); |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 743 | if (unlikely(!oevent)) { |
| 744 | fd = -ENOMEM; |
| 745 | goto out_destroy_group; |
| 746 | } |
| 747 | group->overflow_event = &oevent->fse; |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 748 | |
Will Woods | 1e2ee49 | 2014-05-06 12:50:10 -0700 | [diff] [blame] | 749 | if (force_o_largefile()) |
| 750 | event_f_flags |= O_LARGEFILE; |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 751 | group->fanotify_data.f_flags = event_f_flags; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 752 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
| 753 | INIT_LIST_HEAD(&group->fanotify_data.access_list); |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 754 | switch (flags & FANOTIFY_CLASS_BITS) { |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 755 | case FAN_CLASS_NOTIF: |
| 756 | group->priority = FS_PRIO_0; |
| 757 | break; |
| 758 | case FAN_CLASS_CONTENT: |
| 759 | group->priority = FS_PRIO_1; |
| 760 | break; |
| 761 | case FAN_CLASS_PRE_CONTENT: |
| 762 | group->priority = FS_PRIO_2; |
| 763 | break; |
| 764 | default: |
| 765 | fd = -EINVAL; |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 766 | goto out_destroy_group; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 767 | } |
Eric Paris | cb2d429 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 768 | |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 769 | if (flags & FAN_UNLIMITED_QUEUE) { |
| 770 | fd = -EPERM; |
| 771 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 772 | goto out_destroy_group; |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 773 | group->max_events = UINT_MAX; |
| 774 | } else { |
| 775 | group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS; |
| 776 | } |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 777 | |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 778 | if (flags & FAN_UNLIMITED_MARKS) { |
| 779 | fd = -EPERM; |
| 780 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 781 | goto out_destroy_group; |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 782 | group->fanotify_data.max_marks = UINT_MAX; |
| 783 | } else { |
| 784 | group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS; |
| 785 | } |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 786 | |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 787 | if (flags & FAN_ENABLE_AUDIT) { |
| 788 | fd = -EPERM; |
| 789 | if (!capable(CAP_AUDIT_WRITE)) |
| 790 | goto out_destroy_group; |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 793 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 794 | if (fd < 0) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 795 | goto out_destroy_group; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 796 | |
| 797 | return fd; |
| 798 | |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 799 | out_destroy_group: |
| 800 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 801 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 802 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 803 | |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 804 | static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, |
| 805 | int dfd, const char __user *pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 806 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 807 | struct inode *inode = NULL; |
| 808 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 809 | struct fsnotify_group *group; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 810 | struct fd f; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 811 | struct path path; |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 812 | u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 813 | unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 814 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 815 | |
| 816 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 817 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 818 | |
| 819 | /* we only use the lower 32 bits as of right now. */ |
| 820 | if (mask & ((__u64)0xffffffff << 32)) |
| 821 | return -EINVAL; |
| 822 | |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 823 | if (flags & ~FANOTIFY_MARK_FLAGS) |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 824 | return -EINVAL; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 825 | |
| 826 | switch (mark_type) { |
| 827 | case FAN_MARK_INODE: |
| 828 | case FAN_MARK_MOUNT: |
| 829 | case FAN_MARK_FILESYSTEM: |
| 830 | break; |
| 831 | default: |
| 832 | return -EINVAL; |
| 833 | } |
| 834 | |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 835 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 836 | case FAN_MARK_ADD: /* fallthrough */ |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 837 | case FAN_MARK_REMOVE: |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 838 | if (!mask) |
| 839 | return -EINVAL; |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 840 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 841 | case FAN_MARK_FLUSH: |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 842 | if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH)) |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 843 | return -EINVAL; |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 844 | break; |
| 845 | default: |
| 846 | return -EINVAL; |
| 847 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 848 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 849 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 850 | valid_mask |= FANOTIFY_PERM_EVENTS; |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 851 | |
| 852 | if (mask & ~valid_mask) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 853 | return -EINVAL; |
| 854 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 855 | f = fdget(fanotify_fd); |
| 856 | if (unlikely(!f.file)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 857 | return -EBADF; |
| 858 | |
| 859 | /* verify that this is indeed an fanotify instance */ |
| 860 | ret = -EINVAL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 861 | if (unlikely(f.file->f_op != &fanotify_fops)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 862 | goto fput_and_out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 863 | group = f.file->private_data; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 864 | |
| 865 | /* |
| 866 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not |
| 867 | * allowed to set permissions events. |
| 868 | */ |
| 869 | ret = -EINVAL; |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 870 | if (mask & FANOTIFY_PERM_EVENTS && |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 871 | group->priority == FS_PRIO_0) |
| 872 | goto fput_and_out; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 873 | |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 874 | if (flags & FAN_MARK_FLUSH) { |
| 875 | ret = 0; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 876 | if (mark_type == FAN_MARK_MOUNT) |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 877 | fsnotify_clear_vfsmount_marks_by_group(group); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 878 | else if (mark_type == FAN_MARK_FILESYSTEM) |
| 879 | fsnotify_clear_sb_marks_by_group(group); |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 880 | else |
| 881 | fsnotify_clear_inode_marks_by_group(group); |
| 882 | goto fput_and_out; |
| 883 | } |
| 884 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 885 | ret = fanotify_find_path(dfd, pathname, &path, flags); |
| 886 | if (ret) |
| 887 | goto fput_and_out; |
| 888 | |
| 889 | /* 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] | 890 | if (mark_type == FAN_MARK_INODE) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 891 | inode = path.dentry->d_inode; |
| 892 | else |
| 893 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 894 | |
| 895 | /* create/update an inode mark */ |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 896 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 897 | case FAN_MARK_ADD: |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 898 | if (mark_type == FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 899 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 900 | else if (mark_type == FAN_MARK_FILESYSTEM) |
| 901 | ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, flags); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 902 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 903 | ret = fanotify_add_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 904 | break; |
| 905 | case FAN_MARK_REMOVE: |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 906 | if (mark_type == FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 907 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 908 | else if (mark_type == FAN_MARK_FILESYSTEM) |
| 909 | ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 910 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 911 | ret = fanotify_remove_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 912 | break; |
| 913 | default: |
| 914 | ret = -EINVAL; |
| 915 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 916 | |
| 917 | path_put(&path); |
| 918 | fput_and_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 919 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 920 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 921 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 922 | |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 923 | SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, |
| 924 | __u64, mask, int, dfd, |
| 925 | const char __user *, pathname) |
| 926 | { |
| 927 | return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname); |
| 928 | } |
| 929 | |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 930 | #ifdef CONFIG_COMPAT |
| 931 | COMPAT_SYSCALL_DEFINE6(fanotify_mark, |
| 932 | int, fanotify_fd, unsigned int, flags, |
| 933 | __u32, mask0, __u32, mask1, int, dfd, |
| 934 | const char __user *, pathname) |
| 935 | { |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 936 | return do_fanotify_mark(fanotify_fd, flags, |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 937 | #ifdef __BIG_ENDIAN |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 938 | ((__u64)mask0 << 32) | mask1, |
Heiko Carstens | 592f6b8 | 2014-01-27 17:07:19 -0800 | [diff] [blame] | 939 | #else |
| 940 | ((__u64)mask1 << 32) | mask0, |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 941 | #endif |
| 942 | dfd, pathname); |
| 943 | } |
| 944 | #endif |
| 945 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 946 | /* |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 947 | * fanotify_user_setup - Our initialization function. Note that we cannot return |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 948 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 949 | * must result in panic(). |
| 950 | */ |
| 951 | static int __init fanotify_user_setup(void) |
| 952 | { |
Amir Goldstein | d0a6a87 | 2018-10-04 00:25:38 +0300 | [diff] [blame] | 953 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7); |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 954 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); |
| 955 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 956 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, |
| 957 | SLAB_PANIC|SLAB_ACCOUNT); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 958 | fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 959 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) { |
| 960 | fanotify_perm_event_cachep = |
| 961 | KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC); |
| 962 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | device_initcall(fanotify_user_setup); |