Skip to content

Commit 33daa38

Browse files
committed
workshop/ControlChannelServer: start the task from a DeferEvent
InvokeTask::Start() does not tell whether (and how) the coroutine has finished. If the "spawn" task finished synchronously with an error (e.g. because sending the error reply failed), OnTaskFinished() invoked OnControlPermanentError() which deletes this object, and then OnControl() accessed the freed object (use-after-free). Move the InvokeTask::Start() call out of OnControl()'s stack frame with a DeferEvent; this way, OnControl() returns before the task runs, and the completion callback can safely destroy this object.
1 parent 0a296fb commit 33daa38

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎debian/changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cm4all-workshop (7.19) unstable; urgency=low
55
* cron: remove unschedulable jobs from the scheduler
66
* cron: an invalid time zone affects only its own job
77
* cron: fix claim stall on next_run='-infinity'
8+
* workshop: fix use-after-free after failed spawn reply
89

910
--
1011

‎src/workshop/ControlChannelServer.cxx‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ WorkshopControlChannelServer::WorkshopControlChannelServer(EventLoop &_event_loo
2626
UniqueSocketDescriptor &&_socket,
2727
WorkshopControlChannelHandler &_handler) noexcept
2828
:socket(_event_loop, std::move(_socket), *this),
29-
handler(_handler) {}
29+
handler(_handler),
30+
defer_start(_event_loop, BIND_THIS_METHOD(OnDeferredStart)) {}
3031

3132
void
3233
WorkshopControlChannelServer::InvokeTemporaryError(const char *msg) noexcept
@@ -40,6 +41,16 @@ WorkshopControlChannelServer::StartTask(Co::InvokeTask &&_task) noexcept
4041
assert(!task);
4142

4243
task = std::move(_task);
44+
defer_start.Schedule();
45+
}
46+
47+
void
48+
WorkshopControlChannelServer::OnDeferredStart() noexcept
49+
{
50+
assert(task);
51+
52+
/* this may finish the task synchronously, and
53+
OnTaskFinished() may destroy this object */
4354
task.Start(BIND_THIS_METHOD(OnTaskFinished));
4455
}
4556

@@ -152,14 +163,10 @@ WorkshopControlChannelServer::OnControl(std::vector<std::string> &&args) noexcep
152163
} else if (cmd == "spawn"sv) {
153164
StartTask(OnSpawn(std::move(args)));
154165

155-
if (task) {
156-
/* the task is still running - wait for it to
157-
finish before we handle more requests */
158-
socket.Disable();
159-
return false;
160-
}
161-
162-
return true;
166+
/* wait for the task to finish before we handle more
167+
requests */
168+
socket.Disable();
169+
return false;
163170
} else {
164171
InvokeTemporaryError("unknown command on control channel");
165172
return true;

‎src/workshop/ControlChannelServer.hxx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "event/net/UdpHandler.hxx"
88
#include "event/net/UdpListener.hxx"
9+
#include "event/DeferEvent.hxx"
910
#include "co/InvokeTask.hxx"
1011

1112
#include <string>
@@ -23,6 +24,13 @@ class WorkshopControlChannelServer final : UdpHandler {
2324

2425
WorkshopControlChannelHandler &handler;
2526

27+
/**
28+
* Starts the #task. This moves the InvokeTask::Start() call
29+
* out of the OnControl() stack frame, because the task may
30+
* finish synchronously and destroy this object.
31+
*/
32+
DeferEvent defer_start;
33+
2634
Co::InvokeTask task;
2735

2836
public:
@@ -38,6 +46,7 @@ private:
3846
void InvokeTemporaryError(const char *msg) noexcept;
3947

4048
void StartTask(Co::InvokeTask &&_task) noexcept;
49+
void OnDeferredStart() noexcept;
4150
void OnTaskFinished(std::exception_ptr &&error) noexcept;
4251

4352
Co::InvokeTask OnSpawn(std::vector<std::string> args);

0 commit comments

Comments
 (0)