Skip to contentSkip to conversation

inotify: Add flag IN_ONLY_CREATE for inotify_add_watch()

PATCH Superseded Henry Wilson 4 participants 21 messages May 3, 2018 – May 31, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 inotify: Add flag IN_ONLY_CREATE for inotify_add_watch()

From: Henry Wilson <[email protected]>

inotify: Add flag IN_ONLY_CREATE for inotify_add_watch()

The flag IN_ONLY_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_ONLY_CREATE is specified, fail with an errno of EEXIST.

---
RATIONALE

When using inotify_add_watch() a programmer will expect the watch descriptor to create a new watch descriptor referencing the inode the path is associated with. However while the word 'add' implies these semantics, the system call may either create or modify watches with this call. This would be fine if the interface has a mechanism by which to distinguish between these actions. If two files, file1, file2 the new path points to the same inode as an existing watch, for example with a hard-link, a dereferenced symbolic link or the original file being renamed. The programmer will be given a watch descriptor that they alrady have.

IMPLICATIONS OF THE DEFECT

The programmer may not realise the watch descriptor has been duplicated. Thus, two logical entities in the program will have the same watch descriptor and will compete for events from the queue. The watch descriptor will be deleted more times than neccessary in this case.

The call to inotify_add_watch() may modify the watch mask for the original watch descriptor and the program may attempt to revert these changes. They may be unable to revert changes due to the path no-longer being associated with the same inode. The problem may be compounded if the path is assiciated with a different inode that is being watched. If IN_MASK_ADD was not used with the call, by the time the watch mask is reverted, important events may have been missed, without a mechanism to explicitly say that events were missed. (Unlike queue overflow)

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.

ALTERNATIVES

Create a new system call inotify_create_watch(fd, pathname, mask) which only creates watches. This would avoid creating a new flag-bit but would require some implementation refactoring. Adding a new flag-bit was chosen as currently 21 IN_xxx flag-bits exist, an additional flag-bit leaves 10 remaining for future expansion. This leverages the existing implementation and as such is a minor implementation change.

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

ADDITIONS TO MANPAGES

inotify(7)

IN_ONLY_CREATE
Only watch pathname if it refers to an inode that is not already being watched by this inotify instance.

inotify_add_watch(2)

EEXIST
pathname references an existing watch descriptor and IN_ONLY_CREATE was specified.

---
 fs/notify/inotify/inotify_user.c | 3 +++
 include/linux/inotify.h          | 2 +-
 include/uapi/linux/inotify.h     | 1 +
 3 files changed, 5 insertions(+), 1 deletion(-)
fs/notify/inotify/inotify_user.c +3 -0
index ef32f3657958..24cad6bf7579 100644
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	__u32 old_mask, new_mask;
 	__u32 mask;
 	int add = (arg & IN_MASK_ADD);
+	int create = (arg & IN_ONLY_CREATE);
 	int ret;
 
 	mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
 	if (!fsn_mark)
 		return -ENOENT;
+	else if (create)
+		return -EEXIST;
 
 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
include/linux/inotify.h +1 -1
index 44f9ffe72c87..1b0b76cfc55b 100644
@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
 			  IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
 			  IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
 			  IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
-			  IN_ISDIR | IN_ONESHOT)
+			  IN_ONLY_CREATE | IN_ISDIR | IN_ONESHOT)
 
 #endif	/* _LINUX_INOTIFY_H */
