Commit b6ff1070 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'fsnotify_for_v5.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs

Pull fsnotify updates from Jan Kara:
 "This implements the fanotify FAN_DIR_MODIFY event.

  This event reports the name in a directory under which a change
  happened and together with the directory filehandle and fstatat()
  allows reliable and efficient implementation of directory
  synchronization"

* tag 'fsnotify_for_v5.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs:
  fanotify: Fix the checks in fanotify_fsid_equal
  fanotify: report name info for FAN_DIR_MODIFY event
  fanotify: record name info for FAN_DIR_MODIFY event
  fanotify: Drop fanotify_event_has_fid()
  fanotify: prepare to report both parent and child fid's
  fanotify: send FAN_DIR_MODIFY event flavor with dir inode and name
  fanotify: divorce fanotify_path_event and fanotify_fid_event
  fanotify: Store fanotify handles differently
  fanotify: Simplify create_fd()
  fanotify: fix merging marks masks with FAN_ONDIR
  fanotify: merge duplicate events on parent and child
  fsnotify: replace inode pointer with an object id
  fsnotify: simplify arguments passing to fsnotify_parent()
  fsnotify: use helpers to access data by data_type
  fsnotify: funnel all dirent events through fsnotify_name()
  fsnotify: factor helpers fsnotify_dentry() and fsnotify_file()
  fsnotify: tidy up FS_ and FAN_ constants
