diff options
author | Amir Goldstein <amir73il@gmail.com> | 2019-01-10 12:04:36 -0500 |
---|---|---|
committer | Jan Kara <jack@suse.cz> | 2019-02-07 10:38:35 -0500 |
commit | a8b13aa20afb69161b5123b4f1acc7ea0a03d360 (patch) | |
tree | fc886af970f46d54e78c9c430c9d82d0548fd108 /fs/notify | |
parent | 5e469c830fdb5a1ebaa69b375b87f583326fd296 (diff) |
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>
Diffstat (limited to 'fs/notify')
-rw-r--r-- | fs/notify/fanotify/fanotify_user.c | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index cd82dd713c91..1638c171ca82 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c | |||
@@ -17,6 +17,8 @@ | |||
17 | #include <linux/compat.h> | 17 | #include <linux/compat.h> |
18 | #include <linux/sched/signal.h> | 18 | #include <linux/sched/signal.h> |
19 | #include <linux/memcontrol.h> | 19 | #include <linux/memcontrol.h> |
20 | #include <linux/statfs.h> | ||
21 | #include <linux/exportfs.h> | ||
20 | 22 | ||
21 | #include <asm/ioctls.h> | 23 | #include <asm/ioctls.h> |
22 | 24 | ||
@@ -768,6 +770,10 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) | |||
768 | return -EINVAL; | 770 | return -EINVAL; |
769 | } | 771 | } |
770 | 772 | ||
773 | if ((flags & FAN_REPORT_FID) && | ||
774 | (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF) | ||
775 | return -EINVAL; | ||
776 | |||
771 | user = get_current_user(); | 777 | user = get_current_user(); |
772 | if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) { | 778 | if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) { |
773 | free_uid(user); | 779 | free_uid(user); |
@@ -854,6 +860,52 @@ out_destroy_group: | |||
854 | return fd; | 860 | return fd; |
855 | } | 861 | } |
856 | 862 | ||
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 | |||
857 | static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, | 909 | static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, |
858 | int dfd, const char __user *pathname) | 910 | int dfd, const char __user *pathname) |
859 | { | 911 | { |
@@ -939,6 +991,12 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, | |||
939 | if (ret) | 991 | if (ret) |
940 | goto fput_and_out; | 992 | goto fput_and_out; |
941 | 993 | ||
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 | |||
942 | /* inode held in place by reference to path; group by fget on fd */ | 1000 | /* inode held in place by reference to path; group by fget on fd */ |
943 | if (mark_type == FAN_MARK_INODE) | 1001 | if (mark_type == FAN_MARK_INODE) |
944 | inode = path.dentry->d_inode; | 1002 | inode = path.dentry->d_inode; |
@@ -967,6 +1025,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, | |||
967 | ret = -EINVAL; | 1025 | ret = -EINVAL; |
968 | } | 1026 | } |
969 | 1027 | ||
1028 | path_put_and_out: | ||
970 | path_put(&path); | 1029 | path_put(&path); |
971 | fput_and_out: | 1030 | fput_and_out: |
972 | fdput(f); | 1031 | fdput(f); |
@@ -1003,7 +1062,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark, | |||
1003 | */ | 1062 | */ |
1004 | static int __init fanotify_user_setup(void) | 1063 | static int __init fanotify_user_setup(void) |
1005 | { | 1064 | { |
1006 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7); | 1065 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8); |
1007 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); | 1066 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); |
1008 | 1067 | ||
1009 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, | 1068 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, |