Commit 98070bb
FROMLIST: misc: fastrpc: fix use-after-free of fastrpc_user in workqueue context
There is a race between fastrpc_device_release() and the workqueue
that processes DSP responses. When the user closes the file descriptor,
fastrpc_device_release() frees the fastrpc_user structure. Concurrently,
an in-flight DSP invocation can complete and fastrpc_rpmsg_callback()
schedules context cleanup via schedule_work(&ctx->put_work). If the
workqueue runs fastrpc_context_free() in parallel with or after
fastrpc_device_release() has freed the user structure, it dereferences
the freed fastrpc_user. Depending on the state of the context at the
time of the race, any one of the following accesses can be hit:
1. fastrpc_buf_free() calls fastrpc_ipa_to_dma_addr(buf->fl->cctx, ...)
to strip the SID bits from the stored IOVA before passing the
physical address to dma_free_coherent().
2. fastrpc_free_map() reads map->fl->cctx->vmperms[0].vmid to
reconstruct the source permission bitmask needed for the
qcom_scm_assign_mem() call that returns memory from the DSP VM
back to HLOS.
3. fastrpc_free_map() acquires map->fl->lock to safely remove the
map node from the fl->maps list.
The resulting use-after-free manifests as:
pc : fastrpc_buf_free+0x38/0x80 [fastrpc]
lr : fastrpc_context_free+0xa8/0x1b0 [fastrpc]
fastrpc_context_free+0xa8/0x1b0 [fastrpc]
fastrpc_context_put_wq+0x78/0xa0 [fastrpc]
process_one_work+0x180/0x450
worker_thread+0x26c/0x388
Add kref-based reference counting to fastrpc_user. Have each invoke
context take a reference on the user at allocation time and release it
when the context is freed. Release the initial reference in
fastrpc_device_release() at file close. Move the teardown of the user
structure — freeing pending contexts, maps, mmaps, and the channel
context reference — into the kref release callback fastrpc_user_free(),
so that it runs only when the last reference is dropped, regardless of
whether that happens at device close or after the final in-flight
context completes.
Link:https://lore.kernel.org/all/20260518203507.3754994-1-anandu.e@oss.qualcomm.com/
Fixes: 6cffd79 ("misc: fastrpc: Add support for dmabuf exporter")
Cc: stable@kernel.org
Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>1 parent 7e60e1e commit 98070bb
1 file changed
Lines changed: 52 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
| 335 | + | |
| 336 | + | |
335 | 337 | | |
336 | 338 | | |
337 | 339 | | |
| |||
549 | 551 | | |
550 | 552 | | |
551 | 553 | | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
552 | 594 | | |
553 | 595 | | |
554 | 596 | | |
555 | 597 | | |
| 598 | + | |
556 | 599 | | |
557 | 600 | | |
558 | 601 | | |
559 | 602 | | |
560 | 603 | | |
| 604 | + | |
561 | 605 | | |
562 | 606 | | |
563 | 607 | | |
| |||
572 | 616 | | |
573 | 617 | | |
574 | 618 | | |
| 619 | + | |
| 620 | + | |
575 | 621 | | |
576 | 622 | | |
577 | 623 | | |
| |||
679 | 725 | | |
680 | 726 | | |
681 | 727 | | |
| 728 | + | |
| 729 | + | |
682 | 730 | | |
683 | 731 | | |
684 | 732 | | |
| |||
709 | 757 | | |
710 | 758 | | |
711 | 759 | | |
| 760 | + | |
712 | 761 | | |
713 | 762 | | |
714 | 763 | | |
| |||
1689 | 1738 | | |
1690 | 1739 | | |
1691 | 1740 | | |
1692 | | - | |
1693 | | - | |
1694 | | - | |
1695 | 1741 | | |
1696 | 1742 | | |
1697 | 1743 | | |
| |||
1700 | 1746 | | |
1701 | 1747 | | |
1702 | 1748 | | |
1703 | | - | |
1704 | | - | |
1705 | | - | |
1706 | | - | |
1707 | | - | |
1708 | | - | |
1709 | | - | |
1710 | | - | |
1711 | | - | |
1712 | | - | |
1713 | | - | |
1714 | | - | |
1715 | | - | |
1716 | | - | |
1717 | | - | |
1718 | 1749 | | |
1719 | | - | |
1720 | | - | |
1721 | | - | |
1722 | | - | |
1723 | 1750 | | |
| 1751 | + | |
| 1752 | + | |
1724 | 1753 | | |
1725 | 1754 | | |
1726 | 1755 | | |
| |||
1764 | 1793 | | |
1765 | 1794 | | |
1766 | 1795 | | |
| 1796 | + | |
1767 | 1797 | | |
1768 | 1798 | | |
1769 | 1799 | | |
| |||
0 commit comments