fanotify: divorce fanotify_path_event and fanotify_fid_event

Breakup the union and make them both inherit from abstract fanotify_event.

fanotify_path_event, fanotify_fid_event and fanotify_perm_event inherit
from fanotify_event.

type field in abstract fanotify_event determines the concrete event type.

fanotify_path_event, fanotify_fid_event and fanotify_perm_event are
allocated from separate memcache pools.

Rename fanotify_perm_event casting macro to FANOTIFY_PERM(), so that
FANOTIFY_PE() and FANOTIFY_FE() can be used as casting macros to
fanotify_path_event and fanotify_fid_event.

[JK: Cleanup FANOTIFY_PE() and FANOTIFY_FE() to be proper inline
functions and remove requirement that fanotify_event is the first in
event structures]

Link: https://lore.kernel.org/r/20200319151022.31456-11-amir73il@gmail.com
Suggested-by: Jan Kara <jack@suse.cz>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
This commit is contained in:
Jan Kara 2020-03-24 17:04:20 +01:00
parent afc894c784
commit 7088f35720
3 changed files with 180 additions and 94 deletions

View File

@ -29,7 +29,7 @@ static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
}
static bool fanotify_fh_equal(struct fanotify_fh *fh1,
struct fanotify_fh *fh2)
struct fanotify_fh *fh2)
{
if (fh1->type != fh2->type || fh1->len != fh2->len)
return false;
@ -42,6 +42,17 @@ static bool fanotify_fh_equal(struct fanotify_fh *fh1,
!memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
}
static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
struct fanotify_fid_event *ffe2)
{
/* Do not merge fid events without object fh */
if (!ffe1->object_fh.len)
return false;
return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
}
static bool should_merge(struct fsnotify_event *old_fsn,
struct fsnotify_event *new_fsn)
{
@ -51,14 +62,15 @@ static bool should_merge(struct fsnotify_event *old_fsn,
old = FANOTIFY_E(old_fsn);
new = FANOTIFY_E(new_fsn);
if (old_fsn->objectid != new_fsn->objectid || old->pid != new->pid ||
old->fh.type != new->fh.type)
if (old_fsn->objectid != new_fsn->objectid ||
old->type != new->type || old->pid != new->pid)
return false;
if (fanotify_event_has_path(old)) {
switch (old->type) {
case FANOTIFY_EVENT_TYPE_PATH:
return fanotify_path_equal(fanotify_event_path(old),
fanotify_event_path(new));
} else if (fanotify_event_has_fid(old)) {
case FANOTIFY_EVENT_TYPE_FID:
/*
* We want to merge many dirent events in the same dir (i.e.
* creates/unlinks/renames), but we do not want to merge dirent
@ -70,11 +82,12 @@ static bool should_merge(struct fsnotify_event *old_fsn,
if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
return false;
return fanotify_fsid_equal(&old->fsid, &new->fsid) &&
fanotify_fh_equal(&old->fh, &new->fh);
return fanotify_fid_event_equal(FANOTIFY_FE(old),
FANOTIFY_FE(new));
default:
WARN_ON_ONCE(1);
}
/* Do not merge events if we failed to encode fid */
return false;
}
@ -310,6 +323,7 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
__kernel_fsid_t *fsid)
{
struct fanotify_event *event = NULL;
struct fanotify_fid_event *ffe = NULL;
gfp_t gfp = GFP_KERNEL_ACCOUNT;
struct inode *id = fanotify_fid_inode(inode, mask, data, data_type);
const struct path *path = fsnotify_data_path(data, data_type);
@ -334,14 +348,32 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
if (!pevent)
goto out;
event = &pevent->fae;
event->type = FANOTIFY_EVENT_TYPE_PATH_PERM;
pevent->response = 0;
pevent->state = FAN_EVENT_INIT;
goto init;
}
event = kmem_cache_alloc(fanotify_event_cachep, gfp);
if (!event)
goto out;
if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
if (!ffe)
goto out;
event = &ffe->fae;
event->type = FANOTIFY_EVENT_TYPE_FID;
} else {
struct fanotify_path_event *pevent;
pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
if (!pevent)
goto out;
event = &pevent->fae;
event->type = FANOTIFY_EVENT_TYPE_PATH;
}
init: __maybe_unused
/*
* Use the victim inode instead of the watching inode as the id for
@ -354,19 +386,23 @@ init: __maybe_unused
event->pid = get_pid(task_pid(current));
else
event->pid = get_pid(task_tgid(current));
event->fh.len = 0;
if (id && FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
event->fsid = *fsid;
if (fanotify_event_has_fid(event)) {
ffe->object_fh.len = 0;
if (fsid)
ffe->fsid = *fsid;
if (id)
fanotify_encode_fh(&event->fh, id, gfp);
} else if (path) {
event->fh.type = FILEID_ROOT;
event->path = *path;
path_get(path);
} else {
event->fh.type = FILEID_INVALID;
event->path.mnt = NULL;
event->path.dentry = NULL;
fanotify_encode_fh(&ffe->object_fh, id, gfp);
} else if (fanotify_event_has_path(event)) {
struct path *p = fanotify_event_path(event);
if (path) {
*p = *path;
path_get(path);
} else {
p->mnt = NULL;
p->dentry = NULL;
}
}
out:
memalloc_unuse_memcg();
@ -486,7 +522,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
ret = 0;
} else if (fanotify_is_perm_event(mask)) {
ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
ret = fanotify_get_response(group, FANOTIFY_PERM(event),
iter_info);
}
finish:
@ -505,22 +541,46 @@ static void fanotify_free_group_priv(struct fsnotify_group *group)
free_uid(user);
}
static void fanotify_free_path_event(struct fanotify_event *event)
{
path_put(fanotify_event_path(event));
kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
}
static void fanotify_free_perm_event(struct fanotify_event *event)
{
path_put(fanotify_event_path(event));
kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
}
static void fanotify_free_fid_event(struct fanotify_event *event)
{
struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
if (fanotify_fh_has_ext_buf(&ffe->object_fh))
kfree(fanotify_fh_ext_buf(&ffe->object_fh));
kmem_cache_free(fanotify_fid_event_cachep, ffe);
}
static void fanotify_free_event(struct fsnotify_event *fsn_event)
{
struct fanotify_event *event;
event = FANOTIFY_E(fsn_event);
if (fanotify_event_has_path(event))
path_put(&event->path);
else if (fanotify_fh_has_ext_buf(&event->fh))
kfree(fanotify_fh_ext_buf(&event->fh));
put_pid(event->pid);
if (fanotify_is_perm_event(event->mask)) {
kmem_cache_free(fanotify_perm_event_cachep,
FANOTIFY_PE(fsn_event));
return;
switch (event->type) {
case FANOTIFY_EVENT_TYPE_PATH:
fanotify_free_path_event(event);
break;
case FANOTIFY_EVENT_TYPE_PATH_PERM:
fanotify_free_perm_event(event);
break;
case FANOTIFY_EVENT_TYPE_FID:
fanotify_free_fid_event(event);
break;
default:
WARN_ON_ONCE(1);
}
kmem_cache_free(fanotify_event_cachep, event);
}
static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)

View File

@ -5,7 +5,8 @@
#include <linux/exportfs.h>
extern struct kmem_cache *fanotify_mark_cache;
extern struct kmem_cache *fanotify_event_cachep;
extern struct kmem_cache *fanotify_fid_event_cachep;
extern struct kmem_cache *fanotify_path_event_cachep;
extern struct kmem_cache *fanotify_perm_event_cachep;
/* Possible states of the permission event */
@ -52,43 +53,45 @@ static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
}
/*
* Structure for normal fanotify events. It gets allocated in
* Common structure for fanotify events. Concrete structs are allocated in
* fanotify_handle_event() and freed when the information is retrieved by
* userspace
* userspace. The type of event determines how it was allocated, how it will
* be freed and which concrete struct it may be cast to.
*/
enum fanotify_event_type {
FANOTIFY_EVENT_TYPE_FID,
FANOTIFY_EVENT_TYPE_PATH,
FANOTIFY_EVENT_TYPE_PATH_PERM,
};
struct fanotify_event {
struct fsnotify_event fse;
u32 mask;
/*
* With FAN_REPORT_FID, we do not hold any reference on the
* victim object. Instead we store its NFS file handle and its
* filesystem's fsid as a unique identifier.
*/
__kernel_fsid_t fsid;
struct fanotify_fh fh;
/*
* We hold ref to this path so it may be dereferenced at any
* point during this object's lifetime
*/
struct path path;
enum fanotify_event_type type;
struct pid *pid;
};
static inline bool fanotify_event_has_path(struct fanotify_event *event)
struct fanotify_fid_event {
struct fanotify_event fae;
__kernel_fsid_t fsid;
struct fanotify_fh object_fh;
};
static inline struct fanotify_fid_event *
FANOTIFY_FE(struct fanotify_event *event)
{
return event->fh.type == FILEID_ROOT;
return container_of(event, struct fanotify_fid_event, fae);
}
static inline bool fanotify_event_has_fid(struct fanotify_event *event)
{
return event->fh.type != FILEID_ROOT &&
event->fh.type != FILEID_INVALID;
return event->type == FANOTIFY_EVENT_TYPE_FID;
}
static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
{
if (fanotify_event_has_fid(event))
return &event->fsid;
if (event->type == FANOTIFY_EVENT_TYPE_FID)
return &FANOTIFY_FE(event)->fsid;
else
return NULL;
}
@ -96,8 +99,8 @@ static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
static inline struct fanotify_fh *fanotify_event_object_fh(
struct fanotify_event *event)
{
if (fanotify_event_has_fid(event))
return &event->fh;
if (event->type == FANOTIFY_EVENT_TYPE_FID)
return &FANOTIFY_FE(event)->object_fh;
else
return NULL;
}
@ -109,6 +112,17 @@ static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
return fh ? fh->len : 0;
}
struct fanotify_path_event {
struct fanotify_event fae;
struct path path;
};
static inline struct fanotify_path_event *
FANOTIFY_PE(struct fanotify_event *event)
{
return container_of(event, struct fanotify_path_event, fae);
}
/*
* Structure for permission fanotify events. It gets allocated and freed in
* fanotify_handle_event() since we wait there for user response. When the
@ -118,15 +132,16 @@ static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
*/
struct fanotify_perm_event {
struct fanotify_event fae;
struct path path;
unsigned short response; /* userspace answer to the event */
unsigned short state; /* state of the event */
int fd; /* fd we passed to userspace for this event */
};
static inline struct fanotify_perm_event *
FANOTIFY_PE(struct fsnotify_event *fse)
FANOTIFY_PERM(struct fanotify_event *event)
{
return container_of(fse, struct fanotify_perm_event, fae.fse);
return container_of(event, struct fanotify_perm_event, fae);
}
static inline bool fanotify_is_perm_event(u32 mask)
@ -140,10 +155,18 @@ static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
return container_of(fse, struct fanotify_event, fse);
}
static inline bool fanotify_event_has_path(struct fanotify_event *event)
{
return event->type == FANOTIFY_EVENT_TYPE_PATH ||
event->type == FANOTIFY_EVENT_TYPE_PATH_PERM;
}
static inline struct path *fanotify_event_path(struct fanotify_event *event)
{
if (fanotify_event_has_path(event))
return &event->path;
if (event->type == FANOTIFY_EVENT_TYPE_PATH)
return &FANOTIFY_PE(event)->path;
else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
return &FANOTIFY_PERM(event)->path;
else
return NULL;
}

