blob: 4e565c814309d2637c7d9bb8081f03a4d19216d0 [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
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050028extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050029
Eric Paris2a3edf82009-12-17 21:24:26 -050030static struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7053aee2014-01-21 15:48:14 -080031struct kmem_cache *fanotify_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070032struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050033
Eric Parisa1014f12009-12-17 21:24:26 -050034/*
35 * Get an fsnotify notification event if one exists and is small
36 * enough to fit in "count". Return an error pointer if the count
37 * is not large enough.
38 *
39 * Called with the group->notification_mutex held.
40 */
41static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
42 size_t count)
43{
44 BUG_ON(!mutex_is_locked(&group->notification_mutex));
45
46 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
47
48 if (fsnotify_notify_queue_is_empty(group))
49 return NULL;
50
51 if (FAN_EVENT_METADATA_LEN > count)
52 return ERR_PTR(-EINVAL);
53
54 /* held the notification_mutex the whole time, so this is the
55 * same event we peeked above */
56 return fsnotify_remove_notify_event(group);
57}
58
Al Viro352e3b22012-08-19 12:30:45 -040059static int create_fd(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -080060 struct fanotify_event_info *event,
61 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -050062{
63 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050064 struct file *new_file;
65
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050066 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050067
68 client_fd = get_unused_fd();
69 if (client_fd < 0)
70 return client_fd;
71
Eric Parisa1014f12009-12-17 21:24:26 -050072 /*
73 * we need a new file handle for the userspace program so it can read even if it was
74 * originally opened O_WRONLY.
75 */
Eric Parisa1014f12009-12-17 21:24:26 -050076 /* it's possible this event was an overflow event. in that case dentry and mnt
77 * are NULL; That's fine, just don't call dentry open */
Al Viro765927b2012-06-26 21:58:53 +040078 if (event->path.dentry && event->path.mnt)
79 new_file = dentry_open(&event->path,
Eric Paris80af2582010-07-28 10:18:37 -040080 group->fanotify_data.f_flags | FMODE_NONOTIFY,
Eric Parisa1014f12009-12-17 21:24:26 -050081 current_cred());
82 else
83 new_file = ERR_PTR(-EOVERFLOW);
84 if (IS_ERR(new_file)) {
85 /*
86 * we still send an event even if we can't open the file. this
87 * can happen when say tasks are gone and we try to open their
88 * /proc files or we try to open a WRONLY file like in sysfs
89 * we just send the errno to userspace since there isn't much
90 * else we can do.
91 */
92 put_unused_fd(client_fd);
93 client_fd = PTR_ERR(new_file);
94 } else {
Al Viro352e3b22012-08-19 12:30:45 -040095 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -050096 }
97
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050098 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050099}
100
Eric Parisecf6f5e2010-11-08 18:08:14 -0500101static int fill_event_metadata(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800102 struct fanotify_event_metadata *metadata,
103 struct fsnotify_event *fsn_event,
104 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500105{
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100106 int ret = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800107 struct fanotify_event_info *event;
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100108
Eric Parisa1014f12009-12-17 21:24:26 -0500109 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
Jan Kara7053aee2014-01-21 15:48:14 -0800110 group, metadata, fsn_event);
Eric Parisa1014f12009-12-17 21:24:26 -0500111
Al Viro352e3b22012-08-19 12:30:45 -0400112 *file = NULL;
Jan Kara7053aee2014-01-21 15:48:14 -0800113 event = container_of(fsn_event, struct fanotify_event_info, fse);
Eric Parisa1014f12009-12-17 21:24:26 -0500114 metadata->event_len = FAN_EVENT_METADATA_LEN;
Eric Paris7d131622010-12-07 15:27:57 -0500115 metadata->metadata_len = FAN_EVENT_METADATA_LEN;
Eric Parisa1014f12009-12-17 21:24:26 -0500116 metadata->vers = FANOTIFY_METADATA_VERSION;
Dan Carpenterde1e0c42013-07-08 15:59:40 -0700117 metadata->reserved = 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800118 metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500119 metadata->pid = pid_vnr(event->tgid);
Jan Kara7053aee2014-01-21 15:48:14 -0800120 if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100121 metadata->fd = FAN_NOFD;
122 else {
Al Viro352e3b22012-08-19 12:30:45 -0400123 metadata->fd = create_fd(group, event, file);
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100124 if (metadata->fd < 0)
125 ret = metadata->fd;
126 }
Eric Parisa1014f12009-12-17 21:24:26 -0500127
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100128 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500129}
130
Eric Parisb2d87902009-12-17 21:24:34 -0500131#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karaf0834412014-04-03 14:46:33 -0700132static struct fanotify_perm_event_info *dequeue_event(
133 struct fsnotify_group *group, int fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500134{
Jan Karaf0834412014-04-03 14:46:33 -0700135 struct fanotify_perm_event_info *event, *return_e = NULL;
Eric Parisb2d87902009-12-17 21:24:34 -0500136
Jan Kara9573f792014-04-03 14:46:34 -0700137 spin_lock(&group->fanotify_data.access_lock);
Jan Karaf0834412014-04-03 14:46:33 -0700138 list_for_each_entry(event, &group->fanotify_data.access_list,
139 fae.fse.list) {
140 if (event->fd != fd)
Eric Parisb2d87902009-12-17 21:24:34 -0500141 continue;
142
Jan Karaf0834412014-04-03 14:46:33 -0700143 list_del_init(&event->fae.fse.list);
144 return_e = event;
Eric Parisb2d87902009-12-17 21:24:34 -0500145 break;
146 }
Jan Kara9573f792014-04-03 14:46:34 -0700147 spin_unlock(&group->fanotify_data.access_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500148
Jan Karaf0834412014-04-03 14:46:33 -0700149 pr_debug("%s: found return_re=%p\n", __func__, return_e);
Eric Parisb2d87902009-12-17 21:24:34 -0500150
Jan Karaf0834412014-04-03 14:46:33 -0700151 return return_e;
Eric Parisb2d87902009-12-17 21:24:34 -0500152}
153
154static int process_access_response(struct fsnotify_group *group,
155 struct fanotify_response *response_struct)
156{
Jan Karaf0834412014-04-03 14:46:33 -0700157 struct fanotify_perm_event_info *event;
158 int fd = response_struct->fd;
159 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500160
161 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
162 fd, response);
163 /*
164 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300165 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500166 * timeout
167 */
168 switch (response) {
169 case FAN_ALLOW:
170 case FAN_DENY:
171 break;
172 default:
173 return -EINVAL;
174 }
175
176 if (fd < 0)
177 return -EINVAL;
178
Jan Karaf0834412014-04-03 14:46:33 -0700179 event = dequeue_event(group, fd);
180 if (!event)
Eric Parisb2d87902009-12-17 21:24:34 -0500181 return -ENOENT;
182
Jan Karaf0834412014-04-03 14:46:33 -0700183 event->response = response;
Eric Parisb2d87902009-12-17 21:24:34 -0500184 wake_up(&group->fanotify_data.access_waitq);
185
Eric Parisb2d87902009-12-17 21:24:34 -0500186 return 0;
187}
Eric Parisb2d87902009-12-17 21:24:34 -0500188#endif
189
Eric Parisa1014f12009-12-17 21:24:26 -0500190static ssize_t copy_event_to_user(struct fsnotify_group *group,
191 struct fsnotify_event *event,
192 char __user *buf)
193{
194 struct fanotify_event_metadata fanotify_event_metadata;
Al Viro352e3b22012-08-19 12:30:45 -0400195 struct file *f;
Eric Parisb2d87902009-12-17 21:24:34 -0500196 int fd, ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500197
198 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
199
Al Viro352e3b22012-08-19 12:30:45 -0400200 ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
Eric Parisecf6f5e2010-11-08 18:08:14 -0500201 if (ret < 0)
Jan Karad5078162014-04-03 14:46:36 -0700202 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500203
Lino Sanfilippofdbf3ce2010-11-24 18:26:04 +0100204 fd = fanotify_event_metadata.fd;
Al Viro352e3b22012-08-19 12:30:45 -0400205 ret = -EFAULT;
206 if (copy_to_user(buf, &fanotify_event_metadata,
207 fanotify_event_metadata.event_len))
208 goto out_close_fd;
209
Jan Karaf0834412014-04-03 14:46:33 -0700210#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karad5078162014-04-03 14:46:36 -0700211 if (event->mask & FAN_ALL_PERM_EVENTS)
212 FANOTIFY_PE(event)->fd = fd;
Jan Karaf0834412014-04-03 14:46:33 -0700213#endif
Eric Parisb2d87902009-12-17 21:24:34 -0500214
Al Viro3587b1b2012-11-18 19:19:00 +0000215 if (fd != FAN_NOFD)
216 fd_install(fd, f);
Eric Paris7d131622010-12-07 15:27:57 -0500217 return fanotify_event_metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500218
Eric Parisb2d87902009-12-17 21:24:34 -0500219out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400220 if (fd != FAN_NOFD) {
221 put_unused_fd(fd);
222 fput(f);
223 }
Eric Parisb2d87902009-12-17 21:24:34 -0500224 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500225}
226
227/* intofiy userspace file descriptor functions */
228static unsigned int fanotify_poll(struct file *file, poll_table *wait)
229{
230 struct fsnotify_group *group = file->private_data;
231 int ret = 0;
232
233 poll_wait(file, &group->notification_waitq, wait);
234 mutex_lock(&group->notification_mutex);
235 if (!fsnotify_notify_queue_is_empty(group))
236 ret = POLLIN | POLLRDNORM;
237 mutex_unlock(&group->notification_mutex);
238
239 return ret;
240}
241
242static ssize_t fanotify_read(struct file *file, char __user *buf,
243 size_t count, loff_t *pos)
244{
245 struct fsnotify_group *group;
246 struct fsnotify_event *kevent;
247 char __user *start;
248 int ret;
249 DEFINE_WAIT(wait);
250
251 start = buf;
252 group = file->private_data;
253
254 pr_debug("%s: group=%p\n", __func__, group);
255
256 while (1) {
257 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
258
259 mutex_lock(&group->notification_mutex);
260 kevent = get_one_event(group, count);
261 mutex_unlock(&group->notification_mutex);
262
Jan Karad8aaab42014-04-03 14:46:35 -0700263 if (IS_ERR(kevent)) {
Eric Parisa1014f12009-12-17 21:24:26 -0500264 ret = PTR_ERR(kevent);
Jan Karad8aaab42014-04-03 14:46:35 -0700265 break;
266 }
267
268 if (!kevent) {
269 ret = -EAGAIN;
270 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500271 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700272
273 ret = -ERESTARTSYS;
274 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500275 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700276
277 if (start != buf)
278 break;
279 schedule();
Eric Parisa1014f12009-12-17 21:24:26 -0500280 continue;
281 }
282
Jan Karad8aaab42014-04-03 14:46:35 -0700283 ret = copy_event_to_user(group, kevent, buf);
284 /*
285 * Permission events get queued to wait for response. Other
286 * events can be destroyed now.
287 */
Jan Karad5078162014-04-03 14:46:36 -0700288 if (!(kevent->mask & FAN_ALL_PERM_EVENTS)) {
Jan Karad8aaab42014-04-03 14:46:35 -0700289 fsnotify_destroy_event(group, kevent);
Jan Karad5078162014-04-03 14:46:36 -0700290 if (ret < 0)
291 break;
292 } else {
293#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
294 if (ret < 0) {
295 FANOTIFY_PE(kevent)->response = FAN_DENY;
296 wake_up(&group->fanotify_data.access_waitq);
297 break;
298 }
299 spin_lock(&group->fanotify_data.access_lock);
300 list_add_tail(&kevent->list,
301 &group->fanotify_data.access_list);
302 spin_unlock(&group->fanotify_data.access_lock);
303#endif
304 }
Jan Karad8aaab42014-04-03 14:46:35 -0700305 buf += ret;
306 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500307 }
308
309 finish_wait(&group->notification_waitq, &wait);
310 if (start != buf && ret != -EFAULT)
311 ret = buf - start;
312 return ret;
313}
314
Eric Parisb2d87902009-12-17 21:24:34 -0500315static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
316{
317#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
318 struct fanotify_response response = { .fd = -1, .response = -1 };
319 struct fsnotify_group *group;
320 int ret;
321
322 group = file->private_data;
323
324 if (count > sizeof(response))
325 count = sizeof(response);
326
327 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
328
329 if (copy_from_user(&response, buf, count))
330 return -EFAULT;
331
332 ret = process_access_response(group, &response);
333 if (ret < 0)
334 count = ret;
335
336 return count;
337#else
338 return -EINVAL;
339#endif
340}
341
Eric Paris52c923d2009-12-17 21:24:26 -0500342static int fanotify_release(struct inode *ignored, struct file *file)
343{
344 struct fsnotify_group *group = file->private_data;
Eric Paris52c923d2009-12-17 21:24:26 -0500345
Eric Paris2eebf582010-08-18 12:25:50 -0400346#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karaf0834412014-04-03 14:46:33 -0700347 struct fanotify_perm_event_info *event, *next;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400348
Jan Kara9573f792014-04-03 14:46:34 -0700349 spin_lock(&group->fanotify_data.access_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400350
Lino Sanfilippo09e5f142010-11-19 10:58:07 +0100351 atomic_inc(&group->fanotify_data.bypass_perm);
Eric Paris2eebf582010-08-18 12:25:50 -0400352
Jan Karaf0834412014-04-03 14:46:33 -0700353 list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
354 fae.fse.list) {
355 pr_debug("%s: found group=%p event=%p\n", __func__, group,
356 event);
Eric Paris2eebf582010-08-18 12:25:50 -0400357
Jan Karaf0834412014-04-03 14:46:33 -0700358 list_del_init(&event->fae.fse.list);
359 event->response = FAN_ALLOW;
Eric Paris2eebf582010-08-18 12:25:50 -0400360 }
Jan Kara9573f792014-04-03 14:46:34 -0700361 spin_unlock(&group->fanotify_data.access_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400362
363 wake_up(&group->fanotify_data.access_waitq);
364#endif
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400365
Eric Paris52c923d2009-12-17 21:24:26 -0500366 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200367 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500368
369 return 0;
370}
371
Eric Parisa1014f12009-12-17 21:24:26 -0500372static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
373{
374 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800375 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500376 void __user *p;
377 int ret = -ENOTTY;
378 size_t send_len = 0;
379
380 group = file->private_data;
381
382 p = (void __user *) arg;
383
384 switch (cmd) {
385 case FIONREAD:
386 mutex_lock(&group->notification_mutex);
Jan Kara7053aee2014-01-21 15:48:14 -0800387 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500388 send_len += FAN_EVENT_METADATA_LEN;
389 mutex_unlock(&group->notification_mutex);
390 ret = put_user(send_len, (int __user *) p);
391 break;
392 }
393
394 return ret;
395}
396
Eric Paris52c923d2009-12-17 21:24:26 -0500397static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800398 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500399 .poll = fanotify_poll,
400 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500401 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500402 .fasync = NULL,
403 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500404 .unlocked_ioctl = fanotify_ioctl,
405 .compat_ioctl = fanotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200406 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500407};
408
Eric Paris2a3edf82009-12-17 21:24:26 -0500409static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
410{
411 kmem_cache_free(fanotify_mark_cache, fsn_mark);
412}
413
414static int fanotify_find_path(int dfd, const char __user *filename,
415 struct path *path, unsigned int flags)
416{
417 int ret;
418
419 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
420 dfd, filename, flags);
421
422 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400423 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500424
425 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400426 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500427 goto out;
428
429 ret = -ENOTDIR;
430 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500431 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400432 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500433 goto out;
434 }
435
Al Viro2903ff02012-08-28 12:52:22 -0400436 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500437 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400438 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500439 } else {
440 unsigned int lookup_flags = 0;
441
442 if (!(flags & FAN_MARK_DONT_FOLLOW))
443 lookup_flags |= LOOKUP_FOLLOW;
444 if (flags & FAN_MARK_ONLYDIR)
445 lookup_flags |= LOOKUP_DIRECTORY;
446
447 ret = user_path_at(dfd, filename, lookup_flags, path);
448 if (ret)
449 goto out;
450 }
451
452 /* you can only watch an inode if you have read permissions on it */
453 ret = inode_permission(path->dentry->d_inode, MAY_READ);
454 if (ret)
455 path_put(path);
456out:
457 return ret;
458}
459
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500460static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
461 __u32 mask,
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200462 unsigned int flags,
463 int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500464{
465 __u32 oldmask;
466
467 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500468 if (!(flags & FAN_MARK_IGNORED_MASK)) {
469 oldmask = fsn_mark->mask;
470 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
471 } else {
472 oldmask = fsn_mark->ignored_mask;
473 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
474 }
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500475 spin_unlock(&fsn_mark->lock);
476
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200477 *destroy = !(oldmask & ~mask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500478
479 return mask & oldmask;
480}
481
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500482static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500483 struct vfsmount *mnt, __u32 mask,
484 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500485{
486 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500487 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200488 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500489
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700490 mutex_lock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500491 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700492 if (!fsn_mark) {
493 mutex_unlock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500494 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700495 }
Eric Paris88826272009-12-17 21:24:28 -0500496
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200497 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
498 &destroy_mark);
499 if (destroy_mark)
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700500 fsnotify_destroy_mark_locked(fsn_mark, group);
501 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200502
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500503 fsnotify_put_mark(fsn_mark);
Al Viroc63181e2011-11-25 02:35:16 -0500504 if (removed & real_mount(mnt)->mnt_fsnotify_mask)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500505 fsnotify_recalc_vfsmount_mask(mnt);
Eric Paris88826272009-12-17 21:24:28 -0500506
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500507 return 0;
508}
509
510static int fanotify_remove_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500511 struct inode *inode, __u32 mask,
512 unsigned int flags)
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500513{
514 struct fsnotify_mark *fsn_mark = NULL;
515 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200516 int destroy_mark;
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500517
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700518 mutex_lock(&group->mark_mutex);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500519 fsn_mark = fsnotify_find_inode_mark(group, inode);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700520 if (!fsn_mark) {
521 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500522 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700523 }
Eric Paris88826272009-12-17 21:24:28 -0500524
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200525 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
526 &destroy_mark);
527 if (destroy_mark)
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700528 fsnotify_destroy_mark_locked(fsn_mark, group);
529 mutex_unlock(&group->mark_mutex);
530
Eric Paris5444e292009-12-17 21:24:27 -0500531 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500532 fsnotify_put_mark(fsn_mark);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500533 if (removed & inode->i_fsnotify_mask)
534 fsnotify_recalc_inode_mask(inode);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500535
Eric Paris2a3edf82009-12-17 21:24:26 -0500536 return 0;
537}
538
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500539static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
540 __u32 mask,
541 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500542{
Eric Paris192ca4d2010-10-28 17:21:59 -0400543 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500544
545 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500546 if (!(flags & FAN_MARK_IGNORED_MASK)) {
547 oldmask = fsn_mark->mask;
548 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
549 } else {
Eric Paris192ca4d2010-10-28 17:21:59 -0400550 __u32 tmask = fsn_mark->ignored_mask | mask;
551 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
Eric Parisc9778a92009-12-17 21:24:33 -0500552 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
553 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500554 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400555
556 if (!(flags & FAN_MARK_ONDIR)) {
557 __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR;
558 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
559 }
560
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500561 spin_unlock(&fsn_mark->lock);
562
563 return mask & ~oldmask;
564}
565
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700566static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
567 struct inode *inode,
568 struct vfsmount *mnt)
569{
570 struct fsnotify_mark *mark;
571 int ret;
572
573 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
574 return ERR_PTR(-ENOSPC);
575
576 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
577 if (!mark)
578 return ERR_PTR(-ENOMEM);
579
580 fsnotify_init_mark(mark, fanotify_free_mark);
581 ret = fsnotify_add_mark_locked(mark, group, inode, mnt, 0);
582 if (ret) {
583 fsnotify_put_mark(mark);
584 return ERR_PTR(ret);
585 }
586
587 return mark;
588}
589
590
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500591static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500592 struct vfsmount *mnt, __u32 mask,
593 unsigned int flags)
Eric Paris2a3edf82009-12-17 21:24:26 -0500594{
595 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500596 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500597
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700598 mutex_lock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500599 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
600 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700601 fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
602 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700603 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700604 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700605 }
Eric Paris88826272009-12-17 21:24:28 -0500606 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500607 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700608 mutex_unlock(&group->mark_mutex);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100609
Al Viroc63181e2011-11-25 02:35:16 -0500610 if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
Eric Paris43709a22010-07-28 10:18:39 -0400611 fsnotify_recalc_vfsmount_mask(mnt);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700612
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100613 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700614 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500615}
616
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500617static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500618 struct inode *inode, __u32 mask,
619 unsigned int flags)
Eric Paris88826272009-12-17 21:24:28 -0500620{
621 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500622 __u32 added;
Eric Paris88826272009-12-17 21:24:28 -0500623
624 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500625
Eric Paris5322a592010-10-28 17:21:57 -0400626 /*
627 * If some other task has this inode open for write we should not add
628 * an ignored mark, unless that ignored mark is supposed to survive
629 * modification changes anyway.
630 */
631 if ((flags & FAN_MARK_IGNORED_MASK) &&
632 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
633 (atomic_read(&inode->i_writecount) > 0))
634 return 0;
635
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700636 mutex_lock(&group->mark_mutex);
Eric Paris5444e292009-12-17 21:24:27 -0500637 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500638 if (!fsn_mark) {
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700639 fsn_mark = fanotify_add_new_mark(group, inode, NULL);
640 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700641 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700642 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700643 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500644 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500645 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700646 mutex_unlock(&group->mark_mutex);
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100647
Eric Paris43709a22010-07-28 10:18:39 -0400648 if (added & ~inode->i_fsnotify_mask)
649 fsnotify_recalc_inode_mask(inode);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700650
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100651 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700652 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500653}
Eric Paris2a3edf82009-12-17 21:24:26 -0500654
Eric Paris52c923d2009-12-17 21:24:26 -0500655/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400656SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500657{
Eric Paris52c923d2009-12-17 21:24:26 -0500658 struct fsnotify_group *group;
659 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400660 struct user_struct *user;
Jan Karaff57cd52014-02-21 19:14:11 +0100661 struct fanotify_event_info *oevent;
Eric Paris52c923d2009-12-17 21:24:26 -0500662
Eric Paris08ae8932010-05-27 09:41:40 -0400663 pr_debug("%s: flags=%d event_f_flags=%d\n",
664 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500665
Eric Paris52c923d2009-12-17 21:24:26 -0500666 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200667 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500668
669 if (flags & ~FAN_ALL_INIT_FLAGS)
670 return -EINVAL;
671
Eric Paris4afeff82010-10-28 17:21:58 -0400672 user = get_current_user();
673 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
674 free_uid(user);
675 return -EMFILE;
676 }
677
Eric Parisb2d87902009-12-17 21:24:34 -0500678 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500679 if (flags & FAN_CLOEXEC)
680 f_flags |= O_CLOEXEC;
681 if (flags & FAN_NONBLOCK)
682 f_flags |= O_NONBLOCK;
683
684 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
685 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500686 if (IS_ERR(group)) {
687 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500688 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500689 }
Eric Paris52c923d2009-12-17 21:24:26 -0500690
Eric Paris4afeff82010-10-28 17:21:58 -0400691 group->fanotify_data.user = user;
692 atomic_inc(&user->fanotify_listeners);
693
Jan Karaf0834412014-04-03 14:46:33 -0700694 oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
Jan Karaff57cd52014-02-21 19:14:11 +0100695 if (unlikely(!oevent)) {
696 fd = -ENOMEM;
697 goto out_destroy_group;
698 }
699 group->overflow_event = &oevent->fse;
Jan Karaff57cd52014-02-21 19:14:11 +0100700
Eric Paris80af2582010-07-28 10:18:37 -0400701 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500702#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Kara9573f792014-04-03 14:46:34 -0700703 spin_lock_init(&group->fanotify_data.access_lock);
Eric Paris9e66e422009-12-17 21:24:34 -0500704 init_waitqueue_head(&group->fanotify_data.access_waitq);
705 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Lino Sanfilippo09e5f142010-11-19 10:58:07 +0100706 atomic_set(&group->fanotify_data.bypass_perm, 0);
Eric Paris9e66e422009-12-17 21:24:34 -0500707#endif
Eric Paris4231a232010-10-28 17:21:56 -0400708 switch (flags & FAN_ALL_CLASS_BITS) {
709 case FAN_CLASS_NOTIF:
710 group->priority = FS_PRIO_0;
711 break;
712 case FAN_CLASS_CONTENT:
713 group->priority = FS_PRIO_1;
714 break;
715 case FAN_CLASS_PRE_CONTENT:
716 group->priority = FS_PRIO_2;
717 break;
718 default:
719 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200720 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -0400721 }
Eric Pariscb2d4292009-12-17 21:24:34 -0500722
Eric Paris5dd03f52010-10-28 17:21:57 -0400723 if (flags & FAN_UNLIMITED_QUEUE) {
724 fd = -EPERM;
725 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200726 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -0400727 group->max_events = UINT_MAX;
728 } else {
729 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
730 }
Eric Paris2529a0d2010-10-28 17:21:57 -0400731
Eric Parisac7e22d2010-10-28 17:21:58 -0400732 if (flags & FAN_UNLIMITED_MARKS) {
733 fd = -EPERM;
734 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200735 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -0400736 group->fanotify_data.max_marks = UINT_MAX;
737 } else {
738 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
739 }
Eric Parise7099d82010-10-28 17:21:57 -0400740
Eric Paris52c923d2009-12-17 21:24:26 -0500741 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
742 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200743 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -0500744
745 return fd;
746
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200747out_destroy_group:
748 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500749 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500750}
Eric Parisbbaa4162009-12-17 21:24:26 -0500751
Al Viro4a0fd5b2013-01-21 15:16:58 -0500752SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
753 __u64, mask, int, dfd,
754 const char __user *, pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500755{
Eric Paris0ff21db2009-12-17 21:24:29 -0500756 struct inode *inode = NULL;
757 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -0500758 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -0400759 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -0500760 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400761 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500762
763 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
764 __func__, fanotify_fd, flags, dfd, pathname, mask);
765
766 /* we only use the lower 32 bits as of right now. */
767 if (mask & ((__u64)0xffffffff << 32))
768 return -EINVAL;
769
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500770 if (flags & ~FAN_ALL_MARK_FLAGS)
771 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500772 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100773 case FAN_MARK_ADD: /* fallthrough */
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500774 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +0100775 if (!mask)
776 return -EINVAL;
Eric Paris4d926042009-12-17 21:24:34 -0500777 case FAN_MARK_FLUSH:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500778 break;
779 default:
780 return -EINVAL;
781 }
Eric Paris8fcd6522010-10-28 17:21:59 -0400782
783 if (mask & FAN_ONDIR) {
784 flags |= FAN_MARK_ONDIR;
785 mask &= ~FAN_ONDIR;
786 }
787
Eric Parisb2d87902009-12-17 21:24:34 -0500788#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
789 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
790#else
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -0500791 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
Eric Parisb2d87902009-12-17 21:24:34 -0500792#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500793 return -EINVAL;
794
Al Viro2903ff02012-08-28 12:52:22 -0400795 f = fdget(fanotify_fd);
796 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -0500797 return -EBADF;
798
799 /* verify that this is indeed an fanotify instance */
800 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400801 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -0500802 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -0400803 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -0400804
805 /*
806 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
807 * allowed to set permissions events.
808 */
809 ret = -EINVAL;
810 if (mask & FAN_ALL_PERM_EVENTS &&
811 group->priority == FS_PRIO_0)
812 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -0500813
814 ret = fanotify_find_path(dfd, pathname, &path, flags);
815 if (ret)
816 goto fput_and_out;
817
818 /* inode held in place by reference to path; group by fget on fd */
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500819 if (!(flags & FAN_MARK_MOUNT))
Eric Paris0ff21db2009-12-17 21:24:29 -0500820 inode = path.dentry->d_inode;
821 else
822 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -0500823
824 /* create/update an inode mark */
Eric Paris4d926042009-12-17 21:24:34 -0500825 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500826 case FAN_MARK_ADD:
Andreas Gruenbachereac8e9e2009-12-17 21:24:29 -0500827 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500828 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
Eric Paris0ff21db2009-12-17 21:24:29 -0500829 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500830 ret = fanotify_add_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500831 break;
832 case FAN_MARK_REMOVE:
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500833 if (flags & FAN_MARK_MOUNT)
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500834 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -0500835 else
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500836 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500837 break;
Eric Paris4d926042009-12-17 21:24:34 -0500838 case FAN_MARK_FLUSH:
839 if (flags & FAN_MARK_MOUNT)
840 fsnotify_clear_vfsmount_marks_by_group(group);
841 else
842 fsnotify_clear_inode_marks_by_group(group);
Eric Paris4d926042009-12-17 21:24:34 -0500843 break;
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500844 default:
845 ret = -EINVAL;
846 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500847
848 path_put(&path);
849fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400850 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500851 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500852}
Eric Paris2a3edf82009-12-17 21:24:26 -0500853
Al Viro91c2e0b2013-03-05 20:10:59 -0500854#ifdef CONFIG_COMPAT
855COMPAT_SYSCALL_DEFINE6(fanotify_mark,
856 int, fanotify_fd, unsigned int, flags,
857 __u32, mask0, __u32, mask1, int, dfd,
858 const char __user *, pathname)
859{
860 return sys_fanotify_mark(fanotify_fd, flags,
861#ifdef __BIG_ENDIAN
Al Viro91c2e0b2013-03-05 20:10:59 -0500862 ((__u64)mask0 << 32) | mask1,
Heiko Carstens592f6b82014-01-27 17:07:19 -0800863#else
864 ((__u64)mask1 << 32) | mask0,
Al Viro91c2e0b2013-03-05 20:10:59 -0500865#endif
866 dfd, pathname);
867}
868#endif
869
Eric Paris2a3edf82009-12-17 21:24:26 -0500870/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100871 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -0500872 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
873 * must result in panic().
874 */
875static int __init fanotify_user_setup(void)
876{
877 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
Jan Kara7053aee2014-01-21 15:48:14 -0800878 fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
Jan Karaf0834412014-04-03 14:46:33 -0700879#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
880 fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
881 SLAB_PANIC);
882#endif
Eric Paris2a3edf82009-12-17 21:24:26 -0500883
884 return 0;
885}
886device_initcall(fanotify_user_setup);