include/uapi/linux/inotify.h +1 -0
index 4800bf2a531d..bf6ac25244d7 100644
@@ -53,6 +53,7 @@ struct inotify_event {
 #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
 #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
 #define IN_EXCL_UNLINK		0x04000000	/* exclude events on unlinked objects */
+#define IN_ONLY_CREATE		0x10000000	/* only create watches */
 #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
 #define IN_ISDIR		0x40000000	/* event occurred against dir */
 #define IN_ONESHOT		0x80000000	/* only send event once */
Signature · 1 line
2.17.0
#1 lore raw
Quoted 65 lines · Henry Wilson

From: Henry Wilson <[email protected]>

inotify: Add flag IN_ONLY_CREATE for inotify_add_watch()

The flag IN_ONLY_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_ONLY_CREATE is specified, fail with an errno of EEXIST.

---
RATIONALE

When using inotify_add_watch() a programmer will expect the watch descriptor to create a new watch descriptor referencing the inode the path is associated with. However while the word 'add' implies these semantics, the system call may either create or modify watches with this call. This would be fine if the interface has a mechanism by which to distinguish between these actions. If two files, file1, file2 the new path points to the same inode as an existing watch, for example with a hard-link, a dereferenced symbolic link or the original file being renamed. The programmer will be given a watch descriptor that they alrady have.

IMPLICATIONS OF THE DEFECT

The programmer may not realise the watch descriptor has been duplicated. Thus, two logical entities in the program will have the same watch descriptor and will compete for events from the queue. The watch descriptor will be deleted more times than neccessary in this case.

The call to inotify_add_watch() may modify the watch mask for the original watch descriptor and the program may attempt to revert these changes. They may be unable to revert changes due to the path no-longer being associated with the same inode. The problem may be compounded if the path is assiciated with a different inode that is being watched. If IN_MASK_ADD was not used with the call, by the time the watch mask is reverted, important events may have been missed, without a mechanism to explicitly say that events were missed. (Unlike queue overflow)

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.

ALTERNATIVES

Create a new system call inotify_create_watch(fd, pathname, mask) which only creates watches. This would avoid creating a new flag-bit but would require some implementation refactoring. Adding a new flag-bit was chosen as currently 21 IN_xxx flag-bits exist, an additional flag-bit leaves 10 remaining for future expansion. This leverages the existing implementation and as such is a minor implementation change.

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

ADDITIONS TO MANPAGES

inotify(7)

IN_ONLY_CREATE
Only watch pathname if it refers to an inode that is not already being watched by this inotify instance.

Quoted 5 lines

inotify_add_watch(2)

EEXIST
pathname references an existing watch descriptor and IN_ONLY_CREATE was specified.

I'm not crazy about the flag name. No great ideas for name. Maybe I_NO_UPDATE.

If this API change is considered, I would like to see a similar change to fanotify,
because I don't like fact there is missing functionality in fanotify and would not
like that gap to grow.

Thanks,
Amir.

#2 lore raw
Quoted 1 line · Amir Goldstein

I'm not crazy about the flag name.

I'm not particularly attached to the name. Here's a list of the names I can think of at this moment:

IN_ONLY_CREATE
IN_NO_UPDATE
IN_DONT_UPDATE
IN_NO_MODIFY
IN_DONT_MODIFY
IN_SAFE_CREATE
IN_ADD_WATCH_CREATE_ONLY
IN_CONSTRUCT

Regards, Henry.

#3 lore raw

PATCH v2 1/1 From: Henry Wilson <[email protected]>

In reply to Henry Wilson

From: Henry Wilson <[email protected]>

inotify: Add flag IN_ONLY_CREATE for inotify_add_watch()

The flag IN_ONLY_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_ONLY_CREATE is specified, fail with an errno of EEXIST.

---
RATIONALE

When using inotify_add_watch() a programmer will expect the watch descriptor to create a new watch descriptor referencing the inode the path is associated with. However while the word 'add' implies these semantics, the system call may either create or modify watches with this call. This would be fine if the interface has a mechanism by which to distinguish between these actions. If two files, file1, file2 the new path points to the same inode as an existing watch, for example with a hard-link, a dereferenced symbolic link or the original file being renamed. The programmer will be given a watch descriptor that they alrady have.

IMPLICATIONS OF THE DEFECT

The programmer may not realise the watch descriptor has been duplicated. Thus, two logical entities in the program will have the same watch descriptor and will compete for events from the queue. The watch descriptor will be deleted more times than neccessary in this case.

The call to inotify_add_watch() may modify the watch mask for the original watch descriptor and the program may attempt to revert these changes. They may be unable to revert changes due to the path no-longer being associated with the same inode. The problem may be compounded if the path is assiciated with a different inode that is being watched. If IN_MASK_ADD was not used with the call, by the time the watch mask is reverted, important events may have been missed, without a mechanism to explicitly say that events were missed. (Unlike queue overflow)

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.

ALTERNATIVES

Create a new system call inotify_create_watch(fd, pathname, mask) which only creates watches. This would avoid creating a new flag-bit but would require some implementation refactoring. Adding a new flag-bit was chosen as currently 21 IN_xxx flag-bits exist, an additional flag-bit leaves 10 remaining for future expansion. This leverages the existing implementation and as such is a minor implementation change.

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

ADDITIONS TO MANPAGES

inotify(7)

IN_ONLY_CREATE
Only watch pathname if it refers to an inode that is not already being watched by this inotify instance.

inotify_add_watch(2)

EEXIST
pathname references an existing watch descriptor and IN_ONLY_CREATE was specified.

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS

---
 fs/notify/inotify/inotify_user.c | 5 ++++-
 include/linux/inotify.h          | 2 +-
 include/uapi/linux/inotify.h     | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +4 -1
index ef32f3657958..94e1234b0d38 100644
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	__u32 old_mask, new_mask;
 	__u32 mask;
 	int add = (arg & IN_MASK_ADD);
+	int create = (arg & IN_ONLY_CREATE);
 	int ret;
 
 	mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
 	if (!fsn_mark)
 		return -ENOENT;
+	else if (create)
+		return -EEXIST;
 
 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
@@ -802,7 +805,7 @@ static int __init inotify_user_setup(void)
 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
 
-	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
+	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
 
 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
 
include/linux/inotify.h +1 -1
index 44f9ffe72c87..1b0b76cfc55b 100644
@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
 			  IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
 			  IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
 			  IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
-			  IN_ISDIR | IN_ONESHOT)
+			  IN_ONLY_CREATE | IN_ISDIR | IN_ONESHOT)
 
 #endif	/* _LINUX_INOTIFY_H */
