Commit 875e23d
committed
schedule: zephyr_ll: fix use-after-free when freeing an active user-space task
zephyr_ll_task_sched_free() frees an active (RUNNING/RESCHEDULE) task by
setting pdata->freeing and waiting on pdata->sem for the scheduler thread
to remove the task from its run list before the memory is released.
Under CONFIG_SOF_USERSPACE_LL this function runs in kernel context while
pdata->sem is a sys_sem allocated on the user heap. sys_sem_take() returns
-EINVAL immediately when called from kernel context, so the wait is a
no-op: pdata is freed (and the struct task is subsequently freed by
pipeline_free()) while the task is still linked in sch->tasks with
n_tasks != 0 and the scheduling domain handler still set. Because n_tasks
is non-zero, schedule_free() does not stop the LL timer, and the next
timer tick runs zephyr_ll_run() over the dangling task, dereferencing
freed memory and taking a load/store-privilege exception (EXCCAUSE 26) in
the user-space LL thread.
Stop relying on the cross-privilege semaphore handshake in this path. When
the task must be waited for, mark it cancelled so that, should it actually
be mid-execution on the scheduler's temporary list, it is removed via the
cancel path without re-running task->run() on resources the caller may
already have freed. If the task is still linked on the run list, the
scheduler thread is provably not executing it (a running task is moved off
sch->tasks with the lock dropped), so remove it directly and skip the
wait. This guarantees the task is delisted (n_tasks -> 0, handler -> NULL)
before pdata is freed, eliminating both the dangling list entry and the
stray timer wakeups.
Verified on PTL with the standalone user-space LL boot tests: the
userspace_ll suite, including pipeline_two_components_user, now passes
without the fatal exception at teardown.
Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>1 parent 9b146ad commit 875e23d
1 file changed
Lines changed: 41 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
121 | 135 | | |
122 | 136 | | |
123 | 137 | | |
| |||
469 | 483 | | |
470 | 484 | | |
471 | 485 | | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
472 | 513 | | |
473 | 514 | | |
474 | 515 | | |
| |||
0 commit comments