Skip to content

Commit 09e5f14

Browse files
LinoSanfilippo333eparis
authored andcommitted
fanotify: on group destroy allow all waiters to bypass permission check
When fanotify_release() is called, there may still be processes waiting for access permission. Currently only processes for which an event has already been queued into the groups access list will be woken up. Processes for which no event has been queued will continue to sleep and thus cause a deadlock when fsnotify_put_group() is called. Furthermore there is a race allowing further processes to be waiting on the access wait queue after wake_up (if they arrive before clear_marks_by_group() is called). This patch corrects this by setting a flag to inform processes that the group is about to be destroyed and thus not to wait for access permission. [additional changelog from eparis] Lets think about the 4 relevant code paths from the PoV of the 'operator' 'listener' 'responder' and 'closer'. Where operator is the process doing an action (like open/read) which could require permission. Listener is the task (or in this case thread) slated with reading from the fanotify file descriptor. The 'responder' is the thread responsible for responding to access requests. 'Closer' is the thread attempting to close the fanotify file descriptor. The 'operator' is going to end up in: fanotify_handle_event() get_response_from_access() (THIS BLOCKS WAITING ON USERSPACE) The 'listener' interesting code path fanotify_read() copy_event_to_user() prepare_for_access_response() (THIS CREATES AN fanotify_response_event) The 'responder' code path: fanotify_write() process_access_response() (REMOVE A fanotify_response_event, SET RESPONSE, WAKE UP 'operator') The 'closer': fanotify_release() (SUPPOSED TO CLEAN UP THE REST OF THIS MESS) What we have today is that in the closer we remove all of the fanotify_response_events and set a bit so no more response events are ever created in prepare_for_access_response(). The bug is that we never wake all of the operators up and tell them to move along. You fix that in fanotify_get_response_from_access(). You also fix other operators which haven't gotten there yet. So I agree that's a good fix. [/additional changelog from eparis] [remove additional changes to minimize patch size] [move initialization so it was inside CONFIG_FANOTIFY_PERMISSION] Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de> Signed-off-by: Eric Paris <eparis@redhat.com>
1 parent 1734dee commit 09e5f14

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ static int fanotify_get_response_from_access(struct fsnotify_group *group,
9292

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

95-
wait_event(group->fanotify_data.access_waitq, event->response);
95+
wait_event(group->fanotify_data.access_waitq, event->response ||
96+
atomic_read(&group->fanotify_data.bypass_perm));
97+
98+
if (!event->response) /* bypass_perm set */
99+
return 0;
96100

97101
/* userspace responded, convert to something usable */
98102
spin_lock(&event->lock);

fs/notify/fanotify/fanotify_user.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static int prepare_for_access_response(struct fsnotify_group *group,
200200

201201
mutex_lock(&group->fanotify_data.access_mutex);
202202

203-
if (group->fanotify_data.bypass_perm) {
203+
if (atomic_read(&group->fanotify_data.bypass_perm)) {
204204
mutex_unlock(&group->fanotify_data.access_mutex);
205205
kmem_cache_free(fanotify_response_event_cache, re);
206206
event->response = FAN_ALLOW;
@@ -390,7 +390,7 @@ static int fanotify_release(struct inode *ignored, struct file *file)
390390

391391
mutex_lock(&group->fanotify_data.access_mutex);
392392

393-
group->fanotify_data.bypass_perm = true;
393+
atomic_inc(&group->fanotify_data.bypass_perm);
394394

395395
list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
396396
pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
@@ -703,6 +703,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
703703
mutex_init(&group->fanotify_data.access_mutex);
704704
init_waitqueue_head(&group->fanotify_data.access_waitq);
705705
INIT_LIST_HEAD(&group->fanotify_data.access_list);
706+
atomic_set(&group->fanotify_data.bypass_perm, 0);
706707
#endif
707708
switch (flags & FAN_ALL_CLASS_BITS) {
708709
case FAN_CLASS_NOTIF:

include/linux/fsnotify_backend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ struct fsnotify_group {
166166
struct mutex access_mutex;
167167
struct list_head access_list;
168168
wait_queue_head_t access_waitq;
169-
bool bypass_perm; /* protected by access_mutex */
169+
atomic_t bypass_perm;
170170
#endif /* CONFIG_FANOTIFY_ACCESS_PERMISSIONS */
171171
int f_flags;
172172
unsigned int max_marks;

0 commit comments

Comments
 (0)