include/uapi/linux/inotify.h +1 -0
index 4800bf2a531d..bf6ac25244d7 100644
@@ -53,6 +53,7 @@ struct inotify_event {
 #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
 #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
 #define IN_EXCL_UNLINK		0x04000000	/* exclude events on unlinked objects */
+#define IN_ONLY_CREATE		0x10000000	/* only create watches */
 #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
 #define IN_ISDIR		0x40000000	/* event occurred against dir */
 #define IN_ONESHOT		0x80000000	/* only send event once */
Signature · 1 line
2.17.0
maintainer
#4 lore raw

Re: [PATCH v2 4.17] From: Henry Wilson <[email protected]>

Quoted 10 lines · On Wed 16-05-18 10:44:30, [email protected] wrote:

From: Henry Wilson <[email protected]>

inotify: Add flag IN_ONLY_CREATE for inotify_add_watch()

The flag IN_ONLY_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_ONLY_CREATE is specified, fail with an errno of EEXIST.

Thanks for the patch and sorry for not responding earlier. You've got my email address subtly wrong (the domain is 'suse', not 'suze') and thus I've seen your email only now when reading older list posts.

On a general note a lot of your reasoning below actually belongs to the commit message itself so that it is available later when someone wonders:
"Why the hell this flag was added?"

Quoted 13 lines

---
RATIONALE

When using inotify_add_watch() a programmer will expect the watch descriptor to create a new watch descriptor referencing the inode the path is associated with. However while the word 'add' implies these semantics, the system call may either create or modify watches with this call. This would be fine if the interface has a mechanism by which to distinguish between these actions. If two files, file1, file2 the new path points to the same inode as an existing watch, for example with a hard-link, a dereferenced symbolic link or the original file being renamed. The programmer will be given a watch descriptor that they alrady have.

I can understand this may be inconvenient but you could work around this rather easily in your program, couldn't you? E.g. by having a data structure with already known watch descriptors for given inotify instance and checking against that. Is there some problem with that?

Honza

Quoted 116 lines

IMPLICATIONS OF THE DEFECT

The programmer may not realise the watch descriptor has been duplicated. Thus, two logical entities in the program will have the same watch descriptor and will compete for events from the queue. The watch descriptor will be deleted more times than neccessary in this case.

The call to inotify_add_watch() may modify the watch mask for the original watch descriptor and the program may attempt to revert these changes. They may be unable to revert changes due to the path no-longer being associated with the same inode. The problem may be compounded if the path is assiciated with a different inode that is being watched. If IN_MASK_ADD was not used with the call, by the time the watch mask is reverted, important events may have been missed, without a mechanism to explicitly say that events were missed. (Unlike queue overflow)

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.

ALTERNATIVES

Create a new system call inotify_create_watch(fd, pathname, mask) which only creates watches. This would avoid creating a new flag-bit but would require some implementation refactoring. Adding a new flag-bit was chosen as currently 21 IN_xxx flag-bits exist, an additional flag-bit leaves 10 remaining for future expansion. This leverages the existing implementation and as such is a minor implementation change.

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

ADDITIONS TO MANPAGES

inotify(7)

IN_ONLY_CREATE
Only watch pathname if it refers to an inode that is not already being watched by this inotify instance.

inotify_add_watch(2)

EEXIST
pathname references an existing watch descriptor and IN_ONLY_CREATE was specified.

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS

---
 fs/notify/inotify/inotify_user.c | 5 ++++-
 include/linux/inotify.h          | 2 +-
 include/uapi/linux/inotify.h     | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +4 -1
index ef32f3657958..94e1234b0d38 100644
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	__u32 old_mask, new_mask;
 	__u32 mask;
 	int add = (arg & IN_MASK_ADD);
+	int create = (arg & IN_ONLY_CREATE);
 	int ret;
 
 	mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
 	if (!fsn_mark)
 		return -ENOENT;
+	else if (create)
+		return -EEXIST;
 
 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
@@ -802,7 +805,7 @@ static int __init inotify_user_setup(void)
 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
 
-	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
+	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
 
 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
 
include/linux/inotify.h +1 -1
index 44f9ffe72c87..1b0b76cfc55b 100644
@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
 			  IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
 			  IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
 			  IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
-			  IN_ISDIR | IN_ONESHOT)
+			  IN_ONLY_CREATE | IN_ISDIR | IN_ONESHOT)
 
 #endif	/* _LINUX_INOTIFY_H */
include/uapi/linux/inotify.h +1 -0
index 4800bf2a531d..bf6ac25244d7 100644
@@ -53,6 +53,7 @@ struct inotify_event {
 #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
 #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
 #define IN_EXCL_UNLINK		0x04000000	/* exclude events on unlinked objects */
+#define IN_ONLY_CREATE		0x10000000	/* only create watches */
 #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
 #define IN_ISDIR		0x40000000	/* event occurred against dir */
 #define IN_ONESHOT		0x80000000	/* only send event once */
Signature · 1 line
2.17.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw

Re: [PATCH v2 4.17] From: Henry Wilson <[email protected]>

Quoted 10 lines · Jan Kara
Quoted 9 lines · On Wed 16-05-18 10:44:30, [email protected] wrote:

From: Henry Wilson <[email protected]>

inotify: Add flag IN_ONLY_CREATE for inotify_add_watch()

The flag IN_ONLY_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_ONLY_CREATE is specified, fail with an errno of EEXIST.

Quoted 5 lines
Quoted 1 line

Thanks for the patch and sorry for not responding earlier. You've got my email address subtly wrong (the domain is 'suse', not 'suze') and thus I've seen your email only now when reading older list posts.

My apologies for missing that and messing up the title of the previous email. Thank you for taking the time to respond.

Quoted 3 lines

On a general note a lot of your reasoning below actually belongs to the commit message itself so that it is available later when someone wonders:
"Why the hell this flag was added?"

Okay, I shall include it in the next patch submission.

Quoted 14 lines
Quoted 13 lines

---
RATIONALE

When using inotify_add_watch() a programmer will expect the watch descriptor to create a new watch descriptor referencing the inode the path is associated with. However while the word 'add' implies these semantics, the system call may either create or modify watches with this call. This would be fine if the interface has a mechanism by which to distinguish between these actions. If two files, file1, file2 the new path points to the same inode as an existing watch, for example with a hard-link, a dereferenced symbolic link or the original file being renamed. The programmer will be given a watch descriptor that they alrady have.

Quoted 6 lines

I can understand this may be inconvenient but you could work around this rather easily in your program, couldn't you? E.g. by having a data structure with already known watch descriptors for given inotify instance and checking against that. Is there some problem with that?

Honza

While I agree that keeping a searchable record of all known watch descriptors allows the program to successfully *detect* that the inode is already being watched, inotify_add_watch() is a modifying action.

The assumption that a particular path will map to the same inode over multiple calls to inotify_add_watch() cannot be made.
Thus the program cannot guarantee to be able to *revert* any changes made by the detection of the repeated watch descriptor. Further uses of inotify_add_watch() to attempt to rectify the original modification to the intended watch descriptor's flags may end up clobbering other watch descriptors' flags. In this situation the program may be in a catch-22. It can try clearing affected watch descriptors and start again, but there may be inodes which it was watching that have been moved to an unknown path. Or it can give up and forget about the affected inodes.

By preventing the situation from occurring in the first place, the API is simpler to use.

Regards, Henry.

Quoted 116 lines
Quoted 116 lines

IMPLICATIONS OF THE DEFECT

The programmer may not realise the watch descriptor has been duplicated. Thus, two logical entities in the program will have the same watch descriptor and will compete for events from the queue. The watch descriptor will be deleted more times than neccessary in this case.

The call to inotify_add_watch() may modify the watch mask for the original watch descriptor and the program may attempt to revert these changes. They may be unable to revert changes due to the path no-longer being associated with the same inode. The problem may be compounded if the path is assiciated with a different inode that is being watched. If IN_MASK_ADD was not used with the call, by the time the watch mask is reverted, important events may have been missed, without a mechanism to explicitly say that events were missed. (Unlike queue overflow)

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.

ALTERNATIVES

Create a new system call inotify_create_watch(fd, pathname, mask) which only creates watches. This would avoid creating a new flag-bit but would require some implementation refactoring. Adding a new flag-bit was chosen as currently 21 IN_xxx flag-bits exist, an additional flag-bit leaves 10 remaining for future expansion. This leverages the existing implementation and as such is a minor implementation change.

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

ADDITIONS TO MANPAGES

inotify(7)

IN_ONLY_CREATE
Only watch pathname if it refers to an inode that is not already being watched by this inotify instance.

inotify_add_watch(2)

EEXIST
pathname references an existing watch descriptor and IN_ONLY_CREATE was specified.

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS

---
  fs/notify/inotify/inotify_user.c | 5 ++++-
  include/linux/inotify.h          | 2 +-
  include/uapi/linux/inotify.h     | 1 +
  3 files changed, 6 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +4 -1
index ef32f3657958..94e1234b0d38 100644
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
  	__u32 old_mask, new_mask;
  	__u32 mask;
  	int add = (arg & IN_MASK_ADD);
+	int create = (arg & IN_ONLY_CREATE);
  	int ret;
 
  	mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
  	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
  	if (!fsn_mark)
  		return -ENOENT;
+	else if (create)
+		return -EEXIST;
 
  	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
@@ -802,7 +805,7 @@ static int __init inotify_user_setup(void)
  	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
  	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
 
-	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
+	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
 
  	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
 
include/linux/inotify.h +1 -1
index 44f9ffe72c87..1b0b76cfc55b 100644
@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
  			  IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
  			  IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
  			  IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
-			  IN_ISDIR | IN_ONESHOT)
+			  IN_ONLY_CREATE | IN_ISDIR | IN_ONESHOT)
 
  #endif	/* _LINUX_INOTIFY_H */
