]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/5.0.10/coredump-fix-race-condition-between-mmget_not_zero-get_task_mm-and-core-dumping.patch
Fixes for 4.19
[thirdparty/kernel/stable-queue.git] / releases / 5.0.10 / coredump-fix-race-condition-between-mmget_not_zero-get_task_mm-and-core-dumping.patch
1 From 04f5866e41fb70690e28397487d8bd8eea7d712a Mon Sep 17 00:00:00 2001
2 From: Andrea Arcangeli <aarcange@redhat.com>
3 Date: Thu, 18 Apr 2019 17:50:52 -0700
4 Subject: coredump: fix race condition between mmget_not_zero()/get_task_mm() and core dumping
5
6 From: Andrea Arcangeli <aarcange@redhat.com>
7
8 commit 04f5866e41fb70690e28397487d8bd8eea7d712a upstream.
9
10 The core dumping code has always run without holding the mmap_sem for
11 writing, despite that is the only way to ensure that the entire vma
12 layout will not change from under it. Only using some signal
13 serialization on the processes belonging to the mm is not nearly enough.
14 This was pointed out earlier. For example in Hugh's post from Jul 2017:
15
16 https://lkml.kernel.org/r/alpine.LSU.2.11.1707191716030.2055@eggly.anvils
17
18 "Not strictly relevant here, but a related note: I was very surprised
19 to discover, only quite recently, how handle_mm_fault() may be called
20 without down_read(mmap_sem) - when core dumping. That seems a
21 misguided optimization to me, which would also be nice to correct"
22
23 In particular because the growsdown and growsup can move the
24 vm_start/vm_end the various loops the core dump does around the vma will
25 not be consistent if page faults can happen concurrently.
26
27 Pretty much all users calling mmget_not_zero()/get_task_mm() and then
28 taking the mmap_sem had the potential to introduce unexpected side
29 effects in the core dumping code.
30
31 Adding mmap_sem for writing around the ->core_dump invocation is a
32 viable long term fix, but it requires removing all copy user and page
33 faults and to replace them with get_dump_page() for all binary formats
34 which is not suitable as a short term fix.
35
36 For the time being this solution manually covers the places that can
37 confuse the core dump either by altering the vma layout or the vma flags
38 while it runs. Once ->core_dump runs under mmap_sem for writing the
39 function mmget_still_valid() can be dropped.
40
41 Allowing mmap_sem protected sections to run in parallel with the
42 coredump provides some minor parallelism advantage to the swapoff code
43 (which seems to be safe enough by never mangling any vma field and can
44 keep doing swapins in parallel to the core dumping) and to some other
45 corner case.
46
47 In order to facilitate the backporting I added "Fixes: 86039bd3b4e6"
48 however the side effect of this same race condition in /proc/pid/mem
49 should be reproducible since before 2.6.12-rc2 so I couldn't add any
50 other "Fixes:" because there's no hash beyond the git genesis commit.
51
52 Because find_extend_vma() is the only location outside of the process
53 context that could modify the "mm" structures under mmap_sem for
54 reading, by adding the mmget_still_valid() check to it, all other cases
55 that take the mmap_sem for reading don't need the new check after
56 mmget_not_zero()/get_task_mm(). The expand_stack() in page fault
57 context also doesn't need the new check, because all tasks under core
58 dumping are frozen.
59
60 Link: http://lkml.kernel.org/r/20190325224949.11068-1-aarcange@redhat.com
61 Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization")
62 Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
63 Reported-by: Jann Horn <jannh@google.com>
64 Suggested-by: Oleg Nesterov <oleg@redhat.com>
65 Acked-by: Peter Xu <peterx@redhat.com>
66 Reviewed-by: Mike Rapoport <rppt@linux.ibm.com>
67 Reviewed-by: Oleg Nesterov <oleg@redhat.com>
68 Reviewed-by: Jann Horn <jannh@google.com>
69 Acked-by: Jason Gunthorpe <jgg@mellanox.com>
70 Acked-by: Michal Hocko <mhocko@suse.com>
71 Cc: <stable@vger.kernel.org>
72 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
73 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
74 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
75
76 ---
77 drivers/infiniband/core/uverbs_main.c | 3 +++
78 fs/proc/task_mmu.c | 18 ++++++++++++++++++
79 fs/userfaultfd.c | 9 +++++++++
80 include/linux/sched/mm.h | 21 +++++++++++++++++++++
81 mm/mmap.c | 7 ++++++-
82 5 files changed, 57 insertions(+), 1 deletion(-)
83
84 --- a/drivers/infiniband/core/uverbs_main.c
85 +++ b/drivers/infiniband/core/uverbs_main.c
86 @@ -992,6 +992,8 @@ void uverbs_user_mmap_disassociate(struc
87 * will only be one mm, so no big deal.
88 */
89 down_write(&mm->mmap_sem);
90 + if (!mmget_still_valid(mm))
91 + goto skip_mm;
92 mutex_lock(&ufile->umap_lock);
93 list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
94 list) {
95 @@ -1006,6 +1008,7 @@ void uverbs_user_mmap_disassociate(struc
96 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
97 }
98 mutex_unlock(&ufile->umap_lock);
99 + skip_mm:
100 up_write(&mm->mmap_sem);
101 mmput(mm);
102 }
103 --- a/fs/proc/task_mmu.c
104 +++ b/fs/proc/task_mmu.c
105 @@ -1141,6 +1141,24 @@ static ssize_t clear_refs_write(struct f
106 count = -EINTR;
107 goto out_mm;
108 }
109 + /*
110 + * Avoid to modify vma->vm_flags
111 + * without locked ops while the
112 + * coredump reads the vm_flags.
113 + */
114 + if (!mmget_still_valid(mm)) {
115 + /*
116 + * Silently return "count"
117 + * like if get_task_mm()
118 + * failed. FIXME: should this
119 + * function have returned
120 + * -ESRCH if get_task_mm()
121 + * failed like if
122 + * get_proc_task() fails?
123 + */
124 + up_write(&mm->mmap_sem);
125 + goto out_mm;
126 + }
127 for (vma = mm->mmap; vma; vma = vma->vm_next) {
128 vma->vm_flags &= ~VM_SOFTDIRTY;
129 vma_set_page_prot(vma);
130 --- a/fs/userfaultfd.c
131 +++ b/fs/userfaultfd.c
132 @@ -629,6 +629,8 @@ static void userfaultfd_event_wait_compl
133
134 /* the various vma->vm_userfaultfd_ctx still points to it */
135 down_write(&mm->mmap_sem);
136 + /* no task can run (and in turn coredump) yet */
137 + VM_WARN_ON(!mmget_still_valid(mm));
138 for (vma = mm->mmap; vma; vma = vma->vm_next)
139 if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
140 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
141 @@ -883,6 +885,8 @@ static int userfaultfd_release(struct in
142 * taking the mmap_sem for writing.
143 */
144 down_write(&mm->mmap_sem);
145 + if (!mmget_still_valid(mm))
146 + goto skip_mm;
147 prev = NULL;
148 for (vma = mm->mmap; vma; vma = vma->vm_next) {
149 cond_resched();
150 @@ -905,6 +909,7 @@ static int userfaultfd_release(struct in
151 vma->vm_flags = new_flags;
152 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
153 }
154 +skip_mm:
155 up_write(&mm->mmap_sem);
156 mmput(mm);
157 wakeup:
158 @@ -1333,6 +1338,8 @@ static int userfaultfd_register(struct u
159 goto out;
160
161 down_write(&mm->mmap_sem);
162 + if (!mmget_still_valid(mm))
163 + goto out_unlock;
164 vma = find_vma_prev(mm, start, &prev);
165 if (!vma)
166 goto out_unlock;
167 @@ -1520,6 +1527,8 @@ static int userfaultfd_unregister(struct
168 goto out;
169
170 down_write(&mm->mmap_sem);
171 + if (!mmget_still_valid(mm))
172 + goto out_unlock;
173 vma = find_vma_prev(mm, start, &prev);
174 if (!vma)
175 goto out_unlock;
176 --- a/include/linux/sched/mm.h
177 +++ b/include/linux/sched/mm.h
178 @@ -49,6 +49,27 @@ static inline void mmdrop(struct mm_stru
179 __mmdrop(mm);
180 }
181
182 +/*
183 + * This has to be called after a get_task_mm()/mmget_not_zero()
184 + * followed by taking the mmap_sem for writing before modifying the
185 + * vmas or anything the coredump pretends not to change from under it.
186 + *
187 + * NOTE: find_extend_vma() called from GUP context is the only place
188 + * that can modify the "mm" (notably the vm_start/end) under mmap_sem
189 + * for reading and outside the context of the process, so it is also
190 + * the only case that holds the mmap_sem for reading that must call
191 + * this function. Generally if the mmap_sem is hold for reading
192 + * there's no need of this check after get_task_mm()/mmget_not_zero().
193 + *
194 + * This function can be obsoleted and the check can be removed, after
195 + * the coredump code will hold the mmap_sem for writing before
196 + * invoking the ->core_dump methods.
197 + */
198 +static inline bool mmget_still_valid(struct mm_struct *mm)
199 +{
200 + return likely(!mm->core_state);
201 +}
202 +
203 /**
204 * mmget() - Pin the address space associated with a &struct mm_struct.
205 * @mm: The address space to pin.
206 --- a/mm/mmap.c
207 +++ b/mm/mmap.c
208 @@ -45,6 +45,7 @@
209 #include <linux/moduleparam.h>
210 #include <linux/pkeys.h>
211 #include <linux/oom.h>
212 +#include <linux/sched/mm.h>
213
214 #include <linux/uaccess.h>
215 #include <asm/cacheflush.h>
216 @@ -2526,7 +2527,8 @@ find_extend_vma(struct mm_struct *mm, un
217 vma = find_vma_prev(mm, addr, &prev);
218 if (vma && (vma->vm_start <= addr))
219 return vma;
220 - if (!prev || expand_stack(prev, addr))
221 + /* don't alter vm_end if the coredump is running */
222 + if (!prev || !mmget_still_valid(mm) || expand_stack(prev, addr))
223 return NULL;
224 if (prev->vm_flags & VM_LOCKED)
225 populate_vma_page_range(prev, addr, prev->vm_end, NULL);
226 @@ -2552,6 +2554,9 @@ find_extend_vma(struct mm_struct *mm, un
227 return vma;
228 if (!(vma->vm_flags & VM_GROWSDOWN))
229 return NULL;
230 + /* don't alter vm_start if the coredump is running */
231 + if (!mmget_still_valid(mm))
232 + return NULL;
233 start = vma->vm_start;
234 if (expand_stack(vma, addr))
235 return NULL;