Skip to content

Commit 6dd8b1a

Browse files
ctmarinaswilldeacon
authored andcommitted
arm64: mte: Dump the MTE tags in the core file
For each vma mapped with PROT_MTE (the VM_MTE flag set), generate a PT_ARM_MEMTAG_MTE segment in the core file and dump the corresponding tags. The in-file size for such segments is 128 bytes per page. For pages in a VM_MTE vma which are not present in the user page tables or don't have the PG_mte_tagged flag set (e.g. execute-only), just write zeros in the core file. An example of program headers for two vmas, one 2-page, the other 4-page long: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align ... LOAD 0x030000 0x0000ffff80034000 0x0000000000000000 0x000000 0x002000 RW 0x1000 LOAD 0x030000 0x0000ffff80036000 0x0000000000000000 0x004000 0x004000 RW 0x1000 ... LOPROC+0x1 0x05b000 0x0000ffff80034000 0x0000000000000000 0x000100 0x002000 0 LOPROC+0x1 0x05b100 0x0000ffff80036000 0x0000000000000000 0x000200 0x004000 0 Signed-off-by: Catalin Marinas <catalin.marinas@arm.com> Acked-by: Luis Machado <luis.machado@linaro.org> Reviewed-by: Mark Brown <broonie@kernel.org> Link: https://lore.kernel.org/r/20220131165456.2160675-5-catalin.marinas@arm.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent ab1e435 commit 6dd8b1a

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ config ARM64
1010
select ACPI_SPCR_TABLE if ACPI
1111
select ACPI_PPTT if ACPI
1212
select ARCH_HAS_DEBUG_WX
13+
select ARCH_BINFMT_ELF_EXTRA_PHDRS
1314
select ARCH_BINFMT_ELF_STATE
1415
select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE
1516
select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION

arch/arm64/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ obj-$(CONFIG_ARM64_ACPI_PARKING_PROTOCOL) += acpi_parking_protocol.o
6161
obj-$(CONFIG_PARAVIRT) += paravirt.o
6262
obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
6363
obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o
64+
obj-$(CONFIG_ELF_CORE) += elfcore.o
6465
obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \
6566
cpu-reset.o
6667
obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o kexec_image.o

arch/arm64/kernel/elfcore.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include <linux/coredump.h>
4+
#include <linux/elfcore.h>
5+
#include <linux/kernel.h>
6+
#include <linux/mm.h>
7+
8+
#include <asm/cpufeature.h>
9+
#include <asm/mte.h>
10+
11+
#define for_each_mte_vma(tsk, vma) \
12+
if (system_supports_mte()) \
13+
for (vma = tsk->mm->mmap; vma; vma = vma->vm_next) \
14+
if (vma->vm_flags & VM_MTE)
15+
16+
static unsigned long mte_vma_tag_dump_size(struct vm_area_struct *vma)
17+
{
18+
if (vma->vm_flags & VM_DONTDUMP)
19+
return 0;
20+
21+
return vma_pages(vma) * MTE_PAGE_TAG_STORAGE;
22+
}
23+
24+
/* Derived from dump_user_range(); start/end must be page-aligned */
25+
static int mte_dump_tag_range(struct coredump_params *cprm,
26+
unsigned long start, unsigned long end)
27+
{
28+
unsigned long addr;
29+
30+
for (addr = start; addr < end; addr += PAGE_SIZE) {
31+
char tags[MTE_PAGE_TAG_STORAGE];
32+
struct page *page = get_dump_page(addr);
33+
34+
/*
35+
* get_dump_page() returns NULL when encountering an empty
36+
* page table entry that would otherwise have been filled with
37+
* the zero page. Skip the equivalent tag dump which would
38+
* have been all zeros.
39+
*/
40+
if (!page) {
41+
dump_skip(cprm, MTE_PAGE_TAG_STORAGE);
42+
continue;
43+
}
44+
45+
/*
46+
* Pages mapped in user space as !pte_access_permitted() (e.g.
47+
* PROT_EXEC only) may not have the PG_mte_tagged flag set.
48+
*/
49+
if (!test_bit(PG_mte_tagged, &page->flags)) {
50+
put_page(page);
51+
dump_skip(cprm, MTE_PAGE_TAG_STORAGE);
52+
continue;
53+
}
54+
55+
mte_save_page_tags(page_address(page), tags);
56+
put_page(page);
57+
if (!dump_emit(cprm, tags, MTE_PAGE_TAG_STORAGE))
58+
return 0;
59+
}
60+
61+
return 1;
62+
}
63+
64+
Elf_Half elf_core_extra_phdrs(void)
65+
{
66+
struct vm_area_struct *vma;
67+
int vma_count = 0;
68+
69+
for_each_mte_vma(current, vma)
70+
vma_count++;
71+
72+
return vma_count;
73+
}
74+
75+
int elf_core_write_extra_phdrs(struct coredump_params *cprm, loff_t offset)
76+
{
77+
struct vm_area_struct *vma;
78+
79+
for_each_mte_vma(current, vma) {
80+
struct elf_phdr phdr;
81+
82+
phdr.p_type = PT_ARM_MEMTAG_MTE;
83+
phdr.p_offset = offset;
84+
phdr.p_vaddr = vma->vm_start;
85+
phdr.p_paddr = 0;
86+
phdr.p_filesz = mte_vma_tag_dump_size(vma);
87+
phdr.p_memsz = vma->vm_end - vma->vm_start;
88+
offset += phdr.p_filesz;
89+
phdr.p_flags = 0;
90+
phdr.p_align = 0;
91+
92+
if (!dump_emit(cprm, &phdr, sizeof(phdr)))
93+
return 0;
94+
}
95+
96+
return 1;
97+
}
98+
99+
size_t elf_core_extra_data_size(void)
100+
{
101+
struct vm_area_struct *vma;
102+
size_t data_size = 0;
103+
104+
for_each_mte_vma(current, vma)
105+
data_size += mte_vma_tag_dump_size(vma);
106+
107+
return data_size;
108+
}
109+
110+
int elf_core_write_extra_data(struct coredump_params *cprm)
111+
{
112+
struct vm_area_struct *vma;
113+
114+
for_each_mte_vma(current, vma) {
115+
if (vma->vm_flags & VM_DONTDUMP)
116+
continue;
117+
118+
if (!mte_dump_tag_range(cprm, vma->vm_start, vma->vm_end))
119+
return 0;
120+
}
121+
122+
return 1;
123+
}

0 commit comments

Comments
 (0)