blob: 0455ea729384b611d35a48f092f271fc0105e4f9 [file] [log] [blame]
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05001#include <linux/fanotify.h>
Eric Paris11637e42009-12-17 21:24:25 -05002#include <linux/fcntl.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05003#include <linux/file.h>
Eric Paris11637e42009-12-17 21:24:25 -05004#include <linux/fs.h>
Eric Paris52c923d2009-12-17 21:24:26 -05005#include <linux/anon_inodes.h>
Eric Paris11637e42009-12-17 21:24:25 -05006#include <linux/fsnotify_backend.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05007#include <linux/init.h>
Eric Parisa1014f12009-12-17 21:24:26 -05008#include <linux/mount.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05009#include <linux/namei.h>
Eric Parisa1014f12009-12-17 21:24:26 -050010#include <linux/poll.h>
Eric Paris11637e42009-12-17 21:24:25 -050011#include <linux/security.h>
12#include <linux/syscalls.h>
Tejun Heoe4e047a2010-05-20 01:36:28 +100013#include <linux/slab.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050014#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050015#include <linux/uaccess.h>
Al Viro91c2e0b2013-03-05 20:10:59 -050016#include <linux/compat.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
Eric Parisa1014f12009-12-17 21:24:26 -050018
19#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050020
Al Viroc63181e2011-11-25 02:35:16 -050021#include "../../mount.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080022#include "../fdinfo.h"
Jan Kara7053aee2014-01-21 15:48:14 -080023#include "fanotify.h"
Al Viroc63181e2011-11-25 02:35:16 -050024
Eric Paris2529a0d2010-10-28 17:21:57 -040025#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Eric Parise7099d82010-10-28 17:21:57 -040026#define FANOTIFY_DEFAULT_MAX_MARKS 8192
Eric Paris4afeff82010-10-28 17:21:58 -040027#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
Eric Paris2529a0d2010-10-28 17:21:57 -040028
Heinrich Schuchardt48149e92014-06-04 16:05:44 -070029/*
30 * All flags that may be specified in parameter event_f_flags of fanotify_init.
31 *
32 * Internal and external open flags are stored together in field f_flags of
33 * struct file. Only external open flags shall be allowed in event_f_flags.
34 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
35 * excluded.
36 */
37#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
38 O_ACCMODE | O_APPEND | O_NONBLOCK | \
39 __O_SYNC | O_DSYNC | O_CLOEXEC | \
40 O_LARGEFILE | O_NOATIME )
41
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050042extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050043
Jan Kara054c6362016-12-21 18:06:12 +010044struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7053aee2014-01-21 15:48:14 -080045struct kmem_cache *fanotify_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070046struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050047
Eric Parisa1014f12009-12-17 21:24:26 -050048/*
49 * Get an fsnotify notification event if one exists and is small
50 * enough to fit in "count". Return an error pointer if the count
51 * is not large enough.
52 *
Jan Karac21dbe22016-10-07 16:56:52 -070053 * Called with the group->notification_lock held.
Eric Parisa1014f12009-12-17 21:24:26 -050054 */
55static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
56 size_t count)
57{
Jan Karaed272642016-10-07 16:57:01 -070058 assert_spin_locked(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -050059
60 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
61
62 if (fsnotify_notify_queue_is_empty(group))
63 return NULL;
64
65 if (FAN_EVENT_METADATA_LEN > count)
66 return ERR_PTR(-EINVAL);
67
Jan Karac21dbe22016-10-07 16:56:52 -070068 /* held the notification_lock the whole time, so this is the
Eric Parisa1014f12009-12-17 21:24:26 -050069 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -070070 return fsnotify_remove_first_event(group);
Eric Parisa1014f12009-12-17 21:24:26 -050071}
72
Al Viro352e3b22012-08-19 12:30:45 -040073static int create_fd(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -080074 struct fanotify_event_info *event,
75 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -050076{
77 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050078 struct file *new_file;
79
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050080 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050081
Yann Droneaud0b37e092014-10-09 15:24:40 -070082 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -050083 if (client_fd < 0)
84 return client_fd;
85
Eric Parisa1014f12009-12-17 21:24:26 -050086 /*
87 * we need a new file handle for the userspace program so it can read even if it was
88 * originally opened O_WRONLY.
89 */
Eric Parisa1014f12009-12-17 21:24:26 -050090 /* it's possible this event was an overflow event. in that case dentry and mnt
91 * are NULL; That's fine, just don't call dentry open */
Al Viro765927b2012-06-26 21:58:53 +040092 if (event->path.dentry && event->path.mnt)
93 new_file = dentry_open(&event->path,
Eric Paris80af2582010-07-28 10:18:37 -040094 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -050095 current_cred());
96 else
97 new_file = ERR_PTR(-EOVERFLOW);
98 if (IS_ERR(new_file)) {
99 /*
100 * we still send an event even if we can't open the file. this
101 * can happen when say tasks are gone and we try to open their
102 * /proc files or we try to open a WRONLY file like in sysfs
103 * we just send the errno to userspace since there isn't much
104 * else we can do.
105 */
106 put_unused_fd(client_fd);
107 client_fd = PTR_ERR(new_file);
108 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400109 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500110 }
111
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500112 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500113}
114
Eric Parisecf6f5e2010-11-08 18:08:14 -0500115static int fill_event_metadata(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800116 struct fanotify_event_metadata *metadata,
117 struct fsnotify_event *fsn_event,
118 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500119{
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100120 int ret = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800121 struct fanotify_event_info *event;
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100122
Eric Parisa1014f12009-12-17 21:24:26 -0500123 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
Jan Kara7053aee2014-01-21 15:48:14 -0800124 group, metadata, fsn_event);
Eric Parisa1014f12009-12-17 21:24:26 -0500125
Al Viro352e3b22012-08-19 12:30:45 -0400126 *file = NULL;
Jan Kara7053aee2014-01-21 15:48:14 -0800127 event = container_of(fsn_event, struct fanotify_event_info, fse);
Eric Parisa1014f12009-12-17 21:24:26 -0500128 metadata->event_len = FAN_EVENT_METADATA_LEN;
Eric Paris7d131622010-12-07 15:27:57 -0500129 metadata->metadata_len = FAN_EVENT_METADATA_LEN;
Eric Parisa1014f12009-12-17 21:24:26 -0500130 metadata->vers = FANOTIFY_METADATA_VERSION;
Dan Carpenterde1e0c42013-07-08 15:59:40 -0700131 metadata->reserved = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800132 metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500133 metadata->pid = pid_vnr(event->tgid);
Jan Kara7053aee2014-01-21 15:48:14 -0800134 if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100135 metadata->fd = FAN_NOFD;
136 else {
Al Viro352e3b22012-08-19 12:30:45 -0400137 metadata->fd = create_fd(group, event, file);
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100138 if (metadata->fd < 0)
139 ret = metadata->fd;
140 }
Eric Parisa1014f12009-12-17 21:24:26 -0500141
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100142 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500143}
144
Eric Parisb2d87902009-12-17 21:24:34 -0500145#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
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#endif
206
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
Jan Karaf0834412014-04-03 14:46:33 -0700227#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karad5078162014-04-03 14:46:36 -0700228 if (event->mask & FAN_ALL_PERM_EVENTS)
229 FANOTIFY_PE(event)->fd = fd;
Jan Karaf0834412014-04-03 14:46:33 -0700230#endif
Eric Parisb2d87902009-12-17 21:24:34 -0500231
Al Viro3587b1b2012-11-18 19:19:00 +0000232 if (fd != FAN_NOFD)
233 fd_install(fd, f);
Eric Paris7d131622010-12-07 15:27:57 -0500234 return fanotify_event_metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500235
Eric Parisb2d87902009-12-17 21:24:34 -0500236out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400237 if (fd != FAN_NOFD) {
238 put_unused_fd(fd);
239 fput(f);
240 }
Eric Parisb2d87902009-12-17 21:24:34 -0500241 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500242}
243
244/* intofiy userspace file descriptor functions */
245static unsigned int fanotify_poll(struct file *file, poll_table *wait)
246{
247 struct fsnotify_group *group = file->private_data;
248 int ret = 0;
249
250 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700251 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500252 if (!fsnotify_notify_queue_is_empty(group))
253 ret = POLLIN | POLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700254 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500255
256 return ret;
257}
258
259static ssize_t fanotify_read(struct file *file, char __user *buf,
260 size_t count, loff_t *pos)
261{
262 struct fsnotify_group *group;
263 struct fsnotify_event *kevent;
264 char __user *start;
265 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100266 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500267
268 start = buf;
269 group = file->private_data;
270
271 pr_debug("%s: group=%p\n", __func__, group);
272
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100273 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500274 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700275 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500276 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700277 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500278
Jan Karad8aaab42014-04-03 14:46:35 -0700279 if (IS_ERR(kevent)) {
Eric Parisa1014f12009-12-17 21:24:26 -0500280 ret = PTR_ERR(kevent);
Jan Karad8aaab42014-04-03 14:46:35 -0700281 break;
282 }
283
284 if (!kevent) {
285 ret = -EAGAIN;
286 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500287 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700288
289 ret = -ERESTARTSYS;
290 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500291 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700292
293 if (start != buf)
294 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100295
296 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500297 continue;
298 }
299
Jan Karad8aaab42014-04-03 14:46:35 -0700300 ret = copy_event_to_user(group, kevent, buf);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300301 if (unlikely(ret == -EOPENSTALE)) {
302 /*
303 * We cannot report events with stale fd so drop it.
304 * Setting ret to 0 will continue the event loop and
305 * do the right thing if there are no more events to
306 * read (i.e. return bytes read, -EAGAIN or wait).
307 */
308 ret = 0;
309 }
310
Jan Karad8aaab42014-04-03 14:46:35 -0700311 /*
312 * Permission events get queued to wait for response. Other
313 * events can be destroyed now.
314 */
Jan Karad5078162014-04-03 14:46:36 -0700315 if (!(kevent->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karad8aaab42014-04-03 14:46:35 -0700316 fsnotify_destroy_event(group, kevent);
Jan Karad5078162014-04-03 14:46:36 -0700317 } else {
318#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300319 if (ret <= 0) {
Jan Karad5078162014-04-03 14:46:36 -0700320 FANOTIFY_PE(kevent)->response = FAN_DENY;
321 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300322 } else {
323 spin_lock(&group->notification_lock);
324 list_add_tail(&kevent->list,
325 &group->fanotify_data.access_list);
326 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700327 }
Jan Karad5078162014-04-03 14:46:36 -0700328#endif
329 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300330 if (ret < 0)
331 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700332 buf += ret;
333 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500334 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100335 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500336
Eric Parisa1014f12009-12-17 21:24:26 -0500337 if (start != buf && ret != -EFAULT)
338 ret = buf - start;
339 return ret;
340}
341
Eric Parisb2d87902009-12-17 21:24:34 -0500342static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
343{
344#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
345 struct fanotify_response response = { .fd = -1, .response = -1 };
346 struct fsnotify_group *group;
347 int ret;
348
349 group = file->private_data;
350
351 if (count > sizeof(response))
352 count = sizeof(response);
353
354 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
355
356 if (copy_from_user(&response, buf, count))
357 return -EFAULT;
358
359 ret = process_access_response(group, &response);
360 if (ret < 0)
361 count = ret;
362
363 return count;
364#else
365 return -EINVAL;
366#endif
367}
368
Eric Paris52c923d2009-12-17 21:24:26 -0500369static int fanotify_release(struct inode *ignored, struct file *file)
370{
371 struct fsnotify_group *group = file->private_data;
Eric Paris52c923d2009-12-17 21:24:26 -0500372
Eric Paris2eebf582010-08-18 12:25:50 -0400373#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karaf0834412014-04-03 14:46:33 -0700374 struct fanotify_perm_event_info *event, *next;
Jan Kara96d41012016-09-19 14:44:30 -0700375 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400376
Jan Kara5838d442014-08-06 16:03:28 -0700377 /*
Jan Kara96d41012016-09-19 14:44:30 -0700378 * Stop new events from arriving in the notification queue. since
379 * userspace cannot use fanotify fd anymore, no event can enter or
380 * leave access_list by now either.
381 */
382 fsnotify_group_stop_queueing(group);
383
384 /*
385 * Process all permission events on access_list and notification queue
386 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700387 */
Jan Kara073f6552016-10-07 16:56:55 -0700388 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700389 list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
390 fae.fse.list) {
391 pr_debug("%s: found group=%p event=%p\n", __func__, group,
392 event);
Eric Paris2eebf582010-08-18 12:25:50 -0400393
Jan Karaf0834412014-04-03 14:46:33 -0700394 list_del_init(&event->fae.fse.list);
395 event->response = FAN_ALLOW;
Eric Paris2eebf582010-08-18 12:25:50 -0400396 }
Eric Paris2eebf582010-08-18 12:25:50 -0400397
Jan Kara5838d442014-08-06 16:03:28 -0700398 /*
Jan Kara96d41012016-09-19 14:44:30 -0700399 * Destroy all non-permission events. For permission events just
400 * dequeue them and set the response. They will be freed once the
401 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700402 */
Jan Kara96d41012016-09-19 14:44:30 -0700403 while (!fsnotify_notify_queue_is_empty(group)) {
404 fsn_event = fsnotify_remove_first_event(group);
Jan Kara1404ff32016-10-07 16:56:49 -0700405 if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700406 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700407 fsnotify_destroy_event(group, fsn_event);
Jan Karac21dbe22016-10-07 16:56:52 -0700408 spin_lock(&group->notification_lock);
Jan Kara1404ff32016-10-07 16:56:49 -0700409 } else
Jan Kara96d41012016-09-19 14:44:30 -0700410 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
411 }
Jan Karac21dbe22016-10-07 16:56:52 -0700412 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700413
414 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400415 wake_up(&group->fanotify_data.access_waitq);
416#endif
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400417
Eric Paris52c923d2009-12-17 21:24:26 -0500418 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200419 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500420
421 return 0;
422}
423
Eric Parisa1014f12009-12-17 21:24:26 -0500424static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
425{
426 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800427 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500428 void __user *p;
429 int ret = -ENOTTY;
430 size_t send_len = 0;
431
432 group = file->private_data;
433
434 p = (void __user *) arg;
435
436 switch (cmd) {
437 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700438 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800439 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500440 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700441 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500442 ret = put_user(send_len, (int __user *) p);
443 break;
444 }
445
446 return ret;
447}
448
Eric Paris52c923d2009-12-17 21:24:26 -0500449static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800450 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500451 .poll = fanotify_poll,
452 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500453 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500454 .fasync = NULL,
455 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500456 .unlocked_ioctl = fanotify_ioctl,
457 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200458 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500459};
460
Eric Paris2a3edf82009-12-17 21:24:26 -0500461static int fanotify_find_path(int dfd, const char __user *filename,
462 struct path *path, unsigned int flags)
463{
464 int ret;
465
466 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
467 dfd, filename, flags);
468
469 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400470 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500471
472 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400473 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500474 goto out;
475
476 ret = -ENOTDIR;
477 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500478 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400479 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500480 goto out;
481 }
482
Al Viro2903ff02012-08-28 12:52:22 -0400483 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500484 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400485 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500486 } else {
487 unsigned int lookup_flags = 0;
488
489 if (!(flags & FAN_MARK_DONT_FOLLOW))
490 lookup_flags |= LOOKUP_FOLLOW;
491 if (flags & FAN_MARK_ONLYDIR)
492 lookup_flags |= LOOKUP_DIRECTORY;
493
494 ret = user_path_at(dfd, filename, lookup_flags, path);
495 if (ret)
496 goto out;
497 }
498
499 /* you can only watch an inode if you have read permissions on it */
500 ret = inode_permission(path->dentry->d_inode, MAY_READ);
501 if (ret)
502 path_put(path);
503out:
504 return ret;
505}
506
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500507static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
508 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200509 unsigned int flags,
510 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500511{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800512 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500513
514 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500515 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800516 __u32 tmask = fsn_mark->mask & ~mask;
517
518 if (flags & FAN_MARK_ONDIR)
519 tmask &= ~FAN_ONDIR;
520
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500521 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100522 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500523 } else {
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800524 __u32 tmask = fsn_mark->ignored_mask & ~mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800525 if (flags & FAN_MARK_ONDIR)
526 tmask &= ~FAN_ONDIR;
Jan Kara66d2b812016-12-21 16:03:59 +0100527 fsn_mark->ignored_mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500528 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800529 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500530 spin_unlock(&fsn_mark->lock);
531
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500532 return mask & oldmask;
533}
534
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500535static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500536 struct vfsmount *mnt, __u32 mask,
537 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500538{
539 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500540 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200541 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500542
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700543 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100544 fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
545 group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700546 if (!fsn_mark) {
547 mutex_unlock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500548 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700549 }
Eric Paris88826272009-12-17 21:24:28 -0500550
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200551 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
552 &destroy_mark);
Jan Karac9747642016-12-14 13:53:46 +0100553 if (removed & real_mount(mnt)->mnt_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100554 fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200555 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700556 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700557 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700558 if (destroy_mark)
559 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200560
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500561 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500562 return 0;
563}
564
565static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500566 struct inode *inode, __u32 mask,
567 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500568{
569 struct fsnotify_mark *fsn_mark = NULL;
570 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200571 int destroy_mark;
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500572
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700573 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100574 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700575 if (!fsn_mark) {
576 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500577 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700578 }
Eric Paris88826272009-12-17 21:24:28 -0500579
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200580 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
581 &destroy_mark);
Jan Karac9747642016-12-14 13:53:46 +0100582 if (removed & inode->i_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100583 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200584 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700585 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700586 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700587 if (destroy_mark)
588 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700589
Jan Karab1362ed2016-12-21 16:28:45 +0100590 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500591 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500592
Eric Paris2a3edf82009-12-17 21:24:26 -0500593 return 0;
594}
595
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500596static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
597 __u32 mask,
598 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500599{
Eric Paris192ca4d2010-10-28 17:21:59 -0400600 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500601
602 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500603 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800604 __u32 tmask = fsn_mark->mask | mask;
605
606 if (flags & FAN_MARK_ONDIR)
607 tmask |= FAN_ONDIR;
608
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500609 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100610 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500611 } else {
Eric Paris192ca4d2010-10-28 17:21:59 -0400612 __u32 tmask = fsn_mark->ignored_mask | mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800613 if (flags & FAN_MARK_ONDIR)
614 tmask |= FAN_ONDIR;
615
Jan Kara66d2b812016-12-21 16:03:59 +0100616 fsn_mark->ignored_mask = tmask;
Eric Parisc9778a92009-12-17 21:24:33 -0500617 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
618 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500619 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500620 spin_unlock(&fsn_mark->lock);
621
622 return mask & ~oldmask;
623}
624
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700625static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
626 struct inode *inode,
627 struct vfsmount *mnt)
628{
629 struct fsnotify_mark *mark;
630 int ret;
631
632 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
633 return ERR_PTR(-ENOSPC);
634
635 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
636 if (!mark)
637 return ERR_PTR(-ENOMEM);
638
Jan Kara054c6362016-12-21 18:06:12 +0100639 fsnotify_init_mark(mark, group);
Jan Kara7b1293232016-12-21 18:32:48 +0100640 ret = fsnotify_add_mark_locked(mark, inode, mnt, 0);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700641 if (ret) {
642 fsnotify_put_mark(mark);
643 return ERR_PTR(ret);
644 }
645
646 return mark;
647}
648
649
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500650static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500651 struct vfsmount *mnt, __u32 mask,
652 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500653{
654 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500655 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500656
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700657 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100658 fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
659 group);
Eric Paris88826272009-12-17 21:24:28 -0500660 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700661 fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
662 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700663 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700664 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700665 }
Eric Paris88826272009-12-17 21:24:28 -0500666 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500667 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Al Viroc63181e2011-11-25 02:35:16 -0500668 if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100669 fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
Jan Karac9747642016-12-14 13:53:46 +0100670 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700671
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100672 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700673 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500674}
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{
680 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500681 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500682
683 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500684
Eric Paris5322a592010-10-28 17:21:57 -0400685 /*
686 * If some other task has this inode open for write we should not add
687 * an ignored mark, unless that ignored mark is supposed to survive
688 * modification changes anyway.
689 */
690 if ((flags & FAN_MARK_IGNORED_MASK) &&
691 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
692 (atomic_read(&inode->i_writecount) > 0))
693 return 0;
694
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700695 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100696 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris2a3edf82009-12-17 21:24:26 -0500697 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700698 fsn_mark = fanotify_add_new_mark(group, inode, NULL);
699 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700700 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700701 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700702 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500703 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500704 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Eric Paris43709a22010-07-28 10:18:39 -0400705 if (added & ~inode->i_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100706 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Jan Karac9747642016-12-14 13:53:46 +0100707 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700708
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100709 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700710 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500711}
Eric Paris2a3edf82009-12-17 21:24:26 -0500712
Eric Paris52c923d2009-12-17 21:24:26 -0500713/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400714SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500715{
Eric Paris52c923d2009-12-17 21:24:26 -0500716 struct fsnotify_group *group;
717 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400718 struct user_struct *user;
Jan Karaff57cd52014-02-21 19:14:11 +0100719 struct fanotify_event_info *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500720
Eric Paris08ae8932010-05-27 09:41:40 -0400721 pr_debug("%s: flags=%d event_f_flags=%d\n",
722 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500723
Eric Paris52c923d2009-12-17 21:24:26 -0500724 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200725 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500726
Steve Grubbde8cd832017-10-02 20:21:39 -0400727#ifdef CONFIG_AUDITSYSCALL
728 if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
729#else
Eric Paris52c923d2009-12-17 21:24:26 -0500730 if (flags & ~FAN_ALL_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400731#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500732 return -EINVAL;
733
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700734 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
735 return -EINVAL;
736
737 switch (event_f_flags & O_ACCMODE) {
738 case O_RDONLY:
739 case O_RDWR:
740 case O_WRONLY:
741 break;
742 default:
743 return -EINVAL;
744 }
745
Eric Paris4afeff82010-10-28 17:21:58 -0400746 user = get_current_user();
747 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
748 free_uid(user);
749 return -EMFILE;
750 }
751
Eric Parisb2d87902009-12-17 21:24:34 -0500752 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500753 if (flags & FAN_CLOEXEC)
754 f_flags |= O_CLOEXEC;
755 if (flags & FAN_NONBLOCK)
756 f_flags |= O_NONBLOCK;
757
758 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
759 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500760 if (IS_ERR(group)) {
761 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500762 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500763 }
Eric Paris52c923d2009-12-17 21:24:26 -0500764
Eric Paris4afeff82010-10-28 17:21:58 -0400765 group->fanotify_data.user = user;
766 atomic_inc(&user->fanotify_listeners);
767
Jan Karaf0834412014-04-03 14:46:33 -0700768 oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100769 if (unlikely(!oevent)) {
770 fd = -ENOMEM;
771 goto out_destroy_group;
772 }
773 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100774
Will Woods1e2ee492014-05-06 12:50:10 -0700775 if (force_o_largefile())
776 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400777 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500778#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Eric Paris9e66e422009-12-17 21:24:34 -0500779 init_waitqueue_head(&group->fanotify_data.access_waitq);
780 INIT_LIST_HEAD(&group->fanotify_data.access_list);
781#endif
Eric Paris4231a232010-10-28 17:21:56 -0400782 switch (flags & FAN_ALL_CLASS_BITS) {
783 case FAN_CLASS_NOTIF:
784 group->priority = FS_PRIO_0;
785 break;
786 case FAN_CLASS_CONTENT:
787 group->priority = FS_PRIO_1;
788 break;
789 case FAN_CLASS_PRE_CONTENT:
790 group->priority = FS_PRIO_2;
791 break;
792 default:
793 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200794 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400795 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500796
Eric Paris5dd03f52010-10-28 17:21:57 -0400797 if (flags & FAN_UNLIMITED_QUEUE) {
798 fd = -EPERM;
799 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200800 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400801 group->max_events = UINT_MAX;
802 } else {
803 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
804 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400805
Eric Parisac7e22d2010-10-28 17:21:58 -0400806 if (flags & FAN_UNLIMITED_MARKS) {
807 fd = -EPERM;
808 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200809 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400810 group->fanotify_data.max_marks = UINT_MAX;
811 } else {
812 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
813 }
Eric Parise7099d82010-10-28 17:21:57 -0400814
Steve Grubbde8cd832017-10-02 20:21:39 -0400815 if (flags & FAN_ENABLE_AUDIT) {
816 fd = -EPERM;
817 if (!capable(CAP_AUDIT_WRITE))
818 goto out_destroy_group;
819 group->fanotify_data.audit = true;
820 }
821
Eric Paris52c923d2009-12-17 21:24:26 -0500822 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
823 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200824 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500825
826 return fd;
827
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200828out_destroy_group:
829 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500830 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500831}
Eric Parisbbaa4162009-12-17 21:24:26 -0500832
Al Viro4a0fd5b2013-01-21 15:16:58 -0500833SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
834 __u64, mask, int, dfd,
835 const char __user *, pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500836{
Eric Paris0ff21db2009-12-17 21:24:29 -0500837 struct inode *inode = NULL;
838 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500839 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400840 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500841 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400842 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500843
844 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
845 __func__, fanotify_fd, flags, dfd, pathname, mask);
846
847 /* we only use the lower 32 bits as of right now. */
848 if (mask & ((__u64)0xffffffff << 32))
849 return -EINVAL;
850
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500851 if (flags & ~FAN_ALL_MARK_FLAGS)
852 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500853 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100854 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500855 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100856 if (!mask)
857 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700858 break;
Eric Paris4d926042009-12-17 21:24:34 -0500859 case FAN_MARK_FLUSH:
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700860 if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
861 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500862 break;
863 default:
864 return -EINVAL;
865 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400866
867 if (mask & FAN_ONDIR) {
868 flags |= FAN_MARK_ONDIR;
869 mask &= ~FAN_ONDIR;
870 }
871
Eric Parisb2d87902009-12-17 21:24:34 -0500872#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
873 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
874#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500875 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500876#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500877 return -EINVAL;
878
Al Viro2903ff02012-08-28 12:52:22 -0400879 f = fdget(fanotify_fd);
880 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -0500881 return -EBADF;
882
883 /* verify that this is indeed an fanotify instance */
884 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400885 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -0500886 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -0400887 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -0400888
889 /*
890 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
891 * allowed to set permissions events.
892 */
893 ret = -EINVAL;
894 if (mask & FAN_ALL_PERM_EVENTS &&
895 group->priority == FS_PRIO_0)
896 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500897
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700898 if (flags & FAN_MARK_FLUSH) {
899 ret = 0;
900 if (flags & FAN_MARK_MOUNT)
901 fsnotify_clear_vfsmount_marks_by_group(group);
902 else
903 fsnotify_clear_inode_marks_by_group(group);
904 goto fput_and_out;
905 }
906
Eric Paris2a3edf82009-12-17 21:24:26 -0500907 ret = fanotify_find_path(dfd, pathname, &path, flags);
908 if (ret)
909 goto fput_and_out;
910
911 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500912 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500913 inode = path.dentry->d_inode;
914 else
915 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500916
917 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700918 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500919 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500920 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500921 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500922 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500923 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500924 break;
925 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500926 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500927 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500928 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500929 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500930 break;
931 default:
932 ret = -EINVAL;
933 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500934
935 path_put(&path);
936fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400937 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500938 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500939}
Eric Paris2a3edf82009-12-17 21:24:26 -0500940
Al Viro91c2e0b2013-03-05 20:10:59 -0500941#ifdef CONFIG_COMPAT
942COMPAT_SYSCALL_DEFINE6(fanotify_mark,
943 int, fanotify_fd, unsigned int, flags,
944 __u32, mask0, __u32, mask1, int, dfd,
945 const char __user *, pathname)
946{
947 return sys_fanotify_mark(fanotify_fd, flags,
948#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -0500949 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -0800950#else
951 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -0500952#endif
953 dfd, pathname);
954}
955#endif
956
Eric Paris2a3edf82009-12-17 21:24:26 -0500957/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100958 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500959 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
960 * must result in panic().
961 */
962static int __init fanotify_user_setup(void)
963{
964 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Jan Kara7053aee2014-01-21 15:48:14 -0800965 fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
Jan Karaf0834412014-04-03 14:46:33 -0700966#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
967 fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
968 SLAB_PANIC);
969#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500970
971 return 0;
972}
973device_initcall(fanotify_user_setup);