blob: 15719d4aa4b56432f0a51e28803d3eb681f3447c [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,
Jan Kara7053aee2014-01-21 15:48:14 -080076 struct fanotify_event_info *event,
77 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
Eric Parisecf6f5e2010-11-08 18:08:14 -0500117static int fill_event_metadata(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800118 struct fanotify_event_metadata *metadata,
119 struct fsnotify_event *fsn_event,
120 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500121{
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100122 int ret = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800123 struct fanotify_event_info *event;
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100124
Eric Parisa1014f12009-12-17 21:24:26 -0500125 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
Jan Kara7053aee2014-01-21 15:48:14 -0800126 group, metadata, fsn_event);
Eric Parisa1014f12009-12-17 21:24:26 -0500127
Al Viro352e3b22012-08-19 12:30:45 -0400128 *file = NULL;
Jan Kara7053aee2014-01-21 15:48:14 -0800129 event = container_of(fsn_event, struct fanotify_event_info, fse);
Eric Parisa1014f12009-12-17 21:24:26 -0500130 metadata->event_len = FAN_EVENT_METADATA_LEN;
Eric Paris7d131622010-12-07 15:27:57 -0500131 metadata->metadata_len = FAN_EVENT_METADATA_LEN;
Eric Parisa1014f12009-12-17 21:24:26 -0500132 metadata->vers = FANOTIFY_METADATA_VERSION;
Dan Carpenterde1e0c42013-07-08 15:59:40 -0700133 metadata->reserved = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800134 metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500135 metadata->pid = pid_vnr(event->tgid);
Jan Kara7053aee2014-01-21 15:48:14 -0800136 if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100137 metadata->fd = FAN_NOFD;
138 else {
Al Viro352e3b22012-08-19 12:30:45 -0400139 metadata->fd = create_fd(group, event, file);
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100140 if (metadata->fd < 0)
141 ret = metadata->fd;
142 }
Eric Parisa1014f12009-12-17 21:24:26 -0500143
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100144 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500145}
146
Jan Karaf0834412014-04-03 14:46:33 -0700147static struct fanotify_perm_event_info *dequeue_event(
148 struct fsnotify_group *group, int fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500149{
Jan Karaf0834412014-04-03 14:46:33 -0700150 struct fanotify_perm_event_info *event, *return_e = NULL;
Eric Parisb2d87902009-12-17 21:24:34 -0500151
Jan Kara073f6552016-10-07 16:56:55 -0700152 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700153 list_for_each_entry(event, &group->fanotify_data.access_list,
154 fae.fse.list) {
155 if (event->fd != fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500156 continue;
157
Jan Karaf0834412014-04-03 14:46:33 -0700158 list_del_init(&event->fae.fse.list);
159 return_e = event;
Eric Parisb2d87902009-12-17 21:24:34 -0500160 break;
161 }
Jan Kara073f6552016-10-07 16:56:55 -0700162 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500163
Jan Karaf0834412014-04-03 14:46:33 -0700164 pr_debug("%s: found return_re=%p\n", __func__, return_e);
Eric Parisb2d87902009-12-17 21:24:34 -0500165
Jan Karaf0834412014-04-03 14:46:33 -0700166 return return_e;
Eric Parisb2d87902009-12-17 21:24:34 -0500167}
168
169static int process_access_response(struct fsnotify_group *group,
170 struct fanotify_response *response_struct)
171{
Jan Karaf0834412014-04-03 14:46:33 -0700172 struct fanotify_perm_event_info *event;
173 int fd = response_struct->fd;
174 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500175
176 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
177 fd, response);
178 /*
179 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300180 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500181 * timeout
182 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400183 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500184 case FAN_ALLOW:
185 case FAN_DENY:
186 break;
187 default:
188 return -EINVAL;
189 }
190
191 if (fd < 0)
192 return -EINVAL;
193
Amir Goldstein96a71f22018-09-21 21:20:30 +0300194 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400195 return -EINVAL;
196
Jan Karaf0834412014-04-03 14:46:33 -0700197 event = dequeue_event(group, fd);
198 if (!event)
Eric Parisb2d87902009-12-17 21:24:34 -0500199 return -ENOENT;
200
Jan Karaf0834412014-04-03 14:46:33 -0700201 event->response = response;
Eric Parisb2d87902009-12-17 21:24:34 -0500202 wake_up(&group->fanotify_data.access_waitq);
203
Eric Parisb2d87902009-12-17 21:24:34 -0500204 return 0;
205}
Eric Parisb2d87902009-12-17 21:24:34 -0500206
Eric Parisa1014f12009-12-17 21:24:26 -0500207static ssize_t copy_event_to_user(struct fsnotify_group *group,
208 struct fsnotify_event *event,
209 char __user *buf)
210{
211 struct fanotify_event_metadata fanotify_event_metadata;
Al Viro352e3b22012-08-19 12:30:45 -0400212 struct file *f;
Eric Parisb2d87902009-12-17 21:24:34 -0500213 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500214
215 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
216
Al Viro352e3b22012-08-19 12:30:45 -0400217 ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
Eric Parisecf6f5e2010-11-08 18:08:14 -0500218 if (ret < 0)
Jan Karad5078162014-04-03 14:46:36 -0700219 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500220
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100221 fd = fanotify_event_metadata.fd;
Al Viro352e3b22012-08-19 12:30:45 -0400222 ret = -EFAULT;
223 if (copy_to_user(buf, &fanotify_event_metadata,
224 fanotify_event_metadata.event_len))
225 goto out_close_fd;
226
Miklos Szeredi6685df32017-10-30 21:14:56 +0100227 if (fanotify_is_perm_event(event->mask))
Jan Karad5078162014-04-03 14:46:36 -0700228 FANOTIFY_PE(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500229
Al Viro3587b1b2012-11-18 19:19:00 +0000230 if (fd != FAN_NOFD)
231 fd_install(fd, f);
Eric Paris7d131622010-12-07 15:27:57 -0500232 return fanotify_event_metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500233
Eric Parisb2d87902009-12-17 21:24:34 -0500234out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400235 if (fd != FAN_NOFD) {
236 put_unused_fd(fd);
237 fput(f);
238 }
Eric Parisb2d87902009-12-17 21:24:34 -0500239 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500240}
241
242/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400243static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500244{
245 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400246 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500247
248 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700249 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500250 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800251 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700252 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500253
254 return ret;
255}
256
257static ssize_t fanotify_read(struct file *file, char __user *buf,
258 size_t count, loff_t *pos)
259{
260 struct fsnotify_group *group;
261 struct fsnotify_event *kevent;
262 char __user *start;
263 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100264 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500265
266 start = buf;
267 group = file->private_data;
268
269 pr_debug("%s: group=%p\n", __func__, group);
270
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100271 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500272 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700273 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500274 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700275 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500276
Jan Karad8aaab42014-04-03 14:46:35 -0700277 if (IS_ERR(kevent)) {
Eric Parisa1014f12009-12-17 21:24:26 -0500278 ret = PTR_ERR(kevent);
Jan Karad8aaab42014-04-03 14:46:35 -0700279 break;
280 }
281
282 if (!kevent) {
283 ret = -EAGAIN;
284 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500285 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700286
287 ret = -ERESTARTSYS;
288 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500289 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700290
291 if (start != buf)
292 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100293
294 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500295 continue;
296 }
297
Jan Karad8aaab42014-04-03 14:46:35 -0700298 ret = copy_event_to_user(group, kevent, buf);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300299 if (unlikely(ret == -EOPENSTALE)) {
300 /*
301 * We cannot report events with stale fd so drop it.
302 * Setting ret to 0 will continue the event loop and
303 * do the right thing if there are no more events to
304 * read (i.e. return bytes read, -EAGAIN or wait).
305 */
306 ret = 0;
307 }
308
Jan Karad8aaab42014-04-03 14:46:35 -0700309 /*
310 * Permission events get queued to wait for response. Other
311 * events can be destroyed now.
312 */
Miklos Szeredi6685df32017-10-30 21:14:56 +0100313 if (!fanotify_is_perm_event(kevent->mask)) {
Jan Karad8aaab42014-04-03 14:46:35 -0700314 fsnotify_destroy_event(group, kevent);
Jan Karad5078162014-04-03 14:46:36 -0700315 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300316 if (ret <= 0) {
Jan Karad5078162014-04-03 14:46:36 -0700317 FANOTIFY_PE(kevent)->response = FAN_DENY;
318 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300319 } else {
320 spin_lock(&group->notification_lock);
321 list_add_tail(&kevent->list,
322 &group->fanotify_data.access_list);
323 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700324 }
Jan Karad5078162014-04-03 14:46:36 -0700325 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300326 if (ret < 0)
327 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700328 buf += ret;
329 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500330 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100331 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500332
Eric Parisa1014f12009-12-17 21:24:26 -0500333 if (start != buf && ret != -EFAULT)
334 ret = buf - start;
335 return ret;
336}
337
Eric Parisb2d87902009-12-17 21:24:34 -0500338static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
339{
Eric Parisb2d87902009-12-17 21:24:34 -0500340 struct fanotify_response response = { .fd = -1, .response = -1 };
341 struct fsnotify_group *group;
342 int ret;
343
Miklos Szeredi6685df32017-10-30 21:14:56 +0100344 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
345 return -EINVAL;
346
Eric Parisb2d87902009-12-17 21:24:34 -0500347 group = file->private_data;
348
349 if (count > sizeof(response))
350 count = sizeof(response);
351
352 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
353
354 if (copy_from_user(&response, buf, count))
355 return -EFAULT;
356
357 ret = process_access_response(group, &response);
358 if (ret < 0)
359 count = ret;
360
361 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500362}
363
Eric Paris52c923d2009-12-17 21:24:26 -0500364static int fanotify_release(struct inode *ignored, struct file *file)
365{
366 struct fsnotify_group *group = file->private_data;
Jan Karaf0834412014-04-03 14:46:33 -0700367 struct fanotify_perm_event_info *event, *next;
Jan Kara96d41012016-09-19 14:44:30 -0700368 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400369
Jan Kara5838d442014-08-06 16:03:28 -0700370 /*
Jan Kara96d41012016-09-19 14:44:30 -0700371 * Stop new events from arriving in the notification queue. since
372 * userspace cannot use fanotify fd anymore, no event can enter or
373 * leave access_list by now either.
374 */
375 fsnotify_group_stop_queueing(group);
376
377 /*
378 * Process all permission events on access_list and notification queue
379 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700380 */
Jan Kara073f6552016-10-07 16:56:55 -0700381 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700382 list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
383 fae.fse.list) {
384 pr_debug("%s: found group=%p event=%p\n", __func__, group,
385 event);
Eric Paris2eebf582010-08-18 12:25:50 -0400386
Jan Karaf0834412014-04-03 14:46:33 -0700387 list_del_init(&event->fae.fse.list);
388 event->response = FAN_ALLOW;
Eric Paris2eebf582010-08-18 12:25:50 -0400389 }
Eric Paris2eebf582010-08-18 12:25:50 -0400390
Jan Kara5838d442014-08-06 16:03:28 -0700391 /*
Jan Kara96d41012016-09-19 14:44:30 -0700392 * Destroy all non-permission events. For permission events just
393 * dequeue them and set the response. They will be freed once the
394 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700395 */
Jan Kara96d41012016-09-19 14:44:30 -0700396 while (!fsnotify_notify_queue_is_empty(group)) {
397 fsn_event = fsnotify_remove_first_event(group);
Jan Kara1404ff32016-10-07 16:56:49 -0700398 if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700399 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700400 fsnotify_destroy_event(group, fsn_event);
Jan Karac21dbe22016-10-07 16:56:52 -0700401 spin_lock(&group->notification_lock);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100402 } else {
Jan Kara96d41012016-09-19 14:44:30 -0700403 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100404 }
Jan Kara96d41012016-09-19 14:44:30 -0700405 }
Jan Karac21dbe22016-10-07 16:56:52 -0700406 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700407
408 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400409 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400410
Eric Paris52c923d2009-12-17 21:24:26 -0500411 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200412 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500413
414 return 0;
415}
416
Eric Parisa1014f12009-12-17 21:24:26 -0500417static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
418{
419 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800420 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500421 void __user *p;
422 int ret = -ENOTTY;
423 size_t send_len = 0;
424
425 group = file->private_data;
426
427 p = (void __user *) arg;
428
429 switch (cmd) {
430 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700431 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800432 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500433 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700434 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500435 ret = put_user(send_len, (int __user *) p);
436 break;
437 }
438
439 return ret;
440}
441
Eric Paris52c923d2009-12-17 21:24:26 -0500442static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800443 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500444 .poll = fanotify_poll,
445 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500446 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500447 .fasync = NULL,
448 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500449 .unlocked_ioctl = fanotify_ioctl,
450 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200451 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500452};
453
Eric Paris2a3edf82009-12-17 21:24:26 -0500454static int fanotify_find_path(int dfd, const char __user *filename,
455 struct path *path, unsigned int flags)
456{
457 int ret;
458
459 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
460 dfd, filename, flags);
461
462 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400463 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500464
465 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400466 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500467 goto out;
468
469 ret = -ENOTDIR;
470 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500471 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400472 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500473 goto out;
474 }
475
Al Viro2903ff02012-08-28 12:52:22 -0400476 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500477 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400478 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500479 } else {
480 unsigned int lookup_flags = 0;
481
482 if (!(flags & FAN_MARK_DONT_FOLLOW))
483 lookup_flags |= LOOKUP_FOLLOW;
484 if (flags & FAN_MARK_ONLYDIR)
485 lookup_flags |= LOOKUP_DIRECTORY;
486
487 ret = user_path_at(dfd, filename, lookup_flags, path);
488 if (ret)
489 goto out;
490 }
491
492 /* you can only watch an inode if you have read permissions on it */
493 ret = inode_permission(path->dentry->d_inode, MAY_READ);
494 if (ret)
495 path_put(path);
496out:
497 return ret;
498}
499
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500500static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
501 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200502 unsigned int flags,
503 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500504{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800505 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500506
507 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500508 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800509 __u32 tmask = fsn_mark->mask & ~mask;
510
511 if (flags & FAN_MARK_ONDIR)
512 tmask &= ~FAN_ONDIR;
513
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500514 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100515 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500516 } else {
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800517 __u32 tmask = fsn_mark->ignored_mask & ~mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800518 if (flags & FAN_MARK_ONDIR)
519 tmask &= ~FAN_ONDIR;
Jan Kara66d2b812016-12-21 16:03:59 +0100520 fsn_mark->ignored_mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500521 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800522 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500523 spin_unlock(&fsn_mark->lock);
524
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500525 return mask & oldmask;
526}
527
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300528static int fanotify_remove_mark(struct fsnotify_group *group,
529 fsnotify_connp_t *connp, __u32 mask,
530 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500531{
532 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500533 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200534 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500535
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700536 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300537 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700538 if (!fsn_mark) {
539 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500540 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700541 }
Eric Paris88826272009-12-17 21:24:28 -0500542
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200543 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
544 &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300545 if (removed & fsnotify_conn_mask(fsn_mark->connector))
546 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200547 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700548 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700549 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700550 if (destroy_mark)
551 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700552
Jan Karab1362ed2016-12-21 16:28:45 +0100553 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500554 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500555 return 0;
556}
557
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300558static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
559 struct vfsmount *mnt, __u32 mask,
560 unsigned int flags)
561{
562 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
563 mask, flags);
564}
565
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300566static int fanotify_remove_sb_mark(struct fsnotify_group *group,
567 struct super_block *sb, __u32 mask,
568 unsigned int flags)
569{
570 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
571}
572
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300573static int fanotify_remove_inode_mark(struct fsnotify_group *group,
574 struct inode *inode, __u32 mask,
575 unsigned int flags)
576{
577 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
578 flags);
579}
580
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500581static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
582 __u32 mask,
583 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500584{
Eric Paris192ca4d2010-10-28 17:21:59 -0400585 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500586
587 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500588 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800589 __u32 tmask = fsn_mark->mask | mask;
590
591 if (flags & FAN_MARK_ONDIR)
592 tmask |= FAN_ONDIR;
593
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500594 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100595 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500596 } else {
Eric Paris192ca4d2010-10-28 17:21:59 -0400597 __u32 tmask = fsn_mark->ignored_mask | mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800598 if (flags & FAN_MARK_ONDIR)
599 tmask |= FAN_ONDIR;
600
Jan Kara66d2b812016-12-21 16:03:59 +0100601 fsn_mark->ignored_mask = tmask;
Eric Parisc9778a92009-12-17 21:24:33 -0500602 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
603 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500604 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500605 spin_unlock(&fsn_mark->lock);
606
607 return mask & ~oldmask;
608}
609
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700610static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300611 fsnotify_connp_t *connp,
612 unsigned int type)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700613{
614 struct fsnotify_mark *mark;
615 int ret;
616
617 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
618 return ERR_PTR(-ENOSPC);
619
620 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
621 if (!mark)
622 return ERR_PTR(-ENOMEM);
623
Jan Kara054c6362016-12-21 18:06:12 +0100624 fsnotify_init_mark(mark, group);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300625 ret = fsnotify_add_mark_locked(mark, connp, type, 0);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700626 if (ret) {
627 fsnotify_put_mark(mark);
628 return ERR_PTR(ret);
629 }
630
631 return mark;
632}
633
634
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300635static int fanotify_add_mark(struct fsnotify_group *group,
636 fsnotify_connp_t *connp, unsigned int type,
637 __u32 mask, unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500638{
639 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500640 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500641
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700642 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300643 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500644 if (!fsn_mark) {
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300645 fsn_mark = fanotify_add_new_mark(group, connp, type);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700646 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700647 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700648 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700649 }
Eric Paris88826272009-12-17 21:24:28 -0500650 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500651 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300652 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
653 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100654 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700655
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100656 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700657 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500658}
659
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300660static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
661 struct vfsmount *mnt, __u32 mask,
662 unsigned int flags)
663{
664 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
665 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags);
666}
667
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300668static int fanotify_add_sb_mark(struct fsnotify_group *group,
669 struct super_block *sb, __u32 mask,
670 unsigned int flags)
671{
672 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
673 FSNOTIFY_OBJ_TYPE_SB, mask, flags);
674}
675
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500676static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500677 struct inode *inode, __u32 mask,
678 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500679{
Eric Paris88826272009-12-17 21:24:28 -0500680 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500681
Eric Paris5322a592010-10-28 17:21:57 -0400682 /*
683 * If some other task has this inode open for write we should not add
684 * an ignored mark, unless that ignored mark is supposed to survive
685 * modification changes anyway.
686 */
687 if ((flags & FAN_MARK_IGNORED_MASK) &&
688 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
689 (atomic_read(&inode->i_writecount) > 0))
690 return 0;
691
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300692 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
693 FSNOTIFY_OBJ_TYPE_INODE, mask, flags);
Eric Paris88826272009-12-17 21:24:28 -0500694}
Eric Paris2a3edf82009-12-17 21:24:26 -0500695
Eric Paris52c923d2009-12-17 21:24:26 -0500696/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400697SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500698{
Eric Paris52c923d2009-12-17 21:24:26 -0500699 struct fsnotify_group *group;
700 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400701 struct user_struct *user;
Jan Karaff57cd52014-02-21 19:14:11 +0100702 struct fanotify_event_info *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500703
Amir Goldstein96a71f22018-09-21 21:20:30 +0300704 pr_debug("%s: flags=%x event_f_flags=%x\n",
705 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500706
Eric Paris52c923d2009-12-17 21:24:26 -0500707 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200708 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500709
Steve Grubbde8cd832017-10-02 20:21:39 -0400710#ifdef CONFIG_AUDITSYSCALL
711 if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
712#else
Eric Paris52c923d2009-12-17 21:24:26 -0500713 if (flags & ~FAN_ALL_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400714#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500715 return -EINVAL;
716
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700717 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
718 return -EINVAL;
719
720 switch (event_f_flags & O_ACCMODE) {
721 case O_RDONLY:
722 case O_RDWR:
723 case O_WRONLY:
724 break;
725 default:
726 return -EINVAL;
727 }
728
Eric Paris4afeff82010-10-28 17:21:58 -0400729 user = get_current_user();
730 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
731 free_uid(user);
732 return -EMFILE;
733 }
734
Eric Parisb2d87902009-12-17 21:24:34 -0500735 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500736 if (flags & FAN_CLOEXEC)
737 f_flags |= O_CLOEXEC;
738 if (flags & FAN_NONBLOCK)
739 f_flags |= O_NONBLOCK;
740
741 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
742 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500743 if (IS_ERR(group)) {
744 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500745 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500746 }
Eric Paris52c923d2009-12-17 21:24:26 -0500747
Eric Paris4afeff82010-10-28 17:21:58 -0400748 group->fanotify_data.user = user;
Amir Goldstein96a71f22018-09-21 21:20:30 +0300749 group->fanotify_data.flags = flags;
Eric Paris4afeff82010-10-28 17:21:58 -0400750 atomic_inc(&user->fanotify_listeners);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700751 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -0400752
Jan Kara1f5eaa92018-02-21 14:10:59 +0100753 oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100754 if (unlikely(!oevent)) {
755 fd = -ENOMEM;
756 goto out_destroy_group;
757 }
758 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100759
Will Woods1e2ee492014-05-06 12:50:10 -0700760 if (force_o_largefile())
761 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400762 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500763 init_waitqueue_head(&group->fanotify_data.access_waitq);
764 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Eric Paris4231a232010-10-28 17:21:56 -0400765 switch (flags & FAN_ALL_CLASS_BITS) {
766 case FAN_CLASS_NOTIF:
767 group->priority = FS_PRIO_0;
768 break;
769 case FAN_CLASS_CONTENT:
770 group->priority = FS_PRIO_1;
771 break;
772 case FAN_CLASS_PRE_CONTENT:
773 group->priority = FS_PRIO_2;
774 break;
775 default:
776 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200777 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400778 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500779
Eric Paris5dd03f52010-10-28 17:21:57 -0400780 if (flags & FAN_UNLIMITED_QUEUE) {
781 fd = -EPERM;
782 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200783 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400784 group->max_events = UINT_MAX;
785 } else {
786 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
787 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400788
Eric Parisac7e22d2010-10-28 17:21:58 -0400789 if (flags & FAN_UNLIMITED_MARKS) {
790 fd = -EPERM;
791 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200792 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400793 group->fanotify_data.max_marks = UINT_MAX;
794 } else {
795 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
796 }
Eric Parise7099d82010-10-28 17:21:57 -0400797
Steve Grubbde8cd832017-10-02 20:21:39 -0400798 if (flags & FAN_ENABLE_AUDIT) {
799 fd = -EPERM;
800 if (!capable(CAP_AUDIT_WRITE))
801 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -0400802 }
803
Eric Paris52c923d2009-12-17 21:24:26 -0500804 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
805 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200806 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500807
808 return fd;
809
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200810out_destroy_group:
811 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500812 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500813}
Eric Parisbbaa4162009-12-17 21:24:26 -0500814
Dominik Brodowski183caa32018-03-17 15:06:11 +0100815static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
816 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500817{
Eric Paris0ff21db2009-12-17 21:24:29 -0500818 struct inode *inode = NULL;
819 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500820 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400821 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500822 struct path path;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100823 u32 valid_mask = FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300824 unsigned int mark_type = flags & FAN_MARK_TYPE_MASK;
Al Viro2903ff02012-08-28 12:52:22 -0400825 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500826
827 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
828 __func__, fanotify_fd, flags, dfd, pathname, mask);
829
830 /* we only use the lower 32 bits as of right now. */
831 if (mask & ((__u64)0xffffffff << 32))
832 return -EINVAL;
833
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500834 if (flags & ~FAN_ALL_MARK_FLAGS)
835 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300836
837 switch (mark_type) {
838 case FAN_MARK_INODE:
839 case FAN_MARK_MOUNT:
840 case FAN_MARK_FILESYSTEM:
841 break;
842 default:
843 return -EINVAL;
844 }
845
Eric Paris4d926042009-12-17 21:24:34 -0500846 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100847 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500848 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100849 if (!mask)
850 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700851 break;
Eric Paris4d926042009-12-17 21:24:34 -0500852 case FAN_MARK_FLUSH:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300853 if (flags & ~(FAN_MARK_TYPE_MASK | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700854 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500855 break;
856 default:
857 return -EINVAL;
858 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400859
860 if (mask & FAN_ONDIR) {
861 flags |= FAN_MARK_ONDIR;
862 mask &= ~FAN_ONDIR;
863 }
864
Miklos Szeredi6685df32017-10-30 21:14:56 +0100865 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
866 valid_mask |= FAN_ALL_PERM_EVENTS;
867
868 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -0500869 return -EINVAL;
870
Al Viro2903ff02012-08-28 12:52:22 -0400871 f = fdget(fanotify_fd);
872 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -0500873 return -EBADF;
874
875 /* verify that this is indeed an fanotify instance */
876 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400877 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -0500878 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -0400879 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -0400880
881 /*
882 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
883 * allowed to set permissions events.
884 */
885 ret = -EINVAL;
886 if (mask & FAN_ALL_PERM_EVENTS &&
887 group->priority == FS_PRIO_0)
888 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500889
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700890 if (flags & FAN_MARK_FLUSH) {
891 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300892 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700893 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300894 else if (mark_type == FAN_MARK_FILESYSTEM)
895 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700896 else
897 fsnotify_clear_inode_marks_by_group(group);
898 goto fput_and_out;
899 }
900
Eric Paris2a3edf82009-12-17 21:24:26 -0500901 ret = fanotify_find_path(dfd, pathname, &path, flags);
902 if (ret)
903 goto fput_and_out;
904
905 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300906 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -0500907 inode = path.dentry->d_inode;
908 else
909 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500910
911 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700912 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500913 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300914 if (mark_type == FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500915 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300916 else if (mark_type == FAN_MARK_FILESYSTEM)
917 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500918 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500919 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500920 break;
921 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300922 if (mark_type == FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500923 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300924 else if (mark_type == FAN_MARK_FILESYSTEM)
925 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500926 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500927 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500928 break;
929 default:
930 ret = -EINVAL;
931 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500932
933 path_put(&path);
934fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400935 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500936 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500937}
Eric Paris2a3edf82009-12-17 21:24:26 -0500938
Dominik Brodowski183caa32018-03-17 15:06:11 +0100939SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
940 __u64, mask, int, dfd,
941 const char __user *, pathname)
942{
943 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
944}
945
Al Viro91c2e0b2013-03-05 20:10:59 -0500946#ifdef CONFIG_COMPAT
947COMPAT_SYSCALL_DEFINE6(fanotify_mark,
948 int, fanotify_fd, unsigned int, flags,
949 __u32, mask0, __u32, mask1, int, dfd,
950 const char __user *, pathname)
951{
Dominik Brodowski183caa32018-03-17 15:06:11 +0100952 return do_fanotify_mark(fanotify_fd, flags,
Al Viro91c2e0b2013-03-05 20:10:59 -0500953#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -0500954 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -0800955#else
956 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -0500957#endif
958 dfd, pathname);
959}
960#endif
961
Eric Paris2a3edf82009-12-17 21:24:26 -0500962/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100963 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500964 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
965 * must result in panic().
966 */
967static int __init fanotify_user_setup(void)
968{
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700969 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
970 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7053aee2014-01-21 15:48:14 -0800971 fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100972 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
973 fanotify_perm_event_cachep =
974 KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
975 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500976
977 return 0;
978}
979device_initcall(fanotify_user_setup);