Commit 28aaa9c
committed
kthread: consolidate kthread exit paths to prevent use-after-free
Guillaume reported crashes via corrupted RCU callback function pointers
during KUnit testing. The crash was traced back to the pidfs rhashtable
conversion which replaced the 24-byte rb_node with an 8-byte rhash_head
in struct pid, shrinking it from 160 to 144 bytes.
struct kthread (without CONFIG_BLK_CGROUP) is also 144 bytes. With
CONFIG_SLAB_MERGE_DEFAULT and SLAB_HWCACHE_ALIGN both round up to
192 bytes and share the same slab cache. struct pid.rcu.func and
struct kthread.affinity_node both sit at offset 0x78.
When a kthread exits via make_task_dead() it bypasses kthread_exit() and
misses the affinity_node cleanup. free_kthread_struct() frees the memory
while the node is still linked into the global kthread_affinity_list. A
subsequent list_del() by another kthread writes through dangling list
pointers into the freed and reused memory, corrupting the pid's
rcu.func pointer.
Instead of patching free_kthread_struct() to handle the missed cleanup,
consolidate all kthread exit paths. Turn kthread_exit() into a macro
that calls do_exit() and add kthread_do_exit() which is called from
do_exit() for any task with PF_KTHREAD set. This guarantees that
kthread-specific cleanup always happens regardless of the exit path -
make_task_dead(), direct do_exit(), or kthread_exit().
Replace __to_kthread() with a new tsk_is_kthread() accessor in the
public header. Export do_exit() since module code using the
kthread_exit() macro now needs it directly.
Reported-by: Guillaume Tucker <[email protected]>
Tested-by: Guillaume Tucker <[email protected]>
Tested-by: Mark Brown <[email protected]>
Tested-by: David Gow <[email protected]>
Cc: <[email protected]>
Link: https://lore.kernel.org/all/20260224-mittlerweile-besessen-2738831ae7f6@brauner
Co-developed-by: Linus Torvalds <[email protected]>
Fixes: 4d13f43 ("kthread: Implement preferred affinity")
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Christian Brauner <[email protected]>1 parent cd3c877 commit 28aaa9c
3 files changed
Lines changed: 31 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
10 | 28 | | |
11 | 29 | | |
12 | 30 | | |
| |||
98 | 116 | | |
99 | 117 | | |
100 | 118 | | |
101 | | - | |
| 119 | + | |
102 | 120 | | |
103 | 121 | | |
| 122 | + | |
104 | 123 | | |
105 | 124 | | |
106 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
896 | 896 | | |
897 | 897 | | |
898 | 898 | | |
| 899 | + | |
899 | 900 | | |
900 | 901 | | |
901 | 902 | | |
902 | 903 | | |
903 | 904 | | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
904 | 909 | | |
905 | 910 | | |
906 | 911 | | |
| |||
1013 | 1018 | | |
1014 | 1019 | | |
1015 | 1020 | | |
| 1021 | + | |
1016 | 1022 | | |
1017 | 1023 | | |
1018 | 1024 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | 88 | | |
107 | 89 | | |
108 | 90 | | |
| |||
193 | 175 | | |
194 | 176 | | |
195 | 177 | | |
196 | | - | |
| 178 | + | |
197 | 179 | | |
198 | 180 | | |
199 | 181 | | |
| |||
234 | 216 | | |
235 | 217 | | |
236 | 218 | | |
237 | | - | |
| 219 | + | |
238 | 220 | | |
239 | 221 | | |
240 | 222 | | |
| |||
266 | 248 | | |
267 | 249 | | |
268 | 250 | | |
269 | | - | |
| 251 | + | |
270 | 252 | | |
271 | 253 | | |
272 | 254 | | |
| |||
309 | 291 | | |
310 | 292 | | |
311 | 293 | | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
| 294 | + | |
323 | 295 | | |
324 | | - | |
325 | 296 | | |
326 | 297 | | |
327 | 298 | | |
| |||
333 | 304 | | |
334 | 305 | | |
335 | 306 | | |
336 | | - | |
337 | 307 | | |
338 | | - | |
339 | 308 | | |
340 | 309 | | |
341 | 310 | | |
| |||
683 | 652 | | |
684 | 653 | | |
685 | 654 | | |
686 | | - | |
| 655 | + | |
687 | 656 | | |
688 | 657 | | |
689 | 658 | | |
| |||
0 commit comments