View File

@ -46,7 +46,8 @@
extern const struct fsnotify_ops fanotify_fsnotify_ops;
struct kmem_cache *fanotify_mark_cache __read_mostly;
struct kmem_cache *fanotify_event_cachep __read_mostly;
struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
struct kmem_cache *fanotify_path_event_cachep __read_mostly;
struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
#define FANOTIFY_EVENT_ALIGN 4
@ -64,16 +65,16 @@ static int fanotify_event_info_len(struct fanotify_event *event)
}
/*
* Get an fsnotify notification event if one exists and is small
* Get an fanotify notification event if one exists and is small
* enough to fit in "count". Return an error pointer if the count
* is not large enough. When permission event is dequeued, its state is
* updated accordingly.
*/
static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
static struct fanotify_event *get_one_event(struct fsnotify_group *group,
size_t count)
{
size_t event_size = FAN_EVENT_METADATA_LEN;
struct fsnotify_event *fsn_event = NULL;
struct fanotify_event *event = NULL;
pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
@ -87,15 +88,15 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
}
if (event_size > count) {
fsn_event = ERR_PTR(-EINVAL);
event = ERR_PTR(-EINVAL);
goto out;
}
fsn_event = fsnotify_remove_first_event(group);
if (fanotify_is_perm_event(FANOTIFY_E(fsn_event)->mask))
FANOTIFY_PE(fsn_event)->state = FAN_EVENT_REPORTED;
event = FANOTIFY_E(fsnotify_remove_first_event(group));
if (fanotify_is_perm_event(event->mask))
FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
out:
spin_unlock(&group->notification_lock);
return fsn_event;
return event;
}
static int create_fd(struct fsnotify_group *group, struct path *path,
@ -252,19 +253,16 @@ static int copy_fid_to_user(struct fanotify_event *event, char __user *buf)
}
static ssize_t copy_event_to_user(struct fsnotify_group *group,
struct fsnotify_event *fsn_event,
struct fanotify_event *event,
char __user *buf, size_t count)
{
struct fanotify_event_metadata metadata;
struct fanotify_event *event;
struct path *path;
struct path *path = fanotify_event_path(event);
struct file *f = NULL;
int ret, fd = FAN_NOFD;
pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
event = container_of(fsn_event, struct fanotify_event, fse);
path = fanotify_event_path(event);
metadata.event_len = FAN_EVENT_METADATA_LEN;
metadata.metadata_len = FAN_EVENT_METADATA_LEN;
metadata.vers = FANOTIFY_METADATA_VERSION;
@ -293,9 +291,9 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
goto out_close_fd;
if (fanotify_is_perm_event(event->mask))
FANOTIFY_PE(fsn_event)->fd = fd;
FANOTIFY_PERM(event)->fd = fd;
if (fanotify_event_has_path(event)) {
if (f) {
fd_install(fd, f);
} else if (fanotify_event_has_fid(event)) {
ret = copy_fid_to_user(event, buf + FAN_EVENT_METADATA_LEN);
@ -332,7 +330,7 @@ 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;
struct fanotify_event *event;
char __user *start;
int ret;
DEFINE_WAIT_FUNC(wait, woken_wake_function);
@ -344,13 +342,13 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
add_wait_queue(&group->notification_waitq, &wait);
while (1) {
kevent = get_one_event(group, count);
if (IS_ERR(kevent)) {
ret = PTR_ERR(kevent);
event = get_one_event(group, count);
if (IS_ERR(event)) {
ret = PTR_ERR(event);
break;
}
if (!kevent) {
if (!event) {
ret = -EAGAIN;
if (file->f_flags & O_NONBLOCK)
break;
@ -366,7 +364,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
continue;
}
ret = copy_event_to_user(group, kevent, buf, count);
ret = copy_event_to_user(group, event, buf, count);
if (unlikely(ret == -EOPENSTALE)) {
/*
* We cannot report events with stale fd so drop it.
@ -381,17 +379,17 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
* Permission events get queued to wait for response. Other
* events can be destroyed now.
*/
if (!fanotify_is_perm_event(FANOTIFY_E(kevent)->mask)) {
fsnotify_destroy_event(group, kevent);
if (!fanotify_is_perm_event(event->mask)) {
fsnotify_destroy_event(group, &event->fse);
} else {
if (ret <= 0) {
spin_lock(&group->notification_lock);
finish_permission_event(group,
FANOTIFY_PE(kevent), FAN_DENY);
FANOTIFY_PERM(event), FAN_DENY);
wake_up(&group->fanotify_data.access_waitq);
} else {
spin_lock(&group->notification_lock);
list_add_tail(&kevent->list,
list_add_tail(&event->fse.list,
&group->fanotify_data.access_list);
spin_unlock(&group->notification_lock);
}
@ -437,8 +435,6 @@ static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t
static int fanotify_release(struct inode *ignored, struct file *file)
{
struct fsnotify_group *group = file->private_data;
struct fanotify_perm_event *event;
struct fsnotify_event *fsn_event;
/*
* Stop new events from arriving in the notification queue. since
@ -453,6 +449,8 @@ static int fanotify_release(struct inode *ignored, struct file *file)
*/
spin_lock(&group->notification_lock);
while (!list_empty(&group->fanotify_data.access_list)) {
struct fanotify_perm_event *event;
event = list_first_entry(&group->fanotify_data.access_list,
struct fanotify_perm_event, fae.fse.list);
list_del_init(&event->fae.fse.list);
@ -466,12 +464,14 @@ static int fanotify_release(struct inode *ignored, struct file *file)
* response is consumed and fanotify_get_response() returns.
*/
while (!fsnotify_notify_queue_is_empty(group)) {
fsn_event = fsnotify_remove_first_event(group);
if (!(FANOTIFY_E(fsn_event)->mask & FANOTIFY_PERM_EVENTS)) {
struct fanotify_event *event;
event = FANOTIFY_E(fsnotify_remove_first_event(group));
if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
spin_unlock(&group->notification_lock);
fsnotify_destroy_event(group, fsn_event);
fsnotify_destroy_event(group, &event->fse);
} else {
finish_permission_event(group, FANOTIFY_PE(fsn_event),
finish_permission_event(group, FANOTIFY_PERM(event),
FAN_ALLOW);
}
spin_lock(&group->notification_lock);
@ -1136,7 +1136,10 @@ static int __init fanotify_user_setup(void)
fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
SLAB_PANIC|SLAB_ACCOUNT);
fanotify_event_cachep = KMEM_CACHE(fanotify_event, SLAB_PANIC);
fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
SLAB_PANIC);
fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
SLAB_PANIC);
if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
fanotify_perm_event_cachep =
KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);