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