blob: bf306d4f72f73561095d4f7ec10627967cfd3e61 [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 */
182 switch (response) {
183 case FAN_ALLOW:
184 case FAN_DENY:
185 break;
186 default:
187 return -EINVAL;
188 }
189
190 if (fd < 0)
191 return -EINVAL;
192
Jan Karaf0834412014-04-03 14:46:33 -0700193 event = dequeue_event(group, fd);
194 if (!event)
Eric Parisb2d87902009-12-17 21:24:34 -0500195 return -ENOENT;
196
Jan Karaf0834412014-04-03 14:46:33 -0700197 event->response = response;
Eric Parisb2d87902009-12-17 21:24:34 -0500198 wake_up(&group->fanotify_data.access_waitq);
199
Eric Parisb2d87902009-12-17 21:24:34 -0500200 return 0;
201}
Eric Parisb2d87902009-12-17 21:24:34 -0500202#endif
203
Eric Parisa1014f12009-12-17 21:24:26 -0500204static ssize_t copy_event_to_user(struct fsnotify_group *group,
205 struct fsnotify_event *event,
206 char __user *buf)
207{
208 struct fanotify_event_metadata fanotify_event_metadata;
Al Viro352e3b22012-08-19 12:30:45 -0400209 struct file *f;
Eric Parisb2d87902009-12-17 21:24:34 -0500210 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500211
212 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
213
Al Viro352e3b22012-08-19 12:30:45 -0400214 ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
Eric Parisecf6f5e2010-11-08 18:08:14 -0500215 if (ret < 0)
Jan Karad5078162014-04-03 14:46:36 -0700216 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500217
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100218 fd = fanotify_event_metadata.fd;
Al Viro352e3b22012-08-19 12:30:45 -0400219 ret = -EFAULT;
220 if (copy_to_user(buf, &fanotify_event_metadata,
221 fanotify_event_metadata.event_len))
222 goto out_close_fd;
223
Jan Karaf0834412014-04-03 14:46:33 -0700224#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karad5078162014-04-03 14:46:36 -0700225 if (event->mask & FAN_ALL_PERM_EVENTS)
226 FANOTIFY_PE(event)->fd = fd;
Jan Karaf0834412014-04-03 14:46:33 -0700227#endif
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 */
242static unsigned int fanotify_poll(struct file *file, poll_table *wait)
243{
244 struct fsnotify_group *group = file->private_data;
245 int ret = 0;
246
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))
250 ret = POLLIN | POLLRDNORM;
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);
298 /*
299 * Permission events get queued to wait for response. Other
300 * events can be destroyed now.
301 */
Jan Karad5078162014-04-03 14:46:36 -0700302 if (!(kevent->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karad8aaab42014-04-03 14:46:35 -0700303 fsnotify_destroy_event(group, kevent);
Jan Karad5078162014-04-03 14:46:36 -0700304 if (ret < 0)
305 break;
306 } else {
307#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
308 if (ret < 0) {
309 FANOTIFY_PE(kevent)->response = FAN_DENY;
310 wake_up(&group->fanotify_data.access_waitq);
311 break;
312 }
Jan Kara073f6552016-10-07 16:56:55 -0700313 spin_lock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700314 list_add_tail(&kevent->list,
315 &group->fanotify_data.access_list);
Jan Kara073f6552016-10-07 16:56:55 -0700316 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700317#endif
318 }
Jan Karad8aaab42014-04-03 14:46:35 -0700319 buf += ret;
320 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500321 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100322 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500323
Eric Parisa1014f12009-12-17 21:24:26 -0500324 if (start != buf && ret != -EFAULT)
325 ret = buf - start;
326 return ret;
327}
328
Eric Parisb2d87902009-12-17 21:24:34 -0500329static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
330{
331#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
332 struct fanotify_response response = { .fd = -1, .response = -1 };
333 struct fsnotify_group *group;
334 int ret;
335
336 group = file->private_data;
337
338 if (count > sizeof(response))
339 count = sizeof(response);
340
341 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
342
343 if (copy_from_user(&response, buf, count))
344 return -EFAULT;
345
346 ret = process_access_response(group, &response);
347 if (ret < 0)
348 count = ret;
349
350 return count;
351#else
352 return -EINVAL;
353#endif
354}
355
Eric Paris52c923d2009-12-17 21:24:26 -0500356static int fanotify_release(struct inode *ignored, struct file *file)
357{
358 struct fsnotify_group *group = file->private_data;
Eric Paris52c923d2009-12-17 21:24:26 -0500359
Eric Paris2eebf582010-08-18 12:25:50 -0400360#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karaf0834412014-04-03 14:46:33 -0700361 struct fanotify_perm_event_info *event, *next;
Jan Kara96d41012016-09-19 14:44:30 -0700362 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400363
Jan Kara5838d442014-08-06 16:03:28 -0700364 /*
Jan Kara96d41012016-09-19 14:44:30 -0700365 * Stop new events from arriving in the notification queue. since
366 * userspace cannot use fanotify fd anymore, no event can enter or
367 * leave access_list by now either.
368 */
369 fsnotify_group_stop_queueing(group);
370
371 /*
372 * Process all permission events on access_list and notification queue
373 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700374 */
Jan Kara073f6552016-10-07 16:56:55 -0700375 spin_lock(&group->notification_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700376 list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
377 fae.fse.list) {
378 pr_debug("%s: found group=%p event=%p\n", __func__, group,
379 event);
Eric Paris2eebf582010-08-18 12:25:50 -0400380
Jan Karaf0834412014-04-03 14:46:33 -0700381 list_del_init(&event->fae.fse.list);
382 event->response = FAN_ALLOW;
Eric Paris2eebf582010-08-18 12:25:50 -0400383 }
Eric Paris2eebf582010-08-18 12:25:50 -0400384
Jan Kara5838d442014-08-06 16:03:28 -0700385 /*
Jan Kara96d41012016-09-19 14:44:30 -0700386 * Destroy all non-permission events. For permission events just
387 * dequeue them and set the response. They will be freed once the
388 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700389 */
Jan Kara96d41012016-09-19 14:44:30 -0700390 while (!fsnotify_notify_queue_is_empty(group)) {
391 fsn_event = fsnotify_remove_first_event(group);
Jan Kara1404ff32016-10-07 16:56:49 -0700392 if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700393 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700394 fsnotify_destroy_event(group, fsn_event);
Jan Karac21dbe22016-10-07 16:56:52 -0700395 spin_lock(&group->notification_lock);
Jan Kara1404ff32016-10-07 16:56:49 -0700396 } else
Jan Kara96d41012016-09-19 14:44:30 -0700397 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
398 }
Jan Karac21dbe22016-10-07 16:56:52 -0700399 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700400
401 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400402 wake_up(&group->fanotify_data.access_waitq);
403#endif
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400404
Eric Paris52c923d2009-12-17 21:24:26 -0500405 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200406 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500407
408 return 0;
409}
410
Eric Parisa1014f12009-12-17 21:24:26 -0500411static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
412{
413 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800414 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500415 void __user *p;
416 int ret = -ENOTTY;
417 size_t send_len = 0;
418
419 group = file->private_data;
420
421 p = (void __user *) arg;
422
423 switch (cmd) {
424 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700425 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800426 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500427 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700428 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500429 ret = put_user(send_len, (int __user *) p);
430 break;
431 }
432
433 return ret;
434}
435
Eric Paris52c923d2009-12-17 21:24:26 -0500436static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800437 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500438 .poll = fanotify_poll,
439 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500440 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500441 .fasync = NULL,
442 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500443 .unlocked_ioctl = fanotify_ioctl,
444 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200445 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500446};
447
Eric Paris2a3edf82009-12-17 21:24:26 -0500448static int fanotify_find_path(int dfd, const char __user *filename,
449 struct path *path, unsigned int flags)
450{
451 int ret;
452
453 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
454 dfd, filename, flags);
455
456 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400457 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500458
459 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400460 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500461 goto out;
462
463 ret = -ENOTDIR;
464 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500465 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400466 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500467 goto out;
468 }
469
Al Viro2903ff02012-08-28 12:52:22 -0400470 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500471 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400472 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500473 } else {
474 unsigned int lookup_flags = 0;
475
476 if (!(flags & FAN_MARK_DONT_FOLLOW))
477 lookup_flags |= LOOKUP_FOLLOW;
478 if (flags & FAN_MARK_ONLYDIR)
479 lookup_flags |= LOOKUP_DIRECTORY;
480
481 ret = user_path_at(dfd, filename, lookup_flags, path);
482 if (ret)
483 goto out;
484 }
485
486 /* you can only watch an inode if you have read permissions on it */
487 ret = inode_permission(path->dentry->d_inode, MAY_READ);
488 if (ret)
489 path_put(path);
490out:
491 return ret;
492}
493
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500494static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
495 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200496 unsigned int flags,
497 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500498{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800499 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500500
501 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500502 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800503 __u32 tmask = fsn_mark->mask & ~mask;
504
505 if (flags & FAN_MARK_ONDIR)
506 tmask &= ~FAN_ONDIR;
507
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500508 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100509 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500510 } else {
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800511 __u32 tmask = fsn_mark->ignored_mask & ~mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800512 if (flags & FAN_MARK_ONDIR)
513 tmask &= ~FAN_ONDIR;
Jan Kara66d2b812016-12-21 16:03:59 +0100514 fsn_mark->ignored_mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500515 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800516 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500517 spin_unlock(&fsn_mark->lock);
518
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500519 return mask & oldmask;
520}
521
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500522static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500523 struct vfsmount *mnt, __u32 mask,
524 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500525{
526 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500527 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200528 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500529
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700530 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100531 fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
532 group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700533 if (!fsn_mark) {
534 mutex_unlock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500535 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700536 }
Eric Paris88826272009-12-17 21:24:28 -0500537
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200538 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
539 &destroy_mark);
Jan Karac9747642016-12-14 13:53:46 +0100540 if (removed & real_mount(mnt)->mnt_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100541 fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200542 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700543 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700544 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700545 if (destroy_mark)
546 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200547
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500548 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500549 return 0;
550}
551
552static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500553 struct inode *inode, __u32 mask,
554 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500555{
556 struct fsnotify_mark *fsn_mark = NULL;
557 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200558 int destroy_mark;
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500559
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700560 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100561 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700562 if (!fsn_mark) {
563 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500564 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700565 }
Eric Paris88826272009-12-17 21:24:28 -0500566
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200567 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
568 &destroy_mark);
Jan Karac9747642016-12-14 13:53:46 +0100569 if (removed & inode->i_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100570 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200571 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700572 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700573 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700574 if (destroy_mark)
575 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700576
Jan Karab1362ed2016-12-21 16:28:45 +0100577 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500578 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500579
Eric Paris2a3edf82009-12-17 21:24:26 -0500580 return 0;
581}
582
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500583static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
584 __u32 mask,
585 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500586{
Eric Paris192ca4d2010-10-28 17:21:59 -0400587 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500588
589 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500590 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800591 __u32 tmask = fsn_mark->mask | mask;
592
593 if (flags & FAN_MARK_ONDIR)
594 tmask |= FAN_ONDIR;
595
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500596 oldmask = fsn_mark->mask;
Jan Kara66d2b812016-12-21 16:03:59 +0100597 fsn_mark->mask = tmask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500598 } else {
Eric Paris192ca4d2010-10-28 17:21:59 -0400599 __u32 tmask = fsn_mark->ignored_mask | mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800600 if (flags & FAN_MARK_ONDIR)
601 tmask |= FAN_ONDIR;
602
Jan Kara66d2b812016-12-21 16:03:59 +0100603 fsn_mark->ignored_mask = tmask;
Eric Parisc9778a92009-12-17 21:24:33 -0500604 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
605 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500606 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500607 spin_unlock(&fsn_mark->lock);
608
609 return mask & ~oldmask;
610}
611
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700612static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
613 struct inode *inode,
614 struct vfsmount *mnt)
615{
616 struct fsnotify_mark *mark;
617 int ret;
618
619 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
620 return ERR_PTR(-ENOSPC);
621
622 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
623 if (!mark)
624 return ERR_PTR(-ENOMEM);
625
Jan Kara054c6362016-12-21 18:06:12 +0100626 fsnotify_init_mark(mark, group);
Jan Kara7b1293232016-12-21 18:32:48 +0100627 ret = fsnotify_add_mark_locked(mark, inode, mnt, 0);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700628 if (ret) {
629 fsnotify_put_mark(mark);
630 return ERR_PTR(ret);
631 }
632
633 return mark;
634}
635
636
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500637static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500638 struct vfsmount *mnt, __u32 mask,
639 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500640{
641 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500642 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500643
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700644 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100645 fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
646 group);
Eric Paris88826272009-12-17 21:24:28 -0500647 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700648 fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
649 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700650 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700651 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700652 }
Eric Paris88826272009-12-17 21:24:28 -0500653 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500654 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Al Viroc63181e2011-11-25 02:35:16 -0500655 if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100656 fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
Jan Karac9747642016-12-14 13:53:46 +0100657 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700658
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100659 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700660 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500661}
662
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500663static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500664 struct inode *inode, __u32 mask,
665 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500666{
667 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500668 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500669
670 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500671
Eric Paris5322a592010-10-28 17:21:57 -0400672 /*
673 * If some other task has this inode open for write we should not add
674 * an ignored mark, unless that ignored mark is supposed to survive
675 * modification changes anyway.
676 */
677 if ((flags & FAN_MARK_IGNORED_MASK) &&
678 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
679 (atomic_read(&inode->i_writecount) > 0))
680 return 0;
681
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700682 mutex_lock(&group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100683 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris2a3edf82009-12-17 21:24:26 -0500684 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700685 fsn_mark = fanotify_add_new_mark(group, inode, NULL);
686 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700687 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700688 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700689 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500690 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500691 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Eric Paris43709a22010-07-28 10:18:39 -0400692 if (added & ~inode->i_fsnotify_mask)
Jan Kara8920d272016-12-21 16:13:54 +0100693 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Jan Karac9747642016-12-14 13:53:46 +0100694 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700695
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100696 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700697 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500698}
Eric Paris2a3edf82009-12-17 21:24:26 -0500699
Eric Paris52c923d2009-12-17 21:24:26 -0500700/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400701SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500702{
Eric Paris52c923d2009-12-17 21:24:26 -0500703 struct fsnotify_group *group;
704 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400705 struct user_struct *user;
Jan Karaff57cd52014-02-21 19:14:11 +0100706 struct fanotify_event_info *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500707
Eric Paris08ae8932010-05-27 09:41:40 -0400708 pr_debug("%s: flags=%d event_f_flags=%d\n",
709 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500710
Eric Paris52c923d2009-12-17 21:24:26 -0500711 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200712 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500713
714 if (flags & ~FAN_ALL_INIT_FLAGS)
715 return -EINVAL;
716
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700717 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
718 return -EINVAL;
719
720 switch (event_f_flags & O_ACCMODE) {
721 case O_RDONLY:
722 case O_RDWR:
723 case O_WRONLY:
724 break;
725 default:
726 return -EINVAL;
727 }
728
Eric Paris4afeff82010-10-28 17:21:58 -0400729 user = get_current_user();
730 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
731 free_uid(user);
732 return -EMFILE;
733 }
734
Eric Parisb2d87902009-12-17 21:24:34 -0500735 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500736 if (flags & FAN_CLOEXEC)
737 f_flags |= O_CLOEXEC;
738 if (flags & FAN_NONBLOCK)
739 f_flags |= O_NONBLOCK;
740
741 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
742 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500743 if (IS_ERR(group)) {
744 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500745 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500746 }
Eric Paris52c923d2009-12-17 21:24:26 -0500747
Eric Paris4afeff82010-10-28 17:21:58 -0400748 group->fanotify_data.user = user;
749 atomic_inc(&user->fanotify_listeners);
750
Jan Karaf0834412014-04-03 14:46:33 -0700751 oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100752 if (unlikely(!oevent)) {
753 fd = -ENOMEM;
754 goto out_destroy_group;
755 }
756 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100757
Will Woods1e2ee492014-05-06 12:50:10 -0700758 if (force_o_largefile())
759 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400760 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500761#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Eric Paris9e66e422009-12-17 21:24:34 -0500762 init_waitqueue_head(&group->fanotify_data.access_waitq);
763 INIT_LIST_HEAD(&group->fanotify_data.access_list);
764#endif
Eric Paris4231a232010-10-28 17:21:56 -0400765 switch (flags & FAN_ALL_CLASS_BITS) {
766 case FAN_CLASS_NOTIF:
767 group->priority = FS_PRIO_0;
768 break;
769 case FAN_CLASS_CONTENT:
770 group->priority = FS_PRIO_1;
771 break;
772 case FAN_CLASS_PRE_CONTENT:
773 group->priority = FS_PRIO_2;
774 break;
775 default:
776 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200777 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400778 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500779
Eric Paris5dd03f52010-10-28 17:21:57 -0400780 if (flags & FAN_UNLIMITED_QUEUE) {
781 fd = -EPERM;
782 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200783 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400784 group->max_events = UINT_MAX;
785 } else {
786 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
787 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400788
Eric Parisac7e22d2010-10-28 17:21:58 -0400789 if (flags & FAN_UNLIMITED_MARKS) {
790 fd = -EPERM;
791 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200792 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400793 group->fanotify_data.max_marks = UINT_MAX;
794 } else {
795 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
796 }
Eric Parise7099d82010-10-28 17:21:57 -0400797
Eric Paris52c923d2009-12-17 21:24:26 -0500798 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
799 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200800 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500801
802 return fd;
803
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200804out_destroy_group:
805 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500806 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500807}
Eric Parisbbaa4162009-12-17 21:24:26 -0500808
Al Viro4a0fd5b2013-01-21 15:16:58 -0500809SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
810 __u64, mask, int, dfd,
811 const char __user *, pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500812{
Eric Paris0ff21db2009-12-17 21:24:29 -0500813 struct inode *inode = NULL;
814 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500815 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400816 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500817 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400818 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500819
820 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
821 __func__, fanotify_fd, flags, dfd, pathname, mask);
822
823 /* we only use the lower 32 bits as of right now. */
824 if (mask & ((__u64)0xffffffff << 32))
825 return -EINVAL;
826
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500827 if (flags & ~FAN_ALL_MARK_FLAGS)
828 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500829 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100830 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500831 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100832 if (!mask)
833 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700834 break;
Eric Paris4d926042009-12-17 21:24:34 -0500835 case FAN_MARK_FLUSH:
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700836 if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
837 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500838 break;
839 default:
840 return -EINVAL;
841 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400842
843 if (mask & FAN_ONDIR) {
844 flags |= FAN_MARK_ONDIR;
845 mask &= ~FAN_ONDIR;
846 }
847
Eric Parisb2d87902009-12-17 21:24:34 -0500848#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
849 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
850#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500851 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500852#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500853 return -EINVAL;
854
Al Viro2903ff02012-08-28 12:52:22 -0400855 f = fdget(fanotify_fd);
856 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -0500857 return -EBADF;
858
859 /* verify that this is indeed an fanotify instance */
860 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400861 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -0500862 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -0400863 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -0400864
865 /*
866 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
867 * allowed to set permissions events.
868 */
869 ret = -EINVAL;
870 if (mask & FAN_ALL_PERM_EVENTS &&
871 group->priority == FS_PRIO_0)
872 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500873
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700874 if (flags & FAN_MARK_FLUSH) {
875 ret = 0;
876 if (flags & FAN_MARK_MOUNT)
877 fsnotify_clear_vfsmount_marks_by_group(group);
878 else
879 fsnotify_clear_inode_marks_by_group(group);
880 goto fput_and_out;
881 }
882
Eric Paris2a3edf82009-12-17 21:24:26 -0500883 ret = fanotify_find_path(dfd, pathname, &path, flags);
884 if (ret)
885 goto fput_and_out;
886
887 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500888 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500889 inode = path.dentry->d_inode;
890 else
891 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500892
893 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700894 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500895 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500896 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500897 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500898 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500899 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500900 break;
901 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500902 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500903 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500904 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500905 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500906 break;
907 default:
908 ret = -EINVAL;
909 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500910
911 path_put(&path);
912fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400913 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500914 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500915}
Eric Paris2a3edf82009-12-17 21:24:26 -0500916
Al Viro91c2e0b2013-03-05 20:10:59 -0500917#ifdef CONFIG_COMPAT
918COMPAT_SYSCALL_DEFINE6(fanotify_mark,
919 int, fanotify_fd, unsigned int, flags,
920 __u32, mask0, __u32, mask1, int, dfd,
921 const char __user *, pathname)
922{
923 return sys_fanotify_mark(fanotify_fd, flags,
924#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -0500925 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -0800926#else
927 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -0500928#endif
929 dfd, pathname);
930}
931#endif
932
Eric Paris2a3edf82009-12-17 21:24:26 -0500933/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100934 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500935 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
936 * must result in panic().
937 */
938static int __init fanotify_user_setup(void)
939{
940 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Jan Kara7053aee2014-01-21 15:48:14 -0800941 fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
Jan Karaf0834412014-04-03 14:46:33 -0700942#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
943 fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
944 SLAB_PANIC);
945#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500946
947 return 0;
948}
949device_initcall(fanotify_user_setup);