linux/drivers/char
Lorenzo Stoakes (ARM) d3ed3a4095 drivers/char/mem: mmap readonly MAP_SHARED-/dev/zero correctly
Rather surprisingly, opening /dev/zero read-only then mmap()'ing it
MAP_SHARED gets you true anonymous memory (albeit in a VMA with non-NULL
vma->vm_file).

This is a by-product of MAP_PRIVATE-/dev/zero being how anonymous memory
was mapped in Linux's distant past.

It happens because mmap_zero_prepare() gates on VMA_SHARED_BIT and when
mapping a read-only file MAP_SHARED, do_mmap() clears VMA_SHARED_BIT and
VMA_MAYWRITE_BIT.

The gating is incorrect - the (poorly named) VMA_MAYSHARE_BIT flag exists
explicitly to tell you if something was originally mapped MAP_SHARED.

So the fix is simple - gate on this instead.

This isn't exactly a common use case, but it's unexpected behaviour which
now causes an assert if CONFIG_DEBUG_VM is set.

While this bug has existed since the dawn of time for linux (or at least
since 2.6.12), it hasn't caused issues in the past, so while it's
incorrect behaviour, it doesn't seem necessary to backport that far.

The mapping is now accounted at mmap time and can fail with -ENOMEM under
strict overcommit, and read faults allocate folios.  However this is
normal behaviour for a read-only shmem mapping.

Commit 93c0c8dc87 ("mm/rmap: use anon pgoff to track MAP_PRIVATE
file-backed anon folios") is the first patch at which the debug assert
fires, so target that instead.

Link: https://lore.kernel.org/20260924-fix-dev-zero-readonly-shared-v1-1-153c2111e323@kernel.org
Fixes: 93c0c8dc87 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios")
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: <syzbot+c181d3198e98f8aef8b9@syzkaller.appspotmail.com>
Closes: https://lore.kernel.org/linux-mm/6ab4ae75.80e1c6cc.1e8e5f.000d.GAE@google.com/
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Jann Horn <jannh@google.com>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Lance Yang <lance.yang@linux.dev>
2026-10-06 16:44:18 -07:00
..
agp agp/amd-k7: Don't rely on address of pci_device_id 2026-07-28 11:18:24 -05:00
hw_random This update includes the following changes: 2026-08-19 17:25:42 -07:00
ipmi ipmi: Fix use-after-free of cmd_rcvr in _ipmi_destroy_user() 2026-08-25 18:55:02 -05:00
tpm tpm: Disable TPM on null key name mismatch 2026-09-29 22:15:21 +03:00
xilinx_hwicap char: xilinx_hwicap: unregister class on init errors 2026-07-17 16:22:57 +02:00
xillybus char: xillybus: Remove redundant dev_err() 2026-07-17 16:23:18 +02:00
adi.c char/adi: Use min_t(size_t,,) in adi_read() + adi_write() 2025-10-22 07:53:49 +02:00
apm-emulation.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
bsr.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
ds1620.c
dsp56k.c
hangcheck-timer.c hangcheck-timer: fix coding style spacing 2025-11-26 15:11:15 +01:00
hpet.c hpet: Check ACPI_COMPANION() against NULL at probe time 2026-05-23 13:47:34 +02:00
Kconfig char: applicom: remove low-quality, unused driver 2026-05-23 13:47:34 +02:00
lp.c
Makefile char: applicom: remove low-quality, unused driver 2026-05-23 13:47:34 +02:00
mem.c drivers/char/mem: mmap readonly MAP_SHARED-/dev/zero correctly 2026-10-06 16:44:18 -07:00
misc_minor_kunit.c initramfs_test: use test init/exit hooks to override init fs 2026-07-31 12:56:51 +02:00
misc.c char: Use list_del_init() in misc_deregister() to reinitialize list pointer 2025-09-06 19:43:18 +02:00
nsc_gpio.c char: remove unnecessary module_init/exit functions 2026-04-02 17:05:53 +02:00
nvram.c char/nvram: Remove redundant nvram_mutex 2026-05-23 13:47:34 +02:00
nwbutton.c
nwbutton.h
nwflash.c
pc8736x_gpio.c
powernv-op-panel.c char: powernv-op-panel: remove unnecessary reset of position pointer 2026-07-17 15:53:21 +02:00
ppdev.c ppdev: prevent overflow when setting port timeout 2026-07-17 15:53:18 +02:00
ps3flash.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
random.c random: vDSO: Remove ifdeffery 2026-03-11 15:22:49 +01:00
scx200_gpio.c
sonypi.c sonypi: Stop setting acpi_device_name/class() 2026-07-09 16:44:07 +03:00
tlclk.c tlclk: if sscanf() fails, fall back to 0, not random value 2026-07-17 14:53:45 +02:00
toshiba.c
ttyprintk.c
uv_mmtimer.c
virtio_console.c virtio_console: do not free control-out buffers on remove 2026-09-07 18:54:02 -04:00