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> |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 20 | #include <linux/statfs.h> |
| 21 | #include <linux/exportfs.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 22 | |
| 23 | #include <asm/ioctls.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 24 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 25 | #include "../../mount.h" |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 26 | #include "../fdinfo.h" |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 27 | #include "fanotify.h" |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 28 | |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 29 | #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 30 | #define FANOTIFY_DEFAULT_MAX_MARKS 8192 |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 31 | #define FANOTIFY_DEFAULT_MAX_LISTENERS 128 |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 32 | |
Heinrich Schuchardt | 48149e9 | 2014-06-04 16:05:44 -0700 | [diff] [blame] | 33 | /* |
| 34 | * All flags that may be specified in parameter event_f_flags of fanotify_init. |
| 35 | * |
| 36 | * Internal and external open flags are stored together in field f_flags of |
| 37 | * struct file. Only external open flags shall be allowed in event_f_flags. |
| 38 | * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be |
| 39 | * excluded. |
| 40 | */ |
| 41 | #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ |
| 42 | O_ACCMODE | O_APPEND | O_NONBLOCK | \ |
| 43 | __O_SYNC | O_DSYNC | O_CLOEXEC | \ |
| 44 | O_LARGEFILE | O_NOATIME ) |
| 45 | |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 46 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 47 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 48 | struct kmem_cache *fanotify_mark_cache __read_mostly; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 49 | struct kmem_cache *fanotify_fid_event_cachep __read_mostly; |
| 50 | struct kmem_cache *fanotify_path_event_cachep __read_mostly; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 51 | struct kmem_cache *fanotify_perm_event_cachep __read_mostly; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 52 | |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 53 | #define FANOTIFY_EVENT_ALIGN 4 |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 54 | #define FANOTIFY_INFO_HDR_LEN \ |
| 55 | (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 56 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 57 | static int fanotify_fid_info_len(int fh_len, int name_len) |
Amir Goldstein | d766b55 | 2020-03-19 17:10:20 +0200 | [diff] [blame] | 58 | { |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 59 | int info_len = fh_len; |
| 60 | |
| 61 | if (name_len) |
| 62 | info_len += name_len + 1; |
| 63 | |
| 64 | return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN); |
Amir Goldstein | d766b55 | 2020-03-19 17:10:20 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 67 | static int fanotify_event_info_len(struct fanotify_event *event) |
| 68 | { |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 69 | int info_len = 0; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 70 | int fh_len = fanotify_event_object_fh_len(event); |
| 71 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 72 | if (fh_len) |
| 73 | info_len += fanotify_fid_info_len(fh_len, 0); |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 74 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 75 | if (fanotify_event_name_len(event)) { |
| 76 | struct fanotify_name_event *fne = FANOTIFY_NE(event); |
| 77 | |
| 78 | info_len += fanotify_fid_info_len(fne->dir_fh.len, |
| 79 | fne->name_len); |
| 80 | } |
| 81 | |
| 82 | return info_len; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 85 | /* |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 86 | * Get an fanotify notification event if one exists and is small |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 87 | * enough to fit in "count". Return an error pointer if the count |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 88 | * is not large enough. When permission event is dequeued, its state is |
| 89 | * updated accordingly. |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 90 | */ |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 91 | static struct fanotify_event *get_one_event(struct fsnotify_group *group, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 92 | size_t count) |
| 93 | { |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 94 | size_t event_size = FAN_EVENT_METADATA_LEN; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 95 | struct fanotify_event *event = NULL; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 96 | |
| 97 | pr_debug("%s: group=%p count=%zd\n", __func__, group, count); |
| 98 | |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 99 | spin_lock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 100 | if (fsnotify_notify_queue_is_empty(group)) |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 101 | goto out; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 102 | |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame^] | 103 | if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) { |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 104 | event_size += fanotify_event_info_len( |
| 105 | FANOTIFY_E(fsnotify_peek_first_event(group))); |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 108 | if (event_size > count) { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 109 | event = ERR_PTR(-EINVAL); |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 110 | goto out; |
| 111 | } |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 112 | event = FANOTIFY_E(fsnotify_remove_first_event(group)); |
| 113 | if (fanotify_is_perm_event(event->mask)) |
| 114 | FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED; |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 115 | out: |
| 116 | spin_unlock(&group->notification_lock); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 117 | return event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Jan Kara | a741c2f | 2020-03-24 15:27:52 +0100 | [diff] [blame] | 120 | static int create_fd(struct fsnotify_group *group, struct path *path, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 121 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 122 | { |
| 123 | int client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 124 | struct file *new_file; |
| 125 | |
Yann Droneaud | 0b37e09 | 2014-10-09 15:24:40 -0700 | [diff] [blame] | 126 | client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 127 | if (client_fd < 0) |
| 128 | return client_fd; |
| 129 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 130 | /* |
| 131 | * we need a new file handle for the userspace program so it can read even if it was |
| 132 | * originally opened O_WRONLY. |
| 133 | */ |
Jan Kara | a741c2f | 2020-03-24 15:27:52 +0100 | [diff] [blame] | 134 | new_file = dentry_open(path, |
| 135 | group->fanotify_data.f_flags | FMODE_NONOTIFY, |
| 136 | current_cred()); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 137 | if (IS_ERR(new_file)) { |
| 138 | /* |
| 139 | * we still send an event even if we can't open the file. this |
| 140 | * can happen when say tasks are gone and we try to open their |
| 141 | * /proc files or we try to open a WRONLY file like in sysfs |
| 142 | * we just send the errno to userspace since there isn't much |
| 143 | * else we can do. |
| 144 | */ |
| 145 | put_unused_fd(client_fd); |
| 146 | client_fd = PTR_ERR(new_file); |
| 147 | } else { |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 148 | *file = new_file; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 151 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 154 | /* |
| 155 | * Finish processing of permission event by setting it to ANSWERED state and |
| 156 | * drop group->notification_lock. |
| 157 | */ |
| 158 | static void finish_permission_event(struct fsnotify_group *group, |
| 159 | struct fanotify_perm_event *event, |
| 160 | unsigned int response) |
| 161 | __releases(&group->notification_lock) |
| 162 | { |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 163 | bool destroy = false; |
| 164 | |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 165 | assert_spin_locked(&group->notification_lock); |
| 166 | event->response = response; |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 167 | if (event->state == FAN_EVENT_CANCELED) |
| 168 | destroy = true; |
| 169 | else |
| 170 | event->state = FAN_EVENT_ANSWERED; |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 171 | spin_unlock(&group->notification_lock); |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 172 | if (destroy) |
| 173 | fsnotify_destroy_event(group, &event->fae.fse); |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 176 | static int process_access_response(struct fsnotify_group *group, |
| 177 | struct fanotify_response *response_struct) |
| 178 | { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 179 | struct fanotify_perm_event *event; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 180 | int fd = response_struct->fd; |
| 181 | int response = response_struct->response; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 182 | |
| 183 | pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group, |
| 184 | fd, response); |
| 185 | /* |
| 186 | * 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] | 187 | * 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] | 188 | * timeout |
| 189 | */ |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 190 | switch (response & ~FAN_AUDIT) { |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 191 | case FAN_ALLOW: |
| 192 | case FAN_DENY: |
| 193 | break; |
| 194 | default: |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | if (fd < 0) |
| 199 | return -EINVAL; |
| 200 | |
Amir Goldstein | 96a71f2 | 2018-09-21 21:20:30 +0300 | [diff] [blame] | 201 | if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 202 | return -EINVAL; |
| 203 | |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 204 | spin_lock(&group->notification_lock); |
| 205 | list_for_each_entry(event, &group->fanotify_data.access_list, |
| 206 | fae.fse.list) { |
| 207 | if (event->fd != fd) |
| 208 | continue; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 209 | |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 210 | list_del_init(&event->fae.fse.list); |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 211 | finish_permission_event(group, event, response); |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 212 | wake_up(&group->fanotify_data.access_waitq); |
| 213 | return 0; |
| 214 | } |
| 215 | spin_unlock(&group->notification_lock); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 216 | |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 217 | return -ENOENT; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 218 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 219 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 220 | static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh, |
| 221 | const char *name, size_t name_len, |
| 222 | char __user *buf, size_t count) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 223 | { |
| 224 | struct fanotify_event_info_fid info = { }; |
| 225 | struct file_handle handle = { }; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 226 | unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf; |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 227 | size_t fh_len = fh ? fh->len : 0; |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 228 | size_t info_len = fanotify_fid_info_len(fh_len, name_len); |
| 229 | size_t len = info_len; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 230 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 231 | pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n", |
| 232 | __func__, fh_len, name_len, info_len, count); |
| 233 | |
| 234 | if (!fh_len || (name && !name_len)) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 235 | return 0; |
| 236 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 237 | if (WARN_ON_ONCE(len < sizeof(info) || len > count)) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 238 | return -EFAULT; |
| 239 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 240 | /* |
| 241 | * Copy event info fid header followed by variable sized file handle |
| 242 | * and optionally followed by variable sized filename. |
| 243 | */ |
| 244 | info.hdr.info_type = name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME : |
| 245 | FAN_EVENT_INFO_TYPE_FID; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 246 | info.hdr.len = len; |
Amir Goldstein | d766b55 | 2020-03-19 17:10:20 +0200 | [diff] [blame] | 247 | info.fsid = *fsid; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 248 | if (copy_to_user(buf, &info, sizeof(info))) |
| 249 | return -EFAULT; |
| 250 | |
| 251 | buf += sizeof(info); |
| 252 | len -= sizeof(info); |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 253 | if (WARN_ON_ONCE(len < sizeof(handle))) |
| 254 | return -EFAULT; |
| 255 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 256 | handle.handle_type = fh->type; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 257 | handle.handle_bytes = fh_len; |
| 258 | if (copy_to_user(buf, &handle, sizeof(handle))) |
| 259 | return -EFAULT; |
| 260 | |
| 261 | buf += sizeof(handle); |
| 262 | len -= sizeof(handle); |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 263 | if (WARN_ON_ONCE(len < fh_len)) |
| 264 | return -EFAULT; |
| 265 | |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 266 | /* |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 267 | * For an inline fh and inline file name, copy through stack to exclude |
| 268 | * the copy from usercopy hardening protections. |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 269 | */ |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 270 | fh_buf = fanotify_fh_buf(fh); |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 271 | if (fh_len <= FANOTIFY_INLINE_FH_LEN) { |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 272 | memcpy(bounce, fh_buf, fh_len); |
| 273 | fh_buf = bounce; |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 274 | } |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 275 | if (copy_to_user(buf, fh_buf, fh_len)) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 276 | return -EFAULT; |
| 277 | |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 278 | buf += fh_len; |
| 279 | len -= fh_len; |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 280 | |
| 281 | if (name_len) { |
| 282 | /* Copy the filename with terminating null */ |
| 283 | name_len++; |
| 284 | if (WARN_ON_ONCE(len < name_len)) |
| 285 | return -EFAULT; |
| 286 | |
| 287 | if (copy_to_user(buf, name, name_len)) |
| 288 | return -EFAULT; |
| 289 | |
| 290 | buf += name_len; |
| 291 | len -= name_len; |
| 292 | } |
| 293 | |
| 294 | /* Pad with 0's */ |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 295 | WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN); |
| 296 | if (len > 0 && clear_user(buf, len)) |
| 297 | return -EFAULT; |
| 298 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 299 | return info_len; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 302 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 303 | struct fanotify_event *event, |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 304 | char __user *buf, size_t count) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 305 | { |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 306 | struct fanotify_event_metadata metadata; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 307 | struct path *path = fanotify_event_path(event); |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 308 | struct file *f = NULL; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 309 | int ret, fd = FAN_NOFD; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 310 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 311 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 312 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 313 | metadata.event_len = FAN_EVENT_METADATA_LEN + |
| 314 | fanotify_event_info_len(event); |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 315 | metadata.metadata_len = FAN_EVENT_METADATA_LEN; |
| 316 | metadata.vers = FANOTIFY_METADATA_VERSION; |
| 317 | metadata.reserved = 0; |
| 318 | metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS; |
| 319 | metadata.pid = pid_vnr(event->pid); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 320 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 321 | if (path && path->mnt && path->dentry) { |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 322 | fd = create_fd(group, path, &f); |
| 323 | if (fd < 0) |
| 324 | return fd; |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 325 | } |
| 326 | metadata.fd = fd; |
| 327 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 328 | ret = -EFAULT; |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 329 | /* |
| 330 | * Sanity check copy size in case get_one_event() and |
Fabian Frederick | c5e443c | 2020-05-12 20:18:36 +0200 | [diff] [blame] | 331 | * event_len sizes ever get out of sync. |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 332 | */ |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 333 | if (WARN_ON_ONCE(metadata.event_len > count)) |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 334 | goto out_close_fd; |
| 335 | |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 336 | if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN)) |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 337 | goto out_close_fd; |
| 338 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 339 | buf += FAN_EVENT_METADATA_LEN; |
| 340 | count -= FAN_EVENT_METADATA_LEN; |
| 341 | |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 342 | if (fanotify_is_perm_event(event->mask)) |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 343 | FANOTIFY_PERM(event)->fd = fd; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 344 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 345 | if (f) |
Al Viro | 3587b1b | 2012-11-18 19:19:00 +0000 | [diff] [blame] | 346 | fd_install(fd, f); |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 347 | |
| 348 | /* Event info records order is: dir fid + name, child fid */ |
| 349 | if (fanotify_event_name_len(event)) { |
| 350 | struct fanotify_name_event *fne = FANOTIFY_NE(event); |
| 351 | |
| 352 | ret = copy_info_to_user(fanotify_event_fsid(event), |
| 353 | fanotify_event_dir_fh(event), |
| 354 | fne->name, fne->name_len, |
| 355 | buf, count); |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 356 | if (ret < 0) |
| 357 | return ret; |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 358 | |
| 359 | buf += ret; |
| 360 | count -= ret; |
| 361 | } |
| 362 | |
| 363 | if (fanotify_event_object_fh_len(event)) { |
| 364 | ret = copy_info_to_user(fanotify_event_fsid(event), |
| 365 | fanotify_event_object_fh(event), |
| 366 | NULL, 0, buf, count); |
| 367 | if (ret < 0) |
| 368 | return ret; |
| 369 | |
| 370 | buf += ret; |
| 371 | count -= ret; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 374 | return metadata.event_len; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 375 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 376 | out_close_fd: |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 377 | if (fd != FAN_NOFD) { |
| 378 | put_unused_fd(fd); |
| 379 | fput(f); |
| 380 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 381 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* intofiy userspace file descriptor functions */ |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 385 | static __poll_t fanotify_poll(struct file *file, poll_table *wait) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 386 | { |
| 387 | struct fsnotify_group *group = file->private_data; |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 388 | __poll_t ret = 0; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 389 | |
| 390 | poll_wait(file, &group->notification_waitq, wait); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 391 | spin_lock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 392 | if (!fsnotify_notify_queue_is_empty(group)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 393 | ret = EPOLLIN | EPOLLRDNORM; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 394 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 395 | |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static ssize_t fanotify_read(struct file *file, char __user *buf, |
| 400 | size_t count, loff_t *pos) |
| 401 | { |
| 402 | struct fsnotify_group *group; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 403 | struct fanotify_event *event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 404 | char __user *start; |
| 405 | int ret; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 406 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 407 | |
| 408 | start = buf; |
| 409 | group = file->private_data; |
| 410 | |
| 411 | pr_debug("%s: group=%p\n", __func__, group); |
| 412 | |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 413 | add_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 414 | while (1) { |
Jan Kara | 47aaabd | 2020-07-15 14:06:21 +0200 | [diff] [blame] | 415 | /* |
| 416 | * User can supply arbitrarily large buffer. Avoid softlockups |
| 417 | * in case there are lots of available events. |
| 418 | */ |
| 419 | cond_resched(); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 420 | event = get_one_event(group, count); |
| 421 | if (IS_ERR(event)) { |
| 422 | ret = PTR_ERR(event); |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 423 | break; |
| 424 | } |
| 425 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 426 | if (!event) { |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 427 | ret = -EAGAIN; |
| 428 | if (file->f_flags & O_NONBLOCK) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 429 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 430 | |
| 431 | ret = -ERESTARTSYS; |
| 432 | if (signal_pending(current)) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 433 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 434 | |
| 435 | if (start != buf) |
| 436 | break; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 437 | |
| 438 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 439 | continue; |
| 440 | } |
| 441 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 442 | ret = copy_event_to_user(group, event, buf, count); |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 443 | if (unlikely(ret == -EOPENSTALE)) { |
| 444 | /* |
| 445 | * We cannot report events with stale fd so drop it. |
| 446 | * Setting ret to 0 will continue the event loop and |
| 447 | * do the right thing if there are no more events to |
| 448 | * read (i.e. return bytes read, -EAGAIN or wait). |
| 449 | */ |
| 450 | ret = 0; |
| 451 | } |
| 452 | |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 453 | /* |
| 454 | * Permission events get queued to wait for response. Other |
| 455 | * events can be destroyed now. |
| 456 | */ |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 457 | if (!fanotify_is_perm_event(event->mask)) { |
| 458 | fsnotify_destroy_event(group, &event->fse); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 459 | } else { |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 460 | if (ret <= 0) { |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 461 | spin_lock(&group->notification_lock); |
| 462 | finish_permission_event(group, |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 463 | FANOTIFY_PERM(event), FAN_DENY); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 464 | wake_up(&group->fanotify_data.access_waitq); |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 465 | } else { |
| 466 | spin_lock(&group->notification_lock); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 467 | list_add_tail(&event->fse.list, |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 468 | &group->fanotify_data.access_list); |
| 469 | spin_unlock(&group->notification_lock); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 470 | } |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 471 | } |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 472 | if (ret < 0) |
| 473 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 474 | buf += ret; |
| 475 | count -= ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 476 | } |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 477 | remove_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 478 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 479 | if (start != buf && ret != -EFAULT) |
| 480 | ret = buf - start; |
| 481 | return ret; |
| 482 | } |
| 483 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 484 | static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 485 | { |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 486 | struct fanotify_response response = { .fd = -1, .response = -1 }; |
| 487 | struct fsnotify_group *group; |
| 488 | int ret; |
| 489 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 490 | if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
| 491 | return -EINVAL; |
| 492 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 493 | group = file->private_data; |
| 494 | |
Fabian Frederick | 5e23663 | 2020-05-12 20:19:21 +0200 | [diff] [blame] | 495 | if (count < sizeof(response)) |
| 496 | return -EINVAL; |
| 497 | |
| 498 | count = sizeof(response); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 499 | |
| 500 | pr_debug("%s: group=%p count=%zu\n", __func__, group, count); |
| 501 | |
| 502 | if (copy_from_user(&response, buf, count)) |
| 503 | return -EFAULT; |
| 504 | |
| 505 | ret = process_access_response(group, &response); |
| 506 | if (ret < 0) |
| 507 | count = ret; |
| 508 | |
| 509 | return count; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 510 | } |
| 511 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 512 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 513 | { |
| 514 | struct fsnotify_group *group = file->private_data; |
Andrew Morton | 19ba54f | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 515 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 516 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 517 | * Stop new events from arriving in the notification queue. since |
| 518 | * userspace cannot use fanotify fd anymore, no event can enter or |
| 519 | * leave access_list by now either. |
| 520 | */ |
| 521 | fsnotify_group_stop_queueing(group); |
| 522 | |
| 523 | /* |
| 524 | * Process all permission events on access_list and notification queue |
| 525 | * and simulate reply from userspace. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 526 | */ |
Jan Kara | 073f655 | 2016-10-07 16:56:55 -0700 | [diff] [blame] | 527 | spin_lock(&group->notification_lock); |
Jan Kara | ca6f869 | 2019-01-09 13:21:01 +0100 | [diff] [blame] | 528 | while (!list_empty(&group->fanotify_data.access_list)) { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 529 | struct fanotify_perm_event *event; |
| 530 | |
Jan Kara | ca6f869 | 2019-01-09 13:21:01 +0100 | [diff] [blame] | 531 | event = list_first_entry(&group->fanotify_data.access_list, |
| 532 | struct fanotify_perm_event, fae.fse.list); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 533 | list_del_init(&event->fae.fse.list); |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 534 | finish_permission_event(group, event, FAN_ALLOW); |
| 535 | spin_lock(&group->notification_lock); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 536 | } |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 537 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 538 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 539 | * Destroy all non-permission events. For permission events just |
| 540 | * dequeue them and set the response. They will be freed once the |
| 541 | * response is consumed and fanotify_get_response() returns. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 542 | */ |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 543 | while (!fsnotify_notify_queue_is_empty(group)) { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 544 | struct fanotify_event *event; |
| 545 | |
| 546 | event = FANOTIFY_E(fsnotify_remove_first_event(group)); |
| 547 | if (!(event->mask & FANOTIFY_PERM_EVENTS)) { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 548 | spin_unlock(&group->notification_lock); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 549 | fsnotify_destroy_event(group, &event->fse); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 550 | } else { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 551 | finish_permission_event(group, FANOTIFY_PERM(event), |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 552 | FAN_ALLOW); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 553 | } |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 554 | spin_lock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 555 | } |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 556 | spin_unlock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 557 | |
| 558 | /* Response for all permission events it set, wakeup waiters */ |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 559 | wake_up(&group->fanotify_data.access_waitq); |
Eric Paris | 0a6b6bd | 2011-10-14 17:43:39 -0400 | [diff] [blame] | 560 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 561 | /* matches the fanotify_init->fsnotify_alloc_group */ |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 562 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 567 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 568 | { |
| 569 | struct fsnotify_group *group; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 570 | struct fsnotify_event *fsn_event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 571 | void __user *p; |
| 572 | int ret = -ENOTTY; |
| 573 | size_t send_len = 0; |
| 574 | |
| 575 | group = file->private_data; |
| 576 | |
| 577 | p = (void __user *) arg; |
| 578 | |
| 579 | switch (cmd) { |
| 580 | case FIONREAD: |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 581 | spin_lock(&group->notification_lock); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 582 | list_for_each_entry(fsn_event, &group->notification_list, list) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 583 | send_len += FAN_EVENT_METADATA_LEN; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 584 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 585 | ret = put_user(send_len, (int __user *) p); |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 592 | static const struct file_operations fanotify_fops = { |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 593 | .show_fdinfo = fanotify_show_fdinfo, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 594 | .poll = fanotify_poll, |
| 595 | .read = fanotify_read, |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 596 | .write = fanotify_write, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 597 | .fasync = NULL, |
| 598 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 599 | .unlocked_ioctl = fanotify_ioctl, |
Arnd Bergmann | 1832f2d | 2018-09-11 21:59:08 +0200 | [diff] [blame] | 600 | .compat_ioctl = compat_ptr_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 601 | .llseek = noop_llseek, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 602 | }; |
| 603 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 604 | static int fanotify_find_path(int dfd, const char __user *filename, |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 605 | struct path *path, unsigned int flags, __u64 mask, |
| 606 | unsigned int obj_type) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 607 | { |
| 608 | int ret; |
| 609 | |
| 610 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, |
| 611 | dfd, filename, flags); |
| 612 | |
| 613 | if (filename == NULL) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 614 | struct fd f = fdget(dfd); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 615 | |
| 616 | ret = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 617 | if (!f.file) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 618 | goto out; |
| 619 | |
| 620 | ret = -ENOTDIR; |
| 621 | if ((flags & FAN_MARK_ONLYDIR) && |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 622 | !(S_ISDIR(file_inode(f.file)->i_mode))) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 623 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 624 | goto out; |
| 625 | } |
| 626 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 627 | *path = f.file->f_path; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 628 | path_get(path); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 629 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 630 | } else { |
| 631 | unsigned int lookup_flags = 0; |
| 632 | |
| 633 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 634 | lookup_flags |= LOOKUP_FOLLOW; |
| 635 | if (flags & FAN_MARK_ONLYDIR) |
| 636 | lookup_flags |= LOOKUP_DIRECTORY; |
| 637 | |
| 638 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 639 | if (ret) |
| 640 | goto out; |
| 641 | } |
| 642 | |
| 643 | /* you can only watch an inode if you have read permissions on it */ |
| 644 | ret = inode_permission(path->dentry->d_inode, MAY_READ); |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 645 | if (ret) { |
| 646 | path_put(path); |
| 647 | goto out; |
| 648 | } |
| 649 | |
| 650 | ret = security_path_notify(path, mask, obj_type); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 651 | if (ret) |
| 652 | path_put(path); |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 653 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 654 | out: |
| 655 | return ret; |
| 656 | } |
| 657 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 658 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
| 659 | __u32 mask, |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 660 | unsigned int flags, |
| 661 | int *destroy) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 662 | { |
Lino Sanfilippo | d2c1874 | 2015-02-10 14:08:24 -0800 | [diff] [blame] | 663 | __u32 oldmask = 0; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 664 | |
| 665 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 666 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 667 | oldmask = fsn_mark->mask; |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 668 | fsn_mark->mask &= ~mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 669 | } else { |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 670 | fsn_mark->ignored_mask &= ~mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 671 | } |
Lino Sanfilippo | a118449 | 2015-02-10 14:08:21 -0800 | [diff] [blame] | 672 | *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 673 | spin_unlock(&fsn_mark->lock); |
| 674 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 675 | return mask & oldmask; |
| 676 | } |
| 677 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 678 | static int fanotify_remove_mark(struct fsnotify_group *group, |
| 679 | fsnotify_connp_t *connp, __u32 mask, |
| 680 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 681 | { |
| 682 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 683 | __u32 removed; |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 684 | int destroy_mark; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 685 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 686 | mutex_lock(&group->mark_mutex); |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 687 | fsn_mark = fsnotify_find_mark(connp, group); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 688 | if (!fsn_mark) { |
| 689 | mutex_unlock(&group->mark_mutex); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 690 | return -ENOENT; |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 691 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 692 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 693 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
| 694 | &destroy_mark); |
Amir Goldstein | 3ac70bf | 2018-06-23 17:54:50 +0300 | [diff] [blame] | 695 | if (removed & fsnotify_conn_mask(fsn_mark->connector)) |
| 696 | fsnotify_recalc_mask(fsn_mark->connector); |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 697 | if (destroy_mark) |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 698 | fsnotify_detach_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 699 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 700 | if (destroy_mark) |
| 701 | fsnotify_free_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 702 | |
Jan Kara | b1362ed | 2016-12-21 16:28:45 +0100 | [diff] [blame] | 703 | /* matches the fsnotify_find_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 704 | fsnotify_put_mark(fsn_mark); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 708 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
| 709 | struct vfsmount *mnt, __u32 mask, |
| 710 | unsigned int flags) |
| 711 | { |
| 712 | return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, |
| 713 | mask, flags); |
| 714 | } |
| 715 | |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 716 | static int fanotify_remove_sb_mark(struct fsnotify_group *group, |
| 717 | struct super_block *sb, __u32 mask, |
| 718 | unsigned int flags) |
| 719 | { |
| 720 | return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags); |
| 721 | } |
| 722 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 723 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
| 724 | struct inode *inode, __u32 mask, |
| 725 | unsigned int flags) |
| 726 | { |
| 727 | return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask, |
| 728 | flags); |
| 729 | } |
| 730 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 731 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 732 | __u32 mask, |
| 733 | unsigned int flags) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 734 | { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 735 | __u32 oldmask = -1; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 736 | |
| 737 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 738 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 739 | oldmask = fsn_mark->mask; |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 740 | fsn_mark->mask |= mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 741 | } else { |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 742 | fsn_mark->ignored_mask |= mask; |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 743 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 744 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 745 | } |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 746 | spin_unlock(&fsn_mark->lock); |
| 747 | |
| 748 | return mask & ~oldmask; |
| 749 | } |
| 750 | |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 751 | static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, |
Amir Goldstein | b812a9f | 2018-06-23 17:54:48 +0300 | [diff] [blame] | 752 | fsnotify_connp_t *connp, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 753 | unsigned int type, |
| 754 | __kernel_fsid_t *fsid) |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 755 | { |
| 756 | struct fsnotify_mark *mark; |
| 757 | int ret; |
| 758 | |
| 759 | if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks) |
| 760 | return ERR_PTR(-ENOSPC); |
| 761 | |
| 762 | mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 763 | if (!mark) |
| 764 | return ERR_PTR(-ENOMEM); |
| 765 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 766 | fsnotify_init_mark(mark, group); |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 767 | ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 768 | if (ret) { |
| 769 | fsnotify_put_mark(mark); |
| 770 | return ERR_PTR(ret); |
| 771 | } |
| 772 | |
| 773 | return mark; |
| 774 | } |
| 775 | |
| 776 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 777 | static int fanotify_add_mark(struct fsnotify_group *group, |
| 778 | fsnotify_connp_t *connp, unsigned int type, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 779 | __u32 mask, unsigned int flags, |
| 780 | __kernel_fsid_t *fsid) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 781 | { |
| 782 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 783 | __u32 added; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 784 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 785 | mutex_lock(&group->mark_mutex); |
Amir Goldstein | b812a9f | 2018-06-23 17:54:48 +0300 | [diff] [blame] | 786 | fsn_mark = fsnotify_find_mark(connp, group); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 787 | if (!fsn_mark) { |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 788 | fsn_mark = fanotify_add_new_mark(group, connp, type, fsid); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 789 | if (IS_ERR(fsn_mark)) { |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 790 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 791 | return PTR_ERR(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 792 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 793 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 794 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Amir Goldstein | 3ac70bf | 2018-06-23 17:54:50 +0300 | [diff] [blame] | 795 | if (added & ~fsnotify_conn_mask(fsn_mark->connector)) |
| 796 | fsnotify_recalc_mask(fsn_mark->connector); |
Jan Kara | c974764 | 2016-12-14 13:53:46 +0100 | [diff] [blame] | 797 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 798 | |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 799 | fsnotify_put_mark(fsn_mark); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 800 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 801 | } |
| 802 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 803 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
| 804 | struct vfsmount *mnt, __u32 mask, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 805 | unsigned int flags, __kernel_fsid_t *fsid) |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 806 | { |
| 807 | return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 808 | FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid); |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 809 | } |
| 810 | |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 811 | static int fanotify_add_sb_mark(struct fsnotify_group *group, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 812 | struct super_block *sb, __u32 mask, |
| 813 | unsigned int flags, __kernel_fsid_t *fsid) |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 814 | { |
| 815 | return fanotify_add_mark(group, &sb->s_fsnotify_marks, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 816 | FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 817 | } |
| 818 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 819 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 820 | struct inode *inode, __u32 mask, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 821 | unsigned int flags, __kernel_fsid_t *fsid) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 822 | { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 823 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 824 | |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 825 | /* |
| 826 | * If some other task has this inode open for write we should not add |
| 827 | * an ignored mark, unless that ignored mark is supposed to survive |
| 828 | * modification changes anyway. |
| 829 | */ |
| 830 | if ((flags & FAN_MARK_IGNORED_MASK) && |
| 831 | !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && |
Nikolay Borisov | ac9498d | 2018-12-11 10:27:23 +0200 | [diff] [blame] | 832 | inode_is_open_for_write(inode)) |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 833 | return 0; |
| 834 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 835 | return fanotify_add_mark(group, &inode->i_fsnotify_marks, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 836 | FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 837 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 838 | |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame] | 839 | static struct fsnotify_event *fanotify_alloc_overflow_event(void) |
| 840 | { |
| 841 | struct fanotify_event *oevent; |
| 842 | |
| 843 | oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT); |
| 844 | if (!oevent) |
| 845 | return NULL; |
| 846 | |
| 847 | fanotify_init_event(oevent, 0, FS_Q_OVERFLOW); |
| 848 | oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW; |
| 849 | |
| 850 | return &oevent->fse; |
| 851 | } |
| 852 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 853 | /* fanotify syscalls */ |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 854 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 855 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 856 | struct fsnotify_group *group; |
| 857 | int f_flags, fd; |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 858 | struct user_struct *user; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 859 | |
Amir Goldstein | 96a71f2 | 2018-09-21 21:20:30 +0300 | [diff] [blame] | 860 | pr_debug("%s: flags=%x event_f_flags=%x\n", |
| 861 | __func__, flags, event_f_flags); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 862 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 863 | if (!capable(CAP_SYS_ADMIN)) |
Andreas Gruenbacher | a2f13ad | 2010-08-24 12:58:54 +0200 | [diff] [blame] | 864 | return -EPERM; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 865 | |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 866 | #ifdef CONFIG_AUDITSYSCALL |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 867 | if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT)) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 868 | #else |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 869 | if (flags & ~FANOTIFY_INIT_FLAGS) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 870 | #endif |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 871 | return -EINVAL; |
| 872 | |
Heinrich Schuchardt | 48149e9 | 2014-06-04 16:05:44 -0700 | [diff] [blame] | 873 | if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS) |
| 874 | return -EINVAL; |
| 875 | |
| 876 | switch (event_f_flags & O_ACCMODE) { |
| 877 | case O_RDONLY: |
| 878 | case O_RDWR: |
| 879 | case O_WRONLY: |
| 880 | break; |
| 881 | default: |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame^] | 885 | if ((flags & FANOTIFY_FID_BITS) && |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 886 | (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF) |
| 887 | return -EINVAL; |
| 888 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 889 | user = get_current_user(); |
| 890 | if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) { |
| 891 | free_uid(user); |
| 892 | return -EMFILE; |
| 893 | } |
| 894 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 895 | f_flags = O_RDWR | FMODE_NONOTIFY; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 896 | if (flags & FAN_CLOEXEC) |
| 897 | f_flags |= O_CLOEXEC; |
| 898 | if (flags & FAN_NONBLOCK) |
| 899 | f_flags |= O_NONBLOCK; |
| 900 | |
| 901 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 902 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 903 | if (IS_ERR(group)) { |
| 904 | free_uid(user); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 905 | return PTR_ERR(group); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 906 | } |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 907 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 908 | group->fanotify_data.user = user; |
Amir Goldstein | 96a71f2 | 2018-09-21 21:20:30 +0300 | [diff] [blame] | 909 | group->fanotify_data.flags = flags; |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 910 | atomic_inc(&user->fanotify_listeners); |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 911 | group->memcg = get_mem_cgroup_from_mm(current->mm); |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 912 | |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame] | 913 | group->overflow_event = fanotify_alloc_overflow_event(); |
| 914 | if (unlikely(!group->overflow_event)) { |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 915 | fd = -ENOMEM; |
| 916 | goto out_destroy_group; |
| 917 | } |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 918 | |
Will Woods | 1e2ee49 | 2014-05-06 12:50:10 -0700 | [diff] [blame] | 919 | if (force_o_largefile()) |
| 920 | event_f_flags |= O_LARGEFILE; |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 921 | group->fanotify_data.f_flags = event_f_flags; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 922 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
| 923 | INIT_LIST_HEAD(&group->fanotify_data.access_list); |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 924 | switch (flags & FANOTIFY_CLASS_BITS) { |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 925 | case FAN_CLASS_NOTIF: |
| 926 | group->priority = FS_PRIO_0; |
| 927 | break; |
| 928 | case FAN_CLASS_CONTENT: |
| 929 | group->priority = FS_PRIO_1; |
| 930 | break; |
| 931 | case FAN_CLASS_PRE_CONTENT: |
| 932 | group->priority = FS_PRIO_2; |
| 933 | break; |
| 934 | default: |
| 935 | fd = -EINVAL; |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 936 | goto out_destroy_group; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 937 | } |
Eric Paris | cb2d429 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 938 | |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 939 | if (flags & FAN_UNLIMITED_QUEUE) { |
| 940 | fd = -EPERM; |
| 941 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 942 | goto out_destroy_group; |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 943 | group->max_events = UINT_MAX; |
| 944 | } else { |
| 945 | group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS; |
| 946 | } |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 947 | |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 948 | if (flags & FAN_UNLIMITED_MARKS) { |
| 949 | fd = -EPERM; |
| 950 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 951 | goto out_destroy_group; |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 952 | group->fanotify_data.max_marks = UINT_MAX; |
| 953 | } else { |
| 954 | group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS; |
| 955 | } |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 956 | |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 957 | if (flags & FAN_ENABLE_AUDIT) { |
| 958 | fd = -EPERM; |
| 959 | if (!capable(CAP_AUDIT_WRITE)) |
| 960 | goto out_destroy_group; |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 961 | } |
| 962 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 963 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 964 | if (fd < 0) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 965 | goto out_destroy_group; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 966 | |
| 967 | return fd; |
| 968 | |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 969 | out_destroy_group: |
| 970 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 971 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 972 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 973 | |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 974 | /* Check if filesystem can encode a unique fid */ |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 975 | static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid) |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 976 | { |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 977 | __kernel_fsid_t root_fsid; |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 978 | int err; |
| 979 | |
| 980 | /* |
| 981 | * Make sure path is not in filesystem with zero fsid (e.g. tmpfs). |
| 982 | */ |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 983 | err = vfs_get_fsid(path->dentry, fsid); |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 984 | if (err) |
| 985 | return err; |
| 986 | |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 987 | if (!fsid->val[0] && !fsid->val[1]) |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 988 | return -ENODEV; |
| 989 | |
| 990 | /* |
| 991 | * Make sure path is not inside a filesystem subvolume (e.g. btrfs) |
| 992 | * which uses a different fsid than sb root. |
| 993 | */ |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 994 | err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid); |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 995 | if (err) |
| 996 | return err; |
| 997 | |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 998 | if (root_fsid.val[0] != fsid->val[0] || |
| 999 | root_fsid.val[1] != fsid->val[1]) |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1000 | return -EXDEV; |
| 1001 | |
| 1002 | /* |
| 1003 | * We need to make sure that the file system supports at least |
| 1004 | * encoding a file handle so user can use name_to_handle_at() to |
| 1005 | * compare fid returned with event to the file handle of watched |
| 1006 | * objects. However, name_to_handle_at() requires that the |
| 1007 | * filesystem also supports decoding file handles. |
| 1008 | */ |
| 1009 | if (!path->dentry->d_sb->s_export_op || |
| 1010 | !path->dentry->d_sb->s_export_op->fh_to_dentry) |
| 1011 | return -EOPNOTSUPP; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
Jan Kara | 0b3b094 | 2019-05-15 16:28:34 +0200 | [diff] [blame] | 1016 | static int fanotify_events_supported(struct path *path, __u64 mask) |
| 1017 | { |
| 1018 | /* |
| 1019 | * Some filesystems such as 'proc' acquire unusual locks when opening |
| 1020 | * files. For them fanotify permission events have high chances of |
| 1021 | * deadlocking the system - open done when reporting fanotify event |
| 1022 | * blocks on this "unusual" lock while another process holding the lock |
| 1023 | * waits for fanotify permission event to be answered. Just disallow |
| 1024 | * permission events for such filesystems. |
| 1025 | */ |
| 1026 | if (mask & FANOTIFY_PERM_EVENTS && |
| 1027 | path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM) |
| 1028 | return -EINVAL; |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 1032 | static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, |
| 1033 | int dfd, const char __user *pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1034 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1035 | struct inode *inode = NULL; |
| 1036 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1037 | struct fsnotify_group *group; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1038 | struct fd f; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1039 | struct path path; |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1040 | __kernel_fsid_t __fsid, *fsid = NULL; |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 1041 | u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1042 | unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame^] | 1043 | unsigned int obj_type, fid_mode; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1044 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1045 | |
| 1046 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 1047 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 1048 | |
| 1049 | /* we only use the lower 32 bits as of right now. */ |
| 1050 | if (mask & ((__u64)0xffffffff << 32)) |
| 1051 | return -EINVAL; |
| 1052 | |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1053 | if (flags & ~FANOTIFY_MARK_FLAGS) |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1054 | return -EINVAL; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1055 | |
| 1056 | switch (mark_type) { |
| 1057 | case FAN_MARK_INODE: |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1058 | obj_type = FSNOTIFY_OBJ_TYPE_INODE; |
| 1059 | break; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1060 | case FAN_MARK_MOUNT: |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1061 | obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT; |
| 1062 | break; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1063 | case FAN_MARK_FILESYSTEM: |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1064 | obj_type = FSNOTIFY_OBJ_TYPE_SB; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1065 | break; |
| 1066 | default: |
| 1067 | return -EINVAL; |
| 1068 | } |
| 1069 | |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 1070 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 1071 | case FAN_MARK_ADD: /* fallthrough */ |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1072 | case FAN_MARK_REMOVE: |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 1073 | if (!mask) |
| 1074 | return -EINVAL; |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 1075 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 1076 | case FAN_MARK_FLUSH: |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1077 | if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH)) |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 1078 | return -EINVAL; |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1079 | break; |
| 1080 | default: |
| 1081 | return -EINVAL; |
| 1082 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 1083 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1084 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1085 | valid_mask |= FANOTIFY_PERM_EVENTS; |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1086 | |
| 1087 | if (mask & ~valid_mask) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1088 | return -EINVAL; |
| 1089 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1090 | f = fdget(fanotify_fd); |
| 1091 | if (unlikely(!f.file)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1092 | return -EBADF; |
| 1093 | |
| 1094 | /* verify that this is indeed an fanotify instance */ |
| 1095 | ret = -EINVAL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1096 | if (unlikely(f.file->f_op != &fanotify_fops)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1097 | goto fput_and_out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1098 | group = f.file->private_data; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 1099 | |
| 1100 | /* |
| 1101 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not |
| 1102 | * allowed to set permissions events. |
| 1103 | */ |
| 1104 | ret = -EINVAL; |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1105 | if (mask & FANOTIFY_PERM_EVENTS && |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 1106 | group->priority == FS_PRIO_0) |
| 1107 | goto fput_and_out; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1108 | |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 1109 | /* |
| 1110 | * Events with data type inode do not carry enough information to report |
| 1111 | * event->fd, so we do not allow setting a mask for inode events unless |
| 1112 | * group supports reporting fid. |
| 1113 | * inode events are not supported on a mount mark, because they do not |
| 1114 | * carry enough information (i.e. path) to be filtered by mount point. |
| 1115 | */ |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame^] | 1116 | fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 1117 | if (mask & FANOTIFY_INODE_EVENTS && |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame^] | 1118 | (!fid_mode || mark_type == FAN_MARK_MOUNT)) |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 1119 | goto fput_and_out; |
| 1120 | |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1121 | if (flags & FAN_MARK_FLUSH) { |
| 1122 | ret = 0; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1123 | if (mark_type == FAN_MARK_MOUNT) |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1124 | fsnotify_clear_vfsmount_marks_by_group(group); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1125 | else if (mark_type == FAN_MARK_FILESYSTEM) |
| 1126 | fsnotify_clear_sb_marks_by_group(group); |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1127 | else |
| 1128 | fsnotify_clear_inode_marks_by_group(group); |
| 1129 | goto fput_and_out; |
| 1130 | } |
| 1131 | |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1132 | ret = fanotify_find_path(dfd, pathname, &path, flags, |
| 1133 | (mask & ALL_FSNOTIFY_EVENTS), obj_type); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1134 | if (ret) |
| 1135 | goto fput_and_out; |
| 1136 | |
Jan Kara | 0b3b094 | 2019-05-15 16:28:34 +0200 | [diff] [blame] | 1137 | if (flags & FAN_MARK_ADD) { |
| 1138 | ret = fanotify_events_supported(&path, mask); |
| 1139 | if (ret) |
| 1140 | goto path_put_and_out; |
| 1141 | } |
| 1142 | |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame^] | 1143 | if (fid_mode) { |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1144 | ret = fanotify_test_fid(&path, &__fsid); |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1145 | if (ret) |
| 1146 | goto path_put_and_out; |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1147 | |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1148 | fsid = &__fsid; |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1151 | /* 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] | 1152 | if (mark_type == FAN_MARK_INODE) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1153 | inode = path.dentry->d_inode; |
| 1154 | else |
| 1155 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1156 | |
| 1157 | /* create/update an inode mark */ |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1158 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1159 | case FAN_MARK_ADD: |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1160 | if (mark_type == FAN_MARK_MOUNT) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1161 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, |
| 1162 | flags, fsid); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1163 | else if (mark_type == FAN_MARK_FILESYSTEM) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1164 | ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, |
| 1165 | flags, fsid); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1166 | else |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1167 | ret = fanotify_add_inode_mark(group, inode, mask, |
| 1168 | flags, fsid); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1169 | break; |
| 1170 | case FAN_MARK_REMOVE: |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1171 | if (mark_type == FAN_MARK_MOUNT) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1172 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, |
| 1173 | flags); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1174 | else if (mark_type == FAN_MARK_FILESYSTEM) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1175 | ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, |
| 1176 | flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1177 | else |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1178 | ret = fanotify_remove_inode_mark(group, inode, mask, |
| 1179 | flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1180 | break; |
| 1181 | default: |
| 1182 | ret = -EINVAL; |
| 1183 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1184 | |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1185 | path_put_and_out: |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1186 | path_put(&path); |
| 1187 | fput_and_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1188 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1189 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1190 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1191 | |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 1192 | SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, |
| 1193 | __u64, mask, int, dfd, |
| 1194 | const char __user *, pathname) |
| 1195 | { |
| 1196 | return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname); |
| 1197 | } |
| 1198 | |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 1199 | #ifdef CONFIG_COMPAT |
| 1200 | COMPAT_SYSCALL_DEFINE6(fanotify_mark, |
| 1201 | int, fanotify_fd, unsigned int, flags, |
| 1202 | __u32, mask0, __u32, mask1, int, dfd, |
| 1203 | const char __user *, pathname) |
| 1204 | { |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 1205 | return do_fanotify_mark(fanotify_fd, flags, |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 1206 | #ifdef __BIG_ENDIAN |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 1207 | ((__u64)mask0 << 32) | mask1, |
Heiko Carstens | 592f6b8 | 2014-01-27 17:07:19 -0800 | [diff] [blame] | 1208 | #else |
| 1209 | ((__u64)mask1 << 32) | mask0, |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 1210 | #endif |
| 1211 | dfd, pathname); |
| 1212 | } |
| 1213 | #endif |
| 1214 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1215 | /* |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 1216 | * fanotify_user_setup - Our initialization function. Note that we cannot return |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1217 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 1218 | * must result in panic(). |
| 1219 | */ |
| 1220 | static int __init fanotify_user_setup(void) |
| 1221 | { |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1222 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8); |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 1223 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); |
| 1224 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 1225 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, |
| 1226 | SLAB_PANIC|SLAB_ACCOUNT); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 1227 | fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event, |
| 1228 | SLAB_PANIC); |
| 1229 | fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event, |
| 1230 | SLAB_PANIC); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1231 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) { |
| 1232 | fanotify_perm_event_cachep = |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 1233 | KMEM_CACHE(fanotify_perm_event, SLAB_PANIC); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1234 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1235 | |
| 1236 | return 0; |
| 1237 | } |
| 1238 | device_initcall(fanotify_user_setup); |