include/uapi/linux/inotify.h +1 -0
index 4800bf2a531d..bf6ac25244d7 100644
@@ -53,6 +53,7 @@ struct inotify_event {
  #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
  #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
  #define IN_EXCL_UNLINK		0x04000000	/* exclude events on unlinked objects */
+#define IN_ONLY_CREATE		0x10000000	/* only create watches */
  #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
  #define IN_ISDIR		0x40000000	/* event occurred against dir */
  #define IN_ONESHOT		0x80000000	/* only send event once */
Signature · 1 line
2.17.0
maintainer
#6 lore raw

Re: [PATCH v2 4.17] From: Henry Wilson <[email protected]>

Quoted 7 lines · Henry Wilson

On 29/05/18 10:15, Jan Kara wrote:
Thank you for taking the time to respond.

Quoted 3 lines

On a general note a lot of your reasoning below actually belongs to the commit message itself so that it is available later when someone wonders:
"Why the hell this flag was added?"

Okay, I shall include it in the next patch submission.

Yes, please.

Quoted 25 lines
Quoted 14 lines
Quoted 13 lines

---
RATIONALE

When using inotify_add_watch() a programmer will expect the watch descriptor to create a new watch descriptor referencing the inode the path is associated with. However while the word 'add' implies these semantics, the system call may either create or modify watches with this call. This would be fine if the interface has a mechanism by which to distinguish between these actions. If two files, file1, file2 the new path points to the same inode as an existing watch, for example with a hard-link, a dereferenced symbolic link or the original file being renamed. The programmer will be given a watch descriptor that they alrady have.

Quoted 6 lines

I can understand this may be inconvenient but you could work around this rather easily in your program, couldn't you? E.g. by having a data structure with already known watch descriptors for given inotify instance and checking against that. Is there some problem with that?

Honza

While I agree that keeping a searchable record of all known watch descriptors allows the program to successfully *detect* that the inode is already being watched, inotify_add_watch() is a modifying action.

Quoted 10 lines

The assumption that a particular path will map to the same inode over multiple calls to inotify_add_watch() cannot be made.
Thus the program cannot guarantee to be able to *revert* any changes made by the detection of the repeated watch descriptor. Further uses of inotify_add_watch() to attempt to rectify the original modification to the intended watch descriptor's flags may end up clobbering other watch descriptors' flags. In this situation the program may be in a catch-22. It can try clearing affected watch descriptors and start again, but there may be inodes which it was watching that have been moved to an unknown path. Or it can give up and forget about the affected inodes.

That are all good points and belong to the commit description as well. Please add them for the next submission.

So now I agree with adding this functionality to inotify. But please also provide corresponding patch for man-pages so that documentation is uptodate. Also I don't like the IN_ONLY_CREATE name as that sounds more like an event type than a flag (and inotify sadly combines these in a single field). Can we call it like IN_EXCL_ADD to indicate we want the mask addition to be exclusive?

If you have time, it would be good to also provide similar flag for fanotify_mark(2) syscall as Amir suggested. If not, I can eventually handle that.

Honza

Quoted 115 lines

By preventing the situation from occurring in the first place, the API is simpler to use.

Regards, Henry.

Quoted 109 lines
Quoted 109 lines

IMPLICATIONS OF THE DEFECT

The programmer may not realise the watch descriptor has been duplicated. Thus, two logical entities in the program will have the same watch descriptor and will compete for events from the queue. The watch descriptor will be deleted more times than neccessary in this case.

The call to inotify_add_watch() may modify the watch mask for the original watch descriptor and the program may attempt to revert these changes. They may be unable to revert changes due to the path no-longer being associated with the same inode. The problem may be compounded if the path is assiciated with a different inode that is being watched. If IN_MASK_ADD was not used with the call, by the time the watch mask is reverted, important events may have been missed, without a mechanism to explicitly say that events were missed. (Unlike queue overflow)

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.

ALTERNATIVES

Create a new system call inotify_create_watch(fd, pathname, mask) which only creates watches. This would avoid creating a new flag-bit but would require some implementation refactoring. Adding a new flag-bit was chosen as currently 21 IN_xxx flag-bits exist, an additional flag-bit leaves 10 remaining for future expansion. This leverages the existing implementation and as such is a minor implementation change.

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

ADDITIONS TO MANPAGES

inotify(7)

IN_ONLY_CREATE
Only watch pathname if it refers to an inode that is not already being watched by this inotify instance.

inotify_add_watch(2)

EEXIST
pathname references an existing watch descriptor and IN_ONLY_CREATE was specified.

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS

---
  fs/notify/inotify/inotify_user.c | 5 ++++-
  include/linux/inotify.h          | 2 +-
  include/uapi/linux/inotify.h     | 1 +
  3 files changed, 6 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +4 -1
index ef32f3657958..94e1234b0d38 100644
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
  	__u32 old_mask, new_mask;
  	__u32 mask;
  	int add = (arg & IN_MASK_ADD);
+	int create = (arg & IN_ONLY_CREATE);
  	int ret;
  	mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
  	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
  	if (!fsn_mark)
  		return -ENOENT;
+	else if (create)
+		return -EEXIST;
  	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
@@ -802,7 +805,7 @@ static int __init inotify_user_setup(void)
  	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
  	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
-	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
+	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
  	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
include/linux/inotify.h +1 -1
index 44f9ffe72c87..1b0b76cfc55b 100644
@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
  			  IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
  			  IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
  			  IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
-			  IN_ISDIR | IN_ONESHOT)
+			  IN_ONLY_CREATE | IN_ISDIR | IN_ONESHOT)
  #endif	/* _LINUX_INOTIFY_H */
