fanotify: fix false positive on permission events
Subsystems (2)
PATCH 1/1 fanotify: fix false positive on permission events
fsnotify_get_mark_safe() may return false for a mark on an unrelated group, which results in bypassing the permission check.
Fix by skipping over detached marks that are not in the current group.
--- fs/notify/fsnotify.c | 2 +- fs/notify/mark.c | 18 +++++++++++------- include/linux/fsnotify_backend.h | 1 + 3 files changed, 13 insertions(+), 8 deletions(-)
fs/notify/fsnotify.c +1 -1
@@ -388,7 +388,7 @@ static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector return hlist_entry_safe(node, struct fsnotify_mark, obj_list); } -static struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark) +struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark) {struct hlist_node *node =NULL ;
fs/notify/mark.c +11 -7
@@ -457,9 +457,6 @@ EXPORT_SYMBOL_GPL(fsnotify_put_mark);*/ static bool fsnotify_get_mark_safe (struct fsnotify_mark *mark) { -if (!mark) -return true; -if (refcount_inc_not_zero (&mark->refcnt)) {spin_lock (&mark->lock);if (mark->flags &FSNOTIFY_MARK_FLAG_ATTACHED ) {
@@ -500,15 +497,22 @@ bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)int type;fsnotify_foreach_iter_type (type) { +struct fsnotify_mark *mark = iter_info->marks[type]; +/* This can fail if mark is being removed */ -if (!fsnotify_get_mark_safe (iter_info->marks[type])) { -__release (&fsnotify_mark_srcu); - goto fail; +while (mark && !fsnotify_get_mark_safe (mark)) { +if (mark->group == iter_info->current_group) { +__release (&fsnotify_mark_srcu); + goto fail; + } +/* This is a mark in an unrelated group, skip */ + mark =fsnotify_next_mark (mark); + iter_info->marks[type] = mark; } }/* -* Now that both marks are pinned by refcount in the inode / vfsmount +* Now that all marks are pinned by refcount in the inode / vfsmount / etc * lists, we can drop SRCU lock, and safely resume the list iteration * once userspace returns. */
include/linux/fsnotify_backend.h +1 -0
@@ -915,6 +915,7 @@ extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group, unsigned int obj_type);extern void fsnotify_get_mark (struct fsnotify_mark *mark);extern void fsnotify_put_mark (struct fsnotify_mark *mark); +struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark);extern void fsnotify_finish_user_wait (struct fsnotify_iter_info *iter_info);extern bool fsnotify_prepare_user_wait (struct fsnotify_iter_info *iter_info);
Signature · 1 line
2.53.0
Amir Goldstein
reviewerQuoted 51 lines · Miklos Szeredi
fsnotify_get_mark_safe()may return false for a mark on an unrelated group, which results in bypassing the permission check.Fix by skipping over detached marks that are not in the current group.
Signed-off-by Miklos Szeredi <[email protected]>--- fs/notify/fsnotify.c | 2 +- fs/notify/mark.c | 18 +++++++++++------- include/linux/fsnotify_backend.h | 1 + 3 files changed, 13 insertions(+), 8 deletions(-)fs/notify/fsnotify.c +1
-1@@ -388,7 +388,7 @@ static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector return hlist_entry_safe(node, struct fsnotify_mark, obj_list); } -static struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark) +struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark) {struct hlist_node *node =NULL ;fs/notify/mark.c +3
-6@@ -457,9 +457,6 @@ EXPORT_SYMBOL_GPL(fsnotify_put_mark);*/ static bool fsnotify_get_mark_safe (struct fsnotify_mark *mark) { -if (!mark) -return true; -if (refcount_inc_not_zero (&mark->refcnt)) {spin_lock (&mark->lock);if (mark->flags &FSNOTIFY_MARK_FLAG_ATTACHED ) {@@ -500,15 +497,22 @@ bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)int type;fsnotify_foreach_iter_type (type) { +struct fsnotify_mark *mark = iter_info->marks[type]; +/* This can fail if mark is being removed */ -if (!fsnotify_get_mark_safe (iter_info->marks[type])) { -__release (&fsnotify_mark_srcu); - goto fail; +while (mark && !fsnotify_get_mark_safe (mark)) {
fs/notify/mark.c bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info) · line 513
Quoted 10 lines
fs/notify/mark.c +7
-0+if (mark->group == iter_info->current_group) { +__release (&fsnotify_mark_srcu); + goto fail; + } +/* This is a mark in an unrelated group, skip */ + mark =fsnotify_next_mark (mark); + iter_info->marks[type] = mark; } }
Locally, for mark iteration, the fix looks fine, but fsnotify_mark_srcu also protects lifetime of destroyed mark and connector and I have the eering feeling that some of that code may be relying of the detached mark to either have a ref or fsnotify_mark_srcu.
It's not that I know of something wrong, I just feel that I need proof that the lifetime or mark and connector is not affected.
Jan, WDYT?
Thanks,
Amir.
Quoted 4 lines · Amir Goldstein
Locally, for mark iteration, the fix looks fine, but
fsnotify_mark_srcualso protects lifetime of destroyed mark and connector and I have the eering feeling that some of that code may be relying of the detached mark to either have a ref orfsnotify_mark_srcu.
The fsnotify_next_mark() call in fsnotify_iter_next() does the same thing. Why do you think this could be different?
Thanks,
Miklos
Amir Goldstein
reviewerQuoted 10 lines · Miklos Szeredi
Quoted 4 lines · Amir Goldstein
Locally, for mark iteration, the fix looks fine, but
fsnotify_mark_srcualso protects lifetime of destroyed mark and connector and I have the eering feeling that some of that code may be relying of the detached mark to either have a ref orfsnotify_mark_srcu.The
fsnotify_next_mark()call infsnotify_iter_next()does the same thing. Why do you think this could be different?
No you're right. seems safe.
Thanks,
Amir.
Jan Kara
maintainerQuoted 6 lines · Miklos Szeredi
fsnotify_get_mark_safe()may return false for a mark on an unrelated group, which results in bypassing the permission check.Fix by skipping over detached marks that are not in the current group.
Signed-off-by Miklos Szeredi <[email protected]>
Sorry for a bit delayed reply. I was thinking about this for a while and the fix looks good. I like it. I'll queue it once I push out pull requests to Linus later this week. Thanks!
Honza
Quoted 75 lines
--- fs/notify/fsnotify.c | 2 +- fs/notify/mark.c | 18 +++++++++++------- include/linux/fsnotify_backend.h | 1 + 3 files changed, 13 insertions(+), 8 deletions(-)fs/notify/fsnotify.c +1
-1@@ -388,7 +388,7 @@ static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector return hlist_entry_safe(node, struct fsnotify_mark, obj_list); } -static struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark) +struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark) {struct hlist_node *node =NULL ;fs/notify/mark.c +11
-7@@ -457,9 +457,6 @@ EXPORT_SYMBOL_GPL(fsnotify_put_mark);*/ static bool fsnotify_get_mark_safe (struct fsnotify_mark *mark) { -if (!mark) -return true; -if (refcount_inc_not_zero (&mark->refcnt)) {spin_lock (&mark->lock);if (mark->flags &FSNOTIFY_MARK_FLAG_ATTACHED ) {@@ -500,15 +497,22 @@ bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)int type;fsnotify_foreach_iter_type (type) { +struct fsnotify_mark *mark = iter_info->marks[type]; +/* This can fail if mark is being removed */ -if (!fsnotify_get_mark_safe (iter_info->marks[type])) { -__release (&fsnotify_mark_srcu); - goto fail; +while (mark && !fsnotify_get_mark_safe (mark)) { +if (mark->group == iter_info->current_group) { +__release (&fsnotify_mark_srcu); + goto fail; + } +/* This is a mark in an unrelated group, skip */ + mark =fsnotify_next_mark (mark); + iter_info->marks[type] = mark; } }/* -* Now that both marks are pinned by refcount in the inode / vfsmount +* Now that all marks are pinned by refcount in the inode / vfsmount / etc * lists, we can drop SRCU lock, and safely resume the list iteration * once userspace returns. */ include/linux/fsnotify_backend.h +1
-0@@ -915,6 +915,7 @@ extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group, unsigned int obj_type);extern void fsnotify_get_mark (struct fsnotify_mark *mark);extern void fsnotify_put_mark (struct fsnotify_mark *mark); +struct fsnotify_mark *fsnotify_next_mark (struct fsnotify_mark *mark);extern void fsnotify_finish_user_wait (struct fsnotify_iter_info *iter_info);extern bool fsnotify_prepare_user_wait (struct fsnotify_iter_info *iter_info);Signature · 1 line
2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR