Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

inotify: Add flag IN_MASK_CREATE for inotify_add_watch()

The flag IN_MASK_CREATE is introduced as a flag for inotiy_add_watch()
which prevents inotify from modifying any existing watches when invoked.
If the pathname specified in the call has a watched inode associated
with it and IN_MASK_CREATE is specified, fail with an errno of EEXIST.

Use of IN_MASK_CREATE with IN_MASK_ADD is reserved for future use and
will return EINVAL.

RATIONALE

In the current implementation, there is no way to prevent
inotify_add_watch() from modifying existing watch descriptors. Even if
the caller keeps a record of all watch descriptors collected, this is
only sufficient to detect that an existing watch descriptor may have
been modified.

The assumption that a particular path will map to the same inode over
multiple calls to inotify_add_watch() cannot be made as files can be
renamed or deleted. It is also not possible to assume that two distinct
paths do no map to the same inode, due to hard-links or a dereferenced
symbolic link. Further uses of inotify_add_watch() to revert the change
may cause other watch descriptors to be modified or created, merely
compunding the problem. There is currently no system call such as
inotify_modify_watch() to explicity modify a watch descriptor, which
would be able to revert unwanted changes. Thus the caller cannot
guarantee to be able to revert any changes to existing watch decriptors.

Additionally the caller cannot assume that the events that are
associated with a watch descriptor are within the set requested, as any
future calls to inotify_add_watch() may unintentionally modify a watch
descriptor's mask. Thus it cannot currently be guaranteed that a watch
descriptor will only generate events which have been requested. The
program must filter events which come through its watch descriptor to
within its expected range.

Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Henry Wilson <henry.wilson@acentic.com>
Signed-off-by: Jan Kara <jack@suse.cz>

authored by

Henry Wilson and committed by
Jan Kara
4d97f7d5 eaa2c6b0

+10 -2
+8 -1
fs/notify/inotify/inotify_user.c
··· 510 510 __u32 old_mask, new_mask; 511 511 __u32 mask; 512 512 int add = (arg & IN_MASK_ADD); 513 + int create = (arg & IN_MASK_CREATE); 513 514 int ret; 514 515 515 516 mask = inotify_arg_to_mask(arg); ··· 518 517 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); 519 518 if (!fsn_mark) 520 519 return -ENOENT; 520 + else if (create) 521 + return -EEXIST; 521 522 522 523 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 523 524 ··· 721 718 if (unlikely(!f.file)) 722 719 return -EBADF; 723 720 721 + /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */ 722 + if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) 723 + return -EINVAL; 724 + 724 725 /* verify that this is indeed an inotify instance */ 725 726 if (unlikely(f.file->f_op != &inotify_fops)) { 726 727 ret = -EINVAL; ··· 813 806 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); 814 807 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); 815 808 816 - BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21); 809 + BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22); 817 810 818 811 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC); 819 812
+1 -1
include/linux/inotify.h
··· 18 18 IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \ 19 19 IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \ 20 20 IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \ 21 - IN_ISDIR | IN_ONESHOT) 21 + IN_MASK_CREATE | IN_ISDIR | IN_ONESHOT) 22 22 23 23 #endif /* _LINUX_INOTIFY_H */
+1
include/uapi/linux/inotify.h
··· 53 53 #define IN_ONLYDIR 0x01000000 /* only watch the path if it is a directory */ 54 54 #define IN_DONT_FOLLOW 0x02000000 /* don't follow a sym link */ 55 55 #define IN_EXCL_UNLINK 0x04000000 /* exclude events on unlinked objects */ 56 + #define IN_MASK_CREATE 0x10000000 /* only create watches */ 56 57 #define IN_MASK_ADD 0x20000000 /* add to the mask of an already existing watch */ 57 58 #define IN_ISDIR 0x40000000 /* event occurred against dir */ 58 59 #define IN_ONESHOT 0x80000000 /* only send event once */