Commit c29b2a8
FROMLIST: misc: fastrpc: Add reference counting for fastrpc_user structure
Add reference counting using kref to the fastrpc_user structure to
prevent use-after-free issues when contexts are freed from workqueue
after device release.
The issue occurs when fastrpc_device_release() frees the user structure
while invoke contexts are still pending in the workqueue. When the
workqueue later calls fastrpc_context_free(), it attempts to access
buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:
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
Implement proper reference counting to fix this:
- Initialize kref in fastrpc_device_open()
- Take a reference in fastrpc_context_alloc() for each context
- Release the reference in fastrpc_context_free() when context is freed
- Release the initial reference in fastrpc_device_release()
This ensures the user structure remains valid as long as there are
contexts holding references to it, preventing the race condition.
Link: https://lore.kernel.org/all/20260226151121.818852-1-anandu.e@oss.qualcomm.com/
Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>1 parent 54f1bd4 commit c29b2a8
1 file changed
Lines changed: 31 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
328 | 328 | | |
329 | 329 | | |
330 | 330 | | |
| 331 | + | |
| 332 | + | |
331 | 333 | | |
332 | 334 | | |
333 | 335 | | |
| |||
515 | 517 | | |
516 | 518 | | |
517 | 519 | | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
518 | 539 | | |
519 | 540 | | |
520 | 541 | | |
521 | 542 | | |
| 543 | + | |
522 | 544 | | |
523 | 545 | | |
524 | 546 | | |
525 | 547 | | |
526 | 548 | | |
| 549 | + | |
527 | 550 | | |
528 | 551 | | |
529 | 552 | | |
| |||
539 | 562 | | |
540 | 563 | | |
541 | 564 | | |
| 565 | + | |
| 566 | + | |
542 | 567 | | |
543 | 568 | | |
544 | 569 | | |
| |||
646 | 671 | | |
647 | 672 | | |
648 | 673 | | |
| 674 | + | |
| 675 | + | |
649 | 676 | | |
650 | 677 | | |
651 | 678 | | |
| |||
676 | 703 | | |
677 | 704 | | |
678 | 705 | | |
| 706 | + | |
679 | 707 | | |
680 | 708 | | |
681 | 709 | | |
| |||
1700 | 1728 | | |
1701 | 1729 | | |
1702 | 1730 | | |
1703 | | - | |
1704 | | - | |
1705 | | - | |
1706 | | - | |
1707 | 1731 | | |
| 1732 | + | |
| 1733 | + | |
1708 | 1734 | | |
1709 | 1735 | | |
1710 | 1736 | | |
| |||
1748 | 1774 | | |
1749 | 1775 | | |
1750 | 1776 | | |
| 1777 | + | |
1751 | 1778 | | |
1752 | 1779 | | |
1753 | 1780 | | |
| |||
0 commit comments