blob: 02a314acc7574be80f58530e5de89e59548d4fc5 [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 Goldstein5e469c82019-01-10 19:04:35 +0200103 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
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 Kara7088f352020-03-24 17:04:20 +0100415 event = get_one_event(group, count);
416 if (IS_ERR(event)) {
417 ret = PTR_ERR(event);
Jan Karad8aaab42014-04-03 14:46:35 -0700418 break;
419 }
420
Jan Kara7088f352020-03-24 17:04:20 +0100421 if (!event) {
Jan Karad8aaab42014-04-03 14:46:35 -0700422 ret = -EAGAIN;
423 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500424 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700425
426 ret = -ERESTARTSYS;
427 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500428 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700429
430 if (start != buf)
431 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100432
433 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500434 continue;
435 }
436
Jan Kara7088f352020-03-24 17:04:20 +0100437 ret = copy_event_to_user(group, event, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300438 if (unlikely(ret == -EOPENSTALE)) {
439 /*
440 * We cannot report events with stale fd so drop it.
441 * Setting ret to 0 will continue the event loop and
442 * do the right thing if there are no more events to
443 * read (i.e. return bytes read, -EAGAIN or wait).
444 */
445 ret = 0;
446 }
447
Jan Karad8aaab42014-04-03 14:46:35 -0700448 /*
449 * Permission events get queued to wait for response. Other
450 * events can be destroyed now.
451 */
Jan Kara7088f352020-03-24 17:04:20 +0100452 if (!fanotify_is_perm_event(event->mask)) {
453 fsnotify_destroy_event(group, &event->fse);
Jan Karad5078162014-04-03 14:46:36 -0700454 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300455 if (ret <= 0) {
Jan Kara40873282019-01-08 14:02:44 +0100456 spin_lock(&group->notification_lock);
457 finish_permission_event(group,
Jan Kara7088f352020-03-24 17:04:20 +0100458 FANOTIFY_PERM(event), FAN_DENY);
Jan Karad5078162014-04-03 14:46:36 -0700459 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300460 } else {
461 spin_lock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100462 list_add_tail(&event->fse.list,
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300463 &group->fanotify_data.access_list);
464 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700465 }
Jan Karad5078162014-04-03 14:46:36 -0700466 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300467 if (ret < 0)
468 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700469 buf += ret;
470 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500471 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100472 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500473
Eric Parisa1014f12009-12-17 21:24:26 -0500474 if (start != buf && ret != -EFAULT)
475 ret = buf - start;
476 return ret;
477}
478
Eric Parisb2d87902009-12-17 21:24:34 -0500479static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
480{
Eric Parisb2d87902009-12-17 21:24:34 -0500481 struct fanotify_response response = { .fd = -1, .response = -1 };
482 struct fsnotify_group *group;
483 int ret;
484
Miklos Szeredi6685df32017-10-30 21:14:56 +0100485 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
486 return -EINVAL;
487
Eric Parisb2d87902009-12-17 21:24:34 -0500488 group = file->private_data;
489
490 if (count > sizeof(response))
491 count = sizeof(response);
492
493 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
494
495 if (copy_from_user(&response, buf, count))
496 return -EFAULT;
497
498 ret = process_access_response(group, &response);
499 if (ret < 0)
500 count = ret;
501
502 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500503}
504
Eric Paris52c923d2009-12-17 21:24:26 -0500505static int fanotify_release(struct inode *ignored, struct file *file)
506{
507 struct fsnotify_group *group = file->private_data;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400508
Jan Kara5838d442014-08-06 16:03:28 -0700509 /*
Jan Kara96d41012016-09-19 14:44:30 -0700510 * Stop new events from arriving in the notification queue. since
511 * userspace cannot use fanotify fd anymore, no event can enter or
512 * leave access_list by now either.
513 */
514 fsnotify_group_stop_queueing(group);
515
516 /*
517 * Process all permission events on access_list and notification queue
518 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700519 */
Jan Kara073f6552016-10-07 16:56:55 -0700520 spin_lock(&group->notification_lock);
Jan Karaca6f8692019-01-09 13:21:01 +0100521 while (!list_empty(&group->fanotify_data.access_list)) {
Jan Kara7088f352020-03-24 17:04:20 +0100522 struct fanotify_perm_event *event;
523
Jan Karaca6f8692019-01-09 13:21:01 +0100524 event = list_first_entry(&group->fanotify_data.access_list,
525 struct fanotify_perm_event, fae.fse.list);
Jan Karaf0834412014-04-03 14:46:33 -0700526 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100527 finish_permission_event(group, event, FAN_ALLOW);
528 spin_lock(&group->notification_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400529 }
Eric Paris2eebf582010-08-18 12:25:50 -0400530
Jan Kara5838d442014-08-06 16:03:28 -0700531 /*
Jan Kara96d41012016-09-19 14:44:30 -0700532 * Destroy all non-permission events. For permission events just
533 * dequeue them and set the response. They will be freed once the
534 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700535 */
Jan Kara96d41012016-09-19 14:44:30 -0700536 while (!fsnotify_notify_queue_is_empty(group)) {
Jan Kara7088f352020-03-24 17:04:20 +0100537 struct fanotify_event *event;
538
539 event = FANOTIFY_E(fsnotify_remove_first_event(group));
540 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700541 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100542 fsnotify_destroy_event(group, &event->fse);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100543 } else {
Jan Kara7088f352020-03-24 17:04:20 +0100544 finish_permission_event(group, FANOTIFY_PERM(event),
Jan Kara40873282019-01-08 14:02:44 +0100545 FAN_ALLOW);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100546 }
Jan Kara40873282019-01-08 14:02:44 +0100547 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700548 }
Jan Karac21dbe22016-10-07 16:56:52 -0700549 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700550
551 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400552 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400553
Eric Paris52c923d2009-12-17 21:24:26 -0500554 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200555 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500556
557 return 0;
558}
559
Eric Parisa1014f12009-12-17 21:24:26 -0500560static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
561{
562 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800563 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500564 void __user *p;
565 int ret = -ENOTTY;
566 size_t send_len = 0;
567
568 group = file->private_data;
569
570 p = (void __user *) arg;
571
572 switch (cmd) {
573 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700574 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800575 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500576 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700577 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500578 ret = put_user(send_len, (int __user *) p);
579 break;
580 }
581
582 return ret;
583}
584
Eric Paris52c923d2009-12-17 21:24:26 -0500585static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800586 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500587 .poll = fanotify_poll,
588 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500589 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500590 .fasync = NULL,
591 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500592 .unlocked_ioctl = fanotify_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200593 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200594 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500595};
596
Eric Paris2a3edf82009-12-17 21:24:26 -0500597static int fanotify_find_path(int dfd, const char __user *filename,
Aaron Goidelac5656d2019-08-12 11:20:00 -0400598 struct path *path, unsigned int flags, __u64 mask,
599 unsigned int obj_type)
Eric Paris2a3edf82009-12-17 21:24:26 -0500600{
601 int ret;
602
603 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
604 dfd, filename, flags);
605
606 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400607 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500608
609 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400610 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500611 goto out;
612
613 ret = -ENOTDIR;
614 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500615 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400616 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500617 goto out;
618 }
619
Al Viro2903ff02012-08-28 12:52:22 -0400620 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500621 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400622 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500623 } else {
624 unsigned int lookup_flags = 0;
625
626 if (!(flags & FAN_MARK_DONT_FOLLOW))
627 lookup_flags |= LOOKUP_FOLLOW;
628 if (flags & FAN_MARK_ONLYDIR)
629 lookup_flags |= LOOKUP_DIRECTORY;
630
631 ret = user_path_at(dfd, filename, lookup_flags, path);
632 if (ret)
633 goto out;
634 }
635
636 /* you can only watch an inode if you have read permissions on it */
637 ret = inode_permission(path->dentry->d_inode, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400638 if (ret) {
639 path_put(path);
640 goto out;
641 }
642
643 ret = security_path_notify(path, mask, obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -0500644 if (ret)
645 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400646
Eric Paris2a3edf82009-12-17 21:24:26 -0500647out:
648 return ret;
649}
650
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500651static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
652 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200653 unsigned int flags,
654 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500655{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800656 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500657
658 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500659 if (!(flags & FAN_MARK_IGNORED_MASK)) {
660 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300661 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500662 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300663 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500664 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800665 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500666 spin_unlock(&fsn_mark->lock);
667
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500668 return mask & oldmask;
669}
670
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300671static int fanotify_remove_mark(struct fsnotify_group *group,
672 fsnotify_connp_t *connp, __u32 mask,
673 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500674{
675 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500676 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200677 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500678
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700679 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300680 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700681 if (!fsn_mark) {
682 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500683 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700684 }
Eric Paris88826272009-12-17 21:24:28 -0500685
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200686 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
687 &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300688 if (removed & fsnotify_conn_mask(fsn_mark->connector))
689 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200690 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700691 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700692 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700693 if (destroy_mark)
694 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700695
Jan Karab1362ed2016-12-21 16:28:45 +0100696 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500697 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500698 return 0;
699}
700
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300701static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
702 struct vfsmount *mnt, __u32 mask,
703 unsigned int flags)
704{
705 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
706 mask, flags);
707}
708
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300709static int fanotify_remove_sb_mark(struct fsnotify_group *group,
710 struct super_block *sb, __u32 mask,
711 unsigned int flags)
712{
713 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
714}
715
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300716static int fanotify_remove_inode_mark(struct fsnotify_group *group,
717 struct inode *inode, __u32 mask,
718 unsigned int flags)
719{
720 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
721 flags);
722}
723
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500724static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
725 __u32 mask,
726 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500727{
Eric Paris192ca4d2010-10-28 17:21:59 -0400728 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500729
730 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500731 if (!(flags & FAN_MARK_IGNORED_MASK)) {
732 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300733 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500734 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300735 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -0500736 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
737 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500738 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500739 spin_unlock(&fsn_mark->lock);
740
741 return mask & ~oldmask;
742}
743
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700744static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300745 fsnotify_connp_t *connp,
Amir Goldstein77115222019-01-10 19:04:37 +0200746 unsigned int type,
747 __kernel_fsid_t *fsid)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700748{
749 struct fsnotify_mark *mark;
750 int ret;
751
752 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
753 return ERR_PTR(-ENOSPC);
754
755 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
756 if (!mark)
757 return ERR_PTR(-ENOMEM);
758
Jan Kara054c6362016-12-21 18:06:12 +0100759 fsnotify_init_mark(mark, group);
Amir Goldstein77115222019-01-10 19:04:37 +0200760 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700761 if (ret) {
762 fsnotify_put_mark(mark);
763 return ERR_PTR(ret);
764 }
765
766 return mark;
767}
768
769
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300770static int fanotify_add_mark(struct fsnotify_group *group,
771 fsnotify_connp_t *connp, unsigned int type,
Amir Goldstein77115222019-01-10 19:04:37 +0200772 __u32 mask, unsigned int flags,
773 __kernel_fsid_t *fsid)
Eric Paris2a3edf82009-12-17 21:24:26 -0500774{
775 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500776 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500777
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700778 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300779 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500780 if (!fsn_mark) {
Amir Goldstein77115222019-01-10 19:04:37 +0200781 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700782 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700783 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700784 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700785 }
Eric Paris88826272009-12-17 21:24:28 -0500786 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500787 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300788 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
789 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100790 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700791
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100792 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700793 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500794}
795
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300796static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
797 struct vfsmount *mnt, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200798 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300799{
800 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200801 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300802}
803
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300804static int fanotify_add_sb_mark(struct fsnotify_group *group,
Amir Goldstein77115222019-01-10 19:04:37 +0200805 struct super_block *sb, __u32 mask,
806 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300807{
808 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200809 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300810}
811
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500812static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500813 struct inode *inode, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200814 unsigned int flags, __kernel_fsid_t *fsid)
Eric Paris88826272009-12-17 21:24:28 -0500815{
Eric Paris88826272009-12-17 21:24:28 -0500816 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500817
Eric Paris5322a592010-10-28 17:21:57 -0400818 /*
819 * If some other task has this inode open for write we should not add
820 * an ignored mark, unless that ignored mark is supposed to survive
821 * modification changes anyway.
822 */
823 if ((flags & FAN_MARK_IGNORED_MASK) &&
824 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +0200825 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -0400826 return 0;
827
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300828 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200829 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
Eric Paris88826272009-12-17 21:24:28 -0500830}
Eric Paris2a3edf82009-12-17 21:24:26 -0500831
Eric Paris52c923d2009-12-17 21:24:26 -0500832/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400833SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500834{
Eric Paris52c923d2009-12-17 21:24:26 -0500835 struct fsnotify_group *group;
836 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400837 struct user_struct *user;
Amir Goldstein33913992019-01-10 19:04:32 +0200838 struct fanotify_event *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500839
Amir Goldstein96a71f22018-09-21 21:20:30 +0300840 pr_debug("%s: flags=%x event_f_flags=%x\n",
841 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500842
Eric Paris52c923d2009-12-17 21:24:26 -0500843 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200844 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500845
Steve Grubbde8cd832017-10-02 20:21:39 -0400846#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300847 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400848#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300849 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400850#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500851 return -EINVAL;
852
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700853 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
854 return -EINVAL;
855
856 switch (event_f_flags & O_ACCMODE) {
857 case O_RDONLY:
858 case O_RDWR:
859 case O_WRONLY:
860 break;
861 default:
862 return -EINVAL;
863 }
864
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200865 if ((flags & FAN_REPORT_FID) &&
866 (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
867 return -EINVAL;
868
Eric Paris4afeff82010-10-28 17:21:58 -0400869 user = get_current_user();
870 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
871 free_uid(user);
872 return -EMFILE;
873 }
874
Eric Parisb2d87902009-12-17 21:24:34 -0500875 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500876 if (flags & FAN_CLOEXEC)
877 f_flags |= O_CLOEXEC;
878 if (flags & FAN_NONBLOCK)
879 f_flags |= O_NONBLOCK;
880
881 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
882 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500883 if (IS_ERR(group)) {
884 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500885 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500886 }
Eric Paris52c923d2009-12-17 21:24:26 -0500887
Eric Paris4afeff82010-10-28 17:21:58 -0400888 group->fanotify_data.user = user;
Amir Goldstein96a71f22018-09-21 21:20:30 +0300889 group->fanotify_data.flags = flags;
Eric Paris4afeff82010-10-28 17:21:58 -0400890 atomic_inc(&user->fanotify_listeners);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700891 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -0400892
Amir Goldstein83b535d2019-01-10 19:04:42 +0200893 oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL,
Amir Goldsteincacfb952020-03-19 17:10:21 +0200894 FSNOTIFY_EVENT_NONE, NULL, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100895 if (unlikely(!oevent)) {
896 fd = -ENOMEM;
897 goto out_destroy_group;
898 }
899 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100900
Will Woods1e2ee492014-05-06 12:50:10 -0700901 if (force_o_largefile())
902 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400903 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500904 init_waitqueue_head(&group->fanotify_data.access_waitq);
905 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300906 switch (flags & FANOTIFY_CLASS_BITS) {
Eric Paris4231a232010-10-28 17:21:56 -0400907 case FAN_CLASS_NOTIF:
908 group->priority = FS_PRIO_0;
909 break;
910 case FAN_CLASS_CONTENT:
911 group->priority = FS_PRIO_1;
912 break;
913 case FAN_CLASS_PRE_CONTENT:
914 group->priority = FS_PRIO_2;
915 break;
916 default:
917 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200918 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400919 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500920
Eric Paris5dd03f52010-10-28 17:21:57 -0400921 if (flags & FAN_UNLIMITED_QUEUE) {
922 fd = -EPERM;
923 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200924 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400925 group->max_events = UINT_MAX;
926 } else {
927 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
928 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400929
Eric Parisac7e22d2010-10-28 17:21:58 -0400930 if (flags & FAN_UNLIMITED_MARKS) {
931 fd = -EPERM;
932 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200933 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400934 group->fanotify_data.max_marks = UINT_MAX;
935 } else {
936 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
937 }
Eric Parise7099d82010-10-28 17:21:57 -0400938
Steve Grubbde8cd832017-10-02 20:21:39 -0400939 if (flags & FAN_ENABLE_AUDIT) {
940 fd = -EPERM;
941 if (!capable(CAP_AUDIT_WRITE))
942 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -0400943 }
944
Eric Paris52c923d2009-12-17 21:24:26 -0500945 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
946 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200947 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500948
949 return fd;
950
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200951out_destroy_group:
952 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500953 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500954}
Eric Parisbbaa4162009-12-17 21:24:26 -0500955
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200956/* Check if filesystem can encode a unique fid */
Amir Goldstein73072282019-01-10 19:04:39 +0200957static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200958{
Amir Goldstein73072282019-01-10 19:04:39 +0200959 __kernel_fsid_t root_fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200960 int err;
961
962 /*
963 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
964 */
Amir Goldstein73072282019-01-10 19:04:39 +0200965 err = vfs_get_fsid(path->dentry, fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200966 if (err)
967 return err;
968
Amir Goldstein73072282019-01-10 19:04:39 +0200969 if (!fsid->val[0] && !fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200970 return -ENODEV;
971
972 /*
973 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
974 * which uses a different fsid than sb root.
975 */
Amir Goldstein73072282019-01-10 19:04:39 +0200976 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200977 if (err)
978 return err;
979
Amir Goldstein73072282019-01-10 19:04:39 +0200980 if (root_fsid.val[0] != fsid->val[0] ||
981 root_fsid.val[1] != fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200982 return -EXDEV;
983
984 /*
985 * We need to make sure that the file system supports at least
986 * encoding a file handle so user can use name_to_handle_at() to
987 * compare fid returned with event to the file handle of watched
988 * objects. However, name_to_handle_at() requires that the
989 * filesystem also supports decoding file handles.
990 */
991 if (!path->dentry->d_sb->s_export_op ||
992 !path->dentry->d_sb->s_export_op->fh_to_dentry)
993 return -EOPNOTSUPP;
994
995 return 0;
996}
997
Jan Kara0b3b0942019-05-15 16:28:34 +0200998static int fanotify_events_supported(struct path *path, __u64 mask)
999{
1000 /*
1001 * Some filesystems such as 'proc' acquire unusual locks when opening
1002 * files. For them fanotify permission events have high chances of
1003 * deadlocking the system - open done when reporting fanotify event
1004 * blocks on this "unusual" lock while another process holding the lock
1005 * waits for fanotify permission event to be answered. Just disallow
1006 * permission events for such filesystems.
1007 */
1008 if (mask & FANOTIFY_PERM_EVENTS &&
1009 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1010 return -EINVAL;
1011 return 0;
1012}
1013
Dominik Brodowski183caa32018-03-17 15:06:11 +01001014static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1015 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -05001016{
Eric Paris0ff21db2009-12-17 21:24:29 -05001017 struct inode *inode = NULL;
1018 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -05001019 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -04001020 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -05001021 struct path path;
Amir Goldstein73072282019-01-10 19:04:39 +02001022 __kernel_fsid_t __fsid, *fsid = NULL;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001023 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001024 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Aaron Goidelac5656d2019-08-12 11:20:00 -04001025 unsigned int obj_type;
Al Viro2903ff02012-08-28 12:52:22 -04001026 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -05001027
1028 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1029 __func__, fanotify_fd, flags, dfd, pathname, mask);
1030
1031 /* we only use the lower 32 bits as of right now. */
1032 if (mask & ((__u64)0xffffffff << 32))
1033 return -EINVAL;
1034
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001035 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001036 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001037
1038 switch (mark_type) {
1039 case FAN_MARK_INODE:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001040 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1041 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001042 case FAN_MARK_MOUNT:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001043 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1044 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001045 case FAN_MARK_FILESYSTEM:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001046 obj_type = FSNOTIFY_OBJ_TYPE_SB;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001047 break;
1048 default:
1049 return -EINVAL;
1050 }
1051
Eric Paris4d926042009-12-17 21:24:34 -05001052 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001053 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001054 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001055 if (!mask)
1056 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001057 break;
Eric Paris4d926042009-12-17 21:24:34 -05001058 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001059 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001060 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001061 break;
1062 default:
1063 return -EINVAL;
1064 }
Eric Paris8fcd6522010-10-28 17:21:59 -04001065
Miklos Szeredi6685df32017-10-30 21:14:56 +01001066 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001067 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +01001068
1069 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -05001070 return -EINVAL;
1071
Al Viro2903ff02012-08-28 12:52:22 -04001072 f = fdget(fanotify_fd);
1073 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -05001074 return -EBADF;
1075
1076 /* verify that this is indeed an fanotify instance */
1077 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -04001078 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -05001079 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -04001080 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -04001081
1082 /*
1083 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1084 * allowed to set permissions events.
1085 */
1086 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001087 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -04001088 group->priority == FS_PRIO_0)
1089 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -05001090
Amir Goldstein235328d2019-01-10 19:04:43 +02001091 /*
1092 * Events with data type inode do not carry enough information to report
1093 * event->fd, so we do not allow setting a mask for inode events unless
1094 * group supports reporting fid.
1095 * inode events are not supported on a mount mark, because they do not
1096 * carry enough information (i.e. path) to be filtered by mount point.
1097 */
1098 if (mask & FANOTIFY_INODE_EVENTS &&
1099 (!FAN_GROUP_FLAG(group, FAN_REPORT_FID) ||
1100 mark_type == FAN_MARK_MOUNT))
1101 goto fput_and_out;
1102
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001103 if (flags & FAN_MARK_FLUSH) {
1104 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001105 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001106 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001107 else if (mark_type == FAN_MARK_FILESYSTEM)
1108 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001109 else
1110 fsnotify_clear_inode_marks_by_group(group);
1111 goto fput_and_out;
1112 }
1113
Aaron Goidelac5656d2019-08-12 11:20:00 -04001114 ret = fanotify_find_path(dfd, pathname, &path, flags,
1115 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -05001116 if (ret)
1117 goto fput_and_out;
1118
Jan Kara0b3b0942019-05-15 16:28:34 +02001119 if (flags & FAN_MARK_ADD) {
1120 ret = fanotify_events_supported(&path, mask);
1121 if (ret)
1122 goto path_put_and_out;
1123 }
1124
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001125 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
Amir Goldstein73072282019-01-10 19:04:39 +02001126 ret = fanotify_test_fid(&path, &__fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001127 if (ret)
1128 goto path_put_and_out;
Amir Goldstein77115222019-01-10 19:04:37 +02001129
Amir Goldstein73072282019-01-10 19:04:39 +02001130 fsid = &__fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001131 }
1132
Eric Paris2a3edf82009-12-17 21:24:26 -05001133 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001134 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001135 inode = path.dentry->d_inode;
1136 else
1137 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001138
1139 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001140 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001141 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001142 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001143 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1144 flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001145 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001146 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1147 flags, fsid);
Eric Paris0ff21db2009-12-17 21:24:29 -05001148 else
Amir Goldstein77115222019-01-10 19:04:37 +02001149 ret = fanotify_add_inode_mark(group, inode, mask,
1150 flags, fsid);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001151 break;
1152 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001153 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001154 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1155 flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001156 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001157 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1158 flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001159 else
Amir Goldstein77115222019-01-10 19:04:37 +02001160 ret = fanotify_remove_inode_mark(group, inode, mask,
1161 flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001162 break;
1163 default:
1164 ret = -EINVAL;
1165 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001166
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001167path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001168 path_put(&path);
1169fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001170 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001171 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001172}
Eric Paris2a3edf82009-12-17 21:24:26 -05001173
Dominik Brodowski183caa32018-03-17 15:06:11 +01001174SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1175 __u64, mask, int, dfd,
1176 const char __user *, pathname)
1177{
1178 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1179}
1180
Al Viro91c2e0b2013-03-05 20:10:59 -05001181#ifdef CONFIG_COMPAT
1182COMPAT_SYSCALL_DEFINE6(fanotify_mark,
1183 int, fanotify_fd, unsigned int, flags,
1184 __u32, mask0, __u32, mask1, int, dfd,
1185 const char __user *, pathname)
1186{
Dominik Brodowski183caa32018-03-17 15:06:11 +01001187 return do_fanotify_mark(fanotify_fd, flags,
Al Viro91c2e0b2013-03-05 20:10:59 -05001188#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -05001189 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -08001190#else
1191 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -05001192#endif
1193 dfd, pathname);
1194}
1195#endif
1196
Eric Paris2a3edf82009-12-17 21:24:26 -05001197/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001198 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001199 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1200 * must result in panic().
1201 */
1202static int __init fanotify_user_setup(void)
1203{
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001204 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001205 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1206
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001207 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1208 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7088f352020-03-24 17:04:20 +01001209 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1210 SLAB_PANIC);
1211 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1212 SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001213 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1214 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001215 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001216 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001217
1218 return 0;
1219}
1220device_initcall(fanotify_user_setup);