blob: 80091a5dc8c09e63649f908607f6abb78ccaad00 [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>
Eric Parisa1014f12009-12-17 21:24:26 -050017
18#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050019
Al Viroc63181e2011-11-25 02:35:16 -050020#include "../../mount.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080021#include "../fdinfo.h"
Jan Kara7053aee2014-01-21 15:48:14 -080022#include "fanotify.h"
Al Viroc63181e2011-11-25 02:35:16 -050023
Eric Paris2529a0d2010-10-28 17:21:57 -040024#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Eric Parise7099d82010-10-28 17:21:57 -040025#define FANOTIFY_DEFAULT_MAX_MARKS 8192
Eric Paris4afeff82010-10-28 17:21:58 -040026#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
Eric Paris2529a0d2010-10-28 17:21:57 -040027
Heinrich Schuchardt48149e92014-06-04 16:05:44 -070028/*
29 * All flags that may be specified in parameter event_f_flags of fanotify_init.
30 *
31 * Internal and external open flags are stored together in field f_flags of
32 * struct file. Only external open flags shall be allowed in event_f_flags.
33 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
34 * excluded.
35 */
36#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
37 O_ACCMODE | O_APPEND | O_NONBLOCK | \
38 __O_SYNC | O_DSYNC | O_CLOEXEC | \
39 O_LARGEFILE | O_NOATIME )
40
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050041extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050042
Eric Paris2a3edf82009-12-17 21:24:26 -050043static struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7053aee2014-01-21 15:48:14 -080044struct kmem_cache *fanotify_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070045struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050046
Eric Parisa1014f12009-12-17 21:24:26 -050047/*
48 * Get an fsnotify notification event if one exists and is small
49 * enough to fit in "count". Return an error pointer if the count
50 * is not large enough.
51 *
Jan Karac21dbe22016-10-07 16:56:52 -070052 * Called with the group->notification_lock held.
Eric Parisa1014f12009-12-17 21:24:26 -050053 */
54static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
55 size_t count)
56{
Jan Karac21dbe22016-10-07 16:56:52 -070057 BUG_ON(IS_ENABLED(CONFIG_SMP) &&
58 !spin_is_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 Kara9573f792014-04-03 14:46:34 -0700151 spin_lock(&group->fanotify_data.access_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 Kara9573f792014-04-03 14:46:34 -0700161 spin_unlock(&group->fanotify_data.access_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 }
313 spin_lock(&group->fanotify_data.access_lock);
314 list_add_tail(&kevent->list,
315 &group->fanotify_data.access_list);
316 spin_unlock(&group->fanotify_data.access_lock);
317#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 Kara9573f792014-04-03 14:46:34 -0700375 spin_lock(&group->fanotify_data.access_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 }
Jan Kara9573f792014-04-03 14:46:34 -0700384 spin_unlock(&group->fanotify_data.access_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400385
Jan Kara5838d442014-08-06 16:03:28 -0700386 /*
Jan Kara96d41012016-09-19 14:44:30 -0700387 * Destroy all non-permission events. For permission events just
388 * dequeue them and set the response. They will be freed once the
389 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700390 */
Jan Karac21dbe22016-10-07 16:56:52 -0700391 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700392 while (!fsnotify_notify_queue_is_empty(group)) {
393 fsn_event = fsnotify_remove_first_event(group);
Jan Kara1404ff32016-10-07 16:56:49 -0700394 if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700395 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700396 fsnotify_destroy_event(group, fsn_event);
Jan Karac21dbe22016-10-07 16:56:52 -0700397 spin_lock(&group->notification_lock);
Jan Kara1404ff32016-10-07 16:56:49 -0700398 } else
Jan Kara96d41012016-09-19 14:44:30 -0700399 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
400 }
Jan Karac21dbe22016-10-07 16:56:52 -0700401 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700402
403 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400404 wake_up(&group->fanotify_data.access_waitq);
405#endif
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400406
Eric Paris52c923d2009-12-17 21:24:26 -0500407 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200408 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500409
410 return 0;
411}
412
Eric Parisa1014f12009-12-17 21:24:26 -0500413static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
414{
415 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800416 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500417 void __user *p;
418 int ret = -ENOTTY;
419 size_t send_len = 0;
420
421 group = file->private_data;
422
423 p = (void __user *) arg;
424
425 switch (cmd) {
426 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700427 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800428 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500429 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700430 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500431 ret = put_user(send_len, (int __user *) p);
432 break;
433 }
434
435 return ret;
436}
437
Eric Paris52c923d2009-12-17 21:24:26 -0500438static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800439 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500440 .poll = fanotify_poll,
441 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500442 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500443 .fasync = NULL,
444 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500445 .unlocked_ioctl = fanotify_ioctl,
446 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200447 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500448};
449
Eric Paris2a3edf82009-12-17 21:24:26 -0500450static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
451{
452 kmem_cache_free(fanotify_mark_cache, fsn_mark);
453}
454
455static int fanotify_find_path(int dfd, const char __user *filename,
456 struct path *path, unsigned int flags)
457{
458 int ret;
459
460 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
461 dfd, filename, flags);
462
463 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400464 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500465
466 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400467 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500468 goto out;
469
470 ret = -ENOTDIR;
471 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500472 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400473 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500474 goto out;
475 }
476
Al Viro2903ff02012-08-28 12:52:22 -0400477 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500478 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400479 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500480 } else {
481 unsigned int lookup_flags = 0;
482
483 if (!(flags & FAN_MARK_DONT_FOLLOW))
484 lookup_flags |= LOOKUP_FOLLOW;
485 if (flags & FAN_MARK_ONLYDIR)
486 lookup_flags |= LOOKUP_DIRECTORY;
487
488 ret = user_path_at(dfd, filename, lookup_flags, path);
489 if (ret)
490 goto out;
491 }
492
493 /* you can only watch an inode if you have read permissions on it */
494 ret = inode_permission(path->dentry->d_inode, MAY_READ);
495 if (ret)
496 path_put(path);
497out:
498 return ret;
499}
500
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500501static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
502 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200503 unsigned int flags,
504 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500505{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800506 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500507
508 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500509 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800510 __u32 tmask = fsn_mark->mask & ~mask;
511
512 if (flags & FAN_MARK_ONDIR)
513 tmask &= ~FAN_ONDIR;
514
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500515 oldmask = fsn_mark->mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800516 fsnotify_set_mark_mask_locked(fsn_mark, tmask);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500517 } else {
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800518 __u32 tmask = fsn_mark->ignored_mask & ~mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800519 if (flags & FAN_MARK_ONDIR)
520 tmask &= ~FAN_ONDIR;
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800521
522 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500523 }
Lino Sanfilippoa1184492015-02-10 14:08:21 -0800524 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500525 spin_unlock(&fsn_mark->lock);
526
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500527 return mask & oldmask;
528}
529
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500530static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500531 struct vfsmount *mnt, __u32 mask,
532 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500533{
534 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500535 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200536 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500537
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700538 mutex_lock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500539 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700540 if (!fsn_mark) {
541 mutex_unlock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500542 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700543 }
Eric Paris88826272009-12-17 21:24:28 -0500544
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200545 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
546 &destroy_mark);
547 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700548 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700549 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700550 if (destroy_mark)
551 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200552
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500553 fsnotify_put_mark(fsn_mark);
Al Viroc63181e2011-11-25 02:35:16 -0500554 if (removed & real_mount(mnt)->mnt_fsnotify_mask)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500555 fsnotify_recalc_vfsmount_mask(mnt);
Eric Paris88826272009-12-17 21:24:28 -0500556
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500557 return 0;
558}
559
560static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500561 struct inode *inode, __u32 mask,
562 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500563{
564 struct fsnotify_mark *fsn_mark = NULL;
565 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200566 int destroy_mark;
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500567
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700568 mutex_lock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500569 fsn_mark = fsnotify_find_inode_mark(group, inode);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700570 if (!fsn_mark) {
571 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500572 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700573 }
Eric Paris88826272009-12-17 21:24:28 -0500574
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200575 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
576 &destroy_mark);
577 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700578 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700579 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700580 if (destroy_mark)
581 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700582
Eric Paris5444e292009-12-17 21:24:27 -0500583 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500584 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500585 if (removed & inode->i_fsnotify_mask)
586 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500587
Eric Paris2a3edf82009-12-17 21:24:26 -0500588 return 0;
589}
590
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500591static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
592 __u32 mask,
593 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500594{
Eric Paris192ca4d2010-10-28 17:21:59 -0400595 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500596
597 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500598 if (!(flags & FAN_MARK_IGNORED_MASK)) {
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800599 __u32 tmask = fsn_mark->mask | mask;
600
601 if (flags & FAN_MARK_ONDIR)
602 tmask |= FAN_ONDIR;
603
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500604 oldmask = fsn_mark->mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800605 fsnotify_set_mark_mask_locked(fsn_mark, tmask);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500606 } else {
Eric Paris192ca4d2010-10-28 17:21:59 -0400607 __u32 tmask = fsn_mark->ignored_mask | mask;
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800608 if (flags & FAN_MARK_ONDIR)
609 tmask |= FAN_ONDIR;
610
Eric Paris192ca4d2010-10-28 17:21:59 -0400611 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
Eric Parisc9778a92009-12-17 21:24:33 -0500612 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
613 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500614 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500615 spin_unlock(&fsn_mark->lock);
616
617 return mask & ~oldmask;
618}
619
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700620static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
621 struct inode *inode,
622 struct vfsmount *mnt)
623{
624 struct fsnotify_mark *mark;
625 int ret;
626
627 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
628 return ERR_PTR(-ENOSPC);
629
630 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
631 if (!mark)
632 return ERR_PTR(-ENOMEM);
633
634 fsnotify_init_mark(mark, fanotify_free_mark);
635 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, 0);
636 if (ret) {
637 fsnotify_put_mark(mark);
638 return ERR_PTR(ret);
639 }
640
641 return mark;
642}
643
644
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500645static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500646 struct vfsmount *mnt, __u32 mask,
647 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500648{
649 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500650 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500651
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700652 mutex_lock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500653 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
654 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700655 fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
656 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700657 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700658 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700659 }
Eric Paris88826272009-12-17 21:24:28 -0500660 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500661 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700662 mutex_unlock(&group->mark_mutex);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100663
Al Viroc63181e2011-11-25 02:35:16 -0500664 if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
Eric Paris43709a22010-07-28 10:18:39 -0400665 fsnotify_recalc_vfsmount_mask(mnt);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700666
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100667 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700668 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500669}
670
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500671static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500672 struct inode *inode, __u32 mask,
673 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500674{
675 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500676 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500677
678 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500679
Eric Paris5322a592010-10-28 17:21:57 -0400680 /*
681 * If some other task has this inode open for write we should not add
682 * an ignored mark, unless that ignored mark is supposed to survive
683 * modification changes anyway.
684 */
685 if ((flags & FAN_MARK_IGNORED_MASK) &&
686 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
687 (atomic_read(&inode->i_writecount) > 0))
688 return 0;
689
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700690 mutex_lock(&group->mark_mutex);
Eric Paris5444e292009-12-17 21:24:27 -0500691 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500692 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700693 fsn_mark = fanotify_add_new_mark(group, inode, NULL);
694 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700695 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700696 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700697 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500698 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500699 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700700 mutex_unlock(&group->mark_mutex);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100701
Eric Paris43709a22010-07-28 10:18:39 -0400702 if (added & ~inode->i_fsnotify_mask)
703 fsnotify_recalc_inode_mask(inode);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700704
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100705 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700706 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500707}
Eric Paris2a3edf82009-12-17 21:24:26 -0500708
Eric Paris52c923d2009-12-17 21:24:26 -0500709/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400710SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500711{
Eric Paris52c923d2009-12-17 21:24:26 -0500712 struct fsnotify_group *group;
713 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400714 struct user_struct *user;
Jan Karaff57cd52014-02-21 19:14:11 +0100715 struct fanotify_event_info *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500716
Eric Paris08ae8932010-05-27 09:41:40 -0400717 pr_debug("%s: flags=%d event_f_flags=%d\n",
718 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500719
Eric Paris52c923d2009-12-17 21:24:26 -0500720 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200721 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500722
723 if (flags & ~FAN_ALL_INIT_FLAGS)
724 return -EINVAL;
725
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700726 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
727 return -EINVAL;
728
729 switch (event_f_flags & O_ACCMODE) {
730 case O_RDONLY:
731 case O_RDWR:
732 case O_WRONLY:
733 break;
734 default:
735 return -EINVAL;
736 }
737
Eric Paris4afeff82010-10-28 17:21:58 -0400738 user = get_current_user();
739 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
740 free_uid(user);
741 return -EMFILE;
742 }
743
Eric Parisb2d87902009-12-17 21:24:34 -0500744 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500745 if (flags & FAN_CLOEXEC)
746 f_flags |= O_CLOEXEC;
747 if (flags & FAN_NONBLOCK)
748 f_flags |= O_NONBLOCK;
749
750 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
751 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500752 if (IS_ERR(group)) {
753 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500754 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500755 }
Eric Paris52c923d2009-12-17 21:24:26 -0500756
Eric Paris4afeff82010-10-28 17:21:58 -0400757 group->fanotify_data.user = user;
758 atomic_inc(&user->fanotify_listeners);
759
Jan Karaf0834412014-04-03 14:46:33 -0700760 oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100761 if (unlikely(!oevent)) {
762 fd = -ENOMEM;
763 goto out_destroy_group;
764 }
765 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100766
Will Woods1e2ee492014-05-06 12:50:10 -0700767 if (force_o_largefile())
768 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400769 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500770#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Kara9573f792014-04-03 14:46:34 -0700771 spin_lock_init(&group->fanotify_data.access_lock);
Eric Paris9e66e422009-12-17 21:24:34 -0500772 init_waitqueue_head(&group->fanotify_data.access_waitq);
773 INIT_LIST_HEAD(&group->fanotify_data.access_list);
774#endif
Eric Paris4231a232010-10-28 17:21:56 -0400775 switch (flags & FAN_ALL_CLASS_BITS) {
776 case FAN_CLASS_NOTIF:
777 group->priority = FS_PRIO_0;
778 break;
779 case FAN_CLASS_CONTENT:
780 group->priority = FS_PRIO_1;
781 break;
782 case FAN_CLASS_PRE_CONTENT:
783 group->priority = FS_PRIO_2;
784 break;
785 default:
786 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200787 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400788 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500789
Eric Paris5dd03f52010-10-28 17:21:57 -0400790 if (flags & FAN_UNLIMITED_QUEUE) {
791 fd = -EPERM;
792 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200793 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400794 group->max_events = UINT_MAX;
795 } else {
796 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
797 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400798
Eric Parisac7e22d2010-10-28 17:21:58 -0400799 if (flags & FAN_UNLIMITED_MARKS) {
800 fd = -EPERM;
801 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200802 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400803 group->fanotify_data.max_marks = UINT_MAX;
804 } else {
805 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
806 }
Eric Parise7099d82010-10-28 17:21:57 -0400807
Eric Paris52c923d2009-12-17 21:24:26 -0500808 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
809 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200810 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500811
812 return fd;
813
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200814out_destroy_group:
815 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500816 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500817}
Eric Parisbbaa4162009-12-17 21:24:26 -0500818
Al Viro4a0fd5b2013-01-21 15:16:58 -0500819SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
820 __u64, mask, int, dfd,
821 const char __user *, pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500822{
Eric Paris0ff21db2009-12-17 21:24:29 -0500823 struct inode *inode = NULL;
824 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500825 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400826 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500827 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400828 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500829
830 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
831 __func__, fanotify_fd, flags, dfd, pathname, mask);
832
833 /* we only use the lower 32 bits as of right now. */
834 if (mask & ((__u64)0xffffffff << 32))
835 return -EINVAL;
836
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500837 if (flags & ~FAN_ALL_MARK_FLAGS)
838 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500839 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100840 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500841 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100842 if (!mask)
843 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700844 break;
Eric Paris4d926042009-12-17 21:24:34 -0500845 case FAN_MARK_FLUSH:
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -0700846 if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
847 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500848 break;
849 default:
850 return -EINVAL;
851 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400852
853 if (mask & FAN_ONDIR) {
854 flags |= FAN_MARK_ONDIR;
855 mask &= ~FAN_ONDIR;
856 }
857
Eric Parisb2d87902009-12-17 21:24:34 -0500858#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
859 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
860#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500861 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500862#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500863 return -EINVAL;
864
Al Viro2903ff02012-08-28 12:52:22 -0400865 f = fdget(fanotify_fd);
866 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -0500867 return -EBADF;
868
869 /* verify that this is indeed an fanotify instance */
870 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400871 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -0500872 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -0400873 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -0400874
875 /*
876 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
877 * allowed to set permissions events.
878 */
879 ret = -EINVAL;
880 if (mask & FAN_ALL_PERM_EVENTS &&
881 group->priority == FS_PRIO_0)
882 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500883
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700884 if (flags & FAN_MARK_FLUSH) {
885 ret = 0;
886 if (flags & FAN_MARK_MOUNT)
887 fsnotify_clear_vfsmount_marks_by_group(group);
888 else
889 fsnotify_clear_inode_marks_by_group(group);
890 goto fput_and_out;
891 }
892
Eric Paris2a3edf82009-12-17 21:24:26 -0500893 ret = fanotify_find_path(dfd, pathname, &path, flags);
894 if (ret)
895 goto fput_and_out;
896
897 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500898 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500899 inode = path.dentry->d_inode;
900 else
901 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500902
903 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -0700904 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500905 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500906 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500907 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500908 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500909 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500910 break;
911 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500912 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500913 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500914 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500915 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500916 break;
917 default:
918 ret = -EINVAL;
919 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500920
921 path_put(&path);
922fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400923 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500924 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500925}
Eric Paris2a3edf82009-12-17 21:24:26 -0500926
Al Viro91c2e0b2013-03-05 20:10:59 -0500927#ifdef CONFIG_COMPAT
928COMPAT_SYSCALL_DEFINE6(fanotify_mark,
929 int, fanotify_fd, unsigned int, flags,
930 __u32, mask0, __u32, mask1, int, dfd,
931 const char __user *, pathname)
932{
933 return sys_fanotify_mark(fanotify_fd, flags,
934#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -0500935 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -0800936#else
937 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -0500938#endif
939 dfd, pathname);
940}
941#endif
942
Eric Paris2a3edf82009-12-17 21:24:26 -0500943/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100944 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500945 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
946 * must result in panic().
947 */
948static int __init fanotify_user_setup(void)
949{
950 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Jan Kara7053aee2014-01-21 15:48:14 -0800951 fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
Jan Karaf0834412014-04-03 14:46:33 -0700952#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
953 fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
954 SLAB_PANIC);
955#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500956
957 return 0;
958}
959device_initcall(fanotify_user_setup);