blob: 3899ad177651ad29a899e67fe4baeb4c0e7604ad [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>
Eric Parisa1014f12009-12-17 21:24:26 -050019
20#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050021
Al Viroc63181e2011-11-25 02:35:16 -050022#include "../../mount.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080023#include "../fdinfo.h"
Jan Kara7053aee2014-01-21 15:48:14 -080024#include "fanotify.h"
Al Viroc63181e2011-11-25 02:35:16 -050025
Eric Paris2529a0d2010-10-28 17:21:57 -040026#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Eric Parise7099d82010-10-28 17:21:57 -040027#define FANOTIFY_DEFAULT_MAX_MARKS 8192
Eric Paris4afeff82010-10-28 17:21:58 -040028#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
Eric Paris2529a0d2010-10-28 17:21:57 -040029
Heinrich Schuchardt48149e92014-06-04 16:05:44 -070030/*
31 * All flags that may be specified in parameter event_f_flags of fanotify_init.
32 *
33 * Internal and external open flags are stored together in field f_flags of
34 * struct file. Only external open flags shall be allowed in event_f_flags.
35 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
36 * excluded.
37 */
38#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
39 O_ACCMODE | O_APPEND | O_NONBLOCK | \
40 __O_SYNC | O_DSYNC | O_CLOEXEC | \
41 O_LARGEFILE | O_NOATIME )
42
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050043extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050044
Jan Kara054c6362016-12-21 18:06:12 +010045struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7053aee2014-01-21 15:48:14 -080046struct kmem_cache *fanotify_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070047struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050048
Eric Parisa1014f12009-12-17 21:24:26 -050049/*
50 * Get an fsnotify notification event if one exists and is small
51 * enough to fit in "count". Return an error pointer if the count
52 * is not large enough.
53 *
Jan Karac21dbe22016-10-07 16:56:52 -070054 * Called with the group->notification_lock held.
Eric Parisa1014f12009-12-17 21:24:26 -050055 */
56static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
57 size_t count)
58{
Jan Karaed272642016-10-07 16:57:01 -070059 assert_spin_locked(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -050060
61 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
62
63 if (fsnotify_notify_queue_is_empty(group))
64 return NULL;
65
66 if (FAN_EVENT_METADATA_LEN > count)
67 return ERR_PTR(-EINVAL);
68
Jan Karac21dbe22016-10-07 16:56:52 -070069 /* held the notification_lock the whole time, so this is the
Eric Parisa1014f12009-12-17 21:24:26 -050070 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -070071 return fsnotify_remove_first_event(group);
Eric Parisa1014f12009-12-17 21:24:26 -050072}
73
Al Viro352e3b22012-08-19 12:30:45 -040074static int create_fd(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -080075 struct fanotify_event_info *event,
76 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -050077{
78 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050079 struct file *new_file;
80
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050081 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050082
Yann Droneaud0b37e092014-10-09 15:24:40 -070083 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -050084 if (client_fd < 0)
85 return client_fd;
86
Eric Parisa1014f12009-12-17 21:24:26 -050087 /*
88 * we need a new file handle for the userspace program so it can read even if it was
89 * originally opened O_WRONLY.
90 */
Eric Parisa1014f12009-12-17 21:24:26 -050091 /* it's possible this event was an overflow event. in that case dentry and mnt
92 * are NULL; That's fine, just don't call dentry open */
Al Viro765927b2012-06-26 21:58:53 +040093 if (event->path.dentry && event->path.mnt)
94 new_file = dentry_open(&event->path,
Eric Paris80af2582010-07-28 10:18:37 -040095 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -050096 current_cred());
97 else
98 new_file = ERR_PTR(-EOVERFLOW);
99 if (IS_ERR(new_file)) {
100 /*
101 * we still send an event even if we can't open the file. this
102 * can happen when say tasks are gone and we try to open their
103 * /proc files or we try to open a WRONLY file like in sysfs
104 * we just send the errno to userspace since there isn't much
105 * else we can do.
106 */
107 put_unused_fd(client_fd);
108 client_fd = PTR_ERR(new_file);
109 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400110 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500111 }
112
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500113 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500114}
115
Eric Parisecf6f5e2010-11-08 18:08:14 -0500116static int fill_event_metadata(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800117 struct fanotify_event_metadata *metadata,
118 struct fsnotify_event *fsn_event,
119 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500120{
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100121 int ret = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800122 struct fanotify_event_info *event;
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100123
Eric Parisa1014f12009-12-17 21:24:26 -0500124 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
Jan Kara7053aee2014-01-21 15:48:14 -0800125 group, metadata, fsn_event);
Eric Parisa1014f12009-12-17 21:24:26 -0500126
Al Viro352e3b22012-08-19 12:30:45 -0400127 *file = NULL;
Jan Kara7053aee2014-01-21 15:48:14 -0800128 event = container_of(fsn_event, struct fanotify_event_info, fse);
Eric Parisa1014f12009-12-17 21:24:26 -0500129 metadata->event_len = FAN_EVENT_METADATA_LEN;
Eric Paris7d131622010-12-07 15:27:57 -0500130 metadata->metadata_len = FAN_EVENT_METADATA_LEN;
Eric Parisa1014f12009-12-17 21:24:26 -0500131 metadata->vers = FANOTIFY_METADATA_VERSION;
Dan Carpenterde1e0c42013-07-08 15:59:40 -0700132 metadata->reserved = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800133 metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500134 metadata->pid = pid_vnr(event->tgid);
Jan Kara7053aee2014-01-21 15:48:14 -0800135 if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100136 metadata->fd = FAN_NOFD;
137 else {
Al Viro352e3b22012-08-19 12:30:45 -0400138 metadata->fd = create_fd(group, event, file);
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100139 if (metadata->fd < 0)
140 ret = metadata->fd;
141 }
Eric Parisa1014f12009-12-17 21:24:26 -0500142
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100143 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500144}
145
Jan Karaf0834412014-04-03 14:46:33 -0700146static struct fanotify_perm_event_info *dequeue_event(
147 struct fsnotify_group *group, int fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500148{
Jan Karaf0834412014-04-03 14:46:33 -0700149 struct fanotify_perm_event_info *event, *return_e = NULL;
Eric Parisb2d87902009-12-17 21:24:34 -0500150
Jan Kara073f6552016-10-07 16:56:55 -0700151 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700152 list_for_each_entry(event, &group->fanotify_data.access_list,
153 fae.fse.list) {
154 if (event->fd != fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500155 continue;
156
Jan Karaf0834412014-04-03 14:46:33 -0700157 list_del_init(&event->fae.fse.list);
158 return_e = event;
Eric Parisb2d87902009-12-17 21:24:34 -0500159 break;
160 }
Jan Kara073f6552016-10-07 16:56:55 -0700161 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500162
Jan Karaf0834412014-04-03 14:46:33 -0700163 pr_debug("%s: found return_re=%p\n", __func__, return_e);
Eric Parisb2d87902009-12-17 21:24:34 -0500164
Jan Karaf0834412014-04-03 14:46:33 -0700165 return return_e;
Eric Parisb2d87902009-12-17 21:24:34 -0500166}
167
168static int process_access_response(struct fsnotify_group *group,
169 struct fanotify_response *response_struct)
170{
Jan Karaf0834412014-04-03 14:46:33 -0700171 struct fanotify_perm_event_info *event;
172 int fd = response_struct->fd;
173 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500174
175 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
176 fd, response);
177 /*
178 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300179 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500180 * timeout
181 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400182 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500183 case FAN_ALLOW:
184 case FAN_DENY:
185 break;
186 default:
187 return -EINVAL;
188 }
189
190 if (fd < 0)
191 return -EINVAL;
192
Steve Grubbde8cd832017-10-02 20:21:39 -0400193 if ((response & FAN_AUDIT) && !group->fanotify_data.audit)
194 return -EINVAL;
195
Jan Karaf0834412014-04-03 14:46:33 -0700196 event = dequeue_event(group, fd);
197 if (!event)
Eric Parisb2d87902009-12-17 21:24:34 -0500198 return -ENOENT;
199
Jan Karaf0834412014-04-03 14:46:33 -0700200 event->response = response;
Eric Parisb2d87902009-12-17 21:24:34 -0500201 wake_up(&group->fanotify_data.access_waitq);
202
Eric Parisb2d87902009-12-17 21:24:34 -0500203 return 0;
204}
Eric Parisb2d87902009-12-17 21:24:34 -0500205
Eric Parisa1014f12009-12-17 21:24:26 -0500206static ssize_t copy_event_to_user(struct fsnotify_group *group,
207 struct fsnotify_event *event,
208 char __user *buf)
209{
210 struct fanotify_event_metadata fanotify_event_metadata;
Al Viro352e3b22012-08-19 12:30:45 -0400211 struct file *f;
Eric Parisb2d87902009-12-17 21:24:34 -0500212 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500213
214 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
215
Al Viro352e3b22012-08-19 12:30:45 -0400216 ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
Eric Parisecf6f5e2010-11-08 18:08:14 -0500217 if (ret < 0)
Jan Karad5078162014-04-03 14:46:36 -0700218 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500219
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100220 fd = fanotify_event_metadata.fd;
Al Viro352e3b22012-08-19 12:30:45 -0400221 ret = -EFAULT;
222 if (copy_to_user(buf, &fanotify_event_metadata,
223 fanotify_event_metadata.event_len))
224 goto out_close_fd;
225
Miklos Szeredi6685df32017-10-30 21:14:56 +0100226 if (fanotify_is_perm_event(event->mask))
Jan Karad5078162014-04-03 14:46:36 -0700227 FANOTIFY_PE(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500228
Al Viro3587b1b2012-11-18 19:19:00 +0000229 if (fd != FAN_NOFD)
230 fd_install(fd, f);
Eric Paris7d131622010-12-07 15:27:57 -0500231 return fanotify_event_metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500232
Eric Parisb2d87902009-12-17 21:24:34 -0500233out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400234 if (fd != FAN_NOFD) {
235 put_unused_fd(fd);
236 fput(f);
237 }
Eric Parisb2d87902009-12-17 21:24:34 -0500238 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500239}
240
241/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400242static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500243{
244 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400245 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500246
247 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700248 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500249 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800250 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700251 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500252
253 return ret;
254}
255
256static ssize_t fanotify_read(struct file *file, char __user *buf,
257 size_t count, loff_t *pos)
258{
259 struct fsnotify_group *group;
260 struct fsnotify_event *kevent;
261 char __user *start;
262 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100263 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500264
265 start = buf;
266 group = file->private_data;
267
268 pr_debug("%s: group=%p\n", __func__, group);
269
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100270 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500271 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700272 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500273 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700274 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500275
Jan Karad8aaab42014-04-03 14:46:35 -0700276 if (IS_ERR(kevent)) {
Eric Parisa1014f12009-12-17 21:24:26 -0500277 ret = PTR_ERR(kevent);
Jan Karad8aaab42014-04-03 14:46:35 -0700278 break;
279 }
280
281 if (!kevent) {
282 ret = -EAGAIN;
283 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500284 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700285
286 ret = -ERESTARTSYS;
287 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500288 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700289
290 if (start != buf)
291 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100292
293 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500294 continue;
295 }
296
Jan Karad8aaab42014-04-03 14:46:35 -0700297 ret = copy_event_to_user(group, kevent, buf);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300298 if (unlikely(ret == -EOPENSTALE)) {
299 /*
300 * We cannot report events with stale fd so drop it.
301 * Setting ret to 0 will continue the event loop and
302 * do the right thing if there are no more events to
303 * read (i.e. return bytes read, -EAGAIN or wait).
304 */
305 ret = 0;
306 }
307
Jan Karad8aaab42014-04-03 14:46:35 -0700308 /*
309 * Permission events get queued to wait for response. Other
310 * events can be destroyed now.
311 */
Miklos Szeredi6685df32017-10-30 21:14:56 +0100312 if (!fanotify_is_perm_event(kevent->mask)) {
Jan Karad8aaab42014-04-03 14:46:35 -0700313 fsnotify_destroy_event(group, kevent);
Jan Karad5078162014-04-03 14:46:36 -0700314 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300315 if (ret <= 0) {
Jan Karad5078162014-04-03 14:46:36 -0700316 FANOTIFY_PE(kevent)->response = FAN_DENY;
317 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300318 } else {
319 spin_lock(&group->notification_lock);
320 list_add_tail(&kevent->list,
321 &group->fanotify_data.access_list);
322 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700323 }
Jan Karad5078162014-04-03 14:46:36 -0700324 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300325 if (ret < 0)
326 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700327 buf += ret;
328 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500329 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100330 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500331
Eric Parisa1014f12009-12-17 21:24:26 -0500332 if (start != buf && ret != -EFAULT)
333 ret = buf - start;
334 return ret;
335}
336
Eric Parisb2d87902009-12-17 21:24:34 -0500337static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
338{
Eric Parisb2d87902009-12-17 21:24:34 -0500339 struct fanotify_response response = { .fd = -1, .response = -1 };
340 struct fsnotify_group *group;
341 int ret;
342
Miklos Szeredi6685df32017-10-30 21:14:56 +0100343 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
344 return -EINVAL;
345
Eric Parisb2d87902009-12-17 21:24:34 -0500346 group = file->private_data;
347
348 if (count > sizeof(response))
349 count = sizeof(response);
350
351 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
352
353 if (copy_from_user(&response, buf, count))
354 return -EFAULT;
355
356 ret = process_access_response(group, &response);
357 if (ret < 0)
358 count = ret;
359
360 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500361}
362
Eric Paris52c923d2009-12-17 21:24:26 -0500363static int fanotify_release(struct inode *ignored, struct file *file)
364{
365 struct fsnotify_group *group = file->private_data;
Jan Karaf0834412014-04-03 14:46:33 -0700366 struct fanotify_perm_event_info *event, *next;
Jan Kara96d41012016-09-19 14:44:30 -0700367 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400368
Jan Kara5838d442014-08-06 16:03:28 -0700369 /*
Jan Kara96d41012016-09-19 14:44:30 -0700370 * Stop new events from arriving in the notification queue. since
371 * userspace cannot use fanotify fd anymore, no event can enter or
372 * leave access_list by now either.
373 */
374 fsnotify_group_stop_queueing(group);
375
376 /*
377 * Process all permission events on access_list and notification queue
378 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700379 */
Jan Kara073f6552016-10-07 16:56:55 -0700380 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700381 list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
382 fae.fse.list) {
383 pr_debug("%s: found group=%p event=%p\n", __func__, group,
384 event);
Eric Paris2eebf582010-08-18 12:25:50 -0400385
Jan Karaf0834412014-04-03 14:46:33 -0700386 list_del_init(&event->fae.fse.list);
387 event->response = FAN_ALLOW;
Eric Paris2eebf582010-08-18 12:25:50 -0400388 }
Eric Paris2eebf582010-08-18 12:25:50 -0400389
Jan Kara5838d442014-08-06 16:03:28 -0700390 /*
Jan Kara96d41012016-09-19 14:44:30 -0700391 * Destroy all non-permission events. For permission events just
392 * dequeue them and set the response. They will be freed once the
393 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700394 */
Jan Kara96d41012016-09-19 14:44:30 -0700395 while (!fsnotify_notify_queue_is_empty(group)) {
396 fsn_event = fsnotify_remove_first_event(group);
Jan Kara1404ff32016-10-07 16:56:49 -0700397 if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700398 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700399 fsnotify_destroy_event(group, fsn_event);
Jan Karac21dbe22016-10-07 16:56:52 -0700400 spin_lock(&group->notification_lock);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100401 } else {
Jan Kara96d41012016-09-19 14:44:30 -0700402 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100403 }
Jan Kara96d41012016-09-19 14:44:30 -0700404 }
Jan Karac21dbe22016-10-07 16:56:52 -0700405 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700406
407 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400408 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400409
Eric Paris52c923d2009-12-17 21:24:26 -0500410 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200411 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500412
413 return 0;
414}
415
Eric Parisa1014f12009-12-17 21:24:26 -0500416static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
417{
418 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800419 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500420 void __user *p;
421 int ret = -ENOTTY;
422 size_t send_len = 0;
423
424 group = file->private_data;
425
426 p = (void __user *) arg;
427
428 switch (cmd) {
429 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700430 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800431 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500432 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700433 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500434 ret = put_user(send_len, (int __user *) p);
435 break;
436 }
437
438 return ret;
439}
440
Eric Paris52c923d2009-12-17 21:24:26 -0500441static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800442 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500443 .poll = fanotify_poll,
444 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500445 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500446 .fasync = NULL,
447 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500448 .unlocked_ioctl = fanotify_ioctl,
449 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200450 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500451};
452
Eric Paris2a3edf82009-12-17 21:24:26 -0500453static int fanotify_find_path(int dfd, const char __user *filename,
454 struct path *path, unsigned int flags)
455{
456 int ret;
457
458 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
459 dfd, filename, flags);
460
461 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400462 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500463
464 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400465 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500466 goto out;
467
468 ret = -ENOTDIR;
469 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500470 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400471 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500472 goto out;
473 }
474
Al Viro2903ff02012-08-28 12:52:22 -0400475 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500476 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400477 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500478 } else {
479 unsigned int lookup_flags = 0;
480
481 if (!(flags & FAN_MARK_DONT_FOLLOW))
482 lookup_flags |= LOOKUP_FOLLOW;
483 if (flags & FAN_MARK_ONLYDIR)
484 lookup_flags |= LOOKUP_DIRECTORY;
485
486 ret = user_path_at(dfd, filename, lookup_flags, path);
487 if (ret)
488 goto out;
489 }
490
491 /* you can only watch an inode if you have read permissions on it */
492 ret = inode_permission(path->dentry->d_inode, MAY_READ);
493 if (ret)
494 path_put(path);
495out:
496 return ret;
497}
498
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500499static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
500 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200501 unsigned int flags,
502 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500503{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800504 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500505
506 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500507 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800508 __u32 tmask = fsn_mark->mask & ~mask;
509
510 if (flags & FAN_MARK_ONDIR)
511 tmask &= ~FAN_ONDIR;
512
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500513 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100514 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500515 } else {
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800516 __u32 tmask = fsn_mark->ignored_mask & ~mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800517 if (flags & FAN_MARK_ONDIR)
518 tmask &= ~FAN_ONDIR;
Jan Kara66d2b812016-12-21 16:03:59 +0100519 fsn_mark->ignored_mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500520 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800521 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500522 spin_unlock(&fsn_mark->lock);
523
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500524 return mask & oldmask;
525}
526
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500527static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500528 struct vfsmount *mnt, __u32 mask,
529 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500530{
531 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500532 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200533 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500534
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700535 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100536 fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
537 group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700538 if (!fsn_mark) {
539 mutex_unlock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -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 Sanfilippo6dfbd142011-06-14 17:29:49 +0200552
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500553 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500554 return 0;
555}
556
557static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500558 struct inode *inode, __u32 mask,
559 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500560{
561 struct fsnotify_mark *fsn_mark = NULL;
562 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200563 int destroy_mark;
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500564
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700565 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100566 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700567 if (!fsn_mark) {
568 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500569 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700570 }
Eric Paris88826272009-12-17 21:24:28 -0500571
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200572 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
573 &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300574 if (removed & fsnotify_conn_mask(fsn_mark->connector))
575 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200576 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700577 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700578 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700579 if (destroy_mark)
580 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700581
Jan Karab1362ed2016-12-21 16:28:45 +0100582 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500583 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500584
Eric Paris2a3edf82009-12-17 21:24:26 -0500585 return 0;
586}
587
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500588static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
589 __u32 mask,
590 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500591{
Eric Paris192ca4d2010-10-28 17:21:59 -0400592 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500593
594 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500595 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800596 __u32 tmask = fsn_mark->mask | mask;
597
598 if (flags & FAN_MARK_ONDIR)
599 tmask |= FAN_ONDIR;
600
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500601 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100602 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500603 } else {
Eric Paris192ca4d2010-10-28 17:21:59 -0400604 __u32 tmask = fsn_mark->ignored_mask | mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800605 if (flags & FAN_MARK_ONDIR)
606 tmask |= FAN_ONDIR;
607
Jan Kara66d2b812016-12-21 16:03:59 +0100608 fsn_mark->ignored_mask = tmask;
Eric Parisc9778a92009-12-17 21:24:33 -0500609 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
610 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500611 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500612 spin_unlock(&fsn_mark->lock);
613
614 return mask & ~oldmask;
615}
616
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700617static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300618 fsnotify_connp_t *connp,
619 unsigned int type)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700620{
621 struct fsnotify_mark *mark;
622 int ret;
623
624 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
625 return ERR_PTR(-ENOSPC);
626
627 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
628 if (!mark)
629 return ERR_PTR(-ENOMEM);
630
Jan Kara054c6362016-12-21 18:06:12 +0100631 fsnotify_init_mark(mark, group);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300632 ret = fsnotify_add_mark_locked(mark, connp, type, 0);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700633 if (ret) {
634 fsnotify_put_mark(mark);
635 return ERR_PTR(ret);
636 }
637
638 return mark;
639}
640
641
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500642static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500643 struct vfsmount *mnt, __u32 mask,
644 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500645{
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300646 fsnotify_connp_t *connp = &real_mount(mnt)->mnt_fsnotify_marks;
Eric Paris2a3edf82009-12-17 21:24:26 -0500647 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500648 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500649
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700650 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300651 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500652 if (!fsn_mark) {
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300653 fsn_mark = fanotify_add_new_mark(group, connp,
654 FSNOTIFY_OBJ_TYPE_VFSMOUNT);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700655 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700656 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700657 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700658 }
Eric Paris88826272009-12-17 21:24:28 -0500659 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500660 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300661 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
662 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100663 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700664
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100665 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700666 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500667}
668
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500669static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500670 struct inode *inode, __u32 mask,
671 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500672{
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300673 fsnotify_connp_t *connp = &inode->i_fsnotify_marks;
Eric Paris88826272009-12-17 21:24:28 -0500674 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500675 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500676
677 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500678
Eric Paris5322a592010-10-28 17:21:57 -0400679 /*
680 * If some other task has this inode open for write we should not add
681 * an ignored mark, unless that ignored mark is supposed to survive
682 * modification changes anyway.
683 */
684 if ((flags & FAN_MARK_IGNORED_MASK) &&
685 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
686 (atomic_read(&inode->i_writecount) > 0))
687 return 0;
688
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700689 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300690 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris2a3edf82009-12-17 21:24:26 -0500691 if (!fsn_mark) {
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300692 fsn_mark = fanotify_add_new_mark(group, connp,
693 FSNOTIFY_OBJ_TYPE_INODE);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700694 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700695 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700696 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700697 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500698 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500699 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300700 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
701 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100702 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700703
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100704 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700705 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500706}
Eric Paris2a3edf82009-12-17 21:24:26 -0500707
Eric Paris52c923d2009-12-17 21:24:26 -0500708/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400709SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500710{
Eric Paris52c923d2009-12-17 21:24:26 -0500711 struct fsnotify_group *group;
712 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400713 struct user_struct *user;
Jan Karaff57cd52014-02-21 19:14:11 +0100714 struct fanotify_event_info *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500715
Eric Paris08ae8932010-05-27 09:41:40 -0400716 pr_debug("%s: flags=%d event_f_flags=%d\n",
717 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500718
Eric Paris52c923d2009-12-17 21:24:26 -0500719 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200720 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500721
Steve Grubbde8cd832017-10-02 20:21:39 -0400722#ifdef CONFIG_AUDITSYSCALL
723 if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
724#else
Eric Paris52c923d2009-12-17 21:24:26 -0500725 if (flags & ~FAN_ALL_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400726#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500727 return -EINVAL;
728
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700729 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
730 return -EINVAL;
731
732 switch (event_f_flags & O_ACCMODE) {
733 case O_RDONLY:
734 case O_RDWR:
735 case O_WRONLY:
736 break;
737 default:
738 return -EINVAL;
739 }
740
Eric Paris4afeff82010-10-28 17:21:58 -0400741 user = get_current_user();
742 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
743 free_uid(user);
744 return -EMFILE;
745 }
746
Eric Parisb2d87902009-12-17 21:24:34 -0500747 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500748 if (flags & FAN_CLOEXEC)
749 f_flags |= O_CLOEXEC;
750 if (flags & FAN_NONBLOCK)
751 f_flags |= O_NONBLOCK;
752
753 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
754 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500755 if (IS_ERR(group)) {
756 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500757 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500758 }
Eric Paris52c923d2009-12-17 21:24:26 -0500759
Eric Paris4afeff82010-10-28 17:21:58 -0400760 group->fanotify_data.user = user;
761 atomic_inc(&user->fanotify_listeners);
762
Jan Kara1f5eaa92018-02-21 14:10:59 +0100763 oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100764 if (unlikely(!oevent)) {
765 fd = -ENOMEM;
766 goto out_destroy_group;
767 }
768 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100769
Will Woods1e2ee492014-05-06 12:50:10 -0700770 if (force_o_largefile())
771 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400772 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500773 init_waitqueue_head(&group->fanotify_data.access_waitq);
774 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Eric Paris4231a232010-10-28 17:21:56 -0400775 switch (flags & FAN_ALL_CLASS_BITS) {
776 case FAN_CLASS_NOTIF:
777 group->priority = FS_PRIO_0;
778 break;
779 case FAN_CLASS_CONTENT:
780 group->priority = FS_PRIO_1;
781 break;
782 case FAN_CLASS_PRE_CONTENT:
783 group->priority = FS_PRIO_2;
784 break;
785 default:
786 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200787 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400788 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500789
Eric Paris5dd03f52010-10-28 17:21:57 -0400790 if (flags & FAN_UNLIMITED_QUEUE) {
791 fd = -EPERM;
792 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200793 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400794 group->max_events = UINT_MAX;
795 } else {
796 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
797 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400798
Eric Parisac7e22d2010-10-28 17:21:58 -0400799 if (flags & FAN_UNLIMITED_MARKS) {
800 fd = -EPERM;
801 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200802 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400803 group->fanotify_data.max_marks = UINT_MAX;
804 } else {
805 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
806 }
Eric Parise7099d82010-10-28 17:21:57 -0400807
Steve Grubbde8cd832017-10-02 20:21:39 -0400808 if (flags & FAN_ENABLE_AUDIT) {
809 fd = -EPERM;
810 if (!capable(CAP_AUDIT_WRITE))
811 goto out_destroy_group;
812 group->fanotify_data.audit = true;
813 }
814
Eric Paris52c923d2009-12-17 21:24:26 -0500815 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
816 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200817 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500818
819 return fd;
820
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200821out_destroy_group:
822 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500823 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500824}
Eric Parisbbaa4162009-12-17 21:24:26 -0500825
Dominik Brodowski183caa32018-03-17 15:06:11 +0100826static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
827 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500828{
Eric Paris0ff21db2009-12-17 21:24:29 -0500829 struct inode *inode = NULL;
830 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500831 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400832 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500833 struct path path;
Miklos Szeredi6685df32017-10-30 21:14:56 +0100834 u32 valid_mask = FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD;
Al Viro2903ff02012-08-28 12:52:22 -0400835 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500836
837 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
838 __func__, fanotify_fd, flags, dfd, pathname, mask);
839
840 /* we only use the lower 32 bits as of right now. */
841 if (mask & ((__u64)0xffffffff << 32))
842 return -EINVAL;
843
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500844 if (flags & ~FAN_ALL_MARK_FLAGS)
845 return -EINVAL;
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:
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700853 if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
854 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;
892 if (flags & FAN_MARK_MOUNT)
893 fsnotify_clear_vfsmount_marks_by_group(group);
894 else
895 fsnotify_clear_inode_marks_by_group(group);
896 goto fput_and_out;
897 }
898
Eric Paris2a3edf82009-12-17 21:24:26 -0500899 ret = fanotify_find_path(dfd, pathname, &path, flags);
900 if (ret)
901 goto fput_and_out;
902
903 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500904 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500905 inode = path.dentry->d_inode;
906 else
907 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500908
909 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700910 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500911 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500912 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500913 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500914 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500915 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500916 break;
917 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500918 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500919 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500920 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500921 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500922 break;
923 default:
924 ret = -EINVAL;
925 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500926
927 path_put(&path);
928fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400929 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500930 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500931}
Eric Paris2a3edf82009-12-17 21:24:26 -0500932
Dominik Brodowski183caa32018-03-17 15:06:11 +0100933SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
934 __u64, mask, int, dfd,
935 const char __user *, pathname)
936{
937 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
938}
939
Al Viro91c2e0b2013-03-05 20:10:59 -0500940#ifdef CONFIG_COMPAT
941COMPAT_SYSCALL_DEFINE6(fanotify_mark,
942 int, fanotify_fd, unsigned int, flags,
943 __u32, mask0, __u32, mask1, int, dfd,
944 const char __user *, pathname)
945{
Dominik Brodowski183caa32018-03-17 15:06:11 +0100946 return do_fanotify_mark(fanotify_fd, flags,
Al Viro91c2e0b2013-03-05 20:10:59 -0500947#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -0500948 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -0800949#else
950 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -0500951#endif
952 dfd, pathname);
953}
954#endif
955
Eric Paris2a3edf82009-12-17 21:24:26 -0500956/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100957 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500958 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
959 * must result in panic().
960 */
961static int __init fanotify_user_setup(void)
962{
963 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Jan Kara7053aee2014-01-21 15:48:14 -0800964 fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100965 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
966 fanotify_perm_event_cachep =
967 KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
968 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500969
970 return 0;
971}
972device_initcall(fanotify_user_setup);