blob: 1638c171ca824c1035c84c82e000cbd16b96fed3 [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 Kara7053aee2014-01-21 15:48:14 -080049struct kmem_cache *fanotify_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070050struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050051
Amir Goldstein5e469c82019-01-10 19:04:35 +020052#define FANOTIFY_EVENT_ALIGN 4
53
54static int fanotify_event_info_len(struct fanotify_event *event)
55{
56 if (!fanotify_event_has_fid(event))
57 return 0;
58
59 return roundup(sizeof(struct fanotify_event_info_fid) +
60 sizeof(struct file_handle) + event->fh_len,
61 FANOTIFY_EVENT_ALIGN);
62}
63
Eric Parisa1014f12009-12-17 21:24:26 -050064/*
65 * Get an fsnotify notification event if one exists and is small
66 * enough to fit in "count". Return an error pointer if the count
67 * is not large enough.
68 *
Jan Karac21dbe22016-10-07 16:56:52 -070069 * Called with the group->notification_lock held.
Eric Parisa1014f12009-12-17 21:24:26 -050070 */
71static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
72 size_t count)
73{
Amir Goldstein5e469c82019-01-10 19:04:35 +020074 size_t event_size = FAN_EVENT_METADATA_LEN;
75 struct fanotify_event *event;
76
Jan Karaed272642016-10-07 16:57:01 -070077 assert_spin_locked(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -050078
79 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
80
81 if (fsnotify_notify_queue_is_empty(group))
82 return NULL;
83
Amir Goldstein5e469c82019-01-10 19:04:35 +020084 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
85 event = FANOTIFY_E(fsnotify_peek_first_event(group));
86 event_size += fanotify_event_info_len(event);
87 }
88
89 if (event_size > count)
Eric Parisa1014f12009-12-17 21:24:26 -050090 return ERR_PTR(-EINVAL);
91
Amir Goldstein5e469c82019-01-10 19:04:35 +020092 /*
93 * Held the notification_lock the whole time, so this is the
94 * same event we peeked above
95 */
Jan Kara8ba8fa912014-08-06 16:03:26 -070096 return fsnotify_remove_first_event(group);
Eric Parisa1014f12009-12-17 21:24:26 -050097}
98
Al Viro352e3b22012-08-19 12:30:45 -040099static int create_fd(struct fsnotify_group *group,
Amir Goldstein33913992019-01-10 19:04:32 +0200100 struct fanotify_event *event,
Jan Kara7053aee2014-01-21 15:48:14 -0800101 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500102{
103 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500104 struct file *new_file;
105
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500106 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500107
Yann Droneaud0b37e092014-10-09 15:24:40 -0700108 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -0500109 if (client_fd < 0)
110 return client_fd;
111
Eric Parisa1014f12009-12-17 21:24:26 -0500112 /*
113 * we need a new file handle for the userspace program so it can read even if it was
114 * originally opened O_WRONLY.
115 */
Eric Parisa1014f12009-12-17 21:24:26 -0500116 /* it's possible this event was an overflow event. in that case dentry and mnt
117 * are NULL; That's fine, just don't call dentry open */
Al Viro765927b2012-06-26 21:58:53 +0400118 if (event->path.dentry && event->path.mnt)
119 new_file = dentry_open(&event->path,
Eric Paris80af2582010-07-28 10:18:37 -0400120 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -0500121 current_cred());
122 else
123 new_file = ERR_PTR(-EOVERFLOW);
124 if (IS_ERR(new_file)) {
125 /*
126 * we still send an event even if we can't open the file. this
127 * can happen when say tasks are gone and we try to open their
128 * /proc files or we try to open a WRONLY file like in sysfs
129 * we just send the errno to userspace since there isn't much
130 * else we can do.
131 */
132 put_unused_fd(client_fd);
133 client_fd = PTR_ERR(new_file);
134 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400135 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500136 }
137
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500138 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500139}
140
Amir Goldstein33913992019-01-10 19:04:32 +0200141static struct fanotify_perm_event *dequeue_event(
Jan Karaf0834412014-04-03 14:46:33 -0700142 struct fsnotify_group *group, int fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500143{
Amir Goldstein33913992019-01-10 19:04:32 +0200144 struct fanotify_perm_event *event, *return_e = NULL;
Eric Parisb2d87902009-12-17 21:24:34 -0500145
Jan Kara073f6552016-10-07 16:56:55 -0700146 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700147 list_for_each_entry(event, &group->fanotify_data.access_list,
148 fae.fse.list) {
149 if (event->fd != fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500150 continue;
151
Jan Karaf0834412014-04-03 14:46:33 -0700152 list_del_init(&event->fae.fse.list);
153 return_e = event;
Eric Parisb2d87902009-12-17 21:24:34 -0500154 break;
155 }
Jan Kara073f6552016-10-07 16:56:55 -0700156 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500157
Jan Karaf0834412014-04-03 14:46:33 -0700158 pr_debug("%s: found return_re=%p\n", __func__, return_e);
Eric Parisb2d87902009-12-17 21:24:34 -0500159
Jan Karaf0834412014-04-03 14:46:33 -0700160 return return_e;
Eric Parisb2d87902009-12-17 21:24:34 -0500161}
162
163static int process_access_response(struct fsnotify_group *group,
164 struct fanotify_response *response_struct)
165{
Amir Goldstein33913992019-01-10 19:04:32 +0200166 struct fanotify_perm_event *event;
Jan Karaf0834412014-04-03 14:46:33 -0700167 int fd = response_struct->fd;
168 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500169
170 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
171 fd, response);
172 /*
173 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300174 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500175 * timeout
176 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400177 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500178 case FAN_ALLOW:
179 case FAN_DENY:
180 break;
181 default:
182 return -EINVAL;
183 }
184
185 if (fd < 0)
186 return -EINVAL;
187
Amir Goldstein96a71f22018-09-21 21:20:30 +0300188 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400189 return -EINVAL;
190
Jan Karaf0834412014-04-03 14:46:33 -0700191 event = dequeue_event(group, fd);
192 if (!event)
Eric Parisb2d87902009-12-17 21:24:34 -0500193 return -ENOENT;
194
Jan Karaf0834412014-04-03 14:46:33 -0700195 event->response = response;
Eric Parisb2d87902009-12-17 21:24:34 -0500196 wake_up(&group->fanotify_data.access_waitq);
197
Eric Parisb2d87902009-12-17 21:24:34 -0500198 return 0;
199}
Eric Parisb2d87902009-12-17 21:24:34 -0500200
Amir Goldstein5e469c82019-01-10 19:04:35 +0200201static int copy_fid_to_user(struct fanotify_event *event, char __user *buf)
202{
203 struct fanotify_event_info_fid info = { };
204 struct file_handle handle = { };
205 size_t fh_len = event->fh_len;
206 size_t len = fanotify_event_info_len(event);
207
208 if (!len)
209 return 0;
210
211 if (WARN_ON_ONCE(len < sizeof(info) + sizeof(handle) + fh_len))
212 return -EFAULT;
213
214 /* Copy event info fid header followed by vaiable sized file handle */
215 info.hdr.info_type = FAN_EVENT_INFO_TYPE_FID;
216 info.hdr.len = len;
217 info.fsid = event->fid.fsid;
218 if (copy_to_user(buf, &info, sizeof(info)))
219 return -EFAULT;
220
221 buf += sizeof(info);
222 len -= sizeof(info);
223 handle.handle_type = event->fh_type;
224 handle.handle_bytes = fh_len;
225 if (copy_to_user(buf, &handle, sizeof(handle)))
226 return -EFAULT;
227
228 buf += sizeof(handle);
229 len -= sizeof(handle);
230 if (copy_to_user(buf, fanotify_event_fh(event), fh_len))
231 return -EFAULT;
232
233 /* Pad with 0's */
234 buf += fh_len;
235 len -= fh_len;
236 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
237 if (len > 0 && clear_user(buf, len))
238 return -EFAULT;
239
240 return 0;
241}
242
Eric Parisa1014f12009-12-17 21:24:26 -0500243static ssize_t copy_event_to_user(struct fsnotify_group *group,
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200244 struct fsnotify_event *fsn_event,
Kees Cook5b03a472018-12-04 15:44:46 -0800245 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500246{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200247 struct fanotify_event_metadata metadata;
248 struct fanotify_event *event;
249 struct file *f = NULL;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200250 int ret, fd = FAN_NOFD;
Eric Parisa1014f12009-12-17 21:24:26 -0500251
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200252 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
Eric Parisa1014f12009-12-17 21:24:26 -0500253
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200254 event = container_of(fsn_event, struct fanotify_event, fse);
255 metadata.event_len = FAN_EVENT_METADATA_LEN;
256 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
257 metadata.vers = FANOTIFY_METADATA_VERSION;
258 metadata.reserved = 0;
259 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
260 metadata.pid = pid_vnr(event->pid);
Eric Parisa1014f12009-12-17 21:24:26 -0500261
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200262 if (fanotify_event_has_path(event)) {
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200263 fd = create_fd(group, event, &f);
264 if (fd < 0)
265 return fd;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200266 } else if (fanotify_event_has_fid(event)) {
267 metadata.event_len += fanotify_event_info_len(event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200268 }
269 metadata.fd = fd;
270
Al Viro352e3b22012-08-19 12:30:45 -0400271 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800272 /*
273 * Sanity check copy size in case get_one_event() and
274 * fill_event_metadata() event_len sizes ever get out of sync.
275 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200276 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400277 goto out_close_fd;
278
Amir Goldstein5e469c82019-01-10 19:04:35 +0200279 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200280 goto out_close_fd;
281
282 if (fanotify_is_perm_event(event->mask))
283 FANOTIFY_PE(fsn_event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500284
Amir Goldstein5e469c82019-01-10 19:04:35 +0200285 if (fanotify_event_has_path(event)) {
Al Viro3587b1b2012-11-18 19:19:00 +0000286 fd_install(fd, f);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200287 } else if (fanotify_event_has_fid(event)) {
288 ret = copy_fid_to_user(event, buf + FAN_EVENT_METADATA_LEN);
289 if (ret < 0)
290 return ret;
291 }
292
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200293 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500294
Eric Parisb2d87902009-12-17 21:24:34 -0500295out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400296 if (fd != FAN_NOFD) {
297 put_unused_fd(fd);
298 fput(f);
299 }
Eric Parisb2d87902009-12-17 21:24:34 -0500300 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500301}
302
303/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400304static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500305{
306 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400307 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500308
309 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700310 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500311 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800312 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700313 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500314
315 return ret;
316}
317
318static ssize_t fanotify_read(struct file *file, char __user *buf,
319 size_t count, loff_t *pos)
320{
321 struct fsnotify_group *group;
322 struct fsnotify_event *kevent;
323 char __user *start;
324 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100325 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500326
327 start = buf;
328 group = file->private_data;
329
330 pr_debug("%s: group=%p\n", __func__, group);
331
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100332 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500333 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700334 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500335 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700336 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500337
Jan Karad8aaab42014-04-03 14:46:35 -0700338 if (IS_ERR(kevent)) {
Eric Parisa1014f12009-12-17 21:24:26 -0500339 ret = PTR_ERR(kevent);
Jan Karad8aaab42014-04-03 14:46:35 -0700340 break;
341 }
342
343 if (!kevent) {
344 ret = -EAGAIN;
345 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500346 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700347
348 ret = -ERESTARTSYS;
349 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500350 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700351
352 if (start != buf)
353 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100354
355 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500356 continue;
357 }
358
Kees Cook5b03a472018-12-04 15:44:46 -0800359 ret = copy_event_to_user(group, kevent, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300360 if (unlikely(ret == -EOPENSTALE)) {
361 /*
362 * We cannot report events with stale fd so drop it.
363 * Setting ret to 0 will continue the event loop and
364 * do the right thing if there are no more events to
365 * read (i.e. return bytes read, -EAGAIN or wait).
366 */
367 ret = 0;
368 }
369
Jan Karad8aaab42014-04-03 14:46:35 -0700370 /*
371 * Permission events get queued to wait for response. Other
372 * events can be destroyed now.
373 */
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200374 if (!fanotify_is_perm_event(FANOTIFY_E(kevent)->mask)) {
Jan Karad8aaab42014-04-03 14:46:35 -0700375 fsnotify_destroy_event(group, kevent);
Jan Karad5078162014-04-03 14:46:36 -0700376 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300377 if (ret <= 0) {
Jan Karad5078162014-04-03 14:46:36 -0700378 FANOTIFY_PE(kevent)->response = FAN_DENY;
379 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300380 } else {
381 spin_lock(&group->notification_lock);
382 list_add_tail(&kevent->list,
383 &group->fanotify_data.access_list);
384 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700385 }
Jan Karad5078162014-04-03 14:46:36 -0700386 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300387 if (ret < 0)
388 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700389 buf += ret;
390 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500391 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100392 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500393
Eric Parisa1014f12009-12-17 21:24:26 -0500394 if (start != buf && ret != -EFAULT)
395 ret = buf - start;
396 return ret;
397}
398
Eric Parisb2d87902009-12-17 21:24:34 -0500399static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
400{
Eric Parisb2d87902009-12-17 21:24:34 -0500401 struct fanotify_response response = { .fd = -1, .response = -1 };
402 struct fsnotify_group *group;
403 int ret;
404
Miklos Szeredi6685df32017-10-30 21:14:56 +0100405 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
406 return -EINVAL;
407
Eric Parisb2d87902009-12-17 21:24:34 -0500408 group = file->private_data;
409
410 if (count > sizeof(response))
411 count = sizeof(response);
412
413 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
414
415 if (copy_from_user(&response, buf, count))
416 return -EFAULT;
417
418 ret = process_access_response(group, &response);
419 if (ret < 0)
420 count = ret;
421
422 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500423}
424
Eric Paris52c923d2009-12-17 21:24:26 -0500425static int fanotify_release(struct inode *ignored, struct file *file)
426{
427 struct fsnotify_group *group = file->private_data;
Amir Goldstein33913992019-01-10 19:04:32 +0200428 struct fanotify_perm_event *event, *next;
Jan Kara96d41012016-09-19 14:44:30 -0700429 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400430
Jan Kara5838d442014-08-06 16:03:28 -0700431 /*
Jan Kara96d41012016-09-19 14:44:30 -0700432 * Stop new events from arriving in the notification queue. since
433 * userspace cannot use fanotify fd anymore, no event can enter or
434 * leave access_list by now either.
435 */
436 fsnotify_group_stop_queueing(group);
437
438 /*
439 * Process all permission events on access_list and notification queue
440 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700441 */
Jan Kara073f6552016-10-07 16:56:55 -0700442 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700443 list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
444 fae.fse.list) {
445 pr_debug("%s: found group=%p event=%p\n", __func__, group,
446 event);
Eric Paris2eebf582010-08-18 12:25:50 -0400447
Jan Karaf0834412014-04-03 14:46:33 -0700448 list_del_init(&event->fae.fse.list);
449 event->response = FAN_ALLOW;
Eric Paris2eebf582010-08-18 12:25:50 -0400450 }
Eric Paris2eebf582010-08-18 12:25:50 -0400451
Jan Kara5838d442014-08-06 16:03:28 -0700452 /*
Jan Kara96d41012016-09-19 14:44:30 -0700453 * Destroy all non-permission events. For permission events just
454 * dequeue them and set the response. They will be freed once the
455 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700456 */
Jan Kara96d41012016-09-19 14:44:30 -0700457 while (!fsnotify_notify_queue_is_empty(group)) {
458 fsn_event = fsnotify_remove_first_event(group);
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200459 if (!(FANOTIFY_E(fsn_event)->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700460 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700461 fsnotify_destroy_event(group, fsn_event);
Jan Karac21dbe22016-10-07 16:56:52 -0700462 spin_lock(&group->notification_lock);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100463 } else {
Jan Kara96d41012016-09-19 14:44:30 -0700464 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100465 }
Jan Kara96d41012016-09-19 14:44:30 -0700466 }
Jan Karac21dbe22016-10-07 16:56:52 -0700467 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700468
469 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400470 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400471
Eric Paris52c923d2009-12-17 21:24:26 -0500472 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200473 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500474
475 return 0;
476}
477
Eric Parisa1014f12009-12-17 21:24:26 -0500478static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
479{
480 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800481 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500482 void __user *p;
483 int ret = -ENOTTY;
484 size_t send_len = 0;
485
486 group = file->private_data;
487
488 p = (void __user *) arg;
489
490 switch (cmd) {
491 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700492 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800493 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500494 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700495 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500496 ret = put_user(send_len, (int __user *) p);
497 break;
498 }
499
500 return ret;
501}
502
Eric Paris52c923d2009-12-17 21:24:26 -0500503static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800504 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500505 .poll = fanotify_poll,
506 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500507 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500508 .fasync = NULL,
509 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500510 .unlocked_ioctl = fanotify_ioctl,
511 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200512 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500513};
514
Eric Paris2a3edf82009-12-17 21:24:26 -0500515static int fanotify_find_path(int dfd, const char __user *filename,
516 struct path *path, unsigned int flags)
517{
518 int ret;
519
520 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
521 dfd, filename, flags);
522
523 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400524 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500525
526 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400527 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500528 goto out;
529
530 ret = -ENOTDIR;
531 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500532 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400533 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500534 goto out;
535 }
536
Al Viro2903ff02012-08-28 12:52:22 -0400537 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500538 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400539 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500540 } else {
541 unsigned int lookup_flags = 0;
542
543 if (!(flags & FAN_MARK_DONT_FOLLOW))
544 lookup_flags |= LOOKUP_FOLLOW;
545 if (flags & FAN_MARK_ONLYDIR)
546 lookup_flags |= LOOKUP_DIRECTORY;
547
548 ret = user_path_at(dfd, filename, lookup_flags, path);
549 if (ret)
550 goto out;
551 }
552
553 /* you can only watch an inode if you have read permissions on it */
554 ret = inode_permission(path->dentry->d_inode, MAY_READ);
555 if (ret)
556 path_put(path);
557out:
558 return ret;
559}
560
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500561static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
562 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200563 unsigned int flags,
564 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500565{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800566 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500567
568 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500569 if (!(flags & FAN_MARK_IGNORED_MASK)) {
570 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300571 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500572 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300573 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500574 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800575 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500576 spin_unlock(&fsn_mark->lock);
577
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500578 return mask & oldmask;
579}
580
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300581static int fanotify_remove_mark(struct fsnotify_group *group,
582 fsnotify_connp_t *connp, __u32 mask,
583 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500584{
585 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500586 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200587 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500588
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700589 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300590 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700591 if (!fsn_mark) {
592 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500593 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700594 }
Eric Paris88826272009-12-17 21:24:28 -0500595
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200596 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
597 &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300598 if (removed & fsnotify_conn_mask(fsn_mark->connector))
599 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200600 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700601 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700602 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700603 if (destroy_mark)
604 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700605
Jan Karab1362ed2016-12-21 16:28:45 +0100606 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500607 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500608 return 0;
609}
610
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300611static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
612 struct vfsmount *mnt, __u32 mask,
613 unsigned int flags)
614{
615 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
616 mask, flags);
617}
618
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300619static int fanotify_remove_sb_mark(struct fsnotify_group *group,
620 struct super_block *sb, __u32 mask,
621 unsigned int flags)
622{
623 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
624}
625
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300626static int fanotify_remove_inode_mark(struct fsnotify_group *group,
627 struct inode *inode, __u32 mask,
628 unsigned int flags)
629{
630 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
631 flags);
632}
633
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500634static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
635 __u32 mask,
636 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500637{
Eric Paris192ca4d2010-10-28 17:21:59 -0400638 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500639
640 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500641 if (!(flags & FAN_MARK_IGNORED_MASK)) {
642 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300643 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500644 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300645 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -0500646 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
647 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500648 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500649 spin_unlock(&fsn_mark->lock);
650
651 return mask & ~oldmask;
652}
653
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700654static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300655 fsnotify_connp_t *connp,
656 unsigned int type)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700657{
658 struct fsnotify_mark *mark;
659 int ret;
660
661 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
662 return ERR_PTR(-ENOSPC);
663
664 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
665 if (!mark)
666 return ERR_PTR(-ENOMEM);
667
Jan Kara054c6362016-12-21 18:06:12 +0100668 fsnotify_init_mark(mark, group);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300669 ret = fsnotify_add_mark_locked(mark, connp, type, 0);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700670 if (ret) {
671 fsnotify_put_mark(mark);
672 return ERR_PTR(ret);
673 }
674
675 return mark;
676}
677
678
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300679static int fanotify_add_mark(struct fsnotify_group *group,
680 fsnotify_connp_t *connp, unsigned int type,
681 __u32 mask, unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500682{
683 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500684 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500685
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700686 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300687 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500688 if (!fsn_mark) {
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300689 fsn_mark = fanotify_add_new_mark(group, connp, type);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700690 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700691 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700692 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700693 }
Eric Paris88826272009-12-17 21:24:28 -0500694 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500695 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300696 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
697 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100698 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700699
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100700 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700701 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500702}
703
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300704static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
705 struct vfsmount *mnt, __u32 mask,
706 unsigned int flags)
707{
708 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
709 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags);
710}
711
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300712static int fanotify_add_sb_mark(struct fsnotify_group *group,
713 struct super_block *sb, __u32 mask,
714 unsigned int flags)
715{
716 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
717 FSNOTIFY_OBJ_TYPE_SB, mask, flags);
718}
719
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500720static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500721 struct inode *inode, __u32 mask,
722 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500723{
Eric Paris88826272009-12-17 21:24:28 -0500724 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500725
Eric Paris5322a592010-10-28 17:21:57 -0400726 /*
727 * If some other task has this inode open for write we should not add
728 * an ignored mark, unless that ignored mark is supposed to survive
729 * modification changes anyway.
730 */
731 if ((flags & FAN_MARK_IGNORED_MASK) &&
732 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +0200733 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -0400734 return 0;
735
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300736 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
737 FSNOTIFY_OBJ_TYPE_INODE, mask, flags);
Eric Paris88826272009-12-17 21:24:28 -0500738}
Eric Paris2a3edf82009-12-17 21:24:26 -0500739
Eric Paris52c923d2009-12-17 21:24:26 -0500740/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400741SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500742{
Eric Paris52c923d2009-12-17 21:24:26 -0500743 struct fsnotify_group *group;
744 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400745 struct user_struct *user;
Amir Goldstein33913992019-01-10 19:04:32 +0200746 struct fanotify_event *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500747
Amir Goldstein96a71f22018-09-21 21:20:30 +0300748 pr_debug("%s: flags=%x event_f_flags=%x\n",
749 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500750
Eric Paris52c923d2009-12-17 21:24:26 -0500751 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200752 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500753
Steve Grubbde8cd832017-10-02 20:21:39 -0400754#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300755 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400756#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300757 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400758#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500759 return -EINVAL;
760
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700761 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
762 return -EINVAL;
763
764 switch (event_f_flags & O_ACCMODE) {
765 case O_RDONLY:
766 case O_RDWR:
767 case O_WRONLY:
768 break;
769 default:
770 return -EINVAL;
771 }
772
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200773 if ((flags & FAN_REPORT_FID) &&
774 (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
775 return -EINVAL;
776
Eric Paris4afeff82010-10-28 17:21:58 -0400777 user = get_current_user();
778 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
779 free_uid(user);
780 return -EMFILE;
781 }
782
Eric Parisb2d87902009-12-17 21:24:34 -0500783 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500784 if (flags & FAN_CLOEXEC)
785 f_flags |= O_CLOEXEC;
786 if (flags & FAN_NONBLOCK)
787 f_flags |= O_NONBLOCK;
788
789 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
790 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500791 if (IS_ERR(group)) {
792 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500793 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500794 }
Eric Paris52c923d2009-12-17 21:24:26 -0500795
Eric Paris4afeff82010-10-28 17:21:58 -0400796 group->fanotify_data.user = user;
Amir Goldstein96a71f22018-09-21 21:20:30 +0300797 group->fanotify_data.flags = flags;
Eric Paris4afeff82010-10-28 17:21:58 -0400798 atomic_inc(&user->fanotify_listeners);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700799 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -0400800
Jan Kara1f5eaa92018-02-21 14:10:59 +0100801 oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100802 if (unlikely(!oevent)) {
803 fd = -ENOMEM;
804 goto out_destroy_group;
805 }
806 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100807
Will Woods1e2ee492014-05-06 12:50:10 -0700808 if (force_o_largefile())
809 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400810 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500811 init_waitqueue_head(&group->fanotify_data.access_waitq);
812 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300813 switch (flags & FANOTIFY_CLASS_BITS) {
Eric Paris4231a232010-10-28 17:21:56 -0400814 case FAN_CLASS_NOTIF:
815 group->priority = FS_PRIO_0;
816 break;
817 case FAN_CLASS_CONTENT:
818 group->priority = FS_PRIO_1;
819 break;
820 case FAN_CLASS_PRE_CONTENT:
821 group->priority = FS_PRIO_2;
822 break;
823 default:
824 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200825 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400826 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500827
Eric Paris5dd03f52010-10-28 17:21:57 -0400828 if (flags & FAN_UNLIMITED_QUEUE) {
829 fd = -EPERM;
830 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200831 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400832 group->max_events = UINT_MAX;
833 } else {
834 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
835 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400836
Eric Parisac7e22d2010-10-28 17:21:58 -0400837 if (flags & FAN_UNLIMITED_MARKS) {
838 fd = -EPERM;
839 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200840 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400841 group->fanotify_data.max_marks = UINT_MAX;
842 } else {
843 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
844 }
Eric Parise7099d82010-10-28 17:21:57 -0400845
Steve Grubbde8cd832017-10-02 20:21:39 -0400846 if (flags & FAN_ENABLE_AUDIT) {
847 fd = -EPERM;
848 if (!capable(CAP_AUDIT_WRITE))
849 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -0400850 }
851
Eric Paris52c923d2009-12-17 21:24:26 -0500852 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
853 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200854 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500855
856 return fd;
857
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200858out_destroy_group:
859 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500860 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500861}
Eric Parisbbaa4162009-12-17 21:24:26 -0500862
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200863/* Check if filesystem can encode a unique fid */
864static int fanotify_test_fid(struct path *path)
865{
866 struct kstatfs stat, root_stat;
867 struct path root = {
868 .mnt = path->mnt,
869 .dentry = path->dentry->d_sb->s_root,
870 };
871 int err;
872
873 /*
874 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
875 */
876 err = vfs_statfs(path, &stat);
877 if (err)
878 return err;
879
880 if (!stat.f_fsid.val[0] && !stat.f_fsid.val[1])
881 return -ENODEV;
882
883 /*
884 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
885 * which uses a different fsid than sb root.
886 */
887 err = vfs_statfs(&root, &root_stat);
888 if (err)
889 return err;
890
891 if (root_stat.f_fsid.val[0] != stat.f_fsid.val[0] ||
892 root_stat.f_fsid.val[1] != stat.f_fsid.val[1])
893 return -EXDEV;
894
895 /*
896 * We need to make sure that the file system supports at least
897 * encoding a file handle so user can use name_to_handle_at() to
898 * compare fid returned with event to the file handle of watched
899 * objects. However, name_to_handle_at() requires that the
900 * filesystem also supports decoding file handles.
901 */
902 if (!path->dentry->d_sb->s_export_op ||
903 !path->dentry->d_sb->s_export_op->fh_to_dentry)
904 return -EOPNOTSUPP;
905
906 return 0;
907}
908
Dominik Brodowski183caa32018-03-17 15:06:11 +0100909static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
910 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500911{
Eric Paris0ff21db2009-12-17 21:24:29 -0500912 struct inode *inode = NULL;
913 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500914 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400915 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500916 struct path path;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +0300917 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300918 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Al Viro2903ff02012-08-28 12:52:22 -0400919 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500920
921 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
922 __func__, fanotify_fd, flags, dfd, pathname, mask);
923
924 /* we only use the lower 32 bits as of right now. */
925 if (mask & ((__u64)0xffffffff << 32))
926 return -EINVAL;
927
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300928 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500929 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300930
931 switch (mark_type) {
932 case FAN_MARK_INODE:
933 case FAN_MARK_MOUNT:
934 case FAN_MARK_FILESYSTEM:
935 break;
936 default:
937 return -EINVAL;
938 }
939
Eric Paris4d926042009-12-17 21:24:34 -0500940 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100941 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500942 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100943 if (!mask)
944 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700945 break;
Eric Paris4d926042009-12-17 21:24:34 -0500946 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300947 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700948 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500949 break;
950 default:
951 return -EINVAL;
952 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400953
Miklos Szeredi6685df32017-10-30 21:14:56 +0100954 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300955 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100956
957 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -0500958 return -EINVAL;
959
Al Viro2903ff02012-08-28 12:52:22 -0400960 f = fdget(fanotify_fd);
961 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -0500962 return -EBADF;
963
964 /* verify that this is indeed an fanotify instance */
965 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400966 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -0500967 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -0400968 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -0400969
970 /*
971 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
972 * allowed to set permissions events.
973 */
974 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300975 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -0400976 group->priority == FS_PRIO_0)
977 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500978
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700979 if (flags & FAN_MARK_FLUSH) {
980 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300981 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700982 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300983 else if (mark_type == FAN_MARK_FILESYSTEM)
984 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700985 else
986 fsnotify_clear_inode_marks_by_group(group);
987 goto fput_and_out;
988 }
989
Eric Paris2a3edf82009-12-17 21:24:26 -0500990 ret = fanotify_find_path(dfd, pathname, &path, flags);
991 if (ret)
992 goto fput_and_out;
993
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200994 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
995 ret = fanotify_test_fid(&path);
996 if (ret)
997 goto path_put_and_out;
998 }
999
Eric Paris2a3edf82009-12-17 21:24:26 -05001000 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001001 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001002 inode = path.dentry->d_inode;
1003 else
1004 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001005
1006 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001007 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001008 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001009 if (mark_type == FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001010 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001011 else if (mark_type == FAN_MARK_FILESYSTEM)
1012 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -05001013 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001014 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001015 break;
1016 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001017 if (mark_type == FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001018 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001019 else if (mark_type == FAN_MARK_FILESYSTEM)
1020 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001021 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001022 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001023 break;
1024 default:
1025 ret = -EINVAL;
1026 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001027
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001028path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001029 path_put(&path);
1030fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001031 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001032 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001033}
Eric Paris2a3edf82009-12-17 21:24:26 -05001034
Dominik Brodowski183caa32018-03-17 15:06:11 +01001035SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1036 __u64, mask, int, dfd,
1037 const char __user *, pathname)
1038{
1039 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1040}
1041
Al Viro91c2e0b2013-03-05 20:10:59 -05001042#ifdef CONFIG_COMPAT
1043COMPAT_SYSCALL_DEFINE6(fanotify_mark,
1044 int, fanotify_fd, unsigned int, flags,
1045 __u32, mask0, __u32, mask1, int, dfd,
1046 const char __user *, pathname)
1047{
Dominik Brodowski183caa32018-03-17 15:06:11 +01001048 return do_fanotify_mark(fanotify_fd, flags,
Al Viro91c2e0b2013-03-05 20:10:59 -05001049#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -05001050 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -08001051#else
1052 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -05001053#endif
1054 dfd, pathname);
1055}
1056#endif
1057
Eric Paris2a3edf82009-12-17 21:24:26 -05001058/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001059 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001060 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1061 * must result in panic().
1062 */
1063static int __init fanotify_user_setup(void)
1064{
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001065 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001066 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1067
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001068 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1069 SLAB_PANIC|SLAB_ACCOUNT);
Amir Goldstein33913992019-01-10 19:04:32 +02001070 fanotify_event_cachep = KMEM_CACHE(fanotify_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001071 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1072 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001073 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001074 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001075
1076 return 0;
1077}
1078device_initcall(fanotify_user_setup);