2009-12-18 10:24:29 +08:00
|
|
|
#include <linux/fanotify.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
#include <linux/fcntl.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/file.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
#include <linux/fs.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/anon_inodes.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
#include <linux/fsnotify_backend.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/init.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/mount.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/namei.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/poll.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/syscalls.h>
|
2010-05-19 23:36:28 +08:00
|
|
|
#include <linux/slab.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/types.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
#include <linux/uaccess.h>
|
2013-03-06 09:10:59 +08:00
|
|
|
#include <linux/compat.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
#include <asm/ioctls.h>
|
2009-12-18 10:24:25 +08:00
|
|
|
|
2011-11-25 15:35:16 +08:00
|
|
|
#include "../../mount.h"
|
2012-12-18 08:05:12 +08:00
|
|
|
#include "../fdinfo.h"
|
2014-01-22 07:48:14 +08:00
|
|
|
#include "fanotify.h"
|
2011-11-25 15:35:16 +08:00
|
|
|
|
2010-10-29 05:21:57 +08:00
|
|
|
#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
|
2010-10-29 05:21:57 +08:00
|
|
|
#define FANOTIFY_DEFAULT_MAX_MARKS 8192
|
2010-10-29 05:21:58 +08:00
|
|
|
#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
|
2010-10-29 05:21:57 +08:00
|
|
|
|
fanotify: check file flags passed in fanotify_init
Without this patch fanotify_init does not validate the value passed in
event_f_flags.
When a fanotify event is read from the fanotify file descriptor a new
file descriptor is created where file.f_flags = event_f_flags.
Internal and external open flags are stored together in field f_flags of
struct file. Hence, an application might create file descriptors with
internal flags like FMODE_EXEC, FMODE_NOCMTIME set.
Jan Kara and Eric Paris both aggreed that this is a bug and the value of
event_f_flags should be checked:
https://lkml.org/lkml/2014/4/29/522
https://lkml.org/lkml/2014/4/29/539
This updated patch version considers the comments by Michael Kerrisk in
https://lkml.org/lkml/2014/5/4/10
With the patch the value of event_f_flags is checked.
When specifying an invalid value error EINVAL is returned.
Internal flags are disallowed.
File creation flags are disallowed:
O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TRUNC, and O_TTY_INIT.
Flags which do not make sense with fanotify are disallowed:
__O_TMPFILE, O_PATH, FASYNC, and O_DIRECT.
This leaves us with the following allowed values:
O_RDONLY, O_WRONLY, O_RDWR are basic functionality. The are stored in the
bits given by O_ACCMODE.
O_APPEND is working as expected. The value might be useful in a logging
application which appends the current status each time the log is opened.
O_LARGEFILE is needed for files exceeding 4GB on 32bit systems.
O_NONBLOCK may be useful when monitoring slow devices like tapes.
O_NDELAY is equal to O_NONBLOCK except for platform parisc.
To avoid code breaking on parisc either both flags should be
allowed or none. The patch allows both.
__O_SYNC and O_DSYNC may be used to avoid data loss on power disruption.
O_NOATIME may be useful to reduce disk activity.
O_CLOEXEC may be useful, if separate processes shall be used to scan files.
Once this patch is accepted, the fanotify_init.2 manpage has to be updated.
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Valdis Kletnieks <Valdis.Kletnieks@vt.edu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-06-05 07:05:44 +08:00
|
|
|
/*
|
|
|
|
* All flags that may be specified in parameter event_f_flags of fanotify_init.
|
|
|
|
*
|
|
|
|
* Internal and external open flags are stored together in field f_flags of
|
|
|
|
* struct file. Only external open flags shall be allowed in event_f_flags.
|
|
|
|
* Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
|
|
|
|
* excluded.
|
|
|
|
*/
|
|
|
|
#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
|
|
|
|
O_ACCMODE | O_APPEND | O_NONBLOCK | \
|
|
|
|
__O_SYNC | O_DSYNC | O_CLOEXEC | \
|
|
|
|
O_LARGEFILE | O_NOATIME )
|
|
|
|
|
2009-12-18 10:24:29 +08:00
|
|
|
extern const struct fsnotify_ops fanotify_fsnotify_ops;
|
2009-12-18 10:24:25 +08:00
|
|
|
|
2016-12-22 01:06:12 +08:00
|
|
|
struct kmem_cache *fanotify_mark_cache __read_mostly;
|
2014-01-22 07:48:14 +08:00
|
|
|
struct kmem_cache *fanotify_event_cachep __read_mostly;
|
2014-04-04 05:46:33 +08:00
|
|
|
struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
/*
|
|
|
|
* Get an fsnotify notification event if one exists and is small
|
|
|
|
* enough to fit in "count". Return an error pointer if the count
|
|
|
|
* is not large enough.
|
|
|
|
*
|
2016-10-08 07:56:52 +08:00
|
|
|
* Called with the group->notification_lock held.
|
2009-12-18 10:24:26 +08:00
|
|
|
*/
|
|
|
|
static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
|
|
|
|
size_t count)
|
|
|
|
{
|
2016-10-08 07:57:01 +08:00
|
|
|
assert_spin_locked(&group->notification_lock);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
|
|
|
|
|
|
|
|
if (fsnotify_notify_queue_is_empty(group))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (FAN_EVENT_METADATA_LEN > count)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2016-10-08 07:56:52 +08:00
|
|
|
/* held the notification_lock the whole time, so this is the
|
2009-12-18 10:24:26 +08:00
|
|
|
* same event we peeked above */
|
2014-08-07 07:03:26 +08:00
|
|
|
return fsnotify_remove_first_event(group);
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
|
|
|
|
2012-08-20 00:30:45 +08:00
|
|
|
static int create_fd(struct fsnotify_group *group,
|
2014-01-22 07:48:14 +08:00
|
|
|
struct fanotify_event_info *event,
|
|
|
|
struct file **file)
|
2009-12-18 10:24:26 +08:00
|
|
|
{
|
|
|
|
int client_fd;
|
|
|
|
struct file *new_file;
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
fanotify: enable close-on-exec on events' fd when requested in fanotify_init()
According to commit 80af258867648 ("fanotify: groups can specify their
f_flags for new fd"), file descriptors created as part of file access
notification events inherit flags from the event_f_flags argument passed
to syscall fanotify_init(2)[1].
Unfortunately O_CLOEXEC is currently silently ignored.
Indeed, event_f_flags are only given to dentry_open(), which only seems to
care about O_ACCMODE and O_PATH in do_dentry_open(), O_DIRECT in
open_check_o_direct() and O_LARGEFILE in generic_file_open().
It's a pity, since, according to some lookup on various search engines and
http://codesearch.debian.net/, there's already some userspace code which
use O_CLOEXEC:
- in systemd's readahead[2]:
fanotify_fd = fanotify_init(FAN_CLOEXEC|FAN_NONBLOCK, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME);
- in clsync[3]:
#define FANOTIFY_EVFLAGS (O_LARGEFILE|O_RDONLY|O_CLOEXEC)
int fanotify_d = fanotify_init(FANOTIFY_FLAGS, FANOTIFY_EVFLAGS);
- in examples [4] from "Filesystem monitoring in the Linux
kernel" article[5] by Aleksander Morgado:
if ((fanotify_fd = fanotify_init (FAN_CLOEXEC,
O_RDONLY | O_CLOEXEC | O_LARGEFILE)) < 0)
Additionally, since commit 48149e9d3a7e ("fanotify: check file flags
passed in fanotify_init"). having O_CLOEXEC as part of fanotify_init()
second argument is expressly allowed.
So it seems expected to set close-on-exec flag on the file descriptors if
userspace is allowed to request it with O_CLOEXEC.
But Andrew Morton raised[6] the concern that enabling now close-on-exec
might break existing applications which ask for O_CLOEXEC but expect the
file descriptor to be inherited across exec().
In the other hand, as reported by Mihai Dontu[7] close-on-exec on the file
descriptor returned as part of file access notify can break applications
due to deadlock. So close-on-exec is needed for most applications.
More, applications asking for close-on-exec are likely expecting it to be
enabled, relying on O_CLOEXEC being effective. If not, it might weaken
their security, as noted by Jan Kara[8].
So this patch replaces call to macro get_unused_fd() by a call to function
get_unused_fd_flags() with event_f_flags value as argument. This way
O_CLOEXEC flag in the second argument of fanotify_init(2) syscall is
interpreted and close-on-exec get enabled when requested.
[1] http://man7.org/linux/man-pages/man2/fanotify_init.2.html
[2] http://cgit.freedesktop.org/systemd/systemd/tree/src/readahead/readahead-collect.c?id=v208#n294
[3] https://github.com/xaionaro/clsync/blob/v0.2.1/sync.c#L1631
https://github.com/xaionaro/clsync/blob/v0.2.1/configuration.h#L38
[4] http://www.lanedo.com/~aleksander/fanotify/fanotify-example.c
[5] http://www.lanedo.com/2013/filesystem-monitoring-linux-kernel/
[6] http://lkml.kernel.org/r/20141001153621.65e9258e65a6167bf2e4cb50@linux-foundation.org
[7] http://lkml.kernel.org/r/20141002095046.3715eb69@mdontu-l
[8] http://lkml.kernel.org/r/20141002104410.GB19748@quack.suse.cz
Link: http://lkml.kernel.org/r/cover.1411562410.git.ydroneaud@opteya.com
Signed-off-by: Yann Droneaud <ydroneaud@opteya.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Mihai Don\u021bu <mihai.dontu@gmail.com>
Cc: Pádraig Brady <P@draigBrady.com>
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Jan Kara <jack@suse.cz>
Cc: Valdis Kletnieks <Valdis.Kletnieks@vt.edu>
Cc: Michael Kerrisk-manpages <mtk.manpages@gmail.com>
Cc: Lino Sanfilippo <LinoSanfilippo@gmx.de>
Cc: Richard Guy Briggs <rgb@redhat.com>
Cc: Eric Paris <eparis@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-10-10 06:24:40 +08:00
|
|
|
client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
|
2009-12-18 10:24:26 +08:00
|
|
|
if (client_fd < 0)
|
|
|
|
return client_fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we need a new file handle for the userspace program so it can read even if it was
|
|
|
|
* originally opened O_WRONLY.
|
|
|
|
*/
|
|
|
|
/* it's possible this event was an overflow event. in that case dentry and mnt
|
|
|
|
* are NULL; That's fine, just don't call dentry open */
|
2012-06-27 01:58:53 +08:00
|
|
|
if (event->path.dentry && event->path.mnt)
|
|
|
|
new_file = dentry_open(&event->path,
|
2010-07-28 22:18:37 +08:00
|
|
|
group->fanotify_data.f_flags | FMODE_NONOTIFY,
|
2009-12-18 10:24:26 +08:00
|
|
|
current_cred());
|
|
|
|
else
|
|
|
|
new_file = ERR_PTR(-EOVERFLOW);
|
|
|
|
if (IS_ERR(new_file)) {
|
|
|
|
/*
|
|
|
|
* we still send an event even if we can't open the file. this
|
|
|
|
* can happen when say tasks are gone and we try to open their
|
|
|
|
* /proc files or we try to open a WRONLY file like in sysfs
|
|
|
|
* we just send the errno to userspace since there isn't much
|
|
|
|
* else we can do.
|
|
|
|
*/
|
|
|
|
put_unused_fd(client_fd);
|
|
|
|
client_fd = PTR_ERR(new_file);
|
|
|
|
} else {
|
2012-08-20 00:30:45 +08:00
|
|
|
*file = new_file;
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
return client_fd;
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
|
|
|
|
2010-11-09 07:08:14 +08:00
|
|
|
static int fill_event_metadata(struct fsnotify_group *group,
|
2014-01-22 07:48:14 +08:00
|
|
|
struct fanotify_event_metadata *metadata,
|
|
|
|
struct fsnotify_event *fsn_event,
|
|
|
|
struct file **file)
|
2009-12-18 10:24:26 +08:00
|
|
|
{
|
2010-11-25 01:26:04 +08:00
|
|
|
int ret = 0;
|
2014-01-22 07:48:14 +08:00
|
|
|
struct fanotify_event_info *event;
|
2010-11-25 01:26:04 +08:00
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
|
2014-01-22 07:48:14 +08:00
|
|
|
group, metadata, fsn_event);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2012-08-20 00:30:45 +08:00
|
|
|
*file = NULL;
|
2014-01-22 07:48:14 +08:00
|
|
|
event = container_of(fsn_event, struct fanotify_event_info, fse);
|
2009-12-18 10:24:26 +08:00
|
|
|
metadata->event_len = FAN_EVENT_METADATA_LEN;
|
2010-12-08 04:27:57 +08:00
|
|
|
metadata->metadata_len = FAN_EVENT_METADATA_LEN;
|
2009-12-18 10:24:26 +08:00
|
|
|
metadata->vers = FANOTIFY_METADATA_VERSION;
|
2013-07-09 06:59:40 +08:00
|
|
|
metadata->reserved = 0;
|
2014-01-22 07:48:14 +08:00
|
|
|
metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
|
2009-12-18 10:24:27 +08:00
|
|
|
metadata->pid = pid_vnr(event->tgid);
|
2014-01-22 07:48:14 +08:00
|
|
|
if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
|
2010-11-25 01:26:04 +08:00
|
|
|
metadata->fd = FAN_NOFD;
|
|
|
|
else {
|
2012-08-20 00:30:45 +08:00
|
|
|
metadata->fd = create_fd(group, event, file);
|
2010-11-25 01:26:04 +08:00
|
|
|
if (metadata->fd < 0)
|
|
|
|
ret = metadata->fd;
|
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2010-11-25 01:26:04 +08:00
|
|
|
return ret;
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:34 +08:00
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
2014-04-04 05:46:33 +08:00
|
|
|
static struct fanotify_perm_event_info *dequeue_event(
|
|
|
|
struct fsnotify_group *group, int fd)
|
2009-12-18 10:24:34 +08:00
|
|
|
{
|
2014-04-04 05:46:33 +08:00
|
|
|
struct fanotify_perm_event_info *event, *return_e = NULL;
|
2009-12-18 10:24:34 +08:00
|
|
|
|
2016-10-08 07:56:55 +08:00
|
|
|
spin_lock(&group->notification_lock);
|
2014-04-04 05:46:33 +08:00
|
|
|
list_for_each_entry(event, &group->fanotify_data.access_list,
|
|
|
|
fae.fse.list) {
|
|
|
|
if (event->fd != fd)
|
2009-12-18 10:24:34 +08:00
|
|
|
continue;
|
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
list_del_init(&event->fae.fse.list);
|
|
|
|
return_e = event;
|
2009-12-18 10:24:34 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-08 07:56:55 +08:00
|
|
|
spin_unlock(&group->notification_lock);
|
2009-12-18 10:24:34 +08:00
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
pr_debug("%s: found return_re=%p\n", __func__, return_e);
|
2009-12-18 10:24:34 +08:00
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
return return_e;
|
2009-12-18 10:24:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int process_access_response(struct fsnotify_group *group,
|
|
|
|
struct fanotify_response *response_struct)
|
|
|
|
{
|
2014-04-04 05:46:33 +08:00
|
|
|
struct fanotify_perm_event_info *event;
|
|
|
|
int fd = response_struct->fd;
|
|
|
|
int response = response_struct->response;
|
2009-12-18 10:24:34 +08:00
|
|
|
|
|
|
|
pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
|
|
|
|
fd, response);
|
|
|
|
/*
|
|
|
|
* make sure the response is valid, if invalid we do nothing and either
|
2011-03-31 09:57:33 +08:00
|
|
|
* userspace can send a valid response or we will clean it up after the
|
2009-12-18 10:24:34 +08:00
|
|
|
* timeout
|
|
|
|
*/
|
|
|
|
switch (response) {
|
|
|
|
case FAN_ALLOW:
|
|
|
|
case FAN_DENY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
event = dequeue_event(group, fd);
|
|
|
|
if (!event)
|
2009-12-18 10:24:34 +08:00
|
|
|
return -ENOENT;
|
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
event->response = response;
|
2009-12-18 10:24:34 +08:00
|
|
|
wake_up(&group->fanotify_data.access_waitq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
static ssize_t copy_event_to_user(struct fsnotify_group *group,
|
|
|
|
struct fsnotify_event *event,
|
|
|
|
char __user *buf)
|
|
|
|
{
|
|
|
|
struct fanotify_event_metadata fanotify_event_metadata;
|
2012-08-20 00:30:45 +08:00
|
|
|
struct file *f;
|
2009-12-18 10:24:34 +08:00
|
|
|
int fd, ret;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
|
|
|
|
|
2012-08-20 00:30:45 +08:00
|
|
|
ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
|
2010-11-09 07:08:14 +08:00
|
|
|
if (ret < 0)
|
2014-04-04 05:46:36 +08:00
|
|
|
return ret;
|
2009-12-18 10:24:34 +08:00
|
|
|
|
2010-11-25 01:26:04 +08:00
|
|
|
fd = fanotify_event_metadata.fd;
|
2009-12-18 10:24:34 +08:00
|
|
|
ret = -EFAULT;
|
2010-12-08 04:27:57 +08:00
|
|
|
if (copy_to_user(buf, &fanotify_event_metadata,
|
|
|
|
fanotify_event_metadata.event_len))
|
2012-08-20 00:30:45 +08:00
|
|
|
goto out_close_fd;
|
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
2014-04-04 05:46:36 +08:00
|
|
|
if (event->mask & FAN_ALL_PERM_EVENTS)
|
|
|
|
FANOTIFY_PE(event)->fd = fd;
|
2014-04-04 05:46:33 +08:00
|
|
|
#endif
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2012-11-19 03:19:00 +08:00
|
|
|
if (fd != FAN_NOFD)
|
|
|
|
fd_install(fd, f);
|
2010-12-08 04:27:57 +08:00
|
|
|
return fanotify_event_metadata.event_len;
|
2009-12-18 10:24:34 +08:00
|
|
|
|
|
|
|
out_close_fd:
|
2012-08-20 00:30:45 +08:00
|
|
|
if (fd != FAN_NOFD) {
|
|
|
|
put_unused_fd(fd);
|
|
|
|
fput(f);
|
|
|
|
}
|
2009-12-18 10:24:34 +08:00
|
|
|
return ret;
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* intofiy userspace file descriptor functions */
|
|
|
|
static unsigned int fanotify_poll(struct file *file, poll_table *wait)
|
|
|
|
{
|
|
|
|
struct fsnotify_group *group = file->private_data;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
poll_wait(file, &group->notification_waitq, wait);
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_lock(&group->notification_lock);
|
2009-12-18 10:24:26 +08:00
|
|
|
if (!fsnotify_notify_queue_is_empty(group))
|
|
|
|
ret = POLLIN | POLLRDNORM;
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_unlock(&group->notification_lock);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fanotify_read(struct file *file, char __user *buf,
|
|
|
|
size_t count, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct fsnotify_group *group;
|
|
|
|
struct fsnotify_event *kevent;
|
|
|
|
char __user *start;
|
|
|
|
int ret;
|
2014-12-16 23:28:38 +08:00
|
|
|
DEFINE_WAIT_FUNC(wait, woken_wake_function);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
start = buf;
|
|
|
|
group = file->private_data;
|
|
|
|
|
|
|
|
pr_debug("%s: group=%p\n", __func__, group);
|
|
|
|
|
2014-12-16 23:28:38 +08:00
|
|
|
add_wait_queue(&group->notification_waitq, &wait);
|
2009-12-18 10:24:26 +08:00
|
|
|
while (1) {
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_lock(&group->notification_lock);
|
2009-12-18 10:24:26 +08:00
|
|
|
kevent = get_one_event(group, count);
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_unlock(&group->notification_lock);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2014-04-04 05:46:35 +08:00
|
|
|
if (IS_ERR(kevent)) {
|
2009-12-18 10:24:26 +08:00
|
|
|
ret = PTR_ERR(kevent);
|
2014-04-04 05:46:35 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!kevent) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
if (file->f_flags & O_NONBLOCK)
|
2009-12-18 10:24:26 +08:00
|
|
|
break;
|
2014-04-04 05:46:35 +08:00
|
|
|
|
|
|
|
ret = -ERESTARTSYS;
|
|
|
|
if (signal_pending(current))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (start != buf)
|
2009-12-18 10:24:26 +08:00
|
|
|
break;
|
2014-12-16 23:28:38 +08:00
|
|
|
|
|
|
|
wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
|
2009-12-18 10:24:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-04 05:46:35 +08:00
|
|
|
ret = copy_event_to_user(group, kevent, buf);
|
2017-04-25 19:29:35 +08:00
|
|
|
if (unlikely(ret == -EOPENSTALE)) {
|
|
|
|
/*
|
|
|
|
* We cannot report events with stale fd so drop it.
|
|
|
|
* Setting ret to 0 will continue the event loop and
|
|
|
|
* do the right thing if there are no more events to
|
|
|
|
* read (i.e. return bytes read, -EAGAIN or wait).
|
|
|
|
*/
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 05:46:35 +08:00
|
|
|
/*
|
|
|
|
* Permission events get queued to wait for response. Other
|
|
|
|
* events can be destroyed now.
|
|
|
|
*/
|
2014-04-04 05:46:36 +08:00
|
|
|
if (!(kevent->mask & FAN_ALL_PERM_EVENTS)) {
|
2014-04-04 05:46:35 +08:00
|
|
|
fsnotify_destroy_event(group, kevent);
|
2014-04-04 05:46:36 +08:00
|
|
|
} else {
|
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
2017-04-25 19:29:35 +08:00
|
|
|
if (ret <= 0) {
|
2014-04-04 05:46:36 +08:00
|
|
|
FANOTIFY_PE(kevent)->response = FAN_DENY;
|
|
|
|
wake_up(&group->fanotify_data.access_waitq);
|
2017-04-25 19:29:35 +08:00
|
|
|
} else {
|
|
|
|
spin_lock(&group->notification_lock);
|
|
|
|
list_add_tail(&kevent->list,
|
|
|
|
&group->fanotify_data.access_list);
|
|
|
|
spin_unlock(&group->notification_lock);
|
2014-04-04 05:46:36 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2017-04-25 19:29:35 +08:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2014-04-04 05:46:35 +08:00
|
|
|
buf += ret;
|
|
|
|
count -= ret;
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
2014-12-16 23:28:38 +08:00
|
|
|
remove_wait_queue(&group->notification_waitq, &wait);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
if (start != buf && ret != -EFAULT)
|
|
|
|
ret = buf - start;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:34 +08:00
|
|
|
static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
|
|
|
struct fanotify_response response = { .fd = -1, .response = -1 };
|
|
|
|
struct fsnotify_group *group;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
group = file->private_data;
|
|
|
|
|
|
|
|
if (count > sizeof(response))
|
|
|
|
count = sizeof(response);
|
|
|
|
|
|
|
|
pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
|
|
|
|
|
|
|
|
if (copy_from_user(&response, buf, count))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
ret = process_access_response(group, &response);
|
|
|
|
if (ret < 0)
|
|
|
|
count = ret;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
#else
|
|
|
|
return -EINVAL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
static int fanotify_release(struct inode *ignored, struct file *file)
|
|
|
|
{
|
|
|
|
struct fsnotify_group *group = file->private_data;
|
|
|
|
|
2010-08-19 00:25:50 +08:00
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
2014-04-04 05:46:33 +08:00
|
|
|
struct fanotify_perm_event_info *event, *next;
|
2016-09-20 05:44:30 +08:00
|
|
|
struct fsnotify_event *fsn_event;
|
2010-10-29 05:21:59 +08:00
|
|
|
|
2014-08-07 07:03:28 +08:00
|
|
|
/*
|
2016-09-20 05:44:30 +08:00
|
|
|
* Stop new events from arriving in the notification queue. since
|
|
|
|
* userspace cannot use fanotify fd anymore, no event can enter or
|
|
|
|
* leave access_list by now either.
|
2014-08-07 07:03:28 +08:00
|
|
|
*/
|
2016-09-20 05:44:30 +08:00
|
|
|
fsnotify_group_stop_queueing(group);
|
2010-08-19 00:25:50 +08:00
|
|
|
|
2016-09-20 05:44:30 +08:00
|
|
|
/*
|
|
|
|
* Process all permission events on access_list and notification queue
|
|
|
|
* and simulate reply from userspace.
|
|
|
|
*/
|
2016-10-08 07:56:55 +08:00
|
|
|
spin_lock(&group->notification_lock);
|
2014-04-04 05:46:33 +08:00
|
|
|
list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
|
|
|
|
fae.fse.list) {
|
|
|
|
pr_debug("%s: found group=%p event=%p\n", __func__, group,
|
|
|
|
event);
|
2010-08-19 00:25:50 +08:00
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
list_del_init(&event->fae.fse.list);
|
|
|
|
event->response = FAN_ALLOW;
|
2010-08-19 00:25:50 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 07:03:28 +08:00
|
|
|
/*
|
2016-09-20 05:44:30 +08:00
|
|
|
* Destroy all non-permission events. For permission events just
|
|
|
|
* dequeue them and set the response. They will be freed once the
|
|
|
|
* response is consumed and fanotify_get_response() returns.
|
2014-08-07 07:03:28 +08:00
|
|
|
*/
|
2016-09-20 05:44:30 +08:00
|
|
|
while (!fsnotify_notify_queue_is_empty(group)) {
|
|
|
|
fsn_event = fsnotify_remove_first_event(group);
|
2016-10-08 07:56:49 +08:00
|
|
|
if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_unlock(&group->notification_lock);
|
2016-09-20 05:44:30 +08:00
|
|
|
fsnotify_destroy_event(group, fsn_event);
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_lock(&group->notification_lock);
|
2016-10-08 07:56:49 +08:00
|
|
|
} else
|
2016-09-20 05:44:30 +08:00
|
|
|
FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
|
|
|
|
}
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_unlock(&group->notification_lock);
|
2016-09-20 05:44:30 +08:00
|
|
|
|
|
|
|
/* Response for all permission events it set, wakeup waiters */
|
2010-08-19 00:25:50 +08:00
|
|
|
wake_up(&group->fanotify_data.access_waitq);
|
|
|
|
#endif
|
2011-10-15 05:43:39 +08:00
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
/* matches the fanotify_init->fsnotify_alloc_group */
|
2011-06-14 23:29:45 +08:00
|
|
|
fsnotify_destroy_group(group);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct fsnotify_group *group;
|
2014-01-22 07:48:14 +08:00
|
|
|
struct fsnotify_event *fsn_event;
|
2009-12-18 10:24:26 +08:00
|
|
|
void __user *p;
|
|
|
|
int ret = -ENOTTY;
|
|
|
|
size_t send_len = 0;
|
|
|
|
|
|
|
|
group = file->private_data;
|
|
|
|
|
|
|
|
p = (void __user *) arg;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FIONREAD:
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_lock(&group->notification_lock);
|
2014-01-22 07:48:14 +08:00
|
|
|
list_for_each_entry(fsn_event, &group->notification_list, list)
|
2009-12-18 10:24:26 +08:00
|
|
|
send_len += FAN_EVENT_METADATA_LEN;
|
2016-10-08 07:56:52 +08:00
|
|
|
spin_unlock(&group->notification_lock);
|
2009-12-18 10:24:26 +08:00
|
|
|
ret = put_user(send_len, (int __user *) p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
static const struct file_operations fanotify_fops = {
|
2012-12-18 08:05:12 +08:00
|
|
|
.show_fdinfo = fanotify_show_fdinfo,
|
2009-12-18 10:24:26 +08:00
|
|
|
.poll = fanotify_poll,
|
|
|
|
.read = fanotify_read,
|
2009-12-18 10:24:34 +08:00
|
|
|
.write = fanotify_write,
|
2009-12-18 10:24:26 +08:00
|
|
|
.fasync = NULL,
|
|
|
|
.release = fanotify_release,
|
2009-12-18 10:24:26 +08:00
|
|
|
.unlocked_ioctl = fanotify_ioctl,
|
|
|
|
.compat_ioctl = fanotify_ioctl,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = noop_llseek,
|
2009-12-18 10:24:26 +08:00
|
|
|
};
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
static int fanotify_find_path(int dfd, const char __user *filename,
|
|
|
|
struct path *path, unsigned int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
|
|
|
|
dfd, filename, flags);
|
|
|
|
|
|
|
|
if (filename == NULL) {
|
2012-08-29 00:52:22 +08:00
|
|
|
struct fd f = fdget(dfd);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
ret = -EBADF;
|
2012-08-29 00:52:22 +08:00
|
|
|
if (!f.file)
|
2009-12-18 10:24:26 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = -ENOTDIR;
|
|
|
|
if ((flags & FAN_MARK_ONLYDIR) &&
|
2013-01-24 06:07:38 +08:00
|
|
|
!(S_ISDIR(file_inode(f.file)->i_mode))) {
|
2012-08-29 00:52:22 +08:00
|
|
|
fdput(f);
|
2009-12-18 10:24:26 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-29 00:52:22 +08:00
|
|
|
*path = f.file->f_path;
|
2009-12-18 10:24:26 +08:00
|
|
|
path_get(path);
|
2012-08-29 00:52:22 +08:00
|
|
|
fdput(f);
|
2009-12-18 10:24:26 +08:00
|
|
|
} else {
|
|
|
|
unsigned int lookup_flags = 0;
|
|
|
|
|
|
|
|
if (!(flags & FAN_MARK_DONT_FOLLOW))
|
|
|
|
lookup_flags |= LOOKUP_FOLLOW;
|
|
|
|
if (flags & FAN_MARK_ONLYDIR)
|
|
|
|
lookup_flags |= LOOKUP_DIRECTORY;
|
|
|
|
|
|
|
|
ret = user_path_at(dfd, filename, lookup_flags, path);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* you can only watch an inode if you have read permissions on it */
|
|
|
|
ret = inode_permission(path->dentry->d_inode, MAY_READ);
|
|
|
|
if (ret)
|
|
|
|
path_put(path);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:33 +08:00
|
|
|
static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
|
|
|
|
__u32 mask,
|
2011-06-14 23:29:49 +08:00
|
|
|
unsigned int flags,
|
|
|
|
int *destroy)
|
2009-12-18 10:24:28 +08:00
|
|
|
{
|
2015-02-11 06:08:24 +08:00
|
|
|
__u32 oldmask = 0;
|
2009-12-18 10:24:28 +08:00
|
|
|
|
|
|
|
spin_lock(&fsn_mark->lock);
|
2009-12-18 10:24:33 +08:00
|
|
|
if (!(flags & FAN_MARK_IGNORED_MASK)) {
|
2015-02-11 06:08:27 +08:00
|
|
|
__u32 tmask = fsn_mark->mask & ~mask;
|
|
|
|
|
|
|
|
if (flags & FAN_MARK_ONDIR)
|
|
|
|
tmask &= ~FAN_ONDIR;
|
|
|
|
|
2009-12-18 10:24:33 +08:00
|
|
|
oldmask = fsn_mark->mask;
|
2016-12-21 23:03:59 +08:00
|
|
|
fsn_mark->mask = tmask;
|
2009-12-18 10:24:33 +08:00
|
|
|
} else {
|
2015-02-11 06:08:24 +08:00
|
|
|
__u32 tmask = fsn_mark->ignored_mask & ~mask;
|
2015-02-11 06:08:27 +08:00
|
|
|
if (flags & FAN_MARK_ONDIR)
|
|
|
|
tmask &= ~FAN_ONDIR;
|
2016-12-21 23:03:59 +08:00
|
|
|
fsn_mark->ignored_mask = tmask;
|
2009-12-18 10:24:33 +08:00
|
|
|
}
|
2015-02-11 06:08:21 +08:00
|
|
|
*destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
|
2009-12-18 10:24:28 +08:00
|
|
|
spin_unlock(&fsn_mark->lock);
|
|
|
|
|
|
|
|
return mask & oldmask;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:29 +08:00
|
|
|
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
|
2009-12-18 10:24:33 +08:00
|
|
|
struct vfsmount *mnt, __u32 mask,
|
|
|
|
unsigned int flags)
|
2009-12-18 10:24:28 +08:00
|
|
|
{
|
|
|
|
struct fsnotify_mark *fsn_mark = NULL;
|
2009-12-18 10:24:28 +08:00
|
|
|
__u32 removed;
|
2011-06-14 23:29:49 +08:00
|
|
|
int destroy_mark;
|
2009-12-18 10:24:28 +08:00
|
|
|
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_lock(&group->mark_mutex);
|
2016-12-21 23:28:45 +08:00
|
|
|
fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
|
|
|
|
group);
|
2013-07-09 06:59:42 +08:00
|
|
|
if (!fsn_mark) {
|
|
|
|
mutex_unlock(&group->mark_mutex);
|
2009-12-18 10:24:29 +08:00
|
|
|
return -ENOENT;
|
2013-07-09 06:59:42 +08:00
|
|
|
}
|
2009-12-18 10:24:28 +08:00
|
|
|
|
2011-06-14 23:29:49 +08:00
|
|
|
removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
|
|
|
|
&destroy_mark);
|
2016-12-14 20:53:46 +08:00
|
|
|
if (removed & real_mount(mnt)->mnt_fsnotify_mask)
|
2016-12-21 23:13:54 +08:00
|
|
|
fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
|
2011-06-14 23:29:49 +08:00
|
|
|
if (destroy_mark)
|
2015-09-05 06:43:12 +08:00
|
|
|
fsnotify_detach_mark(fsn_mark);
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_unlock(&group->mark_mutex);
|
2015-09-05 06:43:12 +08:00
|
|
|
if (destroy_mark)
|
|
|
|
fsnotify_free_mark(fsn_mark);
|
2011-06-14 23:29:49 +08:00
|
|
|
|
2009-12-18 10:24:29 +08:00
|
|
|
fsnotify_put_mark(fsn_mark);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2009-12-18 10:24:29 +08:00
|
|
|
static int fanotify_remove_inode_mark(struct fsnotify_group *group,
|
2009-12-18 10:24:33 +08:00
|
|
|
struct inode *inode, __u32 mask,
|
|
|
|
unsigned int flags)
|
2009-12-18 10:24:29 +08:00
|
|
|
{
|
|
|
|
struct fsnotify_mark *fsn_mark = NULL;
|
|
|
|
__u32 removed;
|
2011-06-14 23:29:49 +08:00
|
|
|
int destroy_mark;
|
2009-12-18 10:24:29 +08:00
|
|
|
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_lock(&group->mark_mutex);
|
2016-12-21 23:28:45 +08:00
|
|
|
fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
|
2013-07-09 06:59:42 +08:00
|
|
|
if (!fsn_mark) {
|
|
|
|
mutex_unlock(&group->mark_mutex);
|
2009-12-18 10:24:28 +08:00
|
|
|
return -ENOENT;
|
2013-07-09 06:59:42 +08:00
|
|
|
}
|
2009-12-18 10:24:28 +08:00
|
|
|
|
2011-06-14 23:29:49 +08:00
|
|
|
removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
|
|
|
|
&destroy_mark);
|
2016-12-14 20:53:46 +08:00
|
|
|
if (removed & inode->i_fsnotify_mask)
|
2016-12-21 23:13:54 +08:00
|
|
|
fsnotify_recalc_mask(inode->i_fsnotify_marks);
|
2011-06-14 23:29:49 +08:00
|
|
|
if (destroy_mark)
|
2015-09-05 06:43:12 +08:00
|
|
|
fsnotify_detach_mark(fsn_mark);
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_unlock(&group->mark_mutex);
|
2015-09-05 06:43:12 +08:00
|
|
|
if (destroy_mark)
|
|
|
|
fsnotify_free_mark(fsn_mark);
|
2013-07-09 06:59:42 +08:00
|
|
|
|
2016-12-21 23:28:45 +08:00
|
|
|
/* matches the fsnotify_find_mark() */
|
2009-12-18 10:24:26 +08:00
|
|
|
fsnotify_put_mark(fsn_mark);
|
2009-12-18 10:24:28 +08:00
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:33 +08:00
|
|
|
static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
|
|
|
|
__u32 mask,
|
|
|
|
unsigned int flags)
|
2009-12-18 10:24:28 +08:00
|
|
|
{
|
2010-10-29 05:21:59 +08:00
|
|
|
__u32 oldmask = -1;
|
2009-12-18 10:24:28 +08:00
|
|
|
|
|
|
|
spin_lock(&fsn_mark->lock);
|
2009-12-18 10:24:33 +08:00
|
|
|
if (!(flags & FAN_MARK_IGNORED_MASK)) {
|
2015-02-11 06:08:27 +08:00
|
|
|
__u32 tmask = fsn_mark->mask | mask;
|
|
|
|
|
|
|
|
if (flags & FAN_MARK_ONDIR)
|
|
|
|
tmask |= FAN_ONDIR;
|
|
|
|
|
2009-12-18 10:24:33 +08:00
|
|
|
oldmask = fsn_mark->mask;
|
2016-12-21 23:03:59 +08:00
|
|
|
fsn_mark->mask = tmask;
|
2009-12-18 10:24:33 +08:00
|
|
|
} else {
|
2010-10-29 05:21:59 +08:00
|
|
|
__u32 tmask = fsn_mark->ignored_mask | mask;
|
2015-02-11 06:08:27 +08:00
|
|
|
if (flags & FAN_MARK_ONDIR)
|
|
|
|
tmask |= FAN_ONDIR;
|
|
|
|
|
2016-12-21 23:03:59 +08:00
|
|
|
fsn_mark->ignored_mask = tmask;
|
2009-12-18 10:24:33 +08:00
|
|
|
if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
|
|
|
|
fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
|
2009-12-18 10:24:33 +08:00
|
|
|
}
|
2009-12-18 10:24:28 +08:00
|
|
|
spin_unlock(&fsn_mark->lock);
|
|
|
|
|
|
|
|
return mask & ~oldmask;
|
|
|
|
}
|
|
|
|
|
2013-07-09 06:59:43 +08:00
|
|
|
static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
|
|
|
|
struct inode *inode,
|
|
|
|
struct vfsmount *mnt)
|
|
|
|
{
|
|
|
|
struct fsnotify_mark *mark;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
|
|
|
|
return ERR_PTR(-ENOSPC);
|
|
|
|
|
|
|
|
mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
|
|
|
|
if (!mark)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2016-12-22 01:06:12 +08:00
|
|
|
fsnotify_init_mark(mark, group);
|
2016-12-22 01:32:48 +08:00
|
|
|
ret = fsnotify_add_mark_locked(mark, inode, mnt, 0);
|
2013-07-09 06:59:43 +08:00
|
|
|
if (ret) {
|
|
|
|
fsnotify_put_mark(mark);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mark;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-18 10:24:28 +08:00
|
|
|
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
|
2009-12-18 10:24:33 +08:00
|
|
|
struct vfsmount *mnt, __u32 mask,
|
|
|
|
unsigned int flags)
|
2009-12-18 10:24:26 +08:00
|
|
|
{
|
|
|
|
struct fsnotify_mark *fsn_mark;
|
2009-12-18 10:24:28 +08:00
|
|
|
__u32 added;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_lock(&group->mark_mutex);
|
2016-12-21 23:28:45 +08:00
|
|
|
fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
|
|
|
|
group);
|
2009-12-18 10:24:28 +08:00
|
|
|
if (!fsn_mark) {
|
2013-07-09 06:59:43 +08:00
|
|
|
fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
|
|
|
|
if (IS_ERR(fsn_mark)) {
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_unlock(&group->mark_mutex);
|
2013-07-09 06:59:43 +08:00
|
|
|
return PTR_ERR(fsn_mark);
|
2013-07-09 06:59:42 +08:00
|
|
|
}
|
2009-12-18 10:24:28 +08:00
|
|
|
}
|
2009-12-18 10:24:33 +08:00
|
|
|
added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
|
2011-11-25 15:35:16 +08:00
|
|
|
if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
|
2016-12-21 23:13:54 +08:00
|
|
|
fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks);
|
2016-12-14 20:53:46 +08:00
|
|
|
mutex_unlock(&group->mark_mutex);
|
2013-07-09 06:59:43 +08:00
|
|
|
|
2010-11-10 01:18:16 +08:00
|
|
|
fsnotify_put_mark(fsn_mark);
|
2013-07-09 06:59:43 +08:00
|
|
|
return 0;
|
2009-12-18 10:24:28 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:28 +08:00
|
|
|
static int fanotify_add_inode_mark(struct fsnotify_group *group,
|
2009-12-18 10:24:33 +08:00
|
|
|
struct inode *inode, __u32 mask,
|
|
|
|
unsigned int flags)
|
2009-12-18 10:24:28 +08:00
|
|
|
{
|
|
|
|
struct fsnotify_mark *fsn_mark;
|
2009-12-18 10:24:28 +08:00
|
|
|
__u32 added;
|
2009-12-18 10:24:28 +08:00
|
|
|
|
|
|
|
pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2010-10-29 05:21:57 +08:00
|
|
|
/*
|
|
|
|
* If some other task has this inode open for write we should not add
|
|
|
|
* an ignored mark, unless that ignored mark is supposed to survive
|
|
|
|
* modification changes anyway.
|
|
|
|
*/
|
|
|
|
if ((flags & FAN_MARK_IGNORED_MASK) &&
|
|
|
|
!(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
|
|
|
|
(atomic_read(&inode->i_writecount) > 0))
|
|
|
|
return 0;
|
|
|
|
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_lock(&group->mark_mutex);
|
2016-12-21 23:28:45 +08:00
|
|
|
fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
|
2009-12-18 10:24:26 +08:00
|
|
|
if (!fsn_mark) {
|
2013-07-09 06:59:43 +08:00
|
|
|
fsn_mark = fanotify_add_new_mark(group, inode, NULL);
|
|
|
|
if (IS_ERR(fsn_mark)) {
|
2013-07-09 06:59:42 +08:00
|
|
|
mutex_unlock(&group->mark_mutex);
|
2013-07-09 06:59:43 +08:00
|
|
|
return PTR_ERR(fsn_mark);
|
2013-07-09 06:59:42 +08:00
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
2009-12-18 10:24:33 +08:00
|
|
|
added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
|
2010-07-28 22:18:39 +08:00
|
|
|
if (added & ~inode->i_fsnotify_mask)
|
2016-12-21 23:13:54 +08:00
|
|
|
fsnotify_recalc_mask(inode->i_fsnotify_marks);
|
2016-12-14 20:53:46 +08:00
|
|
|
mutex_unlock(&group->mark_mutex);
|
2013-07-09 06:59:43 +08:00
|
|
|
|
2010-11-10 01:18:16 +08:00
|
|
|
fsnotify_put_mark(fsn_mark);
|
2013-07-09 06:59:43 +08:00
|
|
|
return 0;
|
2009-12-18 10:24:28 +08:00
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
/* fanotify syscalls */
|
2010-05-27 21:41:40 +08:00
|
|
|
SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
|
2009-12-18 10:24:25 +08:00
|
|
|
{
|
2009-12-18 10:24:26 +08:00
|
|
|
struct fsnotify_group *group;
|
|
|
|
int f_flags, fd;
|
2010-10-29 05:21:58 +08:00
|
|
|
struct user_struct *user;
|
2014-02-22 02:14:11 +08:00
|
|
|
struct fanotify_event_info *oevent;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2010-05-27 21:41:40 +08:00
|
|
|
pr_debug("%s: flags=%d event_f_flags=%d\n",
|
|
|
|
__func__, flags, event_f_flags);
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
2010-08-24 18:58:54 +08:00
|
|
|
return -EPERM;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
if (flags & ~FAN_ALL_INIT_FLAGS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
fanotify: check file flags passed in fanotify_init
Without this patch fanotify_init does not validate the value passed in
event_f_flags.
When a fanotify event is read from the fanotify file descriptor a new
file descriptor is created where file.f_flags = event_f_flags.
Internal and external open flags are stored together in field f_flags of
struct file. Hence, an application might create file descriptors with
internal flags like FMODE_EXEC, FMODE_NOCMTIME set.
Jan Kara and Eric Paris both aggreed that this is a bug and the value of
event_f_flags should be checked:
https://lkml.org/lkml/2014/4/29/522
https://lkml.org/lkml/2014/4/29/539
This updated patch version considers the comments by Michael Kerrisk in
https://lkml.org/lkml/2014/5/4/10
With the patch the value of event_f_flags is checked.
When specifying an invalid value error EINVAL is returned.
Internal flags are disallowed.
File creation flags are disallowed:
O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TRUNC, and O_TTY_INIT.
Flags which do not make sense with fanotify are disallowed:
__O_TMPFILE, O_PATH, FASYNC, and O_DIRECT.
This leaves us with the following allowed values:
O_RDONLY, O_WRONLY, O_RDWR are basic functionality. The are stored in the
bits given by O_ACCMODE.
O_APPEND is working as expected. The value might be useful in a logging
application which appends the current status each time the log is opened.
O_LARGEFILE is needed for files exceeding 4GB on 32bit systems.
O_NONBLOCK may be useful when monitoring slow devices like tapes.
O_NDELAY is equal to O_NONBLOCK except for platform parisc.
To avoid code breaking on parisc either both flags should be
allowed or none. The patch allows both.
__O_SYNC and O_DSYNC may be used to avoid data loss on power disruption.
O_NOATIME may be useful to reduce disk activity.
O_CLOEXEC may be useful, if separate processes shall be used to scan files.
Once this patch is accepted, the fanotify_init.2 manpage has to be updated.
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Valdis Kletnieks <Valdis.Kletnieks@vt.edu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-06-05 07:05:44 +08:00
|
|
|
if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (event_f_flags & O_ACCMODE) {
|
|
|
|
case O_RDONLY:
|
|
|
|
case O_RDWR:
|
|
|
|
case O_WRONLY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-10-29 05:21:58 +08:00
|
|
|
user = get_current_user();
|
|
|
|
if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
|
|
|
|
free_uid(user);
|
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:34 +08:00
|
|
|
f_flags = O_RDWR | FMODE_NONOTIFY;
|
2009-12-18 10:24:26 +08:00
|
|
|
if (flags & FAN_CLOEXEC)
|
|
|
|
f_flags |= O_CLOEXEC;
|
|
|
|
if (flags & FAN_NONBLOCK)
|
|
|
|
f_flags |= O_NONBLOCK;
|
|
|
|
|
|
|
|
/* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
|
|
|
|
group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
|
2010-11-24 12:48:26 +08:00
|
|
|
if (IS_ERR(group)) {
|
|
|
|
free_uid(user);
|
2009-12-18 10:24:26 +08:00
|
|
|
return PTR_ERR(group);
|
2010-11-24 12:48:26 +08:00
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2010-10-29 05:21:58 +08:00
|
|
|
group->fanotify_data.user = user;
|
|
|
|
atomic_inc(&user->fanotify_listeners);
|
|
|
|
|
2014-04-04 05:46:33 +08:00
|
|
|
oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
|
2014-02-22 02:14:11 +08:00
|
|
|
if (unlikely(!oevent)) {
|
|
|
|
fd = -ENOMEM;
|
|
|
|
goto out_destroy_group;
|
|
|
|
}
|
|
|
|
group->overflow_event = &oevent->fse;
|
|
|
|
|
2014-05-07 03:50:10 +08:00
|
|
|
if (force_o_largefile())
|
|
|
|
event_f_flags |= O_LARGEFILE;
|
2010-07-28 22:18:37 +08:00
|
|
|
group->fanotify_data.f_flags = event_f_flags;
|
2009-12-18 10:24:34 +08:00
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
|
|
|
init_waitqueue_head(&group->fanotify_data.access_waitq);
|
|
|
|
INIT_LIST_HEAD(&group->fanotify_data.access_list);
|
|
|
|
#endif
|
2010-10-29 05:21:56 +08:00
|
|
|
switch (flags & FAN_ALL_CLASS_BITS) {
|
|
|
|
case FAN_CLASS_NOTIF:
|
|
|
|
group->priority = FS_PRIO_0;
|
|
|
|
break;
|
|
|
|
case FAN_CLASS_CONTENT:
|
|
|
|
group->priority = FS_PRIO_1;
|
|
|
|
break;
|
|
|
|
case FAN_CLASS_PRE_CONTENT:
|
|
|
|
group->priority = FS_PRIO_2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fd = -EINVAL;
|
2011-06-14 23:29:45 +08:00
|
|
|
goto out_destroy_group;
|
2010-10-29 05:21:56 +08:00
|
|
|
}
|
2009-12-18 10:24:34 +08:00
|
|
|
|
2010-10-29 05:21:57 +08:00
|
|
|
if (flags & FAN_UNLIMITED_QUEUE) {
|
|
|
|
fd = -EPERM;
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
2011-06-14 23:29:45 +08:00
|
|
|
goto out_destroy_group;
|
2010-10-29 05:21:57 +08:00
|
|
|
group->max_events = UINT_MAX;
|
|
|
|
} else {
|
|
|
|
group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
|
|
|
|
}
|
2010-10-29 05:21:57 +08:00
|
|
|
|
2010-10-29 05:21:58 +08:00
|
|
|
if (flags & FAN_UNLIMITED_MARKS) {
|
|
|
|
fd = -EPERM;
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
2011-06-14 23:29:45 +08:00
|
|
|
goto out_destroy_group;
|
2010-10-29 05:21:58 +08:00
|
|
|
group->fanotify_data.max_marks = UINT_MAX;
|
|
|
|
} else {
|
|
|
|
group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
|
|
|
|
}
|
2010-10-29 05:21:57 +08:00
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
|
|
|
|
if (fd < 0)
|
2011-06-14 23:29:45 +08:00
|
|
|
goto out_destroy_group;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
2011-06-14 23:29:45 +08:00
|
|
|
out_destroy_group:
|
|
|
|
fsnotify_destroy_group(group);
|
2009-12-18 10:24:26 +08:00
|
|
|
return fd;
|
2009-12-18 10:24:25 +08:00
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2013-01-22 04:16:58 +08:00
|
|
|
SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
|
|
|
|
__u64, mask, int, dfd,
|
|
|
|
const char __user *, pathname)
|
2009-12-18 10:24:26 +08:00
|
|
|
{
|
2009-12-18 10:24:29 +08:00
|
|
|
struct inode *inode = NULL;
|
|
|
|
struct vfsmount *mnt = NULL;
|
2009-12-18 10:24:26 +08:00
|
|
|
struct fsnotify_group *group;
|
2012-08-29 00:52:22 +08:00
|
|
|
struct fd f;
|
2009-12-18 10:24:26 +08:00
|
|
|
struct path path;
|
2012-08-29 00:52:22 +08:00
|
|
|
int ret;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
|
|
|
|
__func__, fanotify_fd, flags, dfd, pathname, mask);
|
|
|
|
|
|
|
|
/* we only use the lower 32 bits as of right now. */
|
|
|
|
if (mask & ((__u64)0xffffffff << 32))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-12-18 10:24:29 +08:00
|
|
|
if (flags & ~FAN_ALL_MARK_FLAGS)
|
|
|
|
return -EINVAL;
|
2009-12-18 10:24:34 +08:00
|
|
|
switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
|
2010-11-23 01:46:33 +08:00
|
|
|
case FAN_MARK_ADD: /* fallthrough */
|
2009-12-18 10:24:29 +08:00
|
|
|
case FAN_MARK_REMOVE:
|
2010-11-23 01:46:33 +08:00
|
|
|
if (!mask)
|
|
|
|
return -EINVAL;
|
2014-06-05 07:05:43 +08:00
|
|
|
break;
|
2009-12-18 10:24:34 +08:00
|
|
|
case FAN_MARK_FLUSH:
|
2014-06-05 07:05:43 +08:00
|
|
|
if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
|
|
|
|
return -EINVAL;
|
2009-12-18 10:24:29 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-10-29 05:21:59 +08:00
|
|
|
|
|
|
|
if (mask & FAN_ONDIR) {
|
|
|
|
flags |= FAN_MARK_ONDIR;
|
|
|
|
mask &= ~FAN_ONDIR;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:34 +08:00
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
|
|
|
if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
|
|
|
|
#else
|
2009-12-18 10:24:29 +08:00
|
|
|
if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
|
2009-12-18 10:24:34 +08:00
|
|
|
#endif
|
2009-12-18 10:24:26 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2012-08-29 00:52:22 +08:00
|
|
|
f = fdget(fanotify_fd);
|
|
|
|
if (unlikely(!f.file))
|
2009-12-18 10:24:26 +08:00
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
/* verify that this is indeed an fanotify instance */
|
|
|
|
ret = -EINVAL;
|
2012-08-29 00:52:22 +08:00
|
|
|
if (unlikely(f.file->f_op != &fanotify_fops))
|
2009-12-18 10:24:26 +08:00
|
|
|
goto fput_and_out;
|
2012-08-29 00:52:22 +08:00
|
|
|
group = f.file->private_data;
|
2010-10-29 05:21:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
|
|
|
|
* allowed to set permissions events.
|
|
|
|
*/
|
|
|
|
ret = -EINVAL;
|
|
|
|
if (mask & FAN_ALL_PERM_EVENTS &&
|
|
|
|
group->priority == FS_PRIO_0)
|
|
|
|
goto fput_and_out;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
2014-06-05 07:05:40 +08:00
|
|
|
if (flags & FAN_MARK_FLUSH) {
|
|
|
|
ret = 0;
|
|
|
|
if (flags & FAN_MARK_MOUNT)
|
|
|
|
fsnotify_clear_vfsmount_marks_by_group(group);
|
|
|
|
else
|
|
|
|
fsnotify_clear_inode_marks_by_group(group);
|
|
|
|
goto fput_and_out;
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
ret = fanotify_find_path(dfd, pathname, &path, flags);
|
|
|
|
if (ret)
|
|
|
|
goto fput_and_out;
|
|
|
|
|
|
|
|
/* inode held in place by reference to path; group by fget on fd */
|
2009-12-18 10:24:29 +08:00
|
|
|
if (!(flags & FAN_MARK_MOUNT))
|
2009-12-18 10:24:29 +08:00
|
|
|
inode = path.dentry->d_inode;
|
|
|
|
else
|
|
|
|
mnt = path.mnt;
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
/* create/update an inode mark */
|
2014-06-05 07:05:40 +08:00
|
|
|
switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
|
2009-12-18 10:24:28 +08:00
|
|
|
case FAN_MARK_ADD:
|
2009-12-18 10:24:29 +08:00
|
|
|
if (flags & FAN_MARK_MOUNT)
|
2009-12-18 10:24:33 +08:00
|
|
|
ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
|
2009-12-18 10:24:29 +08:00
|
|
|
else
|
2009-12-18 10:24:33 +08:00
|
|
|
ret = fanotify_add_inode_mark(group, inode, mask, flags);
|
2009-12-18 10:24:28 +08:00
|
|
|
break;
|
|
|
|
case FAN_MARK_REMOVE:
|
2009-12-18 10:24:29 +08:00
|
|
|
if (flags & FAN_MARK_MOUNT)
|
2009-12-18 10:24:33 +08:00
|
|
|
ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
|
2009-12-18 10:24:29 +08:00
|
|
|
else
|
2009-12-18 10:24:33 +08:00
|
|
|
ret = fanotify_remove_inode_mark(group, inode, mask, flags);
|
2009-12-18 10:24:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
path_put(&path);
|
|
|
|
fput_and_out:
|
2012-08-29 00:52:22 +08:00
|
|
|
fdput(f);
|
2009-12-18 10:24:26 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:10:59 +08:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
COMPAT_SYSCALL_DEFINE6(fanotify_mark,
|
|
|
|
int, fanotify_fd, unsigned int, flags,
|
|
|
|
__u32, mask0, __u32, mask1, int, dfd,
|
|
|
|
const char __user *, pathname)
|
|
|
|
{
|
|
|
|
return sys_fanotify_mark(fanotify_fd, flags,
|
|
|
|
#ifdef __BIG_ENDIAN
|
|
|
|
((__u64)mask0 << 32) | mask1,
|
2014-01-28 09:07:19 +08:00
|
|
|
#else
|
|
|
|
((__u64)mask1 << 32) | mask0,
|
2013-03-06 09:10:59 +08:00
|
|
|
#endif
|
|
|
|
dfd, pathname);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-18 10:24:26 +08:00
|
|
|
/*
|
2011-03-01 22:06:02 +08:00
|
|
|
* fanotify_user_setup - Our initialization function. Note that we cannot return
|
2009-12-18 10:24:26 +08:00
|
|
|
* error because we have compiled-in VFS hooks. So an (unlikely) failure here
|
|
|
|
* must result in panic().
|
|
|
|
*/
|
|
|
|
static int __init fanotify_user_setup(void)
|
|
|
|
{
|
|
|
|
fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
|
2014-01-22 07:48:14 +08:00
|
|
|
fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
|
2014-04-04 05:46:33 +08:00
|
|
|
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
|
|
|
|
fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
|
|
|
|
SLAB_PANIC);
|
|
|
|
#endif
|
2009-12-18 10:24:26 +08:00
|
|
|
|
|
|
|
return 0;
|
2009-12-18 10:24:26 +08:00
|
|
|
}
|
2009-12-18 10:24:26 +08:00
|
|
|
device_initcall(fanotify_user_setup);
|