Skip to content

Commit 533ef58

Browse files
cvinayakcfriedt
authored andcommitted
Bluetooth: Controller: Fix use-after-release in lll_scan/lll_scan_aux
Fix use-after-release in lll_scan/lll_scan_aux when using mayfly_enqueue to defer execution of the offset calculation using ull_sched_mfy_after_cen_offset_get(). Apply suggestion from @Copilot Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent d0ab7a2 commit 533ef58

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

subsys/bluetooth/controller/ll_sw/lll_scan.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ struct lll_scan {
1717
uint8_t adv_addr[BDADDR_SIZE];
1818
uint32_t conn_win_offset_us;
1919
uint16_t conn_timeout;
20+
21+
#if defined(CONFIG_BT_CTLR_SCHED_ADVANCED)
22+
/* Stores prepare parameters for deferred mayfly execution.
23+
* This prevents use-after-release issues by ensuring the parameters
24+
* remain valid until execution.
25+
*/
26+
struct lll_prepare_param prepare_param;
27+
#endif /* CONFIG_BT_CTLR_SCHED_ADVANCED */
2028
#endif /* CONFIG_BT_CENTRAL */
2129

2230
uint8_t state:1;

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_scan.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,19 @@ static int common_prepare_cb(struct lll_prepare_param *p, bool is_resume)
509509
static memq_link_t link;
510510
static struct mayfly mfy_after_cen_offset_get = {
511511
0U, 0U, &link, NULL, ull_sched_mfy_after_cen_offset_get};
512-
uint32_t retval;
512+
struct lll_prepare_param *prepare_param;
513513

514-
mfy_after_cen_offset_get.param = p;
514+
/* Copy the required values to calculate the offsets */
515+
prepare_param = &lll->prepare_param;
516+
prepare_param->ticks_at_expire = p->ticks_at_expire;
517+
prepare_param->remainder = p->remainder;
518+
prepare_param->param = lll;
515519

516-
retval = mayfly_enqueue(TICKER_USER_ID_LLL, TICKER_USER_ID_ULL_LOW, 1U,
517-
&mfy_after_cen_offset_get);
518-
LL_ASSERT_ERR(!retval);
520+
mfy_after_cen_offset_get.param = prepare_param;
521+
522+
ret = mayfly_enqueue(TICKER_USER_ID_LLL, TICKER_USER_ID_ULL_LOW, 1U,
523+
&mfy_after_cen_offset_get);
524+
LL_ASSERT_ERR(!ret);
519525
}
520526
#endif /* CONFIG_BT_CENTRAL && CONFIG_BT_CTLR_SCHED_ADVANCED */
521527

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_scan_aux.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,19 @@ static int prepare_cb(struct lll_prepare_param *p)
607607
static memq_link_t link;
608608
static struct mayfly mfy_after_cen_offset_get = {
609609
0U, 0U, &link, NULL, ull_sched_mfy_after_cen_offset_get};
610+
struct lll_prepare_param *prepare_param;
610611

611-
/* NOTE: LLL scan instance passed, as done when
612+
/* Copy the required values to calculate the offsets
613+
*
614+
* NOTE: LLL scan instance passed, as done when
612615
* establishing legacy connections.
613616
*/
614-
p->param = lll;
615-
mfy_after_cen_offset_get.param = p;
617+
prepare_param = &lll->prepare_param;
618+
prepare_param->ticks_at_expire = p->ticks_at_expire;
619+
prepare_param->remainder = p->remainder;
620+
prepare_param->param = lll;
621+
622+
mfy_after_cen_offset_get.param = prepare_param;
616623

617624
ret = mayfly_enqueue(TICKER_USER_ID_LLL, TICKER_USER_ID_ULL_LOW, 1U,
618625
&mfy_after_cen_offset_get);

0 commit comments

Comments
 (0)