Skip to contentSkip to conversation

event_notifier: prevent accidental use after close

PATCH In review Halil Pasic 2 participants 2 messages Mar 2, 2017 – Mar 3, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 event_notifier: prevent accidental use after close

Let's set the handles to the underlying facilities to their extremal value so no accidental misuse can happen, and to make it obvious that the notifier is dysfunctional. E.g. if we just close an fd but do not touch the int holding the fd eventually a read/write could succeed again when the fd gets reused, and corrupt the file addressed by the fd.

---

No strong feelings about this, but obviously, I do think it's worth a try.

The one who brought this unfortunate possibility to my attention was Michael Tsirkin.

---
 util/event_notifier-posix.c | 2 ++
 util/event_notifier-win32.c | 1 +
 2 files changed, 3 insertions(+)
util/event_notifier-posix.c +2 -0
index 7e40252..acdbe3b 100644
@@ -81,8 +81,10 @@ void event_notifier_cleanup(EventNotifier *e)
 {
     if (e->rfd != e->wfd) {
         close(e->rfd);
+        e->rfd = -1;
     }
     close(e->wfd);
+    e->wfd = -1;
 }
 
 int event_notifier_get_fd(const EventNotifier *e)
util/event_notifier-win32.c +1 -0
index 519fb59..62c53b0 100644
@@ -25,6 +25,7 @@ int event_notifier_init(EventNotifier *e, int active)
 void event_notifier_cleanup(EventNotifier *e)
 {
     CloseHandle(e->event);
+    e->event = NULL;
 }
 
 HANDLE event_notifier_get_handle(EventNotifier *e)
Signature · 1 line
2.8.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Halil Pasic

Let's set the handles to the underlying facilities to their extremal value so no accidental misuse can happen, and to make it obvious that the notifier is dysfunctional. E.g. if we just close an fd but do not touch the int holding the fd eventually a read/write could succeed again when the fd gets reused, and corrupt the file addressed by the fd.

---

No strong feelings about this, but obviously, I do think it's worth a try.

Quoted 6 lines

The one who brought this unfortunate possibility to my attention was Michael Tsirkin.

---
 util/event_notifier-posix.c | 2 ++
 util/event_notifier-win32.c | 1 +
 2 files changed, 3 insertions(+)