Skip to content

Commit 578d769

Browse files
lopsided98akpm00
authored andcommitted
proc: nommu: /proc/<pid>/maps: release mmap read lock
The no-MMU implementation of /proc/<pid>/map doesn't normally release the mmap read lock, because it uses !IS_ERR_OR_NULL(_vml) to determine whether to release the lock. Since _vml is NULL when the end of the mappings is reached, the lock is not released. Reading /proc/1/maps twice doesn't cause a hang because it only takes the read lock, which can be taken multiple times and therefore doesn't show any problem if the lock isn't released. Instead, you need to perform some operation that attempts to take the write lock after reading /proc/<pid>/maps. To actually reproduce the bug, compile the following code as 'proc_maps_bug': #include <stdio.h> #include <unistd.h> #include <sys/mman.h> int main(int argc, char *argv[]) { void *buf; sleep(1); buf = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); puts("mmap returned"); return 0; } Then, run: ./proc_maps_bug &; cat /proc/$!/maps; fg Without this patch, mmap() will hang and the command will never complete. This code was incorrectly adapted from the MMU implementation, which at the time released the lock in m_next() before returning the last entry. The MMU implementation has diverged further from the no-MMU version since then, so this patch brings their locking and error handling into sync, fixing the bug and hopefully avoiding similar issues in the future. Link: https://lkml.kernel.org/r/20230914163019.4050530-2-ben.wolsieffer@hefring.com Fixes: 47fecca ("fs/proc/task_nommu.c: don't use priv->task->mm") Signed-off-by: Ben Wolsieffer <ben.wolsieffer@hefring.com> Acked-by: Oleg Nesterov <oleg@redhat.com> Cc: Giulio Benetti <giulio.benetti@benettiengineering.com> Cc: Greg Ungerer <gerg@uclinux.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 9ea9cb0 commit 578d769

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

fs/proc/task_nommu.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,16 @@ static void *m_start(struct seq_file *m, loff_t *pos)
192192
return ERR_PTR(-ESRCH);
193193

194194
mm = priv->mm;
195-
if (!mm || !mmget_not_zero(mm))
195+
if (!mm || !mmget_not_zero(mm)) {
196+
put_task_struct(priv->task);
197+
priv->task = NULL;
196198
return NULL;
199+
}
197200

198201
if (mmap_read_lock_killable(mm)) {
199202
mmput(mm);
203+
put_task_struct(priv->task);
204+
priv->task = NULL;
200205
return ERR_PTR(-EINTR);
201206
}
202207

@@ -205,23 +210,21 @@ static void *m_start(struct seq_file *m, loff_t *pos)
205210
if (vma)
206211
return vma;
207212

208-
mmap_read_unlock(mm);
209-
mmput(mm);
210213
return NULL;
211214
}
212215

213-
static void m_stop(struct seq_file *m, void *_vml)
216+
static void m_stop(struct seq_file *m, void *v)
214217
{
215218
struct proc_maps_private *priv = m->private;
219+
struct mm_struct *mm = priv->mm;
216220

217-
if (!IS_ERR_OR_NULL(_vml)) {
218-
mmap_read_unlock(priv->mm);
219-
mmput(priv->mm);
220-
}
221-
if (priv->task) {
222-
put_task_struct(priv->task);
223-
priv->task = NULL;
224-
}
221+
if (!priv->task)
222+
return;
223+
224+
mmap_read_unlock(mm);
225+
mmput(mm);
226+
put_task_struct(priv->task);
227+
priv->task = NULL;
225228
}
226229

227230
static void *m_next(struct seq_file *m, void *_p, loff_t *pos)

0 commit comments

Comments
 (0)