include/uapi/linux/inotify.h +1 -0
index 4800bf2a531d..bf6ac25244d7 100644
@@ -53,6 +53,7 @@ struct inotify_event {
  #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
  #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
  #define IN_EXCL_UNLINK		0x04000000	/* exclude events on unlinked objects */
+#define IN_ONLY_CREATE		0x10000000	/* only create watches */
  #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
  #define IN_ISDIR		0x40000000	/* event occurred against dir */
  #define IN_ONESHOT		0x80000000	/* only send event once */
Signature · 1 line
2.17.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#7 lore raw

Re: [PATCH v2 4.17] From: Henry Wilson <[email protected]>

Quoted 3 lines · Jan Kara

So now I agree with adding this functionality to inotify. But please also provide corresponding patch for man-pages so that documentation is uptodate.

I shall prepare a patch for man-pages later today.

Regards, Henry

#8 lore raw

PATCH v3 1/1 inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

In reply to [email protected]

From: Henry Wilson <[email protected]>

inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

The flag IN_EXCL_ADD 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_EXCL_ADD is specified, fail with an errno of EEXIST.

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.

---

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

ADDITIONS TO MANPAGES

inotify(7)

IN_EXCL_ADD
Only watch pathname if it refers to an inode that is not already being watched by this inotify instance.

inotify_add_watch(2)

EEXIST
pathname references an existing watch descriptor and IN_EXCL_ADD was specified.

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS
v3: renamed IN_ONLY_CREATE to IN_EXCL_ADD
additional rational for the change introduced to commit

---
 fs/notify/inotify/inotify_user.c | 5 ++++-
 include/linux/inotify.h          | 2 +-
 include/uapi/linux/inotify.h     | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +4 -1
index ef32f3657958..bbe6fbc4fee6 100644
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	__u32 old_mask, new_mask;
 	__u32 mask;
 	int add = (arg & IN_MASK_ADD);
+	int create = (arg & IN_EXCL_ADD);
 	int ret;
 
 	mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
 	if (!fsn_mark)
 		return -ENOENT;
+	else if (create)
+		return -EEXIST;
 
 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
@@ -802,7 +805,7 @@ static int __init inotify_user_setup(void)
 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
 
-	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
+	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
 
 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
 
include/linux/inotify.h +1 -1
index 44f9ffe72c87..ab423272540c 100644
@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
 			  IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
 			  IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
 			  IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
-			  IN_ISDIR | IN_ONESHOT)
+			  IN_EXCL_ADD | IN_ISDIR | IN_ONESHOT)
 
 #endif	/* _LINUX_INOTIFY_H */
