Skip to content

Commit 989cc61

Browse files
authored
[hrtimer] fixup use-after-free (#8928)
Signed-off-by: Shell <[email protected]>
1 parent 5dba9a4 commit 989cc61

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

components/drivers/ipc/completion_mp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ rt_err_t rt_completion_wakeup_by_errno(struct rt_completion *completion,
326326
}
327327

328328
/* safe to assume publication done even on resume failure */
329-
rt_thread_resume(suspend_thread);
330329
RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) ==
331330
RT_WAKING);
332331
IPC_STORE(&completion->susp_thread_n_flag, RT_UNCOMPLETED,
333332
memory_order_release);
333+
rt_thread_resume(suspend_thread);
334334
error = RT_EOK;
335335
break;
336336
}

components/drivers/ktime/src/hrtimer.c

+10-19
Original file line numberDiff line numberDiff line change
@@ -111,32 +111,29 @@ static void _sleep_timeout(void *parameter)
111111
rt_completion_done(&timer->completion);
112112
}
113113

114-
static void _set_next_timeout(void);
114+
static void _set_next_timeout_n_unlock(rt_base_t level);
115115
static void _timeout_callback(void *parameter)
116116
{
117117
rt_ktime_hrtimer_t timer;
118118
timer = (rt_ktime_hrtimer_t)parameter;
119119
rt_base_t level;
120120

121+
level = rt_spin_lock_irqsave(&_spinlock);
122+
_nowtimer = RT_NULL;
123+
rt_list_remove(&(timer->row));
124+
121125
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
122126
{
123127
timer->timeout_func(timer->parameter);
124128
}
125129

126-
level = rt_spin_lock_irqsave(&_spinlock);
127-
_nowtimer = RT_NULL;
128-
rt_list_remove(&(timer->row));
129-
rt_spin_unlock_irqrestore(&_spinlock, level);
130-
131-
_set_next_timeout();
130+
_set_next_timeout_n_unlock(level);
132131
}
133132

134-
static void _set_next_timeout(void)
133+
static void _set_next_timeout_n_unlock(rt_base_t level)
135134
{
136135
rt_ktime_hrtimer_t t;
137-
rt_base_t level;
138136

139-
level = rt_spin_lock_irqsave(&_spinlock);
140137
if (&_timer_list != _timer_list.prev)
141138
{
142139
t = rt_list_entry((&_timer_list)->next, struct rt_ktime_hrtimer, row);
@@ -202,9 +199,6 @@ rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer)
202199
/* parameter check */
203200
RT_ASSERT(timer != RT_NULL);
204201

205-
/* notify the timer stop event */
206-
rt_completion_wakeup_by_errno(&timer->completion, RT_ERROR);
207-
208202
level = rt_spin_lock_irqsave(&_spinlock);
209203
rt_list_remove(&timer->row); /* remove timer from list */
210204
/* change status of timer */
@@ -228,9 +222,8 @@ rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer)
228222
}
229223
rt_list_insert_after(timer_list, &(timer->row));
230224
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
231-
rt_spin_unlock_irqrestore(&_spinlock, level);
232225

233-
_set_next_timeout();
226+
_set_next_timeout_n_unlock(level);
234227

235228
return RT_EOK;
236229
}
@@ -250,9 +243,8 @@ rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer)
250243
_nowtimer = RT_NULL;
251244
rt_list_remove(&timer->row);
252245
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; /* change status */
253-
rt_spin_unlock_irqrestore(&_spinlock, level);
254246

255-
_set_next_timeout();
247+
_set_next_timeout_n_unlock(level);
256248

257249
return RT_EOK;
258250
}
@@ -344,8 +336,7 @@ rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer)
344336
{
345337
_nowtimer = RT_NULL;
346338
rt_list_remove(&timer->row);
347-
rt_spin_unlock_irqrestore(&_spinlock, level);
348-
_set_next_timeout();
339+
_set_next_timeout_n_unlock(level);
349340
}
350341
else
351342
{

src/cpu_mp.c

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ RTM_EXPORT(rt_cpus_lock_status_restore);
219219

220220
/* A safe API with debugging feature to be called in most codes */
221221

222+
#undef rt_cpu_get_id
222223
/**
223224
* @brief Get logical CPU ID
224225
*

0 commit comments

Comments
 (0)