Skip to content

Commit 937c57f

Browse files
committed
Avoid use after free in sql_trunk_query_cancel
Some SQL drivers will immediately cancel in response to trunk_request_signal_cancel() resulting the freeing of treq, and due to the re-parenting, query_ctx.
1 parent a9ba8f8 commit 937c57f

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/modules/rlm_sql/sql.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,16 @@ static unlang_action_t sql_trunk_query_start(UNUSED unlang_result_t *p_result,
209209
static void sql_trunk_query_cancel(UNUSED request_t *request, UNUSED fr_signal_t action, void *uctx)
210210
{
211211
fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t);
212+
trunk_request_t *treq;
212213

213214
if (!query_ctx->treq) return;
215+
treq = query_ctx->treq;
214216

215217
/*
216218
* A reapable trunk request has already completed.
217219
*/
218-
if (unlikely(query_ctx->treq->state == TRUNK_REQUEST_STATE_REAPABLE)) {
219-
trunk_request_signal_complete(query_ctx->treq);
220+
if (unlikely(treq->state == TRUNK_REQUEST_STATE_REAPABLE)) {
221+
trunk_request_signal_complete(treq);
220222
query_ctx->treq = NULL;
221223
return;
222224
}
@@ -225,11 +227,10 @@ static void sql_trunk_query_cancel(UNUSED request_t *request, UNUSED fr_signal_t
225227
* The query_ctx needs to be parented by the treq so that it still exists
226228
* when the cancel_mux callback is run.
227229
*/
228-
if (query_ctx->inst->driver->trunk_io_funcs.request_cancel_mux) talloc_steal(query_ctx->treq, query_ctx);
229-
230-
trunk_request_signal_cancel(query_ctx->treq);
231-
230+
if (query_ctx->inst->driver->trunk_io_funcs.request_cancel_mux) talloc_steal(treq, query_ctx);
232231
query_ctx->treq = NULL;
232+
233+
trunk_request_signal_cancel(treq);
233234
}
234235

235236
/** Submit an SQL query using a trunk connection.

0 commit comments

Comments
 (0)