Commit 43683bb
bpf: fix mm lifecycle in open-coded task_vma iterator
[ Upstream commit d8e27d2 ]
The open-coded task_vma iterator reads task->mm locklessly and acquires
mmap_read_trylock() but never calls mmget(). If the task exits
concurrently, the mm_struct can be freed as it is not
SLAB_TYPESAFE_BY_RCU, resulting in a use-after-free.
Safely read task->mm with a trylock on alloc_lock and acquire an mm
reference. Drop the reference via bpf_iter_mmput_async() in _destroy()
and error paths. bpf_iter_mmput_async() is a local wrapper around
mmput_async() with a fallback to mmput() on !CONFIG_MMU.
Reject irqs-disabled contexts (including NMI) up front. Operations used
by _next() and _destroy() (mmap_read_unlock, bpf_iter_mmput_async)
take spinlocks with IRQs disabled (pool->lock, pi_lock). Running from
NMI or from a tracepoint that fires with those locks held could
deadlock.
A trylock on alloc_lock is used instead of the blocking task_lock()
(get_task_mm) to avoid a deadlock when a softirq BPF program iterates
a task that already holds its alloc_lock on the same CPU.
Fixes: 4ac4546 ("bpf: Introduce task_vma open-coded iterator kfuncs")
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20260408154539.3832150-2-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>1 parent 4e70ba1 commit 43683bb
1 file changed
Lines changed: 51 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
794 | 795 | | |
795 | 796 | | |
796 | 797 | | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
797 | 807 | | |
798 | 808 | | |
799 | 809 | | |
| |||
825 | 835 | | |
826 | 836 | | |
827 | 837 | | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
828 | 856 | | |
829 | 857 | | |
830 | 858 | | |
| |||
834 | 862 | | |
835 | 863 | | |
836 | 864 | | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
837 | 878 | | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
838 | 884 | | |
839 | 885 | | |
840 | 886 | | |
| |||
844 | 890 | | |
845 | 891 | | |
846 | 892 | | |
847 | | - | |
| 893 | + | |
848 | 894 | | |
849 | 895 | | |
850 | 896 | | |
851 | 897 | | |
852 | 898 | | |
| 899 | + | |
| 900 | + | |
853 | 901 | | |
854 | | - | |
855 | | - | |
| 902 | + | |
856 | 903 | | |
857 | 904 | | |
858 | 905 | | |
| |||
875 | 922 | | |
876 | 923 | | |
877 | 924 | | |
| 925 | + | |
878 | 926 | | |
879 | 927 | | |
880 | 928 | | |
| |||
0 commit comments