Skip to content

Commit a8b13aa

Browse files
amir73iljankara
authored andcommitted
fanotify: enable FAN_REPORT_FID init flag
When setting up an fanotify listener, user may request to get fid information in event instead of an open file descriptor. The fid obtained with event on a watched object contains the file handle returned by name_to_handle_at(2) and fsid returned by statfs(2). Restrict FAN_REPORT_FID to class FAN_CLASS_NOTIF, because we have have no good reason to support reporting fid on permission events. When setting a mark, we need to make sure that the filesystem supports encoding file handles with name_to_handle_at(2) and that statfs(2) encodes a non-zero fsid. Cc: <linux-api@vger.kernel.org> Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 5e469c8 commit a8b13aa

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

fs/notify/fanotify/fanotify_user.c

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <linux/compat.h>
1818
#include <linux/sched/signal.h>
1919
#include <linux/memcontrol.h>
20+
#include <linux/statfs.h>
21+
#include <linux/exportfs.h>
2022

2123
#include <asm/ioctls.h>
2224

@@ -768,6 +770,10 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
768770
return -EINVAL;
769771
}
770772

773+
if ((flags & FAN_REPORT_FID) &&
774+
(flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
775+
return -EINVAL;
776+
771777
user = get_current_user();
772778
if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
773779
free_uid(user);
@@ -854,6 +860,52 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
854860
return fd;
855861
}
856862

863+
/* Check if filesystem can encode a unique fid */
864+
static int fanotify_test_fid(struct path *path)
865+
{
866+
struct kstatfs stat, root_stat;
867+
struct path root = {
868+
.mnt = path->mnt,
869+
.dentry = path->dentry->d_sb->s_root,
870+
};
871+
int err;
872+
873+
/*
874+
* Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
875+
*/
876+
err = vfs_statfs(path, &stat);
877+
if (err)
878+
return err;
879+
880+
if (!stat.f_fsid.val[0] && !stat.f_fsid.val[1])
881+
return -ENODEV;
882+
883+
/*
884+
* Make sure path is not inside a filesystem subvolume (e.g. btrfs)
885+
* which uses a different fsid than sb root.
886+
*/
887+
err = vfs_statfs(&root, &root_stat);
888+
if (err)
889+
return err;
890+
891+
if (root_stat.f_fsid.val[0] != stat.f_fsid.val[0] ||
892+
root_stat.f_fsid.val[1] != stat.f_fsid.val[1])
893+
return -EXDEV;
894+
895+
/*
896+
* We need to make sure that the file system supports at least
897+
* encoding a file handle so user can use name_to_handle_at() to
898+
* compare fid returned with event to the file handle of watched
899+
* objects. However, name_to_handle_at() requires that the
900+
* filesystem also supports decoding file handles.
901+
*/
902+
if (!path->dentry->d_sb->s_export_op ||
903+
!path->dentry->d_sb->s_export_op->fh_to_dentry)
904+
return -EOPNOTSUPP;
905+
906+
return 0;
907+
}
908+
857909
static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
858910
int dfd, const char __user *pathname)
859911
{
@@ -939,6 +991,12 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
939991
if (ret)
940992
goto fput_and_out;
941993

994+
if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
995+
ret = fanotify_test_fid(&path);
996+
if (ret)
997+
goto path_put_and_out;
998+
}
999+
9421000
/* inode held in place by reference to path; group by fget on fd */
9431001
if (mark_type == FAN_MARK_INODE)
9441002
inode = path.dentry->d_inode;
@@ -967,6 +1025,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
9671025
ret = -EINVAL;
9681026
}
9691027

1028+
path_put_and_out:
9701029
path_put(&path);
9711030
fput_and_out:
9721031
fdput(f);
@@ -1003,7 +1062,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark,
10031062
*/
10041063
static int __init fanotify_user_setup(void)
10051064
{
1006-
BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7);
1065+
BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
10071066
BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
10081067

10091068
fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,

include/linux/fanotify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
FAN_CLASS_PRE_CONTENT)
2020

2121
#define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \
22-
FAN_REPORT_TID | \
22+
FAN_REPORT_TID | FAN_REPORT_FID | \
2323
FAN_CLOEXEC | FAN_NONBLOCK | \
2424
FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS)
2525

0 commit comments

Comments
 (0)