]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/3.10.87/fsnotify-fix-oops-in-fsnotify_clear_marks_by_group_flags.patch
4.9-stable patches
[thirdparty/kernel/stable-queue.git] / releases / 3.10.87 / fsnotify-fix-oops-in-fsnotify_clear_marks_by_group_flags.patch
1 From 8f2f3eb59dff4ec538de55f2e0592fec85966aab Mon Sep 17 00:00:00 2001
2 From: Jan Kara <jack@suse.com>
3 Date: Thu, 6 Aug 2015 15:46:42 -0700
4 Subject: fsnotify: fix oops in fsnotify_clear_marks_by_group_flags()
5
6 From: Jan Kara <jack@suse.com>
7
8 commit 8f2f3eb59dff4ec538de55f2e0592fec85966aab upstream.
9
10 fsnotify_clear_marks_by_group_flags() can race with
11 fsnotify_destroy_marks() so that when fsnotify_destroy_mark_locked()
12 drops mark_mutex, a mark from the list iterated by
13 fsnotify_clear_marks_by_group_flags() can be freed and thus the next
14 entry pointer we have cached may become stale and we dereference free
15 memory.
16
17 Fix the problem by first moving marks to free to a special private list
18 and then always free the first entry in the special list. This method
19 is safe even when entries from the list can disappear once we drop the
20 lock.
21
22 Signed-off-by: Jan Kara <jack@suse.com>
23 Reported-by: Ashish Sangwan <a.sangwan@samsung.com>
24 Reviewed-by: Ashish Sangwan <a.sangwan@samsung.com>
25 Cc: Lino Sanfilippo <LinoSanfilippo@gmx.de>
26 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
27 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
28 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
29
30 ---
31 fs/notify/mark.c | 30 +++++++++++++++++++++++++-----
32 1 file changed, 25 insertions(+), 5 deletions(-)
33
34 --- a/fs/notify/mark.c
35 +++ b/fs/notify/mark.c
36 @@ -299,16 +299,36 @@ void fsnotify_clear_marks_by_group_flags
37 unsigned int flags)
38 {
39 struct fsnotify_mark *lmark, *mark;
40 + LIST_HEAD(to_free);
41
42 + /*
43 + * We have to be really careful here. Anytime we drop mark_mutex, e.g.
44 + * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
45 + * to_free list so we have to use mark_mutex even when accessing that
46 + * list. And freeing mark requires us to drop mark_mutex. So we can
47 + * reliably free only the first mark in the list. That's why we first
48 + * move marks to free to to_free list in one go and then free marks in
49 + * to_free list one by one.
50 + */
51 mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
52 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
53 - if (mark->flags & flags) {
54 - fsnotify_get_mark(mark);
55 - fsnotify_destroy_mark_locked(mark, group);
56 - fsnotify_put_mark(mark);
57 - }
58 + if (mark->flags & flags)
59 + list_move(&mark->g_list, &to_free);
60 }
61 mutex_unlock(&group->mark_mutex);
62 +
63 + while (1) {
64 + mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
65 + if (list_empty(&to_free)) {
66 + mutex_unlock(&group->mark_mutex);
67 + break;
68 + }
69 + mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
70 + fsnotify_get_mark(mark);
71 + fsnotify_destroy_mark_locked(mark, group);
72 + mutex_unlock(&group->mark_mutex);
73 + fsnotify_put_mark(mark);
74 + }
75 }
76
77 /*