parents 74e934ba 6def1a1d
...@@ -17,6 +17,59 @@ ...@@ -17,6 +17,59 @@
#include "fanotify.h" #include "fanotify.h"
static bool fanotify_path_equal(struct path *p1, struct path *p2)
{
return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
}
static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
__kernel_fsid_t *fsid2)
{
return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
}
static bool fanotify_fh_equal(struct fanotify_fh *fh1,
struct fanotify_fh *fh2)
{
if (fh1->type != fh2->type || fh1->len != fh2->len)
return false;
/* Do not merge events if we failed to encode fh */
if (fh1->type == FILEID_INVALID)
return false;
return !fh1->len ||
!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 fanotify_name_event_equal(struct fanotify_name_event *fne1,
struct fanotify_name_event *fne2)
{
/*
* Do not merge name events without dir fh.
* FAN_DIR_MODIFY does not encode object fh, so it may be empty.
*/
if (!fne1->dir_fh.len)
return false;
if (fne1->name_len != fne2->name_len ||
!fanotify_fh_equal(&fne1->dir_fh, &fne2->dir_fh))
return false;
return !memcmp(fne1->name, fne2->name, fne1->name_len);
}
static bool should_merge(struct fsnotify_event *old_fsn, static bool should_merge(struct fsnotify_event *old_fsn,
struct fsnotify_event *new_fsn) struct fsnotify_event *new_fsn)
{ {
...@@ -26,14 +79,15 @@ static bool should_merge(struct fsnotify_event *old_fsn, ...@@ -26,14 +79,15 @@ static bool should_merge(struct fsnotify_event *old_fsn,
old = FANOTIFY_E(old_fsn); old = FANOTIFY_E(old_fsn);
new = FANOTIFY_E(new_fsn); new = FANOTIFY_E(new_fsn);
if (old_fsn->inode != new_fsn->inode || old->pid != new->pid || if (old_fsn->objectid != new_fsn->objectid ||
old->fh_type != new->fh_type || old->fh_len != new->fh_len) old->type != new->type || old->pid != new->pid)
return false; return false;
if (fanotify_event_has_path(old)) { switch (old->type) {
return old->path.mnt == new->path.mnt && case FANOTIFY_EVENT_TYPE_PATH:
old->path.dentry == new->path.dentry; return fanotify_path_equal(fanotify_event_path(old),
} else if (fanotify_event_has_fid(old)) { fanotify_event_path(new));
case FANOTIFY_EVENT_TYPE_FID:
/* /*
* We want to merge many dirent events in the same dir (i.e. * We want to merge many dirent events in the same dir (i.e.
* creates/unlinks/renames), but we do not want to merge dirent * creates/unlinks/renames), but we do not want to merge dirent
...@@ -42,11 +96,18 @@ static bool should_merge(struct fsnotify_event *old_fsn, ...@@ -42,11 +96,18 @@ static bool should_merge(struct fsnotify_event *old_fsn,
* mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+ * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
* unlink pair or rmdir+create pair of events. * unlink pair or rmdir+create pair of events.
*/ */
return (old->mask & FS_ISDIR) == (new->mask & FS_ISDIR) && if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
fanotify_fid_equal(&old->fid, &new->fid, old->fh_len); return false;
return fanotify_fid_event_equal(FANOTIFY_FE(old),
FANOTIFY_FE(new));
case FANOTIFY_EVENT_TYPE_FID_NAME:
return fanotify_name_event_equal(FANOTIFY_NE(old),
FANOTIFY_NE(new));
default:
WARN_ON_ONCE(1);
} }
/* Do not merge events if we failed to encode fid */
return false; return false;
} }
...@@ -151,7 +212,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, ...@@ -151,7 +212,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
{ {
__u32 marks_mask = 0, marks_ignored_mask = 0; __u32 marks_mask = 0, marks_ignored_mask = 0;
__u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS; __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS;
const struct path *path = data; const struct path *path = fsnotify_data_path(data, data_type);
struct fsnotify_mark *mark; struct fsnotify_mark *mark;
int type; int type;
...@@ -160,7 +221,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, ...@@ -160,7 +221,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
/* Do we have path to open a file descriptor? */ /* Do we have path to open a file descriptor? */
if (data_type != FSNOTIFY_EVENT_PATH) if (!path)
return 0; return 0;
/* Path type events are only relevant for files and dirs */ /* Path type events are only relevant for files and dirs */
if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry)) if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
...@@ -171,6 +232,13 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, ...@@ -171,6 +232,13 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
if (!fsnotify_iter_should_report_type(iter_info, type)) if (!fsnotify_iter_should_report_type(iter_info, type))
continue; continue;
mark = iter_info->marks[type]; mark = iter_info->marks[type];
/*
* If the event is on dir and this mark doesn't care about
* events on dir, don't send it!
*/
if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
continue;
/* /*
* If the event is for a child and this mark doesn't care about * If the event is for a child and this mark doesn't care about
* events on a child, don't send it! * events on a child, don't send it!
...@@ -187,9 +255,9 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, ...@@ -187,9 +255,9 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
test_mask = event_mask & marks_mask & ~marks_ignored_mask; test_mask = event_mask & marks_mask & ~marks_ignored_mask;
/* /*
* dirent modification events (create/delete/move) do not carry the * For dirent modification events (create/delete/move) that do not carry
* child entry name/inode information. Instead, we report FAN_ONDIR * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
* for mkdir/rmdir so user can differentiate them from creat/unlink. * so user can differentiate them from creat/unlink.
* *
* For backward compatibility and consistency, do not report FAN_ONDIR * For backward compatibility and consistency, do not report FAN_ONDIR
* to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
...@@ -203,22 +271,20 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, ...@@ -203,22 +271,20 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
user_mask &= ~FAN_ONDIR; user_mask &= ~FAN_ONDIR;
} }
if (event_mask & FS_ISDIR &&
!(marks_mask & FS_ISDIR & ~marks_ignored_mask))
return 0;
return test_mask & user_mask; return test_mask & user_mask;
} }
static int fanotify_encode_fid(struct fanotify_event *event, static void fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
struct inode *inode, gfp_t gfp, gfp_t gfp)
__kernel_fsid_t *fsid)
{ {
struct fanotify_fid *fid = &event->fid; int dwords, type, bytes = 0;
int dwords, bytes = 0; char *ext_buf = NULL;
int err, type; void *buf = fh->buf;
int err;
if (!inode)
goto out;
fid->ext_fh = NULL;
dwords = 0; dwords = 0;
err = -ENOENT; err = -ENOENT;
type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL); type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
...@@ -229,31 +295,33 @@ static int fanotify_encode_fid(struct fanotify_event *event, ...@@ -229,31 +295,33 @@ static int fanotify_encode_fid(struct fanotify_event *event,
if (bytes > FANOTIFY_INLINE_FH_LEN) { if (bytes > FANOTIFY_INLINE_FH_LEN) {
/* Treat failure to allocate fh as failure to allocate event */ /* Treat failure to allocate fh as failure to allocate event */
err = -ENOMEM; err = -ENOMEM;
fid->ext_fh = kmalloc(bytes, gfp); ext_buf = kmalloc(bytes, gfp);
if (!fid->ext_fh) if (!ext_buf)
goto out_err; goto out_err;
*fanotify_fh_ext_buf_ptr(fh) = ext_buf;
buf = ext_buf;
} }
type = exportfs_encode_inode_fh(inode, fanotify_fid_fh(fid, bytes), type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
&dwords, NULL);
err = -EINVAL; err = -EINVAL;
if (!type || type == FILEID_INVALID || bytes != dwords << 2) if (!type || type == FILEID_INVALID || bytes != dwords << 2)
goto out_err; goto out_err;
fid->fsid = *fsid; fh->type = type;
event->fh_len = bytes; fh->len = bytes;
return type; return;
out_err: out_err:
pr_warn_ratelimited("fanotify: failed to encode fid (fsid=%x.%x, " pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
"type=%d, bytes=%d, err=%i)\n", type, bytes, err);
fsid->val[0], fsid->val[1], type, bytes, err); kfree(ext_buf);
kfree(fid->ext_fh); *fanotify_fh_ext_buf_ptr(fh) = NULL;
fid->ext_fh = NULL; out:
event->fh_len = 0; /* Report the event without a file identifier on encode error */
fh->type = FILEID_INVALID;
return FILEID_INVALID; fh->len = 0;
} }
/* /*
...@@ -269,21 +337,22 @@ static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask, ...@@ -269,21 +337,22 @@ static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask,
{ {
if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
return to_tell; return to_tell;
else if (data_type == FSNOTIFY_EVENT_INODE)
return (struct inode *)data; return (struct inode *)fsnotify_data_inode(data, data_type);
else if (data_type == FSNOTIFY_EVENT_PATH)
return d_inode(((struct path *)data)->dentry);
return NULL;
} }
struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
struct inode *inode, u32 mask, struct inode *inode, u32 mask,
const void *data, int data_type, const void *data, int data_type,
const struct qstr *file_name,
__kernel_fsid_t *fsid) __kernel_fsid_t *fsid)
{ {
struct fanotify_event *event = NULL; struct fanotify_event *event = NULL;
struct fanotify_fid_event *ffe = NULL;
struct fanotify_name_event *fne = NULL;
gfp_t gfp = GFP_KERNEL_ACCOUNT; gfp_t gfp = GFP_KERNEL_ACCOUNT;
struct inode *id = fanotify_fid_inode(inode, mask, data, data_type); struct inode *id = fanotify_fid_inode(inode, mask, data, data_type);
const struct path *path = fsnotify_data_path(data, data_type);
/* /*
* For queues with unlimited length lost events are not expected and * For queues with unlimited length lost events are not expected and
...@@ -305,33 +374,81 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, ...@@ -305,33 +374,81 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
if (!pevent) if (!pevent)
goto out; goto out;
event = &pevent->fae; event = &pevent->fae;
event->type = FANOTIFY_EVENT_TYPE_PATH_PERM;
pevent->response = 0; pevent->response = 0;
pevent->state = FAN_EVENT_INIT; pevent->state = FAN_EVENT_INIT;
goto init; goto init;
} }
event = kmem_cache_alloc(fanotify_event_cachep, gfp);
if (!event) /*
* For FAN_DIR_MODIFY event, we report the fid of the directory and
* the name of the modified entry.
* Allocate an fanotify_name_event struct and copy the name.
*/
if (mask & FAN_DIR_MODIFY && !(WARN_ON_ONCE(!file_name))) {
fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp);
if (!fne)
goto out;
event = &fne->fae;
event->type = FANOTIFY_EVENT_TYPE_FID_NAME;
fne->name_len = file_name->len;
strcpy(fne->name, file_name->name);
goto init;
}
if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
if (!ffe)
goto out; goto out;
init: __maybe_unused
fsnotify_init_event(&event->fse, inode); 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:
/*
* Use the victim inode instead of the watching inode as the id for
* event queue, so event reported on parent is merged with event
* reported on child when both directory and child watches exist.
*/
fsnotify_init_event(&event->fse, (unsigned long)id);
event->mask = mask; event->mask = mask;
if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
event->pid = get_pid(task_pid(current)); event->pid = get_pid(task_pid(current));
else else
event->pid = get_pid(task_tgid(current)); event->pid = get_pid(task_tgid(current));
event->fh_len = 0;
if (id && FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { if (fsid && fanotify_event_fsid(event))
/* Report the event without a file identifier on encode error */ *fanotify_event_fsid(event) = *fsid;
event->fh_type = fanotify_encode_fid(event, id, gfp, fsid);
} else if (data_type == FSNOTIFY_EVENT_PATH) { if (fanotify_event_object_fh(event))
event->fh_type = FILEID_ROOT; fanotify_encode_fh(fanotify_event_object_fh(event), id, gfp);
event->path = *((struct path *)data);
path_get(&event->path); if (fanotify_event_dir_fh(event))
fanotify_encode_fh(fanotify_event_dir_fh(event), id, gfp);
if (fanotify_event_has_path(event)) {
struct path *p = fanotify_event_path(event);
if (path) {
*p = *path;
path_get(path);
} else { } else {
event->fh_type = FILEID_INVALID; p->mnt = NULL;
event->path.mnt = NULL; p->dentry = NULL;
event->path.dentry = NULL; }
} }
out: out:
memalloc_unuse_memcg(); memalloc_unuse_memcg();
...@@ -392,6 +509,7 @@ static int fanotify_handle_event(struct fsnotify_group *group, ...@@ -392,6 +509,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM); BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
BUILD_BUG_ON(FAN_CREATE != FS_CREATE); BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
BUILD_BUG_ON(FAN_DELETE != FS_DELETE); BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
BUILD_BUG_ON(FAN_DIR_MODIFY != FS_DIR_MODIFY);
BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF); BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF); BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
...@@ -402,7 +520,7 @@ static int fanotify_handle_event(struct fsnotify_group *group, ...@@ -402,7 +520,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC); BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19); BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 20);
mask = fanotify_group_event_mask(group, iter_info, mask, data, mask = fanotify_group_event_mask(group, iter_info, mask, data,
data_type); data_type);
...@@ -429,7 +547,7 @@ static int fanotify_handle_event(struct fsnotify_group *group, ...@@ -429,7 +547,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
} }
event = fanotify_alloc_event(group, inode, mask, data, data_type, event = fanotify_alloc_event(group, inode, mask, data, data_type,
&fsid); file_name, &fsid);
ret = -ENOMEM; ret = -ENOMEM;
if (unlikely(!event)) { if (unlikely(!event)) {
/* /*
...@@ -451,7 +569,7 @@ static int fanotify_handle_event(struct fsnotify_group *group, ...@@ -451,7 +569,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
ret = 0; ret = 0;
} else if (fanotify_is_perm_event(mask)) { } 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); iter_info);
} }
finish: finish:
...@@ -470,22 +588,58 @@ static void fanotify_free_group_priv(struct fsnotify_group *group) ...@@ -470,22 +588,58 @@ static void fanotify_free_group_priv(struct fsnotify_group *group)
free_uid(user); 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_name_event(struct fanotify_event *event)
{
struct fanotify_name_event *fne = FANOTIFY_NE(event);
if (fanotify_fh_has_ext_buf(&fne->dir_fh))
kfree(fanotify_fh_ext_buf(&fne->dir_fh));
kfree(fne);
}
static void fanotify_free_event(struct fsnotify_event *fsn_event) static void fanotify_free_event(struct fsnotify_event *fsn_event)
{ {
struct fanotify_event *event; struct fanotify_event *event;
event = FANOTIFY_E(fsn_event); event = FANOTIFY_E(fsn_event);
if (fanotify_event_has_path(event))
path_put(&event->path);
else if (fanotify_event_has_ext_fh(event))
kfree(event->fid.ext_fh);
put_pid(event->pid); put_pid(event->pid);
if (fanotify_is_perm_event(event->mask)) { switch (event->type) {
kmem_cache_free(fanotify_perm_event_cachep, case FANOTIFY_EVENT_TYPE_PATH:
FANOTIFY_PE(fsn_event)); fanotify_free_path_event(event);
return; break;
case FANOTIFY_EVENT_TYPE_PATH_PERM:
fanotify_free_perm_event(event);
break;
case FANOTIFY_EVENT_TYPE_FID:
fanotify_free_fid_event(event);
break;
case FANOTIFY_EVENT_TYPE_FID_NAME:
fanotify_free_name_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) static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
#include <linux/exportfs.h> #include <linux/exportfs.h>
extern struct kmem_cache *fanotify_mark_cache; 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; extern struct kmem_cache *fanotify_perm_event_cachep;
/* Possible states of the permission event */ /* Possible states of the permission event */
...@@ -18,94 +19,140 @@ enum { ...@@ -18,94 +19,140 @@ enum {
/* /*
* 3 dwords are sufficient for most local fs (64bit ino, 32bit generation). * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
* For 32bit arch, fid increases the size of fanotify_event by 12 bytes and * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
* fh_* fields increase the size of fanotify_event by another 4 bytes. * stored in either the first or last 2 dwords.
* For 64bit arch, fid increases the size of fanotify_fid by 8 bytes and
* fh_* fields are packed in a hole after mask.
*/ */
#if BITS_PER_LONG == 32
#define FANOTIFY_INLINE_FH_LEN (3 << 2) #define FANOTIFY_INLINE_FH_LEN (3 << 2)
#else
#define FANOTIFY_INLINE_FH_LEN (4 << 2)
#endif
struct fanotify_fid { struct fanotify_fh {
__kernel_fsid_t fsid; unsigned char buf[FANOTIFY_INLINE_FH_LEN];
union { u8 type;
unsigned char fh[FANOTIFY_INLINE_FH_LEN]; u8 len;
unsigned char *ext_fh; } __aligned(4);
};
};
static inline void *fanotify_fid_fh(struct fanotify_fid *fid, static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
unsigned int fh_len)
{ {
return fh_len <= FANOTIFY_INLINE_FH_LEN ? fid->fh : fid->ext_fh; return fh->len > FANOTIFY_INLINE_FH_LEN;
} }
static inline bool fanotify_fid_equal(struct fanotify_fid *fid1, static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
struct fanotify_fid *fid2,
unsigned int fh_len)
{ {
return fid1->fsid.val[0] == fid2->fsid.val[0] && BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
fid1->fsid.val[1] == fid2->fsid.val[1] && FANOTIFY_INLINE_FH_LEN);
!memcmp(fanotify_fid_fh(fid1, fh_len), return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
fanotify_fid_fh(fid2, fh_len), fh_len); }
static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
{
return *fanotify_fh_ext_buf_ptr(fh);
}
static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
{
return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
} }
/* /*
* 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 * 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, /* fixed length */
FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
FANOTIFY_EVENT_TYPE_PATH,
FANOTIFY_EVENT_TYPE_PATH_PERM,
};
struct fanotify_event { struct fanotify_event {
struct fsnotify_event fse; struct fsnotify_event fse;
u32 mask; u32 mask;
/* enum fanotify_event_type type;
* Those fields are outside fanotify_fid to pack fanotify_event nicely
* on 64bit arch and to use fh_type as an indication of whether path
* or fid are used in the union:
* FILEID_ROOT (0) for path, > 0 for fid, FILEID_INVALID for neither.
*/
u8 fh_type;
u8 fh_len;
u16 pad;
union {
/*
* We hold ref to this path so it may be dereferenced at any
* point during this object's lifetime
*/
struct path path;
/*
* 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.
*/
struct fanotify_fid fid;
};
struct pid *pid; 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 container_of(event, struct fanotify_fid_event, fae);
}
struct fanotify_name_event {
struct fanotify_event fae;
__kernel_fsid_t fsid;
struct fanotify_fh dir_fh;
u8 name_len;
char name[0];
};
static inline struct fanotify_name_event *
FANOTIFY_NE(struct fanotify_event *event)
{ {
return event->fh_type == FILEID_ROOT; return container_of(event, struct fanotify_name_event, fae);
} }
static inline bool fanotify_event_has_fid(struct fanotify_event *event) static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
{ {
return event->fh_type != FILEID_ROOT && if (event->type == FANOTIFY_EVENT_TYPE_FID)
event->fh_type != FILEID_INVALID; return &FANOTIFY_FE(event)->fsid;
else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
return &FANOTIFY_NE(event)->fsid;
else
return NULL;
} }
static inline bool fanotify_event_has_ext_fh(struct fanotify_event *event) static inline struct fanotify_fh *fanotify_event_object_fh(
struct fanotify_event *event)
{ {
return fanotify_event_has_fid(event) && if (event->type == FANOTIFY_EVENT_TYPE_FID)
event->fh_len > FANOTIFY_INLINE_FH_LEN; return &FANOTIFY_FE(event)->object_fh;
else
return NULL;
} }
static inline void *fanotify_event_fh(struct fanotify_event *event) static inline struct fanotify_fh *fanotify_event_dir_fh(
struct fanotify_event *event)
{ {
return fanotify_fid_fh(&event->fid, event->fh_len); if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
return &FANOTIFY_NE(event)->dir_fh;
else
return NULL;
}
static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
{
struct fanotify_fh *fh = fanotify_event_object_fh(event);
return fh ? fh->len : 0;
}
static inline bool fanotify_event_has_name(struct fanotify_event *event)
{
return event->type == FANOTIFY_EVENT_TYPE_FID_NAME;
}
static inline int fanotify_event_name_len(struct fanotify_event *event)
{
return fanotify_event_has_name(event) ?
FANOTIFY_NE(event)->name_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);
} }
/* /*
...@@ -117,15 +164,16 @@ static inline void *fanotify_event_fh(struct fanotify_event *event) ...@@ -117,15 +164,16 @@ static inline void *fanotify_event_fh(struct fanotify_event *event)
*/ */
struct fanotify_perm_event { struct fanotify_perm_event {
struct fanotify_event fae; struct fanotify_event fae;
struct path path;
unsigned short response; /* userspace answer to the event */ unsigned short response; /* userspace answer to the event */
unsigned short state; /* state of the event */ unsigned short state; /* state of the event */
int fd; /* fd we passed to userspace for this event */ int fd; /* fd we passed to userspace for this event */
}; };
static inline struct fanotify_perm_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) static inline bool fanotify_is_perm_event(u32 mask)
...@@ -139,7 +187,24 @@ static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse) ...@@ -139,7 +187,24 @@ static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
return container_of(fse, struct fanotify_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 (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;
}
struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
struct inode *inode, u32 mask, struct inode *inode, u32 mask,
const void *data, int data_type, const void *data, int data_type,
const struct qstr *file_name,
__kernel_fsid_t *fsid); __kernel_fsid_t *fsid);
...@@ -46,32 +46,53 @@ ...@@ -46,32 +46,53 @@
extern const struct fsnotify_ops fanotify_fsnotify_ops; extern const struct fsnotify_ops fanotify_fsnotify_ops;
struct kmem_cache *fanotify_mark_cache __read_mostly; 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; struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
#define FANOTIFY_EVENT_ALIGN 4 #define FANOTIFY_EVENT_ALIGN 4
#define FANOTIFY_INFO_HDR_LEN \
(sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
static int fanotify_fid_info_len(int fh_len, int name_len)
{
int info_len = fh_len;
if (name_len)
info_len += name_len + 1;
return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
}
static int fanotify_event_info_len(struct fanotify_event *event) static int fanotify_event_info_len(struct fanotify_event *event)
{ {
if (!fanotify_event_has_fid(event)) int info_len = 0;
return 0; int fh_len = fanotify_event_object_fh_len(event);
if (fh_len)
info_len += fanotify_fid_info_len(fh_len, 0);
return roundup(sizeof(struct fanotify_event_info_fid) + if (fanotify_event_name_len(event)) {
sizeof(struct file_handle) + event->fh_len, struct fanotify_name_event *fne = FANOTIFY_NE(event);
FANOTIFY_EVENT_ALIGN);
info_len += fanotify_fid_info_len(fne->dir_fh.len,
fne->name_len);
}
return info_len;
} }
/* /*
* 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 * enough to fit in "count". Return an error pointer if the count
* is not large enough. When permission event is dequeued, its state is * is not large enough. When permission event is dequeued, its state is
* updated accordingly. * 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 count)
{ {
size_t event_size = FAN_EVENT_METADATA_LEN; 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); pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
...@@ -85,26 +106,23 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group, ...@@ -85,26 +106,23 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
} }
if (event_size > count) { if (event_size > count) {
fsn_event = ERR_PTR(-EINVAL); event = ERR_PTR(-EINVAL);
goto out; goto out;
} }
fsn_event = fsnotify_remove_first_event(group); event = FANOTIFY_E(fsnotify_remove_first_event(group));
if (fanotify_is_perm_event(FANOTIFY_E(fsn_event)->mask)) if (fanotify_is_perm_event(event->mask))
FANOTIFY_PE(fsn_event)->state = FAN_EVENT_REPORTED; FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
out: out:
spin_unlock(&group->notification_lock); spin_unlock(&group->notification_lock);
return fsn_event; return event;
} }
static int create_fd(struct fsnotify_group *group, static int create_fd(struct fsnotify_group *group, struct path *path,
struct fanotify_event *event,
struct file **file) struct file **file)
{ {
int client_fd; int client_fd;
struct file *new_file; struct file *new_file;
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
if (client_fd < 0) if (client_fd < 0)
return client_fd; return client_fd;
...@@ -113,14 +131,9 @@ static int create_fd(struct fsnotify_group *group, ...@@ -113,14 +131,9 @@ static int create_fd(struct fsnotify_group *group,
* we need a new file handle for the userspace program so it can read even if it was * we need a new file handle for the userspace program so it can read even if it was
* originally opened O_WRONLY. * originally opened O_WRONLY.
*/ */
/* it's possible this event was an overflow event. in that case dentry and mnt new_file = dentry_open(path,
* are NULL; That's fine, just don't call dentry open */
if (event->path.dentry && event->path.mnt)
new_file = dentry_open(&event->path,
group->fanotify_data.f_flags | FMODE_NONOTIFY, group->fanotify_data.f_flags | FMODE_NONOTIFY,
current_cred()); current_cred());
else
new_file = ERR_PTR(-EOVERFLOW);
if (IS_ERR(new_file)) { if (IS_ERR(new_file)) {
/* /*
* we still send an event even if we can't open the file. this * we still send an event even if we can't open the file. this
...@@ -204,83 +217,111 @@ static int process_access_response(struct fsnotify_group *group, ...@@ -204,83 +217,111 @@ static int process_access_response(struct fsnotify_group *group,
return -ENOENT; return -ENOENT;
} }
static int copy_fid_to_user(struct fanotify_event *event, char __user *buf) static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
const char *name, size_t name_len,
char __user *buf, size_t count)
{ {
struct fanotify_event_info_fid info = { }; struct fanotify_event_info_fid info = { };
struct file_handle handle = { }; struct file_handle handle = { };
unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh; unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
size_t fh_len = event->fh_len; size_t fh_len = fh ? fh->len : 0;
size_t len = fanotify_event_info_len(event); size_t info_len = fanotify_fid_info_len(fh_len, name_len);
size_t len = info_len;
if (!len) pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
__func__, fh_len, name_len, info_len, count);
if (!fh_len || (name && !name_len))
return 0; return 0;
if (WARN_ON_ONCE(len < sizeof(info) + sizeof(handle) + fh_len)) if (WARN_ON_ONCE(len < sizeof(info) || len > count))
return -EFAULT; return -EFAULT;
/* Copy event info fid header followed by vaiable sized file handle */ /*
info.hdr.info_type = FAN_EVENT_INFO_TYPE_FID; * Copy event info fid header followed by variable sized file handle
* and optionally followed by variable sized filename.
*/
info.hdr.info_type = name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
FAN_EVENT_INFO_TYPE_FID;
info.hdr.len = len; info.hdr.len = len;
info.fsid = event->fid.fsid; info.fsid = *fsid;
if (copy_to_user(buf, &info, sizeof(info))) if (copy_to_user(buf, &info, sizeof(info)))
return -EFAULT; return -EFAULT;
buf += sizeof(info); buf += sizeof(info);
len -= sizeof(info); len -= sizeof(info);
handle.handle_type = event->fh_type; if (WARN_ON_ONCE(len < sizeof(handle)))
return -EFAULT;
handle.handle_type = fh->type;
handle.handle_bytes = fh_len; handle.handle_bytes = fh_len;
if (copy_to_user(buf, &handle, sizeof(handle))) if (copy_to_user(buf, &handle, sizeof(handle)))
return -EFAULT; return -EFAULT;
buf += sizeof(handle); buf += sizeof(handle);
len -= sizeof(handle); len -= sizeof(handle);
if (WARN_ON_ONCE(len < fh_len))
return -EFAULT;
/* /*
* For an inline fh, copy through stack to exclude the copy from * For an inline fh and inline file name, copy through stack to exclude
* usercopy hardening protections. * the copy from usercopy hardening protections.
*/ */
fh = fanotify_event_fh(event); fh_buf = fanotify_fh_buf(fh);
if (fh_len <= FANOTIFY_INLINE_FH_LEN) { if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
memcpy(bounce, fh, fh_len); memcpy(bounce, fh_buf, fh_len);
fh = bounce; fh_buf = bounce;
} }
if (copy_to_user(buf, fh, fh_len)) if (copy_to_user(buf, fh_buf, fh_len))
return -EFAULT; return -EFAULT;
/* Pad with 0's */
buf += fh_len; buf += fh_len;
len -= fh_len; len -= fh_len;
if (name_len) {
/* Copy the filename with terminating null */
name_len++;
if (WARN_ON_ONCE(len < name_len))
return -EFAULT;
if (copy_to_user(buf, name, name_len))
return -EFAULT;
buf += name_len;
len -= name_len;
}
/* Pad with 0's */
WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN); WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
if (len > 0 && clear_user(buf, len)) if (len > 0 && clear_user(buf, len))
return -EFAULT; return -EFAULT;
return 0; return info_len;
} }
static ssize_t copy_event_to_user(struct fsnotify_group *group, 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) char __user *buf, size_t count)
{ {
struct fanotify_event_metadata metadata; struct fanotify_event_metadata metadata;
struct fanotify_event *event; struct path *path = fanotify_event_path(event);
struct file *f = NULL; struct file *f = NULL;
int ret, fd = FAN_NOFD; 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); metadata.event_len = FAN_EVENT_METADATA_LEN +
metadata.event_len = FAN_EVENT_METADATA_LEN; fanotify_event_info_len(event);
metadata.metadata_len = FAN_EVENT_METADATA_LEN; metadata.metadata_len = FAN_EVENT_METADATA_LEN;
metadata.vers = FANOTIFY_METADATA_VERSION; metadata.vers = FANOTIFY_METADATA_VERSION;
metadata.reserved = 0; metadata.reserved = 0;
metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS; metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
metadata.pid = pid_vnr(event->pid); metadata.pid = pid_vnr(event->pid);
if (fanotify_event_has_path(event)) { if (path && path->mnt && path->dentry) {
fd = create_fd(group, event, &f); fd = create_fd(group, path, &f);
if (fd < 0) if (fd < 0)
return fd; return fd;
} else if (fanotify_event_has_fid(event)) {
metadata.event_len += fanotify_event_info_len(event);
} }
metadata.fd = fd; metadata.fd = fd;
...@@ -295,15 +336,39 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, ...@@ -295,15 +336,39 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN)) if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
goto out_close_fd; goto out_close_fd;
buf += FAN_EVENT_METADATA_LEN;
count -= FAN_EVENT_METADATA_LEN;
if (fanotify_is_perm_event(event->mask)) 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); fd_install(fd, f);
} else if (fanotify_event_has_fid(event)) {
ret = copy_fid_to_user(event, buf + FAN_EVENT_METADATA_LEN); /* Event info records order is: dir fid + name, child fid */
if (fanotify_event_name_len(event)) {
struct fanotify_name_event *fne = FANOTIFY_NE(event);
ret = copy_info_to_user(fanotify_event_fsid(event),
fanotify_event_dir_fh(event),
fne->name, fne->name_len,
buf, count);
if (ret < 0)
return ret;
buf += ret;
count -= ret;
}
if (fanotify_event_object_fh_len(event)) {
ret = copy_info_to_user(fanotify_event_fsid(event),
fanotify_event_object_fh(event),
NULL, 0, buf, count);
if (ret < 0) if (ret < 0)
return ret; return ret;
buf += ret;
count -= ret;
} }
return metadata.event_len; return metadata.event_len;
...@@ -335,7 +400,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, ...@@ -335,7 +400,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
size_t count, loff_t *pos) size_t count, loff_t *pos)
{ {
struct fsnotify_group *group; struct fsnotify_group *group;
struct fsnotify_event *kevent; struct fanotify_event *event;
char __user *start; char __user *start;
int ret; int ret;
DEFINE_WAIT_FUNC(wait, woken_wake_function); DEFINE_WAIT_FUNC(wait, woken_wake_function);
...@@ -347,13 +412,13 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, ...@@ -347,13 +412,13 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
add_wait_queue(&group->notification_waitq, &wait); add_wait_queue(&group->notification_waitq, &wait);
while (1) { while (1) {
kevent = get_one_event(group, count); event = get_one_event(group, count);
if (IS_ERR(kevent)) { if (IS_ERR(event)) {
ret = PTR_ERR(kevent); ret = PTR_ERR(event);
break; break;
} }
if (!kevent) { if (!event) {
ret = -EAGAIN; ret = -EAGAIN;
if (file->f_flags & O_NONBLOCK) if (file->f_flags & O_NONBLOCK)
break; break;
...@@ -369,7 +434,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, ...@@ -369,7 +434,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
continue; continue;
} }
ret = copy_event_to_user(group, kevent, buf, count); ret = copy_event_to_user(group, event, buf, count);
if (unlikely(ret == -EOPENSTALE)) { if (unlikely(ret == -EOPENSTALE)) {
/* /*
* We cannot report events with stale fd so drop it. * We cannot report events with stale fd so drop it.
...@@ -384,17 +449,17 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, ...@@ -384,17 +449,17 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
* Permission events get queued to wait for response. Other * Permission events get queued to wait for response. Other
* events can be destroyed now. * events can be destroyed now.
*/ */
if (!fanotify_is_perm_event(FANOTIFY_E(kevent)->mask)) { if (!fanotify_is_perm_event(event->mask)) {
fsnotify_destroy_event(group, kevent); fsnotify_destroy_event(group, &event->fse);
} else { } else {
if (ret <= 0) { if (ret <= 0) {
spin_lock(&group->notification_lock); spin_lock(&group->notification_lock);
finish_permission_event(group, finish_permission_event(group,
FANOTIFY_PE(kevent), FAN_DENY); FANOTIFY_PERM(event), FAN_DENY);
wake_up(&group->fanotify_data.access_waitq); wake_up(&group->fanotify_data.access_waitq);
} else { } else {
spin_lock(&group->notification_lock); spin_lock(&group->notification_lock);
list_add_tail(&kevent->list, list_add_tail(&event->fse.list,
&group->fanotify_data.access_list); &group->fanotify_data.access_list);
spin_unlock(&group->notification_lock); spin_unlock(&group->notification_lock);
} }
...@@ -440,8 +505,6 @@ static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t ...@@ -440,8 +505,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) static int fanotify_release(struct inode *ignored, struct file *file)
{ {
struct fsnotify_group *group = file->private_data; 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 * Stop new events from arriving in the notification queue. since
...@@ -456,6 +519,8 @@ static int fanotify_release(struct inode *ignored, struct file *file) ...@@ -456,6 +519,8 @@ static int fanotify_release(struct inode *ignored, struct file *file)
*/ */
spin_lock(&group->notification_lock); spin_lock(&group->notification_lock);
while (!list_empty(&group->fanotify_data.access_list)) { while (!list_empty(&group->fanotify_data.access_list)) {
struct fanotify_perm_event *event;
event = list_first_entry(&group->fanotify_data.access_list, event = list_first_entry(&group->fanotify_data.access_list,
struct fanotify_perm_event, fae.fse.list); struct fanotify_perm_event, fae.fse.list);
list_del_init(&event->fae.fse.list); list_del_init(&event->fae.fse.list);
...@@ -469,12 +534,14 @@ static int fanotify_release(struct inode *ignored, struct file *file) ...@@ -469,12 +534,14 @@ static int fanotify_release(struct inode *ignored, struct file *file)
* response is consumed and fanotify_get_response() returns. * response is consumed and fanotify_get_response() returns.
*/ */
while (!fsnotify_notify_queue_is_empty(group)) { while (!fsnotify_notify_queue_is_empty(group)) {
fsn_event = fsnotify_remove_first_event(group); struct fanotify_event *event;
if (!(FANOTIFY_E(fsn_event)->mask & FANOTIFY_PERM_EVENTS)) {
event = FANOTIFY_E(fsnotify_remove_first_event(group));
if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
spin_unlock(&group->notification_lock); spin_unlock(&group->notification_lock);
fsnotify_destroy_event(group, fsn_event); fsnotify_destroy_event(group, &event->fse);
} else { } else {
finish_permission_event(group, FANOTIFY_PE(fsn_event), finish_permission_event(group, FANOTIFY_PERM(event),
FAN_ALLOW); FAN_ALLOW);
} }
spin_lock(&group->notification_lock); spin_lock(&group->notification_lock);
...@@ -824,7 +891,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) ...@@ -824,7 +891,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
group->memcg = get_mem_cgroup_from_mm(current->mm); group->memcg = get_mem_cgroup_from_mm(current->mm);
oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL, oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL,
FSNOTIFY_EVENT_NONE, NULL); FSNOTIFY_EVENT_NONE, NULL, NULL);
if (unlikely(!oevent)) { if (unlikely(!oevent)) {
fd = -ENOMEM; fd = -ENOMEM;
goto out_destroy_group; goto out_destroy_group;
...@@ -1139,7 +1206,10 @@ static int __init fanotify_user_setup(void) ...@@ -1139,7 +1206,10 @@ static int __init fanotify_user_setup(void)
fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
SLAB_PANIC|SLAB_ACCOUNT); 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)) { if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
fanotify_perm_event_cachep = fanotify_perm_event_cachep =
KMEM_CACHE(fanotify_perm_event, SLAB_PANIC); KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
......
...@@ -143,15 +143,13 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode) ...@@ -143,15 +143,13 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)
} }
/* Notify this dentry's parent about a child's events. */ /* Notify this dentry's parent about a child's events. */
int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask) int fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
int data_type)
{ {
struct dentry *parent; struct dentry *parent;
struct inode *p_inode; struct inode *p_inode;
int ret = 0; int ret = 0;
if (!dentry)
dentry = path->dentry;
if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
return 0; return 0;
...@@ -168,12 +166,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask ...@@ -168,12 +166,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask
mask |= FS_EVENT_ON_CHILD; mask |= FS_EVENT_ON_CHILD;
take_dentry_name_snapshot(&name, dentry); take_dentry_name_snapshot(&name, dentry);
if (path) ret = fsnotify(p_inode, mask, data, data_type, &name.name, 0);
ret = fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
&name.name, 0);
else
ret = fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE,
&name.name, 0);
release_dentry_name_snapshot(&name); release_dentry_name_snapshot(&name);
} }
...@@ -181,7 +174,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask ...@@ -181,7 +174,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(__fsnotify_parent); EXPORT_SYMBOL_GPL(fsnotify_parent);
static int send_to_group(struct inode *to_tell, static int send_to_group(struct inode *to_tell,
__u32 mask, const void *data, __u32 mask, const void *data,
...@@ -318,6 +311,7 @@ static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) ...@@ -318,6 +311,7 @@ static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is, int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is,
const struct qstr *file_name, u32 cookie) const struct qstr *file_name, u32 cookie)
{ {
const struct path *path = fsnotify_data_path(data, data_is);
struct fsnotify_iter_info iter_info = {}; struct fsnotify_iter_info iter_info = {};
struct super_block *sb = to_tell->i_sb; struct super_block *sb = to_tell->i_sb;
struct mount *mnt = NULL; struct mount *mnt = NULL;
...@@ -325,8 +319,8 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is, ...@@ -325,8 +319,8 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is,
int ret = 0; int ret = 0;
__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS); __u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
if (data_is == FSNOTIFY_EVENT_PATH) { if (path) {
mnt = real_mount(((const struct path *)data)->mnt); mnt = real_mount(path->mnt);
mnt_or_sb_mask |= mnt->mnt_fsnotify_mask; mnt_or_sb_mask |= mnt->mnt_fsnotify_mask;
} }
/* An event "on child" is not intended for a mount/sb mark */ /* An event "on child" is not intended for a mount/sb mark */
...@@ -389,7 +383,7 @@ static __init int fsnotify_init(void) ...@@ -389,7 +383,7 @@ static __init int fsnotify_init(void)
{ {
int ret; int ret;
BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25); BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 26);
ret = init_srcu_struct(&fsnotify_mark_srcu); ret = init_srcu_struct(&fsnotify_mark_srcu);
if (ret) if (ret)
......
...@@ -39,7 +39,7 @@ static bool event_compare(struct fsnotify_event *old_fsn, ...@@ -39,7 +39,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,
if (old->mask & FS_IN_IGNORED) if (old->mask & FS_IN_IGNORED)
return false; return false;
if ((old->mask == new->mask) && if ((old->mask == new->mask) &&
(old_fsn->inode == new_fsn->inode) && (old_fsn->objectid == new_fsn->objectid) &&
(old->name_len == new->name_len) && (old->name_len == new->name_len) &&
(!old->name_len || !strcmp(old->name, new->name))) (!old->name_len || !strcmp(old->name, new->name)))
return true; return true;
...@@ -61,6 +61,7 @@ int inotify_handle_event(struct fsnotify_group *group, ...@@ -61,6 +61,7 @@ int inotify_handle_event(struct fsnotify_group *group,
const struct qstr *file_name, u32 cookie, const struct qstr *file_name, u32 cookie,
struct fsnotify_iter_info *iter_info) struct fsnotify_iter_info *iter_info)
{ {
const struct path *path = fsnotify_data_path(data, data_type);
struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info); struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
struct inotify_inode_mark *i_mark; struct inotify_inode_mark *i_mark;
struct inotify_event_info *event; struct inotify_event_info *event;
...@@ -73,12 +74,9 @@ int inotify_handle_event(struct fsnotify_group *group, ...@@ -73,12 +74,9 @@ int inotify_handle_event(struct fsnotify_group *group,
return 0; return 0;
if ((inode_mark->mask & FS_EXCL_UNLINK) && if ((inode_mark->mask & FS_EXCL_UNLINK) &&
(data_type == FSNOTIFY_EVENT_PATH)) { path && d_unlinked(path->dentry))
const struct path *path = data;
if (d_unlinked(path->dentry))
return 0; return 0;
}
if (file_name) { if (file_name) {
len = file_name->len; len = file_name->len;
alloc_len += len + 1; alloc_len += len + 1;
...@@ -118,7 +116,7 @@ int inotify_handle_event(struct fsnotify_group *group, ...@@ -118,7 +116,7 @@ int inotify_handle_event(struct fsnotify_group *group,
mask &= ~IN_ISDIR; mask &= ~IN_ISDIR;
fsn_event = &event->fse; fsn_event = &event->fse;
fsnotify_init_event(fsn_event, inode); fsnotify_init_event(fsn_event, (unsigned long)inode);
event->mask = mask; event->mask = mask;
event->wd = i_mark->wd; event->wd = i_mark->wd;
event->sync_cookie = cookie; event->sync_cookie = cookie;
......
...@@ -635,7 +635,7 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events) ...@@ -635,7 +635,7 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
group->overflow_event = &oevent->fse; group->overflow_event = &oevent->fse;
fsnotify_init_event(group->overflow_event, NULL); fsnotify_init_event(group->overflow_event, 0);
oevent->mask = FS_Q_OVERFLOW; oevent->mask = FS_Q_OVERFLOW;
oevent->wd = -1; oevent->wd = -1;
oevent->sync_cookie = 0; oevent->sync_cookie = 0;
......
...@@ -47,7 +47,8 @@ ...@@ -47,7 +47,8 @@
* Directory entry modification events - reported only to directory * Directory entry modification events - reported only to directory
* where entry is modified and not to a watching parent. * where entry is modified and not to a watching parent.
*/ */
#define FANOTIFY_DIRENT_EVENTS (FAN_MOVE | FAN_CREATE | FAN_DELETE) #define FANOTIFY_DIRENT_EVENTS (FAN_MOVE | FAN_CREATE | FAN_DELETE | \
FAN_DIR_MODIFY)
/* Events that can only be reported with data type FSNOTIFY_EVENT_INODE */ /* Events that can only be reported with data type FSNOTIFY_EVENT_INODE */
#define FANOTIFY_INODE_EVENTS (FANOTIFY_DIRENT_EVENTS | \ #define FANOTIFY_INODE_EVENTS (FANOTIFY_DIRENT_EVENTS | \
......
...@@ -18,39 +18,63 @@ ...@@ -18,39 +18,63 @@
#include <linux/bug.h> #include <linux/bug.h>
/* /*
* Notify this @dir inode about a change in the directory entry @dentry. * Notify this @dir inode about a change in a child directory entry.
* The directory entry may have turned positive or negative or its inode may
* have changed (i.e. renamed over).
* *
* Unlike fsnotify_parent(), the event will be reported regardless of the * Unlike fsnotify_parent(), the event will be reported regardless of the
* FS_EVENT_ON_CHILD mask on the parent inode. * FS_EVENT_ON_CHILD mask on the parent inode.
*/ */
static inline int fsnotify_dirent(struct inode *dir, struct dentry *dentry, static inline void fsnotify_name(struct inode *dir, __u32 mask,
__u32 mask) struct inode *child,
const struct qstr *name, u32 cookie)
{ {
return fsnotify(dir, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE, fsnotify(dir, mask, child, FSNOTIFY_EVENT_INODE, name, cookie);
&dentry->d_name, 0); /*
* Send another flavor of the event without child inode data and
* without the specific event type (e.g. FS_CREATE|FS_IS_DIR).
* The name is relative to the dir inode the event is reported to.
*/
fsnotify(dir, FS_DIR_MODIFY, dir, FSNOTIFY_EVENT_INODE, name, 0);
} }
/* Notify this dentry's parent about a child's events. */ static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
static inline int fsnotify_parent(const struct path *path, __u32 mask)
struct dentry *dentry, __u32 mask)
{ {
if (!dentry) fsnotify_name(dir, mask, d_inode(dentry), &dentry->d_name, 0);
dentry = path->dentry;
return __fsnotify_parent(path, dentry, mask);
} }
/* /*
* Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when * Simple wrappers to consolidate calls fsnotify_parent()/fsnotify() when
* an event is on a path. * an event is on a file/dentry.
*/ */
static inline int fsnotify_path(struct inode *inode, const struct path *path, static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
__u32 mask) {
struct inode *inode = d_inode(dentry);
if (S_ISDIR(inode->i_mode))
mask |= FS_ISDIR;
fsnotify_parent(dentry, mask, inode, FSNOTIFY_EVENT_INODE);
fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
}
static inline int fsnotify_file(struct file *file, __u32 mask)
{ {
int ret = fsnotify_parent(path, NULL, mask); const struct path *path = &file->f_path;
struct inode *inode = file_inode(file);
int ret;
if (file->f_mode & FMODE_NONOTIFY)
return 0;
if (S_ISDIR(inode->i_mode))
mask |= FS_ISDIR;
ret = fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
if (ret) if (ret)
return ret; return ret;
return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
} }
...@@ -58,19 +82,16 @@ static inline int fsnotify_path(struct inode *inode, const struct path *path, ...@@ -58,19 +82,16 @@ static inline int fsnotify_path(struct inode *inode, const struct path *path,
static inline int fsnotify_perm(struct file *file, int mask) static inline int fsnotify_perm(struct file *file, int mask)
{ {
int ret; int ret;
const struct path *path = &file->f_path;
struct inode *inode = file_inode(file);
__u32 fsnotify_mask = 0; __u32 fsnotify_mask = 0;
if (file->f_mode & FMODE_NONOTIFY)
return 0;
if (!(mask & (MAY_READ | MAY_OPEN))) if (!(mask & (MAY_READ | MAY_OPEN)))
return 0; return 0;
if (mask & MAY_OPEN) { if (mask & MAY_OPEN) {
fsnotify_mask = FS_OPEN_PERM; fsnotify_mask = FS_OPEN_PERM;
if (file->f_flags & __FMODE_EXEC) { if (file->f_flags & __FMODE_EXEC) {
ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM); ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
if (ret) if (ret)
return ret; return ret;
...@@ -79,10 +100,7 @@ static inline int fsnotify_perm(struct file *file, int mask) ...@@ -79,10 +100,7 @@ static inline int fsnotify_perm(struct file *file, int mask)
fsnotify_mask = FS_ACCESS_PERM; fsnotify_mask = FS_ACCESS_PERM;
} }
if (S_ISDIR(inode->i_mode)) return fsnotify_file(file, fsnotify_mask);
fsnotify_mask |= FS_ISDIR;
return fsnotify_path(inode, path, fsnotify_mask);
} }
/* /*
...@@ -122,10 +140,8 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, ...@@ -122,10 +140,8 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
mask |= FS_ISDIR; mask |= FS_ISDIR;
} }
fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name, fsnotify_name(old_dir, old_dir_mask, source, old_name, fs_cookie);
fs_cookie); fsnotify_name(new_dir, new_dir_mask, source, new_name, fs_cookie);
fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
fs_cookie);
if (target) if (target)
fsnotify_link_count(target); fsnotify_link_count(target);
...@@ -180,12 +196,13 @@ static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) ...@@ -180,12 +196,13 @@ static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
* Note: We have to pass also the linked inode ptr as some filesystems leave * Note: We have to pass also the linked inode ptr as some filesystems leave
* new_dentry->d_inode NULL and instantiate inode pointer later * new_dentry->d_inode NULL and instantiate inode pointer later
*/ */
static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry) static inline void fsnotify_link(struct inode *dir, struct inode *inode,
struct dentry *new_dentry)
{ {
fsnotify_link_count(inode); fsnotify_link_count(inode);
audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE); audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, &new_dentry->d_name, 0); fsnotify_name(dir, FS_CREATE, inode, &new_dentry->d_name, 0);
} }
/* /*
...@@ -229,15 +246,7 @@ static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry) ...@@ -229,15 +246,7 @@ static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
*/ */
static inline void fsnotify_access(struct file *file) static inline void fsnotify_access(struct file *file)
{ {
const struct path *path = &file->f_path; fsnotify_file(file, FS_ACCESS);
struct inode *inode = file_inode(file);
__u32 mask = FS_ACCESS;
if (S_ISDIR(inode->i_mode))
mask |= FS_ISDIR;
if (!(file->f_mode & FMODE_NONOTIFY))
fsnotify_path(inode, path, mask);
} }
/* /*
...@@ -245,15 +254,7 @@ static inline void fsnotify_access(struct file *file) ...@@ -245,15 +254,7 @@ static inline void fsnotify_access(struct file *file)
*/ */
static inline void fsnotify_modify(struct file *file) static inline void fsnotify_modify(struct file *file)
{ {
const struct path *path = &file->f_path; fsnotify_file(file, FS_MODIFY);
struct inode *inode = file_inode(file);
__u32 mask = FS_MODIFY;
if (S_ISDIR(inode->i_mode))
mask |= FS_ISDIR;
if (!(file->f_mode & FMODE_NONOTIFY))
fsnotify_path(inode, path, mask);
} }
/* /*
...@@ -261,16 +262,12 @@ static inline void fsnotify_modify(struct file *file) ...@@ -261,16 +262,12 @@ static inline void fsnotify_modify(struct file *file)
*/ */
static inline void fsnotify_open(struct file *file) static inline void fsnotify_open(struct file *file)
{ {
const struct path *path = &file->f_path;
struct inode *inode = file_inode(file);
__u32 mask = FS_OPEN; __u32 mask = FS_OPEN;
if (S_ISDIR(inode->i_mode))
mask |= FS_ISDIR;
if (file->f_flags & __FMODE_EXEC) if (file->f_flags & __FMODE_EXEC)
mask |= FS_OPEN_EXEC; mask |= FS_OPEN_EXEC;
fsnotify_path(inode, path, mask); fsnotify_file(file, mask);
} }
/* /*
...@@ -278,16 +275,10 @@ static inline void fsnotify_open(struct file *file) ...@@ -278,16 +275,10 @@ static inline void fsnotify_open(struct file *file)
*/ */
static inline void fsnotify_close(struct file *file) static inline void fsnotify_close(struct file *file)
{ {
const struct path *path = &file->f_path; __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
struct inode *inode = file_inode(file); FS_CLOSE_NOWRITE;
fmode_t mode = file->f_mode;
__u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
if (S_ISDIR(inode->i_mode))
mask |= FS_ISDIR;
if (!(file->f_mode & FMODE_NONOTIFY)) fsnotify_file(file, mask);
fsnotify_path(inode, path, mask);
} }
/* /*
...@@ -295,14 +286,7 @@ static inline void fsnotify_close(struct file *file) ...@@ -295,14 +286,7 @@ static inline void fsnotify_close(struct file *file)
*/ */
static inline void fsnotify_xattr(struct dentry *dentry) static inline void fsnotify_xattr(struct dentry *dentry)
{ {
struct inode *inode = dentry->d_inode; fsnotify_dentry(dentry, FS_ATTRIB);
__u32 mask = FS_ATTRIB;
if (S_ISDIR(inode->i_mode))
mask |= FS_ISDIR;
fsnotify_parent(NULL, dentry, mask);
fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
} }
/* /*
...@@ -311,7 +295,6 @@ static inline void fsnotify_xattr(struct dentry *dentry) ...@@ -311,7 +295,6 @@ static inline void fsnotify_xattr(struct dentry *dentry)
*/ */
static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
{ {
struct inode *inode = dentry->d_inode;
__u32 mask = 0; __u32 mask = 0;
if (ia_valid & ATTR_UID) if (ia_valid & ATTR_UID)
...@@ -332,13 +315,8 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) ...@@ -332,13 +315,8 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
if (ia_valid & ATTR_MODE) if (ia_valid & ATTR_MODE)
mask |= FS_ATTRIB; mask |= FS_ATTRIB;
if (mask) { if (mask)
if (S_ISDIR(inode->i_mode)) fsnotify_dentry(dentry, mask);
mask |= FS_ISDIR;
fsnotify_parent(NULL, dentry, mask);
fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
}
} }
#endif /* _LINUX_FS_NOTIFY_H */ #endif /* _LINUX_FS_NOTIFY_H */
...@@ -47,18 +47,18 @@ ...@@ -47,18 +47,18 @@
#define FS_OPEN_PERM 0x00010000 /* open event in an permission hook */ #define FS_OPEN_PERM 0x00010000 /* open event in an permission hook */
#define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */ #define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */
#define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */ #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */
#define FS_DIR_MODIFY 0x00080000 /* Directory entry was modified */
#define FS_EXCL_UNLINK 0x04000000 /* do not send events if object is unlinked */ #define FS_EXCL_UNLINK 0x04000000 /* do not send events if object is unlinked */
#define FS_ISDIR 0x40000000 /* event occurred against dir */
#define FS_IN_ONESHOT 0x80000000 /* only send event once */
#define FS_DN_RENAME 0x10000000 /* file renamed */
#define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */
/* This inode cares about things that happen to its children. Always set for /* This inode cares about things that happen to its children. Always set for
* dnotify and inotify. */ * dnotify and inotify. */
#define FS_EVENT_ON_CHILD 0x08000000 #define FS_EVENT_ON_CHILD 0x08000000
#define FS_DN_RENAME 0x10000000 /* file renamed */
#define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */
#define FS_ISDIR 0x40000000 /* event occurred against dir */
#define FS_IN_ONESHOT 0x80000000 /* only send event once */
#define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO) #define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)
/* /*
...@@ -67,7 +67,8 @@ ...@@ -67,7 +67,8 @@
* The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event * The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event
* when a directory entry inside a child subdir changes. * when a directory entry inside a child subdir changes.
*/ */
#define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE) #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | \
FS_DIR_MODIFY)
#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ #define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
FS_OPEN_EXEC_PERM) FS_OPEN_EXEC_PERM)
...@@ -133,8 +134,7 @@ struct fsnotify_ops { ...@@ -133,8 +134,7 @@ struct fsnotify_ops {
*/ */
struct fsnotify_event { struct fsnotify_event {
struct list_head list; struct list_head list;
/* inode may ONLY be dereferenced during handle_event(). */ unsigned long objectid; /* identifier for queue merges */
struct inode *inode; /* either the inode the event happened to or its parent */
}; };
/* /*
...@@ -213,10 +213,36 @@ struct fsnotify_group { ...@@ -213,10 +213,36 @@ struct fsnotify_group {
}; };
}; };
/* when calling fsnotify tell it if the data is a path or inode */ /* When calling fsnotify tell it if the data is a path or inode */
#define FSNOTIFY_EVENT_NONE 0 enum fsnotify_data_type {
#define FSNOTIFY_EVENT_PATH 1 FSNOTIFY_EVENT_NONE,
#define FSNOTIFY_EVENT_INODE 2 FSNOTIFY_EVENT_PATH,
FSNOTIFY_EVENT_INODE,
};
static inline const struct inode *fsnotify_data_inode(const void *data,
int data_type)
{
switch (data_type) {
case FSNOTIFY_EVENT_INODE:
return data;
case FSNOTIFY_EVENT_PATH:
return d_inode(((const struct path *)data)->dentry);
default:
return NULL;
}
}
static inline const struct path *fsnotify_data_path(const void *data,
int data_type)
{
switch (data_type) {
case FSNOTIFY_EVENT_PATH:
return data;
default:
return NULL;
}
}
enum fsnotify_obj_type { enum fsnotify_obj_type {
FSNOTIFY_OBJ_TYPE_INODE, FSNOTIFY_OBJ_TYPE_INODE,
...@@ -351,9 +377,10 @@ struct fsnotify_mark { ...@@ -351,9 +377,10 @@ struct fsnotify_mark {
/* called from the vfs helpers */ /* called from the vfs helpers */
/* main fsnotify call to send events */ /* main fsnotify call to send events */
extern int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is, extern int fsnotify(struct inode *to_tell, __u32 mask, const void *data,
const struct qstr *name, u32 cookie); int data_type, const struct qstr *name, u32 cookie);
extern int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask); extern int fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
int data_type);
extern void __fsnotify_inode_delete(struct inode *inode); extern void __fsnotify_inode_delete(struct inode *inode);
extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt); extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
extern void fsnotify_sb_delete(struct super_block *sb); extern void fsnotify_sb_delete(struct super_block *sb);
...@@ -500,21 +527,22 @@ extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info); ...@@ -500,21 +527,22 @@ extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info); extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
static inline void fsnotify_init_event(struct fsnotify_event *event, static inline void fsnotify_init_event(struct fsnotify_event *event,
struct inode *inode) unsigned long objectid)
{ {
INIT_LIST_HEAD(&event->list); INIT_LIST_HEAD(&event->list);
event->inode = inode; event->objectid = objectid;
} }
#else #else
static inline int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is, static inline int fsnotify(struct inode *to_tell, __u32 mask, const void *data,
const struct qstr *name, u32 cookie) int data_type, const struct qstr *name, u32 cookie)
{ {
return 0; return 0;
} }
static inline int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask) static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
const void *data, int data_type)
{ {
return 0; return 0;
} }
......
...@@ -24,10 +24,11 @@ ...@@ -24,10 +24,11 @@
#define FAN_OPEN_PERM 0x00010000 /* File open in perm check */ #define FAN_OPEN_PERM 0x00010000 /* File open in perm check */
#define FAN_ACCESS_PERM 0x00020000 /* File accessed in perm check */ #define FAN_ACCESS_PERM 0x00020000 /* File accessed in perm check */
#define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */ #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */
#define FAN_DIR_MODIFY 0x00080000 /* Directory entry was modified */
#define FAN_ONDIR 0x40000000 /* event occurred against dir */ #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */
#define FAN_EVENT_ON_CHILD 0x08000000 /* interested in child events */ #define FAN_ONDIR 0x40000000 /* Event occurred against dir */
/* helper events */ /* helper events */
#define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */ #define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
...@@ -116,6 +117,7 @@ struct fanotify_event_metadata { ...@@ -116,6 +117,7 @@ struct fanotify_event_metadata {
}; };
#define FAN_EVENT_INFO_TYPE_FID 1 #define FAN_EVENT_INFO_TYPE_FID 1
#define FAN_EVENT_INFO_TYPE_DFID_NAME 2
/* Variable length info record following event metadata */ /* Variable length info record following event metadata */
struct fanotify_event_info_header { struct fanotify_event_info_header {
...@@ -124,7 +126,12 @@ struct fanotify_event_info_header { ...@@ -124,7 +126,12 @@ struct fanotify_event_info_header {
__u16 len; __u16 len;
}; };
/* Unique file identifier info record */ /*
* Unique file identifier info record. This is used both for
* FAN_EVENT_INFO_TYPE_FID records and for FAN_EVENT_INFO_TYPE_DFID_NAME
* records. For FAN_EVENT_INFO_TYPE_DFID_NAME there is additionally a null
* terminated name immediately after the file handle.
*/
struct fanotify_event_info_fid { struct fanotify_event_info_fid {
struct fanotify_event_info_header hdr; struct fanotify_event_info_header hdr;
__kernel_fsid_t fsid; __kernel_fsid_t fsid;
......
...@@ -160,23 +160,14 @@ static int audit_mark_handle_event(struct fsnotify_group *group, ...@@ -160,23 +160,14 @@ static int audit_mark_handle_event(struct fsnotify_group *group,
{ {
struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info); struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
struct audit_fsnotify_mark *audit_mark; struct audit_fsnotify_mark *audit_mark;
const struct inode *inode = NULL; const struct inode *inode = fsnotify_data_inode(data, data_type);
audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark); audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark);
BUG_ON(group != audit_fsnotify_group); BUG_ON(group != audit_fsnotify_group);
switch (data_type) { if (WARN_ON(!inode))
case (FSNOTIFY_EVENT_PATH):
inode = ((const struct path *)data)->dentry->d_inode;
break;
case (FSNOTIFY_EVENT_INODE):
inode = (const struct inode *)data;
break;
default:
BUG();
return 0; return 0;
}
if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) { if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) {
if (audit_compare_dname_path(dname, audit_mark->path, AUDIT_NAME_FULL)) if (audit_compare_dname_path(dname, audit_mark->path, AUDIT_NAME_FULL))
......
...@@ -471,25 +471,13 @@ static int audit_watch_handle_event(struct fsnotify_group *group, ...@@ -471,25 +471,13 @@ static int audit_watch_handle_event(struct fsnotify_group *group,
struct fsnotify_iter_info *iter_info) struct fsnotify_iter_info *iter_info)
{ {
struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info); struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
const struct inode *inode; const struct inode *inode = fsnotify_data_inode(data, data_type);
struct audit_parent *parent; struct audit_parent *parent;
parent = container_of(inode_mark, struct audit_parent, mark); parent = container_of(inode_mark, struct audit_parent, mark);
BUG_ON(group != audit_watch_group); BUG_ON(group != audit_watch_group);
WARN_ON(!inode);
switch (data_type) {
case (FSNOTIFY_EVENT_PATH):
inode = d_backing_inode(((const struct path *)data)->dentry);
break;
case (FSNOTIFY_EVENT_INODE):
inode = (const struct inode *)data;
break;
default:
BUG();
inode = NULL;
break;
}
if (mask & (FS_CREATE|FS_MOVED_TO) && inode) if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0); audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment