Commit b3036d0
Anandu Krishnan E
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 dd29e69 commit b3036d0
1 file changed
Lines changed: 52 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
| 333 | + | |
332 | 334 | | |
333 | 335 | | |
334 | 336 | | |
| |||
500 | 502 | | |
501 | 503 | | |
502 | 504 | | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
503 | 545 | | |
504 | 546 | | |
505 | 547 | | |
506 | 548 | | |
| 549 | + | |
507 | 550 | | |
508 | 551 | | |
509 | 552 | | |
510 | 553 | | |
511 | 554 | | |
| 555 | + | |
512 | 556 | | |
513 | 557 | | |
514 | 558 | | |
| |||
523 | 567 | | |
524 | 568 | | |
525 | 569 | | |
| 570 | + | |
| 571 | + | |
526 | 572 | | |
527 | 573 | | |
528 | 574 | | |
| |||
632 | 678 | | |
633 | 679 | | |
634 | 680 | | |
| 681 | + | |
| 682 | + | |
635 | 683 | | |
636 | 684 | | |
637 | 685 | | |
| |||
662 | 710 | | |
663 | 711 | | |
664 | 712 | | |
| 713 | + | |
665 | 714 | | |
666 | 715 | | |
667 | 716 | | |
| |||
1627 | 1676 | | |
1628 | 1677 | | |
1629 | 1678 | | |
1630 | | - | |
1631 | | - | |
1632 | | - | |
1633 | 1679 | | |
1634 | 1680 | | |
1635 | 1681 | | |
| |||
1638 | 1684 | | |
1639 | 1685 | | |
1640 | 1686 | | |
1641 | | - | |
1642 | | - | |
1643 | | - | |
1644 | | - | |
1645 | | - | |
1646 | | - | |
1647 | | - | |
1648 | | - | |
1649 | | - | |
1650 | | - | |
1651 | | - | |
1652 | | - | |
1653 | | - | |
1654 | | - | |
1655 | | - | |
1656 | 1687 | | |
1657 | | - | |
1658 | | - | |
1659 | | - | |
1660 | | - | |
1661 | 1688 | | |
| 1689 | + | |
| 1690 | + | |
1662 | 1691 | | |
1663 | 1692 | | |
1664 | 1693 | | |
| |||
1702 | 1731 | | |
1703 | 1732 | | |
1704 | 1733 | | |
| 1734 | + | |
1705 | 1735 | | |
1706 | 1736 | | |
1707 | 1737 | | |
| |||
0 commit comments