fanotify: add support for FAN_REPORT_NAME

Introduce a new fanotify_init() flag FAN_REPORT_NAME.  It requires the
flag FAN_REPORT_DIR_FID and there is a constant for setting both flags
named FAN_REPORT_DFID_NAME.

For a group with flag FAN_REPORT_NAME, the parent fid and name are
reported for directory entry modification events (create/detete/move)
and for events on non-directory objects.

Events on directories themselves are reported with their own fid and
"." as the name.

The parent fid and name are reported with an info record of type
FAN_EVENT_INFO_TYPE_DFID_NAME, similar to the way that parent fid is
reported with into type FAN_EVENT_INFO_TYPE_DFID, but with an appended
null terminated name string.

Link: https://lore.kernel.org/r/20200716084230.30611-21-amir73il@gmail.com
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
This commit is contained in:
Amir Goldstein 2020-07-16 11:42:28 +03:00 committed by Jan Kara
parent 5128063739
commit 929943b38d
4 changed files with 58 additions and 11 deletions

View File

@ -522,9 +522,25 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
bool name_event = false; bool name_event = false;
if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
id = dirid; id = dirid;
/*
* We record file name only in a group with FAN_REPORT_NAME
* and when we have a directory inode to report.
*
* For directory entry modification event, we record the fid of
* the directory and the name of the modified entry.
*
* For event on non-directory that is reported to parent, we
* record the fid of the parent and the name of the child.
*/
if ((fid_mode & FAN_REPORT_NAME) &&
((mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
!(mask & FAN_ONDIR)))
name_event = true;
}
/* /*
* For queues with unlimited length lost events are not expected and * For queues with unlimited length lost events are not expected and
* can possibly have security implications. Avoid losing events when * can possibly have security implications. Avoid losing events when

View File

@ -64,18 +64,27 @@ static int fanotify_fid_info_len(int fh_len, int name_len)
return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN); 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(unsigned int fid_mode,
struct fanotify_event *event)
{ {
struct fanotify_info *info = fanotify_event_info(event); struct fanotify_info *info = fanotify_event_info(event);
int dir_fh_len = fanotify_event_dir_fh_len(event); int dir_fh_len = fanotify_event_dir_fh_len(event);
int fh_len = fanotify_event_object_fh_len(event); int fh_len = fanotify_event_object_fh_len(event);
int info_len = 0; int info_len = 0;
int dot_len = 0;
if (dir_fh_len) if (dir_fh_len) {
info_len += fanotify_fid_info_len(dir_fh_len, info->name_len); info_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
} else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) {
/*
* With group flag FAN_REPORT_NAME, if name was not recorded in
* event on a directory, we will report the name ".".
*/
dot_len = 1;
}
if (fh_len) if (fh_len)
info_len += fanotify_fid_info_len(fh_len, 0); info_len += fanotify_fid_info_len(fh_len, dot_len);
return info_len; return info_len;
} }
@ -91,6 +100,7 @@ static struct fanotify_event *get_one_event(struct fsnotify_group *group,
{ {
size_t event_size = FAN_EVENT_METADATA_LEN; size_t event_size = FAN_EVENT_METADATA_LEN;
struct fanotify_event *event = NULL; struct fanotify_event *event = NULL;
unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
pr_debug("%s: group=%p count=%zd\n", __func__, group, count); pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
@ -98,8 +108,8 @@ static struct fanotify_event *get_one_event(struct fsnotify_group *group,
if (fsnotify_notify_queue_is_empty(group)) if (fsnotify_notify_queue_is_empty(group))
goto out; goto out;
if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) { if (fid_mode) {
event_size += fanotify_event_info_len( event_size += fanotify_event_info_len(fid_mode,
FANOTIFY_E(fsnotify_peek_first_event(group))); FANOTIFY_E(fsnotify_peek_first_event(group)));
} }
@ -325,7 +335,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
pr_debug("%s: group=%p event=%p\n", __func__, group, event); pr_debug("%s: group=%p event=%p\n", __func__, group, event);
metadata.event_len = FAN_EVENT_METADATA_LEN + metadata.event_len = FAN_EVENT_METADATA_LEN +
fanotify_event_info_len(event); fanotify_event_info_len(fid_mode, 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;
@ -374,12 +384,25 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
} }
if (fanotify_event_object_fh_len(event)) { if (fanotify_event_object_fh_len(event)) {
const char *dot = NULL;
int dot_len = 0;
if (fid_mode == FAN_REPORT_FID || info_type) { if (fid_mode == FAN_REPORT_FID || info_type) {
/* /*
* With only group flag FAN_REPORT_FID only type FID is * With only group flag FAN_REPORT_FID only type FID is
* reported. Second info record type is always FID. * reported. Second info record type is always FID.
*/ */
info_type = FAN_EVENT_INFO_TYPE_FID; info_type = FAN_EVENT_INFO_TYPE_FID;
} else if ((fid_mode & FAN_REPORT_NAME) &&
(event->mask & FAN_ONDIR)) {
/*
* With group flag FAN_REPORT_NAME, if name was not
* recorded in an event on a directory, report the
* name "." with info type DFID_NAME.
*/
info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
dot = ".";
dot_len = 1;
} else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) || } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
(event->mask & FAN_ONDIR)) { (event->mask & FAN_ONDIR)) {
/* /*
@ -400,7 +423,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
ret = copy_info_to_user(fanotify_event_fsid(event), ret = copy_info_to_user(fanotify_event_fsid(event),
fanotify_event_object_fh(event), fanotify_event_object_fh(event),
info_type, NULL, 0, buf, count); info_type, dot, dot_len, buf, count);
if (ret < 0) if (ret < 0)
return ret; return ret;
@ -932,11 +955,15 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
if (fid_mode && class != FAN_CLASS_NOTIF) if (fid_mode && class != FAN_CLASS_NOTIF)
return -EINVAL; return -EINVAL;
/* Reporting either object fid or dir fid */ /*
* Reporting either object fid or dir fid.
* Child name is reported with parent fid so requires dir fid.
*/
switch (fid_mode) { switch (fid_mode) {
case 0: case 0:
case FAN_REPORT_FID: case FAN_REPORT_FID:
case FAN_REPORT_DIR_FID: case FAN_REPORT_DIR_FID:
case FAN_REPORT_DFID_NAME:
break; break;
default: default:
return -EINVAL; return -EINVAL;
@ -1294,7 +1321,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark,
*/ */
static int __init fanotify_user_setup(void) static int __init fanotify_user_setup(void)
{ {
BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 9); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,

View File

@ -18,7 +18,7 @@
#define FANOTIFY_CLASS_BITS (FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | \ #define FANOTIFY_CLASS_BITS (FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | \
FAN_CLASS_PRE_CONTENT) FAN_CLASS_PRE_CONTENT)
#define FANOTIFY_FID_BITS (FAN_REPORT_FID | FAN_REPORT_DIR_FID) #define FANOTIFY_FID_BITS (FAN_REPORT_FID | FAN_REPORT_DFID_NAME)
#define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | FANOTIFY_FID_BITS | \ #define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | FANOTIFY_FID_BITS | \
FAN_REPORT_TID | \ FAN_REPORT_TID | \

View File

@ -54,6 +54,10 @@
#define FAN_REPORT_TID 0x00000100 /* event->pid is thread id */ #define FAN_REPORT_TID 0x00000100 /* event->pid is thread id */
#define FAN_REPORT_FID 0x00000200 /* Report unique file id */ #define FAN_REPORT_FID 0x00000200 /* Report unique file id */
#define FAN_REPORT_DIR_FID 0x00000400 /* Report unique directory id */ #define FAN_REPORT_DIR_FID 0x00000400 /* Report unique directory id */
#define FAN_REPORT_NAME 0x00000800 /* Report events with name */
/* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */
#define FAN_REPORT_DFID_NAME (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
/* Deprecated - do not use this in programs and do not add new flags here! */ /* Deprecated - do not use this in programs and do not add new flags here! */
#define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \