summaryrefslogtreecommitdiffstats
path: root/fs/notify
diff options
context:
space:
mode:
authorJan Kara <jack@suse.cz>2019-01-08 09:18:02 -0500
committerJan Kara <jack@suse.cz>2019-02-18 06:41:16 -0500
commitfabf7f29b3e2ce5ed9741bf06f3583cd7e82ed1c (patch)
treee835a20d5acb44c7a58cf5c279a638f5d60e24d5 /fs/notify
parent40873284d7106fc0f0f4d2deae74b38fb18342cc (diff)
fanotify: Use interruptible wait when waiting for permission events
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation). Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event. Fix these problems by using interruptible sleep for waiting for response to fanotify event. This is slightly tricky though - we have to detect when the event got already reported to userspace as in that case we must not free the event. Instead we push the responsibility for freeing the event to the process that will write response to the event. Reported-by: Orion Poplawski <orion@nwra.com> Reported-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
Diffstat (limited to 'fs/notify')
-rw-r--r--fs/notify/fanotify/fanotify.c35
-rw-r--r--fs/notify/fanotify/fanotify.h3
-rw-r--r--fs/notify/fanotify/fanotify_user.c9
3 files changed, 42 insertions, 5 deletions
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index 812c975df7ec..ff7b8a1cdfe1 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -77,6 +77,13 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
77 return 0; 77 return 0;
78} 78}
79 79
80/*
81 * Wait for response to permission event. The function also takes care of
82 * freeing the permission event (or offloads that in case the wait is canceled
83 * by a signal). The function returns 0 in case access got allowed by userspace,
84 * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
85 * the wait got interrupted by a signal.
86 */
80static int fanotify_get_response(struct fsnotify_group *group, 87static int fanotify_get_response(struct fsnotify_group *group,
81 struct fanotify_perm_event *event, 88 struct fanotify_perm_event *event,
82 struct fsnotify_iter_info *iter_info) 89 struct fsnotify_iter_info *iter_info)
@@ -85,8 +92,29 @@ static int fanotify_get_response(struct fsnotify_group *group,
85 92
86 pr_debug("%s: group=%p event=%p\n", __func__, group, event); 93 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
87 94
88 wait_event(group->fanotify_data.access_waitq, 95 ret = wait_event_interruptible(group->fanotify_data.access_waitq,
89 event->state == FAN_EVENT_ANSWERED); 96 event->state == FAN_EVENT_ANSWERED);
97 /* Signal pending? */
98 if (ret < 0) {
99 spin_lock(&group->notification_lock);
100 /* Event reported to userspace and no answer yet? */
101 if (event->state == FAN_EVENT_REPORTED) {
102 /* Event will get freed once userspace answers to it */
103 event->state = FAN_EVENT_CANCELED;
104 spin_unlock(&group->notification_lock);
105 return ret;
106 }
107 /* Event not yet reported? Just remove it. */
108 if (event->state == FAN_EVENT_INIT)
109 fsnotify_remove_queued_event(group, &event->fae.fse);
110 /*
111 * Event may be also answered in case signal delivery raced
112 * with wakeup. In that case we have nothing to do besides
113 * freeing the event and reporting error.
114 */
115 spin_unlock(&group->notification_lock);
116 goto out;
117 }
90 118
91 /* userspace responded, convert to something usable */ 119 /* userspace responded, convert to something usable */
92 switch (event->response & ~FAN_AUDIT) { 120 switch (event->response & ~FAN_AUDIT) {
@@ -104,6 +132,8 @@ static int fanotify_get_response(struct fsnotify_group *group,
104 132
105 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, 133 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
106 group, event, ret); 134 group, event, ret);
135out:
136 fsnotify_destroy_event(group, &event->fae.fse);
107 137
108 return ret; 138 return ret;
109} 139}
@@ -406,7 +436,6 @@ static int fanotify_handle_event(struct fsnotify_group *group,
406 } else if (fanotify_is_perm_event(mask)) { 436 } else if (fanotify_is_perm_event(mask)) {
407 ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event), 437 ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
408 iter_info); 438 iter_info);
409 fsnotify_destroy_event(group, fsn_event);
410 } 439 }
411finish: 440finish:
412 if (fanotify_is_perm_event(mask)) 441 if (fanotify_is_perm_event(mask))
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index 480f281996d4..68b30504284c 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -12,7 +12,8 @@ extern struct kmem_cache *fanotify_perm_event_cachep;
12enum { 12enum {
13 FAN_EVENT_INIT, 13 FAN_EVENT_INIT,
14 FAN_EVENT_REPORTED, 14 FAN_EVENT_REPORTED,
15 FAN_EVENT_ANSWERED 15 FAN_EVENT_ANSWERED,
16 FAN_EVENT_CANCELED,
16}; 17};
17 18
18/* 19/*
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 3c272f61d341..56992b32c6bb 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -147,10 +147,17 @@ static void finish_permission_event(struct fsnotify_group *group,
147 unsigned int response) 147 unsigned int response)
148 __releases(&group->notification_lock) 148 __releases(&group->notification_lock)
149{ 149{
150 bool destroy = false;
151
150 assert_spin_locked(&group->notification_lock); 152 assert_spin_locked(&group->notification_lock);
151 event->response = response; 153 event->response = response;
152 event->state = FAN_EVENT_ANSWERED; 154 if (event->state == FAN_EVENT_CANCELED)
155 destroy = true;
156 else
157 event->state = FAN_EVENT_ANSWERED;
153 spin_unlock(&group->notification_lock); 158 spin_unlock(&group->notification_lock);
159 if (destroy)
160 fsnotify_destroy_event(group, &event->fae.fse);
154} 161}
155 162
156static int process_access_response(struct fsnotify_group *group, 163static int process_access_response(struct fsnotify_group *group,