linux/fs/proc
Ben Wolsieffer b7a0df4c08 proc: nommu: /proc/<pid>/maps: release mmap read lock
commit 578d7699e5 upstream.

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: 47fecca15c ("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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-10-10 21:53:35 +02:00
..
array.c seccomp: Fix CONFIG tests for Seccomp_filters 2021-05-14 09:50:24 +02:00
base.c proc: fix a dentry lock race between release_task and lookup 2023-09-23 11:01:10 +02:00
bootconfig.c proc: bootconfig: Add null pointer check 2022-04-08 14:40:42 +02:00
cmdline.c
consoles.c
cpuinfo.c proc/cpuinfo: switch to ->read_iter 2020-11-06 10:05:18 -08:00
devices.c block: move block-related definitions out of fs.h 2020-06-24 09:16:02 -06:00
fd.c
fd.h
generic.c proc: fix dentry/inode overinstantiating under /proc/${pid}/net 2022-06-09 10:21:17 +02:00
inode.c proc: wire up generic_file_splice_read for iter ops 2020-11-06 10:05:18 -08:00
internal.h proc: fix lookup in /proc/net subdirectories after setns(2) 2020-12-30 11:53:56 +01:00
interrupts.c
Kconfig treewide: replace '---help---' in Kconfig files with 'help' 2020-06-14 01:57:21 +09:00
kcore.c arm: remove CONFIG_ARCH_HAS_HOLES_MEMORYMODEL 2022-05-15 20:00:09 +02:00
kmsg.c proc: faster open/read/close with "permanent" files 2020-04-07 10:43:42 -07:00
loadavg.c
Makefile
meminfo.c proc/meminfo: avoid open coded reading of vm_committed_as 2020-08-07 11:33:26 -07:00
namespaces.c
nommu.c mm: don't include asm/pgtable.h if linux/mm.h is already included 2020-06-09 09:39:13 -07:00
page.c mm: Add PG_arch_2 page flag 2020-09-04 12:46:06 +01:00
proc_net.c proc: fix dentry/inode overinstantiating under /proc/${pid}/net 2022-06-09 10:21:17 +02:00
proc_sysctl.c sysctl: move some boundary constants from sysctl.c to sysctl_vals 2023-06-28 10:28:09 +02:00
proc_tty.c
root.c proc: s_fs_info may be NULL when proc_kill_sb is called 2020-06-10 14:54:54 -05:00
self.c Revert "proc: don't allow async path resolution of /proc/self components" 2023-01-04 11:39:24 +01:00
softirqs.c
stat.c fs/proc/uptime.c: Fix idle time reporting in /proc/uptime 2021-11-18 14:03:56 +01:00
task_mmu.c mm: hugetlb: proc: check for hugetlb shared PMD in /proc/PID/smaps 2023-02-15 17:22:19 +01:00
task_nommu.c proc: nommu: /proc/<pid>/maps: release mmap read lock 2023-10-10 21:53:35 +02:00
thread_self.c Revert "proc: don't allow async path resolution of /proc/thread-self components" 2023-01-04 11:39:24 +01:00
uptime.c fs/proc/uptime.c: Fix idle time reporting in /proc/uptime 2021-11-18 14:03:56 +01:00
util.c
version.c
vmcore.c proc/vmcore: fix clearing user buffer by properly using clear_user() 2021-12-01 09:19:02 +01:00