From da858655d6934fc48bc12d87577ebdae5d6db0bb Mon Sep 17 00:00:00 2001 From: Alejandro Pacheco Date: Wed, 14 Oct 2026 16:52:00 +0200 Subject: [PATCH] coap_resource.c: Do not hold the next subscriber across a notification coap_notify_observers() walked the subscribers with LL_FOREACH_SAFE(), which holds on to the next subscription while the current one is notified. The request handler runs with the lock released, and a notification that cannot be sent disconnects the session, which deletes all of that session's subscriptions. When the next subscription was one of those, the walk carried on with freed memory. Mark the subscriptions to visit at the start of the walk and look up the next marked one from the head of the list after each notification, so nothing deleted during a notification is used afterwards. Subscriptions added during the walk are not notified in that pass, as before. Add a unit test. --- include/coap3/coap_subscribe_internal.h | 2 + src/coap_resource.c | 23 ++++++- tests/test_session.c | 85 +++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/include/coap3/coap_subscribe_internal.h b/include/coap3/coap_subscribe_internal.h index a952a974..ad149bc6 100644 --- a/include/coap3/coap_subscribe_internal.h +++ b/include/coap3/coap_subscribe_internal.h @@ -65,6 +65,8 @@ struct coap_subscription_t { uint8_t dirty; /**< set if the notification temporarily could not be * sent (in that case, the resource's partially * dirty flag is set too) */ + uint8_t to_visit; /**< set while coap_notify_observers() has still to + * visit this subscription */ coap_cache_key_t *cache_key; /** cache_key to identify requester */ coap_pdu_t *pdu; /**< PDU to use for additional requests */ }; diff --git a/src/coap_resource.c b/src/coap_resource.c index 969af89c..0864d12c 100644 --- a/src/coap_resource.c +++ b/src/coap_resource.c @@ -1183,7 +1183,7 @@ static void coap_notify_observers(coap_context_t *context, coap_resource_t *r, coap_deleting_resource_t deleting) { coap_method_handler_t h; - coap_subscription_t *obs, *otmp; + coap_subscription_t *obs; coap_pdu_t *response; uint8_t buf[4]; coap_string_t *query; @@ -1201,11 +1201,30 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, r->partiallydirty = 0; - LL_FOREACH_SAFE(r->subscribers, obs, otmp) { + /* + * Any subscription (not just the one being notified) may get deleted + * in the callback, by another running thread when executing the + * callback, or when sending a response (e.g. the session fails and all + * of its subscriptions are deleted). So no pointer into the list is kept + * over a notification: the next subscription to visit is looked up again + * from the head of the list each time. + */ + LL_FOREACH(r->subscribers, obs) { + obs->to_visit = 1; + } + for (;;) { coap_session_t *obs_session; coap_pdu_t *obs_pdu; coap_mid_t mid = COAP_INVALID_MID; + LL_FOREACH(r->subscribers, obs) { + if (obs->to_visit) + break; + } + if (obs == NULL) + break; + obs->to_visit = 0; + if ((r->dirty == 0 && obs->dirty == 0) || obs->session->is_rate_limiting) { /* * running this resource due to partiallydirty, but this observation's diff --git a/tests/test_session.c b/tests/test_session.c index e7de4c7f..f875ab43 100644 --- a/tests/test_session.c +++ b/tests/test_session.c @@ -115,6 +115,88 @@ t_observe_local_address(void) { } #endif +#if COAP_SERVER_SUPPORT +static void +notify_fail_handler(coap_resource_t *resource COAP_UNUSED, + coap_session_t *s COAP_UNUSED, + const coap_pdu_t *request COAP_UNUSED, + const coap_string_t *query COAP_UNUSED, + coap_pdu_t *response) { + coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); + coap_add_data(response, 1, (const uint8_t *)"x"); +} + +/* A notification that cannot be sent disconnects the session, which removes + * all of its observers while the list of observers is being walked (the + * use of a removed observer is reported by ASan or valgrind) */ +static void +t_session_notify_send_fail(void) { + static const char *queries[] = { "a", "b", "c" }; + coap_context_t *nctx; + coap_session_t *nsession; + coap_resource_t *r; + coap_address_t addr; + coap_subscription_t *s; + unsigned int count = 0; + size_t i; + + coap_address_init(&addr); + addr.size = sizeof(struct sockaddr_in6); + addr.addr.sin6.sin6_family = AF_INET6; + addr.addr.sin6.sin6_addr = in6addr_loopback; + addr.addr.sin6.sin6_port = htons(20004); + + nctx = coap_new_context(NULL); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(nctx); + r = coap_resource_init(coap_make_str_const("obs"), 0); + if (!r) { + CU_FAIL("no resource"); + coap_free_context(nctx); + return; + } + coap_resource_set_get_observable(r, 1); + coap_register_request_handler(r, COAP_REQUEST_GET, notify_fail_handler); + coap_add_resource(nctx, r); + nsession = coap_new_client_session(nctx, NULL, &addr, COAP_PROTO_UDP); + if (!nsession) { + CU_FAIL("no session"); + coap_free_context(nctx); + return; + } + + coap_lock_lock(return); + for (i = 0; i < sizeof(queries) / sizeof(queries[0]); i++) { + coap_pdu_t *request = coap_pdu_init(COAP_MESSAGE_CON, COAP_REQUEST_CODE_GET, + coap_new_message_id_lkd(nsession), 64); + uint8_t token = (uint8_t)('1' + i); + + CU_ASSERT_PTR_NOT_NULL(request); + if (!request) + continue; + coap_add_token(request, 1, &token); + coap_add_option(request, COAP_OPTION_OBSERVE, 0, NULL); + coap_add_option(request, COAP_OPTION_URI_PATH, 3, (const uint8_t *)"obs"); + coap_add_option(request, COAP_OPTION_URI_QUERY, 1, + (const uint8_t *)queries[i]); + CU_ASSERT_PTR_NOT_NULL(coap_add_observer(r, nsession, &request->actual_token, + request)); + coap_delete_pdu_lkd(request); + } + /* The second notification fails to be sent */ + coap_debug_set_packet_fail("2"); + coap_resource_notify_observers_lkd(r, COAP_NOT_DELETING_RESOURCE); + coap_debug_set_packet_fail("0%"); + coap_lock_unlock(); + + /* The failed send dropped the session's observers */ + LL_COUNT(r->subscribers, s, count); + CU_ASSERT(count == 0); + + coap_session_release(nsession); + coap_free_context(nctx); +} +#endif /* COAP_SERVER_SUPPORT */ + static void t_session1(void) { CU_ASSERT(session->ref == 1); @@ -403,6 +485,9 @@ t_init_session_tests(void) { SESSION_TEST(suite, t_session3); SESSION_TEST(suite, t_session4); SESSION_TEST(suite, t_session5); +#if COAP_SERVER_SUPPORT + SESSION_TEST(suite, t_session_notify_send_fail); +#endif /* COAP_SERVER_SUPPORT */ SESSION_TEST(suite, t_session6); #if COAP_PROXY_SUPPORT SESSION_TEST(suite, t_session_proxy_remove);