linux-brain/arch/x86/mm
Mike Rapoport 08f33350ed x86/mm: Fix kern_addr_valid() to cope with existing but not present entries
commit 34b1999da935a33be6239226bfa6cd4f704c5c88 upstream.

Jiri Olsa reported a fault when running:

  # cat /proc/kallsyms | grep ksys_read
  ffffffff8136d580 T ksys_read
  # objdump -d --start-address=0xffffffff8136d580 --stop-address=0xffffffff8136d590 /proc/kcore

  /proc/kcore:     file format elf64-x86-64

  Segmentation fault

  general protection fault, probably for non-canonical address 0xf887ffcbff000: 0000 [#1] SMP PTI
  CPU: 12 PID: 1079 Comm: objdump Not tainted 5.14.0-rc5qemu+ #508
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-4.fc34 04/01/2014
  RIP: 0010:kern_addr_valid
  Call Trace:
   read_kcore
   ? rcu_read_lock_sched_held
   ? rcu_read_lock_sched_held
   ? rcu_read_lock_sched_held
   ? trace_hardirqs_on
   ? rcu_read_lock_sched_held
   ? lock_acquire
   ? lock_acquire
   ? rcu_read_lock_sched_held
   ? lock_acquire
   ? rcu_read_lock_sched_held
   ? rcu_read_lock_sched_held
   ? rcu_read_lock_sched_held
   ? lock_release
   ? _raw_spin_unlock
   ? __handle_mm_fault
   ? rcu_read_lock_sched_held
   ? lock_acquire
   ? rcu_read_lock_sched_held
   ? lock_release
   proc_reg_read
   ? vfs_read
   vfs_read
   ksys_read
   do_syscall_64
   entry_SYSCALL_64_after_hwframe

The fault happens because kern_addr_valid() dereferences existent but not
present PMD in the high kernel mappings.

Such PMDs are created when free_kernel_image_pages() frees regions larger
than 2Mb. In this case, a part of the freed memory is mapped with PMDs and
the set_memory_np_noalias() -> ... -> __change_page_attr() sequence will
mark the PMD as not present rather than wipe it completely.

Have kern_addr_valid() check whether higher level page table entries are
present before trying to dereference them to fix this issue and to avoid
similar issues in the future.

Stable backporting note:
------------------------

Note that the stable marking is for all active stable branches because
there could be cases where pagetable entries exist but are not valid -
see 9a14aefc1d ("x86: cpa, fix lookup_address"), for example. So make
sure to be on the safe side here and use pXY_present() accessors rather
than pXY_none() which could #GP when accessing pages in the direct map.

Also see:

  c40a56a781 ("x86/mm/init: Remove freed kernel image areas from alias mapping")

for more info.

Reported-by: Jiri Olsa <jolsa@redhat.com>
Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Reviewed-by: David Hildenbrand <david@redhat.com>
Acked-by: Dave Hansen <dave.hansen@intel.com>
Tested-by: Jiri Olsa <jolsa@redhat.com>
Cc: <stable@vger.kernel.org>	# 4.4+
Link: https://lkml.kernel.org/r/20210819132717.19358-1-rppt@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-09-22 12:26:40 +02:00
..
Makefile treewide: prefix header search paths with $(srctree)/ 2019-05-18 11:49:57 +09:00
amdtopology.c mm: remove include/linux/bootmem.h 2018-10-31 08:54:16 -07:00
cpu_entry_area.c x86/pti/32: Calculate the various PTI cpu_entry_area sizes correctly, make the CPU_ENTRY_AREA_PAGES assert precise 2019-11-29 10:10:00 +01:00
debug_pagetables.c x86: mm: no need to check return value of debugfs_create functions 2019-06-03 16:18:12 +02:00
dump_pagetables.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
extable.c treewide: Add SPDX license identifier for missed files 2019-05-21 10:50:45 +02:00
fault.c x86/mm: split vmalloc_sync_all() 2020-03-25 08:25:58 +01:00
highmem_32.c treewide: Add SPDX license identifier for missed files 2019-05-21 10:50:45 +02:00
hugetlbpage.c mm: simplify MEMORY_ISOLATION && COMPACTION || CMA into CONTIG_ALLOC 2019-05-14 09:47:47 -07:00
ident_map.c x86/mm/ident_map: Check for errors from ident_pud_init() 2020-12-30 11:51:02 +01:00
init.c x86/mm: Stop printing BRK addresses 2020-06-22 09:31:08 +02:00
init_32.c mm/memory_hotplug: shrink zones when offlining memory 2020-01-09 10:19:56 +01:00
init_64.c x86/mm: Fix kern_addr_valid() to cope with existing but not present entries 2021-09-22 12:26:40 +02:00
iomap_32.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
ioremap.c x86/ioremap: Fix CONFIG_EFI=n build 2020-04-01 11:01:59 +02:00
kasan_init_64.c x86/kasan: Fix boot with 5-level paging and KASAN 2019-06-14 16:37:30 +02:00
kaslr.c x86/mm/KASLR: Compute the size of the vmemmap section properly 2019-06-07 23:12:13 +02:00
kmmio.c x86/mm/kmmio: Make the tracer robust against L1TF 2018-08-08 22:28:34 +02:00
mem_encrypt.c x86/mem_encrypt: Correct physical address calculation in __set_clr_pte_enc() 2021-03-30 14:35:29 +02:00
mem_encrypt_boot.S treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
mem_encrypt_identity.c x86/mm/mem_encrypt: Fix definition of PMD_FLAGS_DEC_WP 2020-12-16 10:56:58 +01:00
mm_internal.h x86/mm: Do not use set_{pud, pmd}_safe() when splitting a large page 2019-05-08 19:08:35 +02:00
mmap.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
mmio-mod.c x86/mmiotrace: Use cpumask_available() for cpumask_var_t variables 2020-06-07 13:18:51 +02:00
mpx.c signal: Remove task parameter from force_sig 2019-05-27 09:36:28 -05:00
numa.c x86/mm: Fix cpumask_of_node() error condition 2019-09-05 13:03:04 +02:00
numa_32.c mm: remove include/linux/bootmem.h 2018-10-31 08:54:16 -07:00
numa_64.c mm: remove include/linux/bootmem.h 2018-10-31 08:54:16 -07:00
numa_emulation.c x86, fakenuma: Fix invalid starting node ID 2020-09-09 19:12:28 +02:00
numa_internal.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
pageattr-test.c x86/mm/cpa: Simplify the code after making cpa->vaddr invariant 2018-12-17 18:54:25 +01:00
pageattr.c x86/mm/cpa: Flush direct map alias during cpa 2020-05-27 17:46:32 +02:00
pat.c x86: fix seq_file iteration for pat/memtype.c 2021-03-04 10:26:48 +01:00
pat_internal.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
pat_rbtree.c augmented rbtree: add new RB_DECLARE_CALLBACKS_MAX macro 2019-09-25 17:51:39 -07:00
pf_in.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
pf_in.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
pgtable.c x86/mm: Fix leak of pmd ptlock 2021-01-12 20:16:22 +01:00
pgtable_32.c x86/mm: Rename flush_tlb_single() and flush_tlb_one() to __flush_tlb_one_[user|kernel]() 2018-02-15 01:15:52 +01:00
physaddr.c mm: remove include/linux/bootmem.h 2018-10-31 08:54:16 -07:00
physaddr.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
pkeys.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 288 2019-06-05 17:36:37 +02:00
pti.c x86/mm/pti: Do not invoke PTI functions when PTI is disabled 2019-08-29 20:52:53 +02:00
setup_nx.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
srat.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
testmmiotrace.c x86/mmiotrace: Lock down the testmmiotrace module 2019-08-19 21:54:16 -07:00
tlb.c x86/membarrier: Get rid of a dubious optimization 2020-12-16 10:56:59 +01:00