blob: 1e04caf8d6bafedca4131d659724af25671bd3d1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05002#include <linux/fanotify.h>
Eric Paris11637e42009-12-17 21:24:25 -05003#include <linux/fcntl.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05004#include <linux/file.h>
Eric Paris11637e42009-12-17 21:24:25 -05005#include <linux/fs.h>
Eric Paris52c923d2009-12-17 21:24:26 -05006#include <linux/anon_inodes.h>
Eric Paris11637e42009-12-17 21:24:25 -05007#include <linux/fsnotify_backend.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05008#include <linux/init.h>
Eric Parisa1014f12009-12-17 21:24:26 -05009#include <linux/mount.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050010#include <linux/namei.h>
Eric Parisa1014f12009-12-17 21:24:26 -050011#include <linux/poll.h>
Eric Paris11637e42009-12-17 21:24:25 -050012#include <linux/security.h>
13#include <linux/syscalls.h>
Tejun Heoe4e047a2010-05-20 01:36:28 +100014#include <linux/slab.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050015#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050016#include <linux/uaccess.h>
Al Viro91c2e0b2013-03-05 20:10:59 -050017#include <linux/compat.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
Shakeel Buttd46eb14b2018-08-17 15:46:39 -070019#include <linux/memcontrol.h>
Amir Goldsteina8b13aa2019-01-10 19:04:36 +020020#include <linux/statfs.h>
21#include <linux/exportfs.h>
Eric Parisa1014f12009-12-17 21:24:26 -050022
23#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050024
Al Viroc63181e2011-11-25 02:35:16 -050025#include "../../mount.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080026#include "../fdinfo.h"
Jan Kara7053aee2014-01-21 15:48:14 -080027#include "fanotify.h"
Al Viroc63181e2011-11-25 02:35:16 -050028
Eric Paris2529a0d2010-10-28 17:21:57 -040029#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Eric Parise7099d82010-10-28 17:21:57 -040030#define FANOTIFY_DEFAULT_MAX_MARKS 8192
Eric Paris4afeff82010-10-28 17:21:58 -040031#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
Eric Paris2529a0d2010-10-28 17:21:57 -040032
Heinrich Schuchardt48149e92014-06-04 16:05:44 -070033/*
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 Gruenbacher33d3dff2009-12-17 21:24:29 -050046extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050047
Jan Kara054c6362016-12-21 18:06:12 +010048struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7088f352020-03-24 17:04:20 +010049struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
50struct kmem_cache *fanotify_path_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070051struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050052
Amir Goldstein5e469c82019-01-10 19:04:35 +020053#define FANOTIFY_EVENT_ALIGN 4
Amir Goldstein44d705b2020-03-19 17:10:22 +020054#define FANOTIFY_INFO_HDR_LEN \
55 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
Amir Goldstein5e469c82019-01-10 19:04:35 +020056
Amir Goldstein44d705b2020-03-19 17:10:22 +020057static int fanotify_fid_info_len(int fh_len, int name_len)
Amir Goldsteind766b552020-03-19 17:10:20 +020058{
Amir Goldstein44d705b2020-03-19 17:10:22 +020059 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 Goldsteind766b552020-03-19 17:10:20 +020065}
66
Amir Goldstein5e469c82019-01-10 19:04:35 +020067static int fanotify_event_info_len(struct fanotify_event *event)
68{
Amir Goldstein44d705b2020-03-19 17:10:22 +020069 int info_len = 0;
Jan Karaafc894c2020-03-24 16:55:37 +010070 int fh_len = fanotify_event_object_fh_len(event);
71
Amir Goldstein44d705b2020-03-19 17:10:22 +020072 if (fh_len)
73 info_len += fanotify_fid_info_len(fh_len, 0);
Amir Goldstein5e469c82019-01-10 19:04:35 +020074
Amir Goldstein44d705b2020-03-19 17:10:22 +020075 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 Goldstein5e469c82019-01-10 19:04:35 +020083}
84
Eric Parisa1014f12009-12-17 21:24:26 -050085/*
Jan Kara7088f352020-03-24 17:04:20 +010086 * Get an fanotify notification event if one exists and is small
Eric Parisa1014f12009-12-17 21:24:26 -050087 * enough to fit in "count". Return an error pointer if the count
Jan Kara40873282019-01-08 14:02:44 +010088 * is not large enough. When permission event is dequeued, its state is
89 * updated accordingly.
Eric Parisa1014f12009-12-17 21:24:26 -050090 */
Jan Kara7088f352020-03-24 17:04:20 +010091static struct fanotify_event *get_one_event(struct fsnotify_group *group,
Eric Parisa1014f12009-12-17 21:24:26 -050092 size_t count)
93{
Amir Goldstein5e469c82019-01-10 19:04:35 +020094 size_t event_size = FAN_EVENT_METADATA_LEN;
Jan Kara7088f352020-03-24 17:04:20 +010095 struct fanotify_event *event = NULL;
Eric Parisa1014f12009-12-17 21:24:26 -050096
97 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
98
Jan Kara8c554462019-01-08 13:52:31 +010099 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500100 if (fsnotify_notify_queue_is_empty(group))
Jan Kara8c554462019-01-08 13:52:31 +0100101 goto out;
Eric Parisa1014f12009-12-17 21:24:26 -0500102
Amir Goldsteind809daf2020-07-16 11:42:12 +0300103 if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
Jan Kara8c554462019-01-08 13:52:31 +0100104 event_size += fanotify_event_info_len(
105 FANOTIFY_E(fsnotify_peek_first_event(group)));
Amir Goldstein5e469c82019-01-10 19:04:35 +0200106 }
107
Jan Kara8c554462019-01-08 13:52:31 +0100108 if (event_size > count) {
Jan Kara7088f352020-03-24 17:04:20 +0100109 event = ERR_PTR(-EINVAL);
Jan Kara8c554462019-01-08 13:52:31 +0100110 goto out;
111 }
Jan Kara7088f352020-03-24 17:04:20 +0100112 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 Kara8c554462019-01-08 13:52:31 +0100115out:
116 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100117 return event;
Eric Parisa1014f12009-12-17 21:24:26 -0500118}
119
Jan Karaa741c2f2020-03-24 15:27:52 +0100120static int create_fd(struct fsnotify_group *group, struct path *path,
Jan Kara7053aee2014-01-21 15:48:14 -0800121 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500122{
123 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500124 struct file *new_file;
125
Yann Droneaud0b37e092014-10-09 15:24:40 -0700126 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -0500127 if (client_fd < 0)
128 return client_fd;
129
Eric Parisa1014f12009-12-17 21:24:26 -0500130 /*
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 Karaa741c2f2020-03-24 15:27:52 +0100134 new_file = dentry_open(path,
135 group->fanotify_data.f_flags | FMODE_NONOTIFY,
136 current_cred());
Eric Parisa1014f12009-12-17 21:24:26 -0500137 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 Viro352e3b22012-08-19 12:30:45 -0400148 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500149 }
150
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500151 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500152}
153
Jan Kara40873282019-01-08 14:02:44 +0100154/*
155 * Finish processing of permission event by setting it to ANSWERED state and
156 * drop group->notification_lock.
157 */
158static 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 Karafabf7f22019-01-08 15:18:02 +0100163 bool destroy = false;
164
Jan Kara40873282019-01-08 14:02:44 +0100165 assert_spin_locked(&group->notification_lock);
166 event->response = response;
Jan Karafabf7f22019-01-08 15:18:02 +0100167 if (event->state == FAN_EVENT_CANCELED)
168 destroy = true;
169 else
170 event->state = FAN_EVENT_ANSWERED;
Jan Kara40873282019-01-08 14:02:44 +0100171 spin_unlock(&group->notification_lock);
Jan Karafabf7f22019-01-08 15:18:02 +0100172 if (destroy)
173 fsnotify_destroy_event(group, &event->fae.fse);
Jan Kara40873282019-01-08 14:02:44 +0100174}
175
Eric Parisb2d87902009-12-17 21:24:34 -0500176static int process_access_response(struct fsnotify_group *group,
177 struct fanotify_response *response_struct)
178{
Amir Goldstein33913992019-01-10 19:04:32 +0200179 struct fanotify_perm_event *event;
Jan Karaf0834412014-04-03 14:46:33 -0700180 int fd = response_struct->fd;
181 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500182
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 Marchi25985ed2011-03-30 22:57:33 -0300187 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500188 * timeout
189 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400190 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500191 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 Goldstein96a71f22018-09-21 21:20:30 +0300201 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400202 return -EINVAL;
203
Jan Karaaf6a5112019-01-08 13:28:18 +0100204 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 Parisb2d87902009-12-17 21:24:34 -0500209
Jan Karaaf6a5112019-01-08 13:28:18 +0100210 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100211 finish_permission_event(group, event, response);
Jan Karaaf6a5112019-01-08 13:28:18 +0100212 wake_up(&group->fanotify_data.access_waitq);
213 return 0;
214 }
215 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500216
Jan Karaaf6a5112019-01-08 13:28:18 +0100217 return -ENOENT;
Eric Parisb2d87902009-12-17 21:24:34 -0500218}
Eric Parisb2d87902009-12-17 21:24:34 -0500219
Amir Goldstein44d705b2020-03-19 17:10:22 +0200220static 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 Goldstein5e469c82019-01-10 19:04:35 +0200223{
224 struct fanotify_event_info_fid info = { };
225 struct file_handle handle = { };
Jan Karaafc894c2020-03-24 16:55:37 +0100226 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
Amir Goldsteincacfb952020-03-19 17:10:21 +0200227 size_t fh_len = fh ? fh->len : 0;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200228 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
229 size_t len = info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200230
Amir Goldstein44d705b2020-03-19 17:10:22 +0200231 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 Goldstein5e469c82019-01-10 19:04:35 +0200235 return 0;
236
Amir Goldstein44d705b2020-03-19 17:10:22 +0200237 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200238 return -EFAULT;
239
Amir Goldstein44d705b2020-03-19 17:10:22 +0200240 /*
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 Goldstein5e469c82019-01-10 19:04:35 +0200246 info.hdr.len = len;
Amir Goldsteind766b552020-03-19 17:10:20 +0200247 info.fsid = *fsid;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200248 if (copy_to_user(buf, &info, sizeof(info)))
249 return -EFAULT;
250
251 buf += sizeof(info);
252 len -= sizeof(info);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200253 if (WARN_ON_ONCE(len < sizeof(handle)))
254 return -EFAULT;
255
Jan Karaafc894c2020-03-24 16:55:37 +0100256 handle.handle_type = fh->type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200257 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 Goldstein44d705b2020-03-19 17:10:22 +0200263 if (WARN_ON_ONCE(len < fh_len))
264 return -EFAULT;
265
Jan Karab2d22b62019-03-12 12:42:37 +0100266 /*
Amir Goldstein44d705b2020-03-19 17:10:22 +0200267 * For an inline fh and inline file name, copy through stack to exclude
268 * the copy from usercopy hardening protections.
Jan Karab2d22b62019-03-12 12:42:37 +0100269 */
Jan Karaafc894c2020-03-24 16:55:37 +0100270 fh_buf = fanotify_fh_buf(fh);
Jan Karab2d22b62019-03-12 12:42:37 +0100271 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
Jan Karaafc894c2020-03-24 16:55:37 +0100272 memcpy(bounce, fh_buf, fh_len);
273 fh_buf = bounce;
Jan Karab2d22b62019-03-12 12:42:37 +0100274 }
Jan Karaafc894c2020-03-24 16:55:37 +0100275 if (copy_to_user(buf, fh_buf, fh_len))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200276 return -EFAULT;
277
Amir Goldstein5e469c82019-01-10 19:04:35 +0200278 buf += fh_len;
279 len -= fh_len;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200280
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 Goldstein5e469c82019-01-10 19:04:35 +0200295 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
296 if (len > 0 && clear_user(buf, len))
297 return -EFAULT;
298
Amir Goldstein44d705b2020-03-19 17:10:22 +0200299 return info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200300}
301
Eric Parisa1014f12009-12-17 21:24:26 -0500302static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7088f352020-03-24 17:04:20 +0100303 struct fanotify_event *event,
Kees Cook5b03a472018-12-04 15:44:46 -0800304 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500305{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200306 struct fanotify_event_metadata metadata;
Jan Kara7088f352020-03-24 17:04:20 +0100307 struct path *path = fanotify_event_path(event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200308 struct file *f = NULL;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200309 int ret, fd = FAN_NOFD;
Eric Parisa1014f12009-12-17 21:24:26 -0500310
Jan Kara7088f352020-03-24 17:04:20 +0100311 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500312
Amir Goldstein44d705b2020-03-19 17:10:22 +0200313 metadata.event_len = FAN_EVENT_METADATA_LEN +
314 fanotify_event_info_len(event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200315 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 Parisa1014f12009-12-17 21:24:26 -0500320
Amir Goldstein44d705b2020-03-19 17:10:22 +0200321 if (path && path->mnt && path->dentry) {
Jan Karaafc894c2020-03-24 16:55:37 +0100322 fd = create_fd(group, path, &f);
323 if (fd < 0)
324 return fd;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200325 }
326 metadata.fd = fd;
327
Al Viro352e3b22012-08-19 12:30:45 -0400328 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800329 /*
330 * Sanity check copy size in case get_one_event() and
Fabian Frederickc5e443c2020-05-12 20:18:36 +0200331 * event_len sizes ever get out of sync.
Kees Cook5b03a472018-12-04 15:44:46 -0800332 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200333 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400334 goto out_close_fd;
335
Amir Goldstein5e469c82019-01-10 19:04:35 +0200336 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200337 goto out_close_fd;
338
Amir Goldstein44d705b2020-03-19 17:10:22 +0200339 buf += FAN_EVENT_METADATA_LEN;
340 count -= FAN_EVENT_METADATA_LEN;
341
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200342 if (fanotify_is_perm_event(event->mask))
Jan Kara7088f352020-03-24 17:04:20 +0100343 FANOTIFY_PERM(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500344
Amir Goldstein44d705b2020-03-19 17:10:22 +0200345 if (f)
Al Viro3587b1b2012-11-18 19:19:00 +0000346 fd_install(fd, f);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200347
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 Goldstein5e469c82019-01-10 19:04:35 +0200356 if (ret < 0)
357 return ret;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200358
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 Goldstein5e469c82019-01-10 19:04:35 +0200372 }
373
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200374 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500375
Eric Parisb2d87902009-12-17 21:24:34 -0500376out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400377 if (fd != FAN_NOFD) {
378 put_unused_fd(fd);
379 fput(f);
380 }
Eric Parisb2d87902009-12-17 21:24:34 -0500381 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500382}
383
384/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400385static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500386{
387 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400388 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500389
390 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700391 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500392 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800393 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700394 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500395
396 return ret;
397}
398
399static ssize_t fanotify_read(struct file *file, char __user *buf,
400 size_t count, loff_t *pos)
401{
402 struct fsnotify_group *group;
Jan Kara7088f352020-03-24 17:04:20 +0100403 struct fanotify_event *event;
Eric Parisa1014f12009-12-17 21:24:26 -0500404 char __user *start;
405 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100406 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500407
408 start = buf;
409 group = file->private_data;
410
411 pr_debug("%s: group=%p\n", __func__, group);
412
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100413 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500414 while (1) {
Jan Kara47aaabd2020-07-15 14:06:21 +0200415 /*
416 * User can supply arbitrarily large buffer. Avoid softlockups
417 * in case there are lots of available events.
418 */
419 cond_resched();
Jan Kara7088f352020-03-24 17:04:20 +0100420 event = get_one_event(group, count);
421 if (IS_ERR(event)) {
422 ret = PTR_ERR(event);
Jan Karad8aaab42014-04-03 14:46:35 -0700423 break;
424 }
425
Jan Kara7088f352020-03-24 17:04:20 +0100426 if (!event) {
Jan Karad8aaab42014-04-03 14:46:35 -0700427 ret = -EAGAIN;
428 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500429 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700430
431 ret = -ERESTARTSYS;
432 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500433 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700434
435 if (start != buf)
436 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100437
438 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500439 continue;
440 }
441
Jan Kara7088f352020-03-24 17:04:20 +0100442 ret = copy_event_to_user(group, event, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300443 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 Karad8aaab42014-04-03 14:46:35 -0700453 /*
454 * Permission events get queued to wait for response. Other
455 * events can be destroyed now.
456 */
Jan Kara7088f352020-03-24 17:04:20 +0100457 if (!fanotify_is_perm_event(event->mask)) {
458 fsnotify_destroy_event(group, &event->fse);
Jan Karad5078162014-04-03 14:46:36 -0700459 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300460 if (ret <= 0) {
Jan Kara40873282019-01-08 14:02:44 +0100461 spin_lock(&group->notification_lock);
462 finish_permission_event(group,
Jan Kara7088f352020-03-24 17:04:20 +0100463 FANOTIFY_PERM(event), FAN_DENY);
Jan Karad5078162014-04-03 14:46:36 -0700464 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300465 } else {
466 spin_lock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100467 list_add_tail(&event->fse.list,
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300468 &group->fanotify_data.access_list);
469 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700470 }
Jan Karad5078162014-04-03 14:46:36 -0700471 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300472 if (ret < 0)
473 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700474 buf += ret;
475 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500476 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100477 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500478
Eric Parisa1014f12009-12-17 21:24:26 -0500479 if (start != buf && ret != -EFAULT)
480 ret = buf - start;
481 return ret;
482}
483
Eric Parisb2d87902009-12-17 21:24:34 -0500484static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
485{
Eric Parisb2d87902009-12-17 21:24:34 -0500486 struct fanotify_response response = { .fd = -1, .response = -1 };
487 struct fsnotify_group *group;
488 int ret;
489
Miklos Szeredi6685df32017-10-30 21:14:56 +0100490 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
491 return -EINVAL;
492
Eric Parisb2d87902009-12-17 21:24:34 -0500493 group = file->private_data;
494
Fabian Frederick5e236632020-05-12 20:19:21 +0200495 if (count < sizeof(response))
496 return -EINVAL;
497
498 count = sizeof(response);
Eric Parisb2d87902009-12-17 21:24:34 -0500499
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 Parisb2d87902009-12-17 21:24:34 -0500510}
511
Eric Paris52c923d2009-12-17 21:24:26 -0500512static int fanotify_release(struct inode *ignored, struct file *file)
513{
514 struct fsnotify_group *group = file->private_data;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400515
Jan Kara5838d442014-08-06 16:03:28 -0700516 /*
Jan Kara96d41012016-09-19 14:44:30 -0700517 * 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 Kara5838d442014-08-06 16:03:28 -0700526 */
Jan Kara073f6552016-10-07 16:56:55 -0700527 spin_lock(&group->notification_lock);
Jan Karaca6f8692019-01-09 13:21:01 +0100528 while (!list_empty(&group->fanotify_data.access_list)) {
Jan Kara7088f352020-03-24 17:04:20 +0100529 struct fanotify_perm_event *event;
530
Jan Karaca6f8692019-01-09 13:21:01 +0100531 event = list_first_entry(&group->fanotify_data.access_list,
532 struct fanotify_perm_event, fae.fse.list);
Jan Karaf0834412014-04-03 14:46:33 -0700533 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100534 finish_permission_event(group, event, FAN_ALLOW);
535 spin_lock(&group->notification_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400536 }
Eric Paris2eebf582010-08-18 12:25:50 -0400537
Jan Kara5838d442014-08-06 16:03:28 -0700538 /*
Jan Kara96d41012016-09-19 14:44:30 -0700539 * 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 Kara5838d442014-08-06 16:03:28 -0700542 */
Jan Kara96d41012016-09-19 14:44:30 -0700543 while (!fsnotify_notify_queue_is_empty(group)) {
Jan Kara7088f352020-03-24 17:04:20 +0100544 struct fanotify_event *event;
545
546 event = FANOTIFY_E(fsnotify_remove_first_event(group));
547 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700548 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100549 fsnotify_destroy_event(group, &event->fse);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100550 } else {
Jan Kara7088f352020-03-24 17:04:20 +0100551 finish_permission_event(group, FANOTIFY_PERM(event),
Jan Kara40873282019-01-08 14:02:44 +0100552 FAN_ALLOW);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100553 }
Jan Kara40873282019-01-08 14:02:44 +0100554 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700555 }
Jan Karac21dbe22016-10-07 16:56:52 -0700556 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700557
558 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400559 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400560
Eric Paris52c923d2009-12-17 21:24:26 -0500561 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200562 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500563
564 return 0;
565}
566
Eric Parisa1014f12009-12-17 21:24:26 -0500567static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
568{
569 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800570 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500571 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 Karac21dbe22016-10-07 16:56:52 -0700581 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800582 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500583 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700584 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500585 ret = put_user(send_len, (int __user *) p);
586 break;
587 }
588
589 return ret;
590}
591
Eric Paris52c923d2009-12-17 21:24:26 -0500592static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800593 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500594 .poll = fanotify_poll,
595 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500596 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500597 .fasync = NULL,
598 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500599 .unlocked_ioctl = fanotify_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200600 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200601 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500602};
603
Eric Paris2a3edf82009-12-17 21:24:26 -0500604static int fanotify_find_path(int dfd, const char __user *filename,
Aaron Goidelac5656d2019-08-12 11:20:00 -0400605 struct path *path, unsigned int flags, __u64 mask,
606 unsigned int obj_type)
Eric Paris2a3edf82009-12-17 21:24:26 -0500607{
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 Viro2903ff02012-08-28 12:52:22 -0400614 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500615
616 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400617 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500618 goto out;
619
620 ret = -ENOTDIR;
621 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500622 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400623 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500624 goto out;
625 }
626
Al Viro2903ff02012-08-28 12:52:22 -0400627 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500628 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400629 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500630 } 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 Goidelac5656d2019-08-12 11:20:00 -0400645 if (ret) {
646 path_put(path);
647 goto out;
648 }
649
650 ret = security_path_notify(path, mask, obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -0500651 if (ret)
652 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400653
Eric Paris2a3edf82009-12-17 21:24:26 -0500654out:
655 return ret;
656}
657
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500658static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
659 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200660 unsigned int flags,
661 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500662{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800663 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500664
665 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500666 if (!(flags & FAN_MARK_IGNORED_MASK)) {
667 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300668 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500669 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300670 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500671 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800672 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500673 spin_unlock(&fsn_mark->lock);
674
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500675 return mask & oldmask;
676}
677
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300678static int fanotify_remove_mark(struct fsnotify_group *group,
679 fsnotify_connp_t *connp, __u32 mask,
680 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500681{
682 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500683 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200684 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500685
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700686 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300687 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700688 if (!fsn_mark) {
689 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500690 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700691 }
Eric Paris88826272009-12-17 21:24:28 -0500692
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200693 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
694 &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300695 if (removed & fsnotify_conn_mask(fsn_mark->connector))
696 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200697 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700698 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700699 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700700 if (destroy_mark)
701 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700702
Jan Karab1362ed2016-12-21 16:28:45 +0100703 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500704 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500705 return 0;
706}
707
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300708static 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 Goldsteind54f4fb2018-09-01 10:41:13 +0300716static 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 Goldsteineaa2c6b2018-06-23 17:54:51 +0300723static 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 Parisb9e4e3b2009-12-17 21:24:33 -0500731static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
732 __u32 mask,
733 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500734{
Eric Paris192ca4d2010-10-28 17:21:59 -0400735 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500736
737 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500738 if (!(flags & FAN_MARK_IGNORED_MASK)) {
739 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300740 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500741 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300742 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -0500743 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
744 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500745 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500746 spin_unlock(&fsn_mark->lock);
747
748 return mask & ~oldmask;
749}
750
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700751static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300752 fsnotify_connp_t *connp,
Amir Goldstein77115222019-01-10 19:04:37 +0200753 unsigned int type,
754 __kernel_fsid_t *fsid)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700755{
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 Kara054c6362016-12-21 18:06:12 +0100766 fsnotify_init_mark(mark, group);
Amir Goldstein77115222019-01-10 19:04:37 +0200767 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700768 if (ret) {
769 fsnotify_put_mark(mark);
770 return ERR_PTR(ret);
771 }
772
773 return mark;
774}
775
776
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300777static int fanotify_add_mark(struct fsnotify_group *group,
778 fsnotify_connp_t *connp, unsigned int type,
Amir Goldstein77115222019-01-10 19:04:37 +0200779 __u32 mask, unsigned int flags,
780 __kernel_fsid_t *fsid)
Eric Paris2a3edf82009-12-17 21:24:26 -0500781{
782 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500783 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500784
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700785 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300786 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500787 if (!fsn_mark) {
Amir Goldstein77115222019-01-10 19:04:37 +0200788 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700789 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700790 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700791 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700792 }
Eric Paris88826272009-12-17 21:24:28 -0500793 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500794 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300795 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
796 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100797 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700798
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100799 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700800 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500801}
802
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300803static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
804 struct vfsmount *mnt, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200805 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300806{
807 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200808 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300809}
810
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300811static int fanotify_add_sb_mark(struct fsnotify_group *group,
Amir Goldstein77115222019-01-10 19:04:37 +0200812 struct super_block *sb, __u32 mask,
813 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300814{
815 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200816 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300817}
818
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500819static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500820 struct inode *inode, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200821 unsigned int flags, __kernel_fsid_t *fsid)
Eric Paris88826272009-12-17 21:24:28 -0500822{
Eric Paris88826272009-12-17 21:24:28 -0500823 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500824
Eric Paris5322a592010-10-28 17:21:57 -0400825 /*
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 Borisovac9498d2018-12-11 10:27:23 +0200832 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -0400833 return 0;
834
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300835 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200836 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
Eric Paris88826272009-12-17 21:24:28 -0500837}
Eric Paris2a3edf82009-12-17 21:24:26 -0500838
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +0300839static 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 Paris52c923d2009-12-17 21:24:26 -0500853/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400854SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500855{
Eric Paris52c923d2009-12-17 21:24:26 -0500856 struct fsnotify_group *group;
857 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400858 struct user_struct *user;
Eric Paris52c923d2009-12-17 21:24:26 -0500859
Amir Goldstein96a71f22018-09-21 21:20:30 +0300860 pr_debug("%s: flags=%x event_f_flags=%x\n",
861 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500862
Eric Paris52c923d2009-12-17 21:24:26 -0500863 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200864 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500865
Steve Grubbde8cd832017-10-02 20:21:39 -0400866#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300867 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400868#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300869 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400870#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500871 return -EINVAL;
872
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700873 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 Goldsteind809daf2020-07-16 11:42:12 +0300885 if ((flags & FANOTIFY_FID_BITS) &&
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200886 (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
887 return -EINVAL;
888
Eric Paris4afeff82010-10-28 17:21:58 -0400889 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 Parisb2d87902009-12-17 21:24:34 -0500895 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500896 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 Paris26379192010-11-23 23:48:26 -0500903 if (IS_ERR(group)) {
904 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500905 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500906 }
Eric Paris52c923d2009-12-17 21:24:26 -0500907
Eric Paris4afeff82010-10-28 17:21:58 -0400908 group->fanotify_data.user = user;
Amir Goldstein96a71f22018-09-21 21:20:30 +0300909 group->fanotify_data.flags = flags;
Eric Paris4afeff82010-10-28 17:21:58 -0400910 atomic_inc(&user->fanotify_listeners);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700911 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -0400912
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +0300913 group->overflow_event = fanotify_alloc_overflow_event();
914 if (unlikely(!group->overflow_event)) {
Jan Karaff57cd52014-02-21 19:14:11 +0100915 fd = -ENOMEM;
916 goto out_destroy_group;
917 }
Jan Karaff57cd52014-02-21 19:14:11 +0100918
Will Woods1e2ee492014-05-06 12:50:10 -0700919 if (force_o_largefile())
920 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400921 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500922 init_waitqueue_head(&group->fanotify_data.access_waitq);
923 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300924 switch (flags & FANOTIFY_CLASS_BITS) {
Eric Paris4231a232010-10-28 17:21:56 -0400925 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 Sanfilippod8153d42011-06-14 17:29:45 +0200936 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400937 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500938
Eric Paris5dd03f52010-10-28 17:21:57 -0400939 if (flags & FAN_UNLIMITED_QUEUE) {
940 fd = -EPERM;
941 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200942 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400943 group->max_events = UINT_MAX;
944 } else {
945 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
946 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400947
Eric Parisac7e22d2010-10-28 17:21:58 -0400948 if (flags & FAN_UNLIMITED_MARKS) {
949 fd = -EPERM;
950 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200951 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400952 group->fanotify_data.max_marks = UINT_MAX;
953 } else {
954 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
955 }
Eric Parise7099d82010-10-28 17:21:57 -0400956
Steve Grubbde8cd832017-10-02 20:21:39 -0400957 if (flags & FAN_ENABLE_AUDIT) {
958 fd = -EPERM;
959 if (!capable(CAP_AUDIT_WRITE))
960 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -0400961 }
962
Eric Paris52c923d2009-12-17 21:24:26 -0500963 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
964 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200965 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500966
967 return fd;
968
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200969out_destroy_group:
970 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500971 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500972}
Eric Parisbbaa4162009-12-17 21:24:26 -0500973
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200974/* Check if filesystem can encode a unique fid */
Amir Goldstein73072282019-01-10 19:04:39 +0200975static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200976{
Amir Goldstein73072282019-01-10 19:04:39 +0200977 __kernel_fsid_t root_fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200978 int err;
979
980 /*
981 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
982 */
Amir Goldstein73072282019-01-10 19:04:39 +0200983 err = vfs_get_fsid(path->dentry, fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200984 if (err)
985 return err;
986
Amir Goldstein73072282019-01-10 19:04:39 +0200987 if (!fsid->val[0] && !fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200988 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 Goldstein73072282019-01-10 19:04:39 +0200994 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200995 if (err)
996 return err;
997
Amir Goldstein73072282019-01-10 19:04:39 +0200998 if (root_fsid.val[0] != fsid->val[0] ||
999 root_fsid.val[1] != fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001000 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 Kara0b3b0942019-05-15 16:28:34 +02001016static 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 Brodowski183caa32018-03-17 15:06:11 +01001032static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1033 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -05001034{
Eric Paris0ff21db2009-12-17 21:24:29 -05001035 struct inode *inode = NULL;
1036 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -05001037 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -04001038 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -05001039 struct path path;
Amir Goldstein73072282019-01-10 19:04:39 +02001040 __kernel_fsid_t __fsid, *fsid = NULL;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001041 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001042 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Amir Goldsteind809daf2020-07-16 11:42:12 +03001043 unsigned int obj_type, fid_mode;
Al Viro2903ff02012-08-28 12:52:22 -04001044 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -05001045
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 Goldstein23c9dee2018-10-04 00:25:35 +03001053 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001054 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001055
1056 switch (mark_type) {
1057 case FAN_MARK_INODE:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001058 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1059 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001060 case FAN_MARK_MOUNT:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001061 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1062 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001063 case FAN_MARK_FILESYSTEM:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001064 obj_type = FSNOTIFY_OBJ_TYPE_SB;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001065 break;
1066 default:
1067 return -EINVAL;
1068 }
1069
Eric Paris4d926042009-12-17 21:24:34 -05001070 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001071 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001072 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001073 if (!mask)
1074 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001075 break;
Eric Paris4d926042009-12-17 21:24:34 -05001076 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001077 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001078 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001079 break;
1080 default:
1081 return -EINVAL;
1082 }
Eric Paris8fcd6522010-10-28 17:21:59 -04001083
Miklos Szeredi6685df32017-10-30 21:14:56 +01001084 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001085 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +01001086
1087 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -05001088 return -EINVAL;
1089
Al Viro2903ff02012-08-28 12:52:22 -04001090 f = fdget(fanotify_fd);
1091 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -05001092 return -EBADF;
1093
1094 /* verify that this is indeed an fanotify instance */
1095 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -04001096 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -05001097 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -04001098 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -04001099
1100 /*
1101 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1102 * allowed to set permissions events.
1103 */
1104 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001105 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -04001106 group->priority == FS_PRIO_0)
1107 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -05001108
Amir Goldstein235328d2019-01-10 19:04:43 +02001109 /*
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 Goldsteind809daf2020-07-16 11:42:12 +03001116 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Amir Goldstein235328d2019-01-10 19:04:43 +02001117 if (mask & FANOTIFY_INODE_EVENTS &&
Amir Goldsteind809daf2020-07-16 11:42:12 +03001118 (!fid_mode || mark_type == FAN_MARK_MOUNT))
Amir Goldstein235328d2019-01-10 19:04:43 +02001119 goto fput_and_out;
1120
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001121 if (flags & FAN_MARK_FLUSH) {
1122 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001123 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001124 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001125 else if (mark_type == FAN_MARK_FILESYSTEM)
1126 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001127 else
1128 fsnotify_clear_inode_marks_by_group(group);
1129 goto fput_and_out;
1130 }
1131
Aaron Goidelac5656d2019-08-12 11:20:00 -04001132 ret = fanotify_find_path(dfd, pathname, &path, flags,
1133 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -05001134 if (ret)
1135 goto fput_and_out;
1136
Jan Kara0b3b0942019-05-15 16:28:34 +02001137 if (flags & FAN_MARK_ADD) {
1138 ret = fanotify_events_supported(&path, mask);
1139 if (ret)
1140 goto path_put_and_out;
1141 }
1142
Amir Goldsteind809daf2020-07-16 11:42:12 +03001143 if (fid_mode) {
Amir Goldstein73072282019-01-10 19:04:39 +02001144 ret = fanotify_test_fid(&path, &__fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001145 if (ret)
1146 goto path_put_and_out;
Amir Goldstein77115222019-01-10 19:04:37 +02001147
Amir Goldstein73072282019-01-10 19:04:39 +02001148 fsid = &__fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001149 }
1150
Eric Paris2a3edf82009-12-17 21:24:26 -05001151 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001152 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001153 inode = path.dentry->d_inode;
1154 else
1155 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001156
1157 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001158 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001159 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001160 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001161 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1162 flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001163 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001164 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1165 flags, fsid);
Eric Paris0ff21db2009-12-17 21:24:29 -05001166 else
Amir Goldstein77115222019-01-10 19:04:37 +02001167 ret = fanotify_add_inode_mark(group, inode, mask,
1168 flags, fsid);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001169 break;
1170 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001171 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001172 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1173 flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001174 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001175 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1176 flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001177 else
Amir Goldstein77115222019-01-10 19:04:37 +02001178 ret = fanotify_remove_inode_mark(group, inode, mask,
1179 flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001180 break;
1181 default:
1182 ret = -EINVAL;
1183 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001184
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001185path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001186 path_put(&path);
1187fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001188 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001189 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001190}
Eric Paris2a3edf82009-12-17 21:24:26 -05001191
Dominik Brodowski183caa32018-03-17 15:06:11 +01001192SYSCALL_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 Viro91c2e0b2013-03-05 20:10:59 -05001199#ifdef CONFIG_COMPAT
1200COMPAT_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 Brodowski183caa32018-03-17 15:06:11 +01001205 return do_fanotify_mark(fanotify_fd, flags,
Al Viro91c2e0b2013-03-05 20:10:59 -05001206#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -05001207 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -08001208#else
1209 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -05001210#endif
1211 dfd, pathname);
1212}
1213#endif
1214
Eric Paris2a3edf82009-12-17 21:24:26 -05001215/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001216 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001217 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1218 * must result in panic().
1219 */
1220static int __init fanotify_user_setup(void)
1221{
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001222 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001223 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1224
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001225 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1226 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7088f352020-03-24 17:04:20 +01001227 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 Szeredi6685df32017-10-30 21:14:56 +01001231 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1232 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001233 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001234 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001235
1236 return 0;
1237}
1238device_initcall(fanotify_user_setup);