include/uapi/linux/inotify.h +1 -0
index 4800bf2a531d..7601540aa38a 100644
@@ -53,6 +53,7 @@ struct inotify_event {
 #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
 #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
 #define IN_EXCL_UNLINK		0x04000000	/* exclude events on unlinked objects */
+#define IN_EXCL_ADD		0x10000000	/* only create watches */
 #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
 #define IN_ISDIR		0x40000000	/* event occurred against dir */
 #define IN_ONESHOT		0x80000000	/* only send event once */
Signature · 1 line
2.17.0
maintainer
#9 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

Hello,

Quoted 26 lines · On Wed 30-05-18 10:26:13, [email protected] wrote:

From: Henry Wilson <[email protected]>

inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

The flag IN_EXCL_ADD 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_EXCL_ADD is specified, fail with an errno of EEXIST.

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.

Quoted 8 lines

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.

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on working on a similar addition to fanotify?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#10 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

Quoted 5 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things.
I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

if(!fsn_mark) {
	...
}
else if (create) {
	return -EEXIST;
}

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

Regards, Henry.

#11 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

Quoted 13 lines · Henry Wilson
Quoted 7 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on
working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things. I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

Quoted 9 lines
if(!fsn_mark) {
        ...
}
else if (create) {
        return -EEXIST;
}

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

I think that was a yes/no question and I interpret your answer as no??

Anyway, another yes/no question:
Can you write a simple LTP test to verify the new API?

I reccon Jan was also expecting an actual patch posted to man pages maintainer (and linux-api, which was not cc'ed on the latest patch).

About the fanotify change, since fanotify API does have 'flags' separate from 'mask', I am not sure if FAN_MARK_EXCL_ADD would be the best flag name?? Perhaps FAN_MARK_CREATE? FAN_MARK_NEW?
not sure.

But also, I did not get a chance to comment about the chosen inotify flag name that the lexical proximity to IN_EXCL_UNLINK is a bit odd considering that _EXCL_ mean two completely different things.

Should we maybe re-consider the chosen flag name?
Maybe include linux-api in the discussion?

Thanks,
Amir.

maintainer
#12 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

Quoted 24 lines · Amir Goldstein
Quoted 13 lines · Henry Wilson
Quoted 7 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on
working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things. I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

Quoted 9 lines
if(!fsn_mark) {
        ...
}
else if (create) {
        return -EEXIST;
}

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

Quoted 7 lines

I think that was a yes/no question and I interpret your answer as no??

Anyway, another yes/no question:
Can you write a simple LTP test to verify the new API?

I reccon Jan was also expecting an actual patch posted to man pages maintainer (and linux-api, which was not cc'ed on the latest patch).

Yes, and I think Henry is about to post it, just didn't get to it yet.

Quoted 4 lines

About the fanotify change, since fanotify API does have 'flags' separate from 'mask', I am not sure if FAN_MARK_EXCL_ADD would be the best flag name?? Perhaps FAN_MARK_CREATE? FAN_MARK_NEW?
not sure.

Yes, for fanotify we could choose a different name.

Quoted 5 lines

But also, I did not get a chance to comment about the chosen inotify flag name that the lexical proximity to IN_EXCL_UNLINK is a bit odd considering that _EXCL_ mean two completely different things.

Should we maybe re-consider the chosen flag name?

I'm open to that, I have the patch just sitting in an internal branch for now. Do you have a better suggestion? Maybe since we already have IN_MASK_ADD, we could call it IN_MASK_CREATE? And then FAN_MARK_CREATE for fanotify_mark(2)?

Quoted 1 line

Maybe include linux-api in the discussion?

Probably we should, added.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#13 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

Quoted 46 lines · Jan Kara
Quoted 24 lines · Amir Goldstein
Quoted 13 lines · Henry Wilson
Quoted 7 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on
working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things. I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

Quoted 9 lines
if(!fsn_mark) {
        ...
}
else if (create) {
        return -EEXIST;
}

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

Quoted 7 lines

I think that was a yes/no question and I interpret your answer as no??

Anyway, another yes/no question:
Can you write a simple LTP test to verify the new API?

I reccon Jan was also expecting an actual patch posted to man pages maintainer (and linux-api, which was not cc'ed on the latest patch).

Yes, and I think Henry is about to post it, just didn't get to it yet.

Quoted 4 lines

About the fanotify change, since fanotify API does have 'flags' separate from 'mask', I am not sure if FAN_MARK_EXCL_ADD would be the best flag name?? Perhaps FAN_MARK_CREATE? FAN_MARK_NEW?
not sure.

Yes, for fanotify we could choose a different name.

Quoted 4 lines

But also, I did not get a chance to comment about the chosen inotify flag name that the lexical proximity to IN_EXCL_UNLINK is a bit odd considering that _EXCL_ mean two completely different things.

Quoted 7 lines
Quoted 1 line

Should we maybe re-consider the chosen flag name?

I'm open to that, I have the patch just sitting in an internal branch for now. Do you have a better suggestion? Maybe since we already have IN_MASK_ADD, we could call it IN_MASK_CREATE? And then FAN_MARK_CREATE for fanotify_mark(2)?

I like that naming.

Thinking forward, since we don't have many reserved bits left in inotify mask, we should return -EINVAL for both IN_MASK_ADD and IN_MASK_CREATE (they don't make sense together anyway), so we may re purpose the flag combination in the future for something else.

Thanks,
Amir.

maintainer
#14 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

Quoted 55 lines · Amir Goldstein
Quoted 46 lines · Jan Kara
Quoted 24 lines · Amir Goldstein
Quoted 13 lines · Henry Wilson
Quoted 7 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on
working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things. I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

Quoted 9 lines
if(!fsn_mark) {
        ...
}
else if (create) {
        return -EEXIST;
}

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

Quoted 7 lines

I think that was a yes/no question and I interpret your answer as no??

Anyway, another yes/no question:
Can you write a simple LTP test to verify the new API?

I reccon Jan was also expecting an actual patch posted to man pages maintainer (and linux-api, which was not cc'ed on the latest patch).

Yes, and I think Henry is about to post it, just didn't get to it yet.

Quoted 4 lines

About the fanotify change, since fanotify API does have 'flags' separate from 'mask', I am not sure if FAN_MARK_EXCL_ADD would be the best flag name?? Perhaps FAN_MARK_CREATE? FAN_MARK_NEW?
not sure.

Yes, for fanotify we could choose a different name.

Quoted 4 lines

But also, I did not get a chance to comment about the chosen inotify flag name that the lexical proximity to IN_EXCL_UNLINK is a bit odd considering that _EXCL_ mean two completely different things.

Quoted 7 lines
Quoted 1 line

Should we maybe re-consider the chosen flag name?

I'm open to that, I have the patch just sitting in an internal branch for now. Do you have a better suggestion? Maybe since we already have IN_MASK_ADD, we could call it IN_MASK_CREATE? And then FAN_MARK_CREATE for fanotify_mark(2)?

Quoted 6 lines

I like that naming.

Thinking forward, since we don't have many reserved bits left in inotify mask, we should return -EINVAL for both IN_MASK_ADD and IN_MASK_CREATE (they don't make sense together anyway), so we may re purpose the flag combination in the future for something else.

Good point, I'll update the patch.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#15 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

In reply to Jan Kara
Quoted 22 lines · Jan Kara
Quoted 21 lines · Amir Goldstein
Quoted 20 lines · Henry Wilson
Quoted 7 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on
working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things. I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

if(!fsn_mark) {
         ...
}
else if (create) {
         return -EEXIST;
}
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

I think that was a yes/no question and I interpret your answer as no?? >> Anyway, another yes/no question:
Can you write a simple LTP test to verify the new API?

I shall have a go at writing a test, yes.

Quoted 3 lines
Quoted 3 lines

I reccon Jan was also expecting an actual patch posted to man pages maintainer (and linux-api, which was not cc'ed on the latest patch).

Ah, I did not know linux-api needed to be cc'ed in.

Quoted 2 lines

Yes, and I think Henry is about to post it, just didn't get to it yet.

For reference here is an archive link to the thread on the linux-man archive
https://marc.info/?l=linux-man&m=152769572917930&w=2

Quoted 12 lines
Quoted 5 lines

About the fanotify change, since fanotify API does have 'flags' separate from 'mask', I am not sure if FAN_MARK_EXCL_ADD would be the best flag name?? Perhaps FAN_MARK_CREATE? FAN_MARK_NEW?
not sure.

Yes, for fanotify we could choose a different name.

Quoted 4 lines

But also, I did not get a chance to comment about the chosen inotify flag name that the lexical proximity to IN_EXCL_UNLINK is a bit odd considering that _EXCL_ mean two completely different things.

Quoted 5 lines
Quoted 1 line

Should we maybe re-consider the chosen flag name? >

I'm open to that, I have the patch just sitting in an internal branch for now. Do you have a better suggestion? Maybe since we already have IN_MASK_ADD, we could call it IN_MASK_CREATE? And then FAN_MARK_CREATE for fanotify_mark(2)?

IN_MASK_CREATE seems the most logical to me too.
I'm happy to resubmit the patch if necessary.

Quoted 7 lines
Quoted 2 lines

Maybe include linux-api in the discussion?

Probably we should, added.

Honza

#16 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

Quoted 34 lines · Henry Wilson
Quoted 31 lines · Jan Kara
Quoted 29 lines · Amir Goldstein
Quoted 25 lines · Henry Wilson
Quoted 9 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan
on
working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things. I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

if(!fsn_mark) {
         ...
}
else if (create) {
         return -EEXIST;
}

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

I think that was a yes/no question and I interpret your answer as no?? >> Anyway, another yes/no question:
Can you write a simple LTP test to verify the new API?

I shall have a go at writing a test, yes.

Great.

I advice you start by forking testcases/kernel/syscalls/inotify/inotify01.c I would verify that:
A. IN_MASK_CREATE creates a watch and events get generated (minor flavor of inotify01.c)
B. IN_MASK_CREATE returns EEXISTS on second call for same file C. existing mask is not modified by this attempt (events get generated according to original mask)
D. IN_MASK_CREATE && IN_MASK_ADD return EINVAL

B-D. can be just extra steps in setup().
Do keep in mind to exit test with tst_brk(TCONF in case call with IN_MASK_CREATE returns EINVAL

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

I reccon Jan was also expecting an actual patch posted to man pages maintainer (and linux-api, which was not cc'ed on the latest patch).

Quoted 9 lines

Ah, I did not know linux-api needed to be cc'ed in.

Quoted 2 lines

Yes, and I think Henry is about to post it, just didn't get to it yet.

For reference here is an archive link to the thread on the linux-man archive
https://marc.info/?l=linux-man&m=152769572917930&w=2

OK, so I see now with the flag name change documentation patch is out of date, but also the statement (since Linux 4.17) is incorrect

Honestly, I don't know how Michael coordinates man pages updates vs. merged kernel patches, but the more info you provide to Michael about were the kernel patch stands in the process is probably for the best.

Thanks,
Amir.

#17 lore raw

Re: [PATCH v3 4.17] inotify: Add flag IN_EXCL_ADD for inotify_add_watch()

In reply to Amir Goldstein
Quoted 37 lines · Amir Goldstein
Quoted 13 lines · Henry Wilson
Quoted 7 lines · Jan Kara

Thanks. The patch looks good. I've added it to my tree. BTW, do you plan on
working on a similar addition to fanotify?

Honza

Ah that's grand, I'm glad to have helped to improve things. I'm not familiar with fanotify, however a quick look at fanotify_user.c suggests that a similar approach may be taken by modifying:

Quoted 9 lines
if(!fsn_mark) {
        ...
}
else if (create) {
        return -EEXIST;
}

in both fanotify_add_vfsmount_mark() and fanotify_add_inode_mark()

I think that was a yes/no question and I interpret your answer as no??

Anyway, another yes/no question:
Can you write a simple LTP test to verify the new API?

I reccon Jan was also expecting an actual patch posted to man pages maintainer (and linux-api, which was not cc'ed on the latest patch).

About the fanotify change, since fanotify API does have 'flags' separate from 'mask', I am not sure if FAN_MARK_EXCL_ADD would be the best flag name?? Perhaps FAN_MARK_CREATE? FAN_MARK_NEW?
not sure.

Quoted 6 lines

But also, I did not get a chance to comment about the chosen inotify flag name that the lexical proximity to IN_EXCL_UNLINK is a bit odd considering that _EXCL_ mean two completely different things.

Should we maybe re-consider the chosen flag name?
Maybe include linux-api in the discussion?

Certainly (regarding linux-api).

Signature · 1 line
~Randy
#18 lore raw

PATCH v4 1/1 inotify: Add flag IN_MASK_CREATE for inotify_add_watch()

In reply to [email protected]

From: Henry Wilson <[email protected]>

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.

---

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS
v3: renamed IN_ONLY_CREATE to IN_EXCL_ADD
additional rational for the change introduced to commit
v4: renamed IN_EXCL_ADD to IN_MASK_CREATE
EINVAL returned when attempting to use IN_MASK_CREATE and IN_MASK_ADD

---
 fs/notify/inotify/inotify_user.c | 9 ++++++++-
 include/linux/inotify.h          | 2 +-
 include/uapi/linux/inotify.h     | 1 +
 3 files changed, 10 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +8 -1
index ef32f3657958..134e9c5308c8 100644
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	__u32 old_mask, new_mask;
 	__u32 mask;
 	int add = (arg & IN_MASK_ADD);
+	int create = (arg & IN_MASK_CREATE);
 	int ret;
 
 	mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
 	if (!fsn_mark)
 		return -ENOENT;
+	else if (create)
+		return -EEXIST;
 
 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
@@ -714,6 +717,10 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
 	if (unlikely(!f.file))
 		return -EBADF;
 
+	/* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
+	if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE)))
+		return -EINVAL;
+
 	/* verify that this is indeed an inotify instance */
 	if (unlikely(f.file->f_op != &inotify_fops)) {
 		ret = -EINVAL;
@@ -802,7 +809,7 @@ static int __init inotify_user_setup(void)
 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
 
-	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
+	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
 
 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
 
include/linux/inotify.h +1 -1
index 44f9ffe72c87..6a24905f6e1e 100644
@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
 			  IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
 			  IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
 			  IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
-			  IN_ISDIR | IN_ONESHOT)
+			  IN_MASK_CREATE | IN_ISDIR | IN_ONESHOT)
 
 #endif	/* _LINUX_INOTIFY_H */
include/uapi/linux/inotify.h +1 -0
index 4800bf2a531d..884b4846b630 100644
@@ -53,6 +53,7 @@ struct inotify_event {
 #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
 #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
 #define IN_EXCL_UNLINK		0x04000000	/* exclude events on unlinked objects */
+#define IN_MASK_CREATE		0x10000000	/* only create watches */
 #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
 #define IN_ISDIR		0x40000000	/* event occurred against dir */
 #define IN_ONESHOT		0x80000000	/* only send event once */
Signature · 1 line
2.17.0
reviewer Reviewed-by on v4 1/1
#19 lore raw

Re: [PATCH v4 4.17] inotify: Add flag IN_MASK_CREATE for inotify_add_watch()

Quoted 29 lines · On Thu, May 31, 2018 at 12:43 PM, <[email protected]> wrote:

From: Henry Wilson <[email protected]>

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.

Quoted 8 lines

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.

See one suggestions below.

Quoted 26 lines

---

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS
v3: renamed IN_ONLY_CREATE to IN_EXCL_ADD
additional rational for the change introduced to commit
v4: renamed IN_EXCL_ADD to IN_MASK_CREATE
EINVAL returned when attempting to use IN_MASK_CREATE and IN_MASK_ADD

---
 fs/notify/inotify/inotify_user.c | 9 ++++++++-
 include/linux/inotify.h          | 2 +-
 include/uapi/linux/inotify.h     | 1 +
 3 files changed, 10 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +0 -0
index ef32f3657958..134e9c5308c8 100644

fs/notify/inotify/inotify_user.c static int inotify_update_existing_watch(struct fsnotify_group *group, · line 511

Quoted 6 lines
fs/notify/inotify/inotify_user.c +1 -0
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
        __u32 old_mask, new_mask;
        __u32 mask;
        int add = (arg & IN_MASK_ADD);
+       int create = (arg & IN_MASK_CREATE);
        int ret;
 

Exclusive flag validation would look nicer here...

   if (add && create)
      return -EINVAL;
Quoted 11 lines
fs/notify/inotify/inotify_user.c +2 -0
        mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
        fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
        if (!fsn_mark)
                return -ENOENT;
+       else if (create)
+               return -EEXIST;
 
        i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 

fs/notify/inotify/inotify_user.c SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, · line 723

Quoted 7 lines
fs/notify/inotify/inotify_user.c +4 -0
@@ -714,6 +717,10 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
        if (unlikely(!f.file))
                return -EBADF;
 
+       /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
+       if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE)))
+               return -EINVAL;
+

..., but it may be safer practice
to keep it in the outer syscall interface, so not sure.

Thanks,
Amir.

maintainer
#20 lore raw

Re: [PATCH v4 4.17] inotify: Add flag IN_MASK_CREATE for inotify_add_watch()

Quoted 95 lines · Amir Goldstein
Quoted 29 lines · On Thu, May 31, 2018 at 12:43 PM, <[email protected]> wrote:

From: Henry Wilson <[email protected]>

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.

Quoted 8 lines

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.

See one suggestions below.

Quoted 26 lines

---

EXTENSIONS

A new system call inotify_modify_watch(fd, wd, mask) would be useful to modify an existing watch directly to avoid similar problems when modifying a watch descriptor's mask

CHANGELOG

v2: updated inotify_user_init() to the increased size of INOTIFY_ALL_BITS
v3: renamed IN_ONLY_CREATE to IN_EXCL_ADD
additional rational for the change introduced to commit
v4: renamed IN_EXCL_ADD to IN_MASK_CREATE
EINVAL returned when attempting to use IN_MASK_CREATE and IN_MASK_ADD

---
 fs/notify/inotify/inotify_user.c | 9 ++++++++-
 include/linux/inotify.h          | 2 +-
 include/uapi/linux/inotify.h     | 1 +
 3 files changed, 10 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_user.c +0 -0
index ef32f3657958..134e9c5308c8 100644

fs/notify/inotify/inotify_user.c static int inotify_update_existing_watch(struct fsnotify_group *group, · line 511

Quoted 6 lines
fs/notify/inotify/inotify_user.c +1 -0
@@ -506,6 +506,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
        __u32 old_mask, new_mask;
        __u32 mask;
        int add = (arg & IN_MASK_ADD);
+       int create = (arg & IN_MASK_CREATE);
        int ret;
 

Exclusive flag validation would look nicer here...

   if (add && create)
      return -EINVAL;
Quoted 14 lines
fs/notify/inotify/inotify_user.c +2 -0
        mask = inotify_arg_to_mask(arg);
@@ -513,6 +514,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
        fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
        if (!fsn_mark)
                return -ENOENT;
+       else if (create)
+               return -EEXIST;
 
        i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
@@ -714,6 +717,10 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
        if (unlikely(!f.file))
                return -EBADF;
 
Quoted 7 lines

fs/notify/inotify/inotify_user.c SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, · line 723

Quoted 4 lines
fs/notify/inotify/inotify_user.c +4 -0
+       /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
+       if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE)))
+               return -EINVAL;
+

..., but it may be safer practice
to keep it in the outer syscall interface, so not sure.

Yeah, I think the outer interface is better for clarity. I'll take the patch as is. Thanks for your review.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR