]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/3.16.3/fanotify-fix-double-free-of-pending-permission-events.patch
4.9-stable patches
[thirdparty/kernel/stable-queue.git] / releases / 3.16.3 / fanotify-fix-double-free-of-pending-permission-events.patch
1 From 5838d4442bd5971687b72221736222637e03140d Mon Sep 17 00:00:00 2001
2 From: Jan Kara <jack@suse.cz>
3 Date: Wed, 6 Aug 2014 16:03:28 -0700
4 Subject: fanotify: fix double free of pending permission events
5
6 From: Jan Kara <jack@suse.cz>
7
8 commit 5838d4442bd5971687b72221736222637e03140d upstream.
9
10 Commit 85816794240b ("fanotify: Fix use after free for permission
11 events") introduced a double free issue for permission events which are
12 pending in group's notification queue while group is being destroyed.
13 These events are freed from fanotify_handle_event() but they are not
14 removed from groups notification queue and thus they get freed again
15 from fsnotify_flush_notify().
16
17 Fix the problem by removing permission events from notification queue
18 before freeing them if we skip processing access response. Also expand
19 comments in fanotify_release() to explain group shutdown in detail.
20
21 Fixes: 85816794240b9659e66e4d9b0df7c6e814e5f603
22 Signed-off-by: Jan Kara <jack@suse.cz>
23 Reported-by: Douglas Leeder <douglas.leeder@sophos.com>
24 Tested-by: Douglas Leeder <douglas.leeder@sophos.com>
25 Reported-by: Heinrich Schuchard <xypron.glpk@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/fanotify/fanotify.c | 9 ++++++++-
32 fs/notify/fanotify/fanotify_user.c | 12 ++++++++++++
33 fs/notify/notification.c | 18 +++++++++++++++++-
34 include/linux/fsnotify_backend.h | 2 ++
35 4 files changed, 39 insertions(+), 2 deletions(-)
36
37 --- a/fs/notify/fanotify/fanotify.c
38 +++ b/fs/notify/fanotify/fanotify.c
39 @@ -70,8 +70,15 @@ static int fanotify_get_response(struct
40 wait_event(group->fanotify_data.access_waitq, event->response ||
41 atomic_read(&group->fanotify_data.bypass_perm));
42
43 - if (!event->response) /* bypass_perm set */
44 + if (!event->response) { /* bypass_perm set */
45 + /*
46 + * Event was canceled because group is being destroyed. Remove
47 + * it from group's event list because we are responsible for
48 + * freeing the permission event.
49 + */
50 + fsnotify_remove_event(group, &event->fae.fse);
51 return 0;
52 + }
53
54 /* userspace responded, convert to something usable */
55 switch (event->response) {
56 --- a/fs/notify/fanotify/fanotify_user.c
57 +++ b/fs/notify/fanotify/fanotify_user.c
58 @@ -359,6 +359,11 @@ static int fanotify_release(struct inode
59 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
60 struct fanotify_perm_event_info *event, *next;
61
62 + /*
63 + * There may be still new events arriving in the notification queue
64 + * but since userspace cannot use fanotify fd anymore, no event can
65 + * enter or leave access_list by now.
66 + */
67 spin_lock(&group->fanotify_data.access_lock);
68
69 atomic_inc(&group->fanotify_data.bypass_perm);
70 @@ -373,6 +378,13 @@ static int fanotify_release(struct inode
71 }
72 spin_unlock(&group->fanotify_data.access_lock);
73
74 + /*
75 + * Since bypass_perm is set, newly queued events will not wait for
76 + * access response. Wake up the already sleeping ones now.
77 + * synchronize_srcu() in fsnotify_destroy_group() will wait for all
78 + * processes sleeping in fanotify_handle_event() waiting for access
79 + * response and thus also for all permission events to be freed.
80 + */
81 wake_up(&group->fanotify_data.access_waitq);
82 #endif
83
84 --- a/fs/notify/notification.c
85 +++ b/fs/notify/notification.c
86 @@ -73,7 +73,8 @@ void fsnotify_destroy_event(struct fsnot
87 /* Overflow events are per-group and we don't want to free them */
88 if (!event || event->mask == FS_Q_OVERFLOW)
89 return;
90 -
91 + /* If the event is still queued, we have a problem... */
92 + WARN_ON(!list_empty(&event->list));
93 group->ops->free_event(event);
94 }
95
96 @@ -125,6 +126,21 @@ queue:
97 }
98
99 /*
100 + * Remove @event from group's notification queue. It is the responsibility of
101 + * the caller to destroy the event.
102 + */
103 +void fsnotify_remove_event(struct fsnotify_group *group,
104 + struct fsnotify_event *event)
105 +{
106 + mutex_lock(&group->notification_mutex);
107 + if (!list_empty(&event->list)) {
108 + list_del_init(&event->list);
109 + group->q_len--;
110 + }
111 + mutex_unlock(&group->notification_mutex);
112 +}
113 +
114 +/*
115 * Remove and return the first event from the notification list. It is the
116 * responsibility of the caller to destroy the obtained event
117 */
118 --- a/include/linux/fsnotify_backend.h
119 +++ b/include/linux/fsnotify_backend.h
120 @@ -326,6 +326,8 @@ extern int fsnotify_add_notify_event(str
121 struct fsnotify_event *event,
122 int (*merge)(struct list_head *,
123 struct fsnotify_event *));
124 +/* Remove passed event from groups notification queue */
125 +extern void fsnotify_remove_event(struct fsnotify_group *group, struct fsnotify_event *event);
126 /* true if the group notification queue is empty */
127 extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
128 /* return, but do not dequeue the first event on the notification queue */