Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1207852

[PATCH 4.1 11/84] fsnotify: fix oops in fsnotify_clear_marks_by_group_flags()

From Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Newsgroups linux.kernel
Subject [PATCH 4.1 11/84] fsnotify: fix oops in fsnotify_clear_marks_by_group_flags()
Date 2015-08-14 20:40 +0200
Message-ID <pXs3x-1OM-49@gated-at.bofh.it> (permalink)
References <pXrh7-Cx-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


4.1-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Jan Kara <jack@suse.com>

commit 8f2f3eb59dff4ec538de55f2e0592fec85966aab upstream.

fsnotify_clear_marks_by_group_flags() can race with
fsnotify_destroy_marks() so that when fsnotify_destroy_mark_locked()
drops mark_mutex, a mark from the list iterated by
fsnotify_clear_marks_by_group_flags() can be freed and thus the next
entry pointer we have cached may become stale and we dereference free
memory.

Fix the problem by first moving marks to free to a special private list
and then always free the first entry in the special list.  This method
is safe even when entries from the list can disappear once we drop the
lock.

Signed-off-by: Jan Kara <jack@suse.com>
Reported-by: Ashish Sangwan <a.sangwan@samsung.com>
Reviewed-by: Ashish Sangwan <a.sangwan@samsung.com>
Cc: Lino Sanfilippo <LinoSanfilippo@gmx.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 fs/notify/mark.c |   30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

--- a/fs/notify/mark.c
+++ b/fs/notify/mark.c
@@ -412,16 +412,36 @@ void fsnotify_clear_marks_by_group_flags
 					 unsigned int flags)
 {
 	struct fsnotify_mark *lmark, *mark;
+	LIST_HEAD(to_free);
 
+	/*
+	 * We have to be really careful here. Anytime we drop mark_mutex, e.g.
+	 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
+	 * to_free list so we have to use mark_mutex even when accessing that
+	 * list. And freeing mark requires us to drop mark_mutex. So we can
+	 * reliably free only the first mark in the list. That's why we first
+	 * move marks to free to to_free list in one go and then free marks in
+	 * to_free list one by one.
+	 */
 	mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
 	list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
-		if (mark->flags & flags) {
-			fsnotify_get_mark(mark);
-			fsnotify_destroy_mark_locked(mark, group);
-			fsnotify_put_mark(mark);
-		}
+		if (mark->flags & flags)
+			list_move(&mark->g_list, &to_free);
 	}
 	mutex_unlock(&group->mark_mutex);
+
+	while (1) {
+		mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
+		if (list_empty(&to_free)) {
+			mutex_unlock(&group->mark_mutex);
+			break;
+		}
+		mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
+		fsnotify_get_mark(mark);
+		fsnotify_destroy_mark_locked(mark, group);
+		mutex_unlock(&group->mark_mutex);
+		fsnotify_put_mark(mark);
+	}
 }
 
 /*


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 4.1 00/84] 4.1.6-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2015-08-14 20:40 +0200
  [PATCH 4.1 24/84] drivers/usb: Delete XHCI command timer if necessary Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2015-08-14 20:40 +0200
  [PATCH 4.1 27/84] usb: chipidea: ehci_init_driver is intended to call one time Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2015-08-14 20:40 +0200
  [PATCH 4.1 10/84] MIPS: Make set_pte() SMP safe. Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2015-08-14 20:40 +0200
  [PATCH 4.1 11/84] fsnotify: fix oops in fsnotify_clear_marks_by_group_flags() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2015-08-14 20:40 +0200
  Re: [PATCH 4.1 00/84] 4.1.6-stable review Shuah Khan <shuahkh@osg.samsung.com> - 2015-08-15 02:20 +0200
    Re: [PATCH 4.1 00/84] 4.1.6-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2015-08-15 02:50 +0200
  Re: [PATCH 4.1 00/84] 4.1.6-stable review Guenter Roeck <linux@roeck-us.net> - 2015-08-15 17:30 +0200
    Re: [PATCH 4.1 00/84] 4.1.6-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2015-08-15 18:50 +0200

csiph-web