blob: 096503bd0edbc22d810e937bd9c92477dcfc6747 [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>
Eric Parisa1014f12009-12-17 21:24:26 -050020
21#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050022
Al Viroc63181e2011-11-25 02:35:16 -050023#include "../../mount.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080024#include "../fdinfo.h"
Jan Kara7053aee2014-01-21 15:48:14 -080025#include "fanotify.h"
Al Viroc63181e2011-11-25 02:35:16 -050026
Eric Paris2529a0d2010-10-28 17:21:57 -040027#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Eric Parise7099d82010-10-28 17:21:57 -040028#define FANOTIFY_DEFAULT_MAX_MARKS 8192
Eric Paris4afeff82010-10-28 17:21:58 -040029#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
Eric Paris2529a0d2010-10-28 17:21:57 -040030
Heinrich Schuchardt48149e92014-06-04 16:05:44 -070031/*
32 * All flags that may be specified in parameter event_f_flags of fanotify_init.
33 *
34 * Internal and external open flags are stored together in field f_flags of
35 * struct file. Only external open flags shall be allowed in event_f_flags.
36 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
37 * excluded.
38 */
39#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
40 O_ACCMODE | O_APPEND | O_NONBLOCK | \
41 __O_SYNC | O_DSYNC | O_CLOEXEC | \
42 O_LARGEFILE | O_NOATIME )
43
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050044extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050045
Jan Kara054c6362016-12-21 18:06:12 +010046struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7053aee2014-01-21 15:48:14 -080047struct kmem_cache *fanotify_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070048struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050049
Eric Parisa1014f12009-12-17 21:24:26 -050050/*
51 * Get an fsnotify notification event if one exists and is small
52 * enough to fit in "count". Return an error pointer if the count
53 * is not large enough.
54 *
Jan Karac21dbe22016-10-07 16:56:52 -070055 * Called with the group->notification_lock held.
Eric Parisa1014f12009-12-17 21:24:26 -050056 */
57static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
58 size_t count)
59{
Jan Karaed272642016-10-07 16:57:01 -070060 assert_spin_locked(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -050061
62 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
63
64 if (fsnotify_notify_queue_is_empty(group))
65 return NULL;
66
67 if (FAN_EVENT_METADATA_LEN > count)
68 return ERR_PTR(-EINVAL);
69
Jan Karac21dbe22016-10-07 16:56:52 -070070 /* held the notification_lock the whole time, so this is the
Eric Parisa1014f12009-12-17 21:24:26 -050071 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -070072 return fsnotify_remove_first_event(group);
Eric Parisa1014f12009-12-17 21:24:26 -050073}
74
Al Viro352e3b22012-08-19 12:30:45 -040075static int create_fd(struct fsnotify_group *group,
Amir Goldstein33913992019-01-10 19:04:32 +020076 struct fanotify_event *event,
Jan Kara7053aee2014-01-21 15:48:14 -080077 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -050078{
79 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050080 struct file *new_file;
81
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050082 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050083
Yann Droneaud0b37e092014-10-09 15:24:40 -070084 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -050085 if (client_fd < 0)
86 return client_fd;
87
Eric Parisa1014f12009-12-17 21:24:26 -050088 /*
89 * we need a new file handle for the userspace program so it can read even if it was
90 * originally opened O_WRONLY.
91 */
Eric Parisa1014f12009-12-17 21:24:26 -050092 /* it's possible this event was an overflow event. in that case dentry and mnt
93 * are NULL; That's fine, just don't call dentry open */
Al Viro765927b2012-06-26 21:58:53 +040094 if (event->path.dentry && event->path.mnt)
95 new_file = dentry_open(&event->path,
Eric Paris80af2582010-07-28 10:18:37 -040096 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -050097 current_cred());
98 else
99 new_file = ERR_PTR(-EOVERFLOW);
100 if (IS_ERR(new_file)) {
101 /*
102 * we still send an event even if we can't open the file. this
103 * can happen when say tasks are gone and we try to open their
104 * /proc files or we try to open a WRONLY file like in sysfs
105 * we just send the errno to userspace since there isn't much
106 * else we can do.
107 */
108 put_unused_fd(client_fd);
109 client_fd = PTR_ERR(new_file);
110 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400111 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500112 }
113
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500114 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500115}
116
Amir Goldstein33913992019-01-10 19:04:32 +0200117static struct fanotify_perm_event *dequeue_event(
Jan Karaf0834412014-04-03 14:46:33 -0700118 struct fsnotify_group *group, int fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500119{
Amir Goldstein33913992019-01-10 19:04:32 +0200120 struct fanotify_perm_event *event, *return_e = NULL;
Eric Parisb2d87902009-12-17 21:24:34 -0500121
Jan Kara073f6552016-10-07 16:56:55 -0700122 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700123 list_for_each_entry(event, &group->fanotify_data.access_list,
124 fae.fse.list) {
125 if (event->fd != fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500126 continue;
127
Jan Karaf0834412014-04-03 14:46:33 -0700128 list_del_init(&event->fae.fse.list);
129 return_e = event;
Eric Parisb2d87902009-12-17 21:24:34 -0500130 break;
131 }
Jan Kara073f6552016-10-07 16:56:55 -0700132 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500133
Jan Karaf0834412014-04-03 14:46:33 -0700134 pr_debug("%s: found return_re=%p\n", __func__, return_e);
Eric Parisb2d87902009-12-17 21:24:34 -0500135
Jan Karaf0834412014-04-03 14:46:33 -0700136 return return_e;
Eric Parisb2d87902009-12-17 21:24:34 -0500137}
138
139static int process_access_response(struct fsnotify_group *group,
140 struct fanotify_response *response_struct)
141{
Amir Goldstein33913992019-01-10 19:04:32 +0200142 struct fanotify_perm_event *event;
Jan Karaf0834412014-04-03 14:46:33 -0700143 int fd = response_struct->fd;
144 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500145
146 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
147 fd, response);
148 /*
149 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300150 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500151 * timeout
152 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400153 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500154 case FAN_ALLOW:
155 case FAN_DENY:
156 break;
157 default:
158 return -EINVAL;
159 }
160
161 if (fd < 0)
162 return -EINVAL;
163
Amir Goldstein96a71f22018-09-21 21:20:30 +0300164 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400165 return -EINVAL;
166
Jan Karaf0834412014-04-03 14:46:33 -0700167 event = dequeue_event(group, fd);
168 if (!event)
Eric Parisb2d87902009-12-17 21:24:34 -0500169 return -ENOENT;
170
Jan Karaf0834412014-04-03 14:46:33 -0700171 event->response = response;
Eric Parisb2d87902009-12-17 21:24:34 -0500172 wake_up(&group->fanotify_data.access_waitq);
173
Eric Parisb2d87902009-12-17 21:24:34 -0500174 return 0;
175}
Eric Parisb2d87902009-12-17 21:24:34 -0500176
Eric Parisa1014f12009-12-17 21:24:26 -0500177static ssize_t copy_event_to_user(struct fsnotify_group *group,
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200178 struct fsnotify_event *fsn_event,
Kees Cook5b03a472018-12-04 15:44:46 -0800179 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500180{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200181 struct fanotify_event_metadata metadata;
182 struct fanotify_event *event;
183 struct file *f = NULL;
Eric Parisb2d87902009-12-17 21:24:34 -0500184 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500185
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200186 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
Eric Parisa1014f12009-12-17 21:24:26 -0500187
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200188 event = container_of(fsn_event, struct fanotify_event, fse);
189 metadata.event_len = FAN_EVENT_METADATA_LEN;
190 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
191 metadata.vers = FANOTIFY_METADATA_VERSION;
192 metadata.reserved = 0;
193 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
194 metadata.pid = pid_vnr(event->pid);
Eric Parisa1014f12009-12-17 21:24:26 -0500195
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200196 if (unlikely(event->mask & FAN_Q_OVERFLOW)) {
197 fd = FAN_NOFD;
198 } else {
199 fd = create_fd(group, event, &f);
200 if (fd < 0)
201 return fd;
202 }
203 metadata.fd = fd;
204
Al Viro352e3b22012-08-19 12:30:45 -0400205 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800206 /*
207 * Sanity check copy size in case get_one_event() and
208 * fill_event_metadata() event_len sizes ever get out of sync.
209 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200210 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400211 goto out_close_fd;
212
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200213 if (copy_to_user(buf, &metadata, metadata.event_len))
214 goto out_close_fd;
215
216 if (fanotify_is_perm_event(event->mask))
217 FANOTIFY_PE(fsn_event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500218
Al Viro3587b1b2012-11-18 19:19:00 +0000219 if (fd != FAN_NOFD)
220 fd_install(fd, f);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200221 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500222
Eric Parisb2d87902009-12-17 21:24:34 -0500223out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400224 if (fd != FAN_NOFD) {
225 put_unused_fd(fd);
226 fput(f);
227 }
Eric Parisb2d87902009-12-17 21:24:34 -0500228 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500229}
230
231/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400232static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500233{
234 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400235 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500236
237 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700238 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500239 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800240 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700241 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500242
243 return ret;
244}
245
246static ssize_t fanotify_read(struct file *file, char __user *buf,
247 size_t count, loff_t *pos)
248{
249 struct fsnotify_group *group;
250 struct fsnotify_event *kevent;
251 char __user *start;
252 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100253 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500254
255 start = buf;
256 group = file->private_data;
257
258 pr_debug("%s: group=%p\n", __func__, group);
259
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100260 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500261 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700262 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500263 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700264 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500265
Jan Karad8aaab42014-04-03 14:46:35 -0700266 if (IS_ERR(kevent)) {
Eric Parisa1014f12009-12-17 21:24:26 -0500267 ret = PTR_ERR(kevent);
Jan Karad8aaab42014-04-03 14:46:35 -0700268 break;
269 }
270
271 if (!kevent) {
272 ret = -EAGAIN;
273 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500274 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700275
276 ret = -ERESTARTSYS;
277 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500278 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700279
280 if (start != buf)
281 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100282
283 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500284 continue;
285 }
286
Kees Cook5b03a472018-12-04 15:44:46 -0800287 ret = copy_event_to_user(group, kevent, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300288 if (unlikely(ret == -EOPENSTALE)) {
289 /*
290 * We cannot report events with stale fd so drop it.
291 * Setting ret to 0 will continue the event loop and
292 * do the right thing if there are no more events to
293 * read (i.e. return bytes read, -EAGAIN or wait).
294 */
295 ret = 0;
296 }
297
Jan Karad8aaab42014-04-03 14:46:35 -0700298 /*
299 * Permission events get queued to wait for response. Other
300 * events can be destroyed now.
301 */
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200302 if (!fanotify_is_perm_event(FANOTIFY_E(kevent)->mask)) {
Jan Karad8aaab42014-04-03 14:46:35 -0700303 fsnotify_destroy_event(group, kevent);
Jan Karad5078162014-04-03 14:46:36 -0700304 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300305 if (ret <= 0) {
Jan Karad5078162014-04-03 14:46:36 -0700306 FANOTIFY_PE(kevent)->response = FAN_DENY;
307 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300308 } else {
309 spin_lock(&group->notification_lock);
310 list_add_tail(&kevent->list,
311 &group->fanotify_data.access_list);
312 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700313 }
Jan Karad5078162014-04-03 14:46:36 -0700314 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300315 if (ret < 0)
316 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700317 buf += ret;
318 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500319 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100320 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500321
Eric Parisa1014f12009-12-17 21:24:26 -0500322 if (start != buf && ret != -EFAULT)
323 ret = buf - start;
324 return ret;
325}
326
Eric Parisb2d87902009-12-17 21:24:34 -0500327static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
328{
Eric Parisb2d87902009-12-17 21:24:34 -0500329 struct fanotify_response response = { .fd = -1, .response = -1 };
330 struct fsnotify_group *group;
331 int ret;
332
Miklos Szeredi6685df32017-10-30 21:14:56 +0100333 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
334 return -EINVAL;
335
Eric Parisb2d87902009-12-17 21:24:34 -0500336 group = file->private_data;
337
338 if (count > sizeof(response))
339 count = sizeof(response);
340
341 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
342
343 if (copy_from_user(&response, buf, count))
344 return -EFAULT;
345
346 ret = process_access_response(group, &response);
347 if (ret < 0)
348 count = ret;
349
350 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500351}
352
Eric Paris52c923d2009-12-17 21:24:26 -0500353static int fanotify_release(struct inode *ignored, struct file *file)
354{
355 struct fsnotify_group *group = file->private_data;
Amir Goldstein33913992019-01-10 19:04:32 +0200356 struct fanotify_perm_event *event, *next;
Jan Kara96d41012016-09-19 14:44:30 -0700357 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400358
Jan Kara5838d442014-08-06 16:03:28 -0700359 /*
Jan Kara96d41012016-09-19 14:44:30 -0700360 * Stop new events from arriving in the notification queue. since
361 * userspace cannot use fanotify fd anymore, no event can enter or
362 * leave access_list by now either.
363 */
364 fsnotify_group_stop_queueing(group);
365
366 /*
367 * Process all permission events on access_list and notification queue
368 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700369 */
Jan Kara073f6552016-10-07 16:56:55 -0700370 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700371 list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
372 fae.fse.list) {
373 pr_debug("%s: found group=%p event=%p\n", __func__, group,
374 event);
Eric Paris2eebf582010-08-18 12:25:50 -0400375
Jan Karaf0834412014-04-03 14:46:33 -0700376 list_del_init(&event->fae.fse.list);
377 event->response = FAN_ALLOW;
Eric Paris2eebf582010-08-18 12:25:50 -0400378 }
Eric Paris2eebf582010-08-18 12:25:50 -0400379
Jan Kara5838d442014-08-06 16:03:28 -0700380 /*
Jan Kara96d41012016-09-19 14:44:30 -0700381 * Destroy all non-permission events. For permission events just
382 * dequeue them and set the response. They will be freed once the
383 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700384 */
Jan Kara96d41012016-09-19 14:44:30 -0700385 while (!fsnotify_notify_queue_is_empty(group)) {
386 fsn_event = fsnotify_remove_first_event(group);
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200387 if (!(FANOTIFY_E(fsn_event)->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700388 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700389 fsnotify_destroy_event(group, fsn_event);
Jan Karac21dbe22016-10-07 16:56:52 -0700390 spin_lock(&group->notification_lock);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100391 } else {
Jan Kara96d41012016-09-19 14:44:30 -0700392 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100393 }
Jan Kara96d41012016-09-19 14:44:30 -0700394 }
Jan Karac21dbe22016-10-07 16:56:52 -0700395 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700396
397 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400398 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400399
Eric Paris52c923d2009-12-17 21:24:26 -0500400 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200401 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500402
403 return 0;
404}
405
Eric Parisa1014f12009-12-17 21:24:26 -0500406static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
407{
408 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800409 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500410 void __user *p;
411 int ret = -ENOTTY;
412 size_t send_len = 0;
413
414 group = file->private_data;
415
416 p = (void __user *) arg;
417
418 switch (cmd) {
419 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700420 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800421 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500422 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700423 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500424 ret = put_user(send_len, (int __user *) p);
425 break;
426 }
427
428 return ret;
429}
430
Eric Paris52c923d2009-12-17 21:24:26 -0500431static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800432 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500433 .poll = fanotify_poll,
434 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500435 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500436 .fasync = NULL,
437 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500438 .unlocked_ioctl = fanotify_ioctl,
439 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200440 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500441};
442
Eric Paris2a3edf82009-12-17 21:24:26 -0500443static int fanotify_find_path(int dfd, const char __user *filename,
444 struct path *path, unsigned int flags)
445{
446 int ret;
447
448 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
449 dfd, filename, flags);
450
451 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400452 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500453
454 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400455 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500456 goto out;
457
458 ret = -ENOTDIR;
459 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500460 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400461 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500462 goto out;
463 }
464
Al Viro2903ff02012-08-28 12:52:22 -0400465 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500466 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400467 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500468 } else {
469 unsigned int lookup_flags = 0;
470
471 if (!(flags & FAN_MARK_DONT_FOLLOW))
472 lookup_flags |= LOOKUP_FOLLOW;
473 if (flags & FAN_MARK_ONLYDIR)
474 lookup_flags |= LOOKUP_DIRECTORY;
475
476 ret = user_path_at(dfd, filename, lookup_flags, path);
477 if (ret)
478 goto out;
479 }
480
481 /* you can only watch an inode if you have read permissions on it */
482 ret = inode_permission(path->dentry->d_inode, MAY_READ);
483 if (ret)
484 path_put(path);
485out:
486 return ret;
487}
488
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500489static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
490 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200491 unsigned int flags,
492 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500493{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800494 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500495
496 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500497 if (!(flags & FAN_MARK_IGNORED_MASK)) {
498 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300499 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500500 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300501 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500502 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800503 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500504 spin_unlock(&fsn_mark->lock);
505
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500506 return mask & oldmask;
507}
508
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300509static int fanotify_remove_mark(struct fsnotify_group *group,
510 fsnotify_connp_t *connp, __u32 mask,
511 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500512{
513 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500514 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200515 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500516
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700517 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300518 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700519 if (!fsn_mark) {
520 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500521 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700522 }
Eric Paris88826272009-12-17 21:24:28 -0500523
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200524 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
525 &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300526 if (removed & fsnotify_conn_mask(fsn_mark->connector))
527 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200528 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700529 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700530 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700531 if (destroy_mark)
532 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700533
Jan Karab1362ed2016-12-21 16:28:45 +0100534 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500535 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500536 return 0;
537}
538
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300539static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
540 struct vfsmount *mnt, __u32 mask,
541 unsigned int flags)
542{
543 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
544 mask, flags);
545}
546
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300547static int fanotify_remove_sb_mark(struct fsnotify_group *group,
548 struct super_block *sb, __u32 mask,
549 unsigned int flags)
550{
551 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
552}
553
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300554static int fanotify_remove_inode_mark(struct fsnotify_group *group,
555 struct inode *inode, __u32 mask,
556 unsigned int flags)
557{
558 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
559 flags);
560}
561
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500562static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
563 __u32 mask,
564 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500565{
Eric Paris192ca4d2010-10-28 17:21:59 -0400566 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-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 Parisc9778a92009-12-17 21:24:33 -0500574 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
575 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500576 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500577 spin_unlock(&fsn_mark->lock);
578
579 return mask & ~oldmask;
580}
581
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700582static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300583 fsnotify_connp_t *connp,
584 unsigned int type)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700585{
586 struct fsnotify_mark *mark;
587 int ret;
588
589 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
590 return ERR_PTR(-ENOSPC);
591
592 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
593 if (!mark)
594 return ERR_PTR(-ENOMEM);
595
Jan Kara054c6362016-12-21 18:06:12 +0100596 fsnotify_init_mark(mark, group);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300597 ret = fsnotify_add_mark_locked(mark, connp, type, 0);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700598 if (ret) {
599 fsnotify_put_mark(mark);
600 return ERR_PTR(ret);
601 }
602
603 return mark;
604}
605
606
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300607static int fanotify_add_mark(struct fsnotify_group *group,
608 fsnotify_connp_t *connp, unsigned int type,
609 __u32 mask, unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500610{
611 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500612 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500613
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700614 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300615 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500616 if (!fsn_mark) {
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300617 fsn_mark = fanotify_add_new_mark(group, connp, type);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700618 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700619 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700620 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700621 }
Eric Paris88826272009-12-17 21:24:28 -0500622 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500623 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300624 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
625 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100626 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700627
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100628 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700629 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500630}
631
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300632static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
633 struct vfsmount *mnt, __u32 mask,
634 unsigned int flags)
635{
636 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
637 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags);
638}
639
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300640static int fanotify_add_sb_mark(struct fsnotify_group *group,
641 struct super_block *sb, __u32 mask,
642 unsigned int flags)
643{
644 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
645 FSNOTIFY_OBJ_TYPE_SB, mask, flags);
646}
647
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500648static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500649 struct inode *inode, __u32 mask,
650 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500651{
Eric Paris88826272009-12-17 21:24:28 -0500652 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500653
Eric Paris5322a592010-10-28 17:21:57 -0400654 /*
655 * If some other task has this inode open for write we should not add
656 * an ignored mark, unless that ignored mark is supposed to survive
657 * modification changes anyway.
658 */
659 if ((flags & FAN_MARK_IGNORED_MASK) &&
660 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +0200661 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -0400662 return 0;
663
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300664 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
665 FSNOTIFY_OBJ_TYPE_INODE, mask, flags);
Eric Paris88826272009-12-17 21:24:28 -0500666}
Eric Paris2a3edf82009-12-17 21:24:26 -0500667
Eric Paris52c923d2009-12-17 21:24:26 -0500668/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400669SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500670{
Eric Paris52c923d2009-12-17 21:24:26 -0500671 struct fsnotify_group *group;
672 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400673 struct user_struct *user;
Amir Goldstein33913992019-01-10 19:04:32 +0200674 struct fanotify_event *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500675
Amir Goldstein96a71f22018-09-21 21:20:30 +0300676 pr_debug("%s: flags=%x event_f_flags=%x\n",
677 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500678
Eric Paris52c923d2009-12-17 21:24:26 -0500679 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200680 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500681
Steve Grubbde8cd832017-10-02 20:21:39 -0400682#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300683 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400684#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300685 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400686#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500687 return -EINVAL;
688
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700689 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
690 return -EINVAL;
691
692 switch (event_f_flags & O_ACCMODE) {
693 case O_RDONLY:
694 case O_RDWR:
695 case O_WRONLY:
696 break;
697 default:
698 return -EINVAL;
699 }
700
Eric Paris4afeff82010-10-28 17:21:58 -0400701 user = get_current_user();
702 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
703 free_uid(user);
704 return -EMFILE;
705 }
706
Eric Parisb2d87902009-12-17 21:24:34 -0500707 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500708 if (flags & FAN_CLOEXEC)
709 f_flags |= O_CLOEXEC;
710 if (flags & FAN_NONBLOCK)
711 f_flags |= O_NONBLOCK;
712
713 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
714 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500715 if (IS_ERR(group)) {
716 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500717 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500718 }
Eric Paris52c923d2009-12-17 21:24:26 -0500719
Eric Paris4afeff82010-10-28 17:21:58 -0400720 group->fanotify_data.user = user;
Amir Goldstein96a71f22018-09-21 21:20:30 +0300721 group->fanotify_data.flags = flags;
Eric Paris4afeff82010-10-28 17:21:58 -0400722 atomic_inc(&user->fanotify_listeners);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700723 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -0400724
Jan Kara1f5eaa92018-02-21 14:10:59 +0100725 oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100726 if (unlikely(!oevent)) {
727 fd = -ENOMEM;
728 goto out_destroy_group;
729 }
730 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100731
Will Woods1e2ee492014-05-06 12:50:10 -0700732 if (force_o_largefile())
733 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400734 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500735 init_waitqueue_head(&group->fanotify_data.access_waitq);
736 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300737 switch (flags & FANOTIFY_CLASS_BITS) {
Eric Paris4231a232010-10-28 17:21:56 -0400738 case FAN_CLASS_NOTIF:
739 group->priority = FS_PRIO_0;
740 break;
741 case FAN_CLASS_CONTENT:
742 group->priority = FS_PRIO_1;
743 break;
744 case FAN_CLASS_PRE_CONTENT:
745 group->priority = FS_PRIO_2;
746 break;
747 default:
748 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200749 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400750 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500751
Eric Paris5dd03f52010-10-28 17:21:57 -0400752 if (flags & FAN_UNLIMITED_QUEUE) {
753 fd = -EPERM;
754 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200755 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400756 group->max_events = UINT_MAX;
757 } else {
758 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
759 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400760
Eric Parisac7e22d2010-10-28 17:21:58 -0400761 if (flags & FAN_UNLIMITED_MARKS) {
762 fd = -EPERM;
763 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200764 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400765 group->fanotify_data.max_marks = UINT_MAX;
766 } else {
767 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
768 }
Eric Parise7099d82010-10-28 17:21:57 -0400769
Steve Grubbde8cd832017-10-02 20:21:39 -0400770 if (flags & FAN_ENABLE_AUDIT) {
771 fd = -EPERM;
772 if (!capable(CAP_AUDIT_WRITE))
773 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -0400774 }
775
Eric Paris52c923d2009-12-17 21:24:26 -0500776 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
777 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200778 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500779
780 return fd;
781
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200782out_destroy_group:
783 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500784 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500785}
Eric Parisbbaa4162009-12-17 21:24:26 -0500786
Dominik Brodowski183caa32018-03-17 15:06:11 +0100787static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
788 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500789{
Eric Paris0ff21db2009-12-17 21:24:29 -0500790 struct inode *inode = NULL;
791 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500792 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400793 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500794 struct path path;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +0300795 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300796 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Al Viro2903ff02012-08-28 12:52:22 -0400797 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500798
799 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
800 __func__, fanotify_fd, flags, dfd, pathname, mask);
801
802 /* we only use the lower 32 bits as of right now. */
803 if (mask & ((__u64)0xffffffff << 32))
804 return -EINVAL;
805
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300806 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500807 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300808
809 switch (mark_type) {
810 case FAN_MARK_INODE:
811 case FAN_MARK_MOUNT:
812 case FAN_MARK_FILESYSTEM:
813 break;
814 default:
815 return -EINVAL;
816 }
817
Eric Paris4d926042009-12-17 21:24:34 -0500818 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100819 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500820 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100821 if (!mask)
822 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700823 break;
Eric Paris4d926042009-12-17 21:24:34 -0500824 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300825 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700826 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500827 break;
828 default:
829 return -EINVAL;
830 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400831
Miklos Szeredi6685df32017-10-30 21:14:56 +0100832 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300833 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100834
835 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -0500836 return -EINVAL;
837
Al Viro2903ff02012-08-28 12:52:22 -0400838 f = fdget(fanotify_fd);
839 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -0500840 return -EBADF;
841
842 /* verify that this is indeed an fanotify instance */
843 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400844 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -0500845 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -0400846 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -0400847
848 /*
849 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
850 * allowed to set permissions events.
851 */
852 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300853 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -0400854 group->priority == FS_PRIO_0)
855 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500856
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700857 if (flags & FAN_MARK_FLUSH) {
858 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300859 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700860 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300861 else if (mark_type == FAN_MARK_FILESYSTEM)
862 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700863 else
864 fsnotify_clear_inode_marks_by_group(group);
865 goto fput_and_out;
866 }
867
Eric Paris2a3edf82009-12-17 21:24:26 -0500868 ret = fanotify_find_path(dfd, pathname, &path, flags);
869 if (ret)
870 goto fput_and_out;
871
872 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300873 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -0500874 inode = path.dentry->d_inode;
875 else
876 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500877
878 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700879 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500880 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300881 if (mark_type == FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500882 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300883 else if (mark_type == FAN_MARK_FILESYSTEM)
884 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500885 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500886 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500887 break;
888 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300889 if (mark_type == FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500890 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300891 else if (mark_type == FAN_MARK_FILESYSTEM)
892 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500893 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500894 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500895 break;
896 default:
897 ret = -EINVAL;
898 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500899
900 path_put(&path);
901fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400902 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500903 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500904}
Eric Paris2a3edf82009-12-17 21:24:26 -0500905
Dominik Brodowski183caa32018-03-17 15:06:11 +0100906SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
907 __u64, mask, int, dfd,
908 const char __user *, pathname)
909{
910 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
911}
912
Al Viro91c2e0b2013-03-05 20:10:59 -0500913#ifdef CONFIG_COMPAT
914COMPAT_SYSCALL_DEFINE6(fanotify_mark,
915 int, fanotify_fd, unsigned int, flags,
916 __u32, mask0, __u32, mask1, int, dfd,
917 const char __user *, pathname)
918{
Dominik Brodowski183caa32018-03-17 15:06:11 +0100919 return do_fanotify_mark(fanotify_fd, flags,
Al Viro91c2e0b2013-03-05 20:10:59 -0500920#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -0500921 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -0800922#else
923 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -0500924#endif
925 dfd, pathname);
926}
927#endif
928
Eric Paris2a3edf82009-12-17 21:24:26 -0500929/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100930 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500931 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
932 * must result in panic().
933 */
934static int __init fanotify_user_setup(void)
935{
Amir Goldsteind0a6a872018-10-04 00:25:38 +0300936 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +0300937 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
938
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700939 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
940 SLAB_PANIC|SLAB_ACCOUNT);
Amir Goldstein33913992019-01-10 19:04:32 +0200941 fanotify_event_cachep = KMEM_CACHE(fanotify_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100942 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
943 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +0200944 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100945 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500946
947 return 0;
948}
949device_initcall(fanotify_user_setup);