Commit 68973f9
can: bcm: defer rx_op deallocation to workqueue to fix thrtimer UAF
Commit f1b4e32 ("can: bcm: use call_rcu() instead of costly
synchronize_rcu()") replaced synchronize_rcu() in bcm_delete_rx_op()
with call_rcu() and introduced the RX_NO_AUTOTIMER flag.
However, this flag check was omitted for thrtimer in the packet rx
fast-path. During BCM RX operation teardown, a concurrent RCU reader
(bcm_rx_handler) can race and re-arm thrtimer via
bcm_rx_update_and_send() after call_rcu() has been scheduled. Once
the RCU grace period elapses, bcm_op is freed. The subsequently
firing thrtimer then dereferences the deallocated op, causing a UAF.
Adding flag checks to the rx fast-path (bcm_rx_update_and_send) does not
fully close the TOCTOU race and introduces latency for every CAN frame.
Conversely, calling hrtimer_cancel() directly inside the RCU callback
(softirq context) is fatal as hrtimer_cancel() can sleep, triggering
a "scheduling while atomic" panic.
Resolve this by deferring the timer cancellation and memory free to a
dedicated unbound workqueue (bcm_wq). The RCU callback now queues a
work item to bcm_wq, which safely cancels both timers and deallocates
memory in sleepable process context. A dedicated workqueue is used to
prevent system-wide WQ saturation and is cleanly flushed/destroyed
on module unload to avoid rmmod page faults.
Since the deferred work can now outlive the calling context by an
unbounded amount, also take a reference on op->sk when it is assigned
and drop it only once the deferred work has cancelled both timers, so a
socket can no longer be freed out from under a still-armed timer whose
callback (bcm_send_to_user()) dereferences op->sk.
Fixes: f1b4e32 ("can: bcm: use call_rcu() instead of costly synchronize_rcu()")
Tested-by: Feng Xue <feng.xue@outlook.com>
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Lee Jones <lee@kernel.org>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20260714-bcm_fixes-v15-1-562f7e3e42da@hartkopp.net
Cc: stable@kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>1 parent d837620 commit 68973f9
1 file changed
Lines changed: 34 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| |||
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| 96 | + | |
| 97 | + | |
95 | 98 | | |
96 | 99 | | |
97 | 100 | | |
| |||
105 | 108 | | |
106 | 109 | | |
107 | 110 | | |
| 111 | + | |
108 | 112 | | |
109 | 113 | | |
110 | 114 | | |
| |||
793 | 797 | | |
794 | 798 | | |
795 | 799 | | |
796 | | - | |
| 800 | + | |
797 | 801 | | |
798 | | - | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
799 | 806 | | |
800 | 807 | | |
801 | 808 | | |
802 | 809 | | |
803 | 810 | | |
804 | 811 | | |
805 | 812 | | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
806 | 819 | | |
807 | 820 | | |
808 | 821 | | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
809 | 830 | | |
810 | 831 | | |
811 | 832 | | |
| |||
1060 | 1081 | | |
1061 | 1082 | | |
1062 | 1083 | | |
| 1084 | + | |
1063 | 1085 | | |
1064 | 1086 | | |
1065 | 1087 | | |
| |||
1221 | 1243 | | |
1222 | 1244 | | |
1223 | 1245 | | |
| 1246 | + | |
1224 | 1247 | | |
1225 | 1248 | | |
1226 | 1249 | | |
| |||
1839 | 1862 | | |
1840 | 1863 | | |
1841 | 1864 | | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
1842 | 1869 | | |
1843 | 1870 | | |
1844 | 1871 | | |
1845 | 1872 | | |
1846 | | - | |
| 1873 | + | |
1847 | 1874 | | |
1848 | 1875 | | |
1849 | 1876 | | |
| |||
1861 | 1888 | | |
1862 | 1889 | | |
1863 | 1890 | | |
| 1891 | + | |
| 1892 | + | |
1864 | 1893 | | |
1865 | 1894 | | |
1866 | 1895 | | |
| |||
1869 | 1898 | | |
1870 | 1899 | | |
1871 | 1900 | | |
| 1901 | + | |
| 1902 | + | |
1872 | 1903 | | |
1873 | 1904 | | |
1874 | 1905 | | |
| |||
0 commit comments