Skip to content

Commit 96d4101

Browse files
jankaratorvalds
authored andcommitted
fanotify: fix list corruption in fanotify_get_response()
fanotify_get_response() calls fsnotify_remove_event() when it finds that group is being released from fanotify_release() (bypass_perm is set). However the event it removes need not be only in the group's notification queue but it can have already moved to access_list (userspace read the event before closing the fanotify instance fd) which is protected by a different lock. Thus when fsnotify_remove_event() races with fanotify_release() operating on access_list, the list can get corrupted. Fix the problem by moving all the logic removing permission events from the lists to one place - fanotify_release(). Fixes: 5838d44 ("fanotify: fix double free of pending permission events") Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Jan Kara <[email protected]> Reported-by: Miklos Szeredi <[email protected]> Tested-by: Miklos Szeredi <[email protected]> Reviewed-by: Miklos Szeredi <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 12703db commit 96d4101

File tree

4 files changed

+25
-42
lines changed

4 files changed

+25
-42
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,7 @@ static int fanotify_get_response(struct fsnotify_group *group,
6767

6868
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
6969

70-
wait_event(group->fanotify_data.access_waitq, event->response ||
71-
atomic_read(&group->fanotify_data.bypass_perm));
72-
73-
if (!event->response) { /* bypass_perm set */
74-
/*
75-
* Event was canceled because group is being destroyed. Remove
76-
* it from group's event list because we are responsible for
77-
* freeing the permission event.
78-
*/
79-
fsnotify_remove_event(group, &event->fae.fse);
80-
return 0;
81-
}
70+
wait_event(group->fanotify_data.access_waitq, event->response);
8271

8372
/* userspace responded, convert to something usable */
8473
switch (event->response) {

fs/notify/fanotify/fanotify_user.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,16 +358,20 @@ static int fanotify_release(struct inode *ignored, struct file *file)
358358

359359
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
360360
struct fanotify_perm_event_info *event, *next;
361+
struct fsnotify_event *fsn_event;
361362

362363
/*
363-
* There may be still new events arriving in the notification queue
364-
* but since userspace cannot use fanotify fd anymore, no event can
365-
* enter or leave access_list by now.
364+
* Stop new events from arriving in the notification queue. since
365+
* userspace cannot use fanotify fd anymore, no event can enter or
366+
* leave access_list by now either.
366367
*/
367-
spin_lock(&group->fanotify_data.access_lock);
368-
369-
atomic_inc(&group->fanotify_data.bypass_perm);
368+
fsnotify_group_stop_queueing(group);
370369

370+
/*
371+
* Process all permission events on access_list and notification queue
372+
* and simulate reply from userspace.
373+
*/
374+
spin_lock(&group->fanotify_data.access_lock);
371375
list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
372376
fae.fse.list) {
373377
pr_debug("%s: found group=%p event=%p\n", __func__, group,
@@ -379,12 +383,21 @@ static int fanotify_release(struct inode *ignored, struct file *file)
379383
spin_unlock(&group->fanotify_data.access_lock);
380384

381385
/*
382-
* Since bypass_perm is set, newly queued events will not wait for
383-
* access response. Wake up the already sleeping ones now.
384-
* synchronize_srcu() in fsnotify_destroy_group() will wait for all
385-
* processes sleeping in fanotify_handle_event() waiting for access
386-
* response and thus also for all permission events to be freed.
386+
* Destroy all non-permission events. For permission events just
387+
* dequeue them and set the response. They will be freed once the
388+
* response is consumed and fanotify_get_response() returns.
387389
*/
390+
mutex_lock(&group->notification_mutex);
391+
while (!fsnotify_notify_queue_is_empty(group)) {
392+
fsn_event = fsnotify_remove_first_event(group);
393+
if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS))
394+
fsnotify_destroy_event(group, fsn_event);
395+
else
396+
FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
397+
}
398+
mutex_unlock(&group->notification_mutex);
399+
400+
/* Response for all permission events it set, wakeup waiters */
388401
wake_up(&group->fanotify_data.access_waitq);
389402
#endif
390403

@@ -755,7 +768,6 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
755768
spin_lock_init(&group->fanotify_data.access_lock);
756769
init_waitqueue_head(&group->fanotify_data.access_waitq);
757770
INIT_LIST_HEAD(&group->fanotify_data.access_list);
758-
atomic_set(&group->fanotify_data.bypass_perm, 0);
759771
#endif
760772
switch (flags & FAN_ALL_CLASS_BITS) {
761773
case FAN_CLASS_NOTIF:

fs/notify/notification.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,6 @@ int fsnotify_add_event(struct fsnotify_group *group,
131131
return ret;
132132
}
133133

134-
/*
135-
* Remove @event from group's notification queue. It is the responsibility of
136-
* the caller to destroy the event.
137-
*/
138-
void fsnotify_remove_event(struct fsnotify_group *group,
139-
struct fsnotify_event *event)
140-
{
141-
mutex_lock(&group->notification_mutex);
142-
if (!list_empty(&event->list)) {
143-
list_del_init(&event->list);
144-
group->q_len--;
145-
}
146-
mutex_unlock(&group->notification_mutex);
147-
}
148-
149134
/*
150135
* Remove and return the first event from the notification list. It is the
151136
* responsibility of the caller to destroy the obtained event

include/linux/fsnotify_backend.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ struct fsnotify_group {
180180
spinlock_t access_lock;
181181
struct list_head access_list;
182182
wait_queue_head_t access_waitq;
183-
atomic_t bypass_perm;
184183
#endif /* CONFIG_FANOTIFY_ACCESS_PERMISSIONS */
185184
int f_flags;
186185
unsigned int max_marks;
@@ -307,8 +306,6 @@ extern int fsnotify_add_event(struct fsnotify_group *group,
307306
struct fsnotify_event *event,
308307
int (*merge)(struct list_head *,
309308
struct fsnotify_event *));
310-
/* Remove passed event from groups notification queue */
311-
extern void fsnotify_remove_event(struct fsnotify_group *group, struct fsnotify_event *event);
312309
/* true if the group notification queue is empty */
313310
extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
314311
/* return, but do not dequeue the first event on the notification queue */

0 commit comments

Comments
 (0)