From 30fef395b45b82b4f7cdf8b5018f8c81628b2ef1 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 13 Mar 2023 12:14:55 +0000 Subject: [PATCH] Increase dc_context_t reference count during dc_configure() This fixes use-after-free in case dc_context_unref() is called while the background process spawned by dc_configure() is still running. Corresponding regression test in Python can be run with `pytest tests/test_1_online.py::test_configure_unref`. --- deltachat-ffi/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 0e5ac85e3..630a66f33 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -428,9 +428,10 @@ pub unsafe extern "C" fn dc_configure(context: *mut dc_context_t) { return; } - let ctx = &*context; + // Clone the context Arc so we do not use the reference after dc_configure() returns. + let ctx = (*context).clone(); - spawn(async move { ctx.configure().await.log_err(ctx, "Configure failed") }); + spawn(async move { ctx.configure().await.log_err(&ctx, "Configure failed") }); } #[no_mangle]